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

Merge remote-tracking branch 'origin/kenric' into frank

frogsoftware преди 1 година
родител
ревизия
cd0e817408

+ 6 - 1
InABox.Database/DbFactory.cs

@@ -12,6 +12,11 @@ public class DatabaseMetadata : BaseObject, IGlobalConfigurationSettings
     public Guid DatabaseID { get; set; } = Guid.NewGuid();
 }
 
+public class DbLockedException : Exception
+{
+    public DbLockedException(): base("Database is read-only due to PRS license expiry.") { }
+}
+
 public static class DbFactory
 {
     public static Dictionary<string, ScriptDocument> LoadedScripts = new();
@@ -102,7 +107,7 @@ public static class DbFactory
 
 
         //Load up your custom properties here!
-        // Can't use clients (b/c were inside the database layer already
+        // Can't use clients (b/c we're inside the database layer already
         // but we can simply access the store directly :-)
         //CustomProperty[] props = FindStore<CustomProperty>("", "", "", "").Load(new Filter<CustomProperty>(x=>x.ID).IsNotEqualTo(Guid.Empty),null);
         var props = Provider.Query<CustomProperty>().Rows.Select(x => x.ToObject<CustomProperty>()).ToArray();

+ 1 - 0
InABox.Database/IProvider.cs

@@ -42,6 +42,7 @@ namespace InABox.Database
         void Save<T>(T entity) where T : Entity;
         void Save<T>(IEnumerable<T> entities) where T : Entity;
 
+        void Save(Type type, Entity entity);
         void Save(Type type, IEnumerable<Entity> entities);
 
         void Delete<T>(T entity, string userID) where T : Entity, new();

+ 35 - 22
inabox.database.sqlite/SQLiteProvider.cs

@@ -2380,7 +2380,7 @@ namespace InABox.Database.SQLite
 
         #region CRUD Operations
         
-        public object[] GetValues(IDataReader reader, int count)
+        public static object[] GetValues(IDataReader reader, int count)
         {
             var result = new object[count];
             reader.GetValues(result);
@@ -2794,51 +2794,52 @@ namespace InABox.Database.SQLite
                 throw;
             }
         }
-        private void OnSave<T>(IEnumerable<T> entities) where T : Entity
-            => OnSaveNonGeneric(typeof(T), entities);
 
-        public void Save(Type type, IEnumerable<Entity> entities)
-            => OnSaveNonGeneric(type, entities);
+        private void OnSave<T>(T entity) where T : Entity
+            => OnSaveNonGeneric(typeof(T), entity);
 
-        public static bool CanSave<T>()
+        public static bool CanSave(Type T)
         {
             if (DbFactory.IsReadOnly)
             {
-                if (typeof(T).IsAssignableTo(typeof(License)) || typeof(T).IsAssignableTo(typeof(UserTracking)))
+                if (T.IsAssignableTo(typeof(License)) || T.IsAssignableTo(typeof(UserTracking)))
                     return true;
                 DbFactory.LogReadOnly();
-                return false;
+                throw new DbLockedException();
             }
             return true;
         }
+        public static bool CanSave<T>() => CanSave(typeof(T));
 
-
-        
-        public void Save<T>(IEnumerable<T> entities) where T : Entity
+        public void Save(Type type, Entity entity)
         {
-            if (!CanSave<T>())
+            if (!CanSave(type))
+            {
                 return;
-            OnSave(entities);
+            }
+            OnSaveNonGeneric(type, entity);
         }
-
-        private void OnSave<T>(T entity) where T : Entity
-            => OnSaveNonGeneric(typeof(T), entity);
-
-        public void Save<T>(T entity) where T : Entity
+        public void Save(Type type, IEnumerable<Entity> entities)
         {
-            if (!CanSave<T>())
+            if (!CanSave(type))
             {
                 return;
             }
-            OnSave(entity);
+            OnSaveNonGeneric(type, entities);
         }
 
+        public void Save<T>(IEnumerable<T> entities) where T : Entity => Save(typeof(T), entities);
+
+        public void Save<T>(T entity) where T : Entity => Save(typeof(T), entity);
+
         #endregion
 
         #region Delete
 
         public void Purge<T>(T entity) where T : Entity
         {
+            if (!CanSave<T>()) return;
+
             using var access = GetWriteAccess();
             using var command = access.CreateCommand();
             PrepareDelete(command, entity);
@@ -2847,6 +2848,8 @@ namespace InABox.Database.SQLite
 
         public void Purge<T>(IEnumerable<T> entities) where T : Entity
         {
+            if (!CanSave<T>()) return;
+
             // Casting to IList so that we can use it multiple times.
             entities = entities.AsIList();
 
@@ -2934,7 +2937,7 @@ namespace InABox.Database.SQLite
             }
         }
 
-        private MethodInfo _deleteEntitiesMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
+        private readonly MethodInfo _deleteEntitiesMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
             .Single(x => x.Name == nameof(DeleteEntity) && x.IsGenericMethod);
         private void DeleteEntity<T>(Guid[] parentIDs, string parentField, DeletionData deletionData) where T : Entity, new()
         {
@@ -2960,7 +2963,7 @@ namespace InABox.Database.SQLite
             _deleteEntitiesMethod.MakeGenericMethod(T).Invoke(this, new object?[] { parentIDs, parentField, deletionData });
         }
 
-        private MethodInfo _setNullEntityMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
+        private readonly MethodInfo _setNullEntityMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
             .Single(x => x.Name == nameof(SetNullEntity) && x.IsGenericMethod);
         private void SetNullEntity<T>(List<string> properties, Guid[] parentIDs, DeletionData deletionData) where T : Entity, new()
         {
@@ -3132,6 +3135,11 @@ namespace InABox.Database.SQLite
         
         public void Purge(Deletion deletion)
         {
+            if (!CanSave<Deletion>())
+            {
+                return;
+            }
+
             var data = Serialization.Deserialize<DeletionData>(deletion.Data);
             if(data is not null)
             {
@@ -3155,6 +3163,11 @@ namespace InABox.Database.SQLite
 
         public void Recover(Deletion deletion)
         {
+            if (!CanSave<Deletion>())
+            {
+                return;
+            }
+
             if (deletion.ID == Guid.Empty)
             {
                 Logger.Send(LogType.Error, "", "Empty Deletion ID; Recovery cancelled");

+ 1 - 4
inabox.wpf/DynamicGrid/Editors/CodePopupEditor/CodePopupEditorControl.cs

@@ -271,10 +271,7 @@ namespace InABox.DynamicGrid
             foreach (var key in OtherColumns.Where(x => !cols.Contains(x.Key)))
                 columns.Add(key.Key);
             var sort = LookupFactory.DefineSort(_type);
-            var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(_type));
-            CoreUtils.SetPropertyValue(filter, "Expression", CoreUtils.GetMemberExpression(_type, column));
-            CoreUtils.SetPropertyValue(filter, "Operator", Operator.IsEqualTo);
-            CoreUtils.SetPropertyValue(filter, "Value", value);
+            var filter = Filter.Create(_type, column).IsEqualTo(value);
 
             var lookup = client.Query(filter, columns, sort);
             var display = new Dictionary<string,object?>();