refactor(db): Update SqlQueryBuilder references + Various refactors
Consolidates the creation of SQL query builders across multiple classes to ensure a unified approach for database operations. Replaces manual type checks and specific query creators with a generic method for better maintainability and to prevent errors. Improves code readability and reduces duplication, facilitating easier updates in the future.
This commit is contained in:
parent
27fde1f9ac
commit
084411f847
15 changed files with 399 additions and 488 deletions
|
|
@ -53,10 +53,8 @@ namespace TShockAPI.DB
|
|||
new SqlColumn("LastAccessed", MySqlDbType.Text),
|
||||
new SqlColumn("KnownIPs", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
|
||||
SqlTableCreator creator = new(db, db.GetSqlQueryBuilder());
|
||||
creator.EnsureTableStructure(table);
|
||||
}
|
||||
|
||||
|
|
@ -241,12 +239,10 @@ namespace TShockAPI.DB
|
|||
{
|
||||
try
|
||||
{
|
||||
using (var reader = _database.QueryReader("SELECT * FROM Users WHERE Username=@0", username))
|
||||
using var reader = _database.QueryReader("SELECT * FROM Users WHERE Username=@0", username);
|
||||
if (reader.Read())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return reader.Get<int>("ID");
|
||||
}
|
||||
return reader.Get<int>("ID");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -310,16 +306,14 @@ namespace TShockAPI.DB
|
|||
|
||||
try
|
||||
{
|
||||
using (var result = _database.QueryReader(query, arg))
|
||||
using var result = _database.QueryReader(query, arg);
|
||||
if (result.Read())
|
||||
{
|
||||
if (result.Read())
|
||||
{
|
||||
account = LoadUserAccountFromResult(account, result);
|
||||
// Check for multiple matches
|
||||
if (!result.Read())
|
||||
return account;
|
||||
multiple = true;
|
||||
}
|
||||
account = LoadUserAccountFromResult(account, result);
|
||||
// Check for multiple matches
|
||||
if (!result.Read())
|
||||
return account;
|
||||
multiple = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -339,14 +333,12 @@ namespace TShockAPI.DB
|
|||
try
|
||||
{
|
||||
List<UserAccount> accounts = new List<UserAccount>();
|
||||
using (var reader = _database.QueryReader("SELECT * FROM Users"))
|
||||
using var reader = _database.QueryReader("SELECT * FROM Users");
|
||||
while (reader.Read())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
accounts.Add(LoadUserAccountFromResult(new UserAccount(), reader));
|
||||
}
|
||||
return accounts;
|
||||
accounts.Add(LoadUserAccountFromResult(new UserAccount(), reader));
|
||||
}
|
||||
return accounts;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -367,14 +359,13 @@ namespace TShockAPI.DB
|
|||
{
|
||||
List<UserAccount> accounts = new List<UserAccount>();
|
||||
string search = notAtStart ? string.Format("%{0}%", username) : string.Format("{0}%", username);
|
||||
using (var reader = _database.QueryReader("SELECT * FROM Users WHERE Username LIKE @0",
|
||||
search))
|
||||
using var reader = _database.QueryReader("SELECT * FROM Users WHERE Username LIKE @0",
|
||||
search);
|
||||
while (reader.Read())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
accounts.Add(LoadUserAccountFromResult(new UserAccount(), reader));
|
||||
}
|
||||
accounts.Add(LoadUserAccountFromResult(new UserAccount(), reader));
|
||||
}
|
||||
|
||||
return accounts;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -497,7 +488,7 @@ namespace TShockAPI.DB
|
|||
int currentWorkFactor;
|
||||
try
|
||||
{
|
||||
currentWorkFactor = Int32.Parse((Password.Split('$')[2]));
|
||||
currentWorkFactor = int.Parse((Password.Split('$')[2]));
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue