mirror of https://gitlab.com/pamhyr/pamhyr2
238 lines
6.1 KiB
Python
238 lines
6.1 KiB
Python
# HydraulicStructures.py -- Pamhyr
|
|
# Copyright (C) 2023-2024 INRAE
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import logging
|
|
|
|
from tools import trace, timer
|
|
|
|
from Model.Tools.PamhyrDB import SQLSubModel
|
|
from Model.Scenario import Scenario
|
|
from Model.Except import NotImplementedMethodeError
|
|
|
|
from Model.HydraulicStructures.Basic.Value import (
|
|
BHSValue
|
|
)
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
class BasicHS(SQLSubModel):
|
|
_sub_classes = [
|
|
BHSValue,
|
|
]
|
|
_id_cnt = 0
|
|
|
|
def __init__(self, id: int = -1, name: str = "",
|
|
status=None):
|
|
super(BasicHS, self).__init__(id)
|
|
|
|
self._status = status
|
|
|
|
self._name = name
|
|
self._type = ""
|
|
self._enabled = True
|
|
self._data = []
|
|
|
|
@classmethod
|
|
def _db_create(cls, execute, ext=""):
|
|
execute(f"""
|
|
CREATE TABLE hydraulic_structures_basic{ext} (
|
|
{cls.create_db_add_pamhyr_id()},
|
|
name TEXT NOT NULL,
|
|
type TEXT NOT NULL,
|
|
enabled BOOLEAN NOT NULL,
|
|
hs INTEGER,
|
|
{Scenario.create_db_add_scenario()},
|
|
{Scenario.create_db_add_scenario_fk()},
|
|
FOREIGN KEY(hs) REFERENCES hydraulic_structures(pamhyr_id),
|
|
PRIMARY KEY(pamhyr_id, scenario)
|
|
)
|
|
""")
|
|
|
|
if ext == "_tmp":
|
|
return True
|
|
|
|
return cls._create_submodel(execute)
|
|
|
|
@classmethod
|
|
def _db_update(cls, execute, version, data=None):
|
|
major, minor, release = version.strip().split(".")
|
|
if major == minor == "0":
|
|
if int(release) < 6:
|
|
cls._db_create(execute)
|
|
return True
|
|
else:
|
|
cls._db_update_to_0_1_0(execute, data)
|
|
|
|
return cls._update_submodel(execute, version, data)
|
|
|
|
@classmethod
|
|
def _db_update_to_0_1_0(cls, execute, data):
|
|
table = "hydraulic_structures_basic"
|
|
hs = data['id2pid']['hydraulic_structures']
|
|
|
|
cls.update_db_add_pamhyr_id(execute, table, data)
|
|
cls._db_update_to_0_1_0_set_hs_pid(execute, table, hs)
|
|
Scenario.update_db_add_scenario(execute, table)
|
|
|
|
cls._db_create(execute, ext="_tmp")
|
|
|
|
execute(
|
|
f"INSERT INTO {table}_tmp " +
|
|
"(pamhyr_id, name, type, enabled, hs, scenario) " +
|
|
"SELECT pamhyr_id, name, type, enabled, hs, scenario " +
|
|
f"FROM {table}"
|
|
)
|
|
|
|
execute(f"DROP TABLE {table}")
|
|
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
|
|
|
|
@classmethod
|
|
def _db_update_to_0_1_0_set_hs_pid(cls, execute, table, hs):
|
|
els = execute(
|
|
f"SELECT pamhyr_id, hs FROM {table}"
|
|
)
|
|
|
|
for row in els:
|
|
it = iter(row)
|
|
pid = next(it)
|
|
hs_id = next(it)
|
|
|
|
execute(
|
|
f"UPDATE {table} " +
|
|
f"SET hs = {hs[hs_id]} " +
|
|
f"WHERE pamhyr_id = {pid}"
|
|
)
|
|
|
|
@classmethod
|
|
def _get_ctor_from_type(cls, t):
|
|
from Model.HydraulicStructures.Basic.Types import (
|
|
BHS_types, NotDefined,
|
|
)
|
|
|
|
res = NotDefined
|
|
|
|
if t in BHS_types.keys():
|
|
res = BHS_types[t]
|
|
|
|
return res
|
|
|
|
@classmethod
|
|
def _db_load(cls, execute, data=None):
|
|
new = []
|
|
|
|
table = execute(
|
|
"SELECT pamhyr_id, name, type, enabled, hs " +
|
|
"FROM hydraulic_structures_basic " +
|
|
f"WHERE hs = {data['hs_id']} "
|
|
)
|
|
|
|
for row in table:
|
|
it = iter(row)
|
|
|
|
bhs_pid = next(it)
|
|
name = next(it)
|
|
type = next(it)
|
|
enabled = (next(it) == 1)
|
|
hs_id = next(it)
|
|
|
|
ctor = cls._get_ctor_from_type(type)
|
|
bhs = ctor(
|
|
id=bhs_pid,
|
|
name=name,
|
|
status=data['status']
|
|
)
|
|
|
|
bhs.enabled = enabled
|
|
|
|
data['bhs_id'] = bhs_pid
|
|
bhs._data = BHSValue._db_load(
|
|
execute, data
|
|
)
|
|
|
|
new.append(bhs)
|
|
|
|
return new
|
|
|
|
def _db_save(self, execute, data=None):
|
|
hs_id = data['hs_id']
|
|
|
|
sql = (
|
|
"INSERT INTO " +
|
|
"hydraulic_structures_basic(pamhyr_id, name, type, enabled, hs) " +
|
|
"VALUES (" +
|
|
f"{self.pamhyr_id}, " +
|
|
f"'{self._db_format(self._name)}', " +
|
|
f"'{self._db_format(self._type)}', " +
|
|
f"{self._db_format(self.enabled)}, " +
|
|
f"{hs_id} " +
|
|
")"
|
|
)
|
|
execute(sql)
|
|
|
|
data['bhs_id'] = self.pamhyr_id
|
|
execute(
|
|
"DELETE FROM hydraulic_structures_basic_value " +
|
|
f"WHERE bhs = {self.pamhyr_id}"
|
|
)
|
|
|
|
for values in self._data:
|
|
values._db_save(execute, data)
|
|
|
|
return True
|
|
|
|
def __len__(self):
|
|
return len(self._data)
|
|
|
|
@property
|
|
def name(self):
|
|
if self._name == "":
|
|
return f"{self._type} #{self.pamhyr_id}"
|
|
|
|
return self._name
|
|
|
|
@name.setter
|
|
def name(self, name):
|
|
self._name = name
|
|
self._status.modified()
|
|
|
|
@property
|
|
def type(self):
|
|
return self._type
|
|
|
|
@type.setter
|
|
def type(self, type):
|
|
self._type = type
|
|
self._status.modified()
|
|
|
|
@property
|
|
def enabled(self):
|
|
return self._enabled
|
|
|
|
@enabled.setter
|
|
def enabled(self, enabled):
|
|
self._enabled = enabled
|
|
self._status.modified()
|
|
|
|
@property
|
|
def parameters(self):
|
|
return self._data
|
|
|
|
def convert(self, new_type):
|
|
return new_type(id=self.id, name=self.name, status=self._status)
|