Model, Section: Add SQL export for section.

results
Pierre-Antoine Rouby 2023-07-03 15:41:59 +02:00
parent 9cf3177b57
commit 911e0c7e17
4 changed files with 145 additions and 22 deletions

View File

@ -59,7 +59,7 @@ class RiverNode(Node, SQLSubModel):
# Update id counter # Update id counter
cls._id_cnt = max(cls._id_cnt, row[0]) cls._id_cnt = max(cls._id_cnt, row[0])
# Create new node # Create new node
nodes.append(cls(*row, **data)) nodes.append(cls(*row, status=data["status"]))
return nodes return nodes
@ -86,7 +86,7 @@ class RiverNode(Node, SQLSubModel):
class RiverReach(Edge, SQLSubModel): class RiverReach(Edge, SQLSubModel):
_sub_classes = [ _sub_classes = [
Reach, Reach,
# SectionList, SectionList,
] ]
def __init__(self, id:str, name:str, def __init__(self, id:str, name:str,
@ -100,7 +100,7 @@ class RiverReach(Edge, SQLSubModel):
) )
self._reach = Reach(status=self._status, parent=self) self._reach = Reach(status=self._status, parent=self)
self._sections = SectionList(status=self._status) self._sections = SectionList(status = self._status)
@classmethod @classmethod
def _sql_create(cls, execute): def _sql_create(cls, execute):
@ -149,6 +149,8 @@ class RiverReach(Edge, SQLSubModel):
data["parent"] = new data["parent"] = new
new._reach = Reach._sql_load(execute, data) new._reach = Reach._sql_load(execute, data)
new._sections = SectionList._sql_load(execute, data)
reachs.append(new) reachs.append(new)
return reachs return reachs
@ -165,7 +167,10 @@ class RiverReach(Edge, SQLSubModel):
) )
execute(sql) execute(sql)
objs = [self._reach] if data is None:
data = {}
objs = [self._reach, self._sections]
return self._save_submodel(execute, objs, data) return self._save_submodel(execute, objs, data)
@property @property
@ -213,8 +218,17 @@ class River(Graph, SQLSubModel):
@classmethod @classmethod
def _sql_load(cls, execute, data = None): def _sql_load(cls, execute, data = None):
# Network
new = cls(data["status"]) 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( new._nodes = RiverNode._sql_load(
execute, execute,
data data
@ -244,12 +258,6 @@ class River(Graph, SQLSubModel):
data data
) )
# Stricklers
new._stricklers = StricklersList._sql_load(
execute,
data
)
# Parameters # Parameters
new._parameters = SolverParametersList._sql_load( new._parameters = SolverParametersList._sql_load(
execute, execute,

View File

@ -2,8 +2,9 @@
from tools import trace, timer from tools import trace, timer
from Model.DB import SQLSubModel
class Section(object): class Section(SQLSubModel):
def __init__(self, name:str = "", status = None): def __init__(self, name:str = "", status = None):
super(Section, self).__init__() super(Section, self).__init__()
@ -16,6 +17,74 @@ class Section(object):
self._begin_strickler = None self._begin_strickler = None
self._end_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 @property
def name(self): def name(self):
return self._name return self._name

View File

@ -3,15 +3,48 @@
from copy import copy from copy import copy
from tools import trace, timer from tools import trace, timer
from Model.DB import SQLSubModel
from Model.Section.Section import Section from Model.Section.Section import Section
class SectionList(object): class SectionList(SQLSubModel):
_sub_classes = [
Section
]
def __init__(self, status = None): def __init__(self, status = None):
super(SectionList, self).__init__() super(SectionList, self).__init__()
self._status = status self._status = status
self._sections = [] 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): def __len__(self):
return len(self._sections) return len(self._sections)

View File

@ -5,15 +5,25 @@ from tools import trace, timer
from Model.DB import SQLSubModel from Model.DB import SQLSubModel
class Stricklers(SQLSubModel): class Stricklers(SQLSubModel):
def __init__(self, name:str = "", _id_cnt = 0
_sub_classes = []
def __init__(self, id:int = -1,
name:str = "",
comment:str = "", comment:str = "",
minor:float = 35.0, minor:float = 35.0,
medium:float = 15.0, medium:float = 15.0,
status = None): status = None):
super(Stricklers, self).__init__() super(Stricklers, self).__init__()
self._status = status 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._name = name
self._comment = comment self._comment = comment
@ -25,7 +35,7 @@ class Stricklers(SQLSubModel):
def _sql_create(cls, execute): def _sql_create(cls, execute):
execute(""" execute("""
CREATE TABLE stricklers( CREATE TABLE stricklers(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id INTEGER NOT NULL PRIMARY KEY,
name TEXT, name TEXT,
comment TEXT, comment TEXT,
minor REAL NOT NULL, minor REAL NOT NULL,
@ -45,7 +55,7 @@ class Stricklers(SQLSubModel):
status = data["status"] status = data["status"]
table = execute( table = execute(
"SELECT name, comment, minor, medium " + "SELECT id, name, comment, minor, medium " +
"FROM stricklers" "FROM stricklers"
) )
@ -53,12 +63,14 @@ class Stricklers(SQLSubModel):
return None return None
for row in table: for row in table:
name = row[0] id = row[0]
comment = row[1] name = row[1]
minor = row[2] comment = row[2]
medium = row[3] minor = row[3]
medium = row[4]
new = cls( new = cls(
id = id,
name = name, name = name,
comment = comment, comment = comment,
minor = minor, medium = medium, minor = minor, medium = medium,
@ -72,8 +84,9 @@ class Stricklers(SQLSubModel):
def _sql_save(self, execute, data = None): def _sql_save(self, execute, data = None):
sql = ( sql = (
"INSERT INTO " + "INSERT INTO " +
"stricklers(name, comment, minor, medium) "+ "stricklers(id, name, comment, minor, medium) "+
"VALUES (" + "VALUES (" +
f"{self.id}, " +
f"'{self._sql_format(self.name)}', " + f"'{self._sql_format(self.name)}', " +
f"'{self._sql_format(self.comment)}', " + f"'{self._sql_format(self.comment)}', " +
f"{float(self.minor)}, {float(self.medium)}" + f"{float(self.minor)}, {float(self.medium)}" +