Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite coalesce #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/repositories/achievements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions app/repositories/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/repositories/clans.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions app/repositories/ingame_logins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
42 changes: 21 additions & 21 deletions app/repositories/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down
16 changes: 8 additions & 8 deletions app/repositories/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
40 changes: 20 additions & 20 deletions app/repositories/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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] = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions app/repositories/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions app/repositories/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading