From 02c6430ed25579c91650978d106368bed53b04fb Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Fri, 20 Sep 2024 11:15:00 +0200 Subject: [PATCH] Geometry: Point: Add scenario support. --- src/Model/Geometry/Point.py | 10 ++++--- src/Model/Geometry/PointXYZ.py | 54 +++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/Model/Geometry/Point.py b/src/Model/Geometry/Point.py index cce29ba8..8c8137ea 100644 --- a/src/Model/Geometry/Point.py +++ b/src/Model/Geometry/Point.py @@ -21,10 +21,12 @@ from Model.Except import NotImplementedMethodeError class Point(object): def __init__(self, id: int = -1, name: str = "", - profile=None, status=None): - super(Point, self).__init__() - - self._status = status + profile=None, status=None, + owner_scenario=-1): + super(Point, self).__init__( + status=status, + owner_scenario=owner_scenario + ) self._name = name self._profile = profile diff --git a/src/Model/Geometry/PointXYZ.py b/src/Model/Geometry/PointXYZ.py index 9c62e0ca..69131bef 100644 --- a/src/Model/Geometry/PointXYZ.py +++ b/src/Model/Geometry/PointXYZ.py @@ -33,9 +33,11 @@ class PointXYZ(Point, SQLSubModel): def __init__(self, id: int = -1, x: float = 0.0, y: float = 0.0, z: float = 0.0, - name: str = "", profile=None, status=None): + name: str = "", profile=None, + status=None, owner_scenario=-1): super(PointXYZ, self).__init__( - id=id, name=name, profile=profile, status=status + id=id, name=name, profile=profile, status=status, + owner_scenario=owner_scenario ) self._x = float(x) @@ -47,6 +49,7 @@ class PointXYZ(Point, SQLSubModel): execute(f""" CREATE TABLE geometry_pointXYZ{ext} ( {cls.create_db_add_pamhyr_id()}, + deleted BOOLEAN NOT NULL DEFAULT FALSE, ind INTEGER NOT NULL, name TEXT, x INTEGER NOT NULL, @@ -80,6 +83,13 @@ class PointXYZ(Point, SQLSubModel): cls._db_update_to_0_1_0(execute, data) + if major == "0" and minor == "1": + if int(release) < 2: + execute( + "ALTER TABLE geometry_pointXYZ " + + "ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE" + ) + return cls._update_submodel(execute, version, data) @classmethod @@ -156,11 +166,19 @@ class PointXYZ(Point, SQLSubModel): def _db_load(cls, execute, data=None): status = data["status"] profile = data["profile"] + scenario = data["scenario"] + loaded = data['loaded_pid'] + + if scenario is None: + return table = execute( - "SELECT pamhyr_id, name, x, y, z, sl " + + "SELECT pamhyr_id, deleted, " + + "name, x, y, z, sl, scenario " + "FROM geometry_pointXYZ " + f"WHERE profile = {profile.pamhyr_id} " + + f"AND scenario = {scenario.id} " + + f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) " + "ORDER BY ind ASC" ) @@ -168,19 +186,24 @@ class PointXYZ(Point, SQLSubModel): it = iter(row) pid = next(it) + deleted = (next(it) == 1) name = next(it) x = next(it) y = next(it) z = next(it) sl = next(it) + owner_scenario = next(it) new = cls( id=pid, name=name, x=x, y=y, z=z, profile=profile, - status=status + status=status, + owner_scenario=owner_scenario ) + if deleted: + nd.set_as_deleted() if sl == -1 or sl is None: new._sl = None @@ -192,9 +215,17 @@ class PointXYZ(Point, SQLSubModel): ) ) + loaded.add(pid) yield new + data["scenario"] = scenario.parent + yield from cls._db_load(execute, data) + data["scenario"] = scenario + def _db_save(self, execute, data=None): + if not self.must_be_saved(): + return True + profile = data["profile"] ind = data["ind"] @@ -202,13 +233,14 @@ class PointXYZ(Point, SQLSubModel): execute( "INSERT INTO " + - "geometry_pointXYZ(pamhyr_id, ind, name, " + - "x, y, z, profile, sl) " + + "geometry_pointXYZ(pamhyr_id, deleted, ind, name, " + + "x, y, z, profile, sl, scenario) " + "VALUES (" + - f"{self.pamhyr_id}, " + + f"{self.pamhyr_id}, {self._db_format(self.is_deleted())}, " + f"{ind}, '{self._db_format(self._name)}', " + f"{self.x}, {self.y}, {self.z}, " + - f"{profile.pamhyr_id}, {sl}" + + f"{profile.pamhyr_id}, {sl}, " + + f"{self._status.scenario_id}" + ")" ) @@ -246,7 +278,7 @@ class PointXYZ(Point, SQLSubModel): @x.setter def x(self, value): self._x = float(value) - self._status.modified() + self.modified() @property def y(self): @@ -255,7 +287,7 @@ class PointXYZ(Point, SQLSubModel): @y.setter def y(self, value): self._y = float(value) - self._status.modified() + self.modified() @property def z(self): @@ -264,7 +296,7 @@ class PointXYZ(Point, SQLSubModel): @z.setter def z(self, value): self._z = float(value) - self._status.modified() + self.modified() def is_nan(self): """