-
Notifications
You must be signed in to change notification settings - Fork 0
/
pysql.py
443 lines (376 loc) · 17 KB
/
pysql.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from mysql import connector as mysql_connector
from typing import Union, List, Dict, Tuple, Callable, Any
class db_handler:
dbname: str
def __init__(self): ...
def execute(self, cmd, args=[]): ...
def close(self): ...
def __del__(self): ...
class mysql_handler(db_handler):
def __init__(self, host="localhost", user="root", password="", database="test"):
self.db = mysql_connector.connect(host=host, user=user, password=password, database=database)
self.cursor = self.db.cursor()
self.dbname = database
def execute(self, cmd, args: list = []) -> dict:
try:
self.cursor.execute(cmd, args)
except Exception as ex:
return Exception(str(ex))
try:
self.db.commit()
except:
pass
res = dict(lastrowid=self.cursor.lastrowid, rowcount=self.cursor.rowcount)
try:
res['data'] = self.cursor.fetchall()
except:
pass
return res
def close(self):
try:
self.cursor.close()
except:
pass
try:
self.db.close()
except:
pass
def __del__(self):
self.close()
def stringify(msg:str):
return msg.replace('\n', '\\n').replace('\r', '\\r').replace('"', '\\"').replace("'", "\\'")
class str_query(str):
def __init__(self, text:str) -> None:
super().__init__()
self.text = text
def __and__(self, other):
return str_query(self.text + " AND " + str(other))
def __or__(self, other):
return str_query(self.text + " OR " + str(other))
class DataType:
def __lt__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} < {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} < '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} < {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def __le__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} <= {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} <= '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} <= {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def __eq__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} = {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} = '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} = {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def __ne__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} != {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} != '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} != {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def __gt__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} > {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} > '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} > {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def __ge__(self, other):
if isinstance(other, Union[int, float]):
return str_query(f"{self.table}.{self.cname} >= {other}")
elif isinstance(other, str):
return str_query(f"{self.table}.{self.cname} >= '{stringify(other)}'")
elif isinstance(other, DataType):
return str_query(f"{self.table}.{self.cname} >= {other.table}.{other.cname}")
else:
raise TypeError(f"{other} is not a valid type for comparison")
def like(self, other):
if isinstance(other, str):
return str_query(f"{self.table}.{self.cname} LIKE '{stringify(other)}'")
else:
raise TypeError(f"{other} is not a valid type for comparison")
class INTEGER(DataType):
def __init__(self, primary_key=False, auto_increment=False, required=False, default=None, auto_number_start=1):
self.primary_key = primary_key
self.auto_increment = auto_increment
self.required = required
self.default = default
self.auto_number_start = auto_number_start
def __str__(self, multiple=False):
res = "INTEGER" + " PRIMARY KEY" * self.primary_key * (not multiple) + " AUTO_INCREMENT" * self.auto_increment + " NOT NULL" * self.required
if self.default is not None and self.default != "":
if type(self.default) in [int, float]:
res += " DEFAULT " + str(self.default)
else:
res += " DEFAULT '" + str(self.default) + "'"
return res
class TEXT(DataType):
def __init__(self, primary_key=False, required=False, default=None):
self.primary_key = primary_key
self.required = required
self.default = default
def __str__(self, multiple=False):
res = "TEXT" + " PRIMARY KEY" * self.primary_key * (not multiple) + " NOT NULL" * self.required
if self.default is not None:
res += " DEFAULT '" + str(self.default).replace("'", "\\\'").replace('"', '\\\"') + "'"
return res
class VARCHAR(DataType):
def __init__(self, length, primary_key=False, required=False, default=None):
self.length = length
self.primary_key = primary_key
self.required = required
self.default = default
def __str__(self, multiple=False):
res = f"VARCHAR({self.length})" + " PRIMARY KEY" * self.primary_key * (not multiple) + " NOT NULL" * self.required
if self.default is not None:
res += " DEFAULT '" + str(self.default).replace("'", "\\\'").replace('"', '\\\"') + "'"
return res
class BOOLIAN(DataType):
def __init__(self, primary_key=False, required=False, default=None):
self.primary_key = primary_key
self.required = required
self.default = default
def __str__(self, multiple=False):
res = "BOOL" + " PRIMARY KEY" * self.primary_key * (not multiple) + " NOT NULL" * self.required
if self.default is not None and self.default in [True, False]:
if type(self.default) in [int, bool]:
res += " DEFAULT " + str(bool(self.default)).upper()
else:
raise Exception("Default value for BOOL must be True or False")
return res
class JSON(DataType):
def __init__(self, primary_key=False, required=False):
self.primary_key = primary_key
self.required = required
def __str__(self, multiple=False):
res = "JSON" + " PRIMARY KEY" * self.primary_key * (not multiple) + " NOT NULL" * self.required
return res
class select_row:
def __init__(self, data:list, columns:list):
self.data = list(data)
self.columns = list(columns)
def __getitem__(self, item):
if isinstance(item, str):
index = self.columns.index(item)
return self.data[index]
elif isinstance(item, int):
return self.data[item]
else:
raise ValueError(f"{item} is not a valid type for indexing")
def __getattribute__(self, __name: str) -> Any:
try:
return object.__getattribute__(self, __name)
except:
if __name in object.__getattribute__(self, "columns"):
index = self.columns.index(__name)
return self.data[index]
else:
raise AttributeError
class select_table:
def __init__(self, data:list, columns:list):
self.data = data
self.columns = columns
def __getitem__(self, item):
if isinstance(item, str):
index = self.columns.index(item)
return [d[index] for d in self.data]
elif isinstance(item, int):
return select_row(self.data[item], self.columns)
else:
raise ValueError(f"{item} is not a valid type for indexing")
def __getattribute__(self, __name: str):
try:
return super().__getattribute__(__name)
except:
if __name in object.__getattribute__(self, "columns"):
return self[__name]
else:
raise AttributeError
def count(self):
return len(self.data)
def __len__(self):
return len(self.data)
def __iter__(self):
return iter(self.data)
def __next__(self):
return next(self.data)
class Database: ...
class table:
def __init__(self, tname:str, db: Database, columns:dict):
self.tname = tname
self.db = db
self.columns = columns
self.require_cols = [n for n, c in self.columns.items() if not (isinstance(c, INTEGER) and c.auto_increment)]
self._closed = False
def __getattribute__(self, __name: str):
if __name in (columns:=object.__getattribute__(self, "columns")):
columns[__name].table = self.tname
columns[__name].cname = __name
return columns[__name]
else:
return object.__getattribute__(self, __name)
def __getitem__(self, item):
return self.select(item)
def select(self, *args, WHERE=None):
if self._closed:
raise Exception("Table was not found")
tables = []
[tables.append(arg.table) for arg in args if arg.table not in tables]
if len(tables) < 1:
tables.append(self.tname)
sql = f"SELECT {', '.join([f'{arg.table}.{arg.cname}' for arg in args]) if len(args) > 0 else '*'} FROM {', '.join(tables)}{f' WHERE {WHERE}' if not WHERE is None else ''}"
return select_table(self.db.handler.execute(sql)['data'], args if len(args) > 0 else self.columns.keys())
def insert(self, *args, **kwargs):
if self._closed:
raise Exception("Table was not found")
if len(args) == 0 and len(kwargs) == 0:
raise Exception("No data provided")
column_data = {k: v.default for k, v in self.columns.items() if k in self.require_cols}
for index in range(len(args)):
if index >= len(self.require_cols):
raise Exception("Too many data provided")
column_data[self.require_cols[index]] = args[index]
for k, v in kwargs.items():
if k not in self.columns:
raise Exception("Unknown column: " + k)
column_data[k] = v
for k, v in column_data.items():
if v is None:
if self.columns[k].required:
raise Exception("Column " + k + " is required")
sql = "INSERT INTO " + self.tname + " (" + ", ".join(column_data.keys()) + ") VALUES (" + ", ".join(["%s"] * len(column_data)) + ")"
return self.db.handler.execute(sql, list(column_data.values()))['lastrowid']
def update(self, WHERE, **kwargs):
if self._closed:
raise Exception("Table was not found")
if WHERE is None:
raise Exception("No WHERE clause provided")
if len(kwargs) == 0:
raise Exception("No data provided")
for k, v in kwargs.items():
if k not in self.columns:
raise Exception("Unknown column: " + k)
if v is None:
if self.columns[k].required:
raise Exception("Column " + k + " is not equal to None")
sql = "UPDATE " + self.tname + " SET " + ", ".join([f"{k} = %s" for k in kwargs.keys()]) + " WHERE " + WHERE
return self.db.handler.execute(sql, list(kwargs.values()))['rowcount']
def delete(self, WHERE=None):
if self._closed:
raise Exception("Table was not found")
sql = "DELETE FROM " + self.tname
if not WHERE is None:
sql += " WHERE " + WHERE
return self.db.handler.execute(sql)['rowcount']
def drop(self):
if self._closed:
raise Exception("Table was not found")
sql = "DROP TABLE " + self.tname
self.db.handler.execute(sql)
self._closed = True
class Database:
def __init__(self, handher: db_handler):
self.handler = handher
self.dbname = self.handler.dbname
def create_table(self, tname:str, **tcolumns):
sql = "CREATE TABLE " + tname + " ("
columns = []
primary_keys = []
auto_number_start = 0
for key, value in tcolumns.items():
if type(value) in [INTEGER, VARCHAR, TEXT, BOOLIAN, JSON]:
columns.append(key + " " + value.__str__(multiple=True))
if value.primary_key:
primary_keys.append(key)
if type(value) == INTEGER and value.auto_increment:
auto_number_start = value.auto_number_start
else:
raise Exception("Invalid column type")
sql += ", ".join(columns)
if len(primary_keys) > 0:
sql += ", PRIMARY KEY (" + ", ".join(primary_keys) + ")"
sql += ")" + (" AUTO_INCREMENT=" + str(auto_number_start) if auto_number_start > 0 else "")
self.handler.execute(sql)
return table(tname, self, tcolumns)
def get_table(self, tname:str):
if self.is_table_exists(tname):
return table(tname, self, self.get_table_columns(tname))
else:
raise Exception("Table does not exist")
def load_tables_from_dict(self, tables:dict):
_tables = []
for tname, tcolumns in tables.items():
if not self.is_table_exists(tname):
cols = {}
for cname, cdata in tcolumns.items():
_type = None
match cdata["type"]:
case "int":
_type = INTEGER
case "varchar":
_type = VARCHAR
case "text":
_type = TEXT
case "boolian":
_type = BOOLIAN
case "json":
_type = JSON
case _:
raise Exception("Invalid column type")
cdata.pop("type")
cols[cname] = _type(**cdata)
_tables.append(self.create_table(tname, **cols))
else:
_tables.append(self.get_table(tname))
return _tables
def drop_table(self, tname:str):
if self.is_table_exists(tname):
sql = "DROP TABLE IF EXISTS " + tname
self.handler.execute(sql)
return True
else:
return False
def query(self, sql:str):
return self.handler.execute(sql)['data']
@property
def tables(self):
return [row[0] for row in self.handler.execute("SHOW TABLES")['data']]
def is_table_exists(self, tname:str):
return tname in self.tables
def get_table_columns(self, tname:str) -> dict:
cols = {}
for row in self.handler.execute("SELECT DATA_TYPE, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, COLUMN_KEY, EXTRA from INFORMATION_SCHEMA.COLUMNS where table_schema = %s and table_name = %s", (self.dbname, tname,))['data']:
match row:
case ('int', cname, default, is_nullable, None, key, extra):
cols[cname] = INTEGER(primary_key=key == "PRI", required=is_nullable == "NO", default=default, auto_increment="auto_increment" in extra)
case ('varchar', cname, default, is_nullable, length, key, extra):
cols[cname] = VARCHAR(length, primary_key=key == "PRI", required=is_nullable == "NO", default=default)
case ('text', cname, default, is_nullable, clenght, key, extra):
cols[cname] = TEXT(primary_key=key == "PRI", required=is_nullable == "NO")
case ('bool', cname, default, is_nullable, None, key, extra):
cols[cname] = BOOLIAN(primary_key=key == "PRI", required=is_nullable == "NO", default=default)
case ('json', cname, default, is_nullable, None, key, extra):
cols[cname] = JSON(primary_key=key == "PRI", required=is_nullable == "NO")
return cols
def __del__(self):
self.close()
def close(self):
self.handler.close()