Преглед на файлове

Removed Duplicates from TypeList based on EntityName() (was causing GPSTrackerStore to initialize twice)
Added code to drop views if a table of the same name needs to be created

frogsoftware преди 1 година
родител
ревизия
585a41fd2e
променени са 2 файла, в които са добавени 60 реда и са изтрити 11 реда
  1. 5 2
      InABox.Core/CoreUtils.cs
  2. 55 9
      inabox.database.sqlite/SQLiteProvider.cs

+ 5 - 2
InABox.Core/CoreUtils.cs

@@ -471,8 +471,11 @@ namespace InABox.Core
                                 try
                                 {
                                     var asType = type.AsType();
-                                    if (Predicate(asType) && !result.Contains(asType))
-                                        result.Add(asType);
+                                    if (Predicate(asType))
+                                    {
+                                        if (!result.Any(x => String.Equals(x.EntityName(), asType.EntityName())))
+                                            result.Add(asType);
+                                    }
                                 }
                                 catch (Exception el)
                                 {

+ 55 - 9
inabox.database.sqlite/SQLiteProvider.cs

@@ -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();
                     }