PamhyrDB: Add PamhyrID at parent class and add PamhyrID and

Scenario to AdditionalFiles.
scenarios
Pierre-Antoine Rouby 2024-07-19 15:27:10 +02:00
parent 3917b84b56
commit f19941f6da
4 changed files with 95 additions and 7 deletions

View File

@ -23,6 +23,8 @@ from tools import trace, timer
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Except import NotImplementedMethodeError
from Model.Scenario import Scenario
class AddFile(SQLSubModel):
_sub_classes = []
@ -114,13 +116,16 @@ class AddFile(SQLSubModel):
@classmethod
def _db_create(cls, execute):
execute("""
execute(f"""
CREATE TABLE additional_files(
id INTEGER NOT NULL PRIMARY KEY,
{cls.create_db_add_pamhyr_id()},
enabled BOOLEAN NOT NULL,
name TEXT NOT NULL,
path TEXT NOT NULL,
text TEXT NOT NULL
text TEXT NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@ -130,9 +135,14 @@ class AddFile(SQLSubModel):
def _db_update(cls, execute, version):
major, minor, release = version.strip().split(".")
if major == minor == "0":
if int(release) < 8:
release = int(release)
if release < 8:
cls._db_create(execute)
if release < 13:
cls.update_db_add_pamhyr_id(execute)
return True
@classmethod
@ -140,7 +150,7 @@ class AddFile(SQLSubModel):
new = []
table = execute(
"SELECT id, enabled, name, path, text " +
"SELECT pamhyr_id, enabled, name, path, text " +
"FROM additional_files"
)
@ -165,7 +175,7 @@ class AddFile(SQLSubModel):
def _db_save(self, execute, data=None):
sql = (
"INSERT INTO " +
"additional_files(id, enabled, name, path, text) " +
"additional_files(pamhyr_id, enabled, name, path, text) " +
"VALUES (" +
f"{self.id}, {self._enabled}, " +
f"'{self._db_format(self._name)}', " +

View File

@ -77,6 +77,14 @@ class Scenario(SQLSubModel):
")"
)
@classmethod
def create_db_add_scenario(cls):
return "scenario INTEGER NOT NULL DEFAULT 0"
@classmethod
def create_db_add_scenario_fk(cls):
return "FOREIGN KEY(scenario) REFERENCES scenario(id)"
@classmethod
def _db_update(cls, execute, version):
major, minor, release = version.strip().split(".")

View File

@ -26,6 +26,8 @@ from functools import reduce
from SQL import SQL
from Model.Except import NotImplementedMethodeError
from Model.Tools.PamhyrID import PamhyrID
logger = logging.getLogger()
# Top level model class
@ -153,7 +155,7 @@ class SQLModel(SQL):
# Sub model class
class SQLSubModel(object):
class SQLSubModel(PamhyrID):
_sub_classes = []
def _db_format(self, value):

View File

@ -0,0 +1,68 @@
# PamhyrID.py -- Pamhyr
# Copyright (C) 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 -*-
from tools import trace, timer
class PamhyrID(object):
_pamhyr_id_cnt = 0
def __init__(self, id: int = -1):
super(PamhyrID, self).__init__()
self._pamhyr_id = self.get_new_pamhyr_id(id)
def get_new_pamhyr_id(self, id):
pid = id
if pid == -1:
pid = PamhyrID._pamhyr_id_cnt
PamhyrID._pamhyr_id_cnt = max(
id + 1,
PamhyrID._pamhyr_id_cnt + 1
)
@property
def pamhyr_id(self):
return self._pamhyr_id
@classmethod
def update_db_add_pamhyr_id(cls, execute, table,
autoset=True):
execute(
f"ALTER TABLE {table} " +
f"ADD COLUMN pamhyr_id INTEGER"
)
if not autoset:
return True
table = execute(f"SELECT id FROM {table}")
for row in table:
pid = cls.get_new_pamhyr_id(-1)
execute(
f"UPDATE {table} " +
f"SET pamhyr_id = {pid} " +
f"WHERE id = {row[0]}"
)
@classmethod
def create_db_add_pamhyr_id(cls):
return "pamhyr_id INTEGER NOT NULL"