Browse Source

重载 Column 和 Cell 方法,增加 getter 参数自定义内容转换。

master
Elivo 4 weeks ago
parent
commit
097121e92f
  1. 74
      Apewer/Source/SourceUtility.cs

74
Apewer/Source/SourceUtility.cs

@ -470,60 +470,80 @@ namespace Apewer.Source
#region Query
/// <summary>简单查询:取结果中第 0 列所有单元格的文本形式,可指定查询后关闭服务器连接,返回结果中不包含无效文本。</summary>
/// <summary>简单查询:取结果中第 0 列所有单元格的文本形式,返回结果中不包含无效文本和空文本。</summary>
/// <param name="source">数据库客户端。</param>
/// <param name="sql">用于查询的 SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <exception cref="SqlException"></exception>
public static string[] Column(this IDbAdo source, string sql, object parameters = null)
public static string[] Column(this IDbAdo source, string sql, object parameters = null) => Column(source, sql, parameters, TextUtility.Text).FindAll(x => x.NotEmpty());
/// <summary>简单查询:取结果中第 0 列所有单元格。</summary>
/// <param name="source">数据库客户端。</param>
/// <param name="sql">用于查询的 SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getter">获取单元格内容的程序。value 有可能是 NULL 值,需要 getter 自行处理。</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static T[] Column<T>(this IDbAdo source, string sql, object parameters, Func<object, T> getter)
{
if (source == null) return new string[0];
if (source == null) throw new ArgumentNullException(nameof(source));
if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql));
if (getter == null) throw new ArgumentNullException(nameof(getter));
var pool = null as string[];
var rows = 0;
var count = 0;
using (var query = source.Query(sql, parameters))
{
if (query == null) throw new SqlException("方法 IDbAdo.Query 返回了 NULL 值。");
if (!query.Success) throw new SqlException(query, sql);
rows = query.Rows;
if (rows < 1) return new string[0];
var rows = query.Rows;
if (rows < 1) return new T[0];
pool = new string[rows];
var list = new List<T>();
list.Capacity = rows;
for (int i = 0; i < rows; i++)
{
var cell = TextUtility.Trim(query.Text(i, 0));
if (string.IsNullOrEmpty(cell)) continue;
var value = query.Value(i, 0);
if (value != null && value.Equals(DBNull.Value)) value = null;
pool[count] = cell;
count++;
// 此处 value 有可能是 null
var item = getter.Invoke(value);
list.Add(item);
}
return list.ToArray();
}
if (count < 1) return new string[0];
if (count == rows) return pool;
var array = new string[count];
Array.Copy(pool, 0, array, 0, count);
return array;
}
/// <summary>简单查询:取结果中第 0 行、第 0 列单元格中的文本,可指定查询后关闭服务器连接。</summary>
/// <param name="dbClient">数据库客户端。</param>
/// <summary>简单查询:取结果中第 0 行、第 0 列单元格中的文本。</summary>
/// <param name="source">数据库客户端。</param>
/// <param name="sql">用于查询的 SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static string Cell(this IDbAdo dbClient, string sql, object parameters = null)
public static string Cell(this IDbAdo source, string sql, object parameters = null) => Cell(source, sql, parameters, TextUtility.Text);
/// <summary>简单查询:取结果中第 0 行、第 0 列单元格。</summary>
/// <param name="source">数据库客户端。</param>
/// <param name="sql">用于查询的 SQL 语句。</param>
/// <param name="parameters">SQL 参数。</param>
/// <param name="getter">获取单元格内容的程序。value 有可能是 NULL 值,需要 getter 自行处理。</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="SqlException"></exception>
public static T Cell<T>(this IDbAdo source, string sql, object parameters, Func<object, T> getter)
{
if (dbClient == null) throw new ArgumentNullException(nameof(dbClient));
if (source == null) throw new ArgumentNullException(nameof(source));
if (sql.IsEmpty()) throw new ArgumentNullException(nameof(sql));
if (getter == null) throw new ArgumentNullException(nameof(getter));
using (var query = dbClient.Query(sql, parameters))
using (var query = source.Query(sql, parameters))
{
if (query == null) throw new SqlException("方法 IDbAdo.Query 返回了 NULL 值。");
if (!query.Success) throw new SqlException(query, sql);
var value = TextUtility.Trim(query.Text(0, 0));
return value;
var value = query.Value(0, 0);
if (value != null && value.Equals(DBNull.Value)) value = null;
// 此处 value 有可能是 null
var result = getter.Invoke(value);
return result;
}
}

Loading…
Cancel
Save