diff --git a/app/repositories/achievements.py b/app/repositories/achievements.py index fd8119b8..e896e4e2 100644 --- a/app/repositories/achievements.py +++ b/app/repositories/achievements.py @@ -92,8 +92,8 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM achievements - WHERE id = COALESCE(:id, id) - OR name = COALESCE(:name, name) + WHERE COALESCE(id = :id, :id IS NULL) + OR COALESCE(name = :name, :name IS NULL) """ params: dict[str, Any] = { "id": id, @@ -173,7 +173,7 @@ async def update( query = f"""\ UPDATE achievements - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE id = :id """ values = {"id": id} | update_fields diff --git a/app/repositories/channels.py b/app/repositories/channels.py index 04d0962c..cc191fc1 100644 --- a/app/repositories/channels.py +++ b/app/repositories/channels.py @@ -91,8 +91,8 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM channels - WHERE id = COALESCE(:id, id) - AND name = COALESCE(:name, name) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(name = :name, :name IS NULL) """ params: dict[str, Any] = { "id": id, @@ -114,9 +114,9 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM channels - WHERE read_priv = COALESCE(:read_priv, read_priv) - AND write_priv = COALESCE(:write_priv, write_priv) - AND auto_join = COALESCE(:auto_join, auto_join) + WHERE COALESCE(read_priv = :read_priv, :read_priv IS NULL) + AND COALESCE(write_priv = :write_priv, :write_priv IS NULL) + AND COALESCE(auto_join = :auto_join, :auto_join IS NULL) """ params: dict[str, Any] = { "read_priv": read_priv, @@ -140,9 +140,9 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM channels - WHERE read_priv = COALESCE(:read_priv, read_priv) - AND write_priv = COALESCE(:write_priv, write_priv) - AND auto_join = COALESCE(:auto_join, auto_join) + WHERE COALESCE(read_priv = :read_priv, :read_priv IS NULL) + AND COALESCE(write_priv = :write_priv, :write_priv IS NULL) + AND COALESCE(auto_join = :auto_join, :auto_join IS NULL) """ params: dict[str, Any] = { "read_priv": read_priv, @@ -182,7 +182,7 @@ async def update( query = f"""\ UPDATE channels - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE name = :name """ values = {"name": name} | update_fields diff --git a/app/repositories/clans.py b/app/repositories/clans.py index fc11ef67..bafdc6ad 100644 --- a/app/repositories/clans.py +++ b/app/repositories/clans.py @@ -85,10 +85,10 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM clans - WHERE id = COALESCE(:id, id) - AND name = COALESCE(:name, name) - AND tag = COALESCE(:tag, tag) - AND owner = COALESCE(:owner, owner) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(name = :name, :name IS NULL) + AND COALESCE(tag = :tag, :tag IS NULL) + AND COALESCE(owner = :owner, :owner IS NULL) """ params: dict[str, Any] = {"id": id, "name": name, "tag": tag, "owner": owner} clan = await app.state.services.database.fetch_one(query, params) diff --git a/app/repositories/ingame_logins.py b/app/repositories/ingame_logins.py index 5c6ce1b0..19435d72 100644 --- a/app/repositories/ingame_logins.py +++ b/app/repositories/ingame_logins.py @@ -99,8 +99,8 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM ingame_logins - WHERE userid = COALESCE(:userid, userid) - AND ip = COALESCE(:ip, ip) + WHERE COALESCE(userid = :userid, :userid IS NULL) + AND COALESCE(ip = :ip, :ip IS NULL) """ params: dict[str, Any] = { "userid": user_id, @@ -123,10 +123,10 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM ingame_logins - WHERE userid = COALESCE(:userid, userid) - AND ip = COALESCE(:ip, ip) - AND osu_ver = COALESCE(:osu_ver, osu_ver) - AND osu_stream = COALESCE(:osu_stream, osu_stream) + WHERE COALESCE(userid = :userid, :userid IS NULL) + AND COALESCE(ip = :ip, :ip IS NULL) + AND COALESCE(osu_ver = :osu_ver, :osu_ver IS NULL) + AND COALESCE(osu_stream = :osu_stream, :osu_stream IS NULL) """ params: dict[str, Any] = { "userid": user_id, diff --git a/app/repositories/logs.py b/app/repositories/logs.py index 5b61672b..46c76aef 100644 --- a/app/repositories/logs.py +++ b/app/repositories/logs.py @@ -73,10 +73,10 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM logs - WHERE id = COALESCE(:id, id) - AND from = COALESCE(:from, from) - AND to = COALESCE(:to, to) - AND action = COALESCE(:action, action) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(from = :from, :from IS NULL) + AND COALESCE(to = :to, :to IS NULL) + AND COALESCE(action = :action, :action IS NULL) """ params = { "id": id, @@ -100,12 +100,12 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM logs - WHERE id = COALESCE(:id, id) - AND from = COALESCE(:from, from) - AND to = COALESCE(:to, to) - AND action = COALESCE(:action, action) - AND msg = COALESCE(:msg, msg) - AND time = COALESCE(:time, time) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(from = :from, :from IS NULL) + AND COALESCE(to = :to, :to IS NULL) + AND COALESCE(action = :action, :action IS NULL) + AND COALESCE(msg = :msg, :msg IS NULL) + AND COALESCE(time = :time, :time IS NULL) """ params = { "id": id, @@ -134,12 +134,12 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM logs - WHERE id = COALESCE(:id, id) - AND from = COALESCE(:from, from) - AND to = COALESCE(:to, to) - AND action = COALESCE(:action, action) - AND msg = COALESCE(:msg, msg) - AND time = COALESCE(:time, time) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(from = :from, :from IS NULL) + AND COALESCE(to = :to, :to IS NULL) + AND COALESCE(action = :action, :action IS NULL) + AND COALESCE(msg = :msg, :msg IS NULL) + AND COALESCE(time = :time, :time IS NULL) """ params = { "id": id, @@ -173,11 +173,11 @@ async def update( """Update a log entry in the database.""" query = """\ UPDATE logs - SET from = COALESCE(:from, from), - to = COALESCE(:to, to), - action = COALESCE(:action, action), - msg = COALESCE(:msg, msg), - time = COALESCE(:time, time) + SET COALESCE(from = :from, :from IS NULL), + COALESCE(to = :to, :to IS NULL), + COALESCE(action = :action, :action IS NULL), + COALESCE(msg = :msg, :msg IS NULL), + COALESCE(time = :time, :time IS NULL) WHERE id = :id """ params = { diff --git a/app/repositories/mails.py b/app/repositories/mails.py index 85908ead..45b00597 100644 --- a/app/repositories/mails.py +++ b/app/repositories/mails.py @@ -59,10 +59,10 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM mail - WHERE from_id = COALESCE(:from_id, from_id) - AND to_id = COALESCE(:to_id, to_id) - AND time = COALESCE(:time, time) - AND read = COALESCE(:read, read) + WHERE COALESCE(from_id = :from_id, :from_id IS NULL) + AND COALESCE(to_id = :to_id, :to_id IS NULL) + AND COALESCE(time = :time, :time IS NULL) + AND COALESCE(read = :read, :read IS NULL) """ params = { "from_id": from_id, @@ -84,10 +84,10 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM mail - WHERE from_id = COALESCE(:from_id, from_id) - AND to_id = COALESCE(:to_id, to_id) - AND time = COALESCE(:time, time) - AND `read` = COALESCE(:read, read) + WHERE COALESCE(from_id = :from_id, :from_id IS NULL) + AND COALESCE(to_id = :to_id, :to_id IS NULL) + AND COALESCE(time = :time, :time IS NULL) + AND COALESCE(`read` = :read, :read IS NULL) """ params = { "from_id": from_id, diff --git a/app/repositories/maps.py b/app/repositories/maps.py index 1d55000c..423f6d27 100644 --- a/app/repositories/maps.py +++ b/app/repositories/maps.py @@ -186,9 +186,9 @@ async def fetch_one( query = f"""\ SELECT {READ_PARAMS} FROM maps - WHERE id = COALESCE(:id, id) - AND md5 = COALESCE(:md5, md5) - AND filename = COALESCE(:filename, filename) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(md5 = :md5, :md5 IS NULL) + AND COALESCE(filename = :filename, :filename IS NULL) """ params: dict[str, Any] = { "id": id, @@ -214,14 +214,14 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM maps - WHERE server = COALESCE(:server, server) - AND set_id = COALESCE(:set_id, set_id) - AND status = COALESCE(:status, status) - AND artist = COALESCE(:artist, artist) - AND creator = COALESCE(:creator, creator) - AND filename = COALESCE(:filename, filename) - AND mode = COALESCE(:mode, mode) - AND frozen = COALESCE(:frozen, frozen) + WHERE COALESCE(server = :server, :server IS NULL) + AND COALESCE(set_id = :set_id, :set_id IS NULL) + AND COALESCE(status = :status, :status IS NULL) + AND COALESCE(artist = :artist, :artist IS NULL) + AND COALESCE(creator = :creator, :creator IS NULL) + AND COALESCE(filename = :filename, :filename IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) + AND COALESCE(frozen = :frozen, :frozen IS NULL) """ params: dict[str, Any] = { @@ -255,14 +255,14 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM maps - WHERE server = COALESCE(:server, server) - AND set_id = COALESCE(:set_id, set_id) - AND status = COALESCE(:status, status) - AND artist = COALESCE(:artist, artist) - AND creator = COALESCE(:creator, creator) - AND filename = COALESCE(:filename, filename) - AND mode = COALESCE(:mode, mode) - AND frozen = COALESCE(:frozen, frozen) + WHERE COALESCE(server = :server, :server IS NULL) + AND COALESCE(set_id = :set_id, :set_id IS NULL) + AND COALESCE(status = :status, :status IS NULL) + AND COALESCE(artist = :artist, :artist IS NULL) + AND COALESCE(creator = :creator, :creator IS NULL) + AND COALESCE(filename = :filename, :filename IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) + AND COALESCE(frozen = :frozen, :frozen IS NULL) """ params: dict[str, Any] = { "server": server, @@ -361,7 +361,7 @@ async def update( query = f"""\ UPDATE maps - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE id = :id """ values = {"id": id} | update_fields diff --git a/app/repositories/players.py b/app/repositories/players.py index cd4f1a68..80190b92 100644 --- a/app/repositories/players.py +++ b/app/repositories/players.py @@ -131,9 +131,9 @@ async def fetch_one( query = f"""\ SELECT {'*' if fetch_all_fields else READ_PARAMS} FROM users - WHERE id = COALESCE(:id, id) - AND safe_name = COALESCE(:safe_name, safe_name) - AND email = COALESCE(:email, email) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(safe_name = :safe_name, :safe_name IS NULL) + AND COALESCE(email = :email, :email IS NULL) """ params: dict[str, Any] = { "id": id, @@ -156,12 +156,12 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM users - WHERE priv = COALESCE(:priv, priv) - AND country = COALESCE(:country, country) - AND clan_id = COALESCE(:clan_id, clan_id) - AND clan_priv = COALESCE(:clan_priv, clan_priv) - AND preferred_mode = COALESCE(:preferred_mode, preferred_mode) - AND play_style = COALESCE(:play_style, play_style) + WHERE COALESCE(priv = :priv, :priv IS NULL) + AND COALESCE(country = :country, :country IS NULL) + AND COALESCE(clan_id = :clan_id, :clan_id IS NULL) + AND COALESCE(clan_priv = :clan_priv, :clan_priv IS NULL) + AND COALESCE(preferred_mode = :preferred_mode, :preferred_mode IS NULL) + AND COALESCE(play_style = :play_style, :play_style IS NULL) """ params: dict[str, Any] = { "priv": priv, @@ -190,12 +190,12 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM users - WHERE priv = COALESCE(:priv, priv) - AND country = COALESCE(:country, country) - AND clan_id = COALESCE(:clan_id, clan_id) - AND clan_priv = COALESCE(:clan_priv, clan_priv) - AND preferred_mode = COALESCE(:preferred_mode, preferred_mode) - AND play_style = COALESCE(:play_style, play_style) + WHERE COALESCE(priv = :priv, :priv IS NULL) + AND COALESCE(country = :country, :country IS NULL) + AND COALESCE(clan_id = :clan_id, :clan_id IS NULL) + AND COALESCE(clan_priv = :clan_priv, :clan_priv IS NULL) + AND COALESCE(preferred_mode = :preferred_mode, :preferred_mode IS NULL) + AND COALESCE(play_style = :play_style, :play_style IS NULL) """ params: dict[str, Any] = { "priv": priv, @@ -275,7 +275,7 @@ async def update( query = f"""\ UPDATE users - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE id = :id """ values = {"id": id} | update_fields diff --git a/app/repositories/scores.py b/app/repositories/scores.py index dc440c4a..799ea89a 100644 --- a/app/repositories/scores.py +++ b/app/repositories/scores.py @@ -186,11 +186,11 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM scores - WHERE map_md5 = COALESCE(:map_md5, map_md5) - AND mods = COALESCE(:mods, mods) - AND status = COALESCE(:status, status) - AND mode = COALESCE(:mode, mode) - AND userid = COALESCE(:userid, userid) + WHERE COALESCE(map_md5 = :map_md5, :map_md5 IS NULL) + AND COALESCE(mods = :mods, :mods IS NULL) + AND COALESCE(status = :status, :status IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) + AND COALESCE(userid = :userid, :userid IS NULL) """ params: dict[str, Any] = { "map_md5": map_md5, @@ -216,11 +216,11 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM scores - WHERE map_md5 = COALESCE(:map_md5, map_md5) - AND mods = COALESCE(:mods, mods) - AND status = COALESCE(:status, status) - AND mode = COALESCE(:mode, mode) - AND userid = COALESCE(:userid, userid) + WHERE COALESCE(map_md5 = :map_md5, :map_md5 IS NULL) + AND COALESCE(mods = :mods, :mods IS NULL) + AND COALESCE(status = :status, :status IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) + AND COALESCE(userid = :userid, :userid IS NULL) """ params: dict[str, Any] = { "map_md5": map_md5, @@ -255,7 +255,7 @@ async def update( query = f"""\ UPDATE scores - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE id = :id """ values = {"id": id} | update_fields diff --git a/app/repositories/stats.py b/app/repositories/stats.py index 6dc623ff..7912a465 100644 --- a/app/repositories/stats.py +++ b/app/repositories/stats.py @@ -161,8 +161,8 @@ async def fetch_count( query = """\ SELECT COUNT(*) AS count FROM stats - WHERE id = COALESCE(:id, id) - AND mode = COALESCE(:mode, mode) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) """ params: dict[str, Any] = { "id": player_id, @@ -182,8 +182,8 @@ async def fetch_many( query = f"""\ SELECT {READ_PARAMS} FROM stats - WHERE id = COALESCE(:id, id) - AND mode = COALESCE(:mode, mode) + WHERE COALESCE(id = :id, :id IS NULL) + AND COALESCE(mode = :mode, :mode IS NULL) """ params: dict[str, Any] = { "id": player_id, @@ -253,7 +253,7 @@ async def update( query = f"""\ UPDATE stats - SET {",".join(f"{k} = COALESCE(:{k}, {k})" for k in update_fields)} + SET {",".join(f"COALESCE({k} = :{k}, :{k} IS NULL)" for k in update_fields)} WHERE id = :id AND mode = :mode """