浏览代码

Merge branch 'events' into kenric

Kenric Nugteren 1 年之前
父节点
当前提交
d428fe5d85

+ 39 - 1
InABox.Core/CoreExpression.cs

@@ -93,9 +93,10 @@ namespace InABox.Core
 
 
         protected virtual Type? ReturnType { get; }
         protected virtual Type? ReturnType { get; }
 
 
-        public CoreExpression(string expressionString)
+        public CoreExpression(string expressionString, Type? returnType = null)
         {
         {
             Expression = new Expression(expressionString, _context);
             Expression = new Expression(expressionString, _context);
+            ReturnType = returnType;
             if (!IsValid)
             if (!IsValid)
             {
             {
                 var expr = "\"" + expressionString + "\"";
                 var expr = "\"" + expressionString + "\"";
@@ -117,6 +118,32 @@ namespace InABox.Core
             return result;
             return result;
         }
         }
 
 
+        public object? Evaluate(IVariableProvider provider)
+        {
+            var result = Expression.Evaluate(provider);
+            if(ReturnType != null)
+            {
+                return CoreUtils.ChangeType(result, ReturnType);
+            }
+            return result;
+        }
+        public Result<T, Exception> TryEvaluate<T>(IVariableProvider provider)
+        {
+            try
+            {
+                var result = Evaluate(provider);
+                if(result is T ret)
+                {
+                    return Result.Ok(ret);
+                }
+                return Result.Ok<T>(default);
+            }
+            catch (Exception e)
+            {
+                return Result.Error(e);
+            }
+        }
+
         public static List<string> GetModelVariables(Type modelType)
         public static List<string> GetModelVariables(Type modelType)
         {
         {
             var props = DatabaseSchema.Properties(modelType).Select(x => x.Name).ToList();
             var props = DatabaseSchema.Properties(modelType).Select(x => x.Name).ToList();
@@ -166,6 +193,17 @@ namespace InABox.Core
             return default;
             return default;
         }
         }
 
 
+        [return: MaybeNull]
+        public new TReturn Evaluate(IVariableProvider provider)
+        {
+            var result = base.Evaluate(provider);
+            if(result is TReturn ret)
+            {
+                return ret;
+            }
+            return default;
+        }
+
         public Result<TReturn, Exception> Evaluate(TModel model)
         public Result<TReturn, Exception> Evaluate(TModel model)
         {
         {
             var values = new Dictionary<string, object?>();
             var values = new Dictionary<string, object?>();

+ 0 - 1
InABox.Core/CoreTable/CoreRow.cs

@@ -119,7 +119,6 @@ namespace InABox.Core
                 }
                 }
             }
             }
 
 
-            obj.CommitChanges();
             obj.SetObserving(true);
             obj.SetObserving(true);
         }
         }
 
 

+ 8 - 2
InABox.Core/CoreUtils.cs

@@ -2216,9 +2216,9 @@ namespace InABox.Core
             return string.Join("\n", messages);
             return string.Join("\n", messages);
         }
         }
 
 
-        public static void LogException(string userid, Exception err, string? extra = null)
+        public static void LogException(string userid, Exception err, string? extra = null, Logger? logger = null)
         {
         {
-            Logger.Send(LogType.Error, userid, (extra != null ? $"{extra}: " : "") + CoreUtils.FormatException(err));
+            Logger.Send(LogType.Error, userid, (extra != null ? $"{extra}: " : "") + CoreUtils.FormatException(err), transaction: logger?.Transaction);
         }
         }
 
 
         #region OneToMany Relationships
         #region OneToMany Relationships
@@ -2728,6 +2728,12 @@ namespace InABox.Core
             return new Queue<T>(enumerable.ToArray());
             return new Queue<T>(enumerable.ToArray());
         }
         }
 
 
+        public static void SortBy<T, TProp>(this T[] list, Func<T, TProp> comparison)
+            where TProp : IComparable
+        {
+            Array.Sort(list, (a, b) => comparison(a).CompareTo(comparison(b)));
+        }
+
         public static void SortBy<T, TProp>(this List<T> list, Func<T, TProp> comparison)
         public static void SortBy<T, TProp>(this List<T> list, Func<T, TProp> comparison)
             where TProp : IComparable
             where TProp : IComparable
         {
         {

+ 6 - 0
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -354,6 +354,12 @@ namespace InABox.Core
         private static IEnumerable<IProperty> PropertiesInternal(Type type)
         private static IEnumerable<IProperty> PropertiesInternal(Type type)
             => CheckPropertiesInternal(type)?.Values ?? Enumerable.Empty<IProperty>();
             => CheckPropertiesInternal(type)?.Values ?? Enumerable.Empty<IProperty>();
 
 
+        /// <summary>
+        /// Returns every property, both parents and children, for <paramref name="type"/>.
+        /// </summary>
+        public static IEnumerable<IProperty> AllProperties(Type type)
+            => PropertiesInternal(type);
+
         /// <summary>
         /// <summary>
         /// Return the standard property list for <paramref name="type"/>; this includes nested properties.
         /// Return the standard property list for <paramref name="type"/>; this includes nested properties.
         /// </summary>
         /// </summary>

+ 23 - 19
InABox.Core/Serialization.cs

@@ -38,29 +38,33 @@ namespace InABox.Core
 
 
         private static JsonSerializerSettings SerializerSettings(bool indented = true)
         private static JsonSerializerSettings SerializerSettings(bool indented = true)
         {
         {
-            if (_serializerSettings == null)
-            {
-                _serializerSettings = new JsonSerializerSettings
-                {
-                    DateParseHandling = DateParseHandling.DateTime,
-                    DateFormatHandling = DateFormatHandling.IsoDateFormat,
-                    DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
-                };
+            _serializerSettings ??= CreateSerializerSettings();
+            _serializerSettings.Formatting = indented ? Formatting.Indented : Formatting.None;
 
 
-                _serializerSettings.Converters.Add(new CoreTableJsonConverter());
-                //serializerSettings.Converters.Add(new DateTimeJsonConverter());
+            return _serializerSettings;
+        }
 
 
-                _serializerSettings.Converters.Add(new FilterJsonConverter());
-                _serializerSettings.Converters.Add(new ColumnJsonConverter());
-                _serializerSettings.Converters.Add(new SortOrderJsonConverter());
-                _serializerSettings.Converters.Add(new UserPropertiesJsonConverter());
-                //_serializerSettings.Converters.Add(new BaseObjectJSONConverter());
+        public static JsonSerializerSettings CreateSerializerSettings(bool indented = true)
+        {
+            var settings = new JsonSerializerSettings
+            {
+                DateParseHandling = DateParseHandling.DateTime,
+                DateFormatHandling = DateFormatHandling.IsoDateFormat,
+                DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
+            };
 
 
-                _serializerSettings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
-            }
-            _serializerSettings.Formatting = indented ? Formatting.Indented : Formatting.None;
+            settings.Converters.Add(new CoreTableJsonConverter());
+            //serializerSettings.Converters.Add(new DateTimeJsonConverter());
 
 
-            return _serializerSettings;
+            settings.Converters.Add(new FilterJsonConverter());
+            settings.Converters.Add(new ColumnJsonConverter());
+            settings.Converters.Add(new SortOrderJsonConverter());
+            settings.Converters.Add(new UserPropertiesJsonConverter());
+            //_serializerSettings.Converters.Add(new BaseObjectJSONConverter());
+
+            settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
+            settings.Formatting = indented ? Formatting.Indented : Formatting.None;
+            return settings;
         }
         }
 
 
         public static string Serialize(object? o, bool indented = false)
         public static string Serialize(object? o, bool indented = false)

+ 3 - 1
InABox.Database/DbFactory.cs

@@ -44,6 +44,8 @@ public static class DbFactory
     
     
     public static string? ColorScheme { get; set; }
     public static string? ColorScheme { get; set; }
     public static byte[]? Logo { get; set; }
     public static byte[]? Logo { get; set; }
+
+    public static Type DefaultStore { get; set; } = typeof(Store<>);
     
     
     // See notes in Request.DatabaseInfo class
     // See notes in Request.DatabaseInfo class
     // Once RPC transport is stable, these settings need
     // Once RPC transport is stable, these settings need
@@ -351,7 +353,7 @@ public static class DbFactory
 
 
     public static IStore FindStore(Type type, Guid userguid, string userid, Platform platform, string version, Logger logger)
     public static IStore FindStore(Type type, Guid userguid, string userid, Platform platform, string version, Logger logger)
     {
     {
-        var defType = typeof(Store<>).MakeGenericType(type);
+        var defType = DefaultStore.MakeGenericType(type);
         Type? subType = Stores.Where(myType => myType.IsSubclassOf(defType)).FirstOrDefault();
         Type? subType = Stores.Where(myType => myType.IsSubclassOf(defType)).FirstOrDefault();
 
 
         var store = (Activator.CreateInstance(subType ?? defType) as IStore)!;
         var store = (Activator.CreateInstance(subType ?? defType) as IStore)!;

+ 1 - 1
inabox.scripting/ScriptDocument.cs

@@ -192,7 +192,7 @@ public class ScriptDocument : INotifyPropertyChanged
         return true;
         return true;
     }
     }
 
 
-    public void SetValue(string name, object value)
+    public void SetValue(string name, object? value)
     {
     {
         var prop = Properties.FirstOrDefault(x => x.Name.Equals(name));
         var prop = Properties.FirstOrDefault(x => x.Name.Equals(name));
         if (prop == null)
         if (prop == null)

+ 2 - 2
inabox.wpf/DynamicGrid/DynamicGridColumn/DynamicGridColumnNameSelectorWindow.cs

@@ -37,7 +37,7 @@ public class DynamicGridColumnNameSelectorGrid : DynamicItemsListGrid<DynamicGri
 
 
     public string SearchText { get; set; }
     public string SearchText { get; set; }
 
 
-    public DynamicGridColumnNameSelectorGrid(Type type, string[] columnNames)
+    public DynamicGridColumnNameSelectorGrid(Type type, IEnumerable<string> columnNames)
     {
     {
         var items = new List<DynamicGridColumnNameSelectorItem>();
         var items = new List<DynamicGridColumnNameSelectorItem>();
         var parentCols = new HashSet<string>();
         var parentCols = new HashSet<string>();
@@ -223,7 +223,7 @@ public class DynamicGridColumnNameSelectorGrid : DynamicItemsListGrid<DynamicGri
         base.Reload(criteria, columns, ref sort, token, action);
         base.Reload(criteria, columns, ref sort, token, action);
     }
     }
 
 
-    public static bool SelectColumnName(Type type, string[] columnNames, out string value)
+    public static bool SelectColumnName(Type type, IEnumerable<string> columnNames, out string value)
     {
     {
         var grid = new DynamicGridColumnNameSelectorGrid(type, columnNames)
         var grid = new DynamicGridColumnNameSelectorGrid(type, columnNames)
         {
         {

+ 4 - 0
inabox.wpf/InABox.Wpf.csproj

@@ -44,6 +44,7 @@
         <None Remove="Resources\DecreaseIndent16.png" />
         <None Remove="Resources\DecreaseIndent16.png" />
         <None Remove="Resources\delete.png" />
         <None Remove="Resources\delete.png" />
         <None Remove="Resources\design.png" />
         <None Remove="Resources\design.png" />
+        <None Remove="Resources\disabled.png" />
         <None Remove="Resources\disk.png" />
         <None Remove="Resources\disk.png" />
         <None Remove="Resources\doc-bmp.png" />
         <None Remove="Resources\doc-bmp.png" />
         <None Remove="Resources\doc-jpg.png" />
         <None Remove="Resources\doc-jpg.png" />
@@ -222,6 +223,9 @@
       <Resource Include="Resources\design.png">
       <Resource Include="Resources\design.png">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Resource>
       </Resource>
+      <Resource Include="Resources\disabled.png">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </Resource>
       <Resource Include="Resources\disk.png">
       <Resource Include="Resources\disk.png">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Resource>
       </Resource>

+ 10 - 0
inabox.wpf/Resources.Designer.cs

@@ -290,6 +290,16 @@ namespace InABox.Wpf {
             }
             }
         }
         }
         
         
+        /// <summary>
+        ///   Looks up a localized resource of type System.Drawing.Bitmap.
+        /// </summary>
+        public static System.Drawing.Bitmap disabled {
+            get {
+                object obj = ResourceManager.GetObject("disabled", resourceCulture);
+                return ((System.Drawing.Bitmap)(obj));
+            }
+        }
+        
         /// <summary>
         /// <summary>
         ///   Looks up a localized resource of type System.Drawing.Bitmap.
         ///   Looks up a localized resource of type System.Drawing.Bitmap.
         /// </summary>
         /// </summary>

+ 3 - 0
inabox.wpf/Resources.resx

@@ -187,6 +187,9 @@
   <data name="design" type="System.Resources.ResXFileRef, System.Windows.Forms">
   <data name="design" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>Resources\design.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
     <value>Resources\design.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>
   </data>
+  <data name="disabled" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>Resources\disabled.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
+  </data>
   <data name="disk" type="System.Resources.ResXFileRef, System.Windows.Forms">
   <data name="disk" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>Resources\disk.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
     <value>Resources\disk.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>
   </data>

二进制
inabox.wpf/Resources/disabled.png