Better CommandHandler

This commit is contained in:
JacobTech 2024-03-29 11:11:14 -04:00
parent e9ec3638c5
commit aa31f4589e
7 changed files with 499 additions and 66 deletions

View File

@ -3,64 +3,353 @@ using ServerDatabase.Utils;
namespace ServerDatabase; namespace ServerDatabase;
public class CommandHandler public class CommandHandler<TClass> : ICommandHandler where TClass : class, new()
{ {
internal List<IBetterTable> Tables = new();
internal List<IBetterParameter> _Parameters = new();
internal List<IBetterParameter> _Values = new();
internal List<Parameter> _ValuesOld = new();
internal List<Parameter> _ParametersOld = new();
public IReadOnlyList<IBetterParameter> Parameters
{
get => _Parameters.AsReadOnly();
}
public IReadOnlyList<IBetterParameter> Values
{
get => _Values.AsReadOnly();
}
private Order? Order = null;
private IBetterTable _betterTable; private IBetterTable _betterTable;
private string? ctc = null;
private char table_as = 'A';
private ICommandHandler? _CommandHandlerParent = null;
private ICommandHandler? _Child = null;
public char TableAsLetter { get => table_as; }
public string? CTC { get => ctc; }
public ICommandHandler? CommandHandlerParent { get => _CommandHandlerParent; }
public ICommandHandler? Child { get => _Child; }
public IBetterTable BetterTable { get => _betterTable; }
internal CommandHandler(IBetterTable betterTable) internal CommandHandler(IBetterTable betterTable)
{ {
this._betterTable = betterTable; this._betterTable = betterTable;
} }
private List<Parameter> Parameters = new(); private CommandHandler(IBetterTable betterTable, char TableAs, ICommandHandler ch)
private List<Parameter> Values = new();
private Order? Order = null;
public CommandHandler WithFilter(Parameter p)
{ {
Parameters.Add(p); this._betterTable = betterTable;
this._CommandHandlerParent = ch;
this.table_as = TableAs;
}
internal CommandHandler<TClass> WithFilter(IBetterParameter p)
{
_Parameters.Add(p);
return this; return this;
} }
public CommandHandler WithValue(Parameter p) internal CommandHandler<TClass> WithValue(IBetterParameter p)
{ {
Values.Add(p); _Values.Add(p);
return this; return this;
} }
public CommandHandler WithFilter<T>(TableColumn<T> column, T value, string sign = "=") where T : notnull internal CommandHandler<TClass> WithValue(Parameter p)
{ {
return WithFilter(column.CreateParameter(value, sign)); _ValuesOld.Add(p);
return this;
} }
public CommandHandler<TClass> WithFilter<T>(TableColumn<TClass, T> column, T value, string sign = "=") where T : notnull
public CommandHandler WithValue<T>(TableColumn<T> column, T value) where T : notnull
{ {
return WithValue(column.CreateParameter(value)); return WithFilter(new BetterParameter<T>()
{
Column = column.Name,
Sign = sign,
Value = value,
});
} }
public CommandHandler<TClass> WithValue<T>(TableColumn<TClass, T> column, T value) where T : notnull
public CommandHandler AscendBy<T>(TableColumn<T> column) where T : notnull {
return WithValue(new BetterParameter<T>()
{
Column = column.Name,
Sign = "=",
Value = value,
});
}
public CommandHandler<TClass> AscendBy<T>(TableColumn<TClass, T> column) where T : notnull
{ {
Order = column.GetAssendingOrder(); Order = column.GetAssendingOrder();
return this; return this;
} }
public CommandHandler DescendBy<T>(TableColumn<T> column) where T : notnull public CommandHandler<TClass> DescendBy<T>(TableColumn<TClass, T> column) where T : notnull
{ {
Order = column.GetDecendingOrder(); Order = column.GetDecendingOrder();
return this; return this;
} }
public CommandHandler<TCol> WithCrossTableCheck<TCol, T>(TableColumn<TClass, T> column1, Table<TCol> Table, TableColumn<TCol, T> column2) where TCol : class, new() where T : notnull
public T Read<T>(TableColumn<T> column) where T : notnull
{ {
return _betterTable.DatabaseHandler.Read<T>($"SELECT {column.Name} FROM {_betterTable.Name} WHERE", Order, Parameters.ToArray()); return WithCrossTableCheck(column1, "=", Table,column2);
} }
public void Insert() public CommandHandler<TCol> WithCrossTableCheck<TCol, T>(TableColumn<TClass, T> column1, string sign, Table<TCol> Table, TableColumn<TCol, T> column2) where TCol : class, new() where T : notnull
{ {
_betterTable.Insert(Values.ToArray()); ctc = $"{table_as}.{column1.Name} {sign} {(char)(table_as + 1)}.{column2.Name}";
CommandHandler<TCol> child = new CommandHandler<TCol>(Table, (char)(table_as + 1), this);
_Child = child;
return child;
} }
public T Read<T>(TableColumn<TClass, T> column) where T : notnull
{
string command = $"SELECT {TableAsLetter}.{column.Name} FROM {_betterTable.Name} AS {TableAsLetter}";
ICommandHandler? pc = this;
ICommandHandler? c = this.CommandHandlerParent;
while (c is not null)
{
command += $", {c.BetterTable.Name} AS {c.TableAsLetter}";
pc = c;
c = c.CommandHandlerParent;
}
command += " WHERE";
if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
char col_char = 'A';
string vals = "";
while (pc is not null)
{
foreach (IBetterParameter param in pc.Parameters)
{
vals += $"{pc.TableAsLetter}.{param.Column} {param.Sign} @{col_char} AND ";
cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
}
if (pc.CTC is not null) vals += pc.CTC + " AND ";
pc = pc.Child;
}
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
command += ";";
cmd.CommandText = command;
object? temp = cmd.ExecuteScalar();
con.Close();
if (temp is DBNull || temp is null) return default!;
if (typeof(T).IsEnum) return (T?)Enum.Parse(typeof(T), temp.ToString()!)!;
if (typeof(T).IsNullableEnum()) return (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, temp.ToString()!)!;
return (T?)temp!;
}
public (T, T2) Read<T, T2>(TableColumn<TClass, T> column, TableColumn<TClass, T2> column2) where T : notnull where T2 : notnull
{
string command = $"SELECT {TableAsLetter}.{column.Name}, {TableAsLetter}.{column2.Name} FROM {_betterTable.Name} AS {TableAsLetter}";
ICommandHandler? pc = this;
ICommandHandler? c = this.CommandHandlerParent;
while (c is not null)
{
command += $", {c.BetterTable.Name} AS {c.TableAsLetter}";
pc = c;
c = c.CommandHandlerParent;
}
command += " WHERE";
if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
char col_char = 'A';
string vals = "";
while (pc is not null)
{
foreach (IBetterParameter param in pc.Parameters)
{
vals += $"{pc.TableAsLetter}.{param.Column} {param.Sign} @{col_char} AND ";
cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
}
if (pc.CTC is not null) vals += pc.CTC + " AND ";
pc = pc.Child;
}
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
reader.Read();
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
string colname = reader.GetName(i);
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
con.Close();
return ((T)temp[0]!, (T2)temp[1]!);
}
public (T, T2, T3) Read<T, T2, T3>(TableColumn<TClass, T> column, TableColumn<TClass, T2> column2, TableColumn<TClass, T3> column3) where T : notnull where T2 : notnull where T3 : notnull
{
string command = $"SELECT {TableAsLetter}.{column.Name}, {TableAsLetter}.{column2.Name}, {TableAsLetter}.{column3.Name} FROM {_betterTable.Name} AS {TableAsLetter}";
ICommandHandler? pc = this;
ICommandHandler? c = this.CommandHandlerParent;
while (c is not null)
{
command += $", {c.BetterTable.Name} AS {c.TableAsLetter}";
pc = c;
c = c.CommandHandlerParent;
}
command += " WHERE";
if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
char col_char = 'A';
string vals = "";
while (pc is not null)
{
foreach (IBetterParameter param in pc.Parameters)
{
vals += $"{pc.TableAsLetter}.{param.Column} {param.Sign} @{col_char} AND ";
cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
}
if (pc.CTC is not null) vals += pc.CTC + " AND ";
pc = pc.Child;
}
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
reader.Read();
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
string colname = reader.GetName(i);
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
else if (i == 2)
{
if (typeof(T3).IsEnum) temp[i] = (T3?)Enum.Parse(typeof(T3), val.ToString()!)!;
if (typeof(T3).IsNullableEnum()) temp[i] = (T3?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T3))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
con.Close();
return ((T)temp[0]!, (T2)temp[1]!, (T3)temp[2]!);
}
public (T, T2, T3)[] ReadColumns<T, T2, T3>(TableColumn<TClass, T> column, TableColumn<TClass, T2> column2, TableColumn<TClass, T3> column3) where T : notnull where T2 : notnull where T3 : notnull
{
string command = $"SELECT {TableAsLetter}.{column.Name}, {TableAsLetter}.{column2.Name}, {TableAsLetter}.{column3.Name} FROM {_betterTable.Name} AS {TableAsLetter}";
ICommandHandler? pc = this;
ICommandHandler? c = this.CommandHandlerParent;
while (c is not null)
{
command += $", {c.BetterTable.Name} AS {c.TableAsLetter}";
pc = c;
c = c.CommandHandlerParent;
}
command += " WHERE";
if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
char col_char = 'A';
string vals = "";
while (pc is not null)
{
foreach (IBetterParameter param in pc.Parameters)
{
vals += $"{pc.TableAsLetter}.{param.Column} {param.Sign} @{col_char} AND ";
cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
}
if (pc.CTC is not null) vals += pc.CTC + " AND ";
pc = pc.Child;
}
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
List<(T, T2, T3)> l = new();
while (reader.Read())
{
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
else if (i == 2)
{
if (typeof(T3).IsEnum) temp[i] = (T3?)Enum.Parse(typeof(T3), val.ToString()!)!;
if (typeof(T3).IsNullableEnum()) temp[i] = (T3?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T3))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
l.Add(new((T)temp[0]!, (T2)temp[1]!, (T3)temp[2]!));
}
con.Close();
return l.ToArray();
}
public void Update() public void Update()
{ {
if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database connection not fully defined"); if (string.IsNullOrEmpty(_betterTable.DatabaseHandler.DB) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.IP) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.Uname) || string.IsNullOrEmpty(_betterTable.DatabaseHandler.PW)) throw new Exception("Database connection not fully defined");
@ -69,7 +358,7 @@ public class CommandHandler
using NpgsqlCommand cmd = new(); using NpgsqlCommand cmd = new();
cmd.Connection = con; cmd.Connection = con;
string values = ""; string values = "";
foreach (Parameter param in Values) foreach (Parameter param in _ValuesOld)
{ {
values += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName}, "; values += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName}, ";
cmd.Parameters.Add(param.PGParameter); cmd.Parameters.Add(param.PGParameter);
@ -77,7 +366,7 @@ public class CommandHandler
values = values.Remove(values.Length - 2, 2); values = values.Remove(values.Length - 2, 2);
string fils = ""; string fils = "";
foreach (Parameter param in Parameters) foreach (Parameter param in _ParametersOld)
{ {
fils += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND"; fils += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND";
cmd.Parameters.Add(param.PGParameter); cmd.Parameters.Add(param.PGParameter);

View File

@ -38,7 +38,7 @@ public sealed class Database
{ {
foreach (ITableColumn Column in Table.Colums) foreach (ITableColumn Column in Table.Colums)
{ {
VersionsTable.WithColumn(new TableColumn<long>($"{Table.Name}_{Column.Name}") VersionsTable.WithColumn(new TableColumn<TableVersions.blankrow, long>($"{Table.Name}_{Column.Name}")
{ {
DefaultValue = Column.ColumnVersion DefaultValue = Column.ColumnVersion
}, string.Empty); }, string.Empty);
@ -122,4 +122,150 @@ public sealed class Database
if (typeof(T).IsNullableEnum()) return (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, temp.ToString()!)!; if (typeof(T).IsNullableEnum()) return (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, temp.ToString()!)!;
return (T?)temp!; return (T?)temp!;
} }
public (T, T2) Read<T, T2>(string command, Order? order = null, params Parameter[] Parameters)
{
if (string.IsNullOrEmpty(DB) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Uname) || string.IsNullOrEmpty(PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
string vals = "";
foreach (Parameter param in Parameters)
{
vals += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND ";
cmd.Parameters.Add(param.PGParameter);
}
if (command.EndsWith(";")) command = command.Remove(command.Length - 1, 1);
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
if (order is not null) command += $" ORDER BY {order.Type} ";
if (order is not null && order.Assending) command += "ASC";
else { if (order is not null) command += "DESC"; }
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
reader.Read();
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
string colname = reader.GetName(i);
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
con.Close();
return ((T)temp[0]!, (T2)temp[1]!);
}
public (T, T2, T3) Read<T, T2, T3>(string command, Order? order = null, params Parameter[] Parameters)
{
if (string.IsNullOrEmpty(DB) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Uname) || string.IsNullOrEmpty(PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
string vals = "";
foreach (Parameter param in Parameters)
{
vals += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND ";
cmd.Parameters.Add(param.PGParameter);
}
if (command.EndsWith(";")) command = command.Remove(command.Length - 1, 1);
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
if (order is not null) command += $" ORDER BY {order.Type} ";
if (order is not null && order.Assending) command += "ASC";
else { if (order is not null) command += "DESC"; }
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
reader.Read();
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
string colname = reader.GetName(i);
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
else if (i == 2)
{
if (typeof(T3).IsEnum) temp[i] = (T3?)Enum.Parse(typeof(T3), val.ToString()!)!;
if (typeof(T3).IsNullableEnum()) temp[i] = (T3?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T3))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
con.Close();
return ((T)temp[0]!, (T2)temp[1]!, (T3)temp[2]!);
}
public (T, T2, T3)[] ReadColumns<T, T2, T3>(string command, Order? order = null, params Parameter[] Parameters)
{
if (string.IsNullOrEmpty(DB) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Uname) || string.IsNullOrEmpty(PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(ConectionString);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
string vals = "";
foreach (Parameter param in Parameters)
{
vals += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND ";
cmd.Parameters.Add(param.PGParameter);
}
if (command.EndsWith(";")) command = command.Remove(command.Length - 1, 1);
if (!string.IsNullOrWhiteSpace(vals)) vals = vals.Remove(vals.Length - 5, 5);
if (!string.IsNullOrWhiteSpace(vals) && command.EndsWith("WHERE")) command += $" {vals}";
if (order is not null) command += $" ORDER BY {order.Type} ";
if (order is not null && order.Assending) command += "ASC";
else { if (order is not null) command += "DESC"; }
command += ";";
cmd.CommandText = command;
NpgsqlDataReader reader = cmd.ExecuteReader();
List<(T, T2, T3)> l = new();
while (reader.Read())
{
object?[] temp = new object[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
object val = reader.GetValue(i);
if (i == 0)
{
if (typeof(T).IsEnum) temp[i] = (T?)Enum.Parse(typeof(T), val.ToString()!)!;
if (typeof(T).IsNullableEnum()) temp[i] = (T?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T))!, val.ToString()!)!;
}
else if (i == 1)
{
if (typeof(T2).IsEnum) temp[i] = (T2?)Enum.Parse(typeof(T2), val.ToString()!)!;
if (typeof(T2).IsNullableEnum()) temp[i] = (T2?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T2))!, val.ToString()!)!;
}
else if (i == 2)
{
if (typeof(T3).IsEnum) temp[i] = (T3?)Enum.Parse(typeof(T3), val.ToString()!)!;
if (typeof(T3).IsNullableEnum()) temp[i] = (T3?)Enum.Parse(Nullable.GetUnderlyingType(typeof(T3))!, val.ToString()!)!;
}
if (temp[i] is null) temp[i] = val;
}
l.Add(new((T)temp[0]!, (T2)temp[1]!, (T3)temp[2]!));
}
con.Close();
return l.ToArray();
}
} }

View File

@ -0,0 +1,14 @@
using ServerDatabase.Utils;
namespace ServerDatabase;
public interface ICommandHandler
{
string? CTC { get; }
char TableAsLetter { get; }
ICommandHandler? CommandHandlerParent { get; }
ICommandHandler? Child { get; }
IReadOnlyList<IBetterParameter> Parameters { get; }
IReadOnlyList<IBetterParameter> Values { get; }
IBetterTable BetterTable { get; }
}

View File

@ -10,7 +10,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageTags>Postgresql;sql</PackageTags> <PackageTags>Postgresql;sql</PackageTags>
<Title>Server Database</Title> <Title>Server Database</Title>
<Version>2.9.9</Version> <Version>3.0.6</Version>
<LangVersion>11</LangVersion> <LangVersion>11</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
@ -22,7 +22,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0" />
<PackageReference Include="ServerDatabase.Utils" Version="1.0.4" /> <PackageReference Include="ServerDatabase.Utils" Version="1.0.8" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -22,9 +22,9 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
DatabaseHandler = DB; DatabaseHandler = DB;
} }
public CommandHandler CreateCommand() public CommandHandler<TRow> CreateCommand()
{ {
return new CommandHandler(this); return new CommandHandler<TRow>(this);
} }
public Table<TRow> WithColumn(ITableColumn Column, string name) public Table<TRow> WithColumn(ITableColumn Column, string name)
@ -146,7 +146,7 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
} }
} }
public Table<TRow> DropColumn<TType>(TableColumn<TType> Column) where TType : notnull public Table<TRow> DropColumn<TType>(TableColumn<TRow, TType> Column) where TType : notnull
{ {
Colums_.Remove(Column); Colums_.Remove(Column);
return this; return this;
@ -313,7 +313,7 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
DatabaseHandler.ExecuteNonQuery($"ALTER TABLE IF EXISTS public.{Name} OWNER to \"{DatabaseHandler.Uname}\";"); DatabaseHandler.ExecuteNonQuery($"ALTER TABLE IF EXISTS public.{Name} OWNER to \"{DatabaseHandler.Uname}\";");
} }
public bool TryRead<T>(TableColumn<T> column, Order order, [NotNullWhen(true)]out T? result, params Parameter[] Parameters) where T : notnull public bool TryRead<T>(TableColumn<TRow, T> column, Order order, [NotNullWhen(true)]out T? result, params Parameter[] Parameters) where T : notnull
{ {
if (string.IsNullOrEmpty(DatabaseHandler.DB) || string.IsNullOrEmpty(DatabaseHandler.IP) || string.IsNullOrEmpty(DatabaseHandler.Uname) || string.IsNullOrEmpty(DatabaseHandler.PW)) throw new Exception("Database conection not fully defined"); if (string.IsNullOrEmpty(DatabaseHandler.DB) || string.IsNullOrEmpty(DatabaseHandler.IP) || string.IsNullOrEmpty(DatabaseHandler.Uname) || string.IsNullOrEmpty(DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(DatabaseHandler.ConectionString); using NpgsqlConnection con = new(DatabaseHandler.ConectionString);
@ -486,24 +486,24 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
return Rows.Any(); return Rows.Any();
} }
public bool TryRead<T>(TableColumn<T> column, [NotNullWhen(true)]out T? result, params Parameter[] Parameters) where T : notnull public bool TryRead<T>(TableColumn<TRow, T> column, [NotNullWhen(true)]out T? result, params Parameter[] Parameters) where T : notnull
{ {
return TryRead(column, null!, out result, Parameters); return TryRead(column, null!, out result, Parameters);
} }
public void Update<Ttype>(TableColumn<Ttype> condiction_column, Ttype condiction_value, params Parameter[] Parameters) where Ttype : notnull public void Update<Ttype>(TableColumn<TRow, Ttype> condiction_column, Ttype condiction_value, params Parameter[] Parameters) where Ttype : notnull
{ {
Update(condiction_column, condiction_value, "=", Parameters); Update(condiction_column, condiction_value, "=", Parameters);
} }
public void Update<Ttype>(TableColumn<Ttype> condiction_column, Ttype condiction_value, string Sign, params Parameter[] Parameters) where Ttype : notnull public void Update<Ttype>(TableColumn<TRow, Ttype> condiction_column, Ttype condiction_value, string Sign, params Parameter[] Parameters) where Ttype : notnull
{ {
CommandHandler command = CreateCommand().WithFilter(condiction_column, condiction_value, Sign); CommandHandler<TRow> tableCommand = CreateCommand().WithFilter(condiction_column, condiction_value, Sign);
foreach (var Parameter in Parameters) foreach (var Parameter in Parameters)
{ {
command.WithValue(Parameter); tableCommand.WithValue(Parameter);
} }
command.Update(); tableCommand.Update();
} }
public TRow ReadRow(params Parameter[] Parameters) => ReadRow(null, Parameters); public TRow ReadRow(params Parameter[] Parameters) => ReadRow(null, Parameters);
@ -553,13 +553,13 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
return Row; return Row;
} }
public TColumn[] ReadColumn<TColumn>(TableColumn<TColumn> column, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, uint.MaxValue, null, Parameters); public TColumn[] ReadColumn<TColumn>(TableColumn<TRow, TColumn> column, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, uint.MaxValue, null, Parameters);
public TColumn[] ReadColumn<TColumn>(TableColumn<TColumn> column, uint NumRows, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, NumRows, null, Parameters); public TColumn[] ReadColumn<TColumn>(TableColumn<TRow, TColumn> column, uint NumRows, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, NumRows, null, Parameters);
public TColumn[] ReadColumn<TColumn>(TableColumn<TColumn> column, Order? Order, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, uint.MaxValue, Order, Parameters); public TColumn[] ReadColumn<TColumn>(TableColumn<TRow, TColumn> column, Order? Order, params Parameter[] Parameters) where TColumn : notnull => ReadColumn(column, uint.MaxValue, Order, Parameters);
public TColumn[] ReadColumn<TColumn>(TableColumn<TColumn> column, uint NumRows, Order? order, params Parameter[] Parameters) where TColumn : notnull public TColumn[] ReadColumn<TColumn>(TableColumn<TRow, TColumn> column, uint NumRows, Order? order, params Parameter[] Parameters) where TColumn : notnull
{ {
if (string.IsNullOrEmpty(DatabaseHandler.DB) || string.IsNullOrEmpty(DatabaseHandler.IP) || string.IsNullOrEmpty(DatabaseHandler.Uname) || string.IsNullOrEmpty(DatabaseHandler.PW)) throw new Exception("Database conection not fully defined"); if (string.IsNullOrEmpty(DatabaseHandler.DB) || string.IsNullOrEmpty(DatabaseHandler.IP) || string.IsNullOrEmpty(DatabaseHandler.Uname) || string.IsNullOrEmpty(DatabaseHandler.PW)) throw new Exception("Database conection not fully defined");
using NpgsqlConnection con = new(DatabaseHandler.ConectionString); using NpgsqlConnection con = new(DatabaseHandler.ConectionString);
@ -650,13 +650,13 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
return Rows.ToArray(); return Rows.ToArray();
} }
public T Read<T>(TableColumn<T> column, params Parameter[] Parameters) where T : notnull public T Read<T>(TableColumn<TRow, T> column, params Parameter[] Parameters) where T : notnull
{ {
if (Parameters is null) throw new ArgumentNullException(nameof(Parameters)); if (Parameters is null) throw new ArgumentNullException(nameof(Parameters));
return DatabaseHandler.Read<T>($"SELECT {column.Name} FROM {Name} WHERE", (Order?)null, Parameters); return DatabaseHandler.Read<T>($"SELECT {column.Name} FROM {Name} WHERE", (Order?)null, Parameters);
} }
public T Read<T>(TableColumn<T> column, Order order, params Parameter[] Parameters) where T : notnull public T Read<T>(TableColumn<TRow, T> column, Order order, params Parameter[] Parameters) where T : notnull
{ {
if (Parameters is null) throw new ArgumentNullException(nameof(Parameters)); if (Parameters is null) throw new ArgumentNullException(nameof(Parameters));
return DatabaseHandler.Read<T>($"SELECT {column.Name} FROM {Name} WHERE", order, Parameters); return DatabaseHandler.Read<T>($"SELECT {column.Name} FROM {Name} WHERE", order, Parameters);

View File

@ -6,7 +6,7 @@ using ServerDatabase.Utils;
namespace ServerDatabase; namespace ServerDatabase;
public class TableColumn<Ttype> : ITableColumn where Ttype : notnull public class TableColumn<TRow, Ttype> : ITableColumn where Ttype : notnull where TRow : class, new()
{ {
public TableColumn(string Name, bool IsPrimaryKey = false) public TableColumn(string Name, bool IsPrimaryKey = false)
{ {
@ -159,23 +159,6 @@ public class TableColumn<Ttype> : ITableColumn where Ttype : notnull
t = t.MakeArrayType(); t = t.MakeArrayType();
types = typemap[t]; types = typemap[t];
} }
/*if (value is null)
{
t = typeof(Ttype);
if (t.IsNullableEnum()) t = Nullable.GetUnderlyingType(t)!.GetEnumUnderlyingType();
types = typemap[t];
return new()
{
PGParameter = new(Name, value)
{
IsNullable = true,
Value = DBNull.Value,
DbType = types.Item1,
NpgsqlDbType = types.Item2
},
Sign = sign
};
}*/
types = typemap[t!]; types = typemap[t!];
if (types is null) if (types is null)
{ {

View File

@ -4,12 +4,13 @@ public class TableVersions : Table<TableVersions.blankrow>
{ {
internal TableVersions(Database db) : base(db) internal TableVersions(Database db) : base(db)
{ {
ID = new TableColumn<short>("id", true); ID = new TableColumn<blankrow, short>("id", true);
WithColumn(ID, new("ID")); WithColumn(ID, new("ID"));
} }
public class blankrow public class blankrow
{ {
} }
public TableColumn<short> ID { get; }
public TableColumn<blankrow, short> ID { get; }
} }