IC: Add prepare db 0.1.0.

scenarios
Pierre-Antoine Rouby 2024-07-24 10:59:56 +02:00
parent aea308a5a2
commit 9f4b297d23
2 changed files with 53 additions and 24 deletions

View File

@ -23,17 +23,18 @@ from tools import trace, timer
from functools import reduce from functools import reduce
from Model.Tools.PamhyrDB import SQLSubModel from Model.Tools.PamhyrDB import SQLSubModel
from Model.Scenario import Scenario
logger = logging.getLogger() logger = logging.getLogger()
class Data(SQLSubModel): class Data(SQLSubModel):
def __init__(self, name: str = "", def __init__(self, id: int = -1, name: str = "",
comment: str = "", reach=None, comment: str = "", reach=None,
rk: float = 0.0, discharge: float = 0.0, rk: float = 0.0, discharge: float = 0.0,
height: float = 0.0, height: float = 0.0,
status=None): status=None):
super(Data, self).__init__() super(Data, self).__init__(id)
self._status = status self._status = status
@ -56,10 +57,10 @@ class Data(SQLSubModel):
self._update_from_discharge() self._update_from_discharge()
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute, ext=""):
execute(""" execute(f"""
CREATE TABLE initial_conditions( CREATE TABLE initial_conditions(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, {cls.create_db_add_pamhyr_id()},
ind INTEGER NOT NULL, ind INTEGER NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
comment TEXT NOT NULL, comment TEXT NOT NULL,
@ -67,7 +68,10 @@ class Data(SQLSubModel):
rk REAL NOT NULL, rk REAL NOT NULL,
discharge REAL NOT NULL, discharge REAL NOT NULL,
height REAL NOT NULL, height REAL NOT NULL,
FOREIGN KEY(reach) REFERENCES river_reach(id) {Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id),
PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -82,31 +86,54 @@ class Data(SQLSubModel):
"ALTER TABLE initial_conditions RENAME COLUMN kp TO rk" "ALTER TABLE initial_conditions RENAME COLUMN kp TO rk"
) )
cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_1_0(cls, execute, data):
table = "initial_conditions"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table} " +
"(pamhyr_id, name, comment, tab, reach, rk, scenario) " +
"SELECT pamhyr_id, name, comment, tab, reach, rk, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
id = data["reach"].id id = data["reach"].pamhyr_id
table = execute( table = execute(
"SELECT ind, name, comment, rk, discharge, height " + "SELECT pamhyr_id, ind, name, comment, rk, discharge, height " +
"FROM initial_conditions " + "FROM initial_conditions " +
f"WHERE reach = {id}" f"WHERE reach = {id} " +
"ORDER BY ind ASC"
) )
new = [] new = []
for _ in table:
new.append(None)
for row in table: for row in table:
ind = row[0] it = iter(row)
name = row[1]
comment = row[2] pid = next(it)
rk = row[3] ind = next(it)
discharge = row[4] name = next(it)
height = row[5] comment = next(it)
rk = next(it)
discharge = next(it)
height = next(it)
d = cls( d = cls(
id=pid,
reach=data["reach"], reach=data["reach"],
status=data["status"], status=data["status"],
name=name, name=name,
@ -116,7 +143,7 @@ class Data(SQLSubModel):
height=height, height=height,
) )
new[ind] = d new.append(d)
return new return new
@ -125,9 +152,10 @@ class Data(SQLSubModel):
execute( execute(
"INSERT INTO " + "INSERT INTO " +
"initial_conditions(ind, name, comment, rk, " + "initial_conditions(pamhyr_id, ind, name, comment, rk, " +
"discharge, height, reach) " + "discharge, height, reach) " +
"VALUES (" + "VALUES (" +
f"{self.pamhyr_id}, " +
f"{ind}, '{self._db_format(self.name)}', " + f"{ind}, '{self._db_format(self.name)}', " +
f"'{self._db_format(self._comment)}', " + f"'{self._db_format(self._comment)}', " +
f"{self._rk}, {self._discharge}, {self._height}, " + f"{self._rk}, {self._discharge}, {self._height}, " +

View File

@ -175,7 +175,8 @@ class LateralContribution(SQLSubModel):
FOREIGN KEY(begin_section) FOREIGN KEY(begin_section)
REFERENCES geometry_profileXYZ(pamhyr_id), REFERENCES geometry_profileXYZ(pamhyr_id),
FOREIGN KEY(end_section) FOREIGN KEY(end_section)
REFERENCES geometry_profileXYZ(pamhyr_id) REFERENCES geometry_profileXYZ(pamhyr_id),
PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -206,15 +207,15 @@ class LateralContribution(SQLSubModel):
""" """
) )
cls._db_update_to_0_1_0(execute) cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute): def _db_update_to_0_1_0(cls, execute, data):
table = "lateral_contribution" table = "lateral_contribution"
cls.update_db_add_pamhyr_id(execute, table) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp") cls._db_create(execute, ext="_tmp")