refactor(db/pgsql): Revert SQL queries, lower column casing for PGSQL
Reverts SQL query identifier escaping to simplify queries and improves overall readability by using lowercase column names. Enhances maintainability and alignment with database conventions by adopting a uniform casing scheme across all SQL operations. Removes unnecessary complexity in query construction, streamlining the database operations performed within the application.
This commit is contained in:
parent
4c13084eb3
commit
9c473e35a6
8 changed files with 79 additions and 111 deletions
|
|
@ -70,22 +70,15 @@ namespace TShockAPI.DB
|
|||
int ret;
|
||||
try
|
||||
{
|
||||
string query = _database.GetSqlType() switch
|
||||
{
|
||||
SqlType.Postgres => "INSERT INTO Users (\"Username\", \"Password\", \"UUID\", \"Usergroup\", \"Registered\") VALUES (@0, @1, @2, @3, @4);",
|
||||
_ => "INSERT INTO Users (Username, Password, UUID, Usergroup, Registered) VALUES (@0, @1, @2, @3, @4);"
|
||||
};
|
||||
|
||||
ret = _database.Query(query, account.Name, account.Password, account.UUID, account.Group, DateTime.UtcNow.ToString("s"));
|
||||
ret = _database.Query("INSERT INTO Users (Username, Password, UUID, UserGroup, Registered) VALUES (@0, @1, @2, @3, @4);", account.Name,
|
||||
account.Password, account.UUID, account.Group, DateTime.UtcNow.ToString("s"));
|
||||
}
|
||||
// Detect duplicate user using a regexp as Sqlite doesn't have well structured exceptions
|
||||
catch (Exception e) when (Regex.IsMatch(e.Message, "Username.*not unique|UNIQUE constraint failed: Users\\.Username"))
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserAccountExistsException(account.Name);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new UserAccountManagerException(GetString($"AddUser SQL returned an error ({e.Message})"), e);
|
||||
// Detect duplicate user using a regexp as Sqlite doesn't have well structured exceptions
|
||||
if (Regex.IsMatch(ex.Message, "Username.*not unique|UNIQUE constraint failed: Users\\.Username"))
|
||||
throw new UserAccountExistsException(account.Name);
|
||||
throw new UserAccountManagerException(GetString($"AddUser SQL returned an error ({ex.Message})"), ex);
|
||||
}
|
||||
|
||||
if (1 > ret)
|
||||
|
|
@ -106,7 +99,7 @@ namespace TShockAPI.DB
|
|||
TShock.Players.Where(p => p?.IsLoggedIn == true && p.Account.Name == account.Name).ForEach(p => p.Logout());
|
||||
|
||||
UserAccount tempuser = GetUserAccount(account);
|
||||
int affected = _database.Query($"DELETE FROM Users WHERE {"Username".EscapeSqlId(_database)}=@0", account.Name);
|
||||
int affected = _database.Query("DELETE FROM Users WHERE Username=@0", account.Name);
|
||||
|
||||
if (affected < 1)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
|
|
@ -131,11 +124,10 @@ namespace TShockAPI.DB
|
|||
{
|
||||
account.CreateBCryptHash(password);
|
||||
|
||||
if (_database.Query($"UPDATE Users SET {"Password".EscapeSqlId(_database)} = @0 WHERE {"Username".EscapeSqlId(_database)} = @1;",
|
||||
account.Password, account.Name) is 0)
|
||||
{
|
||||
if (
|
||||
_database.Query("UPDATE Users SET Password = @0 WHERE Username = @1;", account.Password,
|
||||
account.Name) == 0)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -152,10 +144,10 @@ namespace TShockAPI.DB
|
|||
{
|
||||
try
|
||||
{
|
||||
if (_database.Query(/*lang=sql*/$"UPDATE Users SET {"UUID".EscapeSqlId(_database)} = @0 WHERE {"Username".EscapeSqlId(_database)} = @1;", uuid, account.Name) is 0)
|
||||
{
|
||||
if (
|
||||
_database.Query("UPDATE Users SET UUID = @0 WHERE Username = @1;", uuid,
|
||||
account.Name) == 0)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -177,7 +169,7 @@ namespace TShockAPI.DB
|
|||
if (AccountHooks.OnAccountGroupUpdate(account, ref grp))
|
||||
throw new UserGroupUpdateLockedException(account.Name);
|
||||
|
||||
if (_database.Query($"UPDATE Users SET {"UserGroup".EscapeSqlId(_database)} = @0 WHERE {"Username".EscapeSqlId(_database)} = @1;", grp.Name, account.Name) == 0)
|
||||
if (_database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", grp.Name, account.Name) == 0)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
|
||||
try
|
||||
|
|
@ -208,7 +200,7 @@ namespace TShockAPI.DB
|
|||
if (AccountHooks.OnAccountGroupUpdate(account, author, ref grp))
|
||||
throw new UserGroupUpdateLockedException(account.Name);
|
||||
|
||||
if (_database.Query($"UPDATE Users SET {"UserGroup".EscapeSqlId(_database)} = @0 WHERE {"Username".EscapeSqlId(_database)} = @1;", grp.Name, account.Name) == 0)
|
||||
if (_database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", grp.Name, account.Name) == 0)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
|
||||
try
|
||||
|
|
@ -231,12 +223,8 @@ namespace TShockAPI.DB
|
|||
{
|
||||
try
|
||||
{
|
||||
if (_database.Query(
|
||||
$"UPDATE Users SET {"LastAccessed".EscapeSqlId(_database)} = @0, {"KnownIPs".EscapeSqlId(_database)} = @1 WHERE {"Username".EscapeSqlId(_database)} = @2;",
|
||||
DateTime.UtcNow.ToString("s"), account.KnownIps, account.Name) is 0
|
||||
) {
|
||||
if (_database.Query("UPDATE Users SET LastAccessed = @0, KnownIps = @1 WHERE Username = @2;", DateTime.UtcNow.ToString("s"), account.KnownIps, account.Name) == 0)
|
||||
throw new UserAccountNotExistException(account.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -251,7 +239,7 @@ namespace TShockAPI.DB
|
|||
{
|
||||
try
|
||||
{
|
||||
using var reader = _database.QueryReader($"SELECT * FROM Users WHERE {"Username".EscapeSqlId(_database)}=@0", username);
|
||||
using var reader = _database.QueryReader("SELECT * FROM Users WHERE Username=@0", username);
|
||||
if (reader.Read())
|
||||
{
|
||||
return reader.Get<int>("ID");
|
||||
|
|
@ -305,13 +293,13 @@ namespace TShockAPI.DB
|
|||
object arg;
|
||||
if (account.ID != 0)
|
||||
{
|
||||
query = $"SELECT * FROM Users WHERE {"ID".EscapeSqlId(_database)}=@0";
|
||||
query = "SELECT * FROM Users WHERE ID=@0";
|
||||
arg = account.ID;
|
||||
type = "id";
|
||||
}
|
||||
else
|
||||
{
|
||||
query = $"SELECT * FROM Users WHERE {"Username".EscapeSqlId(_database)}=@0";
|
||||
query = "SELECT * FROM Users WHERE Username=@0";
|
||||
arg = account.Name;
|
||||
type = "name";
|
||||
}
|
||||
|
|
@ -370,9 +358,9 @@ namespace TShockAPI.DB
|
|||
try
|
||||
{
|
||||
List<UserAccount> accounts = new List<UserAccount>();
|
||||
string search = $"{(notAtStart ? "%" : "")}{username}%";
|
||||
using var reader = _database.QueryReader($"SELECT * FROM Users WHERE {"Username".EscapeSqlId(_database)} LIKE @0", search);
|
||||
|
||||
string search = notAtStart ? string.Format("%{0}%", username) : string.Format("{0}%", username);
|
||||
using var reader = _database.QueryReader("SELECT * FROM Users WHERE Username LIKE @0",
|
||||
search);
|
||||
while (reader.Read())
|
||||
{
|
||||
accounts.Add(LoadUserAccountFromResult(new UserAccount(), reader));
|
||||
|
|
@ -400,7 +388,7 @@ namespace TShockAPI.DB
|
|||
account.Name = result.Get<string>("Username");
|
||||
account.Registered = result.Get<string>("Registered");
|
||||
account.LastAccessed = result.Get<string>("LastAccessed");
|
||||
account.KnownIps = result.Get<string>("KnownIPs");
|
||||
account.KnownIps = result.Get<string>("KnownIps");
|
||||
return account;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue