From e52d889ff0575b37cb7fe7360d994efe32f0c71e Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 1 Dec 2021 18:34:14 +0800 Subject: [PATCH] Apewer-6.5.6 --- Apewer.Source/Apewer.Source.csproj | 2 +- Apewer.Source/Source/DbClient.cs | 4 +- Apewer.Source/Source/MySql.cs | 20 +++--- Apewer.Source/Source/SqlClient.cs | 14 ++-- Apewer.Source/Source/SqlClientThin.cs | 12 ++-- Apewer.Source/Source/Sqlite.cs | 16 ++--- Apewer.Web/Apewer.Web.csproj | 2 +- Apewer.Windows/Apewer.Windows.csproj | 2 +- Apewer/Apewer.csproj | 2 +- Apewer/Apewer.props | 63 ++++++++++++++++++ Apewer/ArrayBuilder.cs | 18 +++++ Apewer/NetworkUtility.cs | 14 ++-- Apewer/RuntimeUtility.cs | 2 +- Apewer/Source/Query.cs | 8 +-- Apewer/Source/Record.cs | 66 +++---------------- .../Source/{OrmHelper.cs => SourceUtility.cs} | 47 +++++++++---- 16 files changed, 170 insertions(+), 122 deletions(-) create mode 100644 Apewer/Apewer.props rename Apewer/Source/{OrmHelper.cs => SourceUtility.cs} (92%) diff --git a/Apewer.Source/Apewer.Source.csproj b/Apewer.Source/Apewer.Source.csproj index 010b8c3..ae9a2a0 100644 --- a/Apewer.Source/Apewer.Source.csproj +++ b/Apewer.Source/Apewer.Source.csproj @@ -1,6 +1,6 @@  - + SOURCE;$(DefineConstants); diff --git a/Apewer.Source/Source/DbClient.cs b/Apewer.Source/Source/DbClient.cs index a0541db..ef95aa4 100644 --- a/Apewer.Source/Source/DbClient.cs +++ b/Apewer.Source/Source/DbClient.cs @@ -299,7 +299,7 @@ namespace Apewer.Source /// 使用指定语句查询,获取查询结果。 /// 要执行的 SQL 语句。 /// 为 SQL 语句提供的参数。 - public Result Query(string sql, IEnumerable parameters = null) where T : class, new() => OrmHelper.As(Query(typeof(T), sql, parameters)); + public Result Query(string sql, IEnumerable parameters = null) where T : class, new() => SourceUtility.As(Query(typeof(T), sql, parameters)); /// 使用指定语句查询,获取查询结果。 /// 目标记录的类型。 @@ -318,7 +318,7 @@ namespace Apewer.Source { try { - var array = OrmHelper.Fill(query, model); + var array = SourceUtility.Fill(query, model); result = new Result(array); } catch (Exception ex) diff --git a/Apewer.Source/Source/MySql.cs b/Apewer.Source/Source/MySql.cs index 679f4fd..1105d75 100644 --- a/Apewer.Source/Source/MySql.cs +++ b/Apewer.Source/Source/MySql.cs @@ -10,7 +10,7 @@ using System.Net; using System.Text; using System.Transactions; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; namespace Apewer.Source { @@ -480,7 +480,7 @@ namespace Apewer.Source public string Insert(object record, string table = null) { if (record == null) return "参数无效。"; - OrmHelper.FixProperties(record); + SourceUtility.FixProperties(record); var structure = TableStructure.Parse(record.GetType()); if (structure == null) return "无法解析记录模型。"; @@ -541,7 +541,7 @@ namespace Apewer.Source } /// - public Result Query(Type model, string sql, IEnumerable parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result Query(Type model, string sql, IEnumerable parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// public Result Query(string sql, IEnumerable parameters = null) where T : class, new() @@ -556,14 +556,14 @@ namespace Apewer.Source } /// 获取所有记录。Flag 为 0 时将忽略 Flag 条件。 - public Result Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from `{tn}`; "; return $"select * from `{tn}` where `_flag`={flag}; "; }); /// 获取所有记录。Flag 为 0 时将忽略 Flag 条件。 - public Result Query(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query(this, (tn) => + public Result Query(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query(this, (tn) => { if (flag == 0) return $"select * from `{tn}`; "; return $"select * from `{tn}` where `_flag`={flag}; "; @@ -577,7 +577,7 @@ namespace Apewer.Source { if (skip < 0) return new Result("参数 skip 超出了范围。"); if (count < 1) return new Result("参数 count 超出了范围。"); - return OrmHelper.Query(this, model, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); + return SourceUtility.Query(this, model, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); } /// 获取记录。 @@ -587,25 +587,25 @@ namespace Apewer.Source { if (skip < 0) return new Result("参数 skip 超出了范围。"); if (count < 1) return new Result("参数 count 超出了范围。"); - return OrmHelper.Query(this, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); + return SourceUtility.Query(this, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); } /// 获取记录。 - public Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select * from `{tn}` where `_key`='{sk}' limit 1;"; return $"select * from `{tn}` where `_key`='{sk}' and `_flag`={flag} limit 1;"; }); /// 获取记录。 - public Result Get(string key, long flag = 0) where T : class, IRecord, new() => OrmHelper.Get(this, key, (tn, sk) => + public Result Get(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get(this, key, (tn, sk) => { if (flag == 0) return $"select * from `{tn}` where `_key`='{sk}' limit 1;"; return $"select * from `{tn}` where `_key`='{sk}' and `_flag`={flag} limit 1;"; }); /// >获取指定类型的主键,按 Flag 属性筛选。 - public Result Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select `_key` from `{tn}`;"; return $"select `_key` from `{tn}` where `_flag`={flag};"; diff --git a/Apewer.Source/Source/SqlClient.cs b/Apewer.Source/Source/SqlClient.cs index 34b0034..264560c 100644 --- a/Apewer.Source/Source/SqlClient.cs +++ b/Apewer.Source/Source/SqlClient.cs @@ -7,7 +7,7 @@ using System.Data; using System.Data.Common; using System.Text; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; using System.Data.SqlClient; #if NETFRAMEWORK @@ -560,7 +560,7 @@ namespace Apewer.Source } /// 获取按指定语句查询到的所有记录。 - public Result Query(Type model, string sql, IEnumerable parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result Query(Type model, string sql, IEnumerable parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// 获取按指定语句查询到的所有记录。 public Result Query(string sql, IEnumerable parameters = null) where T : class, new() @@ -575,35 +575,35 @@ namespace Apewer.Source } /// 获取记录。 - public Result Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 获取记录。 - public Result Query(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query(this, (tn) => + public Result Query(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 获取具有指定 Key 的记录。 - public Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// 获取具有指定 Key 的记录。 - public Result Get(string key, long flag = 0) where T : class, IRecord, new() => OrmHelper.Get(this, key, (tn, sk) => + public Result Get(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get(this, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// 查询有效的 Key 值。 - public Result Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}]; "; return $"select _key from [{tn}] where _flag={flag}; "; diff --git a/Apewer.Source/Source/SqlClientThin.cs b/Apewer.Source/Source/SqlClientThin.cs index 9d118dd..7776c14 100644 --- a/Apewer.Source/Source/SqlClientThin.cs +++ b/Apewer.Source/Source/SqlClientThin.cs @@ -9,7 +9,7 @@ using System.Data; using System.Data.Common; using System.Text; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; using System.Data.SqlClient; #if NETFRAMEWORK @@ -249,35 +249,35 @@ namespace Apewer.Source } /// 获取记录。 - public override Result Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public override Result Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 获取记录。 - public override Result Query(long flag = 0) => OrmHelper.Query(this, (tn) => + public override Result Query(long flag = 0) => SourceUtility.Query(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 获取具有指定 Key 的记录。 - public override Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public override Result Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// 获取具有指定 Key 的记录。 - public override Result Get(string key, long flag = 0) => OrmHelper.Get(this, key, (tn, sk) => + public override Result Get(string key, long flag = 0) => SourceUtility.Get(this, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// 查询有效的 Key 值。 - public override Result Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public override Result Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}]; "; return $"select _key from [{tn}] where _flag={flag}; "; diff --git a/Apewer.Source/Source/Sqlite.cs b/Apewer.Source/Source/Sqlite.cs index 897361e..86982a9 100644 --- a/Apewer.Source/Source/Sqlite.cs +++ b/Apewer.Source/Source/Sqlite.cs @@ -8,7 +8,7 @@ using System.Data.SQLite; using System.Text; //using Mono.Data.Sqlite; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; namespace Apewer.Source { @@ -422,7 +422,7 @@ namespace Apewer.Source public string Insert(object record, string table = null) { if (record == null) return "参数无效。"; - OrmHelper.FixProperties(record); + SourceUtility.FixProperties(record); // 解析模型,获取表名。 var structure = TableStructure.Parse(record.GetType()); @@ -506,7 +506,7 @@ namespace Apewer.Source } /// 获取按指定语句查询到的所有记录。 - public Result Query(Type model, string sql, IEnumerable parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result Query(Type model, string sql, IEnumerable parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// 获取按指定语句查询到的所有记录。 public Result Query(string sql, IEnumerable parameters = null) where T : class, new() @@ -521,35 +521,35 @@ namespace Apewer.Source } /// 查询多条记录。 - public Result Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 查询多条记录。 - public Result Query(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query(this, (tn) => + public Result Query(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// 获取具有指定 Key 的记录。 - public Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; "; return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; "; }); /// 获取具有指定 Key 的记录。 - public Result Get(string key, long flag) where T : class, IRecord, new() => OrmHelper.Get(this, key, (tn, sk) => + public Result Get(string key, long flag) where T : class, IRecord, new() => SourceUtility.Get(this, key, (tn, sk) => { if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; "; return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; "; }); /// 获取指定类型的主键,按 Flag 属性筛选。 - public Result Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; "; return $"select _key from [{tn}]; "; diff --git a/Apewer.Web/Apewer.Web.csproj b/Apewer.Web/Apewer.Web.csproj index 5ce43fe..3dff1d2 100644 --- a/Apewer.Web/Apewer.Web.csproj +++ b/Apewer.Web/Apewer.Web.csproj @@ -1,6 +1,6 @@  - + WEB;$(DefineConstants); diff --git a/Apewer.Windows/Apewer.Windows.csproj b/Apewer.Windows/Apewer.Windows.csproj index e433f3f..190b45a 100644 --- a/Apewer.Windows/Apewer.Windows.csproj +++ b/Apewer.Windows/Apewer.Windows.csproj @@ -1,6 +1,6 @@  - + CS0108;CS0612 diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj index 7d9403c..4547155 100644 --- a/Apewer/Apewer.csproj +++ b/Apewer/Apewer.csproj @@ -1,6 +1,6 @@  - + netstandard2.1;netstandard2.0;netcoreapp3.1;net461;net40;net20 diff --git a/Apewer/Apewer.props b/Apewer/Apewer.props new file mode 100644 index 0000000..898879a --- /dev/null +++ b/Apewer/Apewer.props @@ -0,0 +1,63 @@ + + + + + + + Apewer Lab + Copyright Apewer Lab. All rights reserved. + + Apewer + Apewer Libraries + 6.5.7 + + + + + true + latest + + + + + none + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + true + true + ..\ + + + + + $(AssemblyName) - .NET 6 + $(AssemblyName) - .NET Core 3.1 + $(AssemblyName) - .NET Standard 2.1 + $(AssemblyName) - .NET Standard 2.0 + $(AssemblyName) - .NET Framework 4.6.1 + $(AssemblyName) - .NET Framework 4.0 + $(AssemblyName) - .NET Framework 2.0 + + + + + NETCORE;NET6;$(DefineConstants);$(AdditionalConstants) + NETCORE;$(DefineConstants);$(AdditionalConstants) + NETSTD;$(DefineConstants);$(AdditionalConstants) + NETSTD;$(DefineConstants);$(AdditionalConstants) + NETFX;NET461;NET45;$(DefineConstants);$(AdditionalConstants) + NETFX;NET40;$(DefineConstants);$(AdditionalConstants) + NETFX;NET20;$(DefineConstants);$(AdditionalConstants) + + + + + + + + \ No newline at end of file diff --git a/Apewer/ArrayBuilder.cs b/Apewer/ArrayBuilder.cs index 710e9d4..6251fca 100644 --- a/Apewer/ArrayBuilder.cs +++ b/Apewer/ArrayBuilder.cs @@ -60,6 +60,24 @@ namespace Apewer /// 当前的元素数量。 public int Count { get => _count; } + /// 指定元素在数组中的偏移位置。 + public int IndexOf(T value) + { + var array = _array; + var length = _count; + if (length < 1) return -1; + for (var i = 0; i < length; i++) + { + var item = array[i]; + if (item == null) continue; + if (item.Equals(value)) return i; + } + return -1; + } + + /// 已包含指定的元素。 + public bool Contains(T value) => IndexOf(value) > -1; + /// 添加元素。 public void Add(T item) { diff --git a/Apewer/NetworkUtility.cs b/Apewer/NetworkUtility.cs index 503c0cf..196926d 100644 --- a/Apewer/NetworkUtility.cs +++ b/Apewer/NetworkUtility.cs @@ -383,7 +383,7 @@ namespace Apewer #region Port - private static List ListActivePort(IPEndPoint[] endpoints) + private static int[] ListActivePort(IPEndPoint[] endpoints) { var list = new List(endpoints.Length); foreach (var endpoint in endpoints) @@ -394,20 +394,14 @@ namespace Apewer } list.Sort(); list.Capacity = list.Count; - return list; + return list.ToArray(); } /// 列出活动的 TCP 端口。 - public static List ListActiveTcpPort() - { - return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - } + public static int[] ListActiveTcpPort() => ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); /// 列出活动的 UDP 端口。 - public static List ListActiveUdpPort() - { - return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); - } + public static int[] ListActiveUdpPort() => ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); #endregion diff --git a/Apewer/RuntimeUtility.cs b/Apewer/RuntimeUtility.cs index afa3a61..892bddf 100644 --- a/Apewer/RuntimeUtility.cs +++ b/Apewer/RuntimeUtility.cs @@ -534,7 +534,7 @@ namespace Apewer /// 设置线程为后台线程。 /// 忽略 Action 抛出的异常。 /// 对返回的 Thread 调用 Abort 可结束线程的执行。 - public static Thread StartThread(Action action, bool background = true, bool @try = false) + public static Thread StartThread(Action action, bool background = false, bool @try = false) { if (action == null) return null; var thread = new Thread(new ThreadStart(() => Invoke(action, @try))); diff --git a/Apewer/Source/Query.cs b/Apewer/Source/Query.cs index b8d489d..6a35933 100644 --- a/Apewer/Source/Query.cs +++ b/Apewer/Source/Query.cs @@ -225,18 +225,18 @@ namespace Apewer.Source } /// 获取指定列的所有值,无效值不加入结果。 - public T[] ReadColumn(int column = 0, Func formatter = null) => OrmHelper.Column(this, (r) => (formatter ?? GetValueFormatter()).Invoke(Value(r, column))); + public T[] ReadColumn(int column = 0, Func formatter = null) => SourceUtility.Column(this, (r) => (formatter ?? GetValueFormatter()).Invoke(Value(r, column))); /// 获取指定列的所有值,无效值不加入结果。 /// - public T[] ReadColumn(string column, Func formatter = null) => OrmHelper.Column(this, (r) => (formatter ?? GetValueFormatter()).Invoke(Value(r, column))); + public T[] ReadColumn(string column, Func formatter = null) => SourceUtility.Column(this, (r) => (formatter ?? GetValueFormatter()).Invoke(Value(r, column))); /// 获取指定列的所有值,无效值不加入结果。 - public string[] ReadColumn(int column = 0) => OrmHelper.Column(this, (r) => this.Text(r, column)); + public string[] ReadColumn(int column = 0) => SourceUtility.Column(this, (r) => this.Text(r, column)); /// 获取指定列的所有值,无效值不加入结果。 /// - public string[] ReadColumn(string column) => OrmHelper.Column(this, (r) => this.Text(r, column)); + public string[] ReadColumn(string column) => SourceUtility.Column(this, (r) => this.Text(r, column)); #endregion diff --git a/Apewer/Source/Record.cs b/Apewer/Source/Record.cs index b35f8b0..f286a5c 100644 --- a/Apewer/Source/Record.cs +++ b/Apewer/Source/Record.cs @@ -14,82 +14,34 @@ namespace Apewer.Source public abstract class Record : IRecord { - const int KeyLength = 32; - - private string _key = null; - private long _flag = 0; - /// 记录主键,一般使用 GUID 的字符串形式。 /// /// 注: /// 1. 默认长度为 32,需要修改长度时应该重写此属性; /// 2. 带有 Independent 特性的模型不包含此属性。 /// - [Column("_key", ColumnType.NVarChar, KeyLength)] - public virtual string Key { get { return _key; } set { _key = Compact(value, KeyLength); } } + [Column("_key", ColumnType.NVarChar, 32)] + public virtual string Key { get; set; } /// 记录的标记,Int64 类型,区分记录的状态。 /// 带有 Independent 特性的模型不包含此属性。 [Column("_flag", ColumnType.Integer)] - public long Flag { get { return _flag; } set { _flag = value; } } + public virtual long Flag { get; set; } /// 重置 Key 属性的值。 public virtual void ResetKey() => Key = TextUtility.Key(); /// - public Record() => ResetKey(); - - #region static - - /// 枚举带有 Table 特性的 派生类型。 - public static List EnumerateTableTypes() where T : IRecord => EnumerateTableTypes(typeof(T)); - - /// 枚举带有 Table 特性的派生类型。 - public static List EnumerateTableTypes(Type baseType) + public Record() { - if (baseType == null) return new List(); - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - var list = new List(); - foreach (var a in assemblies) - { - var types = RuntimeUtility.GetTypes(a); - foreach (var t in types) - { - if (!EnumerateTableTypes(t, baseType)) continue; - if (list.Contains(t)) continue; - list.Add(t); - } - } - return list; + ResetKey(); + Flag = DefaultFlag; } - private static bool EnumerateTableTypes(Type type, Type @base) - { - if (type == null || @base == null) return false; - if (!type.IsAbstract) - { - if (RuntimeUtility.Contains(type, false)) - { - if (type.Equals(@base)) - { - return true; - } - else - { - if (RuntimeUtility.IsInherits(type, @base)) return true; ; - } - } - } - return false; - } + #region static - // 限制文本长度,并去除两边的空白字符。 - static string Compact(string text, int length = -1) - { - if (string.IsNullOrEmpty(text)) return ""; - if (length > 0 && text.Length > length) text = text.Substring(0, length); - return text.Trim(); - } + /// 创建 Record 对象时的默认 Flag 值。 + public static long DefaultFlag { get; set; } #endregion diff --git a/Apewer/Source/OrmHelper.cs b/Apewer/Source/SourceUtility.cs similarity index 92% rename from Apewer/Source/OrmHelper.cs rename to Apewer/Source/SourceUtility.cs index 159cfb1..dcbb987 100644 --- a/Apewer/Source/OrmHelper.cs +++ b/Apewer/Source/SourceUtility.cs @@ -9,7 +9,7 @@ namespace Apewer.Source { /// ORM 帮助程序。 - public static class OrmHelper + public static class SourceUtility { #region As @@ -319,18 +319,6 @@ namespace Apewer.Source var tableName = tableStructure.Name; if (string.IsNullOrEmpty(tableName)) return new Result("表名无效。"); - // var keyName = null as string; - // foreach (var column in tableStructure.Columns) - // { - // if (column.Key == "Key") - // { - // keyName = column.Value.Field; - // break; - // } - // } - // if (string.IsNullOrEmpty(keyName)) return Result>.Error("主键字段无效。"); - - // var sql = sqlGetter(tableName, keyName); var sql = sqlGetter(tableName); var query = null as IQuery; try @@ -413,6 +401,39 @@ namespace Apewer.Source return setted; } + /// 枚举带有 Table 特性的 派生类型。 + public static Type[] EnumerateRecords() where T : IRecord => EnumerateRecords(typeof(T)); + + /// 枚举带有 Table 特性的派生类型。 + /// + public static Type[] EnumerateRecords(Type baseType) + { + if (baseType == null) throw new ArgumentNullException(nameof(baseType)); + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + var builder = new ArrayBuilder(); + foreach (var assembly in assemblies) + { + var types = RuntimeUtility.GetTypes(assembly); + foreach (var type in types) + { + if (!EnumerateRecords(type, baseType)) continue; + if (builder.Contains(type)) continue; + builder.Add(type); + } + } + return builder.Export(); + } + + static bool EnumerateRecords(Type type, Type @base) + { + if (type == null || @base == null) return false; + if (type.IsAbstract) return false; + if (!RuntimeUtility.Contains(type, false)) return false; + if (type.Equals(@base)) return true; + if (RuntimeUtility.IsInherits(type, @base)) return true; + return false; + } + #endregion }