From b88d002715e2f52e69d5060f51f69b8f5afa0afe Mon Sep 17 00:00:00 2001 From: Elivo Date: Thu, 19 Mar 2026 22:30:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=A9=E5=B1=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8CQuery=EF=BC=8C=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=85=83=E7=BB=84=E7=BB=84=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Source/SourceUtility.cs | 349 ++++++++++++++++++++++++++++++++- Apewer/Source/SqlException.cs | 21 +- 2 files changed, 363 insertions(+), 7 deletions(-) diff --git a/Apewer/Source/SourceUtility.cs b/Apewer/Source/SourceUtility.cs index d5a7834..fb6adf1 100644 --- a/Apewer/Source/SourceUtility.cs +++ b/Apewer/Source/SourceUtility.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Reflection; +using System.Runtime.InteropServices.ComTypes; using System.Text; namespace Apewer.Source @@ -598,7 +599,6 @@ namespace Apewer.Source } } - /// 查询。 /// 数据库连接。 /// SQL 语句。 @@ -629,6 +629,353 @@ namespace Apewer.Source } } +#if !NET20 + + /// 执行查询,将结果输出为元组数组。 + /// 元组中的值类型。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, Func getValue) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue == null) throw new ArgumentNullException(nameof(getValue)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var value1 = getValue(query.Value(i, 0)); + tuples[i] = new Tuple(value1); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + tuples[i] = new Tuple(v1, v2); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + tuples[i] = new Tuple(v1, v2, v3); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 获取第四个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3, + Func getValue4) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + if (getValue4 == null) throw new ArgumentNullException(nameof(getValue4)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + var v4 = getValue4(query.Value(i, 3)); + tuples[i] = new Tuple(v1, v2, v3, v4); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 获取第四个值的回调函数。 + /// 获取第五个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3, + Func getValue4, + Func getValue5) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + if (getValue4 == null) throw new ArgumentNullException(nameof(getValue4)); + if (getValue5 == null) throw new ArgumentNullException(nameof(getValue5)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + var v4 = getValue4(query.Value(i, 3)); + var v5 = getValue5(query.Value(i, 4)); + tuples[i] = new Tuple(v1, v2, v3, v4, v5); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 获取第四个值的回调函数。 + /// 获取第五个值的回调函数。 + /// 获取第六个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3, + Func getValue4, + Func getValue5, + Func getValue6) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + if (getValue4 == null) throw new ArgumentNullException(nameof(getValue4)); + if (getValue5 == null) throw new ArgumentNullException(nameof(getValue5)); + if (getValue6 == null) throw new ArgumentNullException(nameof(getValue6)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + var v4 = getValue4(query.Value(i, 3)); + var v5 = getValue5(query.Value(i, 4)); + var v6 = getValue6(query.Value(i, 5)); + tuples[i] = new Tuple(v1, v2, v3, v4, v5, v6); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 获取第四个值的回调函数。 + /// 获取第五个值的回调函数。 + /// 获取第六个值的回调函数。 + /// 获取第七个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3, + Func getValue4, + Func getValue5, + Func getValue6, + Func getValue7) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + if (getValue4 == null) throw new ArgumentNullException(nameof(getValue4)); + if (getValue5 == null) throw new ArgumentNullException(nameof(getValue5)); + if (getValue6 == null) throw new ArgumentNullException(nameof(getValue6)); + if (getValue7 == null) throw new ArgumentNullException(nameof(getValue7)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + var v4 = getValue4(query.Value(i, 3)); + var v5 = getValue5(query.Value(i, 4)); + var v6 = getValue6(query.Value(i, 5)); + var v7 = getValue7(query.Value(i, 6)); + tuples[i] = new Tuple(v1, v2, v3, v4, v5, v6, v7); + } + return tuples; + } + } + + /// 执行查询,将结果输出为元组数组。 + /// 数据库连接。 + /// SQL 语句。 + /// SQL 参数。 + /// 获取第一个值的回调函数。 + /// 获取第二个值的回调函数。 + /// 获取第三个值的回调函数。 + /// 获取第四个值的回调函数。 + /// 获取第五个值的回调函数。 + /// 获取第六个值的回调函数。 + /// 获取第七个值的回调函数。 + /// 获取第八个值的回调函数。 + /// 由元组组成的数组。 + /// + /// + public static Tuple[] Query(this IDbAdo dbClient, string sql, object parameters, + Func getValue1, + Func getValue2, + Func getValue3, + Func getValue4, + Func getValue5, + Func getValue6, + Func getValue7, + Func getValue8) + { + if (dbClient == null) throw new ArgumentNullException(nameof(dbClient)); + if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql)); + if (getValue1 == null) throw new ArgumentNullException(nameof(getValue1)); + if (getValue2 == null) throw new ArgumentNullException(nameof(getValue2)); + if (getValue3 == null) throw new ArgumentNullException(nameof(getValue3)); + if (getValue4 == null) throw new ArgumentNullException(nameof(getValue4)); + if (getValue5 == null) throw new ArgumentNullException(nameof(getValue5)); + if (getValue6 == null) throw new ArgumentNullException(nameof(getValue6)); + if (getValue7 == null) throw new ArgumentNullException(nameof(getValue7)); + if (getValue8 == null) throw new ArgumentNullException(nameof(getValue8)); + + using (var query = dbClient.Query(sql, parameters)) + { + if (!query.Success) throw new SqlException(query, sql); + + var tuples = new Tuple[query.Rows]; + for (var i = 0; i < query.Rows; i++) + { + var v1 = getValue1(query.Value(i, 0)); + var v2 = getValue2(query.Value(i, 1)); + var v3 = getValue3(query.Value(i, 2)); + var v4 = getValue4(query.Value(i, 3)); + var v5 = getValue5(query.Value(i, 4)); + var v6 = getValue6(query.Value(i, 5)); + var v7 = getValue7(query.Value(i, 6)); + var v8 = getValue8(query.Value(i, 7)); + tuples[i] = new Tuple(v1, v2, v3, v4, v5, v6, v7, v8); + } + return tuples; + } + } + +#endif #endregion diff --git a/Apewer/Source/SqlException.cs b/Apewer/Source/SqlException.cs index d35d8f0..47679e5 100644 --- a/Apewer/Source/SqlException.cs +++ b/Apewer/Source/SqlException.cs @@ -14,6 +14,7 @@ namespace Apewer.Source string _msg = null; string _sql = null; + object _parameters = null; /// 获取描述当前异常的消息。 public override string Message { get => _msg; } @@ -21,20 +22,25 @@ namespace Apewer.Source /// 获取引发异常的 SQL 语句。 public string Statement { get => _sql; } + /// 获取引发异常的 SQL 参数。 + public object Parameters { get => _parameters; } + /// 初始化 类的新实例。 /// 描述当前异常的消息。 /// 附带 SQL 语句。 - public SqlException(string message, string statement = null) + /// 附带 SQL 参数。 + public SqlException(string message, string statement = null, object parameters = null) { _msg = string.IsNullOrEmpty(message) ? EmptyMessage : message; - _sql = statement; + _parameters = parameters; } /// 初始化 类的新实例。 /// 用于获取消息的查询结果。 /// 附带 SQL 语句。 - public SqlException(IQuery query, string statement = null) + /// 附带 SQL 参数。 + public SqlException(IQuery query, string statement = null, object parameters = null) { if (query == null) { @@ -44,15 +50,17 @@ namespace Apewer.Source } _msg = query.Message; - if (string.IsNullOrEmpty(_msg)) _msg = EmptyMessage; + if (string.IsNullOrEmpty(Message)) _msg = EmptyMessage; _sql = statement; + _parameters = parameters; } /// 初始化 类的新实例。 /// 用于获取消息的执行结果。 /// 附带 SQL 语句。 - public SqlException(IExecute execute, string statement = null) + /// 附带 SQL 参数。 + public SqlException(IExecute execute, string statement = null, object parameters = null) { if (execute == null) { @@ -61,9 +69,10 @@ namespace Apewer.Source } _msg = execute.Message; - if (string.IsNullOrEmpty(_msg)) _msg = EmptyMessage; + if (string.IsNullOrEmpty(Message)) _msg = EmptyMessage; _sql = statement; + _parameters = parameters; } }