-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase_connector.py
82 lines (67 loc) · 2.45 KB
/
database_connector.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
import mysql.connector
from session_components import User
# Connect to database
database = mysql.connector.connect(
host="srv872.hstgr.io",
port="3306",
user="u425992461_WalkEaseRoot",
passwd="",
database="u425992461_WalkEase",
connection_timeout=3600
)
# Get cursor for database
databaseCursor = database.cursor(buffered=True)
# Update max execution time to 10 seconds
databaseCursor.execute("SET SESSION MAX_STATEMENT_TIME=10000")
### Functions for common database queries
## Returns a User object if an account is found with the given username
#param username: the username to check the database for
#returns a User object coorelating to the given username, or None if the username wasn't found in the database
def getUserFromUsername(username: str) -> User:
reconnect()
# Verify that account exists
databaseCursor.execute('SELECT userID FROM Users WHERE username = %s;', (username,))
userID = databaseCursor.fetchall()
# Account found
if userID:
return User(str(userID[0][0]))
# Account not found
else:
return None
## Adds a new user to the database
#param username: the requested username
#returns -1 if the username is taken, and the new User's ID (as a string) if not
def addNewUser(username: str):
global databaseCursor
reconnect()
# Verify that username is not taken
databaseCursor.execute("SELECT username FROM Users WHERE username = %s;", (username,))
duplicateName = databaseCursor.fetchall()
if duplicateName:
return -1
# Enter user into database
else:
reconnect()
databaseCursor.execute("INSERT INTO Users (username) VALUES (%s);", (username,))
database.commit()
return str(databaseCursor.lastrowid)
## Runs a no-op to test connection, and then reconnects if necessary
def reconnect():
global database
global databaseCursor
try:
databaseCursor.execute("SELECT 1;")
except:
print("Reconnecting...")
database = mysql.connector.connect(
host="srv872.hstgr.io",
port="3306",
user="u425992461_WalkEaseRoot",
passwd="",
database="u425992461_WalkEase",
connection_timeout=3600
)
# Get cursor for database
databaseCursor = database.cursor(buffered=True)
# Update max execution time to 10 seconds
databaseCursor.execute("SET SESSION MAX_STATEMENT_TIME=10000")