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
|
|
@ -75,31 +75,44 @@ namespace TShockAPI.DB
|
|||
|
||||
public List<string> GetColumns(SqlTable table)
|
||||
{
|
||||
var ret = new List<string>();
|
||||
var name = database.GetSqlType();
|
||||
if (name == SqlType.Sqlite)
|
||||
List<string> ret = new();
|
||||
switch (database.GetSqlType())
|
||||
{
|
||||
using (var reader = database.QueryReader("PRAGMA table_info({0})".SFormat(table.Name)))
|
||||
case SqlType.Sqlite:
|
||||
{
|
||||
using QueryResult reader = database.QueryReader("PRAGMA table_info({0})".SFormat(table.Name));
|
||||
while (reader.Read())
|
||||
{
|
||||
ret.Add(reader.Get<string>("name"));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (name == SqlType.Mysql)
|
||||
{
|
||||
using (
|
||||
var reader =
|
||||
database.QueryReader(
|
||||
"SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME=@0 AND TABLE_SCHEMA=@1", table.Name,
|
||||
database.Database))
|
||||
case SqlType.Mysql:
|
||||
{
|
||||
using QueryResult reader =
|
||||
database.QueryReader("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME=@0 AND TABLE_SCHEMA=@1", table.Name, database.Database);
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
ret.Add(reader.Get<string>("COLUMN_NAME"));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
case SqlType.Postgres:
|
||||
{
|
||||
using QueryResult reader =
|
||||
database.QueryReader("SELECT column_name FROM information_schema.columns WHERE table_name=@0", table.Name);
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
ret.Add(reader.Get<string>("column_name"));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default: throw new NotSupportedException();
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
@ -110,4 +123,4 @@ namespace TShockAPI.DB
|
|||
database.Query(creator.DeleteRow(table, wheres));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue