HS: Prepare update db 0.1.0.

scenarios
Pierre-Antoine Rouby 2024-07-29 15:07:44 +02:00
parent 1bae755f1c
commit 9ec080a107
4 changed files with 150 additions and 79 deletions

View File

@ -27,10 +27,13 @@ from Model.Geometry.Point import Point
class PointXYZ(Point, SQLSubModel): class PointXYZ(Point, SQLSubModel):
_sub_classes = [] _sub_classes = []
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, def __init__(self, id: int = -1,
x: float = 0.0, y: float = 0.0, z: float = 0.0,
name: str = "", profile=None, status=None): name: str = "", profile=None, status=None):
super(PointXYZ, self).__init__( super(PointXYZ, self).__init__(
name=name, profile=profile, status=status) id=id
name=name, profile=profile, status=status
)
self._x = float(x) self._x = float(x)
self._y = float(y) self._y = float(y)
@ -76,7 +79,6 @@ class PointXYZ(Point, SQLSubModel):
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, data): def _db_update_to_0_1_0(cls, execute, data):
table = "geometry_pointXYZ" table = "geometry_pointXYZ"
@ -102,15 +104,16 @@ class PointXYZ(Point, SQLSubModel):
profile = data["profile"] profile = data["profile"]
table = execute( table = execute(
"SELECT name, x, y, z, sl " + "SELECT pamhyr_id, name, x, y, z, sl " +
"FROM geometry_pointXYZ " + "FROM geometry_pointXYZ " +
f"WHERE profile = {profile.id} " + f"WHERE profile = {profile.pamhyr_id} " +
"ORDER BY ind ASC" "ORDER BY ind ASC"
) )
for row in table: for row in table:
it = iter(row) it = iter(row)
pid = next(it)
name = next(it) name = next(it)
x = next(it) x = next(it)
y = next(it) y = next(it)
@ -118,6 +121,7 @@ class PointXYZ(Point, SQLSubModel):
sl = next(it) sl = next(it)
new = cls( new = cls(
id=pid,
name=name, name=name,
x=x, y=y, z=z, x=x, y=y, z=z,
profile=profile, profile=profile,
@ -129,7 +133,7 @@ class PointXYZ(Point, SQLSubModel):
else: else:
new._sl = next( new._sl = next(
filter( filter(
lambda s: s.id == sl, lambda s: s.pamhyr_id == sl,
data["sediment_layers_list"].sediment_layers data["sediment_layers_list"].sediment_layers
) )
) )
@ -140,12 +144,14 @@ class PointXYZ(Point, SQLSubModel):
profile = data["profile"] profile = data["profile"]
ind = data["ind"] ind = data["ind"]
sl = self._sl.id if self._sl is not None else -1 sl = self._sl.pamhyr_id if self._sl is not None else -1
execute( execute(
"INSERT INTO " + "INSERT INTO " +
"geometry_pointXYZ(ind, name, x, y, z, profile, sl) " + "geometry_pointXYZ(pamhyr_id, ind, name, " +
"x, y, z, profile, sl) " +
"VALUES (" + "VALUES (" +
f"{self.pamhyr_id}, " +
f"{ind}, '{self._db_format(self._name)}', " + f"{ind}, '{self._db_format(self._name)}', " +
f"{self.x}, {self.y}, {self.z}, " + f"{self.x}, {self.y}, {self.z}, " +
f"{profile.pamhyr_id}, {sl}" + f"{profile.pamhyr_id}, {sl}" +

View File

@ -38,32 +38,28 @@ class BasicHS(SQLSubModel):
def __init__(self, id: int = -1, name: str = "", def __init__(self, id: int = -1, name: str = "",
status=None): status=None):
super(BasicHS, self).__init__() super(BasicHS, self).__init__(id)
self._status = status self._status = status
if id == -1:
self.id = BasicHS._id_cnt
else:
self.id = id
self._name = name self._name = name
self._type = "" self._type = ""
self._enabled = True self._enabled = True
self._data = [] self._data = []
BasicHS._id_cnt = max(BasicHS._id_cnt + 1, self.id)
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute):
execute(""" execute(f"""
CREATE TABLE hydraulic_structures_basic( CREATE TABLE hydraulic_structures_basic{ext} (
id INTEGER NOT NULL PRIMARY KEY, {cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL, name TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
enabled BOOLEAN NOT NULL, enabled BOOLEAN NOT NULL,
hs INTEGER, hs INTEGER,
FOREIGN KEY(hs) REFERENCES hydraulic_structures(id) FOREIGN KEY(hs) REFERENCES hydraulic_structures(pamhyr_id),
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -76,8 +72,29 @@ class BasicHS(SQLSubModel):
if int(release) < 6: if int(release) < 6:
cls._db_create(execute) cls._db_create(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
def _db_update_to_0_1_0(cls, execute, data):
table = "hydraulic_structures_basic"
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}_tmp " +
"(pamhyr_id, name, type, enabled, hs, scenario) " +
"SELECT pamhyr_id, name, type, enabled, hs, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod @classmethod
def _get_ctor_from_type(cls, t): def _get_ctor_from_type(cls, t):
from Model.HydraulicStructures.Basic.Types import ( from Model.HydraulicStructures.Basic.Types import (
@ -96,21 +113,23 @@ class BasicHS(SQLSubModel):
new = [] new = []
table = execute( table = execute(
"SELECT id, name, type, enabled, hs " + "SELECT pamhyr_id, name, type, enabled, hs " +
"FROM hydraulic_structures_basic " + "FROM hydraulic_structures_basic " +
f"WHERE hs = {data['hs_id']} " f"WHERE hs = {data['hs_id']} "
) )
for row in table: for row in table:
bhs_id = row[0] it = iter(row)
name = row[1]
type = row[2] bhs_pid = next(it)
enabled = (row[3] == 1) name = next(it)
hs_id = row[4] type = next(it)
enabled = (next(it) == 1)
hs_id = next(it)
ctor = cls._get_ctor_from_type(type) ctor = cls._get_ctor_from_type(type)
bhs = ctor( bhs = ctor(
id=bhs_id, id=bhs_pid,
name=name, name=name,
status=data['status'] status=data['status']
) )
@ -131,9 +150,9 @@ class BasicHS(SQLSubModel):
sql = ( sql = (
"INSERT INTO " + "INSERT INTO " +
"hydraulic_structures_basic(id, name, type, enabled, hs) " + "hydraulic_structures_basic(pamhyr_id, name, type, enabled, hs) " +
"VALUES (" + "VALUES (" +
f"{self.id}, " + f"{self.pamhyr_id}, " +
f"'{self._db_format(self._name)}', " + f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._type)}', " + f"'{self._db_format(self._type)}', " +
f"{self._db_format(self.enabled)}, " + f"{self._db_format(self.enabled)}, " +
@ -142,10 +161,10 @@ class BasicHS(SQLSubModel):
) )
execute(sql) execute(sql)
data['bhs_id'] = self.id data['bhs_id'] = self.pamhyr_id
execute( execute(
"DELETE FROM hydraulic_structures_basic_value " + "DELETE FROM hydraulic_structures_basic_value " +
f"WHERE bhs = {self.id}" f"WHERE bhs = {self.pamhyr_id}"
) )
for values in self._data: for values in self._data:
@ -159,7 +178,7 @@ class BasicHS(SQLSubModel):
@property @property
def name(self): def name(self):
if self._name == "": if self._name == "":
return f"{self._type}{self.id + 1}" return f"{self._type} #{self.pamhyr_id}"
return self._name return self._name

View File

@ -23,9 +23,10 @@ class BHSValue(SQLSubModel):
_sub_classes = [] _sub_classes = []
_id_cnt = 0 _id_cnt = 0
def __init__(self, name: str = "", type=float, value=0.0, def __init__(self, id: int = -1, name: str = "",
type=float, value=0.0,
status=None): status=None):
super(BHSValue, self).__init__() super(BHSValue, self).__init__(id)
self._status = status self._status = status
@ -34,15 +35,18 @@ class BHSValue(SQLSubModel):
self._value = type(value) self._value = type(value)
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute, ext=""):
execute(""" execute(f"""
CREATE TABLE hydraulic_structures_basic_value( CREATE TABLE hydraulic_structures_basic_value{ext} (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, {cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL, name TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
value TEXT NOT NULL, value TEXT NOT NULL,
bhs INTEGER, bhs INTEGER,
FOREIGN KEY(bhs) REFERENCES hydraulic_structures_basic(id) FOREIGN KEY(bhs) REFERENCES hydraulic_structures_basic(id),
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -55,7 +59,28 @@ class BHSValue(SQLSubModel):
if int(release) < 6: if int(release) < 6:
cls._db_create(execute) cls._db_create(execute)
return True cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_1_0(cls, execute, data):
table = "hydraulic_structures_basic_value"
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}_tmp " +
"(pamhyr_id, name, type, value, bhs, scenario) " +
"SELECT pamhyr_id, name, type, value, bhs, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod @classmethod
def _str_to_type(cls, type): def _str_to_type(cls, type):
@ -89,20 +114,22 @@ class BHSValue(SQLSubModel):
bhs_id = data["bhs_id"] bhs_id = data["bhs_id"]
table = execute( table = execute(
"SELECT name, type, value " + "SELECT pamhyr_id, name, type, value " +
"FROM hydraulic_structures_basic_value " + "FROM hydraulic_structures_basic_value " +
f"WHERE bhs = '{bhs_id}'" f"WHERE bhs = '{bhs_id}'"
) )
for row in table: for row in table:
name = row[0] it = iter(row)
type = cls._str_to_type(row[1])
value = row[2] pid = next(it)
name = next(it)
type = cls._str_to_type(next(it))
value = next(it)
val = cls( val = cls(
name=name, id=pid, name=name,
type=type, type=type, value=value,
value=value,
status=data['status'] status=data['status']
) )
@ -113,17 +140,18 @@ class BHSValue(SQLSubModel):
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
bhs_id = data["bhs_id"] bhs_id = data["bhs_id"]
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"hydraulic_structures_basic_value(name, type, value, bhs) " + "hydraulic_structures_basic_value" +
"(pamhyr_id, name, type, value, bhs) " +
"VALUES (" + "VALUES (" +
f"{self._pamhyr_id}, " +
f"'{self._db_format(self._name)}', " + f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._type_to_str(self._type))}', " + f"'{self._db_format(self._type_to_str(self._type))}', " +
f"'{self._db_format(self._value)}', " + f"'{self._db_format(self._value)}', " +
f"{bhs_id}" + f"{bhs_id}" +
")" ")"
) )
execute(sql)
return True return True

View File

@ -40,15 +40,10 @@ class HydraulicStructure(SQLSubModel):
def __init__(self, id: int = -1, name: str = "", def __init__(self, id: int = -1, name: str = "",
status=None): status=None):
super(HydraulicStructure, self).__init__() super(HydraulicStructure, self).__init__(id)
self._status = status self._status = status
if id == -1:
self.id = HydraulicStructure._id_cnt
else:
self.id = id
self._name = name self._name = name
self._input_rk = None self._input_rk = None
self._output_rk = None self._output_rk = None
@ -57,24 +52,22 @@ class HydraulicStructure(SQLSubModel):
self._enabled = True self._enabled = True
self._data = [] self._data = []
HydraulicStructure._id_cnt = max(
HydraulicStructure._id_cnt + 1,
self.id
)
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute, ext=""):
execute(""" execute(f"""
CREATE TABLE hydraulic_structures( CREATE TABLE hydraulic_structures{ext} (
id INTEGER NOT NULL PRIMARY KEY, {cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL, name TEXT NOT NULL,
enabled BOOLEAN NOT NULL, enabled BOOLEAN NOT NULL,
input_rk REAL NOT NULL, input_rk REAL NOT NULL,
output_rk REAL NOT NULL, output_rk REAL NOT NULL,
input_reach INTEGER, input_reach INTEGER,
output_reach INTEGER, output_reach INTEGER,
FOREIGN KEY(input_reach) REFERENCES river_reach(id), FOREIGN KEY(input_reach) REFERENCES river_reach(pamhyr_id),
FOREIGN KEY(output_reach) REFERENCES river_reach(id) FOREIGN KEY(output_reach) REFERENCES river_reach(pamhyr_id),
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -103,17 +96,40 @@ class HydraulicStructure(SQLSubModel):
""" """
) )
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 = "hydraulic_structures"
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}_tmp " +
"(pamhyr_id, name, enabled, input_rk, output_rk, " +
"input_reach, output_reach, scenario) " +
"SELECT pamhyr_id, name, enabled, input_rk, output_rk, " +
"input_reach, output_reach, 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):
new = [] new = []
table = execute( table = execute(
"SELECT id, name, enabled, " + "SELECT pamhyr_id, name, enabled, " +
"input_rk, output_rk, " + "input_rk, output_rk, " +
"input_reach, output_reach " + "input_reach, output_reach " +
"FROM hydraulic_structures " "FROM hydraulic_structures"
) )
for row in table: for row in table:
@ -154,15 +170,18 @@ class HydraulicStructure(SQLSubModel):
return new return new
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
execute(f"DELETE FROM hydraulic_structures WHERE id = {self.id}") execute(
"DELETE FROM hydraulic_structures " +
f"WHERE pamhyr_id = {self.pamhyr_id}"
)
input_reach_id = -1 input_reach_id = -1
if self._input_reach is not None: if self._input_reach is not None:
input_reach_id = self._input_reach.id input_reach_id = self._input_reach.pamhyr_id
output_reach_id = -1 output_reach_id = -1
if self._output_reach is not None: if self._output_reach is not None:
output_reach_id = self._output_reach.id output_reach_id = self._output_reach.pamhyr_id
input_rk = -1 input_rk = -1
if self.input_rk is not None: if self.input_rk is not None:
@ -172,25 +191,24 @@ class HydraulicStructure(SQLSubModel):
if self.output_rk is not None: if self.output_rk is not None:
output_rk = self.output_rk output_rk = self.output_rk
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"hydraulic_structures(" + "hydraulic_structures(" +
" id, name, enabled, input_rk, output_rk, " + " pamhyr_id, name, enabled, input_rk, output_rk, " +
" input_reach, output_reach" + " input_reach, output_reach" +
") " + ") " +
"VALUES (" + "VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " + f"{self.pamhyr_id}, '{self._db_format(self._name)}', " +
f"{self._db_format(self.enabled)}, " + f"{self._db_format(self.enabled)}, " +
f"{input_rk}, {output_rk}, " + f"{input_rk}, {output_rk}, " +
f"{input_reach_id}, {output_reach_id}" + f"{input_reach_id}, {output_reach_id}" +
")" ")"
) )
execute(sql)
data['hs_id'] = self.id data['hs_id'] = self.pamhyr_id
execute( execute(
"DELETE FROM hydraulic_structures_basic " + "DELETE FROM hydraulic_structures_basic " +
f"WHERE hs = {self.id}" f"WHERE hs = {self.pamhyr_id}"
) )
for basic in self._data: for basic in self._data:
@ -204,7 +222,7 @@ class HydraulicStructure(SQLSubModel):
@property @property
def name(self): def name(self):
if self._name == "": if self._name == "":
return f"HS{self.id + 1}" return f"HS #{self.pamhyr_id}"
return self._name return self._name