SL: Prepare update db 0.1.0.

scenarios
Pierre-Antoine Rouby 2024-07-29 11:26:42 +02:00
parent 2ff635d0cd
commit 9deccb465e
2 changed files with 87 additions and 47 deletions

View File

@ -26,7 +26,6 @@ from Model.Except import NotImplementedMethodeError
class Layer(SQLSubModel):
_sub_classes = []
_id_cnt = 0
def __init__(self,
id: int = -1, name: str = "",
@ -34,7 +33,7 @@ class Layer(SQLSubModel):
height=0.0, d50=0.0, sigma=0.0,
critical_constraint=0.0,
sl=None, status=None):
super(Layer, self).__init__()
super(Layer, self).__init__(id)
self._status = status
@ -46,13 +45,6 @@ class Layer(SQLSubModel):
self._sigma = sigma
self._critical_constraint = critical_constraint
if id == -1:
self.id = Layer._id_cnt
else:
self.id = id
Layer._id_cnt = max(id, Layer._id_cnt+1)
@property
def name(self):
if self._name == "":
@ -105,10 +97,10 @@ class Layer(SQLSubModel):
self._critical_constraint = float(critical_constraint)
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE sedimentary_layer_layer(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE sedimentary_layer_layer{ext} (
{cls.create_db_add_pamhyr_id()}
ind INTEGER NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
@ -117,7 +109,10 @@ class Layer(SQLSubModel):
sigma REAL NOT NULL,
critical_constraint REAL NOT NULL,
sl INTEGER,
FOREIGN KEY(sl) REFERENCES sedimentary_layer(id)
FOREIGN KEY(sl) REFERENCES sedimentary_layer(pamhyr_id),
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@ -130,7 +125,30 @@ class Layer(SQLSubModel):
if int(release) < 2:
cls._db_create(execute)
return True
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 = "sedimentary_layer_layer"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, type, height, " +
"d50, sigma, critical_constraint, sl, scenario) " +
"SELECT pamhyr_id, name, type, height, " +
"d50, sigma, critical_constraint, sl, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
@ -138,27 +156,33 @@ class Layer(SQLSubModel):
sl = data["sl"]
table = execute(
"SELECT id, ind, name, type, height, " +
"SELECT pamhyr_id, name, type, height, " +
"d50, sigma, critical_constraint " +
"FROM sedimentary_layer_layer " +
f"WHERE sl = {sl}"
f"WHERE sl = {sl} " +
"ORDER BY ind ASC"
)
for _ in table:
new.append(None)
for row in table:
ind = row[1]
it = iter(row)
pid = next(it)
name = next(it)
type = next(it)
height = next(it)
d50 = next(it)
sigma = next(it)
critical_constraint = next(it)
layer = cls(
id=row[0], name=row[2],
type=row[3], height=row[4],
d50=row[5], sigma=row[6],
critical_constraint=row[7],
id=pid, name=name,
type=type, height=height,
d50=d50, sigma=sigma,
critical_constraint=critical_constraint,
sl=sl, status=data['status']
)
new[ind] = layer
new.append(layer)
return new
@ -166,43 +190,34 @@ class Layer(SQLSubModel):
ind = data["ind"]
sl = data["sl"]
sql = (
execute(
"INSERT INTO " +
"sedimentary_layer_layer(id, ind, name, type, height, " +
"d50, sigma, critical_constraint, sl) " +
"VALUES (" +
f"{self.id}, {ind}, '{self._db_format(self._name)}', " +
f"{self.pamhyr_id}, {ind}, '{self._db_format(self._name)}', " +
f"'{self._db_format(self._type)}', {self._height}, " +
f"{self._d50}, {self._sigma}, {self._critical_constraint}, " +
f"{sl.id}" +
f"{sl.pamhyr_id}" +
")"
)
execute(sql)
return True
class SedimentLayer(SQLSubModel):
_sub_classes = [Layer]
_id_cnt = 0
def __init__(self, id: int = -1,
name: str = "", comment: str = "",
status=None):
super(SedimentLayer, self).__init__()
super(SedimentLayer, self).__init__(id)
self._status = status
self._name = name
self._comment = comment
self._layers = []
if id == -1:
self.id = SedimentLayer._id_cnt
else:
self.id = id
SedimentLayer._id_cnt = max(id, SedimentLayer._id_cnt+1)
def __str__(self):
s = f"{self.name} ({len(self)})"
@ -248,12 +263,15 @@ class SedimentLayer(SQLSubModel):
self._comment = comment
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE sedimentary_layer(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE sedimentary_layer{ext} (
{cls.create_db_add_pamhyr_id()}
name TEXT NOT NULL,
comment TEXT NOT NULL
comment TEXT NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@ -266,8 +284,29 @@ class SedimentLayer(SQLSubModel):
if int(release) < 2:
cls._db_create(execute)
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 = "sedimentary_layer"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, comment, scenario) " +
"SELECT pamhyr_id, name, comment, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
new = []
@ -296,12 +335,12 @@ class SedimentLayer(SQLSubModel):
if data is None:
data = {}
sql = (
execute(
"INSERT INTO sedimentary_layer (id, name, comment) " +
f"VALUES ({self.id}, '{self._db_format(self._name)}', " +
f"VALUES ({self.pamhyr_id}, " +
f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._comment)}')"
)
execute(sql)
data["sl"] = self

View File

@ -19,6 +19,7 @@
from tools import trace, timer
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Scenario import Scenario
class Stricklers(SQLSubModel):