From 96d03ee41a5e3e21ed8e9aae62ef55fd1dec3807 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Mon, 3 Jul 2023 15:52:08 +0200
Subject: [PATCH] SQL: Minor change.

---
 src/Model/InitialConditions/InitialConditions.py | 2 +-
 src/Model/River.py                               | 2 ++
 src/Model/Section/Section.py                     | 4 +++-
 src/Model/Section/SectionList.py                 | 3 +++
 src/tools.py                                     | 2 +-
 5 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py
index 9b226487..bb106662 100644
--- a/src/Model/InitialConditions/InitialConditions.py
+++ b/src/Model/InitialConditions/InitialConditions.py
@@ -169,7 +169,7 @@ class Data(SQLSubModel):
 
     def _update_from_discharge(self):
         min = self._update_get_min()
-        print("TODO")
+        # print("TODO")
 
     def __setitem__(self, key, value):
         if key == "name":
diff --git a/src/Model/River.py b/src/Model/River.py
index f1aaa613..9f5505ad 100644
--- a/src/Model/River.py
+++ b/src/Model/River.py
@@ -170,6 +170,8 @@ class RiverReach(Edge, SQLSubModel):
         if data is None:
             data = {}
 
+        data["reach"] = self
+
         objs = [self._reach, self._sections]
         return self._save_submodel(execute, objs, data)
 
diff --git a/src/Model/Section/Section.py b/src/Model/Section/Section.py
index 82bd454b..86bdff48 100644
--- a/src/Model/Section/Section.py
+++ b/src/Model/Section/Section.py
@@ -57,9 +57,11 @@ class Section(SQLSubModel):
 
         for row in table:
             ind = row[0]
+            # Get stricklers
             bs = next(filter(lambda s: s.id == row[3], stricklers))
             es = next(filter(lambda s: s.id == row[4], stricklers))
 
+            # Create section
             sec = cls(status = status)
             sec.edge = reach
             sec.begin_kp = row[1]
@@ -74,7 +76,7 @@ class Section(SQLSubModel):
     def _sql_save(self, execute, data = None):
         ind = data["ind"]
         execute(
-            "INSERT OR REPLACE INTO " +
+            "INSERT INTO " +
             "section(ind, begin_kp, end_kp, reach, begin_strickler, end_strickler) " +
             "VALUES (" +
             f"{ind}, {self._begin_kp}, {self._end_kp}, " +
diff --git a/src/Model/Section/SectionList.py b/src/Model/Section/SectionList.py
index 0f018f66..d1f88f08 100644
--- a/src/Model/Section/SectionList.py
+++ b/src/Model/Section/SectionList.py
@@ -36,6 +36,9 @@ class SectionList(SQLSubModel):
         return new
 
     def _sql_save(self, execute, data = None):
+        reach = data["reach"]
+        execute(f"DELETE FROM section WHERE reach = {reach.id}")
+
         ok = True
         ind = 0
         for section in self._sections:
diff --git a/src/tools.py b/src/tools.py
index 27127754..34c4b9b2 100644
--- a/src/tools.py
+++ b/src/tools.py
@@ -233,7 +233,7 @@ class SQL(object):
 
     @timer
     def execute(self, cmd, fetch_one = True, commit = False):
-        print(f"[SQL] {cmd}")
+        # print(f"[SQL] {cmd}")
         res = self._cur.execute(cmd)
 
         if commit:
-- 
GitLab