diff --git a/Apewer/Apewer.props b/Apewer/Apewer.props index 1b888ef..9aad0c6 100644 --- a/Apewer/Apewer.props +++ b/Apewer/Apewer.props @@ -9,7 +9,7 @@ Apewer Apewer Libraries - 6.7.4 + 6.7.5 diff --git a/Apewer/Source/IQuery.cs b/Apewer/Source/IQuery.cs index 33e5e19..2d768a4 100644 --- a/Apewer/Source/IQuery.cs +++ b/Apewer/Source/IQuery.cs @@ -31,17 +31,6 @@ namespace Apewer.Source #region 读取 DateTime 对象。 - /// 获取默认表中第 0 行、第 0 列的单元格内容。 - object Value(); - - /// 获取默认表中指定行中第 0 列的内容。 - /// 行索引,从 0 开始。 - object Value(int rowIndex); - - /// 获取默认表中第 0 行指定列的内容。 - /// 列名称/字段名称,此名称不区分大小写。 - object Value(string columnName); - /// 获取默认表中指定单元的内容。 /// 行索引,从 0 开始。 /// 列索引,从 0 开始。 diff --git a/Apewer/Source/Query.cs b/Apewer/Source/Query.cs index 3db51bf..d1fc953 100644 --- a/Apewer/Source/Query.cs +++ b/Apewer/Source/Query.cs @@ -91,17 +91,6 @@ namespace Apewer.Source #region Value - /// 获取默认表中第 0 行、第 0 列的单元格内容。 - public object Value() => Table.Value(0, 0); - - /// 获取默认表中指定行中第 0 列的内容。 - /// 行索引,从 0 开始。 - public object Value(int rowIndex) => Table.Value(rowIndex, 0); - - /// 获取默认表中第 0 行指定列的内容。 - /// 列名称/字段名称,此名称不区分大小写。 - public object Value(string columnName) => Table.Value(0, columnName); - /// 获取默认表中指定单元格的内容。 /// 行索引,从 0 开始。 /// 列索引,从 0 开始。 diff --git a/Apewer/Source/SourceUtility.cs b/Apewer/Source/SourceUtility.cs index cc49b54..55d2c42 100644 --- a/Apewer/Source/SourceUtility.cs +++ b/Apewer/Source/SourceUtility.cs @@ -416,14 +416,16 @@ namespace Apewer.Source pool = new string[rows]; for (int i = 0; i < rows; i++) { - var cell = TextUtility.Trim(query.Text(i)); + var cell = TextUtility.Trim(query.Text(i, 0)); if (string.IsNullOrEmpty(cell)) continue; + pool[count] = cell; count++; } } if (count < 1) return new string[0]; + if (count == rows) return pool; var array = new string[count]; Array.Copy(pool, 0, array, 0, count); @@ -443,7 +445,7 @@ namespace Apewer.Source using (var query = dbClient.Query(sql)) { if (!query.Success) throw new SqlException(query, sql); - var value = TextUtility.Trim(query.Text()); + var value = TextUtility.Trim(query.Text(0, 0)); return value; } } @@ -484,6 +486,42 @@ namespace Apewer.Source } } + /// 执行 SELECT 语句,获取查询结果。 + /// 数据库连接。 + /// 事务。 + /// SQL 语句。 + /// 参数。 + /// 超时秒数。 + /// 查询结果。 + public static DataTable Query(this IDbConnection connection, IDbTransaction transaction, string sql, IEnumerable parameters = null, int timeout = 3600) + { + if (connection == null) throw new ArgumentNullException(nameof(connection)); + if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql)); + + if (connection.State != ConnectionState.Open) connection.Open(); + using (var command = connection.CreateCommand()) + { + if (transaction != null) command.Transaction = transaction; + if (timeout > 0) command.CommandTimeout = timeout; + command.CommandText = sql; + if (parameters != null) + { + foreach (var parameter in parameters) + { + if (parameter == null) continue; + command.Parameters.Add(parameter); + } + } + + using (var reader = command.ExecuteReader()) + { + var table = new DataTable(); + table.Load(reader); + return table; + } + } + } + #endregion #region Execute @@ -522,6 +560,38 @@ namespace Apewer.Source } } + /// 执行 SQL 语句,获取影响的行数。 + /// 数据库连接。 + /// 事务。 + /// SQL 语句。 + /// 参数。 + /// 超时秒数。 + /// 行数。 + public static int Execute(this IDbConnection connection, IDbTransaction transaction, string sql, IEnumerable parameters = null, int timeout = 3600) + { + if (connection == null) throw new ArgumentNullException(nameof(connection)); + if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql)); + + if (connection.State != ConnectionState.Open) connection.Open(); + using (var command = connection.CreateCommand()) + { + if (transaction != null) command.Transaction = transaction; + if (timeout > 0) command.CommandTimeout = timeout; + command.CommandText = sql; + if (parameters != null) + { + foreach (var parameter in parameters) + { + if (parameter == null) continue; + command.Parameters.Add(parameter); + } + } + + var rows = command.ExecuteNonQuery(); + return rows; + } + } + #endregion #region Transaction @@ -1024,37 +1094,37 @@ namespace Apewer.Source } /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Class DateTime(this DataTable table, int row = 0, int column = 0) => table == null ? null : ClockUtility.DateTime(table.Value(row, column)); + public static Class DateTime(this DataTable table, int row, int column) => table == null ? null : ClockUtility.DateTime(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Class DateTime(this DataTable table, int row, string column) => table == null ? null : ClockUtility.DateTime(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Int32 Int32(this DataTable table, int row = 0, int column = 0) => table == null ? 0 : NumberUtility.Int32(table.Value(row, column)); + public static Int32 Int32(this DataTable table, int row, int column) => table == null ? 0 : NumberUtility.Int32(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Int32 Int32(this DataTable table, int row, string column) => table == null ? 0 : NumberUtility.Int32(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Int64 Int64(this DataTable table, int row = 0, int column = 0) => table == null ? 0L : NumberUtility.Int64(table.Value(row, column)); + public static Int64 Int64(this DataTable table, int row, int column) => table == null ? 0L : NumberUtility.Int64(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Int64 Int64(this DataTable table, int row, string column) => table == null ? 0L : NumberUtility.Int64(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Decimal Decimal(this DataTable table, int row = 0, int column = 0) => table == null ? 0M : NumberUtility.Decimal(table.Value(row, column)); + public static Decimal Decimal(this DataTable table, int row, int column) => table == null ? 0M : NumberUtility.Decimal(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Decimal Decimal(this DataTable table, int row, string column) => table == null ? 0M : NumberUtility.Decimal(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。> - public static Double Double(this DataTable table, int row = 0, int column = 0) => table == null ? 0D : NumberUtility.Double(table.Value(row, column)); + public static Double Double(this DataTable table, int row, int column) => table == null ? 0D : NumberUtility.Double(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。> public static Double Double(this DataTable table, int row, string column) => table == null ? 0D : NumberUtility.Double(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static string Text(this DataTable table, int row = 0, int column = 0) => table == null ? null : TextUtility.Text(table.Value(row, column)); + public static string Text(this DataTable table, int row, int column) => table == null ? null : TextUtility.Text(table.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static string Text(this DataTable table, int row, string column) => table == null ? null : TextUtility.Text(table.Value(row, column)); diff --git a/Apewer/TextUtility.cs b/Apewer/TextUtility.cs index 4133849..00cb80a 100644 --- a/Apewer/TextUtility.cs +++ b/Apewer/TextUtility.cs @@ -143,10 +143,20 @@ namespace Apewer { var first = cells[0]; if (first.IsNull()) return Empty; - if (first is char[] chars) return new string(chars); - if (first is object[] objects) + if (first is string str) return str ?? Empty; + + if (first is IEnumerable chars) + { + var list = new List(); + foreach (var @char in chars) list.Add(@char); + return new string(list.ToArray()); + } + + if (!first.GetType().IsValueType && first is IEnumerable enumerable) { - cells = objects; + var list = new List(); + foreach (var item in enumerable) list.Add(item); + cells = list.ToArray(); continue; } break; diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index bce4968..01f6034 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -26,6 +26,7 @@ namespace Apewer.Web internal ApiProcessor(ApiContext context) { + if (context == null) throw new ArgumentNullException(nameof(context)); _context = context; } @@ -518,11 +519,14 @@ namespace Apewer.Web var preWrite = provider.PreWrite(); if (!string.IsNullOrEmpty(preWrite)) return; - var responsePreOutput = response.PreOutput; - if (responsePreOutput != null) + if (response != null) { - var @continue = responsePreOutput.Invoke(_context); - if (!@continue) return; + var responsePreOutput = response.PreOutput; + if (responsePreOutput != null) + { + var @continue = responsePreOutput.Invoke(_context); + if (!@continue) return; + } } var invokerPreOutput = _context.Invoker.PreOutput; @@ -556,11 +560,14 @@ namespace Apewer.Web var preWrite = provider.PreWrite(); if (!string.IsNullOrEmpty(preWrite)) return; - var responsePreOutput = response.PreOutput; - if (responsePreOutput != null) + if (response != null) { - var @continue = responsePreOutput.Invoke(_context); - if (!@continue) return; + var responsePreOutput = response.PreOutput; + if (responsePreOutput != null) + { + var @continue = responsePreOutput.Invoke(_context); + if (!@continue) return; + } } var invokerPreOutput = _context.Invoker.PreOutput; diff --git a/Apewer/_Extensions.cs b/Apewer/_Extensions.cs index 3f5bb47..7202c65 100644 --- a/Apewer/_Extensions.cs +++ b/Apewer/_Extensions.cs @@ -558,37 +558,37 @@ public static class Extensions #region Source /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Class DateTime(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : ClockUtility.DateTime(@this.Value(row, column)); + public static Class DateTime(this IQuery @this, int row, int column) => @this == null ? null : ClockUtility.DateTime(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Class DateTime(this IQuery @this, int row, string column) => @this == null ? null : ClockUtility.DateTime(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Int32 Int32(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0 : NumberUtility.Int32(@this.Value(row, column)); + public static Int32 Int32(this IQuery @this, int row, int column) => @this == null ? 0 : NumberUtility.Int32(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Int32 Int32(this IQuery @this, int row, string column) => @this == null ? 0 : NumberUtility.Int32(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Int64 Int64(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0L : NumberUtility.Int64(@this.Value(row, column)); + public static Int64 Int64(this IQuery @this, int row, int column) => @this == null ? 0L : NumberUtility.Int64(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Int64 Int64(this IQuery @this, int row, string column) => @this == null ? 0L : NumberUtility.Int64(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static Decimal Decimal(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0M : NumberUtility.Decimal(@this.Value(row, column)); + public static Decimal Decimal(this IQuery @this, int row, int column) => @this == null ? 0M : NumberUtility.Decimal(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static Decimal Decimal(this IQuery @this, int row, string column) => @this == null ? 0M : NumberUtility.Decimal(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。> - public static Double Double(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0D : NumberUtility.Double(@this.Value(row, column)); + public static Double Double(this IQuery @this, int row, int column) => @this == null ? 0D : NumberUtility.Double(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。> public static Double Double(this IQuery @this, int row, string column) => @this == null ? 0D : NumberUtility.Double(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 - public static string Text(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : TextUtility.Text(@this.Value(row, column)); + public static string Text(this IQuery @this, int row, int column) => @this == null ? null : TextUtility.Text(@this.Value(row, column)); /// 获取默认表中指定单元格的内容。从第 0 行开始。 public static string Text(this IQuery @this, int row, string column) => @this == null ? null : TextUtility.Text(@this.Value(row, column)); diff --git a/ChangeLog.md b/ChangeLog.md index ae285f0..8ebbefe 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,13 @@  ### 最新提交 +### 6.7.5 +- 问题修正 + - Query:删除了一些访问单元格的重载方法,减少歧义; + - Source:增加 IDbConnection 的扩展方法 Query 和 Execute; + - TextUtility:修正 Join 无法解析 Lis 的问题; + - Web:修正 Response 无效时会抛出 NULL 引用的问题。 + ### 6.7.4 - 问题修正 - Source:修正了 Timeout 的默认值,已经改为连接 5 秒,查询、执行 3600 秒。