Table Updates.
Checking a table is better now.
This commit is contained in:
parent
de8884aaf4
commit
c36b20ac13
@ -10,7 +10,7 @@
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<PackageTags>Postgresql;sql</PackageTags>
|
||||
<Title>Server Database</Title>
|
||||
<Version>2.8.1</Version>
|
||||
<Version>2.8.8</Version>
|
||||
<LangVersion>10</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
@ -36,12 +36,11 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public IReadOnlyList<ITableColumn> GetMissingColumns(out IReadOnlyList<ITableColumn> UpdatedColumns)
|
||||
public IReadOnlyList<ITableColumn> GetMissingColumns(out IReadOnlyList<ITableColumn> UpdatedColumns, out string[] ExtraColumns)
|
||||
{
|
||||
List<ITableColumn> coll = new(Colums_);
|
||||
List<ITableColumn> col = new(Colums_);
|
||||
List<string> co = new();
|
||||
foreach (DataRow row in DatabaseHandler.CreateConnection().GetSchema("Columns", new[] { DatabaseHandler.DB, null, Name }).Rows)
|
||||
{
|
||||
string name = row.Field<string>("COLUMN_NAME")!,
|
||||
@ -52,18 +51,29 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
col.Remove(t.First());
|
||||
if (t.First()!.GetDatabaseTypeStr() == ty) coll.Remove(t.First());
|
||||
}
|
||||
else
|
||||
{
|
||||
co.Add(name);
|
||||
}
|
||||
}
|
||||
|
||||
UpdatedColumns = coll;
|
||||
ExtraColumns = co.ToArray();
|
||||
return col.AsReadOnly();
|
||||
}
|
||||
|
||||
public void UpdateTable()
|
||||
{
|
||||
Console.WriteLine("Updating Table: {0}", Name);
|
||||
IReadOnlyList<ITableColumn> m = GetMissingColumns(out IReadOnlyList<ITableColumn> n);
|
||||
Console.WriteLine("Verifying Table: {0}", Name);
|
||||
bool i = true;
|
||||
IReadOnlyList<ITableColumn> m = GetMissingColumns(out IReadOnlyList<ITableColumn> n, out string[] o);
|
||||
if (m.Any())
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
i = false;
|
||||
Console.WriteLine("Fixing Table: {0}", Name);
|
||||
}
|
||||
if (DatabaseHandler.Read<long>(
|
||||
$"SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema LIKE 'public' AND table_type LIKE 'BASE TABLE' AND table_name = '{Name}';") ==
|
||||
0)
|
||||
@ -93,6 +103,11 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
|
||||
if (n.Any())
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
i = false;
|
||||
Console.WriteLine("Fixing Table: {0}", Name);
|
||||
}
|
||||
foreach (ITableColumn col in n)
|
||||
{
|
||||
string sql = $"ALTER TABLE public.{Name} ALTER COLUMN {col.Name} TYPE {col.GetDatabaseTypeStr()};";
|
||||
@ -112,6 +127,19 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (o.Any())
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
i = false;
|
||||
Console.WriteLine("Fixing Table: {0}", Name);
|
||||
}
|
||||
foreach (string c in o)
|
||||
{
|
||||
DatabaseHandler.ExecuteNonQuery($"ALTER TABLE IF EXISTS {Name} DROP COLUMN IF EXISTS {c};");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Table<TRow> DropColumn<TType>(TableColumn<TType> Column) where TType : notnull
|
||||
@ -172,6 +200,19 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
cmd.ExecuteNonQuery();
|
||||
con.Close();
|
||||
}
|
||||
|
||||
public void DeleteRows()
|
||||
{
|
||||
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);
|
||||
con.Open();
|
||||
using NpgsqlCommand cmd = new();
|
||||
cmd.Connection = con;
|
||||
cmd.CommandText = $"DELETE FROM {Name};";
|
||||
cmd.Prepare();
|
||||
cmd.ExecuteNonQuery();
|
||||
con.Close();
|
||||
}
|
||||
|
||||
public void Create()
|
||||
{
|
||||
@ -195,7 +236,8 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
cmd.Prepare();
|
||||
cmd.ExecuteNonQuery();
|
||||
con.Close();
|
||||
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)][MaybeNullWhen(false)]out T result, params Parameter[] Parameters) where T : notnull
|
||||
@ -319,6 +361,58 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
return r;
|
||||
}
|
||||
|
||||
public bool TryReadRows(out TRow[] rows, params Parameter[] Parameters) => TryReadRows(null!, out rows, Parameters);
|
||||
|
||||
public bool TryReadRows(Order order, out TRow[] rows, params Parameter[] Parameters)
|
||||
{
|
||||
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);
|
||||
string command = $"SELECT * FROM {Name} WHERE";
|
||||
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<TRow> Rows = new();
|
||||
while (reader.Read())
|
||||
{
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
TRow Row = new();
|
||||
string colname = reader.GetName(i);
|
||||
object? val = reader.GetValue(i);
|
||||
if (colnamesraw.ContainsKey(colname))
|
||||
{
|
||||
PropertyInfo? pi = Row.GetType()
|
||||
.GetProperty(colnamesraw[colname]);
|
||||
if (pi is null)
|
||||
{
|
||||
Console.WriteLine("Could not find row {0}", colnamesraw[colname]);
|
||||
continue;
|
||||
}
|
||||
pi.SetValue(Row, val, null);
|
||||
}
|
||||
Rows.Add(Row);
|
||||
}
|
||||
}
|
||||
|
||||
rows = Rows.ToArray();
|
||||
return Rows.Any();
|
||||
}
|
||||
|
||||
public bool TryRead<T>(TableColumn<T> column, [NotNullWhen(true)][MaybeNullWhen(false)]out T result, params Parameter[] Parameters) where T : notnull
|
||||
{
|
||||
return TryRead(column, null!, out result, Parameters);
|
||||
@ -426,9 +520,9 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
for (int j = 0; j < NumRows; j++)
|
||||
{
|
||||
if (!reader.Read()) break;
|
||||
TRow Row = new();
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
TRow Row = new();
|
||||
string colname = reader.GetName(i);
|
||||
object? val = reader.GetValue(i);
|
||||
if (colnamesraw.ContainsKey(colname))
|
||||
@ -442,8 +536,8 @@ public class Table<TRow> : ITable where TRow : class, new()
|
||||
}
|
||||
pi.SetValue(Row, val, null);
|
||||
}
|
||||
Rows.Add(Row);
|
||||
}
|
||||
Rows.Add(Row);
|
||||
}
|
||||
|
||||
return Rows.ToArray();
|
||||
|
Loading…
Reference in New Issue
Block a user