This repository has been archived by the owner on Jul 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_mysql.py
executable file
·71 lines (62 loc) · 1.79 KB
/
db_mysql.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
import pymysql
class Database:
# Constructor
def __init__(self, host, username, password, db):
self.__host = host
self.__username = username
self.__password = password
self.__db = db
self.__conn = None
'''
# Deconstructor
def __del__(self):
self.__cur.close()
self.__conn.close()
'''
def __connect(self):
try:
self.__conn = pymysql.connect(\
host=self.__host, user=self.__username, passwd=self.__password, db=self.__db)
self.__cur = self.__conn.cursor()
except:
print("Something went wrong connecting to mysql")
def getUserList(self):
self.__connect()
self.__cur.execute("SELECT * FROM USERS")
return self.__cur.__dict__['_rows']
def getGroupList(self):
self.__connect()
self.__cur.execute("SELECT * FROM GROUPS")
return self.__cur.__dict__['_rows']
def getPasswordList(self):
self.__connect()
self.__cur.execute("SELECT * FROM PASSWORDS")
return self.__cur.__dict__['_rows']
def getUser(self, id):
self.__connect()
self.__cur.execute("SELECT UID, ACTIVE FROM USERS WHERE UID=%s", id)
user = self.__cur.fetchone()
def getUserPasswords(self, id):
self.__connect()
self.__cur.execute("\
SELECT P.PID, TITLE, DESCRIPTION, USERNAME, PASSWORD \
FROM PASSWORDS P \
INNER JOIN PASSWORD_GROUPS USING(PID) \
INNER JOIN USER_GROUPS UG USING(GID) \
WHERE P.ACTIVE = 1 \
AND UID = %s \
UNION ALL \
SELECT P.PID, TITLE, DESCRIPTION, USERNAME, PASSWORD \
FROM PASSWORDS P \
INNER JOIN USER_PASSWORDS USING(PID) \
WHERE P.ACTIVE = 1 \
AND UID = %s", (id, id))
return self.__cur.fetchall()
def getUserGroups(self, id):
self.__connect()
self.__cur.execute("\
SELECT GID, NAME, DESCRIPTION \
FROM GROUPS G \
NATURAL JOIN USER_GROUPS UG \
WHERE G.ACTIVE = 1 AND UG.UID = %s", id)
return self.__cur.fetchall()