Fixed db null handling #219 #229

Merged
edraft merged 4 commits from #219 into 1.0.0 2023-02-21 21:10:18 +01:00
6 changed files with 77 additions and 117 deletions

View File

@ -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};
"""

View File

@ -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}'

View File

@ -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};
"""

View File

@ -98,42 +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`
Ebola-Chan marked this conversation as resolved
Review

Wie wäre es hier mit:

def udpate_string(self) -> str:
    return str(
    	f"""
        UPDATE `UserJoinedGameServer`
        SET
        {"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"}
        `LastModifiedAt` = '{self._modified_at}'
        WHERE `Id` = {self._id};
    """
    )

Somit wäre das SQL-Statement besser wartbar, sollte neue Variablen dazu kommen, welche Nullwerte beinhalten könnten.

Wie wäre es hier mit: ```python def udpate_string(self) -> str: return str( f""" UPDATE `UserJoinedGameServer` SET {"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"} `LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ ) ``` Somit wäre das SQL-Statement besser wartbar, sollte neue Variablen dazu kommen, welche Nullwerte beinhalten könnten.
SET `LeavedOn` = '{self._leaved_on}',
SET

Wenn self._leaved_on nicht none ist, käme es hier nicht zum folgenden SQL-Statement:

UPDATE `UserJoinedGameServer`
`LeavedOn' = '20230221T20:58.000'
`LastModifiedAt` = '20230221T20:58.000'
WHERE `Id` = 1;

Welches durch das fehlende SET nicht ausgeführt werden kann?

Wenn ```self._leaved_on``` nicht none ist, käme es hier nicht zum folgenden SQL-Statement: ```sql UPDATE `UserJoinedGameServer` `LeavedOn' = '20230221T20:58.000' `LastModifiedAt` = '20230221T20:58.000' WHERE `Id` = 1; ``` Welches durch das fehlende ```SET``` nicht ausgeführt werden kann?
{"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"}
`LastModifiedAt` = '{self._modified_at}'
WHERE `Id` = {self._id};
"""

View File

@ -97,40 +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}',
SET

Auch hier das SQL-Statement so implementieren für bessere Wartbarkeit.

Auch hier das SQL-Statement so implementieren für bessere Wartbarkeit.

Auch hier das SET-Problem

Auch hier das SET-Problem
{"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"}
`LastModifiedAt` = '{self._modified_at}'
WHERE `UserId` = {self._user.id};
"""

View File

@ -105,42 +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}',
SET

Auch hier das SQL-Statement so implementieren für bessere Wartbarkeit.

Auch hier das SQL-Statement so implementieren für bessere Wartbarkeit.

Auch hier das SET-Problem

Auch hier das SET-Problem
{"" if self._leaved_on is None else f"`LeavedOn` = '{self._leaved_on}',"}
`LastModifiedAt` = '{self._modified_at}'
WHERE `JoinId` = {self._join_id};
"""