This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
175 lines (154 loc) · 6.64 KB
/
database.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
import psycopg2
import config
"""
database.py: Access and edit database
"""
STATEMENT = """
SELECT discord_id, users.name AS user, builders.user AS userid, area, city, county, state FROM (
SELECT user_id AS user, Null AS area, Null AS city, counties.name AS county,
counties.state AS state FROM county_builders
JOIN counties ON county_builders.county_id = counties.id
UNION
SELECT user_id AS user, Null AS area, cities.name AS city, counties.name AS county,
counties.state AS state FROM city_builders
LEFT JOIN cities ON city_builders.city_id = cities.id
JOIN counties ON cities.county_id = counties.id
UNION
SELECT user_id AS user, locations.name AS area, cities.name AS city,
counties.name AS county, counties.state AS state FROM location_builders
LEFT JOIN locations ON location_builders.location_id = locations.id
LEFT JOIN cities ON locations.city_id = cities.id
LEFT JOIN counties ON
CAST(CONCAT(locations.county_id, cities.county_id) AS INT) = counties.id)
AS builders
JOIN users on builders.user = users.id
"""
connection = None
def get_database():
global connection
if connection is None:
connection = BotDB(config.postgres, config.postgres_user, config.postgres_pass)
return connection
class BotDB:
def __init__(self, db, user, passwd):
# Connect to postgresql database
self.conn = psycopg2.connect(f"dbname={db} user={user} password={passwd}")
self.cur = self.conn.cursor()
print(f"[INFO]: Database: Connected to {db} AS {user} successfully")
def execute(self, query, args):
"""
Execute custom sql query to insert into database
:param query: SQL command to be run
:param args: Args to be inserted
:return: None
"""
self.cur.execute(query, args)
# print("[DEBUG]: " + query + ", " + str(args))
self.conn.commit()
def get_query(self, query, args=None):
"""
Execute custom sql query to get from database
:param query: SQL command to be run, string type
:param args: Args to be inserted, iterable type
:return: SQL results
"""
self.cur.execute(query, args)
# print("[DEBUG]: " + query + ", " + str(args))
return self.cur.fetchall()
def get_all(self):
"""
Get all builder info in database
"""
self.cur.execute(STATEMENT)
return self.cur.fetchall()
def get_builders(self, scope, query):
"""
Get all builders in a certain area
"""
self.cur.execute(STATEMENT + f" WHERE {scope} = %s", [query])
return self.cur.fetchall()
def get_builders_by_id(self, scope, id):
"""
Get all builders by ID of location/city/county/state
:param scope: location, city, county, or state
:param id: id of search
:return: Results
"""
self.cur.execute(f"""
SELECT discord_id, users.name AS user, builders.user AS userid, area, city, county, state FROM (
SELECT user_id AS user, Null AS area, Null AS city, counties.id AS county,
counties.state AS state FROM county_builders
JOIN counties ON county_builders.county_id = counties.id
UNION
SELECT user_id AS user, CAST(Null AS INT) AS area, cities.id AS city, counties.id AS county,
counties.state AS state FROM city_builders
LEFT JOIN cities ON city_builders.city_id = cities.id
JOIN counties ON cities.county_id = counties.id
UNION
SELECT user_id AS user, locations.id AS area, cities.id AS city,
counties.id AS county, counties.state AS state FROM location_builders
LEFT JOIN locations ON location_builders.location_id = locations.id
LEFT JOIN cities ON locations.city_id = cities.id
LEFT JOIN counties ON
CAST(CONCAT(locations.county_id, cities.county_id) AS INT) = counties.id)
AS builders
JOIN users on builders.user = users.id
WHERE {scope} = %s
""", [id])
return self.cur.fetchall()
def search_name(self, table, query):
"""
Get the true name of an area based on a query
:param table: Table to search in
:param query: Area to search for
:return: Areas found LIKE query
"""
self.cur.execute(f"SELECT name FROM {table} WHERE lower(name) LIKE lower(%s)",
["%%%s%%" % query])
return self.cur.fetchall()
def area_info(self, id):
"""
Get the city, county, state of a location
:param id: Id of area
:return: City, county, state of location
"""
self.cur.execute(f"""SELECT locations.name, cities.name, counties.name, counties.state FROM locations
LEFT JOIN cities ON locations.city_id = cities.id
LEFT JOIN counties ON CAST(CONCAT(locations.county_id, cities.county_id) AS INT) =
counties.id WHERE locations.id = %s""", [id])
return self.cur.fetchone()
def city_info(self, id):
"""
Get the county, state of a city
:param id: Id of city
:return: County, state of city
"""
self.cur.execute(f"""SELECT cities.name, counties.name, counties.state FROM cities
LEFT JOIN counties ON cities.county_id = counties.id
WHERE cities.id = %s""", [id])
return self.cur.fetchone()
def county_info(self, id):
"""
Get the state of a county
:param id: Id of county
:return: State of county
"""
self.cur.execute("SELECT name, state FROM counties WHERE id = %s", [id])
return self.cur.fetchone()
def get_user(self, user):
"""
Get all info on a user
:param user: discord user id
:return: database entry
"""
self.cur.execute(STATEMENT + " WHERE discord_id = %s;", [user])
return self.cur.fetchall()
def add_user_id(self, username, discord_id):
"""
Add a discord id to a user in the database
:param discord_id: user's discord id
:param username: username of user to add
:return: Nones
"""
self.cur.execute("UPDATE users SET discord_id = %s WHERE name = %s", [discord_id, username])
self.conn.commit()