Compare commits

...

5 Commits

8 changed files with 399 additions and 194 deletions

View File

@ -147,27 +147,48 @@ 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']
if scenario is None:
return new
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 +197,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 +208,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

View File

@ -36,8 +36,7 @@ class D90AdisTS(SQLSubModel):
]
def __init__(self, id: int = -1, name: str = "default",
status=None,
owner_scenario=-1):
status=None, owner_scenario=-1):
super(D90AdisTS, self).__init__(
id=id, status=status,
owner_scenario=owner_scenario
@ -99,59 +98,82 @@ class D90AdisTS(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT pamhyr_id, name, d90, enabled " +
"FROM d90_adists"
"SELECT pamhyr_id, name, d90, enabled, scenario " +
"FROM d90_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:
d90_id = row[0]
name = row[1]
d90 = row[2]
enabled = (row[3] == 1)
it = iter(row)
D90 = cls(
d90_id = next(it)
name = next(it)
value_d90 = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
d90 = cls(
id=d90_id,
name=name,
status=data['status']
status=status,
owner_scenario=owner_scenario
)
D90.d90 = d90
D90.enabled = enabled
d90.d90 = value_d90
d90.enabled = enabled
data['d90_default_id'] = d90_id
D90._data = D90AdisTSSpec._db_load(execute, data)
d90._data = d90AdisTSSpec._db_load(execute, data)
new.append(D90)
loaded.add(pid)
new.append(d90)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
execute(f"DELETE FROM d90_adists WHERE pamhyr_id = {self.id}")
if not self.must_be_saved():
return True
execute(
f"DELETE FROM d90_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
d90 = -1.
if self.d90 is not None:
d90 = self.d90
sql = (
execute(
"INSERT INTO " +
"d90_adists(" +
"pamhyr_id, name, d90, enabled" +
"pamhyr_id, name, d90, enabled, scenario" +
") " +
"VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " +
f"{d90}, {self._enabled}" +
f"{d90}, {self._enabled}, {self._status.scenario_id}" +
")"
)
execute(sql)
data['d90_default_id'] = self.id
execute(
"DELETE FROM d90_spec " +
f"WHERE d90_default = {self.id}"
f"WHERE d90_default = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
for d90_spec in self._data:
@ -169,7 +191,7 @@ class D90AdisTS(SQLSubModel):
@name.setter
def name(self, name):
self._name = name
self._status.modified()
self.modified()
@property
def d90(self):
@ -178,7 +200,7 @@ class D90AdisTS(SQLSubModel):
@d90.setter
def d90(self, d90):
self._d90 = d90
self._status.modified()
self.modified()
@property
def enabled(self):
@ -187,12 +209,12 @@ class D90AdisTS(SQLSubModel):
@enabled.setter
def enabled(self, enabled):
self._enabled = enabled
self._status.modified()
self.modified()
def new(self, index):
n = D90AdisTSSpec(status=self._status)
self._data.insert(index, n)
self._status.modified()
self.modified()
return n
def delete(self, data):
@ -202,13 +224,13 @@ class D90AdisTS(SQLSubModel):
self._data
)
)
self._status.modified()
self.modified()
def delete_i(self, indexes):
for ind in indexes:
del self._data[ind]
self._status.modified()
self.modified()
def insert(self, index, data):
self._data.insert(index, data)
self._status.modified()
self.modified()

View File

@ -33,15 +33,11 @@ class D90AdisTSSpec(SQLSubModel):
_id_cnt = 0
def __init__(self, id: int = -1, name: str = "",
status=None):
super(D90AdisTSSpec, self).__init__()
self._status = status
if id == -1:
self.id = D90AdisTSSpec._id_cnt
else:
self.id = id
status=None, owner_scenario=-1):
super(D90AdisTSSpec, self).__init__(
id=id, status=status,
owner_scenario=owner_scenario
)
self._name_section = name
self._reach = None
@ -50,8 +46,6 @@ class D90AdisTSSpec(SQLSubModel):
self._d90 = None
self._enabled = True
D90AdisTSSpec._id_cnt = max(D90AdisTSSpec._id_cnt + 1, self.id)
@classmethod
def _db_create(cls, execute, ext=""):
execute(f"""
@ -108,28 +102,40 @@ class D90AdisTSSpec(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT id, d90_default, name, reach, start_rk, end_rk, " +
"d90, enabled " +
"FROM d90_spec " +
f"WHERE d90_default = {data['d90_default_id']} "
"SELECT id, name, reach, start_rk, end_rk, " +
"d90, enabled, scenario " +
"FROM d90_adists_spec " +
f"WHERE d90_default = {data['d90_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
id = row[0]
name = row[2]
reach = row[3]
start_rk = row[4]
end_rk = row[5]
d90 = row[6]
enabled = (row[7] == 1)
it = iter(row)
pid = next(it)
name = next(it)
reach = next(it)
start_rk = next(it)
end_rk = next(it)
d90 = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
new_spec = cls(
id=id,
id=pid,
name=name,
status=data['status']
status=status,
owner_scenario=owner_scenario
)
new_spec.reach = reach
@ -138,29 +144,34 @@ class D90AdisTSSpec(SQLSubModel):
new_spec.d90 = d90
new_spec.enabled = enabled
loaded.add(pid)
new.append(new_spec)
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
d90_default = data['d90_default_id']
sql = (
execute(
"INSERT INTO " +
"d90_spec(id, d90_default, name, reach, " +
"start_rk, end_rk, d90, enabled) " +
"d90_spec(pamhyr_id, d90_default, name, reach, " +
"start_rk, end_rk, d90, enabled, scenario) " +
"VALUES (" +
f"{self.id}, " +
f"{d90_default}, " +
f"{self.id}, {d90_default}, " +
f"'{self._db_format(self._name_section)}', " +
f"{self._reach}, " +
f"{self._start_rk}, " +
f"{self._end_rk}, " +
f"{self._d90}, " +
f"{self._enabled}" +
f"{self._start_rk}, {self._end_rk}, " +
f"{self._d90}, {self._enabled}" +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
return True
@ -171,7 +182,7 @@ class D90AdisTSSpec(SQLSubModel):
@name.setter
def name(self, name):
self._name_section = name
self._status.modified()
self.modified()
@property
def reach(self):
@ -180,7 +191,7 @@ class D90AdisTSSpec(SQLSubModel):
@reach.setter
def reach(self, reach):
self._reach = reach
self._status.modified()
self.modified()
@property
def start_rk(self):
@ -189,7 +200,7 @@ class D90AdisTSSpec(SQLSubModel):
@start_rk.setter
def start_rk(self, start_rk):
self._start_rk = start_rk
self._status.modified()
self.modified()
@property
def end_rk(self):
@ -198,7 +209,7 @@ class D90AdisTSSpec(SQLSubModel):
@end_rk.setter
def end_rk(self, end_rk):
self._end_rk = end_rk
self._status.modified()
self.modified()
@property
def d90(self):
@ -207,7 +218,7 @@ class D90AdisTSSpec(SQLSubModel):
@d90.setter
def d90(self, d90):
self._d90 = d90
self._status.modified()
self.modified()
@property
def enabled(self):
@ -216,4 +227,4 @@ class D90AdisTSSpec(SQLSubModel):
@enabled.setter
def enabled(self, enabled):
self._enabled = enabled
self._status.modified()
self.modified()

View File

@ -105,43 +105,67 @@ class DIFAdisTS(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT pamhyr_id, name, method, dif, b, c, enabled " +
"FROM dif_adists"
"SELECT pamhyr_id, name, method, dif, b, c, enabled, scenario " +
"FROM dif_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:
dif_id = row[0]
name = row[1]
method = row[2]
dif = row[3]
b = row[4]
c = row[5]
enabled = (row[6] == 1)
it = iter(row)
DIF = cls(
id=dif_id,
pid = next(it)
name = next(it)
method = next(it)
dif = next(it)
b = next(it)
c = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
dif = cls(
id=pid,
name=name,
status=data['status']
status=status,
owner_scenario=owner_scenario,
)
DIF.method = method
DIF.dif = dif
DIF.b = b
DIF.c = c
DIF.enabled = enabled
dif.method = method
dif.dif = dif
dif.b = b
dif.c = c
dif.enabled = enabled
data['dif_default_id'] = dif_id
DIF._data = DIFAdisTSSpec._db_load(execute, data)
data['dif_default_id'] = pid
dif._data = difAdisTSSpec._db_load(execute, data)
new.append(DIF)
loaded.add(pid)
new.append(dif)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
execute(f"DELETE FROM dif_adists WHERE id = {self.id}")
if not self.must_be_saved():
return True
execute(
"DELETE FROM dif_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
method = ""
if self.method is not None:
@ -162,12 +186,13 @@ class DIFAdisTS(SQLSubModel):
sql = (
"INSERT INTO " +
"dif_adists(" +
"pamhyr_id, name, method, dif, b, c, enabled" +
"pamhyr_id, name, method, dif, b, c, enabled, scenario" +
") " +
"VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " +
f"'{self._db_format(self._method)}', " +
f"{dif}, {b}, {c}, {self._enabled}" +
f"{self._status.scenario_id}" +
")"
)
@ -176,7 +201,8 @@ class DIFAdisTS(SQLSubModel):
data['dif_default_id'] = self.id
execute(
"DELETE FROM dif_adists_spec " +
f"WHERE dif_default = {self.id}"
f"WHERE dif_default = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
for dif_spec in self._data:
@ -194,7 +220,7 @@ class DIFAdisTS(SQLSubModel):
@name.setter
def name(self, name):
self._name = name
self._status.modified()
self.modified()
@property
def method(self):
@ -203,7 +229,7 @@ class DIFAdisTS(SQLSubModel):
@method.setter
def method(self, method):
self._method = method
self._status.modified()
self.modified()
@property
def types(self):
@ -216,7 +242,7 @@ class DIFAdisTS(SQLSubModel):
@dif.setter
def dif(self, dif):
self._dif = dif
self._status.modified()
self.modified()
@property
def b(self):
@ -225,7 +251,7 @@ class DIFAdisTS(SQLSubModel):
@b.setter
def b(self, b):
self._b = b
self._status.modified()
self.modified()
@property
def c(self):
@ -234,7 +260,7 @@ class DIFAdisTS(SQLSubModel):
@c.setter
def c(self, c):
self._c = c
self._status.modified()
self.modified()
@property
def enabled(self):
@ -243,12 +269,12 @@ class DIFAdisTS(SQLSubModel):
@enabled.setter
def enabled(self, enabled):
self._enabled = enabled
self._status.modified()
self.modified()
def new(self, index):
n = DIFAdisTSSpec(status=self._status)
self._data.insert(index, n)
self._status.modified()
self.modified()
return n
def delete(self, data):
@ -258,13 +284,13 @@ class DIFAdisTS(SQLSubModel):
self._data
)
)
self._status.modified()
self.modified()
def delete_i(self, indexes):
for ind in indexes:
del self._data[ind]
self._status.modified()
self.modified()
def insert(self, index, data):
self._data.insert(index, data)
self._status.modified()
self.modified()

View File

@ -115,30 +115,42 @@ class DIFAdisTSSpec(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT id, dif_default, method, reach, start_rk, end_rk, " +
"dif, b, c, enabled " +
"SELECT id, method, reach, start_rk, end_rk, " +
"dif, b, c, enabled, scenario " +
"FROM dif_spec " +
f"WHERE dif_default = {data['dif_default_id']} "
f"WHERE dif_default = {data['dif_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
id = row[0]
method = row[2]
reach = row[3]
start_rk = row[4]
end_rk = row[5]
dif = row[6]
b = row[7]
c = row[8]
enabled = (row[9] == 1)
it = iter(row)
id = next(it)
method = next(it)
reach = next(it)
start_rk = next(it)
end_rk = next(it)
dif = next(it)
b = next(it)
c = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
new_spec = cls(
id=id,
method=method,
status=data['status']
status=status,
owner_scenario=owner_scenario
)
new_spec.reach = reach
@ -149,31 +161,35 @@ class DIFAdisTSSpec(SQLSubModel):
new_spec.c = c
new_spec.enabled = enabled
loaded.add(pid)
new.append(new_spec)
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
dif_default = data['dif_default_id']
sql = (
execute(
"INSERT INTO " +
"dif_spec(id, dif_default, method, reach, " +
"start_rk, end_rk, dif, b, c, enabled) " +
"start_rk, end_rk, dif, b, c, enabled, scenario) " +
"VALUES (" +
f"{self.id}, " +
f"{dif_default}, " +
f"{self.id}, {dif_default}, " +
f"'{self._db_format(self._method)}', " +
f"{self._reach}, " +
f"{self._start_rk}, " +
f"{self._end_rk}, " +
f"{self._dif}, " +
f"{self._b}, " +
f"{self._c}, " +
f"{self._start_rk}, {self._end_rk}, " +
f"{self._dif}, {self._b}, {self._c}, " +
f"{self._enabled}" +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
return True

View File

@ -102,7 +102,7 @@ class InitialConditionsAdisTS(SQLSubModel):
"(pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario) " +
"SELECT pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario) " +
"eg, em, ed, scenario " +
f"FROM {table}"
)

View File

@ -43,8 +43,6 @@ class OutputRKAdists(SQLSubModel):
owner_scenario=owner_scenario
)
self._status = status
self._reach = reach
self._rk = rk
self._title = str(title)
@ -131,48 +129,67 @@ class OutputRKAdists(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
# reach = data["reach"]
# profile = data["profile"]
status = data["status"]
if scenario is None:
return new
table = execute(
"SELECT pamhyr_id, reach, rk, title " +
"SELECT pamhyr_id, reach, rk, title, scenario " +
f"FROM OutputRKAdists"
)
if table is not None:
for row in table:
id = row[0]
id_reach = row[1]
id_rk = row[2]
title = row[3]
it = iter(row)
pid = next(it)
id_reach = next(it)
id_rk = next(it)
title = next(it)
owner_scenario = next(it)
new_output = cls(
id=id, reach=id_reach,
id=pid, reach=id_reach,
rk=id_rk, title=title,
status=status
status=status,
owner_scenario=owner_scenario,
)
loaded.add(pid)
new.append(new_output)
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 OutputRKAdists WHERE id = {self.id}")
execute(
"DELETE FROM OutputRKAdists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
sql = (
execute(
"INSERT INTO " +
"OutputRKAdists(pamhyr_id, reach, rk, title) " +
"OutputRKAdists(pamhyr_id, reach, rk, title, scenario) " +
"VALUES (" +
f"{self.id}, {self._reach}, {self._rk}, " +
f"'{self._db_format(self._title)}'" +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
return True
@property

View File

@ -27,20 +27,20 @@ from tools import (
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Except import NotImplementedMethodeError
from Model.Scenario import Scenario
logger = logging.getLogger()
class Pollutants(SQLSubModel):
_sub_classes = []
_id_cnt = 0
def __init__(self, id: int = -1, name: str = "",
status=None,
owner_scenario=-1):
status=None, owner_scenario=-1):
super(Pollutants, self).__init__(
id=id, status=status,
owner_scenario=owner_scenario)
owner_scenario=owner_scenario
)
self._status = status
@ -52,9 +52,6 @@ class Pollutants(SQLSubModel):
self._data = []
Pollutants._id_cnt = max(
Pollutants._id_cnt + 1, self.id)
@property
def name(self):
return self._name
@ -69,17 +66,26 @@ class Pollutants(SQLSubModel):
return self._data.copy()
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE Pollutants(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL UNIQUE
def _db_create(cls, execute, ext=""):
cls._db_create_pol(execute, ext=ext)
cls._db_create_pol_char(execute, ext=ext)
@classmethod
def _db_create_pol(cls, execute, ext=""):
execute(f"""
CREATE TABLE pollutants{ext}(
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL UNIQUE,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}
)
""")
execute("""
CREATE TABLE Pollutants_characteristics(
id INTEGER NOT NULL PRIMARY KEY,
@classmethod
def _db_create_pol_char(cls, execute, ext=""):
execute(f"""
CREATE TABLE pollutants_characteristics{ext}(
{cls.create_db_add_pamhyr_id()},
type INTEGER NOT NULL,
diametre REAL NOT NULL,
rho REAL NOT NULL,
@ -90,7 +96,9 @@ class Pollutants(SQLSubModel):
ac REAL NOT NULL,
bc REAL NOT NULL,
pollutant INTEGER NOT NULL,
FOREIGN KEY(pollutant) REFERENCES Pollutants(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(pollutant) REFERENCES pollutants(pamhyr_id)
)
""")
@ -103,17 +111,70 @@ class Pollutants(SQLSubModel):
if int(release) < 7:
cls._db_create(execute)
elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
cls._db_update_to_0_2_0_char(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "Pollutants"
table_new = "pollutants"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table_new}_tmp " +
"(pamhyr_id, name, scenario) " +
"SELECT pamhyr_id, name, scenario) " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
@classmethod
def _db_update_to_0_2_0_char(cls, execute, data):
table = "Pollutants_characteristics"
table_new = "pollutants_characteristics"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table_new}_tmp " +
"(pamhyr_id, type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario) " +
"SELECT pamhyr_id, type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data["status"]
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT id, name " +
f"FROM Pollutants"
"SELECT pamhyr_id, name FROM pollutants " +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
)
if table is not None:
@ -129,8 +190,10 @@ class Pollutants(SQLSubModel):
new_data = []
table = execute(
"SELECT * " +
"FROM Pollutants_characteristics " +
f"WHERE pollutant = {id}"
"FROM pollutants_characteristics " +
f"WHERE pollutant = {id}" +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
)
if table is not None:
@ -139,36 +202,49 @@ class Pollutants(SQLSubModel):
new_pollutant._data.append(new_data)
loaded.add(pid)
new.append(new_pollutant)
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 Pollutants WHERE id = {self.id}")
execute(f"DELETE FROM Pollutants_characteristics" +
f" WHERE pollutant = {self.id}")
execute(
"DELETE FROM pollutants " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
execute(
f"DELETE FROM pollutants_characteristics " +
f"WHERE pollutant = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
sql = (
execute(
"INSERT INTO " +
"Pollutants(id, name) " +
"pollutants(id, name, scenario) " +
"VALUES (" +
f"{self.id}, " +
f"'{self._db_format(self._name)}'" +
f"'{self._db_format(self._name)}', " +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
for d in self._data:
sql = (
execute(
"INSERT INTO " +
"Pollutants_characteristics(type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant) " +
"pollutants_characteristics(type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario) " +
f"VALUES ({d[0]}, {d[1]}, {d[2]},{d[3]}, {d[4]}, "
f"{d[5]}, {d[6]}, {d[7]}, {d[8]}, {self.id})"
f"{d[5]}, {d[6]}, {d[7]}, {d[8]}, {self.id}, " +
f"{self._status.scenario_id})"
)
execute(sql)
return True