diff --git a/src/Model/River.py b/src/Model/River.py index da323b6c250713abc3f2b09b8bd135d3afba0c32..f1aaa613efdda5b31cffab542b58c3dd15eea780 100644 --- a/src/Model/River.py +++ b/src/Model/River.py @@ -59,7 +59,7 @@ class RiverNode(Node, SQLSubModel): # Update id counter cls._id_cnt = max(cls._id_cnt, row[0]) # Create new node - nodes.append(cls(*row, **data)) + nodes.append(cls(*row, status=data["status"])) return nodes @@ -86,7 +86,7 @@ class RiverNode(Node, SQLSubModel): class RiverReach(Edge, SQLSubModel): _sub_classes = [ Reach, - # SectionList, + SectionList, ] def __init__(self, id:str, name:str, @@ -100,7 +100,7 @@ class RiverReach(Edge, SQLSubModel): ) self._reach = Reach(status=self._status, parent=self) - self._sections = SectionList(status=self._status) + self._sections = SectionList(status = self._status) @classmethod def _sql_create(cls, execute): @@ -149,6 +149,8 @@ class RiverReach(Edge, SQLSubModel): data["parent"] = new new._reach = Reach._sql_load(execute, data) + new._sections = SectionList._sql_load(execute, data) + reachs.append(new) return reachs @@ -165,7 +167,10 @@ class RiverReach(Edge, SQLSubModel): ) execute(sql) - objs = [self._reach] + if data is None: + data = {} + + objs = [self._reach, self._sections] return self._save_submodel(execute, objs, data) @property @@ -213,8 +218,17 @@ class River(Graph, SQLSubModel): @classmethod def _sql_load(cls, execute, data = None): - # Network new = cls(data["status"]) + + # Stricklers (Stricklers is load in first because it's needed + # for reachs) + new._stricklers = StricklersList._sql_load( + execute, + data + ) + data["stricklers"] = new._stricklers + + # Network new._nodes = RiverNode._sql_load( execute, data @@ -244,12 +258,6 @@ class River(Graph, SQLSubModel): data ) - # Stricklers - new._stricklers = StricklersList._sql_load( - execute, - data - ) - # Parameters new._parameters = SolverParametersList._sql_load( execute, diff --git a/src/Model/Section/Section.py b/src/Model/Section/Section.py index 5d541813e33301167380056fa7aca638492937fd..82bd454bc0bf6dd4ddc50a435a8446603ea83da8 100644 --- a/src/Model/Section/Section.py +++ b/src/Model/Section/Section.py @@ -2,8 +2,9 @@ from tools import trace, timer +from Model.DB import SQLSubModel -class Section(object): +class Section(SQLSubModel): def __init__(self, name:str = "", status = None): super(Section, self).__init__() @@ -16,6 +17,74 @@ class Section(object): self._begin_strickler = None self._end_strickler = None + @classmethod + def _sql_create(cls, execute): + execute(""" + CREATE TABLE section( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + ind INTERGER NOT NULL, + begin_kp REAL NOT NULL, + end_kp REAL NOT NULL, + reach INTEGER NOT NULL, + begin_strickler INTERGER, + end_strickler INTERGER, + FOREIGN KEY(reach) REFERENCES river_reach(id), + FOREIGN KEY(begin_strickler) REFERENCES stricklers(id), + FOREIGN KEY(end_strickler) REFERENCES stricklers(id) + ) + """) + + return True + + @classmethod + def _sql_update(cls, execute, version): + return True + + @classmethod + def _sql_load(cls, execute, data = None): + new = [] + reach = data["parent"] # Reach object + status = data["status"] + stricklers = data["stricklers"].stricklers + + table = execute( + "SELECT ind, begin_kp, end_kp, begin_strickler, end_strickler " + + f"FROM section WHERE reach = {reach.id}" + ) + + for _ in table: + new.append(None) + + for row in table: + ind = row[0] + bs = next(filter(lambda s: s.id == row[3], stricklers)) + es = next(filter(lambda s: s.id == row[4], stricklers)) + + sec = cls(status = status) + sec.edge = reach + sec.begin_kp = row[1] + sec.end_kp = row[2] + sec.begin_strickler = bs + sec.end_strickler = es + + new[ind] = sec + + return new + + def _sql_save(self, execute, data = None): + ind = data["ind"] + execute( + "INSERT OR REPLACE INTO " + + "section(ind, begin_kp, end_kp, reach, begin_strickler, end_strickler) " + + "VALUES (" + + f"{ind}, {self._begin_kp}, {self._end_kp}, " + + f"{self._edge.id}, " + + f"{self._begin_strickler.id}, {self._end_strickler.id}" + + ")" + ) + + return True + @property def name(self): return self._name diff --git a/src/Model/Section/SectionList.py b/src/Model/Section/SectionList.py index ff6ad5efa606549210551b460a450bb69d3ba778..0f018f66a214cd422d05cae3bb4726c25aaebb26 100644 --- a/src/Model/Section/SectionList.py +++ b/src/Model/Section/SectionList.py @@ -3,15 +3,48 @@ from copy import copy from tools import trace, timer +from Model.DB import SQLSubModel from Model.Section.Section import Section -class SectionList(object): +class SectionList(SQLSubModel): + _sub_classes = [ + Section + ] + def __init__(self, status = None): super(SectionList, self).__init__() self._status = status self._sections = [] + @classmethod + def _sql_create(cls, execute): + return cls._create_submodel(execute) + + @classmethod + def _sql_update(cls, execute, version): + return True + + @classmethod + def _sql_load(cls, execute, data = None): + new = cls(status = data['status']) + + new._sections = Section._sql_load( + execute, data + ) + + return new + + def _sql_save(self, execute, data = None): + ok = True + ind = 0 + for section in self._sections: + data["ind"] = ind + ok &= section._sql_save(execute, data = data) + ind += 1 + + return ok + def __len__(self): return len(self._sections) diff --git a/src/Model/Stricklers/Stricklers.py b/src/Model/Stricklers/Stricklers.py index 09ecbe67a301be7752cc0df8647d52ebc9189978..cbc10009418899cd1be8d5a79efbb8e984f2a9aa 100644 --- a/src/Model/Stricklers/Stricklers.py +++ b/src/Model/Stricklers/Stricklers.py @@ -5,15 +5,25 @@ from tools import trace, timer from Model.DB import SQLSubModel class Stricklers(SQLSubModel): - def __init__(self, name:str = "", + _id_cnt = 0 + _sub_classes = [] + + def __init__(self, id:int = -1, + name:str = "", comment:str = "", minor:float = 35.0, medium:float = 15.0, status = None): super(Stricklers, self).__init__() - self._status = status + if id == -1: + self.id = Stricklers._id_cnt + else: + self.id = id + + Stricklers._id_cnt = max(Stricklers._id_cnt + 1, self.id) + self._name = name self._comment = comment @@ -25,7 +35,7 @@ class Stricklers(SQLSubModel): def _sql_create(cls, execute): execute(""" CREATE TABLE stricklers( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + id INTEGER NOT NULL PRIMARY KEY, name TEXT, comment TEXT, minor REAL NOT NULL, @@ -45,7 +55,7 @@ class Stricklers(SQLSubModel): status = data["status"] table = execute( - "SELECT name, comment, minor, medium " + + "SELECT id, name, comment, minor, medium " + "FROM stricklers" ) @@ -53,12 +63,14 @@ class Stricklers(SQLSubModel): return None for row in table: - name = row[0] - comment = row[1] - minor = row[2] - medium = row[3] + id = row[0] + name = row[1] + comment = row[2] + minor = row[3] + medium = row[4] new = cls( + id = id, name = name, comment = comment, minor = minor, medium = medium, @@ -72,8 +84,9 @@ class Stricklers(SQLSubModel): def _sql_save(self, execute, data = None): sql = ( "INSERT INTO " + - "stricklers(name, comment, minor, medium) "+ + "stricklers(id, name, comment, minor, medium) "+ "VALUES (" + + f"{self.id}, " + f"'{self._sql_format(self.name)}', " + f"'{self._sql_format(self.comment)}', " + f"{float(self.minor)}, {float(self.medium)}" +