Просмотр исходного кода

Fixed problem when filtering against EntityLinks.ID

Kenric Nugteren 1 месяц назад
Родитель
Сommit
76c25c3c8b
1 измененных файлов с 14 добавлено и 3 удалено
  1. 14 3
      inabox.database.sqlite/SQLiteProvider.cs

+ 14 - 3
inabox.database.sqlite/SQLiteProvider.cs

@@ -2101,8 +2101,7 @@ public class SQLiteProvider : IProvider
             var prop = filter.Property;
 
             LoadFieldsAndTables(command, T, prefix, fieldmap, tables, columns, Column.Create(T, prop), useparams);
-            if (fieldmap.ContainsKey(prop))
-                prop = fieldmap[prop];
+            fieldmap.TryGetValue(prop, out prop);
 
             if(filter.Value is CustomFilterValue)
             {
@@ -2821,6 +2820,10 @@ public class SQLiteProvider : IProvider
                                     fieldmap[baseCol.Name] = string.Format("{0}.[{1}]", tuple.Item2, string.Join(".", bits.Skip(combinecount)));
                                     foreach (var sibling in siblings)
                                     {
+                                        // I think we want to add the ID to the fieldmap here, since we aren't getting it from
+                                        // the main table. However, this becomes iffy, because it is going to be NULL if there is
+                                        // no child entity, so we will have problems if we try to filter by it.
+
                                         var subcol = string.Format("{0}.{1}", string.Join(".", bits.Take(combinecount)), sibling);
                                         if (!subcol.Equals(baseCol.Name))
                                             fieldmap[subcol] = string.Format("{0}.[{1}]", tuple.Item2, sibling);
@@ -2828,8 +2831,12 @@ public class SQLiteProvider : IProvider
                                 }
                                 else
                                 {
-                                    if (siblings.Count.Equals(1) && siblings.First().Equals("ID"))
+                                    if(remoteProperty == nameof(Entity.ID))
                                     {
+                                        // The ID shouldn't be loaded from the sub-table, but from the main one. This is
+                                        // because when joining, the values get initialised to NULL if the link is invalid;
+                                        // Thus, we were matching "NULL = '0000000....'", which always returned false. Hence,
+                                        // we match against the parent table value, which won't be null.
                                         fieldmap[baseCol.Name] = string.Format("{0}1.[{1}]", prefix, baseCol.Name);
                                     }
                                     else
@@ -2866,6 +2873,10 @@ public class SQLiteProvider : IProvider
                                         fieldmap[baseCol.Name] = string.Format("{0}.[{1}]", tuple.Item2, string.Join(".", bits.Skip(combinecount)));
                                         foreach (var sibling in siblings)
                                         {
+                                            // While we are wanting to add the ID to the subquery (we most certainly have to),
+                                            // we don't actually want to set the field map, because of NULL problems with joining.
+                                            if (sibling == nameof(Entity.ID)) continue;
+
                                             var subcol = string.Format("{0}.{1}", string.Join(".", bits.Take(combinecount)), sibling);
                                             if (!subcol.Equals(baseCol.Name))
                                                 fieldmap[subcol] = string.Format("{0}.[{1}]", tuple.Item2, sibling);