Browse Source

增加扩展方法,Query<T...>,返回元组组数。

master
Elivo 1 month ago
parent
commit
b88d002715
  1. 349
      Apewer/Source/SourceUtility.cs
  2. 21
      Apewer/Source/SqlException.cs

349
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
}
}
/// <summary>查询。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
@ -629,6 +629,353 @@ namespace Apewer.Source
}
}
#if !NET20
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <typeparam name="T">元组中的值类型。</typeparam>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue">获取第一个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T>[] Query<T>(this IDbAdo dbClient, string sql, object parameters, Func<object, T> 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<T>[query.Rows];
for (var i = 0; i < query.Rows; i++)
{
var value1 = getValue(query.Value(i, 0));
tuples[i] = new Tuple<T>(value1);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2>[] Query<T1, T2>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> 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<T1, T2>[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<T1, T2>(v1, v2);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3>[] Query<T1, T2, T3>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> 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<T1, T2, T3>[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<T1, T2, T3>(v1, v2, v3);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <param name="getValue4">获取第四个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3, T4>[] Query<T1, T2, T3, T4>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> getValue3,
Func<object, T4> 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<T1, T2, T3, T4>[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<T1, T2, T3, T4>(v1, v2, v3, v4);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <param name="getValue4">获取第四个值的回调函数。</param>
/// <param name="getValue5">获取第五个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3, T4, T5>[] Query<T1, T2, T3, T4, T5>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> getValue3,
Func<object, T4> getValue4,
Func<object, T5> 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<T1, T2, T3, T4, T5>[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<T1, T2, T3, T4, T5>(v1, v2, v3, v4, v5);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <param name="getValue4">获取第四个值的回调函数。</param>
/// <param name="getValue5">获取第五个值的回调函数。</param>
/// <param name="getValue6">获取第六个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3, T4, T5, T6>[] Query<T1, T2, T3, T4, T5, T6>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> getValue3,
Func<object, T4> getValue4,
Func<object, T5> getValue5,
Func<object, T6> 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<T1, T2, T3, T4, T5, T6>[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<T1, T2, T3, T4, T5, T6>(v1, v2, v3, v4, v5, v6);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <param name="getValue4">获取第四个值的回调函数。</param>
/// <param name="getValue5">获取第五个值的回调函数。</param>
/// <param name="getValue6">获取第六个值的回调函数。</param>
/// <param name="getValue7">获取第七个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3, T4, T5, T6, T7>[] Query<T1, T2, T3, T4, T5, T6, T7>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> getValue3,
Func<object, T4> getValue4,
Func<object, T5> getValue5,
Func<object, T6> getValue6,
Func<object, T7> 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<T1, T2, T3, T4, T5, T6, T7>[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<T1, T2, T3, T4, T5, T6, T7>(v1, v2, v3, v4, v5, v6, v7);
}
return tuples;
}
}
/// <summary>执行查询,将结果输出为元组数组。</summary>
/// <param name="dbClient">数据库连接。</param>
/// <param name="sql">SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getValue1">获取第一个值的回调函数。</param>
/// <param name="getValue2">获取第二个值的回调函数。</param>
/// <param name="getValue3">获取第三个值的回调函数。</param>
/// <param name="getValue4">获取第四个值的回调函数。</param>
/// <param name="getValue5">获取第五个值的回调函数。</param>
/// <param name="getValue6">获取第六个值的回调函数。</param>
/// <param name="getValue7">获取第七个值的回调函数。</param>
/// <param name="getValue8">获取第八个值的回调函数。</param>
/// <returns>由元组组成的数组。</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static Tuple<T1, T2, T3, T4, T5, T6, T7, T8>[] Query<T1, T2, T3, T4, T5, T6, T7, T8>(this IDbAdo dbClient, string sql, object parameters,
Func<object, T1> getValue1,
Func<object, T2> getValue2,
Func<object, T3> getValue3,
Func<object, T4> getValue4,
Func<object, T5> getValue5,
Func<object, T6> getValue6,
Func<object, T7> getValue7,
Func<object, T8> 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<T1, T2, T3, T4, T5, T6, T7, T8>[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<T1, T2, T3, T4, T5, T6, T7, T8>(v1, v2, v3, v4, v5, v6, v7, v8);
}
return tuples;
}
}
#endif
#endregion

21
Apewer/Source/SqlException.cs

@ -14,6 +14,7 @@ namespace Apewer.Source
string _msg = null;
string _sql = null;
object _parameters = null;
/// <summary>获取描述当前异常的消息。</summary>
public override string Message { get => _msg; }
@ -21,20 +22,25 @@ namespace Apewer.Source
/// <summary>获取引发异常的 SQL 语句。</summary>
public string Statement { get => _sql; }
/// <summary>获取引发异常的 SQL 参数。</summary>
public object Parameters { get => _parameters; }
/// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
/// <param name="message">描述当前异常的消息。</param>
/// <param name="statement">附带 SQL 语句。</param>
public SqlException(string message, string statement = null)
/// <param name="parameters">附带 SQL 参数。</param>
public SqlException(string message, string statement = null, object parameters = null)
{
_msg = string.IsNullOrEmpty(message) ? EmptyMessage : message;
_sql = statement;
_parameters = parameters;
}
/// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
/// <param name="query">用于获取消息的查询结果。</param>
/// <param name="statement">附带 SQL 语句。</param>
public SqlException(IQuery query, string statement = null)
/// <param name="parameters">附带 SQL 参数。</param>
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;
}
/// <summary>初始化 <see cref="SqlException"/> 类的新实例。</summary>
/// <param name="execute">用于获取消息的执行结果。</param>
/// <param name="statement">附带 SQL 语句。</param>
public SqlException(IExecute execute, string statement = null)
/// <param name="parameters">附带 SQL 参数。</param>
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;
}
}

Loading…
Cancel
Save