Adists: BC: Update for scenario.

scenarios
Pierre-Antoine 2025-08-14 14:26:07 +02:00
parent 89a898410f
commit 05dcd3bcca
1 changed files with 55 additions and 21 deletions

View File

@ -147,27 +147,45 @@ class BoundaryConditionAdisTS(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
nodes = data['nodes']
scenario = data["scenario"]
loaded = data['loaded_pid']
table = execute(
"SELECT pamhyr_id, pollutant, type, node " +
"FROM boundary_condition_adists"
"SELECT pamhyr_id, pollutant, type, node, scenario " +
"FROM boundary_condition_adists " +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
it = iter(row)
pid = next(it)
pollutant = next(it)
bc_type = next(it)
node = next(it)
owner_scenario = next(it)
bc = cls(
id=row[0],
pollutant=row[1],
status=data['status']
id=pid,
pollutant=pollutant,
status=status,
owner_scenario=owner_scenario
)
bc.type = row[2]
bc.type = bc_type
bc.node = None
if row[3] != -1:
tmp = next(filter(
lambda n: n.id == row[3], data["nodes"]),
None)
tmp = next(
filter(
lambda n: n.id == node, nodes
),
None
)
if tmp is not None:
bc.node = tmp.id
else:
@ -176,7 +194,8 @@ class BoundaryConditionAdisTS(SQLSubModel):
values = execute(
"SELECT data0, data1 FROM " +
"boundary_condition_data_adists " +
f"WHERE bc = '{bc.id}'"
f"WHERE bc = '{bc.id}' " +
f"AND scenario = {scenario.id}"
)
# Write data
@ -186,40 +205,55 @@ class BoundaryConditionAdisTS(SQLSubModel):
# Replace data at pos ind
bc._data.append((data0, data1))
loaded.add(pid)
new.append(bc)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
if not self.must_be_saved():
return True
execute(f"DELETE FROM boundary_condition_adists WHERE id = {self.id}")
execute(f"DELETE FROM boundary_condition_data_adists" +
f" WHERE bc = {self.id}")
execute(
"DELETE FROM boundary_condition_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
execute(
"DELETE FROM boundary_condition_data_adists " +
f"WHERE bc = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
node = -1
if self._node is not None:
node = self._node
sql = (
execute(
"INSERT INTO " +
"boundary_condition_adists(id, pollutant, type, node) " +
"boundary_condition_adists(id, pollutant, type, " +
"node, scenario) " +
"VALUES (" +
f"{self.id}, {self._pollutant}, " +
f"'{self._db_format(self._type)}', {node}" +
f"'{self._db_format(self._type)}', {node}, " +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
for d in self._data:
data0 = self._db_format(str(d[0]))
data1 = self._db_format(str(d[1]))
sql = (
execute(
"INSERT INTO " +
"boundary_condition_data_adists(data0, data1, bc) " +
f"VALUES ('{data0}', {data1}, {self.id})"
"boundary_condition_data_adists(data0, data1, bc, scenario) " +
f"VALUES ('{data0}', {data1}, {self.id}, " +
f"{self._status.scenario_id})"
)
execute(sql)
return True