-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
52 lines (43 loc) · 1.12 KB
/
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
from config import databases
import MySQLdb
import MySQLdb.cursors
class DatabaseFactory:
connections = {}
@staticmethod
def instance(database = 'default'):
MySQLdb.paramstyle = 'qmark'
if database in DatabaseFactory.connections:
return DatabaseFactory.connections[database]
dbinfo = DatabaseFactory.get_properties(database)
if dbinfo is None:
return None
db = None
try:
db = MySQLdb.connect(
host = dbinfo['host'],
user = dbinfo['user'],
passwd = dbinfo['password'],
db = dbinfo['dbname'],
cursorclass = MySQLdb.cursors.DictCursor
)
db.autocommit(True)
DatabaseFactory.connections[database] = db
except:
pass
return db
@staticmethod
def close_connections(uno):
for key in DatabaseFactory.connections:
DatabaseFactory.connections[key].close()
DatabaseFactory.connections = {}
@staticmethod
def get_property(database, p):
dbinfo = DatabaseFactory.get_properties(database)
if dbinfo is not None and p in dbinfo:
return dbinfo[p]
return None
@staticmethod
def get_properties(database):
if database in databases:
return databases[database]
return None