Bladeren bron

Added non generic table utility functions

frogsoftware 8 maanden geleden
bovenliggende
commit
b0ea260476
3 gewijzigde bestanden met toevoegingen van 24 en 13 verwijderingen
  1. 1 1
      InABox.Database/DbFactory.cs
  2. 3 0
      InABox.Database/IProvider.cs
  3. 20 12
      inabox.database.sqlite/SQLiteProvider.cs

+ 1 - 1
InABox.Database/DbFactory.cs

@@ -96,7 +96,7 @@ public static class DbFactory
             throw new Exception("Database migration failed. Aborting startup");
         }
 
-        //DataUpdater.DoSpecificMigration(new VersionNumber(8, 22), new VersionNumber(8, 23));
+        //DataUpdater.DoSpecificMigration(new VersionNumber(8, 23), new VersionNumber(8, 24));
 
         //Load up your custom properties here!
         // Can't use clients (b/c we're inside the database layer already

+ 3 - 0
InABox.Database/IProvider.cs

@@ -23,8 +23,11 @@ public interface IProvider
     IEnumerable<object[]> List<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, CoreRange? range = null) where T : Entity, new();
     
     bool TableExists<T>();
+    bool TableExists(Type t);
     CoreTable? GetTable<T>();
+    CoreTable? GetTable(Type t);
     void DropTable<T>();
+    void DropTable(Type t);
 
     IEnumerable<object[]> List(string sql);
     CoreTable Query(string sql);

+ 20 - 12
inabox.database.sqlite/SQLiteProvider.cs

@@ -1423,14 +1423,16 @@ public class SQLiteProvider : IProvider
         return _result;
      }
 
-    public bool TableExists<T>()
+    public bool TableExists<T>() => TableExists(typeof(T));
+
+    public bool TableExists(Type t)
     {
         bool _result = false;
         using (var access = GetReadAccess())
         {
             using (var _check = access.CreateCommand())
             {
-                _check.CommandText = $"select name from sqlite_master where type='table' and name='{typeof(T).Name.Split('.').Last()}';";
+                _check.CommandText = $"select name from sqlite_master where type='table' and name='{t.Name.Split('.').Last()}';";
                 using (var _reader = _check.ExecuteReader())
                 {
                     _result = _reader.HasRows;
@@ -1440,15 +1442,18 @@ public class SQLiteProvider : IProvider
         }
         return _result;
     }
-    
-    public CoreTable? GetTable<T>()
+
+    public CoreTable? GetTable<T>() => GetTable(typeof(T));
+
+    public CoreTable? GetTable(Type t)
     {
-        var _tablename = typeof(T).Name.Split('.').Last();
-        if (!TableExists<T>())
+        if (!TableExists(t))
             return null;
-        
+
+        var _tablename = t.Name.Split('.').Last();
+
         CoreTable? _result = null;
-        var props = CoreUtils.PropertyInfoList(typeof(T), p => true, true);
+        var props = CoreUtils.PropertyInfoList(t, p => true, true);
         try
         {
             using (var access = GetReadAccess())
@@ -1487,15 +1492,18 @@ public class SQLiteProvider : IProvider
             Logger.Send(LogType.Error,"",$"Exception in GetTable: {_tablename}\nMessage: {e.Message}\nStackTrace: {e.StackTrace}");
         }
         return _result;
+
     }
-    
-    public void DropTable<T>()
+
+    public void DropTable<T>() => DropTable(typeof(T));
+
+    public void DropTable(Type t)
     {
-        if (typeof(T).IsSubclassOf(typeof(Entity)) || !TableExists<T>())
+        if (t.IsSubclassOf(typeof(Entity)) || !TableExists(t))
             return;
         using (var _access = GetWriteAccess())
         {
-            ExecuteSQL(_access, $"DROP TABLE {typeof(T).EntityName().Split('.').Last()}");
+            ExecuteSQL(_access, $"DROP TABLE {t.EntityName().Split('.').Last()}");
         }
     }