-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdata_sql.py
More file actions
229 lines (178 loc) · 6.7 KB
/
Copy pathdata_sql.py
File metadata and controls
229 lines (178 loc) · 6.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import json
import sqlite3
"""
For Manipulatng the databasedb
SQL stuff
My java professor is cool
"""
class dataSQL:
def __init__(self, dbfile):
"""
Initialize a DatabaseManager with the specified SQLite database file.
Parameters:
- dbfile (str): The path to the SQLite database file.
"""
self.dbfile = dbfile
self.connection = sqlite3.connect(self.dbfile)
self.cursor = self.connection.cursor()
self.create_tables()
def create_tables(self):
self.cursor.executescript('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT UNIQUE,
username TEXT,
email TEXT,
password TEXT
);
CREATE TABLE IF NOT EXISTS subdomains (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT,
subdomain TEXT,
FOREIGN KEY (token) REFERENCES users(token) ON DELETE CASCADE
);
''')
def connect(self) -> sqlite3.Connection:
"""
Establish a connection to the SQLite database.
Returns:
- sqlite3.Connection: A database connection object.
"""
return sqlite3.connect(self.dbfile)
def close(self):
"""
Commit any pending changes and close the database connection.
"""
self.connection.commit()
self.connection.close()
def use_database(self, query: str, values: tuple = None):
"""
Execute a database query and return the result.
Parameters:
- query (str): The SQL query to execute.
- values (tuple, optional): A tuple of parameter values to bind to the query.
Returns:
- result: The result of the query execution. If it's a SELECT query, it returns the first row as a tuple; otherwise, it returns None.
"""
self.connection = self.connect()
res = self.connection.execute(query, values)
returned_value = None
if "select" in query.lower():
returned_value = res.fetchone()
self.close()
return returned_value
def subdomains_from_token(self, session):
"""
Retrieve a list of subdomains owned by a user with a specific session token.
Parameters:
- session: User's session token.
Returns:
- domain_list: A list of subdomains owned by the user or an empty list if none are found.
"""
self.connection = self.connect()
self.cursor = self.connection.cursor()
query = f"SELECT subdomain FROM subdomains WHERE token = ?"
print(query)
self.cursor.execute(query, (session, ))
rows = self.cursor.fetchall()
domain_list = [row[0] for row in rows]
print(domain_list)
self.cursor.close()
self.connection.close()
return domain_list
def get_from_token(self, need, session):
"""
Retrieve a specific field (e.g., user information) from the 'users' table based on a session token.
Parameters:
- need (str): The field to retrieve (e.g., 'username', 'email').
- session: User's session token.
Returns:
- value: The value of the requested field or None if not found.
"""
out = self.use_database(
f"SELECT {need} from users where token = ?", (session,)
)
return out[0]
def new_subdomain(self, token, subdomain) -> bool: #inserts new_subdomain
"""
Insert a new subdomain for a user.
Parameters:
- token: User's session token.
- subdomain: The subdomain to be inserted.
Returns:
- success: True if the insertion is successful, False otherwise.
"""
if self.token_exists(token=token): #check if there is a user in the first place
self.connection = self.connect()
self.connection.execute("INSERT INTO subdomains (token, subdomain) VALUES (?, ?)", (token, subdomain))
self.close()
return True
else:
return False
def token_exists(self, token) -> bool:
"""
Check if a user with the specified session token exists in the 'users' table.
Parameters:
- token: User's session token to check.
Returns:
- exists: True if the user exists, False otherwise.
"""
self.connection = self.connect()
self.cursor = self.connection.cursor()
query = "SELECT COUNT(*) FROM users WHERE token = ?"
self.cursor.execute(query, (token, ))
count = self.cursor.fetchone()[0]
self.cursor.close()
self.close()
if count > 0:
return True
else:
return False
def delete(self, subdomain) -> bool:
"""
Delete a subdomain from the 'subdomains' table.
Parameters:
- subdomain: The subdomain to be deleted.
Returns:
- success: True if the deletion is successful, False otherwise.
"""
try:
self.connection = self.connect()
self.cursor = self.connection.cursor()
self.cursor.execute(f"DELETE FROM subdomains WHERE subdomain = '{subdomain}'")
self.cursor.close()
self.close()
return True
except:
return False
def owner_of_subdmain(self, subdomain) -> int:
"""
Retrieve the session token (owner) of a specific subdomain.
Parameters:
- subdomain: The subdomain to query ownership for.
Returns:
- owner_token: The session token (as an integer) of the owner of the subdomain.
"""
self.connection = self.connect()
self.cursor = self.connection.cursor()
self.cursor.execute(f'SELECT Token FROM subdomains WHERE subdomain = "{subdomain}";')
token = self.cursor.fetchone()[0]
self.cursor.close()
self.close()
return token
def admin_fetchall(self) -> list:
output = []
self.connection = self.connect()
self.cursor = self.connection.cursor()
self.cursor.execute('SELECT * FROM subdomains;')
subdomains = self.cursor.fetchall()
for subdomain in subdomains:
user = self.cursor.execute(f'SELECT username FROM users WHERE token = {subdomain[0]};').fetchone()[0]
output.append(SQLRelationship(owner=user, subdomain=subdomain[1]))
self.cursor.close()
self.close()
return output
class SQLRelationship:
def __init__(self, owner, subdomain):
self.owner = owner
self.subdomain = subdomain