|
@@ -743,16 +743,14 @@ namespace InABox.Database.SQLite
|
|
|
|
|
|
private void CreateTable(SQLiteWriteAccessor access, Type type, bool includeconstraints, CustomProperty[] customproperties)
|
|
|
{
|
|
|
+ var tablename = type.EntityName().Split('.').Last();
|
|
|
var ddl = new List<string>();
|
|
|
var view = type.GetCustomAttribute<AutoEntity>();
|
|
|
if (view != null)
|
|
|
{
|
|
|
using (var command = access.CreateCommand())
|
|
|
{
|
|
|
-
|
|
|
- //command.CommandText = String.Format("select name from sqlite_master where type='trigger' and tbl_name='{0}' and sql is not null;", table);
|
|
|
- command.CommandText = "select name from sqlite_master where type='table' and name='" + type.EntityName().Split('.').Last() +
|
|
|
- "' and sql is not null;";
|
|
|
+ command.CommandText = $"select name from sqlite_master where type='view' and name='{tablename}';";
|
|
|
using (var reader = command.ExecuteReader())
|
|
|
{
|
|
|
if (reader.HasRows)
|
|
@@ -931,6 +929,16 @@ namespace InABox.Database.SQLite
|
|
|
String drop = "";
|
|
|
using (var command = access.CreateCommand())
|
|
|
{
|
|
|
+
|
|
|
+ command.CommandText =
|
|
|
+ $"select name from sqlite_master where type='trigger' and tbl_name='{table}' and sql is not null;";
|
|
|
+ using (var reader = command.ExecuteReader())
|
|
|
+ {
|
|
|
+ if (reader.HasRows)
|
|
|
+ while (reader.Read())
|
|
|
+ ExecuteSQL(access,string.Format("DROP TRIGGER {0}", reader.GetString(0)));
|
|
|
+ }
|
|
|
+
|
|
|
command.CommandText = "select type from sqlite_master where name='" + table + "' and sql is not null;";
|
|
|
using (var reader = command.ExecuteReader())
|
|
|
{
|
|
@@ -958,8 +966,29 @@ namespace InABox.Database.SQLite
|
|
|
OnLog?.Invoke(LogType.Information, "Rebuilding Table: " + table);
|
|
|
try
|
|
|
{
|
|
|
+
|
|
|
ExecuteSQL(access, "PRAGMA foreign_keys = off;");
|
|
|
-
|
|
|
+
|
|
|
+ // using (var command = access.CreateCommand())
|
|
|
+ // {
|
|
|
+ //
|
|
|
+ // command.CommandText =
|
|
|
+ // $"select name from sqlite_master where type='trigger' and tbl_name='{table}' and sql is not null;";
|
|
|
+ // using (var reader = command.ExecuteReader())
|
|
|
+ // {
|
|
|
+ // if (reader.HasRows)
|
|
|
+ // while (reader.Read())
|
|
|
+ // ExecuteSQL(access,string.Format("DROP TRIGGER {0}", reader.GetString(0)));
|
|
|
+ // }
|
|
|
+ // command.CommandText = $"select name from sqlite_master where type='view' and name='{table}';";
|
|
|
+ // using (var reader = command.ExecuteReader())
|
|
|
+ // {
|
|
|
+ // if (reader.HasRows)
|
|
|
+ // while (reader.Read())
|
|
|
+ // ExecuteSQL(access,string.Format("DROP VIEW {0}", reader.GetString(0)));
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
using (var transaction = access.Connection.BeginTransaction())
|
|
|
{
|
|
|
var drops = new List<string>();
|
|
@@ -999,7 +1028,20 @@ namespace InABox.Database.SQLite
|
|
|
foreach (var drop in drops)
|
|
|
ExecuteSQL(access, drop);
|
|
|
|
|
|
- ExecuteSQL(access, string.Format("ALTER TABLE {0} RENAME TO _{0}_old;", table));
|
|
|
+ bool existingtable = false;
|
|
|
+ using (var command = access.CreateCommand())
|
|
|
+ {
|
|
|
+ command.CommandText =
|
|
|
+ $"select name from sqlite_master where type='table' and tbl_name='{table}' and sql is not null;";
|
|
|
+ using (var reader = command.ExecuteReader())
|
|
|
+ {
|
|
|
+ if (reader.HasRows)
|
|
|
+ existingtable = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (existingtable)
|
|
|
+ ExecuteSQL(access, string.Format("ALTER TABLE {0} RENAME TO _{0}_old;", table));
|
|
|
|
|
|
CreateTable(access, type, true, customproperties);
|
|
|
|
|
@@ -1008,9 +1050,13 @@ namespace InABox.Database.SQLite
|
|
|
if (table_fields.ContainsKey(field))
|
|
|
fields.Add("[" + field + "]");
|
|
|
|
|
|
- ExecuteSQL(access, string.Format("INSERT INTO {0} ({1}) SELECT {1} FROM _{0}_old;", table, string.Join(", ", fields)));
|
|
|
-
|
|
|
- ExecuteSQL(access, string.Format("DROP TABLE _{0}_old;", table));
|
|
|
+ if (existingtable)
|
|
|
+ {
|
|
|
+ ExecuteSQL(access,
|
|
|
+ string.Format("INSERT INTO {0} ({1}) SELECT {1} FROM _{0}_old;", table,
|
|
|
+ string.Join(", ", fields)));
|
|
|
+ ExecuteSQL(access, string.Format("DROP TABLE _{0}_old;", table));
|
|
|
+ }
|
|
|
|
|
|
transaction.Commit();
|
|
|
}
|