mirror of https://gitlab.com/pamhyr/pamhyr2
Adists: Pollutants: Update for scenario.
parent
468405d4ea
commit
739eb56006
|
|
@ -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}"
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue