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()
{
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);
con.Open();
using NpgsqlCommand cmd = new();
cmd.Connection = con;
string values = "";
foreach (Parameter param in _ValuesOld)
string vals = "";
char col_char = 'A';
foreach (IBetterParameter param in Values)
{
values += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName}, ";
cmd.Parameters.Add(param.PGParameter);
vals += $"{param.Column} = @{col_char}, ";
cmd.Parameters.Add(param.CreateParameter(col_char.ToString()));
col_char++;
}
values = values.Remove(values.Length - 2, 2);
string fils = "";
foreach (Parameter param in _ParametersOld)
command += vals.Remove(vals.Length - 2, 2) + " ";
string from = "";
if (c is not null) from += "FROM ";
while (c is not null)
{
fils += $"{param.PGParameter.ParameterName} {param.Sign} @{param.PGParameter.ParameterName} AND";
cmd.Parameters.Add(param.PGParameter);
from += $"{c.BetterTable.Name} AS {c.TableAsLetter}, ";
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.ExecuteNonQuery();
con.Close();

View File

@ -10,7 +10,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageTags>Postgresql;sql</PackageTags>
<Title>Server Database</Title>
<Version>3.0.6</Version>
<Version>3.0.8-alpha02</Version>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
<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);
}
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);