Explorar o código

Fixes to filters

Kenric Nugteren hai 1 semana
pai
achega
d768283c12
Modificáronse 1 ficheiros con 29 adicións e 19 borrados
  1. 29 19
      inabox.database.sqlite/SQLiteProvider.cs

+ 29 - 19
inabox.database.sqlite/SQLiteProvider.cs

@@ -10,6 +10,7 @@ using System.Runtime.Serialization.Formatters.Binary;
 using System.Text;
 using System.Text.RegularExpressions;
 using InABox.Core;
+using JetBrains.Annotations;
 using Microsoft.CodeAnalysis;
 using NPOI.SS.UserModel;
 using Quickenshtein;
@@ -1703,7 +1704,7 @@ public class SQLiteProvider : IProvider
         return result;
     }
 
-    public static object Encode(object? o, Type? type)
+    public static object Encode(object? o, Type? type, bool convertToNull = true)
     {
         if (IsNull(o) || type is null)
             return DBNull.Value;
@@ -1750,12 +1751,12 @@ public class SQLiteProvider : IProvider
 
         if (type == typeof(bool))
         {
-            if (o.Equals(false))
+            if (convertToNull && o.Equals(false))
                 return DBNull.Value;
             return o;
         }
 
-        if (type == typeof(string) && string.IsNullOrEmpty(o.ToString()))
+        if (convertToNull && type == typeof(string) && string.IsNullOrEmpty(o.ToString()))
             return DBNull.Value;
 
 
@@ -1786,38 +1787,41 @@ public class SQLiteProvider : IProvider
             return ms.ToArray();
         }
 
-        if (o is double d && d == default)
-            return DBNull.Value;
+        if (convertToNull)
+        {
+            if (o is double d && d == default)
+                return DBNull.Value;
 
-        if (o is float f && f == default)
-            return DBNull.Value;
+            if (o is float f && f == default)
+                return DBNull.Value;
 
-        if (o is int i && i == default)
-            return DBNull.Value;
+            if (o is int i && i == default)
+                return DBNull.Value;
 
-        if (o is long l && l == default)
-            return DBNull.Value;
+            if (o is long l && l == default)
+                return DBNull.Value;
 
-        if (o is decimal dec && dec == default)
-            return DBNull.Value;
+            if (o is decimal dec && dec == default)
+                return DBNull.Value;
+        }
 
         if (o is DateTime dateTime)
         {
-            if (dateTime == DateTime.MinValue)
+            if (convertToNull && dateTime == DateTime.MinValue)
                 return DBNull.Value;
             return string.Format("{0:yyyy-MM-dd HH:mm:ss.FFFFFFF}", o);
         }
 
         if (o is TimeSpan timeSpan)
         {
-            if (timeSpan.Ticks == 0L)
+            if (convertToNull && timeSpan.Ticks == 0L)
                 return DBNull.Value;
             return timeSpan.TotalHours;
         }
 
         if (o is Guid id)
         {
-            if (id == Guid.Empty)
+            if (convertToNull && id == Guid.Empty)
                 return DBNull.Value;
             return o.ToString() ?? "";
         }
@@ -1859,6 +1863,8 @@ public class SQLiteProvider : IProvider
             return $"[{col.Property}]";
         if (value is DateTime date)
             return $"'{date:yyyy-MM-dd HH:mm:ss.FFFFFFF}'";
+        if (value is TimeSpan time)
+            return time.TotalHours.ToString();
         return value.ToString() ?? "";
     }
     
@@ -1875,7 +1881,11 @@ public class SQLiteProvider : IProvider
             return DateTime.TryParse(value, out DateTime _value)
                 ? $"'{_value:yyyy-MM-dd HH:mm:ss.FFFFFFF}'"
                 : $"'{DateTime.MinValue:yyyy-MM-dd HH:mm:ss.FFFFFFF}'";
-        return value.ToString() ?? "";
+        if (type == typeof(TimeSpan))
+            return TimeSpan.TryParse(value, out TimeSpan _value)
+                ? _value.TotalHours.ToString()
+                : "0";
+        return value?.ToString() ?? "";
 
     }
     
@@ -1942,7 +1952,7 @@ public class SQLiteProvider : IProvider
         return sParam;
     }
 
-    private object? GetFilterDefaultValue(Type type)
+    private static object? GetFilterDefaultValue(Type type)
     {
         if(type == typeof(string))
         {
@@ -2091,7 +2101,7 @@ public class SQLiteProvider : IProvider
                 var strProp = $"IFNULL({prop},{EscapeValue(GetFilterDefaultValue(filter.Type))})";
                 var strValue = filter.Value is FilterConstant constant
                     ? constant == FilterConstant.Null ? EscapeValue(GetFilterDefaultValue(filter.Type)) : GetFilterConstant(constant)
-                    : useparams ? EncodeParameter(command, filter.Value) : EscapeValue(filter.Value);
+                    : useparams ? EncodeParameter(command, Encode(filter.Value, filter.Type, convertToNull: false)) : EscapeValue(filter.Value);
 
                 result = string.Format($"({operators[filter.Operator]})", strProp, strValue);
             }