Proper rework attributes, update functional
This commit is contained in:
parent
c8f174d3e8
commit
8e53d012c1
@ -20,86 +20,86 @@ namespace ChaosBot.Attribute
|
|||||||
{
|
{
|
||||||
foreach (Type type in dbEntityAssembly.GetTypes())
|
foreach (Type type in dbEntityAssembly.GetTypes())
|
||||||
{
|
{
|
||||||
if (type.GetCustomAttributes(typeof(DBEntity), true).Length > 0)
|
try
|
||||||
{
|
{
|
||||||
// Get a reference to the attribute
|
if (type.GetCustomAttributes(typeof(DBEntity), true).Length > 0)
|
||||||
DBEntity dbEntity = type.GetCustomAttributes(typeof(DBEntity)).Cast<DBEntity>().First();
|
|
||||||
|
|
||||||
// Generate columns
|
|
||||||
// name, type, constraintName, constraints,
|
|
||||||
List<Tuple<string, string, string, string>> columnList = new List<Tuple<string, string, string, string>>();
|
|
||||||
|
|
||||||
// Get the table as an easy variable
|
|
||||||
string table = dbEntity.Table;
|
|
||||||
|
|
||||||
// Loop through all fields
|
|
||||||
foreach (PropertyInfo prop in type.GetProperties())
|
|
||||||
{
|
{
|
||||||
string columnName = prop.Name;
|
// Get a reference to the attribute
|
||||||
string columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString();
|
DBEntity dbEntity = type.GetCustomAttributes(typeof(DBEntity)).Cast<DBEntity>().First();
|
||||||
StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}");
|
|
||||||
List<string> constraintsList = new List<string>();
|
|
||||||
|
|
||||||
// Go through every attribute
|
// Generate columns
|
||||||
foreach (object fieldAttribute in prop.GetCustomAttributes(true))
|
// name, type, constraintName, constraints,
|
||||||
|
List<Tuple<string, string, string, string>> columnList = new List<Tuple<string, string, string, string>>();
|
||||||
|
|
||||||
|
// Get the table as an easy variable
|
||||||
|
string table = dbEntity.Table;
|
||||||
|
|
||||||
|
// Loop through all fields
|
||||||
|
foreach (PropertyInfo prop in type.GetProperties())
|
||||||
{
|
{
|
||||||
if (fieldAttribute is DBFieldAttribute dBFieldAttribute)
|
string columnName = prop.Name;
|
||||||
|
string columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString();
|
||||||
|
StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}");
|
||||||
|
List<string> constraintsList = new List<string>();
|
||||||
|
|
||||||
|
// Go through every attribute
|
||||||
|
foreach (object fieldAttribute in prop.GetCustomAttributes(true))
|
||||||
{
|
{
|
||||||
string constraintSuffix =
|
if (fieldAttribute is DBFieldAttribute dBFieldAttribute)
|
||||||
DBEntity.ConstrainNames.GetValueOrDefault(dBFieldAttribute.GetType(), null);
|
|
||||||
if (constraintSuffix != null)
|
|
||||||
{
|
{
|
||||||
constraintNameBuilder.Append($"_{constraintSuffix}");
|
string constraintSuffix =
|
||||||
constraintsList.Add($"{dBFieldAttribute.GetSQLiteQuery()}");
|
DBEntity.ConstrainNames.GetValueOrDefault(dBFieldAttribute.GetType(), null);
|
||||||
|
if (constraintSuffix != null)
|
||||||
|
{
|
||||||
|
constraintNameBuilder.Append($"_{constraintSuffix}");
|
||||||
|
constraintsList.Add($"{dBFieldAttribute.GetSQLiteQuery()}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string constraintName = constraintNameBuilder.ToString();
|
||||||
|
if (constraintsList.Count > 0)
|
||||||
|
{
|
||||||
|
constraintsList.Insert(0, "CONSTRAINT");
|
||||||
|
constraintsList.Insert(1, constraintName);
|
||||||
|
}
|
||||||
|
string constraints = String.Join(" ", constraintsList);
|
||||||
|
columnList.Add(new Tuple<string, string, string, string>(columnName, columnType, constraintName, constraints));
|
||||||
}
|
}
|
||||||
|
|
||||||
string constraintName = constraintNameBuilder.ToString();
|
// Check table exists
|
||||||
if (constraintsList.Count > 0)
|
bool tableExists =
|
||||||
{
|
Controller.RawQuery($"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'")
|
||||||
constraintsList.Insert(0, "CONSTRAINT");
|
.Rows.Count > 0;
|
||||||
constraintsList.Insert(1, constraintName);
|
|
||||||
}
|
|
||||||
string constraints = String.Join(" ", constraintsList);
|
|
||||||
columnList.Add(new Tuple<string, string, string, string>(columnName, columnType, constraintName, constraints));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check table exists
|
|
||||||
bool tableExists =
|
|
||||||
Controller.RawQuery($"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'")
|
|
||||||
.Rows.Count > 0;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!tableExists)
|
if (!tableExists)
|
||||||
{
|
{
|
||||||
string columnDefs = String.Join(", ", columnList.Select(c => $"{c.Item1} {c.Item2} {c.Item4}"));
|
string columnDefs = String.Join(", ", columnList.Select(c => $"{c.Item1} {c.Item2} {c.Item4}"));
|
||||||
string query = $"CREATE TABLE {table} ({columnDefs})";
|
string query = $"CREATE TABLE {table} ({columnDefs})";
|
||||||
Controller.RawQuery(query);
|
Controller.RawQuery(query, false);
|
||||||
}
|
|
||||||
// // TODO: Generate this in one go instead of many separate commands
|
|
||||||
// Controller.RawQuery($"CREATE TABLE IF NOT EXISTS {table} (id INTEGER NOT NULL CONSTRAINT {table}_pk PRIMARY KEY AUTOINCREMENT)");
|
|
||||||
// Controller.RawQuery($"CREATE UNIQUE INDEX {table}_id_uindex ON {table} (id)", false, true);
|
|
||||||
// string query = $"ALTER TABLE {table} ADD COLUMN {fieldname} {fieldType} {fieldModifiers}";
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (ex is SqliteException sqliteException)
|
|
||||||
{
|
|
||||||
if (!sqliteException.Message.Contains("duplicate column name"))
|
|
||||||
{
|
|
||||||
_logger.Fatal(
|
|
||||||
$"AssemblyController.Register: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.Error(
|
foreach (Tuple<string,string,string,string> column in columnList)
|
||||||
$"AssemblyController.Register: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string query =
|
||||||
|
$"ALTER TABLE {table} ADD COLUMN {column.Item1} {column.Item2} {column.Item4}";
|
||||||
|
Controller.RawQuery(query, false, true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error($"AssemblyController.Register: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user