mirror of https://gitlab.com/pamhyr/pamhyr2
SL: Prepare update db 0.1.0.
parent
2ff635d0cd
commit
9deccb465e
|
|
@ -26,7 +26,6 @@ from Model.Except import NotImplementedMethodeError
|
||||||
|
|
||||||
class Layer(SQLSubModel):
|
class Layer(SQLSubModel):
|
||||||
_sub_classes = []
|
_sub_classes = []
|
||||||
_id_cnt = 0
|
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
id: int = -1, name: str = "",
|
id: int = -1, name: str = "",
|
||||||
|
|
@ -34,7 +33,7 @@ class Layer(SQLSubModel):
|
||||||
height=0.0, d50=0.0, sigma=0.0,
|
height=0.0, d50=0.0, sigma=0.0,
|
||||||
critical_constraint=0.0,
|
critical_constraint=0.0,
|
||||||
sl=None, status=None):
|
sl=None, status=None):
|
||||||
super(Layer, self).__init__()
|
super(Layer, self).__init__(id)
|
||||||
|
|
||||||
self._status = status
|
self._status = status
|
||||||
|
|
||||||
|
|
@ -46,13 +45,6 @@ class Layer(SQLSubModel):
|
||||||
self._sigma = sigma
|
self._sigma = sigma
|
||||||
self._critical_constraint = critical_constraint
|
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
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
if self._name == "":
|
if self._name == "":
|
||||||
|
|
@ -105,10 +97,10 @@ class Layer(SQLSubModel):
|
||||||
self._critical_constraint = float(critical_constraint)
|
self._critical_constraint = float(critical_constraint)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _db_create(cls, execute):
|
def _db_create(cls, execute, ext=""):
|
||||||
execute("""
|
execute(f"""
|
||||||
CREATE TABLE sedimentary_layer_layer(
|
CREATE TABLE sedimentary_layer_layer{ext} (
|
||||||
id INTEGER NOT NULL PRIMARY KEY,
|
{cls.create_db_add_pamhyr_id()}
|
||||||
ind INTEGER NOT NULL,
|
ind INTEGER NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
type TEXT NOT NULL,
|
type TEXT NOT NULL,
|
||||||
|
|
@ -117,7 +109,10 @@ class Layer(SQLSubModel):
|
||||||
sigma REAL NOT NULL,
|
sigma REAL NOT NULL,
|
||||||
critical_constraint REAL NOT NULL,
|
critical_constraint REAL NOT NULL,
|
||||||
sl INTEGER,
|
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:
|
if int(release) < 2:
|
||||||
cls._db_create(execute)
|
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
|
@classmethod
|
||||||
def _db_load(cls, execute, data=None):
|
def _db_load(cls, execute, data=None):
|
||||||
|
|
@ -138,27 +156,33 @@ class Layer(SQLSubModel):
|
||||||
sl = data["sl"]
|
sl = data["sl"]
|
||||||
|
|
||||||
table = execute(
|
table = execute(
|
||||||
"SELECT id, ind, name, type, height, " +
|
"SELECT pamhyr_id, name, type, height, " +
|
||||||
"d50, sigma, critical_constraint " +
|
"d50, sigma, critical_constraint " +
|
||||||
"FROM sedimentary_layer_layer " +
|
"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:
|
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(
|
layer = cls(
|
||||||
id=row[0], name=row[2],
|
id=pid, name=name,
|
||||||
type=row[3], height=row[4],
|
type=type, height=height,
|
||||||
d50=row[5], sigma=row[6],
|
d50=d50, sigma=sigma,
|
||||||
critical_constraint=row[7],
|
critical_constraint=critical_constraint,
|
||||||
sl=sl, status=data['status']
|
sl=sl, status=data['status']
|
||||||
)
|
)
|
||||||
|
|
||||||
new[ind] = layer
|
new.append(layer)
|
||||||
|
|
||||||
return new
|
return new
|
||||||
|
|
||||||
|
|
@ -166,43 +190,34 @@ class Layer(SQLSubModel):
|
||||||
ind = data["ind"]
|
ind = data["ind"]
|
||||||
sl = data["sl"]
|
sl = data["sl"]
|
||||||
|
|
||||||
sql = (
|
execute(
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"sedimentary_layer_layer(id, ind, name, type, height, " +
|
"sedimentary_layer_layer(id, ind, name, type, height, " +
|
||||||
"d50, sigma, critical_constraint, sl) " +
|
"d50, sigma, critical_constraint, sl) " +
|
||||||
"VALUES (" +
|
"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._db_format(self._type)}', {self._height}, " +
|
||||||
f"{self._d50}, {self._sigma}, {self._critical_constraint}, " +
|
f"{self._d50}, {self._sigma}, {self._critical_constraint}, " +
|
||||||
f"{sl.id}" +
|
f"{sl.pamhyr_id}" +
|
||||||
")"
|
")"
|
||||||
)
|
)
|
||||||
execute(sql)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class SedimentLayer(SQLSubModel):
|
class SedimentLayer(SQLSubModel):
|
||||||
_sub_classes = [Layer]
|
_sub_classes = [Layer]
|
||||||
_id_cnt = 0
|
|
||||||
|
|
||||||
def __init__(self, id: int = -1,
|
def __init__(self, id: int = -1,
|
||||||
name: str = "", comment: str = "",
|
name: str = "", comment: str = "",
|
||||||
status=None):
|
status=None):
|
||||||
super(SedimentLayer, self).__init__()
|
super(SedimentLayer, self).__init__(id)
|
||||||
|
|
||||||
self._status = status
|
self._status = status
|
||||||
self._name = name
|
self._name = name
|
||||||
self._comment = comment
|
self._comment = comment
|
||||||
self._layers = []
|
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):
|
def __str__(self):
|
||||||
s = f"{self.name} ({len(self)})"
|
s = f"{self.name} ({len(self)})"
|
||||||
|
|
||||||
|
|
@ -248,12 +263,15 @@ class SedimentLayer(SQLSubModel):
|
||||||
self._comment = comment
|
self._comment = comment
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _db_create(cls, execute):
|
def _db_create(cls, execute, ext=""):
|
||||||
execute("""
|
execute(f"""
|
||||||
CREATE TABLE sedimentary_layer(
|
CREATE TABLE sedimentary_layer{ext} (
|
||||||
id INTEGER NOT NULL PRIMARY KEY,
|
{cls.create_db_add_pamhyr_id()}
|
||||||
name TEXT NOT NULL,
|
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:
|
if int(release) < 2:
|
||||||
cls._db_create(execute)
|
cls._db_create(execute)
|
||||||
|
|
||||||
|
cls._db_update_to_0_1_0(execute, data)
|
||||||
|
|
||||||
return cls._update_submodel(execute, version, 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
|
@classmethod
|
||||||
def _db_load(cls, execute, data=None):
|
def _db_load(cls, execute, data=None):
|
||||||
new = []
|
new = []
|
||||||
|
|
@ -296,12 +335,12 @@ class SedimentLayer(SQLSubModel):
|
||||||
if data is None:
|
if data is None:
|
||||||
data = {}
|
data = {}
|
||||||
|
|
||||||
sql = (
|
execute(
|
||||||
"INSERT INTO sedimentary_layer (id, name, comment) " +
|
"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)}')"
|
f"'{self._db_format(self._comment)}')"
|
||||||
)
|
)
|
||||||
execute(sql)
|
|
||||||
|
|
||||||
data["sl"] = self
|
data["sl"] = self
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
from tools import trace, timer
|
from tools import trace, timer
|
||||||
|
|
||||||
from Model.Tools.PamhyrDB import SQLSubModel
|
from Model.Tools.PamhyrDB import SQLSubModel
|
||||||
|
from Model.Scenario import Scenario
|
||||||
|
|
||||||
|
|
||||||
class Stricklers(SQLSubModel):
|
class Stricklers(SQLSubModel):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue