From 8937243725ae8ddd04d40fac7bba4f8a5842f5cb Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Feb 2023 12:42:47 +0100 Subject: [PATCH 1/3] Fixed db null handling #219 --- kdb-bot/src/bot_data/model/api_key.py | 5 +- kdb-bot/src/bot_data/model/auth_user.py | 48 +++++++++---------- kdb-bot/src/bot_data/model/user.py | 6 +-- .../bot_data/model/user_joined_game_server.py | 47 +++++++----------- .../src/bot_data/model/user_joined_server.py | 44 ++++++----------- .../model/user_joined_voice_channel.py | 47 +++++++----------- 6 files changed, 77 insertions(+), 120 deletions(-) diff --git a/kdb-bot/src/bot_data/model/api_key.py b/kdb-bot/src/bot_data/model/api_key.py index 47e2209e..94ce7dfd 100644 --- a/kdb-bot/src/bot_data/model/api_key.py +++ b/kdb-bot/src/bot_data/model/api_key.py @@ -69,11 +69,11 @@ class ApiKey(TableABC): return str( f""" INSERT INTO `ApiKeys` ( - `Identifier`, `Key`, `CreatorId`, `CreatedAt`, `LastModifiedAt` + `Identifier`, `Key`, {"" if self._creator is None else "`CreatorId`,"} `CreatedAt`, `LastModifiedAt` ) VALUES ( '{self._identifier}', '{self._key}', - {"NULL" if self._creator is None else self._creator.id}, + {"" if self._creator is None else f"{self._creator.id},"} '{self._created_at}', '{self._modified_at}' ); @@ -87,6 +87,7 @@ class ApiKey(TableABC): UPDATE `ApiKeys` SET `Identifier` = '{self._identifier}', `Key` = '{self._key}', + {"" if self._creator is None else f"`CreatorId` = {self._creator.id},"} `LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ diff --git a/kdb-bot/src/bot_data/model/auth_user.py b/kdb-bot/src/bot_data/model/auth_user.py index b9675f30..5ce5914e 100644 --- a/kdb-bot/src/bot_data/model/auth_user.py +++ b/kdb-bot/src/bot_data/model/auth_user.py @@ -215,30 +215,30 @@ class AuthUser(TableABC): `EMail`, `Password`, `PasswordSalt`, - `RefreshToken`, - `ConfirmationId`, - `ForgotPasswordId`, - `OAuthId`, + {"" if self._refresh_token is None else f"`RefreshToken`,"} + {"" if self._confirmation_id is None else f"`ConfirmationId`,"} + {"" if self._forgot_password_id is None else f"`ForgotPasswordId`,"} + {"" if self._oauth_id is None else f"`OAuthId`,"} `RefreshTokenExpiryTime`, `AuthRole`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._auth_user_id}, - '{self._first_name}', - '{self._last_name}', - '{self._email}', - '{self._password}', - '{self._password_salt}', - '{"NULL" if self._refresh_token is None else self._refresh_token}', - '{"NULL" if self._confirmation_id is None else self._confirmation_id}', - '{"NULL" if self._forgot_password_id is None else self._forgot_password_id}', - '{"NULL" if self._oauth_id is None else self._oauth_id}', - '{self._refresh_token_expire_time.isoformat()}', - {self._auth_role_id.value}, - '{self._created_at}', - '{self._modified_at}' - ) + ) VALUES ( + {self._auth_user_id}, + '{self._first_name}', + '{self._last_name}', + '{self._email}', + '{self._password}', + '{self._password_salt}', + {"" if self._refresh_token is None else f"'{self._refresh_token}',"} + {"" if self._confirmation_id is None else f"'{self._confirmation_id}',"} + {"" if self._forgot_password_id is None else f"'{self._forgot_password_id}',"} + {"" if self._oauth_id is None else f"'{self._oauth_id}',"} + '{self._refresh_token_expire_time.isoformat()}', + {self._auth_role_id.value}, + '{self._created_at}', + '{self._modified_at}' + ) """ ) @@ -252,10 +252,10 @@ class AuthUser(TableABC): `EMail` = '{self._email}', `Password` = '{self._password}', `PasswordSalt` = '{self._password_salt}', - `RefreshToken` = '{'null' if self._refresh_token is None else f'{self._refresh_token}'}', - `ConfirmationId` = '{'null' if self._confirmation_id is None else f'{self._confirmation_id}'}', - `ForgotPasswordId` = '{'null' if self._forgot_password_id is None else f'{self._forgot_password_id}'}', - `OAuthId` = '{'null' if self._oauth_id is None else f'{self._oauth_id}'}', + {'' if self._refresh_token is None else f"`RefreshToken` = '{self._refresh_token}',"} + {'' if self._confirmation_id is None else f"'`ConfirmationId` = '{self._confirmation_id}',"} + {'' if self._forgot_password_id is None else f"`ForgotPasswordId` = '{self._forgot_password_id}',"} + {'' if self._oauth_id is None else f"`OAuthId` = '{self._oauth_id}',"} `RefreshTokenExpiryTime` = '{self._refresh_token_expire_time.isoformat()}', `AuthRole` = {self._auth_role_id.value}, `LastModifiedAt` = '{self._modified_at}' diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index a9e8d437..908eff91 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -150,11 +150,11 @@ class User(TableABC): return str( f""" INSERT INTO `Users` ( - `DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt` + `DiscordId`, `XP`, {"" if self._minecraft_id is None else "`MinecraftId`,"} `ServerId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._discord_id}, {self._xp}, - '{self._minecraft_id}', + {"" if self._minecraft_id is None else f"'{self._minecraft_id}',"} {self._server.id}, '{self._created_at}', '{self._modified_at}' @@ -168,7 +168,7 @@ class User(TableABC): f""" UPDATE `Users` SET `XP` = {self._xp}, - `MinecraftId` = {'null' if self.minecraft_id is None else f'{self._minecraft_id}'}, + {"" if self._minecraft_id is None else f"`MinecraftId` = '{self._minecraft_id}',"} `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user_id}; """ diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index 964a19d0..babe98f5 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -98,43 +98,28 @@ class UserJoinedGameServer(TableABC): @property def insert_string(self) -> str: - if self._leaved_on is not None: - return str( - f""" - INSERT INTO `UserJoinedGameServer` ( - `UserId`, `GameServer`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - '{self._game_server}', - '{self._joined_on}', - '{self._leaved_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) - else: - return str( - f""" - INSERT INTO `UserJoinedGameServer` ( - `UserId`, `GameServer`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - '{self._game_server}', - '{self._joined_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) + return str( + f""" + INSERT INTO `UserJoinedGameServer` ( + `UserId`, `GameServer`, `JoinedOn`, {"" if self._leaved_on is None else "`LeavedOn`,"} `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.id}, + '{self._game_server}', + '{self._joined_on}', + {"" if self._leaved_on is None else f"'{self._leaved_on}',"} + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) @property def udpate_string(self) -> str: return str( f""" UPDATE `UserJoinedGameServer` - SET `LeavedOn` = '{self._leaved_on}', - `LastModifiedAt` = '{self._modified_at}' + {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} + {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ ) diff --git a/kdb-bot/src/bot_data/model/user_joined_server.py b/kdb-bot/src/bot_data/model/user_joined_server.py index ce007355..aff19623 100644 --- a/kdb-bot/src/bot_data/model/user_joined_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_server.py @@ -97,41 +97,27 @@ class UserJoinedServer(TableABC): @property def insert_string(self) -> str: - if self._leaved_on is not None: - return str( - f""" - INSERT INTO `UserJoinedServers` ( - `UserId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - '{self._joined_on}', - '{self._leaved_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) - else: - return str( - f""" - INSERT INTO `UserJoinedServers` ( - `UserId`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - '{self._joined_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) + return str( + f""" + INSERT INTO `UserJoinedServers` ( + `UserId`, `JoinedOn`, {"" if self._leaved_on is None else "`LeavedOn`,"} `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.id}, + '{self._joined_on}', + {"" if self._leaved_on is None else f"'{self._leaved_on}',"} + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) @property def udpate_string(self) -> str: return str( f""" UPDATE `UserJoinedServers` - SET `LeavedOn` = '{self._leaved_on}', - `LastModifiedAt` = '{self._modified_at}' + {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} + {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user.id}; """ ) diff --git a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py index 457c9fc1..0cea09b2 100644 --- a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py +++ b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py @@ -105,43 +105,28 @@ class UserJoinedVoiceChannel(TableABC): @property def insert_string(self) -> str: - if self._leaved_on is not None: - return str( - f""" - INSERT INTO `UserJoinedVoiceChannel` ( - `UserId`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - {self._channel_id}, - '{self._joined_on}', - '{self._leaved_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) - else: - return str( - f""" - INSERT INTO `UserJoinedVoiceChannel` ( - `UserId`, `DiscordChannelId`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` - ) VALUES ( - {self._user.id}, - {self._channel_id}, - '{self._joined_on}', - '{self._created_at}', - '{self._modified_at}' - ); - """ - ) + return str( + f""" + INSERT INTO `UserJoinedVoiceChannel` ( + `UserId`, `DiscordChannelId`, `JoinedOn`, {"" if self._leaved_on is None else "`LeavedOn`,"} `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.id}, + {self._channel_id}, + '{self._joined_on}', + {"" if self._leaved_on is None else f"'{self._leaved_on}',"} + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) @property def udpate_string(self) -> str: return str( f""" UPDATE `UserJoinedVoiceChannel` - SET `LeavedOn` = '{self._leaved_on}', - `LastModifiedAt` = '{self._modified_at}' + {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} + {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' WHERE `JoinId` = {self._join_id}; """ ) -- 2.45.2 From d42b76ba87a1cbfbc7dc2596f379a997cafed91a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Feb 2023 20:51:28 +0100 Subject: [PATCH 2/3] Improved SQL statements #219 --- kdb-bot/src/bot_data/model/user_joined_game_server.py | 6 +++--- kdb-bot/src/bot_data/model/user_joined_server.py | 4 ++-- kdb-bot/src/bot_data/model/user_joined_voice_channel.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index babe98f5..3b81dbfc 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -106,7 +106,7 @@ class UserJoinedGameServer(TableABC): {self._user.id}, '{self._game_server}', '{self._joined_on}', - {"" if self._leaved_on is None else f"'{self._leaved_on}',"} + {"" if self._leaved_on is None else f"'{self._leaved_on}',"} '{self._created_at}', '{self._modified_at}' ); @@ -118,8 +118,8 @@ class UserJoinedGameServer(TableABC): return str( f""" UPDATE `UserJoinedGameServer` - {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} - {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' + {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + `LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ ) diff --git a/kdb-bot/src/bot_data/model/user_joined_server.py b/kdb-bot/src/bot_data/model/user_joined_server.py index aff19623..c3dfdf28 100644 --- a/kdb-bot/src/bot_data/model/user_joined_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_server.py @@ -116,8 +116,8 @@ class UserJoinedServer(TableABC): return str( f""" UPDATE `UserJoinedServers` - {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} - {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' + {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user.id}; """ ) diff --git a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py index 0cea09b2..3360da44 100644 --- a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py +++ b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py @@ -125,8 +125,8 @@ class UserJoinedVoiceChannel(TableABC): return str( f""" UPDATE `UserJoinedVoiceChannel` - {"" if self._leaved_on is None else f"SET `LeavedOn` = '{self._leaved_on}',"} - {"SET " if self._leaved_on is None else ""}`LastModifiedAt` = '{self._modified_at}' + {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + `LastModifiedAt` = '{self._modified_at}' WHERE `JoinId` = {self._join_id}; """ ) -- 2.45.2 From f7dc8d135a174685d7d30fd0ce921ebfbcadda81 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Feb 2023 21:08:51 +0100 Subject: [PATCH 3/3] Improved SQL statements #219 --- kdb-bot/src/bot_data/model/user_joined_game_server.py | 3 ++- kdb-bot/src/bot_data/model/user_joined_server.py | 3 ++- kdb-bot/src/bot_data/model/user_joined_voice_channel.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index 3b81dbfc..1a1186e5 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -118,7 +118,8 @@ class UserJoinedGameServer(TableABC): return str( f""" UPDATE `UserJoinedGameServer` - {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + SET + {"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} `LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ diff --git a/kdb-bot/src/bot_data/model/user_joined_server.py b/kdb-bot/src/bot_data/model/user_joined_server.py index c3dfdf28..9af3168a 100644 --- a/kdb-bot/src/bot_data/model/user_joined_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_server.py @@ -116,7 +116,8 @@ class UserJoinedServer(TableABC): return str( f""" UPDATE `UserJoinedServers` - {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + SET + {"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user.id}; """ diff --git a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py index 3360da44..bb69e7cb 100644 --- a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py +++ b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py @@ -125,7 +125,8 @@ class UserJoinedVoiceChannel(TableABC): return str( f""" UPDATE `UserJoinedVoiceChannel` - {"SET " if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} + SET + {"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} `LastModifiedAt` = '{self._modified_at}' WHERE `JoinId` = {self._join_id}; """ -- 2.45.2