From 4bb683c98f9c477b1e66431bc0cd837ff45a2a77 Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sun, 31 Mar 2024 23:53:51 -0400 Subject: [PATCH] Fixed Update --- Database/CommandHandler.cs | 54 ++++++++++++++++++++++++++-------- Database/ServerDatabase.csproj | 2 +- Database/Table.cs | 16 +--------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/Database/CommandHandler.cs b/Database/CommandHandler.cs index c77e58f..ae27cee 100644 --- a/Database/CommandHandler.cs +++ b/Database/CommandHandler.cs @@ -352,27 +352,55 @@ public class CommandHandler : 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(); diff --git a/Database/ServerDatabase.csproj b/Database/ServerDatabase.csproj index 917585b..90c355d 100755 --- a/Database/ServerDatabase.csproj +++ b/Database/ServerDatabase.csproj @@ -10,7 +10,7 @@ snupkg Postgresql;sql Server Database - 3.0.6 + 3.0.8-alpha02 11 enable net8.0 diff --git a/Database/Table.cs b/Database/Table.cs index 8a31b89..81d7973 100644 --- a/Database/Table.cs +++ b/Database/Table.cs @@ -490,21 +490,7 @@ public class Table : IBetterTable where TRow : class, new() { return TryRead(column, null!, out result, Parameters); } - - public void Update(TableColumn condiction_column, Ttype condiction_value, params Parameter[] Parameters) where Ttype : notnull - { - Update(condiction_column, condiction_value, "=", Parameters); - } - - public void Update(TableColumn condiction_column, Ttype condiction_value, string Sign, params Parameter[] Parameters) where Ttype : notnull - { - CommandHandler 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);