diff --git a/bemani/data/mysql/base.py b/bemani/data/mysql/base.py index 756e7f0..dd74bc3 100644 --- a/bemani/data/mysql/base.py +++ b/bemani/data/mysql/base.py @@ -172,10 +172,10 @@ class BaseData: expiration = Time.now() + expiration # Use that session - sql = ( - "INSERT INTO session (id, session, type, expiration) " - + "VALUES (:id, :session, :optype, :expiration)" - ) + sql = """ + INSERT INTO session (id, session, type, expiration) + VALUES (:id, :session, :optype, :expiration) + """ cursor = self.execute( sql, { diff --git a/bemani/data/mysql/game.py b/bemani/data/mysql/game.py index 2e6b76e..78eef8e 100644 --- a/bemani/data/mysql/game.py +++ b/bemani/data/mysql/game.py @@ -115,11 +115,11 @@ class GameData(BaseData): settings - A dictionary of settings that a game wishes to retrieve later. """ # Add settings json to game settings - sql = ( - "INSERT INTO game_settings (game, userid, data) " - + "VALUES (:game, :userid, :data) " - + "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO game_settings (game, userid, data) + VALUES (:game, :userid, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, {"game": game.value, "userid": userid, "data": self.serialize(settings)}, @@ -147,10 +147,10 @@ class GameData(BaseData): Returns: A dictionary as stored by a game class previously, or None if not found. """ - sql = ( - "SELECT data FROM series_achievement " - "WHERE game = :game AND userid = :userid AND id = :id AND type = :type" - ) + sql = """ + SELECT data FROM series_achievement + WHERE game = :game AND userid = :userid AND id = :id AND type = :type + """ cursor = self.execute( sql, { @@ -212,11 +212,11 @@ class GameData(BaseData): data - A dictionary of data that the game wishes to retrieve later. """ # Add achievement JSON to achievements - sql = ( - "INSERT INTO series_achievement (game, userid, id, type, data) " - + "VALUES (:game, :userid, :id, :type, :data) " - + "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO series_achievement (game, userid, id, type, data) + VALUES (:game, :userid, :id, :type, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, { @@ -244,10 +244,15 @@ class GameData(BaseData): If settings were found, they are guaranteed to include the attributes 'start_time' and 'end_time' which will both be seconds since the unix epoch (UTC). """ - sql = ( - "SELECT data, start_time, end_time FROM time_sensitive_settings WHERE " - "game = :game AND version = :version AND name = :name AND start_time <= :time AND end_time > :time" - ) + sql = """ + SELECT data, start_time, end_time FROM time_sensitive_settings + WHERE + game = :game AND + version = :version AND + name = :name AND + start_time <= :time AND + end_time > :time + """ cursor = self.execute( sql, {"game": game.value, "version": version, "name": name, "time": Time.now()}, @@ -278,10 +283,10 @@ class GameData(BaseData): If settings were found, they are guaranteed to include the attributes 'start_time' and 'end_time' which will both be seconds since the unix epoch (UTC). """ - sql = ( - "SELECT data, start_time, end_time FROM time_sensitive_settings WHERE " - "game = :game AND version = :version AND name = :name" - ) + sql = """ + SELECT data, start_time, end_time FROM time_sensitive_settings + WHERE game = :game AND version = :version AND name = :name + """ cursor = self.execute( sql, {"game": game.value, "version": version, "name": name} ) @@ -327,12 +332,12 @@ class GameData(BaseData): # Verify that this isn't overlapping some event. sql = """ - SELECT start_time, end_time FROM time_sensitive_settings WHERE - game = :game AND version = :version AND name = :name AND + SELECT start_time, end_time FROM time_sensitive_settings + WHERE game = :game AND version = :version AND name = :name AND ( - (start_time >= :start_time AND start_time < :end_time) OR - (end_time > :start_time AND end_time <= :end_time) OR - (start_time < :start_time AND end_time > :end_time) + (start_time >= :start_time AND start_time < :end_time) OR + (end_time > :start_time AND end_time <= :end_time) OR + (start_time < :start_time AND end_time > :end_time) ) """ cursor = self.execute( @@ -354,11 +359,11 @@ class GameData(BaseData): ) # Insert or update this setting - sql = ( - "INSERT INTO time_sensitive_settings (game, version, name, start_time, end_time, data) " - "VALUES (:game, :version, :name, :start_time, :end_time, :data) " - "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO time_sensitive_settings (game, version, name, start_time, end_time, data) + VALUES (:game, :version, :name, :start_time, :end_time, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, { @@ -389,10 +394,10 @@ class GameData(BaseData): Returns: A dictionary as stored by a game class previously, or None if not found. """ - sql = ( - "SELECT data FROM catalog " - "WHERE game = :game AND version = :version AND id = :id AND type = :type" - ) + sql = """ + SELECT data FROM catalog + WHERE game = :game AND version = :version AND id = :id AND type = :type + """ cursor = self.execute( sql, {"game": game.value, "version": version, "id": catid, "type": cattype} ) diff --git a/bemani/data/mysql/lobby.py b/bemani/data/mysql/lobby.py index 6b49a21..7db6eef 100644 --- a/bemani/data/mysql/lobby.py +++ b/bemani/data/mysql/lobby.py @@ -62,11 +62,14 @@ class LobbyData(BaseData): always contain an 'id' field which is the play session ID, and a 'time' field which represents the timestamp when the play session began. """ - sql = ( - "SELECT id, time, data FROM playsession " - "WHERE game = :game AND version = :version AND userid = :userid " - "AND time > :time" - ) + sql = """ + SELECT id, time, data FROM playsession + WHERE + game = :game AND + version = :version AND + userid = :userid AND + time > :time + """ cursor = self.execute( sql, { @@ -101,11 +104,10 @@ class LobbyData(BaseData): A list of Tuples, consisting of a UserID and the dictionary that would be returned for that user if get_play_session_info() was called for that user. """ - sql = ( - "SELECT id, time, userid, data FROM playsession " - "WHERE game = :game AND version = :version " - "AND time > :time" - ) + sql = """ + SELECT id, time, userid, data FROM playsession + WHERE game = :game AND version = :version AND time > :time + """ cursor = self.execute( sql, { @@ -142,11 +144,11 @@ class LobbyData(BaseData): del data["time"] # Add json to player session - sql = ( - "INSERT INTO playsession (game, version, userid, time, data) " - + "VALUES (:game, :version, :userid, :time, :data) " - + "ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)" - ) + sql = """ + INSERT INTO playsession (game, version, userid, time, data) + VALUES (:game, :version, :userid, :time, :data) + ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data) + """ self.execute( sql, { @@ -200,11 +202,14 @@ class LobbyData(BaseData): always contain an 'id' field which is the lobby ID, and a 'time' field representing the timestamp the lobby was created. """ - sql = ( - "SELECT id, time, data FROM lobby " - "WHERE game = :game AND version = :version AND userid = :userid " - "AND time > :time" - ) + sql = """ + SELECT id, time, data FROM lobby + WHERE + game = :game AND + version = :version AND + userid = :userid AND + time > :time + """ cursor = self.execute( sql, { @@ -238,10 +243,10 @@ class LobbyData(BaseData): Returns: A list of dictionaries representing lobby info stored by a game class. """ - sql = ( - "SELECT userid, id, data FROM lobby " - "WHERE game = :game AND version = :version AND time > :time" - ) + sql = """ + SELECT userid, id, data FROM lobby + WHERE game = :game AND version = :version AND time > :time + """ cursor = self.execute( sql, { @@ -278,11 +283,11 @@ class LobbyData(BaseData): del data["time"] # Add json to lobby - sql = ( - "INSERT INTO lobby (game, version, userid, time, data) " - + "VALUES (:game, :version, :userid, :time, :data) " - + "ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)" - ) + sql = """ + INSERT INTO lobby (game, version, userid, time, data) + VALUES (:game, :version, :userid, :time, :data) + ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data) + """ self.execute( sql, { diff --git a/bemani/data/mysql/machine.py b/bemani/data/mysql/machine.py index 88300df..0ec2d46 100644 --- a/bemani/data/mysql/machine.py +++ b/bemani/data/mysql/machine.py @@ -166,7 +166,10 @@ class MachineData(BaseData): Returns: A Machine object representing a machine, or None if not found. """ - sql = "SELECT name, description, arcadeid, id, port, game, version, data FROM machine WHERE pcbid = :pcbid" + sql = """ + SELECT name, description, arcadeid, id, port, game, version, data + FROM machine WHERE pcbid = :pcbid + """ cursor = self.execute(sql, {"pcbid": pcbid}) if cursor.rowcount != 1: # Machine doesn't exist @@ -222,10 +225,18 @@ class MachineData(BaseData): machine - A Machine object representing a machine. """ # Update machine name based on game - sql = ( - "UPDATE `machine` SET name = :name, description = :description, arcadeid = :arcadeid, " - + "port = :port, game = :game, version = :version, data = :data WHERE pcbid = :pcbid LIMIT 1" - ) + sql = """ + UPDATE `machine` + SET + name = :name, + description = :description, + arcadeid = :arcadeid, + port = :port, + game = :game, + version = :version, + data = :data + WHERE pcbid = :pcbid LIMIT 1 + """ self.execute( sql, { @@ -280,10 +291,10 @@ class MachineData(BaseData): # Add new machine try: - sql = ( - "INSERT INTO `machine` (pcbid, name, description, port, arcadeid) " - + "VALUES (:pcbid, :name, :description, :port, :arcadeid)" - ) + sql = """ + INSERT INTO `machine` (pcbid, name, description, port, arcadeid) + VALUES (:pcbid, :name, :description, :port, :arcadeid) + """ self.execute( sql, { @@ -327,10 +338,10 @@ class MachineData(BaseData): Returns: An Arcade object representing this arcade """ - sql = ( - "INSERT INTO arcade (name, description, pref, area, data, pin) " - + "VALUES (:name, :desc, :pref, :area, :data, '00000000')" - ) + sql = """ + INSERT INTO arcade (name, description, pref, area, data, pin) + VALUES (:name, :desc, :pref, :area, :data, '00000000') + """ cursor = self.execute( sql, { @@ -345,9 +356,10 @@ class MachineData(BaseData): raise ArcadeCreationException("Failed to create arcade!") arcadeid = cursor.lastrowid for owner in owners: - sql = ( - "INSERT INTO arcade_owner (userid, arcadeid) VALUES(:userid, :arcadeid)" - ) + sql = """ + INSERT INTO arcade_owner (userid, arcadeid) + VALUES (:userid, :arcadeid) + """ self.execute(sql, {"userid": owner, "arcadeid": arcadeid}) new_arcade = self.get_arcade(arcadeid) if new_arcade is None: @@ -364,9 +376,10 @@ class MachineData(BaseData): Returns: An Arcade object if this arcade was found, or None otherwise. """ - sql = ( - "SELECT name, description, pin, pref, area, data FROM arcade WHERE id = :id" - ) + sql = """ + SELECT name, description, pin, pref, area, data + FROM arcade WHERE id = :id + """ cursor = self.execute(sql, {"id": arcadeid}) if cursor.rowcount != 1: # Arcade doesn't exist @@ -396,11 +409,17 @@ class MachineData(BaseData): arcade - An Arcade object that should be updated. """ # Update machine name based on game - sql = ( - "UPDATE `arcade` " - + "SET name = :name, description = :desc, pin = :pin, pref = :pref, area = :area, data = :data " - + "WHERE id = :arcadeid" - ) + sql = """ + UPDATE `arcade` + SET + name = :name, + description = :desc, + pin = :pin, + pref = :pref, + area = :area, + data = :data + WHERE id = :arcadeid + """ self.execute( sql, { @@ -416,9 +435,10 @@ class MachineData(BaseData): sql = "DELETE FROM `arcade_owner` WHERE arcadeid = :arcadeid" self.execute(sql, {"arcadeid": arcade.id}) for owner in arcade.owners: - sql = ( - "INSERT INTO arcade_owner (userid, arcadeid) VALUES(:userid, :arcadeid)" - ) + sql = """ + INSERT INTO arcade_owner (userid, arcadeid) + VALUES (:userid, :arcadeid) + """ self.execute(sql, {"userid": owner, "arcadeid": arcade.id}) def destroy_arcade(self, arcadeid: ArcadeID) -> None: @@ -515,11 +535,11 @@ class MachineData(BaseData): setting - String identifying the particular setting we're interestsed in. data - A dictionary that should be saved for this setting. """ - sql = ( - "INSERT INTO arcade_settings (arcadeid, game, version, type, data) " - + "VALUES (:id, :game, :version, :type, :data) " - "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO arcade_settings (arcadeid, game, version, type, data) + VALUES (:id, :game, :version, :type, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, { diff --git a/bemani/data/mysql/music.py b/bemani/data/mysql/music.py index c5483bc..65c035e 100644 --- a/bemani/data/mysql/music.py +++ b/bemani/data/mysql/music.py @@ -151,19 +151,27 @@ class MusicData(BaseData): # Add to user score if new_record: # We want to update the timestamp/location to now if its a new record. - sql = ( - "INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) " - + "VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) " - + "ON DUPLICATE KEY UPDATE data = VALUES(data), points = VALUES(points), " - + "timestamp = VALUES(timestamp), `update` = VALUES(`update`), lid = VALUES(lid)" - ) + sql = """ + INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) + VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) + ON DUPLICATE KEY UPDATE + data = VALUES(data), + points = VALUES(points), + `update` = VALUES(`update`), + timestamp = VALUES(timestamp), + lid = VALUES(lid) + """ else: - # We only want to add the timestamp if it is new. - sql = ( - "INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) " - + "VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) " - + "ON DUPLICATE KEY UPDATE data = VALUES(data), points = VALUES(points), `update` = VALUES(`update`)" - ) + # We don't want to add the timestamp of the record since it wasn't a new high score. + # We also don't want to update thet location since this wasn't a new record. + sql = """ + INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) + VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) + ON DUPLICATE KEY UPDATE + data = VALUES(data), + points = VALUES(points), + `update` = VALUES(`update`) + """ self.execute( sql, { @@ -213,10 +221,10 @@ class MusicData(BaseData): ts = timestamp if timestamp is not None else Time.now() # Add to score history - sql = ( - "INSERT INTO `score_history` (userid, musicid, timestamp, lid, new_record, points, data) " - + "VALUES (:userid, :musicid, :timestamp, :location, :new_record, :points, :data)" - ) + sql = """ + INSERT INTO `score_history` (userid, musicid, timestamp, lid, new_record, points, data) + VALUES (:userid, :musicid, :timestamp, :location, :new_record, :points, :data) + """ try: self.execute( sql, @@ -256,12 +264,30 @@ class MusicData(BaseData): Returns: The optional data stored by the game previously, or None if no score exists. """ - sql = ( - "SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, score.lid AS lid, " - + "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = :userid) AS plays, " - + "score.points AS points, score.data AS data FROM score, music WHERE score.userid = :userid AND score.musicid = music.id " - + "AND music.game = :game AND music.version = :version AND music.songid = :songid AND music.chart = :songchart" - ) + sql = """ + SELECT + music.songid AS songid, + music.chart AS chart, + score.id AS scorekey, + score.timestamp AS timestamp, + score.update AS `update`, + score.lid AS lid, + ( + SELECT COUNT(score_history.timestamp) + FROM score_history + WHERE score_history.musicid = music.id AND score_history.userid = :userid + ) AS plays, + score.points AS points, + score.data AS data + FROM score, music + WHERE + score.userid = :userid AND + score.musicid = music.id AND + music.game = :game AND + music.version = :version AND + music.songid = :songid AND + music.chart = :songchart + """ cursor = self.execute( sql, { @@ -303,13 +329,29 @@ class MusicData(BaseData): Returns: The optional data stored by the game previously, or None if no score exists. """ - sql = ( - "SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, " - + "score.userid AS userid, score.lid AS lid, " - + "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = score.userid) AS plays, " - + "score.points AS points, score.data AS data FROM score, music WHERE score.id = :scorekey AND score.musicid = music.id " - + "AND music.game = :game AND music.version = :version" - ) + sql = """ + SELECT + music.songid AS songid, + music.chart AS chart, + score.id AS scorekey, + score.timestamp AS timestamp, + score.update AS `update`, + score.userid AS userid, + score.lid AS lid, + ( + SELECT COUNT(score_history.timestamp) + FROM score_history + WHERE score_history.musicid = music.id AND score_history.userid = score.userid + ) AS plays, + score.points AS points, + score.data AS data + FROM score, music + WHERE + score.id = :scorekey AND + score.musicid = music.id AND + music.game = :game AND + music.version = :version + """ cursor = self.execute( sql, { @@ -357,12 +399,27 @@ class MusicData(BaseData): Returns: A list of Score objects representing all high scores for a game. """ - sql = ( - "SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, score.lid AS lid, " - + "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = :userid) AS plays, " - + "score.points AS points, score.data AS data FROM score, music WHERE score.userid = :userid AND score.musicid = music.id " - + "AND music.game = :game AND music.version = :version" - ) + sql = """ + SELECT + music.songid AS songid, + music.chart AS chart, + score.id AS scorekey, + score.timestamp AS timestamp, + score.update AS `update`, + score.lid AS lid, + ( + select COUNT(score_history.timestamp) FROM score_history + WHERE score_history.musicid = music.id AND score_history.userid = :userid + ) AS plays, + score.points AS points, + score.data AS data + FROM score, music + WHERE + score.userid = :userid AND + score.musicid = music.id AND + music.game = :game AND + music.version = :version + """ if since is not None: sql = sql + " AND score.update >= :since" if until is not None: @@ -408,12 +465,18 @@ class MusicData(BaseData): Returns: A list of tuples, containing the songid and the number of plays across all charts for that song. """ - sql = ( - "SELECT music.songid AS songid, COUNT(score_history.timestamp) AS plays FROM score_history, music " - + "WHERE score_history.userid = :userid AND score_history.musicid = music.id " - + "AND music.game = :game AND music.version = :version " - + "GROUP BY songid ORDER BY plays DESC LIMIT :count" - ) + sql = """ + SELECT + music.songid AS songid, + COUNT(score_history.timestamp) AS plays + FROM score_history, music + WHERE + score_history.userid = :userid AND + score_history.musicid = music.id AND + music.game = :game AND + music.version = :version + GROUP BY songid ORDER BY plays DESC LIMIT :count + """ cursor = self.execute( sql, {"userid": userid, "game": game.value, "version": version, "count": count}, @@ -436,12 +499,18 @@ class MusicData(BaseData): Returns: A list of tuples, containing the songid and the last played time for this song. """ - sql = ( - "SELECT DISTINCT(music.songid) AS songid, score_history.timestamp AS timestamp FROM score_history, music " - + "WHERE score_history.userid = :userid AND score_history.musicid = music.id " - + "AND music.game = :game AND music.version = :version " - + "ORDER BY timestamp DESC LIMIT :count" - ) + sql = """ + SELECT + DISTINCT(music.songid) AS songid, + score_history.timestamp AS timestamp + FROM score_history, music + WHERE + score_history.userid = :userid AND + score_history.musicid = music.id AND + music.game = :game AND + music.version = :version + ORDER BY timestamp DESC LIMIT :count + """ cursor = self.execute( sql, {"userid": userid, "game": game.value, "version": version, "count": count}, @@ -467,10 +536,16 @@ class MusicData(BaseData): Returns: A list of tuples, containing the songid and the number of plays across all charts for that song. """ - sql = ( - "SELECT music.songid AS songid, COUNT(score_history.timestamp) AS plays FROM score_history, music " - + "WHERE score_history.musicid = music.id AND music.game = :game AND music.version = :version " - ) + sql = """ + SELECT + music.songid AS songid, + COUNT(score_history.timestamp) AS plays + FROM score_history, music + WHERE + score_history.musicid = music.id AND + music.game = :game AND + music.version = :version + """ timestamp: Optional[int] = None if days is not None: # Only select the last X days of hit chart @@ -509,11 +584,19 @@ class MusicData(BaseData): Returns: A Song object representing the song details """ - sql = ( - "SELECT music.name AS name, music.artist AS artist, music.genre AS genre, music.data AS data " - + "FROM music WHERE music.game = :game AND music.version = :version AND " - + "music.songid = :songid AND music.chart = :songchart" - ) + sql = """ + SELECT + music.name AS name, + music.artist AS artist, + music.genre AS genre, + music.data AS data + FROM music + WHERE + music.game = :game AND + music.version = :version AND + music.songid = :songid AND + music.chart = :songchart + """ cursor = self.execute( sql, { @@ -553,10 +636,10 @@ class MusicData(BaseData): Returns: A list of Song objects detailing the song information for each song. """ - sql = ( - "SELECT version, songid, chart, name, artist, genre, data FROM music " - "WHERE music.game = :game" - ) + sql = """ + SELECT version, songid, chart, name, artist, genre, data + FROM music WHERE music.game = :game + """ params: Dict[str, Any] = {"game": game.value} if version is not None: sql += " AND music.version = :version" @@ -579,49 +662,6 @@ class MusicData(BaseData): for result in cursor ] - def get_all_versions_of_song( - self, - game: GameConstants, - version: int, - songid: int, - songchart: int, - interested_versions: Optional[List[int]] = None, - ) -> List[Song]: - """ - Given a game/version/songid/chart, look up all versions of that song across all game versions. - - Parameters: - game - Enum value representing a game series. - version - Integer representing which version of the game. - songid - Integer representing the ID (from the game) for this song. - songchart - Integer representing the chart for this song. - - Returns: - A list of Song objects representing all song versions. - """ - musicid = self.__get_musicid(game, version, songid, songchart) - sql = ( - "SELECT version, songid, chart, name, artist, genre, data FROM music " - "WHERE music.id = :musicid" - ) - if interested_versions is not None: - sql += f" AND music.version in ({','.join(str(int(v)) for v in interested_versions)})" - cursor = self.execute(sql, {"musicid": musicid}) - - return [ - Song( - game, - result["version"], - result["songid"], - result["chart"], - result["name"], - result["artist"], - result["genre"], - self.deserialize(result["data"]), - ) - for result in cursor - ] - def get_all_scores( self, game: GameConstants, @@ -663,10 +703,20 @@ class MusicData(BaseData): innerselect = innerselect + " AND chart = :songchart" # Finally, construct the full query - sql = ( - "SELECT ({}) AS songid, ({}) AS chart, id AS scorekey, points, timestamp, `update`, lid, data, userid, ({}) AS plays " - "FROM score WHERE musicid IN ({})" - ).format(songidquery, chartquery, playselect, innerselect) + sql = f""" + SELECT + ({songidquery}) AS songid, + ({chartquery}) AS chart, + id AS scorekey, + points, + timestamp, + `update`, + lid, + data, + userid, + ({playselect}) AS plays + FROM score WHERE musicid IN ({innerselect}) + """ # Now, limit the query if userid is not None: @@ -766,17 +816,30 @@ class MusicData(BaseData): params["userlist"] = tuple(userlist) else: user_sql = f"SELECT userid FROM score WHERE score.musicid = played.musicid {location_sql} ORDER BY points DESC, timestamp DESC LIMIT 1" - records_sql = ( - f"SELECT ({user_sql}) AS userid, musicid FROM ({musicid_sql}) played" - ) + records_sql = f""" + SELECT ({user_sql}) AS userid, musicid + FROM ({musicid_sql}) played + """ # Now, join it up against the score and music table to grab the info we need - sql = ( - "SELECT ({}) AS songid, ({}) AS chart, score.points AS points, score.userid AS userid, score.id AS scorekey, score.data AS data, " - + "score.timestamp AS timestamp, score.update AS `update`, " - + "score.lid AS lid, (select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = score.musicid) AS plays " - + "FROM score, ({}) records WHERE records.userid = score.userid AND records.musicid = score.musicid" - ).format(songidquery, chartquery, records_sql) + sql = f""" + SELECT + ({songidquery}) AS songid, + ({chartquery}) AS chart, + score.points AS points, + score.userid AS userid, + score.id AS scorekey, + score.data AS data, + score.timestamp AS timestamp, + score.update AS `update`, + score.lid AS lid, + ( + SELECT COUNT(score_history.timestamp) FROM score_history + WHERE score_history.musicid = score.musicid + ) AS plays + FROM score, ({records_sql}) records + WHERE records.userid = score.userid AND records.musicid = score.musicid + """ cursor = self.execute(sql, params) return [ @@ -811,11 +874,24 @@ class MusicData(BaseData): Returns: The optional data stored by the game previously, or None if no score exists. """ - sql = ( - "SELECT music.songid AS songid, music.chart AS chart, score_history.id AS scorekey, score_history.timestamp AS timestamp, score_history.userid AS userid, " - + "score_history.lid AS lid, score_history.new_record AS new_record, score_history.points AS points, score_history.data AS data FROM score_history, music " - + "WHERE score_history.id = :scorekey AND score_history.musicid = music.id AND music.game = :game AND music.version = :version" - ) + sql = """ + SELECT + music.songid AS songid, + music.chart AS chart, + score_history.id AS scorekey, + score_history.timestamp AS timestamp, + score_history.userid AS userid, + score_history.lid AS lid, + score_history.new_record AS new_record, + score_history.points AS points, + score_history.data AS data + FROM score_history, music + WHERE + score_history.id = :scorekey AND + score_history.musicid = music.id AND + music.game = :game AND + music.version = :version + """ cursor = self.execute( sql, { @@ -882,10 +958,19 @@ class MusicData(BaseData): innerselect = innerselect + " AND chart = :songchart" # Finally, construct the full query - sql = ( - "SELECT ({}) AS songid, ({}) AS chart, id AS scorekey, timestamp, points, new_record, lid, data, userid " - "FROM score_history WHERE musicid IN ({})" - ).format(songidquery, chartquery, innerselect) + sql = f""" + SELECT + ({songidquery}) AS songid, + ({chartquery}) AS chart, + id AS scorekey, + timestamp, + points, + new_record, + lid, + data, + userid + FROM score_history WHERE musicid IN ({innerselect}) + """ # Now, limit the query if userid is not None: diff --git a/bemani/data/mysql/network.py b/bemani/data/mysql/network.py index 79b88d1..04e59c3 100644 --- a/bemani/data/mysql/network.py +++ b/bemani/data/mysql/network.py @@ -170,11 +170,14 @@ class NetworkData(BaseData): "Logic error, specify either 'daily' or 'weekly' for schedule type!" ) - sql = ( - "SELECT year, day FROM scheduled_work " - "WHERE game = :game AND version = :version AND " - "name = :name AND schedule = :schedule" - ) + sql = """ + SELECT year, day FROM scheduled_work + WHERE + game = :game AND + version = :version AND + name = :name AND + schedule = :schedule + """ cursor = self.execute( sql, { @@ -219,11 +222,11 @@ class NetworkData(BaseData): if schedule == "daily": year, day = Time.days_into_year() - sql = ( - "INSERT INTO scheduled_work (game, version, name, schedule, year, day) " - + "VALUES (:game, :version, :name, :schedule, :year, :day) " - + "ON DUPLICATE KEY UPDATE year=VALUES(year), day=VALUES(day)" - ) + sql = """ + INSERT INTO scheduled_work (game, version, name, schedule, year, day) + VALUES (:game, :version, :name, :schedule, :year, :day) + ON DUPLICATE KEY UPDATE year=VALUES(year), day=VALUES(day) + """ self.execute( sql, { @@ -238,11 +241,11 @@ class NetworkData(BaseData): if schedule == "weekly": days = Time.week_in_days_since_epoch() - sql = ( - "INSERT INTO scheduled_work (game, version, name, schedule, day) " - + "VALUES (:game, :version, :name, :schedule, :day) " - + "ON DUPLICATE KEY UPDATE day=VALUES(day)" - ) + sql = """ + INSERT INTO scheduled_work (game, version, name, schedule, day) + VALUES (:game, :version, :name, :schedule, :day) + ON DUPLICATE KEY UPDATE day=VALUES(day) + """ self.execute( sql, { diff --git a/bemani/data/mysql/user.py b/bemani/data/mysql/user.py index d5bc84b..10414b9 100644 --- a/bemani/data/mysql/user.py +++ b/bemani/data/mysql/user.py @@ -509,12 +509,17 @@ class UserData(BaseData): Returns: A dictionary previously stored by a game class if found, or None otherwise. """ - sql = ( - "SELECT refid.refid AS refid, extid.extid AS extid, profile.data AS data " - + "FROM refid, extid, profile " - + "WHERE refid.userid = :userid AND refid.game = :game AND refid.version = :version AND " - "extid.userid = refid.userid AND extid.game = refid.game AND profile.refid = refid.refid" - ) + sql = """ + SELECT refid.refid AS refid, extid.extid AS extid, profile.data AS data + FROM refid, extid, profile + WHERE + refid.userid = :userid AND + refid.game = :game AND + refid.version = :version AND + extid.userid = refid.userid AND + extid.game = refid.game AND + profile.refid = refid.refid + """ cursor = self.execute( sql, {"userid": userid, "game": game.value, "version": version} ) @@ -650,12 +655,16 @@ class UserData(BaseData): Returns: A list of (UserID, dictionaries) previously stored by a game class for each profile. """ - sql = ( - "SELECT refid.userid AS userid, refid.refid AS refid, extid.extid AS extid, profile.data AS data " - "FROM refid, profile, extid " - "WHERE refid.game = :game AND refid.version = :version " - "AND refid.refid = profile.refid AND extid.game = refid.game AND extid.userid = refid.userid" - ) + sql = """ + SELECT refid.userid AS userid, refid.refid AS refid, extid.extid AS extid, profile.data AS data + FROM refid, profile, extid + WHERE + refid.game = :game AND + refid.version = :version AND + refid.refid = profile.refid AND + extid.game = refid.game AND + extid.userid = refid.userid + """ cursor = self.execute(sql, {"game": game.value, "version": version}) return [ @@ -683,10 +692,10 @@ class UserData(BaseData): Returns: A list of UserIDs for users that played this version of this game. """ - sql = ( - "SELECT refid.userid AS userid FROM refid " - "WHERE refid.game = :game AND refid.version = :version" - ) + sql = """ + SELECT refid.userid AS userid FROM refid + WHERE refid.game = :game AND refid.version = :version + """ cursor = self.execute(sql, {"game": game.value, "version": version}) return [UserID(result["userid"]) for result in cursor] @@ -708,11 +717,18 @@ class UserData(BaseData): Returns: A list of (UserID, Achievement) objects. """ - sql = ( - "SELECT achievement.id AS id, achievement.type AS type, achievement.data AS data, " - "refid.userid AS userid FROM achievement, refid WHERE refid.game = :game AND " - "refid.version = :version AND refid.refid = achievement.refid" - ) + sql = """ + SELECT + achievement.id AS id, + achievement.type AS type, + achievement.data AS data, + refid.userid AS userid + FROM achievement, refid + WHERE + refid.game = :game AND + refid.version = :version AND + refid.refid = achievement.refid + """ params: Dict[str, Any] = {"game": game.value, "version": version} if achievementtype is not None: sql += " AND achievement.type = :type" @@ -750,11 +766,11 @@ class UserData(BaseData): refid = self.get_refid(game, version, userid) # Add profile json to game profile - sql = ( - "INSERT INTO profile (refid, data) " - + "VALUES (:refid, :json) " - + "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO profile (refid, data) + VALUES (:refid, :json) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute(sql, {"refid": refid, "json": self.serialize(profile)}) # Update profile details just in case this was a new profile that was just saved. @@ -866,11 +882,11 @@ class UserData(BaseData): refid = self.get_refid(game, version, userid) # Add achievement JSON to achievements - sql = ( - "INSERT INTO achievement (refid, id, type, data) " - + "VALUES (:refid, :id, :type, :data) " - + "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO achievement (refid, id, type, data) + VALUES (:refid, :id, :type, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, { @@ -902,9 +918,10 @@ class UserData(BaseData): refid = self.get_refid(game, version, userid) # Nuke the achievement from the user - sql = ( - "DELETE FROM achievement WHERE refid = :refid AND id = :id AND type = :type" - ) + sql = """ + DELETE FROM achievement + WHERE refid = :refid AND id = :id AND type = :type + """ self.execute( sql, {"refid": refid, "id": achievementid, "type": achievementtype} ) @@ -979,10 +996,10 @@ class UserData(BaseData): refid = self.get_refid(game, version, userid) # Add achievement JSON to achievements - sql = ( - "INSERT INTO time_based_achievement (refid, id, type, timestamp, data) " - + "VALUES (:refid, :id, :type, :ts, :data)" - ) + sql = """ + INSERT INTO time_based_achievement (refid, id, type, timestamp, data) + VALUES (:refid, :id, :type, :ts, :data) + """ self.execute( sql, { @@ -1007,12 +1024,19 @@ class UserData(BaseData): Returns: A list of (UserID, Achievement) objects. """ - sql = ( - "SELECT time_based_achievement.id AS id, time_based_achievement.type AS type, " - "time_based_achievement.data AS data, time_based_achievement.timestamp AS timestamp, " - "refid.userid AS userid FROM time_based_achievement, refid WHERE refid.game = :game AND " - "refid.version = :version AND refid.refid = time_based_achievement.refid" - ) + sql = """ + SELECT + time_based_achievement.id AS id, + time_based_achievement.type AS type, + time_based_achievement.data AS data, + time_based_achievement.timestamp AS timestamp, + refid.userid AS userid + FROM time_based_achievement, refid + WHERE + refid.game = :game AND + refid.version = :version AND + refid.refid = time_based_achievement.refid + """ cursor = self.execute(sql, {"game": game.value, "version": version}) return [ @@ -1052,7 +1076,16 @@ class UserData(BaseData): Returns: A dictionary as stored by a game class previously, or None if not found. """ - sql = "SELECT data FROM link WHERE game = :game AND version = :version AND userid = :userid AND type = :type AND other_userid = :other_userid" + sql = """ + SELECT data + FROM link + WHERE + game = :game AND + version = :version AND + userid = :userid AND + type = :type AND + other_userid = :other_userid + """ cursor = self.execute( sql, { @@ -1084,7 +1117,11 @@ class UserData(BaseData): Returns: A list of Link objects. """ - sql = "SELECT type, other_userid, data FROM link WHERE game = :game AND version = :version AND userid = :userid" + sql = """ + SELECT type, other_userid, data + FROM link + WHERE game = :game AND version = :version AND userid = :userid + """ cursor = self.execute( sql, {"game": game.value, "version": version, "userid": userid} ) @@ -1120,11 +1157,11 @@ class UserData(BaseData): data - A dictionary of data that the game wishes to retrieve later. """ # Add link JSON to link - sql = ( - "INSERT INTO link (game, version, userid, type, other_userid, data) " - "VALUES (:game, :version, :userid, :type, :other_userid, :data) " - "ON DUPLICATE KEY UPDATE data=VALUES(data)" - ) + sql = """ + INSERT INTO link (game, version, userid, type, other_userid, data) + VALUES (:game, :version, :userid, :type, :other_userid, :data) + ON DUPLICATE KEY UPDATE data=VALUES(data) + """ self.execute( sql, { @@ -1155,7 +1192,15 @@ class UserData(BaseData): linktype - The type of link. other_userid - Integer user ID of the account we're linked to. """ - sql = "DELETE FROM link WHERE game = :game AND version = :version AND userid = :userid AND type = :type AND other_userid = :other_userid" + sql = """ + DELETE FROM link + WHERE + game = :game AND + version = :version AND + userid = :userid AND + type = :type AND + other_userid = :other_userid + """ self.execute( sql, { @@ -1200,10 +1245,10 @@ class UserData(BaseData): Returns: The new PASELI balance if successful, or None if there wasn't enough to apply the delta. """ - sql = ( - "INSERT INTO balance (userid, arcadeid, balance) VALUES (:userid, :arcadeid, :delta) " - "ON DUPLICATE KEY UPDATE balance = balance + :delta" - ) + sql = """ + INSERT INTO balance (userid, arcadeid, balance) VALUES (:userid, :arcadeid, :delta) + ON DUPLICATE KEY UPDATE balance = balance + :delta + """ self.execute(sql, {"delta": delta, "userid": userid, "arcadeid": arcadeid}) newbalance = self.get_balance(userid, arcadeid) if newbalance < 0: @@ -1318,10 +1363,10 @@ class UserData(BaseData): break # Use that extid - sql = ( - "INSERT INTO extid (game, extid, userid) " - + "VALUES (:game, :extid, :userid)" - ) + sql = """ + INSERT INTO extid (game, extid, userid) + VALUES (:game, :extid, :userid) + """ try: cursor = self.execute( sql, {"game": game.value, "extid": extid, "userid": userid} @@ -1341,10 +1386,10 @@ class UserData(BaseData): break # Use that refid - sql = ( - "INSERT INTO refid (game, version, refid, userid) " - + "VALUES (:game, :version, :refid, :userid)" - ) + sql = """ + INSERT INTO refid (game, version, refid, userid) + VALUES (:game, :version, :refid, :userid) + """ try: cursor = self.execute( sql,