-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_factory.py
100 lines (79 loc) · 2.87 KB
/
sql_factory.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
import pymysql
from pymysql.err import OperationalError
class SQLFactory:
instance = None
BACKEND = "MySQL"
HOST = "localhost"
PORT = 3306
def __init__(self):
self.db_root = pymysql.connect(host=SQLFactory.HOST, user='root', passwd='example', port=SQLFactory.PORT)
self.db_root.autocommit(True)
self.db_user = None
self.errno = 0
self.errmsg = None
def closeAll(self):
if self.db_root and not self.db_root._closed:
self.db_root.cursor().close()
self.db_root.close()
if self.db_user and not self.db_user._closed:
self.db_user.cursor().close()
self.db_user.close()
def __del__(self):
self.closeAll()
def get_root_cursor(self):
return self.db_root.cursor()
def get_user_cursor(self):
assert self.db_user is not None
return self.db_user.cursor()
def user_login(self, user, passwd, autocommit=True):
try:
assert user and passwd and user != 'root'
self.db_user = pymysql.connect(
host=SQLFactory.HOST,
user=user,
passwd=passwd,
port=SQLFactory.PORT,
database=user
)
self.errno = 0
self.db_user.autocommit(autocommit)
return True
except OperationalError as e:
self.errno, self.errmsg = e.args
return False
except AssertionError:
self.errno = -1
self.errmsg = 'Access denied'
return False
if __name__ == "__main__":
factory = SQLFactory()
print(factory.user_login(None, None))
print(factory.errno)
print(factory.errmsg)
cursor = factory.get_root_cursor()
result = cursor.execute("SELECT VERSION()")
print(result)
for row in cursor.all_results():
print(row)
print(factory.user_login("student1", "123456"))
cursor = factory.get_user_cursor()
cursor.execute("select * from pub.sc")
for row in cursor.all_results():
print(row)
def __create_user(username, password):
username = username.replace(r"[^a-zA-Z0-9]", "")
db = pymysql.connect(host='localhost', user='root', passwd='example', port=3306)
cursor = db.cursor()
query = cursor.mogrify("drop user if exists %s", (username,))
cursor.execute(query)
query = cursor.mogrify("create user %s@'%%' identified by %s", (username, password,))
cursor.execute(query)
query = cursor.mogrify("drop database if exists %s" % (username,))
cursor.execute(query)
# 我只能说转义字符并不是多么智能
query = cursor.mogrify("create database if not exists `" + username + "`")
cursor.execute(query)
query = cursor.mogrify("grant all on `" + username + "`.* to %s with grant option", (username,))
cursor.execute(query)
db.close()
return 'Success'