From f19941f6da533d38bbb883265d8f16468e763352 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Fri, 19 Jul 2024 15:27:10 +0200 Subject: [PATCH] PamhyrDB: Add PamhyrID at parent class and add PamhyrID and Scenario to AdditionalFiles. --- src/Model/AdditionalFile/AddFile.py | 22 +++++++--- src/Model/Scenario.py | 8 ++++ src/Model/Tools/PamhyrDB.py | 4 +- src/Model/Tools/PamhyrID.py | 68 +++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/Model/Tools/PamhyrID.py diff --git a/src/Model/AdditionalFile/AddFile.py b/src/Model/AdditionalFile/AddFile.py index 76e7c963..c77bbb3f 100644 --- a/src/Model/AdditionalFile/AddFile.py +++ b/src/Model/AdditionalFile/AddFile.py @@ -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)}', " + diff --git a/src/Model/Scenario.py b/src/Model/Scenario.py index 4425af24..f522515b 100644 --- a/src/Model/Scenario.py +++ b/src/Model/Scenario.py @@ -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(".") diff --git a/src/Model/Tools/PamhyrDB.py b/src/Model/Tools/PamhyrDB.py index 94eab543..901af6da 100644 --- a/src/Model/Tools/PamhyrDB.py +++ b/src/Model/Tools/PamhyrDB.py @@ -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): diff --git a/src/Model/Tools/PamhyrID.py b/src/Model/Tools/PamhyrID.py new file mode 100644 index 00000000..3a77a6d2 --- /dev/null +++ b/src/Model/Tools/PamhyrID.py @@ -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 . + +# -*- 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"