mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
merge
This commit is contained in:
commit
79b31e8d08
16 files changed
+335
-71
No files matched your search
@@ -80,11 +80,15 @@ class BasicHS(SQLSubModel):
|
||||
|
||||
@classmethod
|
||||
def _get_ctor_from_type(cls, t):
|
||||
from Model.HydraulicStructure.Basic.Types import (
|
||||
NotDefined,
|
||||
from Model.HydraulicStructures.Basic.Types import (
|
||||
NotDefined, Dummy,
|
||||
)
|
||||
|
||||
res = NotDefined
|
||||
|
||||
if t == "DU":
|
||||
res = Dummy
|
||||
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
@@ -93,7 +97,8 @@ class BasicHS(SQLSubModel):
|
||||
|
||||
table = execute(
|
||||
"SELECT id, name, type, enabled, hs " +
|
||||
"FROM hydraulic_structures "
|
||||
"FROM hydraulic_structures_basic " +
|
||||
f"WHERE hs = {data['hs_id']} "
|
||||
)
|
||||
|
||||
for row in table:
|
||||
@@ -113,7 +118,7 @@ class BasicHS(SQLSubModel):
|
||||
bhs.enabled = enabled
|
||||
|
||||
data['bhs_id'] = bhs_id
|
||||
bhs._data = BasicHSValue._db_load(
|
||||
bhs._data = BHSValue._db_load(
|
||||
execute, data
|
||||
)
|
||||
|
||||
@@ -144,7 +149,7 @@ class BasicHS(SQLSubModel):
|
||||
data['bhs_id'] = self.id
|
||||
execute(
|
||||
"DELETE FROM hydraulic_structures_basic_value " +
|
||||
f"WHERE bhs = {bhs_id}"
|
||||
f"WHERE bhs = {self.id}"
|
||||
)
|
||||
|
||||
for values in self._data:
|
||||
@@ -183,8 +188,8 @@ class BasicHS(SQLSubModel):
|
||||
self._status.modified()
|
||||
|
||||
@property
|
||||
def lst(self):
|
||||
return self._data.copy()
|
||||
def parameters(self):
|
||||
return self._data
|
||||
|
||||
def convert(self, new_type):
|
||||
return new_type(id=self.id, name=self.name, status=self._status)
|
||||
@@ -35,9 +35,9 @@ class NotDefined(BasicHS):
|
||||
|
||||
self._type = "ND"
|
||||
self._data = [
|
||||
BHSValue("foo", float, 0.0),
|
||||
BHSValue("bar", float, 42.0),
|
||||
BHSValue("baz", int, 13),
|
||||
BHSValue("foo", float, 0.0, status=status),
|
||||
BHSValue("bar", float, 42.0, status=status),
|
||||
BHSValue("baz", int, 13, status=status),
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
from functools import reduce
|
||||
|
||||
from tools import trace, timer, old_pamhyr_date_to_timestamp
|
||||
|
||||
@@ -117,15 +118,15 @@ class HydraulicStructure(SQLSubModel):
|
||||
)
|
||||
|
||||
hs.enabled = enabled
|
||||
hs.input_kp = input_kp
|
||||
hs.output_kp = output_kp
|
||||
hs.input_kp = input_kp if input_kp != -1 else None
|
||||
hs.output_kp = output_kp if output_kp != -1 else None
|
||||
|
||||
hs.input_reach, hs.output_reach = reduce(
|
||||
lambda acc, n: (
|
||||
n if n.id == input_reach_id else acc[0],
|
||||
n if n.id == output_reach_id else acc[1]
|
||||
),
|
||||
data["reachs"],
|
||||
data["edges"],
|
||||
[None, None]
|
||||
)
|
||||
|
||||
|
||||
+20
-4
@@ -86,7 +86,7 @@ class Study(SQLModel):
|
||||
def is_saved(self):
|
||||
return self.status.is_saved()
|
||||
|
||||
def save(self):
|
||||
def save(self, progress=None):
|
||||
# Save a copy of database
|
||||
fdir, fname = os.path.split(self.filename)
|
||||
|
||||
@@ -110,7 +110,7 @@ class Study(SQLModel):
|
||||
|
||||
# Save
|
||||
self.last_save_date = datetime.now()
|
||||
self._save()
|
||||
self._save(progress=progress)
|
||||
self.status.save()
|
||||
|
||||
@property
|
||||
@@ -266,38 +266,54 @@ class Study(SQLModel):
|
||||
|
||||
return new
|
||||
|
||||
def _save(self):
|
||||
def _save(self, progress=None):
|
||||
progress = progress if progress is not None else lambda: None
|
||||
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{self._db_format(self.name)}' WHERE key='name'"
|
||||
)
|
||||
progress()
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{self._db_format(self.description)}' " +
|
||||
"WHERE key='description'"
|
||||
)
|
||||
progress()
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{self._time_system}' WHERE key='time_system'"
|
||||
)
|
||||
progress()
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{timestamp(self._date)}' WHERE key='date'"
|
||||
)
|
||||
progress()
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{timestamp(self.creation_date)}' " +
|
||||
"WHERE key='creation_date'"
|
||||
)
|
||||
progress()
|
||||
self.execute(
|
||||
f"UPDATE info SET " +
|
||||
f"value='{timestamp(self.last_save_date)}' " +
|
||||
"WHERE key='last_save_date'"
|
||||
)
|
||||
progress()
|
||||
|
||||
self._save_submodel([self._river])
|
||||
self._save_submodel([self._river], data=progress)
|
||||
self.commit()
|
||||
|
||||
def sql_save_request_count(self):
|
||||
return self._count()
|
||||
|
||||
def _count(self):
|
||||
cnt = self._save_count([self._river])
|
||||
logger.debug(cnt)
|
||||
return cnt + 6
|
||||
|
||||
def close(self):
|
||||
"""Close db connection
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import sqlite3
|
||||
import logging
|
||||
|
||||
from pathlib import Path
|
||||
from functools import reduce
|
||||
|
||||
from tools import SQL
|
||||
from Model.Except import NotImplementedMethodeError
|
||||
@@ -87,11 +88,16 @@ class SQLModel(SQL):
|
||||
raise NotImplementedMethodeError(self, self._update)
|
||||
|
||||
def _save_submodel(self, objs, data=None):
|
||||
def fn(sql): return self.execute(
|
||||
sql,
|
||||
fetch_one=False,
|
||||
commit=False
|
||||
)
|
||||
progress = data if data is not None else lambda: None
|
||||
|
||||
def fn(sql):
|
||||
res = self.execute(
|
||||
sql,
|
||||
fetch_one=False,
|
||||
commit=False
|
||||
)
|
||||
progress()
|
||||
return res
|
||||
|
||||
ok = True
|
||||
for obj in objs:
|
||||
@@ -100,9 +106,43 @@ class SQLModel(SQL):
|
||||
self.commit()
|
||||
return ok
|
||||
|
||||
def _save(self):
|
||||
def _save(self, progress=None):
|
||||
raise NotImplementedMethodeError(self, self._save)
|
||||
|
||||
def _count(self):
|
||||
raise NotImplementedMethodeError(self, self._count)
|
||||
|
||||
def _save_count(self, objs, data=None):
|
||||
counter = {
|
||||
"insert": 0,
|
||||
"update": 0,
|
||||
"delete": 0,
|
||||
"other": 0,
|
||||
}
|
||||
|
||||
def fn(sql):
|
||||
if "insert" in sql.lower():
|
||||
counter["insert"] = counter["insert"] + 1
|
||||
elif "update" in sql.lower():
|
||||
counter["update"] = counter["update"] + 1
|
||||
elif "delete" in sql.lower():
|
||||
counter["delete"] = counter["delete"] + 1
|
||||
else:
|
||||
counter["other"] = counter["other"] + 1
|
||||
return []
|
||||
|
||||
ok = True
|
||||
for obj in objs:
|
||||
ok &= obj._db_save(fn)
|
||||
|
||||
logger.debug(counter)
|
||||
|
||||
return reduce(
|
||||
lambda acc, k: acc + counter[k],
|
||||
counter,
|
||||
0
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _load(cls, filename=None):
|
||||
raise NotImplementedMethodeError(cls, cls._load)
|
||||
|
||||
Reference in new issue
Block a user