-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL_ORM.py
340 lines (280 loc) · 12.2 KB
/
MySQL_ORM.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
import MySQLdb
import re
MySQL_Identifier_regex = re.compile("^[0-9a-zA-Z$_]+$")
class MySQL_InvalidColumnNameError(Exception): pass
class MySQL_InvalidTableNameError(Exception): pass
class MySQL_InvalidDatabaseNameError(Exception): pass
class MySQL_TableError(Exception): pass
class MySQL_LimitTypeError(Exception): pass
class MySQL_ConditionTypeError(Exception): pass
class MySQL_ConditionFormatError(Exception): pass
class MySQL_InavlidSizeError(Exception): pass
class MySQL_Object: pass
class MySQL_Integer(MySQL_Object):
def render(self): return "INT"
class MySQL_BigInteger(MySQL_Object):
def render(self): return "BIGINT"
class MySQL_Float(MySQL_Object):
def render(self): return "FLOAT"
class MySQL_Double(MySQL_Object):
def render(self): return "DOUBLE"
class MySQL_Boolean(MySQL_Object):
def render(self): return "BOOLEAN"
class MySQL_Char(MySQL_Object):
def __init__(self, n):
if n<0 or n>255: raise MySQL_InvalidSizeError("MySQL Invalid size for CHAR: " + n)
else: self.n = n
def render(self): return "CHAR(" + str(self.n) + ")"
class MySQL_Varchar(MySQL_Object):
def __init__(self, n):
if n<0 or n>65535: raise MySQL_InvalidSizeError("MySQL Invalid size for VARCHAR: " + n)
else: self.n = n
def render(self): return "VARCHAR(" + str(self.n) + ")"
class MySQL_VarcharBinary(MySQL_Object):
def __init__(self, n):
if n<0 or n>65535: raise MySQL_InvalidSizeError("MySQL Invalid size for VARCHAR: " + n)
else: self.n = n
def render(self): return "VARCHAR(" + str(self.n) + ") BINARY"
class MySQL_Text(MySQL_Object):
def render(self): return "TEXT"
class MySQL_Blob(MySQL_Object):
def __init__(self, n=65535):
self.t = ''
if n>0 and n<=255: self.t = 'TINYBLOB'
elif n<=65535: self.t = 'BLOB'
elif n<=16777216: self.t = 'MEDIUMBLOB'
elif n<=4294967296: self.t = 'LONGBLOB'
else: raise MySQL_InvalidSizeError("MySQL Invalid size for TINYBLOB: " + n)
def render(self): return self.t
class MySQL_Condition:
def __init__(self, columnName, relationalOperator, value):
global MySQL_Identifier__regex
relationalOperator = relationalOperator.lower()
if relationalOperator not in ['=', '!=', '>=', '<=', '<', '>', 'like']:
raise MySQL_ConditionFormatError("invalid relational operator '" + relationalOperator + "'")
if MySQL_Identifier_regex.match(columnName):
self.columnName, self.relationalOperator, self.value = columnName, relationalOperator, value
else:
raise MySQL_InvalidColumnNameError("invalid column name ")
def render(self):
return self.columnName + " " + self.relationalOperator + " %s", [self.value]
class MySQL_Conditions:
def __init__(self, condtion1, logical_operator, condition2):
if logical_operator.lower() not in ['and', 'or']:
raise MySQL_ConditionFormatError("MySQL Condition Format Error, invalid logical operator: " + logical_operator)
self.condition1, self.logical_operator, self.condition2 = condtion1.render(), logical_operator, condition2.render()
def render(self):
values = []
for value in self.condition1[1]: values.append(value)
for value in self.condition2[1]: values.append(value)
return self.condition1[0] + " " + self.logical_operator + " " + self.condition2[0], values
class MySQL_Columns:
def __init__(self):
global MySQL_Identifier_regex
self.regex, self.columns = MySQL_Identifier_regex, []
def add(self, *args):
for arg in args:
if self.regex.match(arg):
self.columns.append(arg)
else:
self.columns = []
return False
return True
def render(self):
if len(self.columns) == 0:
return "*"
else:
return ",".join(self.columns)
def clear(self):
self.columns = []
class MySQL_Column:
def __init__(self, name, type_s, primary_key=False, nullable=False, unique=False):
if MySQL_Identifier_regex.match(name):
self.name, self.t, self.is_pk, self.nullable, self.unique = name, type_s.render(), primary_key, nullable, unique
else:
raise MySQL_InvalidColumnNameError("MySQL Invalid Column Name Error: " + name)
class MySQL_Table:
def __init__(self, sql, tableName, dbName):
self.name, self.sql, self.dbName, self.condition, self.order, self.where_str = tableName, sql, dbName, '', '', ''
self.columns, self.values, self.values1 = MySQL_Columns(), [], []
def exists(self):
cur_dbName = self.sql.current_dbName
exists = self.sql.set_database(self.dbName).table_exists(self.name)
self.sql.set_database(cur_dbName)
return exists
def insert(self, values):
cols = self.get_columns()
keys, vals = [], []
for k in values:
v = values[k]
if k not in cols:
raise MySQL_InvalidColumnNameError("MySQL Invalid Column Name Error: " + k)
keys.append(k)
vals.append(v)
query = "INSERT INTO " + self.name + "(" + ",".join(keys) + ") VALUES(" + ",".join(["%s"]*len(keys)) + ")"
self.sql.execute_query (
query, vals
)
def get_columns(self):
data = self.sql.execute_query (
"""SELECT column_name
FROM information_schema.columns
WHERE table_schema = %s AND table_name = %s
""", (self.dbName, self.name)
)
return [x[0] for x in data]
def set(self, ops):
o, o1 = [], []
for op in ops:
if MySQL_Identifier_regex.match(op): o.append(op + "=%s"); o1.append(ops[op])
else: raise MySQL_InvalidColumnNameError("MySQL Invalid Column name: " + a[0])
cur_dbName = self.sql.current_dbName
self.sql.set_database(self.dbName).execute_query (
"UPDATE " + self.name + " SET " + ",".join(o) + self.where_str, o1 + self.values1
)
self.sql.set_database(cur_dbName)
def where(self, condition):
if isinstance(condition, (MySQL_Condition, MySQL_Conditions)):
self.where_str, self.values1 = condition.render()
self.where_str = " WHERE " + self.where_str
else:
raise MySQL_ConditionTypeError("MySQL Condition Type Error, expection MySQL_Conditions got " + str(type(condition)))
return self
def describe(self):
cur_dbName = self.sql.current_dbName
data = self.sql.set_database(self.dbName).execute_query (
"DESCRIBE " + self.name + ";"
)
self.sql.set_database(cur_dbName)
return data
def get(self, *columns):
if len(columns) == 1 and isinstance(columns[0], MySQL_Columns):
self.columns = columns
else:
if not self.columns.add(*columns):
raise MySQL_InvalidColumnNameError("Invalid MySQL Column Name")
cur_dbName = self.sql.current_dbName
query = "SELECT " + self.columns.render() + " FROM " + self.name + self.condition + self.order + ";"
data = self.sql.set_database(self.dbName).execute_query( query , self.values)
self.sql.set_database(cur_dbName)
return data
def limit(self, n):
if isinstance(n, int) and n>=0:
self.limit = n
else:
raise MySQL_LimitTypeError("MySQL Limit Type Error, expected positive integer got " + str(type(n)))
return self
def filter_by(self, condition):
self.columns.clear()
if isinstance(condition, (MySQL_Conditions, MySQL_Condition)):
self.condition, self.values = condition.render()
self.condition = " WHERE " + self.condition
else:
raise MySQL_ConditionTypeError("MySQL Condition Type Error, expection MySQL_Conditions got " + str(type(condition)))
return self
def order_by(self, columns, order_type):
order_type = order_type.lower()
if order_type == 'asc' or order_type == 'desc' and len(columns) > 0:
cols = MySQL_Columns()
if cols.add(*columns):
self.order = " ORDER BY " + cols.render() + " " + order_type
else:
raise MySQL_InvalidColumnNameError("Invalid Column name was given in order_by")
return self
class MySQL:
def __init__(self, user, passwd, dbName=None, host='localhost', port=3306):
if dbName is None:
self.current_dbName = None
self.connection = MySQLdb.connect(
host = host, port = port, user = user, passwd = passwd
)
else:
self.current_dbName = dbName
self.connection = MySQLdb.connect(
host = host, port = port, db = dbName,
user = user, passwd = passwd
)
self.cursor = self.connection.cursor()
def execute_query(self, code, values=[]):
try:
if len(values) == 0:
self.cursor.execute(code);
else:
self.cursor.execute(code, values);
except MySQLdb.Error as e:
self.connection.rollback()
raise e
return self.cursor.fetchall()
def get_databases(self):
data = self.execute_query (
"""SELECT schema_name
FROM information_schema.schemata;"""
)
return [x[0] for x in data]
def get_tables(self):
if self.current_dbName is None:
raise MySQL_TableError("MySQL Database not specified before accessing tables")
else:
data = self.execute_query (
"""SELECT table_name
FROM information_schema.tables
WHERE table_schema = %s""", (self.current_dbName,))
return [x[0] for x in data]
def database_exists(self, dbName):
exists = self.execute_query (
"""SELECT EXISTS (
SELECT 1 FROM information_schema.schemata
WHERE schema_name = %s
);""", (dbName,)
)
return exists[0][0] == 1
def set_database(self, dbName):
global MySQL_Identifier_regex
if MySQL_Identifier_regex.match(dbName):
if self.current_dbName != dbName:
self.execute_query (
"use " + dbName + ";"
)
self.current_dbName = dbName
else:
raise MySQL_InvalidDatabaseNameError("Invalid Database Name Error: " + dbName)
return self
def table_exists(self, tableName):
exists = self.execute_query (
"""SELECT EXISTS(
SELECT 1 FROM information_schema.tables
WHERE table_schema = %s AND table_name = %s
);""", (self.current_dbName, tableName)
)
return exists[0][0] == 1
def table(self, tableName):
if self.current_dbName is None:
raise MySQL_TableError("MySQL Database not specified before accessing table")
elif MySQL_Identifier_regex.match(tableName) and self.table_exists(tableName):
return MySQL_Table(self, tableName, self.current_dbName)
else:
raise MySQL_InvalidTableNameError("MySQL Invalid Table Name: " + tableName)
def create_table(self, table_name, *args):
primary_keys, o, unique_keys = [], [], []
for arg in args:
if arg.is_pk: primary_keys.append(arg.name)
if arg.unique: unique_keys.append(arg.name)
if arg.nullable:
o.append(arg.name + " " + arg.t + " NULL")
else:
o.append(arg.name + " " + arg.t + " NOT NULL")
if len(primary_keys) > 0:
o.append("PRIMARY KEY (" + ",".join(primary_keys) + ")")
if len(unique_keys) > 0:
o.append("UNIQUE (" + ",".join(unique_keys) + ")")
query = "CREATE TABLE " + table_name + "(" + ",".join(o) + ");"
self.execute_query (
query
)
def delete_table(self, table_name):
self.execute_query ("drop table " + table_name )
def commit(self):
self.connection.commit()
def close(self):
self.cursor.close()
self.connection.close()