Bladeren bron

Fixed crashes with formulae that contained constants

frogsoftware 10 maanden geleden
bovenliggende
commit
551d9d02bd
2 gewijzigde bestanden met toevoegingen van 42 en 26 verwijderingen
  1. 14 0
      InABox.Database/DataUpdater.cs
  2. 28 26
      inabox.database.sqlite/SQLiteProvider.cs

+ 14 - 0
InABox.Database/DataUpdater.cs

@@ -265,4 +265,18 @@ public static class DataUpdater
             return false;
             return false;
         }
         }
     }
     }
+    
+    public static bool DoSpecificMigration(VersionNumber from, VersionNumber to)
+    {
+        try
+        {
+            var _success = MigrateDatabase(from, to, out var _);
+            return _success;
+        }
+        catch(Exception e)
+        {
+            Logger.Send(LogType.Error, "", $"Error while migrating database: {CoreUtils.FormatException(e)}");
+            return false;
+        }
+    }
 }
 }

+ 28 - 26
inabox.database.sqlite/SQLiteProvider.cs

@@ -1722,45 +1722,51 @@ public class SQLiteProvider : IProvider
         if (attribute.Operator == FormulaOperator.Constant)
         if (attribute.Operator == FormulaOperator.Constant)
             return EscapeValue(attribute.Value);
             return EscapeValue(attribute.Value);
 
 
-        if (!fieldmap.ContainsKey(attribute.Value))
-            throw new Exception(string.Format("{0}.{1} -> {2} does not exist", columnname, attribute.GetType().Name, attribute.Value));
+// Crashes when value is a constant, and therefore not a field in the table        
+//        if (!fieldmap.ContainsKey(attribute.Value))
+//            throw new Exception(string.Format("{0}.{1} -> {2} does not exist", columnname, attribute.GetType().Name, attribute.Value));
+  
+// Crashes when modifier is a constant, and therefore not a field in the table        
+//        foreach (var modifier in attribute.Modifiers)
+//            if (!fieldmap.ContainsKey(modifier))
+//                throw new Exception(string.Format("{0}.{1} -> {2} does not exist", columnname, attribute.GetType().Name, modifier));
         
         
-        foreach (var modifier in attribute.Modifiers)
-            if (!fieldmap.ContainsKey(modifier))
-                throw new Exception(string.Format("{0}.{1} -> {2} does not exist", columnname, attribute.GetType().Name, modifier));
-
-
         if (attribute.Operator == FormulaOperator.Add)
         if (attribute.Operator == FormulaOperator.Add)
-            return string.Format("(IFNULL({0},0.00) + {1})", fieldmap[attribute.Value],
-                string.Join(" + ", attribute.Modifiers.Select(x => string.Format("IFNULL({0},0.00)", fieldmap[x]))));
+            return string.Format("(IFNULL({0},0.00) + {1})", 
+                fieldmap.TryGetValue(attribute.Value, out var _value) ? _value : attribute.Value,
+                string.Join(" + ", attribute.Modifiers.Select(x => fieldmap.TryGetValue(x, out var _value) ? $"IFNULL({_value},0.00)" : $"{x}")));
 
 
         if (attribute.Operator == FormulaOperator.Subtract)
         if (attribute.Operator == FormulaOperator.Subtract)
-            return string.Format("(IFNULL({0},0.00) - ({1}))", fieldmap[attribute.Value],
-                string.Join(" + ", attribute.Modifiers.Select(x => string.Format("IFNULL({0},0.00)", fieldmap[x]))));
+            return string.Format("(IFNULL({0},0.00) - ({1}))", 
+                fieldmap.TryGetValue(attribute.Value, out var _value) ? _value : attribute.Value,
+                string.Join(" + ", attribute.Modifiers.Select(x => fieldmap.TryGetValue(x, out var _value) ? $"IFNULL({_value},0.00)" : $"{x}")));
 
 
         if (attribute.Operator == FormulaOperator.Multiply)
         if (attribute.Operator == FormulaOperator.Multiply)
-            return string.Format("(IFNULL({0},0.00) * {1})", fieldmap[attribute.Value],
-                string.Join(" * ", attribute.Modifiers.Select(x => string.Format("IFNULL({0},0.00)", fieldmap[x]))));
+            return string.Format("(IFNULL({0},0.00) * {1})", 
+                fieldmap.TryGetValue(attribute.Value, out var _value) ? _value : attribute.Value,
+                string.Join(" * ", attribute.Modifiers.Select(x => fieldmap.TryGetValue(x, out var _value) ? $"IFNULL({_value},0.00)" : $"{x}")));
 
 
         if (attribute.Operator == FormulaOperator.Divide)
         if (attribute.Operator == FormulaOperator.Divide)
         {
         {
-            var result = string.Format("IFNULL({0},0.00)", fieldmap[attribute.Value]);
+            var result = string.Format("IFNULL({0},0.00)", fieldmap.TryGetValue(attribute.Value, out var _v) ? _v : attribute.Value);
             foreach (var modifier in attribute.Modifiers)
             foreach (var modifier in attribute.Modifiers)
-                result = string.Format("({0} / {1})", result, string.Format("IFNULL({0},1.00)", fieldmap[modifier]));
+                result = string.Format("({0} / {1})", result, fieldmap.TryGetValue(modifier, out var _value) ? $"IFNULL({_value},1.00)" : $"{modifier}");
             return result;
             return result;
         }
         }
 
 
         if (attribute.Operator == FormulaOperator.Maximum)
         if (attribute.Operator == FormulaOperator.Maximum)
         {
         {
-            var parameters = attribute.Modifiers.Select(m => $"IFNULL({fieldmap[m]},0.00)");
-            var result = $"MAX({fieldmap[attribute.Value]}, {String.Join(", ", parameters)})";
+            var parameters = attribute.Modifiers.Select(m => fieldmap.TryGetValue(m, out var _value) ? $"IFNULL({_value},0.00)" : $"{m}");
+            var primary = fieldmap.TryGetValue(attribute.Value, out var _v) ? _v : attribute.Value;
+            var result = $"MAX({primary}, {String.Join(", ", parameters)})";
             return result;
             return result;
         }
         }
 
 
         if (attribute.Operator == FormulaOperator.Minumum)
         if (attribute.Operator == FormulaOperator.Minumum)
         {
         {
-            var parameters = attribute.Modifiers.Select(m => $"IFNULL({fieldmap[m]},0.00)");
-            var result = $"MIN({fieldmap[attribute.Value]}, {String.Join(", ", parameters)})";
+            var parameters = attribute.Modifiers.Select(m => fieldmap.TryGetValue(m, out var _value) ? $"IFNULL({_value},0.00)" : $"{m}");
+            var primary = fieldmap.TryGetValue(attribute.Value, out var _v) ? _v : attribute.Value;
+            var result = $"MIN({primary}, {String.Join(", ", parameters)})";
             return result;
             return result;
         }
         }
 
 
@@ -2085,16 +2091,12 @@ public class SQLiteProvider : IProvider
                         {
                         {
                             foreach (var field in fnc.Modifiers.Prepend(fnc.Value))
                             foreach (var field in fnc.Modifiers.Prepend(fnc.Value))
                             {
                             {
-                                try
+                                if (CoreUtils.TryGetProperty(type,field,out var _))
                                 {
                                 {
                                     CheckColumn(columns, field);
                                     CheckColumn(columns, field);
-                                    LoadFieldsAndTables(command, type, prefix, fieldmap, tables, columns, Column.Create(type, field), useparams);
+                                    LoadFieldsAndTables(command, type, prefix, fieldmap, tables, columns,
+                                        Column.Create(type, field), useparams);
                                 }
                                 }
-                                catch (Exception e2)
-                                {
-                                    Logger.Send(LogType.Error,"",$"Error creating column from field [{field}] : {e2.Message} ");
-                                }
-
                             }
                             }
                             fieldmap[baseCol.Name] = GetFunction(fnc, fieldmap, baseCol.Name);
                             fieldmap[baseCol.Name] = GetFunction(fnc, fieldmap, baseCol.Name);
                         }
                         }