An error occurred while loading the file. Please try again.
-
Pierre-Antoine Rouby authored956a0714
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
# -*- coding: utf-8 -*-
import os
import sqlite3
import logging
from pathlib import Path
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):
fn = lambda sql: self.execute(
sql,
fetch_one = False,
commit = False
)
for cls in self._sub_classes:
requests = cls._sql_create(fn)
self.commit()
return True
def _create(self):
raise NotImplementedMethodeError(self, self._create)
def _update_submodel(self, version):
fn = lambda sql: self.execute(
sql,
fetch_one = False,
commit = False
)
ok = True
for cls in self._sub_classes:
ok &= cls._sql_update(fn, version)
self.commit()
return ok
def _update(self):
raise NotImplementedMethodeError(self, self._update)
def _save_submodel(self, objs, data = None):
fn = lambda sql: self.execute(
sql,
fetch_one = False,
commit = False
)
ok = True
for obj in objs:
ok &= obj._sql_save(fn)
self.commit()
return ok
def _save(self):
raise NotImplementedMethodeError(self, self._save)
@classmethod
def _load(cls, filename = None):
raise NotImplementedMethodeError(cls, cls._load)
# Sub model class
class SQLSubModel(object):
_sub_classes = []
def _sql_format(self, value):
# Replace ''' by ''' to preserve SQL injection
if type(value) == str:
value = value.replace("'", "'")
elif type(value) == bool:
value = 'TRUE' if value else 'FALSE'
return value
@classmethod
def _create_submodel(cls, execute):
for sc in cls._sub_classes:
sc._sql_create(execute)
@classmethod
def _sql_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._sql_create)
@classmethod
def _update_submodel(cls, execute, version):
for sc in cls._sub_classes:
sc._sql_update(execute, version)
@classmethod
def _sql_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._sql_update)
@classmethod
def _sql_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._sql_load)
def _save_submodel(self, execute, objs, data = None):
for o in objs:
o._sql_save(execute, data = data)
def _sql_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._sql_save)