MAJOR: Reworked database to be easier to use and close automatically to prevent resource leaks

This commit is contained in:
JustIceO7
2025-01-29 17:47:29 +00:00
parent 83b458ed99
commit dfdbe4a7d4
9 changed files with 181 additions and 209 deletions

View File

@@ -7,66 +7,60 @@ def streamer_live_status(user_id: int) -> bool:
"""
Returns boolean on whether the given streamer is live
"""
db = Database()
db.create_connection()
is_live = db.fetchone("""SELECT isLive FROM streams WHERE user_id = ?""", (user_id,))
db.close_connection()
with Database() as db:
is_live = db.fetchone("""
SELECT isLive
FROM streams
WHERE user_id = ?
""", (user_id,))
return is_live
def followed_live_streams(user_id: int) -> list[dict]:
"""
Searches for streamers who the user followed which are currently live
"""
db = Database()
db.create_connection()
live_streams = db.fetchall("""
SELECT user_id, stream_id, title, num_viewers
FROM streams
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?)
AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = streams.user_id)
AND isLive = 1;
with Database() as db:
live_streams = db.fetchall("""
SELECT user_id, stream_id, title, num_viewers
FROM streams
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?)
AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = streams.user_id)
AND isLive = 1;
""", (user_id,))
db.close_connection()
return live_streams
def followed_streamers(user_id: int) -> list[dict]:
"""
Returns a list of streamers who the user follows
"""
db = Database()
db.create_connection()
followed_streamers = db.fetchall("""
SELECT user_id, username
FROM users
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
with Database() as db:
followed_streamers = db.fetchall("""
SELECT user_id, username
FROM users
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
""", (user_id,))
db.close_connection()
return followed_streamers
def streamer_most_recent_stream(user_id: int) -> dict:
"""
Returns data of the most recent stream by a streamer
"""
db = Database()
db.create_connection()
most_recent_stream = db.fetchone("""SELECT * FROM streams WHERE
user_id = ? AND
stream_id = (SELECT MAX(stream_id) FROM
streams WHERE user_id = ?)""", (user_id, user_id))
db.close_connection()
with Database() as db:
most_recent_stream = db.fetchone("""
SELECT * FROM streams
WHERE user_id = ?
AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = ?)
""", (user_id, user_id))
return most_recent_stream
def user_stream(user_id: int, stream_id: int) -> dict:
"""
Returns data of a streamers selected stream
"""
db = Database()
db.create_connection()
stream = db.fetchone("SELECT * FROM streams WHERE user_id = ? AND stream_id = ?", (user_id, stream_id))
db.close_connection()
with Database() as db:
stream = db.fetchone("""
SELECT * FROM streams
WHERE user_id = ?
AND stream_id = ?
""", (user_id, stream_id))
return stream