diff --git a/Apewer.Run/Apewer.Run.csproj b/Apewer.Run/Apewer.Run.csproj deleted file mode 100644 index 2ee913f..0000000 --- a/Apewer.Run/Apewer.Run.csproj +++ /dev/null @@ -1,38 +0,0 @@ - - - - Exe - - - - net461 - - - - - $(AssemblyName) - .NET Framework 4.6.1 - NETFX;NET461;NET45;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Apewer.Run/_Program.cs b/Apewer.Run/_Program.cs deleted file mode 100644 index 1e176b8..0000000 --- a/Apewer.Run/_Program.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Apewer.Run -{ - - class Program - { - - static void Main(string[] args) => RuntimeUtility.InvokePublicClass(); - - } - -} diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj index 583708e..8a3e9a7 100644 --- a/Apewer/Apewer.csproj +++ b/Apewer/Apewer.csproj @@ -1,7 +1,5 @@  - - @@ -17,93 +15,4 @@ Apewer - - - $(AssemblyName) - .NET Standard 2.1 - NETSTD;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - $(AssemblyName) - .NET Standard 2.0 - NETSTD;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - $(AssemblyName) - .NET Framework 2.0 - NETFX;NET20;$(DefineConstants);$(AdditionalConstants) - - - - - - - - - - - - - - - $(AssemblyName) - .NET Framework 4.0 - NETFX;NET40;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - - - - - - - - - - - - - - - - - - $(AssemblyName) - .NET Framework 4.6.1 - NETFX;NET461;NET45;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - - - - - - - - - - - - - - - - - - $(AssemblyName) - .NET Core 3.1 - NETCORE;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) - - - - - - - - - - - diff --git a/Apewer/Result.cs b/Apewer/Result.cs index 92e9818..14ae678 100644 --- a/Apewer/Result.cs +++ b/Apewer/Result.cs @@ -138,64 +138,62 @@ namespace Apewer public class Result : IDisposable { - private static readonly T Default = default(T); - - private T _entity = default(T); - private Exception _exception = null; - private int _code = 0; - private string _message = Constant.EmptyString; - /// 对象。 - public T Entity { get { return _entity; } } + public T Entity { get; internal set; } /// 异常。 - public Exception Exception { get { return _exception; } } + public Exception Exception { get; internal set; } /// 代码。 - public int Code { get { return _code; } } + public int Code { get; internal set; } /// 消息,此属性永不为 Null 值,当存在 Exception 时将被 Exception.Message 强制覆盖。 - public string Message { get { return _message; } } + public string Message { get; internal set; } /// 含有实体对象。 - public bool HasEntity { get { return _entity != null; } } + public bool HasEntity { get { return Entity != null; } } /// 执行与释放或重置非托管资源关联的应用程序定义的任务。 public void Dispose() { - var entity = _entity as IDisposable; + var entity = Entity as IDisposable; if (entity != null) entity.Dispose(); } private void Construct(T entity, Exception exception, string message) { - _entity = entity; - _exception = exception; - _message = message ?? Constant.EmptyString; - if (_exception != null && TextVerifier.IsBlank(_message)) + Entity = entity; + Exception = exception; + if (string.IsNullOrEmpty(message)) { - _message = _exception.Message; + if (exception != null) + { + var error = Exception.Message; + if (string.IsNullOrEmpty(error)) error = "存在带有无效消息的异常。"; + Message = error; + } } + else Message = message; } /// 创建空结果,不含有实体、消息和异常。 public Result() { } /// 创建带有异常的结果。 - public Result(Exception exception) { Construct(Default, exception, null); } + public Result(Exception exception) => Construct(default(T), exception, null); /// 创建带有实体的结果。 - public Result(T entity) { Construct(entity, null, null); } + public Result(T entity) => Construct(entity, null, null); /// 创建带有实体和消息的结果。 - public Result(T entity, params string[] message) { Construct(entity, null, TextGenerator.Merge(message)); } + public Result(T entity, params string[] message) => Construct(entity, null, TextGenerator.Merge(message)); /// 创建带有错误消息的结果。 public static Result Error(params string[] message) { var instance = new Result(); var merged = TextGenerator.Merge(message); - instance.Construct(Default, null, merged.IsEmpty() ? "存在无效消息。" : merged); + instance.Construct(default(T), null, merged.IsEmpty() ? "存在无效消息。" : merged); return instance; } @@ -203,7 +201,7 @@ namespace Apewer public static Result Error(Exception exception) { var instance = new Result(); - instance.Construct(Default, exception, (exception == null ? "存在 NULL 异常。" : null)); + instance.Construct(default(T), exception, (exception == null ? "存在 NULL 异常。" : null)); return instance; } diff --git a/Apewer/Source/IOrm.cs b/Apewer/Source/IOrm.cs index fb89a8f..98694f5 100644 --- a/Apewer/Source/IOrm.cs +++ b/Apewer/Source/IOrm.cs @@ -14,32 +14,58 @@ namespace Apewer.Source /// 错误信息。当成功时候返回空字符串。 public string Initialize(Type model); + /// 初始化指定类型,以创建表或增加字段。 + /// 错误信息。当成功时候返回空字符串。 + public string Initialize() where T : IRecord; + /// 插入记录。 /// 要插入的记录实体。 /// 错误信息。当成功时候返回空字符串。 - public string Insert(Record record); + public string Insert(IRecord record); /// 更新记录。 /// 要插入的记录实体。 /// 错误信息。当成功时候返回空字符串。 - public string Update(Record record); + public string Update(IRecord record); /// 获取指定类型的主键,按 Flag 属性筛选。 /// 要查询的类型。 /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 public Result> Keys(Type model, long flag = 0); + /// 获取指定类型的主键,按 Flag 属性筛选。 + /// 要查询的类型。 + /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 + public Result> Keys(long flag = 0) where T : IRecord; + + /// 获取具有指定 Key 的记录,并要求记录具有指定的 Flag 属性。 + /// 目标记录的类型。 + /// 目标记录的主键。 + /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 + public Result Get(Type model, string key, long flag = 0); + /// 获取具有指定 Key 的记录,并要求记录具有指定的 Flag 属性。 /// 目标记录的主键。 /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 - public Result Get(string key, long flag = 0) where T : Record; + public Result Get(string key, long flag = 0) where T : IRecord; + + /// 使用指定语句查询,获取查询结果。 + /// 目标记录的类型。 + /// 要执行的 SQL 语句。 + public Result> Query(Type model, string sql); /// 使用指定语句查询,获取查询结果。 - public Result> Query(string sql) where T : Record; + /// 要执行的 SQL 语句。 + public Result> Query(string sql) where T : IRecord; + + /// 查询所有记录。 + /// 目标记录的类型。 + /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 + public Result> Query(Type model, long flag = 0); /// 查询所有记录。 /// 要求目标记录具有的 Flag 属性,当指定 0 时忽略此要求。 - public Result> Query(long flag = 0) where T : Record; + public Result> Query(long flag = 0) where T : IRecord; } diff --git a/Apewer/Source/MySql.cs b/Apewer/Source/MySql.cs index 0cdad6b..50d032b 100644 --- a/Apewer/Source/MySql.cs +++ b/Apewer/Source/MySql.cs @@ -22,7 +22,7 @@ namespace Apewer.Source public class MySql : IDatabase, IOrm, IDisposable { -#region fields & properties + #region fields & properties private const string EmptyString = TextUtility.EmptyString; @@ -70,9 +70,9 @@ namespace Apewer.Source Pass = pass; } -#endregion + #endregion -#region 日志。 + #region 日志。 /// 获取或设置日志记录。 public Logger Logger { get; set; } @@ -83,9 +83,9 @@ namespace Apewer.Source if (logger != null) logger.Error(this, "MySQL", action, ex.GetType().FullName, ex.Message, addtion); } -#endregion + #endregion -#region methods + #region methods private string CombineString() { @@ -244,9 +244,9 @@ namespace Apewer.Source /// public IExecute Execute(string sql) => Execute(sql, null as IEnumerable); -#endregion + #endregion -#region ORM + #region ORM private List FirstColumn(string sql) { @@ -409,13 +409,13 @@ namespace Apewer.Source public string Initialize(Type model) => Initialize(model, out string sql); /// - public string Initialize() where T : Record => Initialize(typeof(T)); + public string Initialize() where T : IRecord => Initialize(typeof(T)); /// public string Initialize(Record model) => (model == null) ? "参数无效。" : Initialize(model.GetType()); /// 插入记录。成功时候返回空字符串,发生异常时返回异常信息。 - public string Insert(Record record) + public string Insert(IRecord record) { if (record == null) return "参数无效。"; record.FixProperties(); @@ -435,7 +435,7 @@ namespace Apewer.Source /// 更新记录,实体中的 Created 和 Key 属性不被更新。成功时返回空字符串,发生异常时返回异常信息。 /// 无法更新拥有 Independent 特性的模型。 /// - public string Update(Record record) + public string Update(IRecord record) { if (record == null) return "参数无效。"; record.FixProperties(); @@ -455,62 +455,24 @@ namespace Apewer.Source } /// - public Result> Query(string sql) where T : Record - { - if (sql.IsEmpty()) return new Result>(new ArgumentException()); - try - { - // 解析模型,抛出 Exception。 - TableStructure.ParseModel(typeof(T)); + public Result> Query(Type model, string sql) => OrmHelper.Query(this, model, sql); - var query = Query(sql) as Query; - var list = query.Fill(); - query.Dispose(); - return new Result>(list); - } - catch (Exception exception) - { - return new Result>(exception); - } - } + /// + public Result> Query(string sql) where T : IRecord => OrmHelper.Query(this, sql); - /// 获取记录。 - public Result Get(string key, long flag = 0) where T : Record + /// 获取所有记录。Flag 为 0 时将忽略 Flag 条件。 + public Result> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => { - var k = TextUtility.SafeKey(key); - if (k.IsEmpty()) new Result(new Exception("参数无效。")); - try - { - var structure = TableStructure.ParseModel(typeof(T)); - var sqlflag = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" `_flag`=", flag.ToString(), " and"); - var sqlkey = TextUtility.Merge(" `_key`='", k, "'"); - var sql = TextUtility.Merge("select * from `", structure.Table, "` where ", sqlflag, sqlkey, " limit 1;"); - var result = Query(sql); - var list = result.Entity; - if (list != null && list.Count > 0) return new Result(list[0]); - return new Result(null, "无结果。"); - } - catch (Exception exception) - { - return new Result(exception); - } - } + 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 : Record + public Result> Query(long flag = 0) where T : IRecord => OrmHelper.Query(this, (tn) => { - try - { - var structure = TableStructure.ParseModel(typeof(T)); - var sqlflag = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where `_flag`=", flag.ToString()); - var sql = TextUtility.Merge("select * from `", structure.Table, "`", sqlflag, "; "); - return Query(sql); - } - catch (Exception exception) - { - return new Result>(exception); - } - } + if (flag == 0) return $"select * from `{tn}`; "; + return $"select * from `{tn}` where `_flag`={flag}; "; + }); /// 获取记录。 /// 要跳过的记录数,可用最小值为 0。 @@ -531,33 +493,29 @@ namespace Apewer.Source } } + /// 获取记录。 + public Result Get(Type model, string key, long flag = 0) => OrmHelper.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 : IRecord => OrmHelper.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) + public Result> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => { - if (model != null) - { - try - { - var type = model; - var tn = TableStructure.ParseModel(type).Table; - var where = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where `_flag`=", flag.ToString()); - var sql = $"select `_key` from `{tn}`{where}; "; - using (var query = Query(sql) as Query) - { - var list = query.ReadColumn((r) => TextUtility.Trim(query.Text(r, 0))); - return new Result>(list); - } - } - catch (Exception ex) - { - return new Result>(ex); - } - } - return new Result>(new Exception("参数无效。")); - } + if (flag == 0) return $"select `_key` from `{tn}`;"; + return $"select `_key` from `{tn}` where `_flag`={flag};"; + }); /// >获取指定类型的主键,按 Flag 属性筛选。 - public Result> Keys(long flag = 0) where T : Record => Keys(typeof(T), flag); + public Result> Keys(long flag = 0) where T : IRecord => Keys(typeof(T), flag); /// 对表添加列,返回错误信息。 /// 记录类型。 @@ -611,9 +569,9 @@ namespace Apewer.Source return error; } -#endregion + #endregion -#region static + #region static /// 对文本转义,符合 SQL 安全性。可根据字段类型限制 UTF-8 字节数,默认为 0 时不限制字节数。 public static string Escape(string text, int bytes = 0) @@ -972,7 +930,7 @@ namespace Apewer.Source return result; } -#endregion + #endregion } diff --git a/Apewer/Source/OrmHelper.cs b/Apewer/Source/OrmHelper.cs new file mode 100644 index 0000000..019dd76 --- /dev/null +++ b/Apewer/Source/OrmHelper.cs @@ -0,0 +1,223 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Apewer.Source +{ + + /// ORM 帮助程序。 + public static class OrmHelper + { + + private static List Convert(List input) where T : IRecord + { + if (input == null) return null; + var output = new List(input.Count); + foreach (var record in input) + { + if (record == null) continue; + var t = (T)record; + output.Add(t); + } + return output; + } + + private static Result Convert(Result input) where T : IRecord + { + if (input == null) return null; + var result = new Result(); + result.Code = input.Code; + result.Exception = input.Exception; + result.Message = input.Message; + if (input.HasEntity) result.Entity = (T)input.Entity; + return result; + } + + private static Result> Convert(Result> input) where T : IRecord + { + if (input == null) return null; + var result = new Result>(); + result.Code = input.Code; + result.Exception = input.Exception; + result.Message = input.Message; + result.Entity = Convert(input.Entity); + return result; + } + + /// 查询记录。 + /// 数据库对象。 + /// 记录模型。 + /// SQL 语句。 + public static Result> Query(IDatabase database, Type model, string sql) + { + if (database == null) return Result>.Error("数据库无效。"); + if (model == null) return Result>.Error("模型类型无效。"); + if (string.IsNullOrEmpty(sql)) return Result>.Error("SQL 语句无效。"); + using (var query = database.Query(sql) as Query) + { + if (query == null) return Result>.Error("查询实例无效。"); + if (query.Exception != null) return Result>.Error(query.Exception); + try + { + var list = query.Fill(model); + return new Result>(list); + } + catch (Exception ex) + { + return Result>.Error(ex); + } + } + } + + /// 查询记录。 + /// 记录模型。 + /// 数据库对象。 + /// SQL 语句。 + public static Result> Query(IDatabase database, string sql) where T : IRecord + { + return Convert(Query(database, typeof(T), sql)); + } + + /// 查询记录。 + /// 数据库对象。 + /// 记录模型。 + /// 生成 SQL 语句的函数,传入参数为表名。 + public static Result> Query(IDatabase database, Type model, Func sqlGetter) + { + if (sqlGetter == null) return Result>.Error("SQL 语句获取函数无效。"); + try + { + var tableName = TableStructure.ParseModel(model).Table; + if (string.IsNullOrEmpty(tableName)) return Result>.Error("表名无效。"); + return Query(database, model, sqlGetter(tableName)); + } + catch (Exception ex) + { + return Result>.Error(ex); + } + } + + /// 查询记录。 + /// 记录模型。 + /// 数据库对象。 + /// 生成 SQL 语句的函数,传入参数为表名。 + public static Result> Query(IDatabase database, Func sqlGetter) where T : IRecord + { + return Convert(Query(database, typeof(T), sqlGetter)); + } + + /// 获取具有指定主键的记录。 + /// 数据库对象。 + /// 记录模型。 + /// 主键。 + /// 生成 SQL 语句的函数,传入参数为表名和主键值。 + public static Result Get(IDatabase database, Type model, string key, Func sqlGetter) + { + if (sqlGetter == null) return Result.Error("SQL 语句获取函数无效。"); + + var safetyKey = TextUtility.SafeKey(key); + if (string.IsNullOrEmpty(safetyKey)) return Result.Error("主键无效。"); + + string tableName; + try + { + tableName = TableStructure.ParseModel(model).Table; + if (string.IsNullOrEmpty(tableName)) return Result.Error("表名无效。"); + } + catch (Exception ex) + { + return new Result(ex); + } + + var sql = sqlGetter(tableName, safetyKey); + var list = Query(database, model, sql); + var result = new Result(); + result.Code = list.Code; + result.Exception = list.Exception; + result.Message = list.Message; + result.Entity = list.Entity.SafeFirst(); + return result; + } + + /// 获取具有指定主键的记录。 + /// 记录模型。 + /// 数据库对象。 + /// 主键。 + /// 生成 SQL 语句的函数,传入参数为表名和主键值。 + public static Result Get(IDatabase database, string key, Func sqlGetter) where T : IRecord + { + return Convert(Get(database, typeof(T), key, sqlGetter)); + } + + /// 获取主键。 + /// 数据库对象。 + /// 记录模型。 + /// 生成 SQL 语句的函数,传入参数为表名。 + public static Result> Keys(IDatabase database, Type model, Func sqlGetter) + { + if (database == null) return Result>.Error("数据库无效。"); + if (model == null) return Result>.Error("模型类型无效。"); + if (sqlGetter == null) return Result>.Error("SQL 语句获取函数无效。"); + + var tableStructure = null as TableStructure; + try + { + tableStructure = TableStructure.ParseModel(model); + } + catch (Exception ex) + { + return Result>.Error(ex); + } + + var tableName = tableStructure.Table; + if (string.IsNullOrEmpty(tableName)) return Result>.Error("表名无效。"); + + // 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 + { + query = database.Query(sql); + if (query == null) return Result>.Error("查询实例无效。"); + + var list = new List(query.Rows); + for (var r = 0; r < query.Rows; r++) + { + var key = TextUtility.SafeKey(query.Text(r)); + if (string.IsNullOrEmpty(key)) continue; + list.Add(key); + } + query.Dispose(); + list.Capacity = list.Count; + return new Result>(list); + } + catch (Exception ex) + { + RuntimeUtility.Dispose(query); + return Result>.Error(ex); + } + } + + /// 获取主键。 + /// 记录模型。 + /// 数据库对象。 + /// 生成 SQL 语句的函数,传入参数为表名。 + public static Result> Keys(IDatabase database, Func sqlGetter) where T : IRecord + { + return Keys(database, typeof(T), sqlGetter); + } + + } + +} diff --git a/Apewer/Source/Query.cs b/Apewer/Source/Query.cs index 6805b34..69362f7 100644 --- a/Apewer/Source/Query.cs +++ b/Apewer/Source/Query.cs @@ -395,10 +395,14 @@ namespace Apewer.Source /// /// - public List Fill() where T : Record + public List Fill() where T : IRecord => Fill(typeof(T)) as List; + + /// + /// + public List Fill(Type model) { - var list = new List(); - var type = typeof(T); + var type = model; + var list = new List(); for (int r = 0; r < Rows; r++) { list.Capacity = Rows; @@ -486,7 +490,9 @@ namespace Apewer.Source catch { } } } - list.Add((T)entity); + + var iRecord = entity as IRecord; + if (iRecord != null) list.Add(iRecord); } return list; } diff --git a/Apewer/Source/Record.cs b/Apewer/Source/Record.cs index d011cf1..5239b5b 100644 --- a/Apewer/Source/Record.cs +++ b/Apewer/Source/Record.cs @@ -50,7 +50,7 @@ namespace Apewer.Source public virtual string Key { get { return _key; } set { _key = TextModifier.Compact(value, KeyLength); } } /// 重置主键。 - internal static void ResetKey(Record record) + internal static void ResetKey(IRecord record) { if (record == null) return; record.Key = GenerateKey(); @@ -59,7 +59,7 @@ namespace Apewer.Source /// 生成新主键。 public static string GenerateKey() => Guid.NewGuid().ToString().ToLower().Replace("-", ""); - internal static void FixProperties(Record record) + internal static void FixProperties(IRecord record) { if (record == null) return; diff --git a/Apewer/Source/SqlServer.cs b/Apewer/Source/SqlServer.cs index ccd6b59..1801edc 100644 --- a/Apewer/Source/SqlServer.cs +++ b/Apewer/Source/SqlServer.cs @@ -383,6 +383,9 @@ namespace Apewer.Source return list; } + /// 创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。 + public string Initialize() where T : IRecord => Initialize(typeof(T)); + /// 创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。 public string Initialize(Record model) => model == null ? "参数无效。" : Initialize(model); @@ -472,7 +475,7 @@ namespace Apewer.Source } /// 插入记录。成功时候返回空字符串,发生异常时返回异常信息。 - public string Insert(Record record) + public string Insert(IRecord record) { if (record == null) return "参数无效。"; var type = record.GetType(); @@ -496,7 +499,7 @@ namespace Apewer.Source /// 更新记录,实体中的 Created 和 Key 属性不被更新。成功时返回空字符串,发生异常时返回异常信息。 /// 无法更新拥有 Independent 特性的模型。 /// - public string Update(Record record) + public string Update(IRecord record) { if (record == null) return "参数无效。"; var type = record.GetType(); @@ -520,97 +523,49 @@ namespace Apewer.Source return execute.Error; } - /// 获取具有指定 Key 的记录。 - public Result Get(string key, long flag = 0) where T : Record + /// 获取按指定语句查询到的所有记录。 + public Result> Query(Type model, string sql) => OrmHelper.Query(this, model, sql); + + /// 获取按指定语句查询到的所有记录。 + public Result> Query(string sql) where T : IRecord => OrmHelper.Query(this, sql); + + /// 获取记录。 + public Result> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => { - var k = TextUtility.SafeKey(key); - if (TextUtility.IsBlank(k) == false) - { - try - { - var type = typeof(T); - var ts = TableStructure.ParseModel(type); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" _flag=", flag.ToString(), " and"); - var query = (Query)Query(TextUtility.Merge("select top 1 * from [", ts.Table, "] where", f, " _key='", k, "'; ")); - var list = query.Fill(); - query.Dispose(); - if (list.Count > 0) return new Result(list[0]); - } - catch (Exception ex) - { - return new Result(ex); - } - } - return new Result(new Exception("参数无效。")); - } + if (flag == 0) return $"select * from [{tn}]; "; + return $"select * from [{tn}] where _flag={flag}; "; + }); /// 获取记录。 - public Result> Query(long flag = 0) where T : Record + public Result> Query(long flag = 0) where T : IRecord => OrmHelper.Query(this, (tn) => { - try - { - var type = typeof(T); - var ts = TableStructure.ParseModel(type); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where _flag=", flag.ToString()); - var query = (Query)Query(TextUtility.Merge("select * from [", ts.Table, "]", f, "; ")); - var list = query.Fill(); - query.Dispose(); - return new Result>(list); - } - catch (Exception ex) - { - return new Result>(ex); - } - } + if (flag == 0) return $"select * from [{tn}]; "; + return $"select * from [{tn}] where _flag={flag}; "; + }); - /// 获取按指定语句查询到的所有记录。 - public Result> Query(string sql) where T : Record + /// 获取具有指定 Key 的记录。 + public Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => { - var query = (Query)Query(sql); - if (query.Exception == null) - { - var list = query.Fill(); - query.Dispose(); - return new Result>(list); - } - else - { - var result = new Result>(query.Exception); - query.Dispose(); - return result; - } - } + 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 : IRecord => OrmHelper.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) + public Result> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => { - if (model != null) - { - try - { - var type = model; - var list = new List(); - var ts = TableStructure.ParseModel(type); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where _flag=", flag.ToString()); - var query = (Query)Query(TextUtility.Merge("select _key from [", ts.Table, "]", f, "; ")); - for (var r = 0; r < query.Rows; r++) - { - var cell = TextUtility.Trim(query.Text(r)); - if (cell.Length > 0) list.Add(cell); - } - query.Dispose(); - return new Result>(list); - } - catch (Exception ex) - { - return new Result>(ex); - } - } - return new Result>(new Exception("参数无效。")); - } + if (flag == 0) return $"select _key from [{tn}]; "; + return $"select _key from [{tn}] where _flag={flag}; "; + }); /// 查询有效的 Key 值。 - public Result> Keys(long flag = 0) where T : Record => Keys(typeof(T), flag); + public Result> Keys(long flag = 0) where T : IRecord => Keys(typeof(T), flag); #endregion diff --git a/Apewer/Source/Sqlite.cs b/Apewer/Source/Sqlite.cs index d3a9c88..2c82e1d 100644 --- a/Apewer/Source/Sqlite.cs +++ b/Apewer/Source/Sqlite.cs @@ -428,7 +428,7 @@ namespace Apewer.Source public string Initialize(Record model) => model == null ? "参数无效。" : Initialize(model.GetType()); /// 创建表,不修改已存在表。成功时返回空字符串,发生异常时返回异常信息。 - public string Initialize() where T : Record => Initialize(typeof(T)); + public string Initialize() where T : IRecord => Initialize(typeof(T)); /// 创建表,不修改已存在表。当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。 public string Initialize(Type model) @@ -478,7 +478,7 @@ namespace Apewer.Source } /// 插入记录。成功时候返回空字符串,发生异常时返回异常信息。 - public string Insert(Record record) + public string Insert(IRecord record) { if (record == null) return "参数无效。"; record.FixProperties(); @@ -497,7 +497,7 @@ namespace Apewer.Source } /// 更新记录,实体中的 Created 和 Key 属性不被更新。成功时返回空字符串,发生异常时返回异常信息。 - public string Update(Record record) + public string Update(IRecord record) { if (record == null) return "参数无效。"; record.FixProperties(); @@ -516,98 +516,49 @@ namespace Apewer.Source return execute.Error; } - /// 获取具有指定 Key 的记录。 - public Result Get(string key) where T : Record => Get(key, 0); + /// 获取按指定语句查询到的所有记录。 + public Result> Query(Type model, string sql) => OrmHelper.Query(this, model, sql); - /// 获取具有指定 Key 的记录。 - public Result Get(string key, long flag) where T : Record - { - var k = TextUtility.SafeKey(key); - if (TextUtility.IsBlank(k) == false) - { - try - { - var ts = TableStructure.ParseModel(typeof(T)); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" _flag=", flag.ToString(), " and"); - var query = (Query)Query(TextUtility.Merge("select * from [", ts.Table, "] where", f, " _key='", k, "' limit 1; ")); - var list = query.Fill(); - query.Dispose(); - if (list.Count > 0) return new Result(list[0]); - } - catch (Exception ex) - { - return new Result(ex); - } - } - return new Result(new Exception("参数无效。")); - } + /// 获取按指定语句查询到的所有记录。 + public Result> Query(string sql) where T : IRecord => OrmHelper.Query(this, sql); /// 查询多条记录。 - public Result> Query() where T : Record => Query(0); + public Result> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + { + if (flag == 0) return $"select * from [{tn}]; "; + return $"select * from [{tn}] where _flag={flag}; "; + }); /// 查询多条记录。 - public Result> Query(long flag) where T : Record + public Result> Query(long flag = 0) where T : IRecord => OrmHelper.Query(this, (tn) => { - try - { - var ts = TableStructure.ParseModel(typeof(T)); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where _flag=", flag.ToString()); - var query = Query(TextUtility.Merge("select * from [", ts.Table, "]", f, "; ")) as Query; - var list = query.Fill(); - query.Dispose(); - return new Result>(list); - } - catch (Exception ex) - { - return new Result>(ex); - } - } + if (flag == 0) return $"select * from [{tn}]; "; + return $"select * from [{tn}] where _flag={flag}; "; + }); - /// 获取按指定语句查询到的所有记录。 - public Result> Query(string sql) where T : Record + /// 获取具有指定 Key 的记录。 + public Result Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => { - using (var query = Query(sql) as Query) - { - if (query.Exception == null) return new Result>(query.Fill()); - else return new Result>(query.Exception); - } - } - - /// 查询所有有效的 Key 值。 - public Result> Keys() where T : Record => Keys(0); - - /// 查询所有有效的 Key 值。 - public Result> Keys(long flag) where T : Record => Keys(typeof(T), flag); + 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> Keys(Type model) => Keys(model, 0); + /// 获取具有指定 Key 的记录。 + public Result Get(string key, long flag) where T : IRecord => OrmHelper.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; "; + }); - /// 查询所有有效的 Key 值。 - public Result> Keys(Type model, long flag) + /// 获取指定类型的主键,按 Flag 属性筛选。 + public Result> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => { - if (model != null) - { - try - { - var list = new List(); - var ts = TableStructure.ParseModel((Type)model); - var f = (flag == 0) ? TextUtility.EmptyString : TextUtility.Merge(" where _flag=", flag.ToString()); - var query = (Query)Query(TextUtility.Merge("select _key from [", ts.Table, "]", f, "; ")); - for (var r = 0; r < query.Rows; r++) - { - var cell = TextUtility.Trim(query.Text(r)); - if (cell.Length > 0) list.Add(cell); - } - query.Dispose(); - return new Result>(list); - } - catch (Exception ex) - { - return new Result>(ex); - } - } - return new Result>(new Exception("参数无效。")); - } + if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; "; + return $"select _key from [{tn}]; "; + }); + + /// >获取指定类型的主键,按 Flag 属性筛选。 + public Result> Keys(long flag = 0) where T : IRecord => Keys(typeof(T), flag); #endregion diff --git a/Apewer/Source/TableStructure.cs b/Apewer/Source/TableStructure.cs index a89b28c..3d2f91b 100644 --- a/Apewer/Source/TableStructure.cs +++ b/Apewer/Source/TableStructure.cs @@ -316,7 +316,7 @@ namespace Apewer.Source return lower; } - private static IDataParameter GenerateDataParameter(Record entity, ColumnAttribute attribute, CreateDataParameterCallback callback) + private static IDataParameter GenerateDataParameter(IRecord entity, ColumnAttribute attribute, CreateDataParameterCallback callback) { var property = attribute.Property; if (property == null) return null; @@ -371,7 +371,7 @@ namespace Apewer.Source /// 生成 IDataParameter 列表,用于 Insert 或 Update。 /// - public List CreateDataParameters(Record entity, CreateDataParameterCallback callback, params string[] excluded) + public List CreateDataParameters(IRecord entity, CreateDataParameterCallback callback, params string[] excluded) { if (entity == null) throw new ArgumentNullException(nameof(entity)); if (callback == null) throw new ArgumentNullException(nameof(excluded)); diff --git a/Apewer/_Common.props b/Apewer/_Common.props index c0de5ae..bae7c3d 100644 --- a/Apewer/_Common.props +++ b/Apewer/_Common.props @@ -1,5 +1,7 @@  + + true @@ -11,7 +13,7 @@ Apewer Libraries - 6.3.5 + 6.3.6 @@ -25,4 +27,93 @@ DEBUG;TRACE;$(DefineConstants);$(AdditionalConstants) + + + $(AssemblyName) - .NET Standard 2.1 + NETSTD;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) + + + + + $(AssemblyName) - .NET Standard 2.0 + NETSTD;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) + + + + + $(AssemblyName) - .NET Framework 2.0 + NETFX;NET20;$(DefineConstants);$(AdditionalConstants) + + + + + + + + + + + + + + + $(AssemblyName) - .NET Framework 4.0 + NETFX;NET40;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) + + + + + + + + + + + + + + + + + + + + + + $(AssemblyName) - .NET Framework 4.6.1 + NETFX;NET461;NET45;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) + + + + + + + + + + + + + + + + + + + + + + $(AssemblyName) - .NET Core 3.1 + NETCORE;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants) + + + + + + + + + + + \ No newline at end of file diff --git a/Apewer/_Extensions.cs b/Apewer/_Extensions.cs index 80ec493..90210a6 100644 --- a/Apewer/_Extensions.cs +++ b/Apewer/_Extensions.cs @@ -492,10 +492,10 @@ public static class Extensions #region Source /// 为记录的 Key 属性设置新值。 - public static void ResetKey(this Record @this) => Record.ResetKey(@this); + public static void ResetKey(this IRecord @this) => Record.ResetKey(@this); /// 修补基本属性。 - public static void FixProperties(this Record @this) => Record.FixProperties(@this); + public static void FixProperties(this IRecord @this) => Record.FixProperties(@this); /// public static DateTime DateTime(this IQuery @this, int row, string column) => Query.DateTime(@this, row, column); diff --git a/ChangeLog.md b/ChangeLog.md index bf8219a..3328adf 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,9 @@  ### 最新提交 +### 6.3.6 +- Source:增加 OrmHelper,并统一了 ORM 方法的实现。 + ### 6.3.5 - Source:FixProperties 不再修改 Flag。