This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL4.py
302 lines (258 loc) · 9.21 KB
/
MySQL4.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
"""
MySQL4 - Simple to use pymysql wrappers. An understanding of SQL statements is preferred.
4.0 DatabaseConnection class created.
RawQuery class created.
TableQuest class created.
"""
import pymysql
import traceback
__author__ = 'CplBDJ'
__version__ = '4.0a'
username = 'root'
password = ''
class DatabaseConnection:
"""
Allows a connection to the database server.
"""
def __init__(self, host='localhost', user=None, passwd=None, port=3306):
self.host = host
self.user = user or username
self.passwd = passwd or password
self.port = port
def new(self):
return pymysql.connect(host=self.host,
user=self.user,
passwd=self.passwd,
port=self.port)
def query(self, sql:str) -> dict:
"""
Does the actual sql query. Returns a dict with keys:
'error' None or the traceback
'sql' The sql passed.
'data' The data that has been requested.
'columns' The data's column information.
"""
connection = self.new()
cursor = connection.cursor()
error = None
data = list()
columns = list()
try:
cursor.execute(sql)
data = cursor.fetchall()
columns = cursor.description
except pymysql.err.MySQLError:
error = traceback.format_exc()
finally:
connection.close()
return dict(error=error,
sql=sql,
data=data,
columns=columns)
def submit(self, sql: str) -> dict:
"""
Does the actual sql submission.
Returns a dict with keys:
'error' None or the traceback
'sql' The sql passed.
"""
connection = self.new()
cursor = connection.cursor()
error = None
try:
cursor.execute(sql)
connection.commit()
error = False
except pymysql.err.MySQLError:
connection.rollback()
error = traceback.format_exc()
finally:
connection.close()
return dict(error=error,
sql=sql)
class RawQuery:
"""
A more raw response to the SQL server. This can be used separately but it's purpose is to be subclassed.
This shows the use of TableQuery, defined elsewhere.
Allows you to use "with" statement or just call it normally.
"where" is the SQL WHERE statement.
"like" is the SQL LIKE statement.
"sort" is the SQL SORT BY statement.
The "where" and "like" parameters are structured the same. They can be used interchangablely.
>>> from pprint import pprint # For readability
You can pass the username & password to the class.
>>> with TableQuery(user='user', passwd='password') as query:
... pprint(query('Apps', 'Users', select='User', where={'User': 'nick'}))
[{'User': 'nick'}]
>>> with TableQuery(user='user', passwd='password') as query:
... pprint(query('Apps', 'Users', select='User, Name', where='User like "%k%"'))
[{'Name': 'Nick', 'User': 'nick'}, {'Name': 'Blake', 'User': 'blake'}]
You can also set the module's username and password, that way you don't have to pass them.
>>> username = 'user'
>>> password = 'password'
>>> query = TableQuery()
>>> pprint(query('Apps', 'Users'))
[{'Initals': None,
'Name': None,
'UID': 0,
'User': 'root'},
{'Initals': 'NRJ',
'Name': 'Nick',
'UID': 1,
'User': 'nick'},
{'Initals': 'TJ',
'Name': 'Tony',
'UID': 2,
'User': 'tony'},
{'Initals': 'JB',
'Name': 'Jesse',
'UID': 3,
'User': 'jesse'},
{'Initals': 'MM',
'Name': 'Mighty Mouse',
'UID': 4,
'User': 'mightymouse'},
{'Initals': 'BO',
'Name': 'Blake',
'UID': 5,
'User': 'blake'}]
>>> pprint(query('Apps', 'Users', like=[('User', '%m%'), ('User', '%n%')]))
[{'Initals': None,
'Name': None,
'UID': 0,
'User': 'root'},
{'Initals': 'NRJ',
'Name': 'Nick',
'UID': 1,
'User': 'nick'},
{'Initals': 'TJ',
'Name': 'Tony',
'UID': 2,
'User': 'tony'},
{'Initals': 'JB',
'Name': 'Jesse',
'UID': 3,
'User': 'jesse'},
{'Initals': 'MM',
'Name': 'Mighty Mouse',
'UID': 4,
'User': 'mightymouse'},
{'Initals': 'BO',
'Name': 'Blake',
'UID': 5,
'User': 'blake'}]
>>> pprint(query('Apps', 'Users', where='User="tony" or User="root"', sort='Name'))
[{'Initals': None,
'Name': None,
'UID': 0,
'User': 'root'},
{'Initals': 'TJ',
'Name': 'Tony',
'UID': 2,
'User': 'tony'}]
>>> pprint(query('Apps', 'Users', sql='WHERE User="tony" or User="root" ORDER BY `Name`'))
[{'Initals': None,
'Name': None,
'UID': 0,
'User': 'root'},
{'Initals': 'TJ',
'Name': 'Tony',
'UID': 2,
'User': 'tony'}]
# Errors can be accessed using the 'errors' method. It will only show the last query's error.
>>> pprint(query('Show', 'error'))
[]
>>> pprint(query.error)
('Traceback (most recent call last):\n'
' File "/home/nick/Scripts/python3/MySQL4/MySQL4.py", line 52, in query\n'
' cursor.execute(sql)\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/cursors.py", '
'line 170, in execute\n'
' result = self._query(query)\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/cursors.py", '
'line 328, in _query\n'
' conn.query(q)\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/connections.py", '
'line 517, in query\n'
' self._affected_rows = self._read_query_result(unbuffered=unbuffered)\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/connections.py", '
'line 732, in _read_query_result\n'
' result.read()\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/connections.py", '
'line 1075, in read\n'
' first_packet = self.connection._read_packet()\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/connections.py", '
'line 684, in _read_packet\n'
' packet.check_error()\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/protocol.py", '
'line 220, in check_error\n'
' err.raise_mysql_exception(self._data)\n'
' File '
'"/home/nick/Scripts/python3/MySQL4/lib/python3.8/site-packages/pymysql/err.py", '
'line 109, in raise_mysql_exception\n'
' raise errorclass(errno, errval)\n'
'pymysql.err.ProgrammingError: (1146, "Table \'Show.error\' doesn\'t '
'exist")\n',
'SELECT * FROM `Show`.`error` None')
"""
def __init__(self, host='localhost', user=None, passwd=None, port=3306):
self._connection = DatabaseConnection(host, user, passwd, port)
self.error = None, None
def __call__(self, database: str, table: str, select:str = '*',
where: str or dict or list = None, # where x = "y"
like: str or dict or list = None, # where x like "y"
sort: str = None,
sql: str = None):
self.error = None, None
_sql = list()
if isinstance(where, dict):
spam = [f'`{key}`="{where[key]}"' for key in where]
_sql.append(f'WHERE {" and ".join(spam)}')
elif isinstance(where, list):
spam = [f'`{item[0]}`="{item[1]}"' for item in where]
elif where and isinstance(where, str):
_sql.append(f'WHERE {where}')
if where and like:
_sql.append(' and ')
if isinstance(like, dict):
spam = [f'`{key}`="{like[key]}"' for key in like]
_sql.append(f'WHERE {" and ".join(spam)}')
elif isinstance(like, list):
spam = [f'`{item[0]}`="{item[1]}"' for item in like]
elif where and isinstance(like, str):
_sql.append(f'WHERE {like}')
if sort:
_sql.append(f'ORDER BY `{sort}`')
if _sql:
sql = ' '.join(_sql)
return self._parse(self._connection.query(f'SELECT {select} FROM `{database}`.`{table}` {sql}'))
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return None
def _parse(self, response):
"""
Subclasses should override this and return the data as requested.
"""
return response
class TableQuery(RawQuery):
"""
Subclasses RawQuery, check RawQuery for usage examples. Returns the table as a dict.
"""
def _parse(self, response):
"""
Overrides the parent classes method.
Returns the table data as a dict.
"""
if response['error']:
self.error = response['error'], response['sql']
return tuple()
keys = tuple(key[0] for key in response['columns'])
return [dict(zip(keys, line)) for line in response['data']]