An error occurred while loading the file. Please try again.
-
Pierre-Antoine Rouby authored878c1403
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# PamhyrDB.py -- Pamhyr abstract model database classes
# Copyright (C) 2023 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 os
import sqlite3
import logging
from pathlib import Path
from functools import reduce
from tools import SQL
from Model.Except import NotImplementedMethodeError
logger = logging.getLogger()
# Top level model class
class SQLModel(SQL):
_sub_classes = []
def _init_db_file(self, db, is_new=True):
exists = Path(db).exists()
if exists and is_new:
os.remove(db)
self._db = sqlite3.connect(db)
self._cur = self._db.cursor()
if is_new:
logger.info("Create database")
self._create() # Create db
# self._save() # Save
else:
self._update() # Update db scheme if necessary
# self._load() # Load data
def __init__(self, filename=None):
self._db = None
def _create_submodel(self):
def fn(sql): return self.execute(
sql,
fetch_one=False,
commit=False
)
for cls in self._sub_classes:
requests = cls._db_create(fn)
self.commit()
return True
def _create(self):
raise NotImplementedMethodeError(self, self._create)
def _update_submodel(self, version):
def fn(sql): return self.execute(
sql,
fetch_one=False,
commit=False
)
ok = True
for cls in self._sub_classes:
ok &= cls._db_update(fn, version)
self.commit()
return ok
def _update(self):
raise NotImplementedMethodeError(self, self._update)
def _save_submodel(self, objs, data=None):
progress = data if data is not None else lambda: None
def fn(sql):
res = self.execute(
sql,
fetch_one=False,
commit=False
)
progress()
return res
ok = True
for obj in objs:
ok &= obj._db_save(fn)
self.commit()
return ok
def _save(self, progress=None):
raise NotImplementedMethodeError(self, self._save)
def _count(self):
raise NotImplementedMethodeError(self, self._count)
def _save_count(self, objs, data=None):
counter = {
"insert": 0,
"update": 0,
"delete": 0,
"other": 0,
}
def fn(sql):
if "insert" in sql.lower():
counter["insert"] = counter["insert"] + 1
elif "update" in sql.lower():
counter["update"] = counter["update"] + 1
elif "delete" in sql.lower():
counter["delete"] = counter["delete"] + 1
else:
counter["other"] = counter["other"] + 1
return []
ok = True
for obj in objs:
ok &= obj._db_save(fn)
logger.debug(counter)
return reduce(
lambda acc, k: acc + counter[k],
counter,
0
)
@classmethod
def _load(cls, filename=None):
raise NotImplementedMethodeError(cls, cls._load)
# Sub model class
class SQLSubModel(object):
_sub_classes = []
def _db_format(self, value):
# Replace ''' by ''' to preserve SQL injection
if type(value) is str:
value = value.replace("'", "'")
elif type(value) is bool:
value = 'TRUE' if value else 'FALSE'
return value
@classmethod
def _create_submodel(cls, execute):
for sc in cls._sub_classes:
sc._db_create(execute)
@classmethod
def _db_create(cls, execute):
"""Create data base scheme
Args:
execute: Function to exec SQL resquest
Returns:
Return true, otherelse false if an issue appear
"""
raise NotImplementedMethodeError(cls, cls._db_create)
@classmethod
def _update_submodel(cls, execute, version):
for sc in cls._sub_classes:
sc._db_update(execute, version)
@classmethod
def _db_update(cls, execute, version):
"""Update data base scheme
Args:
execute: Function to exec SQL resquest
version: Current database version
Returns:
Return true, otherelse false if an issue appear
"""
raise NotImplementedMethodeError(cls, cls._db_update)
@classmethod
def _db_load(cls, execute, data=None):
"""Load instance of this class from SQL data base
Args:
execute: Function to exec SQL request
data: Optional data for the class constructor
Returns:
Return new instance of class
"""
raise NotImplementedMethodeError(cls, cls._db_load)
def _save_submodel(self, execute, objs, data=None):
for o in objs:
o._db_save(execute, data=data)
def _db_save(self, execute, data=None):
"""Save class data to data base
Args:
execute: Function to exec SQL resquest
data: Optional additional information for save
Returns:
Return true, otherelse false if an issue appear during
save
"""
raise NotImplementedMethodeError(self, self._db_save)