Model, LC: Add SQL export for lateral contribution.

This commit is contained in:
Pierre-Antoine Rouby committed 2023-06-29 16:53:17 +02:00
1 parent 36f792d432
commit a4acbdce16
4 files changed
+193 -12

No files matched your search

@@ -2,14 +2,24 @@
from tools import trace, timer, old_pamhyr_date_to_timestamp
from Model.DB import SQLSubModel
from Model.Except import NotImplementedMethodeError
class LateralContribution(object):
def __init__(self, name:str = "", status = None):
class LateralContribution(SQLSubModel):
_sub_classes = []
_id_cnt = 0
def __init__(self, id:int = -1, name:str = "", status = None):
super(LateralContribution, self).__init__()
self._status = status
if id == -1:
self.id = type(self)._id_cnt
else:
self.id = id
self._name = name
self._type = ""
self._edge = None
@@ -19,6 +29,132 @@ class LateralContribution(object):
self._header = []
self._types = [float, float]
type(self)._id_cnt = max(type(self)._id_cnt + 1, self.id)
@classmethod
def _sql_create(cls, execute):
execute("""
CREATE TABLE lateral_contribution(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
tab TEXT NOT NULL,
edge INTEGER,
begin_kp REAL NOT NULL,
end_kp REAL NOT NULL,
FOREIGN KEY(edge) REFERENCES river_reach(id)
)
""")
execute("""
CREATE TABLE lateral_contribution_data(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
ind INTEGER NOT NULL,
data0 TEXT NOT NULL,
data1 TEXT NOT NULL,
lc INTEGER,
FOREIGN KEY(lc) REFERENCES lateral_contribution(id)
)
""")
return cls._create_submodel(execute)
@classmethod
def _sql_update(cls, execute, version):
return True
@classmethod
def _get_ctor_from_type(cls, t):
from Model.LateralContribution.LateralContributionTypes import (
NotDefined, LateralContrib, Rain, Evaporation
)
res = NotDefined
if t == "LC":
res = LateralContrib
elif t == "RA":
res = Rain
elif t == "EV":
res = Evaporation
return res
@classmethod
def _sql_load(cls, execute, data = None):
new = []
tab = data["tab"]
table = execute(
"SELECT id, name, type, edge, begin_kp, end_kp " +
f"FROM lateral_contribution WHERE tab = '{tab}'"
)
for row in table:
t = row[2]
ctor = cls._get_ctor_from_type(t)
lc = ctor(
id = row[0],
name = row[1],
status = data['status']
)
lc.edge = next(filter(lambda e: e.id == row[3], data["edges"]))
lc._begin_kp = row[4]
lc._end_kp = row[5]
values = execute(
"SELECT ind, data0, data1 FROM lateral_contribution_data " +
f"WHERE lc = '{lc.id}'"
)
# Create dummy data list
for _ in values:
lc.add(0)
# Write data
for v in values:
ind = v[0]
data0 = lc._types[0](v[1])
data1 = lc._types[1](v[2])
# Replace data at pos ind
lc._data[ind] = (data0, data1)
new.append(lc)
return new
def _sql_save(self, execute, data = None):
tab = data["tab"]
execute(f"DELETE FROM lateral_contribution WHERE id = {self.id}")
execute(f"DELETE FROM lateral_contribution_data WHERE lc = {self.id}")
edge = -1
if self._edge is not None:
edge = self._edge.id
sql = (
"INSERT INTO " +
"lateral_contribution(id, name, type, tab, edge, begin_kp, end_kp) "+
"VALUES (" +
f"{self.id}, '{self._sql_format(self._name)}', " +
f"'{self._sql_format(self._type)}', '{tab}', {edge}, " +
f"{self._begin_kp}, {self._end_kp}" +
")"
)
execute(sql)
ind = 0
for d in self._data:
data0 = self._sql_format(str(d[0]))
data1 = self._sql_format(str(d[1]))
sql = (
"INSERT INTO " +
"lateral_contribution_data(ind, data0, data1, lc) "+
f"VALUES ({ind}, '{data0}', {data1}, {self.id})"
)
execute(sql)
ind += 1
return True
def __len__(self):
return len(self._data)