Table Updates.
Checking a table is better now.
This commit is contained in:
parent
de8884aaf4
commit
c36b20ac13
@ -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.8.1</Version>
|
<Version>2.8.8</Version>
|
||||||
<LangVersion>10</LangVersion>
|
<LangVersion>10</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<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, out string[] ExtraColumns)
|
||||||
|
|
||||||
public IReadOnlyList<ITableColumn> GetMissingColumns(out IReadOnlyList<ITableColumn> UpdatedColumns)
|
|
||||||
{
|
{
|
||||||
List<ITableColumn> coll = new(Colums_);
|
List<ITableColumn> coll = new(Colums_);
|
||||||
List<ITableColumn> col = 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)
|
foreach (DataRow row in DatabaseHandler.CreateConnection().GetSchema("Columns", new[] { DatabaseHandler.DB, null, Name }).Rows)
|
||||||
{
|
{
|
||||||
string name = row.Field<string>("COLUMN_NAME")!,
|
string name = row.Field<string>("COLUMN_NAME")!,
|
||||||
@ -52,18 +51,29 @@ public class Table<TRow> : ITable where TRow : class, new()
|
|||||||
col.Remove(t.First());
|
col.Remove(t.First());
|
||||||
if (t.First()!.GetDatabaseTypeStr() == ty) coll.Remove(t.First());
|
if (t.First()!.GetDatabaseTypeStr() == ty) coll.Remove(t.First());
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
co.Add(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdatedColumns = coll;
|
UpdatedColumns = coll;
|
||||||
|
ExtraColumns = co.ToArray();
|
||||||
return col.AsReadOnly();
|
return col.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateTable()
|
public void UpdateTable()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Updating Table: {0}", Name);
|
Console.WriteLine("Verifying Table: {0}", Name);
|
||||||
IReadOnlyList<ITableColumn> m = GetMissingColumns(out IReadOnlyList<ITableColumn> n);
|
bool i = true;
|
||||||
|
IReadOnlyList<ITableColumn> m = GetMissingColumns(out IReadOnlyList<ITableColumn> n, out string[] o);
|
||||||
if (m.Any())
|
if (m.Any())
|
||||||
{
|
{
|
||||||
|
if (i)
|
||||||
|
{
|
||||||
|
i = false;
|
||||||
|
Console.WriteLine("Fixing Table: {0}", Name);
|
||||||
|
}
|
||||||
if (DatabaseHandler.Read<long>(
|
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}';") ==
|
$"SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema LIKE 'public' AND table_type LIKE 'BASE TABLE' AND table_name = '{Name}';") ==
|
||||||
0)
|
0)
|
||||||
@ -93,6 +103,11 @@ public class Table<TRow> : ITable where TRow : class, new()
|
|||||||
|
|
||||||
if (n.Any())
|
if (n.Any())
|
||||||
{
|
{
|
||||||
|
if (i)
|
||||||
|
{
|
||||||
|
i = false;
|
||||||
|
Console.WriteLine("Fixing Table: {0}", Name);
|
||||||
|
}
|
||||||
foreach (ITableColumn col in n)
|
foreach (ITableColumn col in n)
|
||||||
{
|
{
|
||||||
string sql = $"ALTER TABLE public.{Name} ALTER COLUMN {col.Name} TYPE {col.GetDatabaseTypeStr()};";
|
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
|
public Table<TRow> DropColumn<TType>(TableColumn<TType> Column) where TType : notnull
|
||||||
@ -173,6 +201,19 @@ public class Table<TRow> : ITable where TRow : class, new()
|
|||||||
con.Close();
|
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()
|
public void Create()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Making Table: {0}", Name);
|
Console.WriteLine("Making Table: {0}", Name);
|
||||||
@ -195,7 +236,8 @@ public class Table<TRow> : ITable where TRow : class, new()
|
|||||||
cmd.Prepare();
|
cmd.Prepare();
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
con.Close();
|
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
|
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;
|
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
|
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);
|
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++)
|
for (int j = 0; j < NumRows; j++)
|
||||||
{
|
{
|
||||||
if (!reader.Read()) break;
|
if (!reader.Read()) break;
|
||||||
|
TRow Row = new();
|
||||||
for (int i = 0; i < reader.FieldCount; i++)
|
for (int i = 0; i < reader.FieldCount; i++)
|
||||||
{
|
{
|
||||||
TRow Row = new();
|
|
||||||
string colname = reader.GetName(i);
|
string colname = reader.GetName(i);
|
||||||
object? val = reader.GetValue(i);
|
object? val = reader.GetValue(i);
|
||||||
if (colnamesraw.ContainsKey(colname))
|
if (colnamesraw.ContainsKey(colname))
|
||||||
@ -442,8 +536,8 @@ public class Table<TRow> : ITable where TRow : class, new()
|
|||||||
}
|
}
|
||||||
pi.SetValue(Row, val, null);
|
pi.SetValue(Row, val, null);
|
||||||
}
|
}
|
||||||
Rows.Add(Row);
|
|
||||||
}
|
}
|
||||||
|
Rows.Add(Row);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Rows.ToArray();
|
return Rows.ToArray();
|
||||||
|
Loading…
Reference in New Issue
Block a user