From 456e1627258c08b420013615ac3d385f24b88c6e Mon Sep 17 00:00:00 2001 From: arily Date: Mon, 4 Dec 2023 22:07:21 +0900 Subject: [PATCH] hotfix --- app/repositories/maps.py | 24 ++++++++++++++++-------- app/repositories/scores.py | 37 ++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/app/repositories/maps.py b/app/repositories/maps.py index 1d55000c..6e979fbd 100644 --- a/app/repositories/maps.py +++ b/app/repositories/maps.py @@ -183,19 +183,27 @@ async def fetch_one( if id is None and md5 is None and filename is None: raise ValueError("Must provide at least one parameter.") - query = f"""\ - SELECT {READ_PARAMS} - FROM maps - WHERE id = COALESCE(:id, id) - AND md5 = COALESCE(:md5, md5) - AND filename = COALESCE(:filename, filename) - """ + queries = [ + "SELECT {READ_PARAMS} FROM maps WHERE 1 = 1", + ] + + if id is not None: + queries.append("AND id = :id") + + if md5 is not None: + queries.append("AND md5 = :md5") + + if filename is not None: + queries.append("AND filename = :filename") + + queries.append(";") + params: dict[str, Any] = { "id": id, "md5": md5, "filename": filename, } - map = await app.state.services.database.fetch_one(query, params) + map = await app.state.services.database.fetch_one(" ".join(queries), params) return cast(Map, dict(map._mapping)) if map is not None else None diff --git a/app/repositories/scores.py b/app/repositories/scores.py index dc440c4a..eb5ad9bc 100644 --- a/app/repositories/scores.py +++ b/app/repositories/scores.py @@ -213,31 +213,34 @@ async def fetch_many( page: int | None = None, page_size: int | None = None, ) -> list[Score]: - 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) - """ + queries = [ + "SELECT {READ_PARAMS} FROM scores WHERE 1 = 1", + "AND map_md5 = :map_md5" if map_md5 is not None else None, + "AND mods = :mods" if mods is not None else None, + "AND status = :status" if status is not None else None, + "AND mode = :mode" if mode is not None else None, + "AND userid = :userid" if user_id is not None else None, + """\ + LIMIT :page_size + OFFSET :offset + """ + if page is not None and page_size is not None + else None, + ] + params: dict[str, Any] = { "map_md5": map_md5, "mods": mods, "status": status, "mode": mode, "userid": user_id, + "page_size": page_size, + "offset": (page - 1) * page_size, } - if page is not None and page_size is not None: - query += """\ - LIMIT :page_size - OFFSET :offset - """ - params["page_size"] = page_size - params["offset"] = (page - 1) * page_size - recs = await app.state.services.database.fetch_all(query, params) + recs = await app.state.services.database.fetch_all( + [q for q in queries if q is not None], params + ) return cast(list[Score], [dict(r._mapping) for r in recs])