Parcourir la source

Removed Deleted flag from queries

Kenric Nugteren il y a 1 an
Parent
commit
daf5c0b3d7
3 fichiers modifiés avec 30 ajouts et 160 suppressions
  1. 1 0
      InABox.Core/Entity.cs
  2. 2 2
      InABox.Database/IProvider.cs
  3. 27 158
      inabox.database.sqlite/SQLiteProvider.cs

+ 1 - 0
InABox.Core/Entity.cs

@@ -150,6 +150,7 @@ namespace InABox.Core
         /// </summary>
         [NullEditor]
         [DoNotSerialize]
+        [Obsolete]
         public Guid Deleted { get; set; } = Guid.Empty;
 
         public static Type ClassVersion(Type t)

+ 2 - 2
InABox.Database/IProvider.cs

@@ -22,7 +22,7 @@ namespace InABox.Database
         CoreTable Query<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, 
             int top = int.MaxValue, bool log = false, bool distinct = false) where T : Entity, new();
         
-        CoreTable Query(Type type, IFilter filter = null, IColumns columns = null, ISortOrder sort = null, 
+        CoreTable Query(Type type, IFilter? filter = null, IColumns? columns = null, ISortOrder? sort = null, 
             int top = int.MaxValue, bool log = false, bool distinct = false);
         
         /// <summary>
@@ -42,7 +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, IEnumerable<Entity> entities, bool addDelete = false);
+        void Save(Type type, IEnumerable<Entity> entities);
 
         void Delete<T>(T entity, string userID) where T : Entity, new();
         void Delete<T>(IEnumerable<T> entities, string userID) where T : Entity, new();

+ 27 - 158
inabox.database.sqlite/SQLiteProvider.cs

@@ -726,8 +726,6 @@ namespace InABox.Database.SQLite
             Dictionary<String, object?> result = new Dictionary<string, object?>();
             if (!viewfields.ContainsKey("ID"))
                 result["ID"] = null;
-            if (!viewfields.ContainsKey("Deleted"))
-                result["Deleted"] = null;
             if (!viewfields.ContainsKey("Created"))
                 result["Created"] = null;
             if (!viewfields.ContainsKey("CreatedBy"))
@@ -792,14 +790,8 @@ namespace InABox.Database.SQLite
                             }
                         }
 
-                        var filter = Filter.Create<Entity>(table.Entity, x => x.Deleted).IsEqualTo(Guid.Empty);
-                        if(table.Filter is not null)
-                        {
-                            filter.And(table.Filter);
-                        }
-
                         var query = PrepareSelectNonGeneric(table.Entity, new SQLiteCommand(), 'A',
-                            filter, columns, null,
+                            table.Filter, columns, null,
                             null, constants, int.MaxValue, union.Distinct, false);
                         
                         queries.Add(query);
@@ -809,8 +801,6 @@ namespace InABox.Database.SQLite
                     // var fields = viewfields.Keys.Select(x => String.Format("[{0}]", x)).ToList();
                     // if (!fields.Contains("[ID]"))
                     //     fields.Add(String.Format("NULL as [ID]"));
-                    // if (!fields.Contains("[Deleted]"))
-                    //     fields.Add(String.Format("NULL as [Deleted]"));
                     // if (!fields.Contains("[Created]"))
                     //     fields.Add(String.Format("NULL as [Created]"));
                     // if (!fields.Contains("[CreatedBy]"))
@@ -822,7 +812,7 @@ namespace InABox.Database.SQLite
                     //
                     // List<String> queries = new List<String>();
                     // foreach (var entity in union.Entities)
-                    //     queries.Add(String.Format("SELECT {0} {1} FROM {2} WHERE [Deleted] is NULL",
+                    //     queries.Add(String.Format("SELECT {0} {1} FROM {2}",
                     //         union.Distinct ? "DISTINCT" : "",
                     //         String.Join(", ", fields),
                     //         entity.EntityName().Split('.').Last())
@@ -836,7 +826,7 @@ namespace InABox.Database.SQLite
                         constants.Add($"{EscapeValue(constant.Value)} as [{constant.Key}]");
                     
                     String query = String.Format(
-                        "SELECT {0} {1}.[{2}] as [{3}], {4}.[{5}] as [{6}], {7} FROM {1}, {4} WHERE {1}.[Deleted] is NULL and {4}.[Deleted] is NULL and {1}.[{8}] = {4}.[{9}]",
+                        "SELECT {0} {1}.[{2}] as [{3}], {4}.[{5}] as [{6}], {7} FROM {1}, {4} WHERE {1}.[{8}] = {4}.[{9}]",
                         cross.Distinct ? "DISTINCT" : "",
                         cross.LeftEntity(),
                         cross.LeftProperty(),
@@ -860,14 +850,8 @@ namespace InABox.Database.SQLite
                     int iTable = 0;
                     foreach (var table in cartesian.Tables)
                     {
-                        var filter = Filter.Create<Entity>(table.Type, x => x.Deleted).IsEqualTo(Guid.Empty);
-                        if (table.Filter is not null)
-                        {
-                            filter.And(table.Filter);
-                        }
-
                         var subQueryText = PrepareSelectNonGeneric(table.Type, new SQLiteCommand(), 'A',
-                            filter, table.Columns, null,
+                            table.Filter, table.Columns, null,
                             null, null, int.MaxValue, cartesian.Distinct, false);
                         
                         tables.Add($"({subQueryText}) T{iTable}");
@@ -1830,22 +1814,17 @@ namespace InABox.Database.SQLite
 
                                 // LogStart();
 
-                                var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(linkedtype), "Deleted") as IFilter;
-                                filter!.Operator = Operator.IsEqualTo;
-                                filter.Value = Guid.Empty;
-
                                 var aggFilter = agg.Filter;
                                 if(aggFilter is not null)
                                 {
-                                    List<String> ffs = new List<string>();
+                                    var ffs = new List<string>();
                                     FilterFields(aggFilter, ffs);
                                     //foreach (var ff in ffs)
                                     //    subcols.Add(ff);
-                                    filter.And(aggFilter);
                                 }
 
                                 var linkedtable = string.Format("({0})",
-                                    PrepareSelectNonGeneric(linkedtype, command, newprefix, filter, subcols, null, scols, null, int.MaxValue, false, useparams));
+                                    PrepareSelectNonGeneric(linkedtype, command, newprefix, aggFilter, subcols, null, scols, null, int.MaxValue, false, useparams));
                                 
                                 var alias = tables.Count + 1;
 
@@ -1996,34 +1975,18 @@ namespace InABox.Database.SQLite
                             if (siblings.Count.Equals(1) && siblings.First().Equals("ID"))
                                 fieldmap[columnname] = string.Format("{0}1.[{1}]", prefix, columnname);
                             
-                            // Hmmm.. there must have been a reason why we do this, but...
-                            // Deleted is not part of parent tables (only ID is), so the same 
-                            // shortcut that limits joining when we are only selecting ID's does
-                            // not work if we are selecting Deleted's.
-                            
-                            //else if (siblings.Count.Equals(2) && siblings.Skip(1).First().Equals("Deleted"))
-                            //    fieldmap[columnname] = string.Format("{0}1.[{1}]", prefix, columnname);
-                            
                             else
                             {
                                 if (!siblings.Contains("ID"))
-                                    siblings.Insert(0, "ID");                                
-                                if (!siblings.Contains("Deleted"))
-                                    siblings.Insert(1, "Deleted");
+                                    siblings.Insert(0, "ID");
 
                                 var subcols = Columns.Create(linkedtype);
 
                                 foreach (var sibling in siblings)
                                     subcols.Add(sibling);
 
-
-                                var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(linkedtype), "Deleted") as IFilter;
-                                filter!.Operator = Operator.IsEqualTo;
-                                filter.Value = Guid.Empty;
-
-
                                 var linkedtable = string.Format("({0})",
-                                    PrepareSelectNonGeneric(linkedtype, command, newprefix, filter, subcols, null, null, null, int.MaxValue, false, useparams));
+                                    PrepareSelectNonGeneric(linkedtype, command, newprefix, null, subcols, null, null, null, int.MaxValue, false, useparams));
 
                                 var link = string.Format("{0}.ID", prop.Name);
                                 var tuple = tables.FirstOrDefault(x =>
@@ -2214,7 +2177,7 @@ namespace InABox.Database.SQLite
             Dictionary<string, string>? aggregates, Dictionary<string, object?>? constants, int top, bool distinct, bool useparams) where T : Entity
             => PrepareSelectNonGeneric(typeof(T), command, prefix, filter, columns, sort, aggregates, constants, top, distinct, useparams);
 
-        private static void PrepareUpsertNonGeneric(Type T, SQLiteCommand command, Entity item, bool addDelete = false)
+        private static void PrepareUpsertNonGeneric(Type T, SQLiteCommand command, Entity item)
         {
             command.CommandText = "";
             command.Parameters.Clear();
@@ -2224,12 +2187,6 @@ namespace InABox.Database.SQLite
 
             Dictionary<string, object> insert = item.GetValues(true);
             Dictionary<string, object> update = item.GetValues(false);
-            if (addDelete)
-            {
-                insert.Add("Deleted", item.Deleted);
-                if (item.HasOriginalValue("Deleted"))
-                    update.Add("Deleted", item.Deleted);
-            }
 
             var insertfields = new List<string>();
             var insertvalues = new List<string>();
@@ -2280,8 +2237,8 @@ namespace InABox.Database.SQLite
             command.CommandText = string.Join(" ", particles);
         }
 
-        private void PrepareUpsert<T>(SQLiteCommand command, T item, bool addDelete = false) where T : Entity
-            => PrepareUpsertNonGeneric(typeof(T), command, item, addDelete);
+        private void PrepareUpsert<T>(SQLiteCommand command, T item) where T : Entity
+            => PrepareUpsertNonGeneric(typeof(T), command, item);
 
         public void PrepareDelete<T>(SQLiteCommand command, T item) where T : Entity
         {
@@ -2328,11 +2285,6 @@ namespace InABox.Database.SQLite
 
         public IEnumerable<object[]> List<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null) where T : Entity, new()
         {
-            var newFilter = new Filter<T>(x => x.Deleted).IsEqualTo(Guid.Empty);
-            if (filter != null)
-                newFilter.And(filter);
-            filter = newFilter;
-
             //Dictionary<String, TimeSpan> stopwatch = new Dictionary<string, TimeSpan>();
             //Stopwatch sw = new Stopwatch();
             //sw.Start();
@@ -2514,19 +2466,16 @@ namespace InABox.Database.SQLite
         private CoreTable DoQuery<T>(Filter<T>? filter, Columns<T>? columns, SortOrder<T>? sort, int top, bool log, bool distinct)
             where T : Entity, new() => DoQueryNonGeneric(typeof(T), filter, columns, sort, top, log, distinct);
 
-        public CoreTable Query(Type type, IFilter filter, IColumns columns, ISortOrder sort, int top, bool log, bool distinct)
+        public CoreTable Query(Type type, IFilter? filter, IColumns? columns, ISortOrder? sort, int top, bool log, bool distinct)
             => DoQueryNonGeneric(type, filter, columns, sort, top, log, distinct);
         
         public CoreTable Query<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, int top = int.MaxValue, bool log = false, bool distinct = false) 
             where T : Entity, new()
         {
-            var newFilter = new Filter<T>(x => x.Deleted).IsEqualTo(Guid.Empty);
-            if (filter != null)
-                newFilter.And(filter);
-
-            return DoQuery(newFilter, columns, sort, top, log, distinct);
+            return DoQuery(filter, columns, sort, top, log, distinct);
         }
-        
+
+        [Obsolete]
         public CoreTable QueryDeleted<T>(Deletion deletion, Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, int top = int.MaxValue, bool log = false)
             where T : Entity, new()
         {
@@ -2547,11 +2496,6 @@ namespace InABox.Database.SQLite
 
         public T[] Load<T>(Filter<T>? filter = null, SortOrder<T>? sort = null) where T : Entity, new()
         {
-            var newFilter = new Filter<T>(x => x.Deleted).IsEqualTo(Guid.Empty);
-            if (filter != null)
-                newFilter.And(filter);
-            filter = newFilter;
-
             var result = new List<T>();
             var cols = CoreUtils.GetColumns<T>(null);
 
@@ -2610,7 +2554,7 @@ namespace InABox.Database.SQLite
 
         #region Save
 
-        private void OnSaveNonGeneric(Type T, IEnumerable<Entity> entities, bool addDelete = false)
+        private void OnSaveNonGeneric(Type T, IEnumerable<Entity> entities)
         {
             if (!entities.Any())
                 return;
@@ -2626,7 +2570,7 @@ namespace InABox.Database.SQLite
                         {
                             foreach (var entity in entities)
                             {
-                                PrepareUpsertNonGeneric(T, command, entity, addDelete);
+                                PrepareUpsertNonGeneric(T, command, entity);
                                 command.ExecuteNonQuery();
                             }
 
@@ -2644,11 +2588,11 @@ namespace InABox.Database.SQLite
                     throw error;
             }
         }
-        private void OnSave<T>(IEnumerable<T> entities, bool addDelete = false) where T : Entity
-            => OnSaveNonGeneric(typeof(T), entities, addDelete);
+        private void OnSave<T>(IEnumerable<T> entities) where T : Entity
+            => OnSaveNonGeneric(typeof(T), entities);
 
-        public void Save(Type type, IEnumerable<Entity> entities, bool addDelete = false)
-            => OnSaveNonGeneric(type, entities, addDelete);
+        public void Save(Type type, IEnumerable<Entity> entities)
+            => OnSaveNonGeneric(type, entities);
 
         public static bool CanSave<T>()
         {
@@ -2672,9 +2616,9 @@ namespace InABox.Database.SQLite
             {
                 return;
             }
-            OnSave(entities, false);
+            OnSave(entities);
         }
-        private void OnSaveNonGeneric(Type T, Entity entity, bool addDelete = false)
+        private void OnSaveNonGeneric(Type T, Entity entity)
         {
             Exception? error = null;
             using (var access = GetWriteAccess())
@@ -2683,7 +2627,7 @@ namespace InABox.Database.SQLite
                 {
                     try
                     {
-                        PrepareUpsertNonGeneric(T, command, entity, addDelete);
+                        PrepareUpsertNonGeneric(T, command, entity);
                         command.ExecuteNonQuery();
                     }
                     catch (Exception e)
@@ -2697,8 +2641,8 @@ namespace InABox.Database.SQLite
                 throw error;
         }
 
-        private void OnSave<T>(T entity, bool addDelete = false) where T : Entity
-            => OnSaveNonGeneric(typeof(T), entity, addDelete);
+        private void OnSave<T>(T entity) where T : Entity
+            => OnSaveNonGeneric(typeof(T), entity);
 
         public void Save<T>(T entity) where T : Entity
         {
@@ -2706,7 +2650,7 @@ namespace InABox.Database.SQLite
             {
                 return;
             }
-            OnSave(entity, false);
+            OnSave(entity);
         }
 
         #endregion
@@ -2873,7 +2817,6 @@ namespace InABox.Database.SQLite
 
         public void Delete<T>(T entity, string userID) where T : Entity, new()
         {
-//#if PURGE
             if (!CanSave<T>())
             {
                 return;
@@ -2901,14 +2844,11 @@ namespace InABox.Database.SQLite
                 Data = Serialization.Serialize(deletionData)
             };
             OnSave(deletion);
-            //#else
             Purge(entity);
-//#endif
         }
 
         public void Delete<T>(IEnumerable<T> entities, string userID) where T : Entity, new()
         {
-//#if PURGE
             if (!CanSave<T>())
             {
                 return;
@@ -2939,9 +2879,7 @@ namespace InABox.Database.SQLite
                 Data = Serialization.Serialize(deletionData)
             };
             OnSave(deletion);
-            //#else
             Purge(entities);
-//#endif
         }
 
         private void AddDeletionType(Type type, List<Type> deletions)
@@ -2967,78 +2905,9 @@ namespace InABox.Database.SQLite
             _allCascades[type] = deletionTypes;
             return deletionTypes;
         }
-
-        /*private void DoSetNull<TChild>(string field, Guid[] parentIDs) where TChild : Entity, new()
-        {
-            var columns = new Columns<TChild>(x => x.ID);
-            columns.Add(field);
-            var children = DoQuery(
-                new Filter<TChild>(field).InList(parentIDs),
-                columns,
-                null,
-                int.MaxValue,
-                true,
-                false
-            ).Rows.Select(x => x.ToObject<TChild>()).ToList();
-            foreach(var child in children)
-            {
-                CoreUtils.SetPropertyValue(child, field, Guid.Empty);
-            }
-            OnSave(children);
-        }*/
-
-        /*private void PurgeEntityType<T>(Deletion deletion) where T : Entity, new()
-        {
-            var entities = QueryDeleted(deletion, null, new Columns<T>(x => x.ID)).Rows.Select(x => x.ToObject<T>()).ToList();
-            if (_setNulls.TryGetValue(typeof(T), out var setNulls))
-            {
-                var ids = entities.Select(x => x.ID).ToArray();
-                var setNullMethod = typeof(SQLiteProvider).GetMethod(nameof(DoSetNull), BindingFlags.NonPublic | BindingFlags.Instance)!;
-                foreach (var (childType, childFields) in setNulls)
-                {
-                    var method = setNullMethod.MakeGenericMethod(childType);
-                    foreach(var childField in childFields)
-                    {
-                        method.Invoke(this, new object[] { childField, ids });
-                    }
-                }
-            }
-            Purge(entities);
-        }*/
-
-        /*private void RecoverEntityType<T>(Deletion deletion) where T : Entity, new()
-        {
-            var entities = QueryDeleted(deletion, null, new Columns<T>(x => x.ID, x => x.Deleted)).Rows.Select(x => x.ToObject<T>()).ToList();
-            foreach (var entity in entities)
-            {
-                entity.Deleted = Guid.Empty;
-            }
-            OnSave(entities, true);
-        }*/
         
         public void Purge(Deletion deletion)
         {
-            /*if(deletion.ID == Guid.Empty)
-            {
-                Logger.Send(LogType.Error, "", "Empty Deletion ID; Purge cancelled");
-                return;
-            }
-
-            var entityType = CoreUtils.Entities.FirstOrDefault(x => x.Name == deletion.HeadTable);
-            if(entityType is null)
-            {
-                Logger.Send(LogType.Error, "", $"Entity {deletion.HeadTable} does not exist; Purge cancelled");
-                return;
-            }
-
-            var deletionTypes = GetDeletionTypes(entityType);
-
-            var purgeMethod = typeof(SQLiteProvider).GetMethod(nameof(PurgeEntityType), BindingFlags.NonPublic | BindingFlags.Instance)!;
-            foreach(var type in deletionTypes)
-            {
-                purgeMethod.MakeGenericMethod(type).Invoke(this, new object[] { deletion });
-            }
-            */
             Purge<Deletion>(deletion);
         }