Fixed Update

This commit is contained in:
JacobTech 2024-03-31 23:53:51 -04:00
parent aa31f4589e
commit 4bb683c98f
3 changed files with 43 additions and 29 deletions

View File

@ -352,27 +352,55 @@ public class CommandHandler<TClass> : ICommandHandler where TClass : class, new(
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"); string command = $"UPDATE {BetterTable.Name} AS {TableAsLetter} SET ";
ICommandHandler? pc = this;
ICommandHandler? c = this.CommandHandlerParent;
using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString); using NpgsqlConnection con = new(_betterTable.DatabaseHandler.ConectionString);
con.Open(); con.Open();
using NpgsqlCommand cmd = new(); using NpgsqlCommand cmd = new();
cmd.Connection = con; cmd.Connection = con;
string values = ""; string vals = "";
foreach (Parameter param in _ValuesOld) char col_char = 'A';
foreach (IBetterParameter param in Values)
{ {
values += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName}, "; vals += $"{param.Column} = @{col_char}, ";
cmd.Parameters.Add(param.PGParameter); cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
} }
values = values.Remove(values.Length - 2, 2);
string fils = ""; command += vals.Remove(vals.Length - 2, 2) + " ";
foreach (Parameter param in _ParametersOld) string from = "";
if (c is not null) from += "FROM ";
while (c is not null)
{ {
fils += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND"; from += $"{c.BetterTable.Name} AS {c.TableAsLetter}, ";
cmd.Parameters.Add(param.PGParameter); pc = c;
c = c.CommandHandlerParent;
} }
fils = fils.Remove(fils.Length - 4, 4);
cmd.CommandText = $"UPDATE {_betterTable.Name} SET {values} WHERE {fils};"; if (from.Length > 0) command += from.Remove(from.Length - 2, 2);
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 - 4, 4);
command += "WHERE " + vals;
}
command = command.Remove(command.Length - 1, 1) + ";";
cmd.CommandText = command;
cmd.Prepare(); cmd.Prepare();
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
con.Close(); con.Close();

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>3.0.6</Version> <Version>3.0.8-alpha02</Version>
<LangVersion>11</LangVersion> <LangVersion>11</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>

View File

@ -491,20 +491,6 @@ public class Table<TRow> : IBetterTable where TRow : class, new()
return TryRead(column, null!, out result, Parameters); return TryRead(column, null!, out result, Parameters);
} }
public void Update<Ttype>(TableColumn<TRow, Ttype> condiction_column, Ttype condiction_value, params Parameter[] Parameters) where Ttype : notnull
{
Update(condiction_column, condiction_value, "=", Parameters);
}
public void Update<Ttype>(TableColumn<TRow, Ttype> condiction_column, Ttype condiction_value, string Sign, params Parameter[] Parameters) where Ttype : notnull
{
CommandHandler<TRow> tableCommand = CreateCommand().WithFilter(condiction_column, condiction_value, Sign);
foreach (var Parameter in Parameters)
{
tableCommand.WithValue(Parameter);
}
tableCommand.Update();
}
public TRow ReadRow(params Parameter[] Parameters) => ReadRow(null, Parameters); public TRow ReadRow(params Parameter[] Parameters) => ReadRow(null, Parameters);