-
Notifications
You must be signed in to change notification settings - Fork 0
/
vorm.py
425 lines (355 loc) · 11.4 KB
/
vorm.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from datetime import datetime, timedelta
import pymysql
import json
with open(__file__[:-8] + '/db.json', 'r', encoding='utf-8') as f:
json_dict = json.loads(f.read())
CREATE = "CREATE TABLE IF NOT EXISTS {} ({})"
INSERT = "INSERT INTO {}({}) VALUES ({})"
UPDATE = "UPDATE {} SET {} WHERE id = {}"
DELETE = "DELETE FROM {} {}"
SELECT = "SELECT * FROM {} {}"
SELECTANDCOUNT = "SELECT SQL_CALC_FOUND_ROWS * FROM {} {}"
DROP = "DROP TABLE IF EXISTS {}"
SHOWTABLE = "SHOW TABLES LIKE '{}'"
SHOWDATA = "SHOW DATABASES LIKE '{}'"
HOUR_DIFF = "HOUR(timediff(now(), {})) AS {}"
MINUTE_DIFF = "MINUTE(timediff(now(), {})) AS {}"
SECOND_DIFF = "SECOND(timediff(now(), {})) AS {}"
DAY_DIFF = "DAY(timediff(now(), {})) AS {}"
HOST = json_dict['HOST']
USER = json_dict['USER']
PWD = json_dict['PWD']
DB = json_dict['DB']
class Period:
TIMESTAMP = "{} BETWEEN '{}' AND '{}'"
def __init__(self, field, time_slot, moment):
if time_slot[0] is '+':
self.oper = True
else:
self.oper = False
self.field = field
self.moment = self.__get_moment(moment)
self.segment = self.__get_segment(time_slot[1:])
if self.oper:
self.other_moment = self.moment + self.segment
else:
self.other_moment = self.moment - self.segment
def __get_moment(self, moment):
if moment.upper().__eq__('NOW'):
moment = datetime.now()
else:
moment = datetime.strptime(moment, "%Y-%m-%d %H:%M:%S")
return moment
def __get_segment(self, time_slot):
time = time_slot.split(':')
for i in range(len(time)):
time[i] = int(time[i])
delta = None
if len(time) is 1:
delta = timedelta(seconds=time[-1])
elif len(time) is 2:
delta = timedelta(seconds=time[-1], minutes=time[-2])
elif len(time) is 3:
delta = timedelta(seconds=time[-1], minutes=time[-2], hours=time[-3])
elif len(time) is 4:
delta = timedelta(seconds=time[-1], minutes=time[-2], hours=time[-3], days=time[-4])
return delta
def get_sql(self):
if self.oper:
sql = Period.TIMESTAMP.format(self.field, self.moment, self.other_moment)
else:
sql = Period.TIMESTAMP.format(self.field, self.other_moment, self.moment)
return sql
def create_all_table():
for table in Module.get_tables():
rows = ''
role = ''
if not isexists(table['table']):
for k, v in table['rows'].items():
if v.__class__.__name__ == 'column':
rows = '{} {} {}, '.format(rows + k, v.type, v.col)
if v.__class__.__name__ == 'foreign':
role = '{} {}, '.format(role, v.col)
if role:
rows = rows + role
sql = CREATE.format(table['table'], rows[:-2])
execute(sql)
def isexists(table):
sql = SHOWTABLE.format(table)
result = execute_get_bool(sql)
return result
def create(mod):
table = mod.get_sql()['table']
rows = ''
for k, v in mod.get_sql()['rows'].items():
rows = rows + k + ' ' + v + ', '
sql = CREATE.format(table, rows[:-2])
execute(sql)
def insert(mod):
table = mod.table()
keys = ''
values = ''
for k, v in mod.get_attr().items():
if v is not None:
keys = keys + k + ', '
values = values + cover(v, "'{}'") + ', '
sql = INSERT.format(table, keys[:-2], values[:-2])
execute(sql)
def update(mod):
table = mod.table()
rows = ''
num = 0
for k, v in mod.get_attr().items():
if k.upper() == 'ID' and v is not None:
num = v
elif v is not None:
rows = rows + k + '=' + cover(v, "'{}'") + ', '
sql = UPDATE.format(table, rows[:-2], num)
execute(sql)
def delete(mod, islike=False):
table = mod.table()
if islike:
oper = ' like '
s = "'%{}%'"
else:
oper = ' = '
s = "'{}'"
rule = 'WHERE '
flag = False
if isinstance(mod.period, Period):
rule = rule + mod.period.get_sql() + ' and '
flag = True
for k, v in mod.get_attr().items():
if v is not None:
rule = rule + k + oper + cover(v, s) + ' and '
flag = True
if flag:
rule = rule[:-4]
else:
rule = ''
sql = DELETE.format(table, rule)
execute(sql)
def select(mod, islike=False, oderby='', isasc=True, limit=0, getone=False):
table = mod.table()
if islike:
oper = ' like '
s = "'%{}%'"
else:
oper = ' = '
s = "'{}'"
rule = 'WHERE '
flag = False
if mod.period:
rule = rule + mod.period.get_sql() + ' and '
flag = True
if mod.no_field:
rule = rule + mod.no_field
flag = True
for k, v in mod.get_attr().items():
if v is not None:
rule = rule + k + oper + cover(v, s) + ' and '
flag = True
if flag:
rule = rule[:-4]
else:
rule = ''
if oderby:
rule = rule + ' ORDER BY ' + oderby
if isasc:
rule = rule + ' ASC '
else:
rule = rule + ' DESC '
if limit:
rule = rule + 'LIMIT {}'.format(limit)
if mod.pagination:
limit = mod.pagination.get_limit()
rule = rule + 'LIMIT {}, {}'.format(limit[0], limit[1])
if mod.pagination:
sql = SELECTANDCOUNT.format(table, rule)
else:
sql = SELECT.format(table, rule)
if getone:
return execute_get_one(sql, mod)
else:
return execute_get(sql, mod.__class__, pagination=mod.pagination)
def drop(mod):
table = mod.table()
sql = DROP.format(table)
execute(sql)
def cover(attr, s):
if isinstance(attr, str) or isinstance(attr, datetime):
return s.format(attr)
else:
return str(attr)
def execute(sql):
db = pymysql.connect(HOST, USER, PWD, DB, charset="utf8")
cursor = db.cursor()
try:
cursor.execute(sql)
db.commit()
db.close()
except TypeError:
print('ERROR: unable to connect')
db.rollback()
db.close()
def execute_get(sql, clazz, pagination=None):
db = pymysql.connect(HOST, USER, PWD, DB, charset="utf8")
cursor = db.cursor()
try:
cursor.execute(sql)
results = cursor.fetchall()
items = []
for rows in results:
mod = clazz()
mod.set_attr(list(rows))
items.append(mod)
if pagination:
cursor.execute('SELECT FOUND_ROWS()')
count = cursor.fetchall()[0][0]
pagination.item_count = count
pagination.items = items
pagination.set_default()
return pagination
else:
return items
except IndexError:
print("Error: unable to fetch data")
finally:
db.close()
def execute_get_one(sql, mod):
db = pymysql.connect(HOST, USER, PWD, DB, charset="utf8")
cursor = db.cursor()
try:
cursor.execute(sql)
results = cursor.fetchall()
if results:
mod.set_attr(list(results[0]))
return mod
except IndexError:
print("Error: unable to fetch data")
finally:
db.close()
def execute_get_bool(sql):
db = pymysql.connect(HOST, USER, PWD, DB, charset="utf8")
cursor = db.cursor()
try:
cursor.execute(sql)
results = cursor.fetchall()
if results:
results = results[0][0]
else:
results = None
return results
except TypeError:
print("Error: unable to fetch data")
finally:
db.close()
class Pagination:
def __init__(self, page, paging):
self.page = page # now page
self.paging = paging # one page items count
self.item_count = None # all page items count
self.items = None # all items
self.pages = None
self.hasPrev = None
self.hasNext = None
self.hasItem = None
def get_limit(self):
return [(self.page - 1) * self.paging, self.paging]
def set_default(self):
self.pages = self.item_count // self.paging + 1
self.hasPrev = self.page is not 1
self.hasNext = self.page is not self.pages
self.hasItem = self.item_count
class ValueOfvSQLError(Exception):
def __init__(self, arg):
self.args = arg
class Module:
INIT = True
M_SELECT = 1
M_INSERT = 2
M_UPDATE = 3
M_DELETE = 4
M_DROP = 5
def __init__(self, **args):
self.pagination = None
self.period = None
self.no_field = None
for k in self.rows():
v = args.get(k)
self.__setattr__(k, v)
if Module.INIT and Module.__subclasses__():
Module.INIT = False
create_all_table()
@staticmethod
def get_tables():
tables = []
for clazz in Module.__subclasses__():
tables.append(clazz().get_sql())
return tables
def get_sql(self):
return {'table': self.table(), 'rows': self.rows()}
def table(self):
return self.__class__.__name__
def rows(self):
rows = {}
for r in self.__class__.__dict__.items():
if (r[1].__class__.__name__ is 'column' or r[1].__class__.__name__ is 'foreign') \
and r[0][0] is not '_':
rows[r[0]] = r[1]
return rows
def set_attr(self, params):
for row in self.rows().keys():
if params or isinstance(params, int) or isinstance(params, bool):
self.__setattr__(row, params.pop(0))
def get_attr(self):
attr = {}
for k in self.rows():
attr[k] = self.__getattribute__(k)
return attr
def __str__(self):
for k in self.rows():
v = self.__getattribute__(k)
def set_pagination(self, page, paging):
self.pagination = Pagination(paging=paging, page=page)
return self
def set_period(self, field, time_slot, moment="now"):
self.period = Period(field, time_slot, moment)
return self
def set_no_field(self, **args):
field = ''
for k, v in args:
field = field + k + '!=' + cover(v, "'{}'") + ' and '
self.no_field = field
return self
def isexists(self):
return isexists(self.table()) is not None
def create(self):
create(self)
def insert(self):
self.listener_begin(do=Module.M_INSERT)
insert(self)
item = select(self, getone=True)
self.listener_end(do=Module.M_INSERT)
return item
def insert_without_return(self):
insert(self)
def update(self):
self.listener_begin(do=Module.M_UPDATE)
update(self)
item = select(self, getone=True)
self.listener_end(do=Module.M_UPDATE)
return item
def delete(self, islike=False):
self.listener_begin(do=Module.M_DELETE)
delete(self, islike=islike)
self.listener_end(do=Module.M_DELETE)
def select(self, islike=False, oderby='', isasc=True, limit=0, getone=False):
self.listener_begin(do=Module.M_SELECT)
items = select(self, islike=islike, oderby=oderby, isasc=isasc, limit=limit, getone=getone)
self.listener_end(do=Module.M_SELECT)
return items
def drop(self):
return drop(self)
def listener_begin(self, do): pass
def listener_end(self, do): pass