diff --git a/SQLBuilder.Core.UnitTest/SelectTest.cs b/SQLBuilder.Core.UnitTest/SelectTest.cs index 9e9b47e..3cc9e66 100644 --- a/SQLBuilder.Core.UnitTest/SelectTest.cs +++ b/SQLBuilder.Core.UnitTest/SelectTest.cs @@ -1008,7 +1008,7 @@ public void Test_Page_01() .AndWhere(o => o.Name == "") .OrWhere(o => o.Subject == "") .Page(3, 2, "Id", "select * from student"); - Assert.AreEqual(@"SELECT COUNT(1) AS Records FROM (select * from student) AS T;SELECT * FROM (select * from student) AS X ORDER BY X.Id LIMIT 3 OFFSET 3;", builder.Sql); + Assert.AreEqual(@"DROP TEMPORARY TABLE IF EXISTS $TEMPORARY;CREATE TEMPORARY TABLE $TEMPORARY SELECT * FROM (select * from student) AS T;SELECT COUNT(1) AS Total FROM $TEMPORARY;SELECT * FROM $TEMPORARY AS X ORDER BY Id LIMIT 3 OFFSET 3;DROP TABLE $TEMPORARY;", builder.Sql); Assert.AreEqual(0, builder.Parameters.Count); } @@ -1023,7 +1023,7 @@ public void Test_Page_02() .AndWhere(o => o.Name == "") .OrWhere(o => o.Subject == "") .Page(3, 2, "Id"); - Assert.AreEqual(@"SELECT COUNT(1) AS Records FROM (SELECT * FROM `student` AS A WHERE A.Score IS NOT NULL AND A.Name = ?Param0 OR A.Subject = ?Param1) AS T;SELECT * FROM (SELECT * FROM `student` AS A WHERE A.Score IS NOT NULL AND A.Name = ?Param0 OR A.Subject = ?Param1) AS X ORDER BY X.Id LIMIT 3 OFFSET 3;", builder.Sql); + Assert.AreEqual(@"DROP TEMPORARY TABLE IF EXISTS $TEMPORARY;CREATE TEMPORARY TABLE $TEMPORARY SELECT * FROM (SELECT * FROM `student` AS A WHERE A.Score IS NOT NULL AND A.Name = ?Param0 OR A.Subject = ?Param1) AS T;SELECT COUNT(1) AS Total FROM $TEMPORARY;SELECT * FROM $TEMPORARY AS X ORDER BY Id LIMIT 3 OFFSET 3;DROP TABLE $TEMPORARY;", builder.Sql); Assert.AreEqual(2, builder.Parameters.Count); } @@ -1036,7 +1036,7 @@ public void Test_Page_03() var builder = SqlBuilder.Select(u => u.Id) .Where(u => u.Name == "b" && (u.Id > 2 && u.Name != null && (u.Email == "11" || u.Email == "22" || u.Email == "ee"))) .Page(10, 1, "Id"); - Assert.AreEqual(@"SELECT COUNT(1) AS Records FROM (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY X.Id) AS RowNumber,X.* FROM (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))) AS X) AS T WHERE RowNumber BETWEEN 1 AND 10;", builder.Sql); + Assert.AreEqual(@"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;SELECT * INTO #TEMPORARY FROM (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))) AS T;SELECT COUNT(1) AS Total FROM #TEMPORARY;SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber, * FROM #TEMPORARY) AS N WHERE RowNumber BETWEEN 1 AND 10;DROP TABLE #TEMPORARY;", builder.Sql); Assert.AreEqual(5, builder.Parameters.Count); } @@ -1049,7 +1049,7 @@ public void Test_Page_04() var builder = SqlBuilder.Select(u => u.Id) .Where(u => u.Name == "b" && (u.Id > 2 && u.Name != null && (u.Email == "11" || u.Email == "22" || u.Email == "ee"))) .PageByWith(10, 1, "Id"); - Assert.AreEqual(@"WITH T AS (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))) SELECT COUNT(1) AS Records FROM T;WITH X AS (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))),T AS (SELECT ROW_NUMBER() OVER (ORDER BY X.Id) AS RowNumber,X.* FROM X) SELECT * FROM T WHERE RowNumber BETWEEN 1 AND 10;", builder.Sql); + Assert.AreEqual(@"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;WITH T AS (SELECT A.Id FROM [Base_UserInfo] AS A WHERE A.Name = @Param0 AND (A.Id > @Param1 AND A.Name IS NOT NULL AND (A.Email = @Param2 OR A.Email = @Param3 OR A.Email = @Param4))) SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN 1 AND 10;DROP TABLE #TEMPORARY;", builder.Sql); Assert.AreEqual(5, builder.Parameters.Count); } @@ -1060,7 +1060,7 @@ public void Test_Page_04() public void Test_Page_05() { var builder = SqlBuilder.Select().PageByWith(10, 1, "Id", "WITH T AS (SELECT * FROM Base_UserInfo)"); - Assert.AreEqual(@"WITH T AS (SELECT * FROM Base_UserInfo)SELECT COUNT(1) AS Records FROM T;WITH T AS (SELECT * FROM Base_UserInfo),R AS (SELECT ROW_NUMBER() OVER (ORDER BY T.Id) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN 1 AND 10;", builder.Sql); + Assert.AreEqual(@"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;WITH T AS (SELECT * FROM Base_UserInfo) SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN 1 AND 10;DROP TABLE #TEMPORARY;", builder.Sql); Assert.AreEqual(0, builder.Parameters.Count); } @@ -1071,7 +1071,7 @@ public void Test_Page_05() public void Test_Page_06() { var builder = SqlBuilder.Select().Page(10, 1, "Id", "WITH T AS (SELECT * FROM Base_UserInfo)"); - Assert.AreEqual(@"WITH T AS (SELECT * FROM Base_UserInfo)SELECT COUNT(1) AS Records FROM T;WITH T AS (SELECT * FROM Base_UserInfo),R AS (SELECT ROW_NUMBER() OVER (ORDER BY T.Id) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN 1 AND 10;", builder.Sql); + Assert.AreEqual(@"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;WITH T AS (SELECT * FROM Base_UserInfo) SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN 1 AND 10;DROP TABLE #TEMPORARY;", builder.Sql); Assert.AreEqual(0, builder.Parameters.Count); } @@ -1082,7 +1082,7 @@ public void Test_Page_06() public void Test_Page_07() { var builder = SqlBuilder.Select(DatabaseType: DatabaseType.MySQL).PageByWith(10, 1, "Id", "WITH T AS (SELECT * FROM `Base_UserInfo`)"); - Assert.AreEqual(@"WITH T AS (SELECT * FROM `Base_UserInfo`)SELECT COUNT(1) AS Records FROM T;WITH T AS (SELECT * FROM `Base_UserInfo`)SELECT * FROM T ORDER BY T.Id LIMIT 10 OFFSET 0;", builder.Sql); + Assert.AreEqual(@"WITH T AS (SELECT * FROM `Base_UserInfo`) SELECT COUNT(1) AS Total FROM T;WITH T AS (SELECT * FROM `Base_UserInfo`) SELECT * FROM T ORDER BY Id LIMIT 10 OFFSET 0;", builder.Sql); Assert.AreEqual(0, builder.Parameters.Count); } #endregion diff --git a/SQLBuilder.Core/Expression/ListInitExpressionResolve.cs b/SQLBuilder.Core/Expression/ListInitExpressionResolve.cs index 7778022..7d99708 100644 --- a/SQLBuilder.Core/Expression/ListInitExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/ListInitExpressionResolve.cs @@ -39,17 +39,20 @@ public override SqlPack Insert(ListInitExpression expression, SqlPack sqlPack) { var fields = new List(); var array = expression.ToObject() as IEnumerable; - foreach (var item in array) + for (var i = 0; i < array.Count(); i++) { - sqlPack.Sql.Append("("); - var properties = item?.GetType().GetProperties(); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("("); + if (i > 0 && sqlPack.DatabaseType == DatabaseType.Oracle) + sqlPack.Sql.Append(" UNION ALL SELECT "); + var properties = array.ElementAt(i)?.GetType().GetProperties(); foreach (var p in properties) { var type = p.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : p.DeclaringType; (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, p); if (isInsert) { - var value = p.GetValue(item, null); + var value = p.GetValue(array.ElementAt(i), null); if (value != null || (sqlPack.IsEnableNullValue && value == null)) { sqlPack.AddDbParameter(value); @@ -61,13 +64,14 @@ public override SqlPack Insert(ListInitExpression expression, SqlPack sqlPack) if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - sqlPack.Sql.Append("),"); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("),"); + else + sqlPack.Sql.Append(" FROM DUAL"); } } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); - } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return sqlPack; } diff --git a/SQLBuilder.Core/Expression/MemberExpressionResolve.cs b/SQLBuilder.Core/Expression/MemberExpressionResolve.cs index 52120bd..de4bbc1 100644 --- a/SQLBuilder.Core/Expression/MemberExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/MemberExpressionResolve.cs @@ -26,7 +26,7 @@ namespace SQLBuilder.Core /// /// 表示访问字段或属性 /// - public class MemberExpressionResolve : BaseSqlBuilder + public class MemberExpressionResolve : BaseSqlBuilder { #region Override Base Class Methods /// @@ -41,28 +41,25 @@ public override SqlPack Insert(MemberExpression expression, SqlPack sqlPack) var fields = new List(); var obj = expression.ToObject(); if (obj.GetType().IsArray) - { objectArray.AddRange(obj as object[]); - } else if (obj.GetType().Name == "List`1") - { objectArray.AddRange(obj as IEnumerable); - } else - { objectArray.Add(obj); - } - foreach (var item in objectArray) + for (var i = 0; i < objectArray.Count; i++) { - sqlPack.Sql.Append("("); - var properties = item?.GetType().GetProperties(); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("("); + if (i > 0 && sqlPack.DatabaseType == DatabaseType.Oracle) + sqlPack.Sql.Append(" UNION ALL SELECT "); + var properties = objectArray[i]?.GetType().GetProperties(); foreach (var p in properties) { var type = p.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : p.DeclaringType; (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, p); if (isInsert) { - var value = p.GetValue(item, null); + var value = p.GetValue(objectArray[i], null); if (value != null || (sqlPack.IsEnableNullValue && value == null)) { sqlPack.AddDbParameter(value); @@ -74,13 +71,14 @@ public override SqlPack Insert(MemberExpression expression, SqlPack sqlPack) if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - sqlPack.Sql.Append("),"); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("),"); + else + sqlPack.Sql.Append(" FROM DUAL"); } } if (sqlPack[sqlPack.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return sqlPack; } diff --git a/SQLBuilder.Core/Expression/MemberInitExpressionResolve.cs b/SQLBuilder.Core/Expression/MemberInitExpressionResolve.cs index e59b8f2..c65de0d 100644 --- a/SQLBuilder.Core/Expression/MemberInitExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/MemberInitExpressionResolve.cs @@ -36,7 +36,8 @@ public class MemberInitExpressionResolve : BaseSqlBuilder /// SqlPack public override SqlPack Insert(MemberInitExpression expression, SqlPack sqlPack) { - sqlPack.Sql.Append("("); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("("); var fields = new List(); foreach (MemberAssignment m in expression.Bindings) { @@ -56,7 +57,10 @@ public override SqlPack Insert(MemberInitExpression expression, SqlPack sqlPack) if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - sqlPack.Sql.Append(")"); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append(")"); + else + sqlPack.Sql.Append(" FROM DUAL"); } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return sqlPack; @@ -86,9 +90,7 @@ public override SqlPack Update(MemberInitExpression expression, SqlPack sqlPack) } } if (sqlPack[sqlPack.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - } return sqlPack; } #endregion diff --git a/SQLBuilder.Core/Expression/MethodCallExpressionResolve.cs b/SQLBuilder.Core/Expression/MethodCallExpressionResolve.cs index 28ec844..6347d21 100644 --- a/SQLBuilder.Core/Expression/MethodCallExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/MethodCallExpressionResolve.cs @@ -439,9 +439,7 @@ public override SqlPack Where(MethodCallExpression expression, SqlPack sqlPack) { var key = expression.Method; if (key.IsGenericMethod) - { key = key.GetGenericMethodDefinition(); - } if (_Methods.TryGetValue(key.Name, out Action action)) { action(expression, sqlPack); @@ -460,17 +458,20 @@ public override SqlPack Insert(MethodCallExpression expression, SqlPack sqlPack) { var fields = new List(); var array = expression.ToObject() as object[]; - foreach (var item in array) + for (var i = 0; i < array.Length; i++) { - sqlPack.Sql.Append("("); - var properties = item?.GetType().GetProperties(); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("("); + if (i > 0 && sqlPack.DatabaseType == DatabaseType.Oracle) + sqlPack.Sql.Append(" UNION ALL SELECT "); + var properties = array[i]?.GetType().GetProperties(); foreach (var p in properties) { var type = p.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : p.DeclaringType; (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, p); if (isInsert) { - var value = p.GetValue(item, null); + var value = p.GetValue(array[i], null); if (value != null || (sqlPack.IsEnableNullValue && value == null)) { sqlPack.AddDbParameter(value); @@ -482,13 +483,14 @@ public override SqlPack Insert(MethodCallExpression expression, SqlPack sqlPack) if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - sqlPack.Sql.Append("),"); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("),"); + else + sqlPack.Sql.Append(" FROM DUAL"); } } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); - } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return sqlPack; } diff --git a/SQLBuilder.Core/Expression/NewArrayExpressionResolve.cs b/SQLBuilder.Core/Expression/NewArrayExpressionResolve.cs index fb4fe9b..35ba6f2 100644 --- a/SQLBuilder.Core/Expression/NewArrayExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/NewArrayExpressionResolve.cs @@ -41,9 +41,7 @@ public override SqlPack In(NewArrayExpression expression, SqlPack sqlPack) sqlPack += ","; } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); - } sqlPack += ")"; return sqlPack; } @@ -59,12 +57,15 @@ public override SqlPack Insert(NewArrayExpression expression, SqlPack sqlPack) foreach (Expression expressionItem in expression.Expressions) { SqlBuilderProvider.Insert(expressionItem, sqlPack); - sqlPack += ","; + if (sqlPack.DatabaseType == DatabaseType.Oracle) + sqlPack += " UNION ALL SELECT "; + else + sqlPack += ","; } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') - { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); - } + if (sqlPack.Sql.ToString().LastIndexOf(" UNION ALL SELECT ") > -1) + sqlPack.Sql.Remove(sqlPack.Sql.Length - 18, 18); return sqlPack; } @@ -103,13 +104,9 @@ public override SqlPack OrderBy(NewArrayExpression expression, SqlPack sqlPack, else if (expression.Expressions[i] is ConstantExpression order) { if (!order.Value.ToString().ToUpper().Contains("ASC") && !order.Value.ToString().ToUpper().Contains("DESC")) - { sqlPack += " ASC,"; - } else - { sqlPack += ","; - } } else { diff --git a/SQLBuilder.Core/Expression/NewExpressionResolve.cs b/SQLBuilder.Core/Expression/NewExpressionResolve.cs index 5d9c7b1..2202889 100644 --- a/SQLBuilder.Core/Expression/NewExpressionResolve.cs +++ b/SQLBuilder.Core/Expression/NewExpressionResolve.cs @@ -67,7 +67,8 @@ public override SqlPack Update(NewExpression expression, SqlPack sqlPack) /// SqlPack public override SqlPack Insert(NewExpression expression, SqlPack sqlPack) { - sqlPack.Sql.Append("("); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append("("); var fields = new List(); for (int i = 0; i < expression.Members?.Count; i++) { @@ -88,7 +89,10 @@ public override SqlPack Insert(NewExpression expression, SqlPack sqlPack) if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); - sqlPack.Sql.Append(")"); + if (sqlPack.DatabaseType != DatabaseType.Oracle) + sqlPack.Sql.Append(")"); + else + sqlPack.Sql.Append(" FROM DUAL"); } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return sqlPack; @@ -109,9 +113,7 @@ public override SqlPack Select(NewExpression expression, SqlPack sqlPack) SqlBuilderProvider.Select(argument, sqlPack); //添加字段别名 if ((argument as MemberExpression)?.Member.Name != member.Name) - { sqlPack.SelectFields[sqlPack.SelectFields.Count - 1] += " AS " + member.Name; - } } return sqlPack; } @@ -145,13 +147,9 @@ public override SqlPack OrderBy(NewExpression expression, SqlPack sqlPack, param { SqlBuilderProvider.OrderBy(expression.Arguments[i], sqlPack); if (i <= orders.Length - 1) - { sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},"; - } else - { sqlPack += " ASC,"; - } } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); return sqlPack; diff --git a/SQLBuilder.Core/Repositories/MySqlRepository.cs b/SQLBuilder.Core/Repositories/MySqlRepository.cs index 0634085..0012b31 100644 --- a/SQLBuilder.Core/Repositories/MySqlRepository.cs +++ b/SQLBuilder.Core/Repositories/MySqlRepository.cs @@ -2071,17 +2071,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2120,17 +2112,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2170,17 +2154,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2234,17 +2210,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2283,17 +2251,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2332,19 +2292,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2381,19 +2332,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -2623,17 +2565,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2672,17 +2606,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2722,17 +2648,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2786,17 +2704,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2835,17 +2745,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2884,19 +2786,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2933,19 +2826,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3051,17 +2935,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3099,17 +2975,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3147,19 +3015,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3195,19 +3054,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3315,17 +3165,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3365,17 +3207,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3415,19 +3249,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3465,19 +3290,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); diff --git a/SQLBuilder.Core/Repositories/NpgsqlRepository.cs b/SQLBuilder.Core/Repositories/NpgsqlRepository.cs index 6724c37..48de964 100644 --- a/SQLBuilder.Core/Repositories/NpgsqlRepository.cs +++ b/SQLBuilder.Core/Repositories/NpgsqlRepository.cs @@ -2071,17 +2071,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2120,17 +2112,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2170,17 +2154,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2234,17 +2210,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2283,17 +2251,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2332,19 +2292,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2381,19 +2332,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -2623,17 +2565,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2672,17 +2606,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2722,17 +2648,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2786,17 +2704,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2835,17 +2745,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -2884,19 +2786,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2933,19 +2826,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3051,17 +2935,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3099,17 +2975,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3147,19 +3015,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3195,19 +3054,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3315,17 +3165,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3365,17 +3207,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) @@ -3415,19 +3249,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3465,19 +3290,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); diff --git a/SQLBuilder.Core/Repositories/OracleRepository.cs b/SQLBuilder.Core/Repositories/OracleRepository.cs index d369987..6a2b7f9 100644 --- a/SQLBuilder.Core/Repositories/OracleRepository.cs +++ b/SQLBuilder.Core/Repositories/OracleRepository.cs @@ -2071,32 +2071,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2119,32 +2109,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2168,32 +2148,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2231,32 +2201,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2279,32 +2239,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2327,33 +2277,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2376,33 +2315,22 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = Transaction.Connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = Transaction.Connection.Query($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var list = multiQuery?.Read(); + var total = connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = connection.Query($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2618,32 +2546,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2666,32 +2584,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2715,32 +2623,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({builder.Sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", builder.DynamicParameters, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({builder.Sql}) T", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({builder.Sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", builder.DynamicParameters, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2778,32 +2676,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2826,32 +2714,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2874,33 +2752,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -2923,33 +2790,22 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = await Transaction.Connection.QueryAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } else { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var list = await multiQuery?.ReadAsync(); + var total = await connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var list = await connection.QueryAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); return (list, total); } } @@ -3041,23 +2897,15 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = Transaction.Connection.ExecuteReader($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3065,9 +2913,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = connection.ExecuteReader($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3090,23 +2938,15 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = Transaction.Connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = Transaction.Connection.ExecuteReader($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3114,9 +2954,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = connection.QueryFirstOrDefault($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = connection.ExecuteReader($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3139,24 +2979,15 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = Transaction.Connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = Transaction.Connection.ExecuteReader($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3164,9 +2995,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = connection.ExecuteReader($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3189,24 +3020,15 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = Transaction.Connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = Transaction.Connection.ExecuteReader($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3214,9 +3036,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = multiQuery?.ReadFirstOrDefault() ?? 0; - var table = multiQuery?.Read()?.ToList()?.ToDataTable(); + var total = connection.QueryFirstOrDefault($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = connection.ExecuteReader($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3311,24 +3133,15 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = await Transaction.Connection.ExecuteReaderAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3336,10 +3149,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = await connection.ExecuteReaderAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3362,24 +3174,15 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = await Transaction.Connection.ExecuteReaderAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3387,10 +3190,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await connection.QueryFirstOrDefaultAsync($"SELECT COUNT(1) AS Total FROM ({sql}) T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = await connection.ExecuteReaderAsync($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3413,25 +3215,15 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = await Transaction.Connection.ExecuteReaderAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3439,10 +3231,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", parameter, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", parameter, Transaction, commandTimeout: CommandTimeout); + var reader = await connection.ExecuteReaderAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", parameter, Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3465,25 +3256,15 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await Transaction.Connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = await Transaction.Connection.ExecuteReaderAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } @@ -3491,10 +3272,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql},R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); - var total = await multiQuery?.ReadFirstOrDefaultAsync(); - var reader = await multiQuery?.ReadAsync(); - var table = reader?.ToList()?.ToDataTable(); + var total = await connection.QueryFirstOrDefaultAsync($"{sql} SELECT COUNT(1) AS Total FROM T", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var reader = await connection.ExecuteReaderAsync($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var table = reader?.ToDataTable(); table?.Columns?.Remove("RowNumber"); return (table, total); } diff --git a/SQLBuilder.Core/Repositories/SqlRepository.cs b/SQLBuilder.Core/Repositories/SqlRepository.cs index 5a3cd88..ca5a021 100644 --- a/SQLBuilder.Core/Repositories/SqlRepository.cs +++ b/SQLBuilder.Core/Repositories/SqlRepository.cs @@ -2071,13 +2071,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2120,13 +2116,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2170,13 +2162,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2234,13 +2222,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2283,13 +2267,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2332,13 +2312,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2347,7 +2323,7 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); + var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var list = multiQuery?.Read(); return (list, total); @@ -2356,7 +2332,7 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); + var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var list = multiQuery?.Read(); return (list, total); @@ -2381,13 +2357,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2396,7 +2368,7 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var list = multiQuery?.Read(); return (list, total); @@ -2405,7 +2377,7 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); + var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var list = multiQuery?.Read(); return (list, total); @@ -2623,13 +2595,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2672,13 +2640,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2722,13 +2686,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2786,13 +2746,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2835,13 +2791,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2884,13 +2836,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2899,7 +2847,7 @@ public async Task> FindListAsync(string sql, params DbParamete var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); + var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var list = await multiQuery?.ReadAsync(); return (list, total); @@ -2908,7 +2856,7 @@ public async Task> FindListAsync(string sql, params DbParamete { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); + var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var list = await multiQuery?.ReadAsync(); return (list, total); @@ -2933,13 +2881,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -2948,7 +2892,7 @@ public async Task> FindListAsync(string sql, params DbParamete var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var list = await multiQuery?.ReadAsync(); return (list, total); @@ -2957,7 +2901,7 @@ public async Task> FindListAsync(string sql, params DbParamete { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); + var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var list = await multiQuery?.ReadAsync(); return (list, total); @@ -3051,13 +2995,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3101,13 +3041,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3151,13 +3087,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3166,7 +3098,7 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND RowNumber {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); + var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND RowNumber {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var table = multiQuery?.Read()?.ToList()?.ToDataTable(); table?.Columns?.Remove("RowNumber"); @@ -3176,7 +3108,7 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); + var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var table = multiQuery?.Read()?.ToList()?.ToDataTable(); table?.Columns?.Remove("RowNumber"); @@ -3201,13 +3133,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3216,7 +3144,7 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND RowNumber {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var multiQuery = Transaction.Connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND RowNumber {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var table = multiQuery?.Read()?.ToList()?.ToDataTable(); table?.Columns?.Remove("RowNumber"); @@ -3226,7 +3154,7 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { using (var connection = Connection) { - var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); + var multiQuery = connection.QueryMultiple($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); var total = multiQuery?.ReadFirstOrDefault() ?? 0; var table = multiQuery?.Read()?.ToList()?.ToDataTable(); table?.Columns?.Remove("RowNumber"); @@ -3323,13 +3251,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3375,13 +3299,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3427,13 +3347,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3442,7 +3358,7 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); + var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, Transaction, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var reader = await multiQuery?.ReadAsync(); var table = reader?.ToList()?.ToDataTable(); @@ -3453,7 +3369,7 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); + var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", parameter, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var reader = await multiQuery?.ReadAsync(); var table = reader?.ToList()?.ToDataTable(); @@ -3479,13 +3395,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } else { @@ -3494,7 +3406,7 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP var guid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); if (Transaction?.Connection != null) { - var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); + var multiQuery = await Transaction.Connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var reader = await multiQuery?.ReadAsync(); var table = reader?.ToList()?.ToDataTable(); @@ -3505,7 +3417,7 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { using (var connection = Connection) { - var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH T AS (SELECT * FROM #TEMPORARY_{guid}),R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); + var multiQuery = await connection.QueryMultipleAsync($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY_{guid}') IS NOT NULL DROP TABLE #TEMPORARY_{guid};{sql} SELECT * INTO #TEMPORARY_{guid} FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY_{guid};WITH R AS (SELECT ROW_NUMBER() OVER ({orderField}) AS RowNumber,* FROM #TEMPORARY_{guid}) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY_{guid};", dbParameter.ToDynamicParameters(), commandTimeout: CommandTimeout); var total = await multiQuery?.ReadFirstOrDefaultAsync(); var reader = await multiQuery?.ReadAsync(); var table = reader?.ToList()?.ToDataTable(); diff --git a/SQLBuilder.Core/Repositories/SqliteRepository.cs b/SQLBuilder.Core/Repositories/SqliteRepository.cs index 3189635..c7a088c 100644 --- a/SQLBuilder.Core/Repositories/SqliteRepository.cs +++ b/SQLBuilder.Core/Repositories/SqliteRepository.cs @@ -2071,17 +2071,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2119,17 +2111,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2168,17 +2152,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2231,17 +2207,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2279,17 +2247,9 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2327,19 +2287,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2376,19 +2327,10 @@ public IEnumerable FindList(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -2618,17 +2560,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2666,17 +2600,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2715,17 +2641,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2778,17 +2696,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2826,17 +2736,9 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -2874,19 +2776,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -2923,19 +2816,10 @@ public async Task> FindListAsync(string sql, params DbParamete { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3041,17 +2925,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -3088,17 +2964,9 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -3135,19 +3003,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3183,19 +3042,10 @@ public DataTable FindTable(string sql, params DbParameter[] dbParameter) { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = Transaction.Connection.QueryMultiple($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); @@ -3303,17 +3153,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -3352,17 +3194,9 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } if (Transaction?.Connection != null) { @@ -3401,19 +3235,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } } - else - { - orderField = "ORDER BY (SELECT 0)"; - } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", parameter, Transaction, commandTimeout: CommandTimeout); @@ -3451,19 +3276,10 @@ public async Task FindTableAsync(string sql, params DbParameter[] dbP { orderField = orderField.ToUpper(); if (orderField.Contains("ASC") || orderField.Contains("DESC")) - { orderField = $"ORDER BY {orderField}"; - } else - { orderField = $"ORDER BY {orderField} {(isAscending ? "ASC" : "DESC")}"; - } - } - else - { - orderField = "ORDER BY (SELECT 0)"; } - //暂未实现临时表with分页 if (Transaction?.Connection != null) { var multiQuery = await Transaction.Connection.QueryMultipleAsync($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};", dbParameter.ToDynamicParameters(), Transaction, commandTimeout: CommandTimeout); diff --git a/SQLBuilder.Core/SQLBuilder.Core.csproj b/SQLBuilder.Core/SQLBuilder.Core.csproj index b088c3b..c44888c 100644 --- a/SQLBuilder.Core/SQLBuilder.Core.csproj +++ b/SQLBuilder.Core/SQLBuilder.Core.csproj @@ -2,18 +2,20 @@ netstandard2.0 - 1.0.2.7 + 1.0.2.8 张强 - 1.0.2.7 + 1.0.2.8 true Expression表达式转换为SQL语句,支持SqlServer、MySQL、Oracle、SQLite、PostgreSQL Copyright © 2018 , 张强 943620963@qq.com https://zqlovejyc.gitee.io/zqutils-js/Images/SQL.png Zq.SQLBuilder.Core - 1.更新OrderType枚举类型名称; -2.新增linq排序扩展方法; -3.修复ToEntities、ToLists扩展方法赋值过程中转换bug; - 1.0.2.7 + 1.修复Expression批量插入转换Oracle数据对应sql错误bug; +2.修复Oracle仓储分页方法错误bug; +3.新增sql注入扩展方法; +4.新增OracleDynamicParameters实现Dapper的IDynamicParameters接口; +5.优化Page方法,精简仓储代码; + 1.0.2.8 https://github.com/zqlovejyc/SQLBuilder.Core diff --git a/SQLBuilder.Core/SqlBuilderCore.cs b/SQLBuilder.Core/SqlBuilderCore.cs index 8176bf2..565b28e 100644 --- a/SQLBuilder.Core/SqlBuilderCore.cs +++ b/SQLBuilder.Core/SqlBuilderCore.cs @@ -802,25 +802,45 @@ public SqlBuilderCore Page(int pageSize, int pageIndex, string orderField, st if (parameters != null) this._sqlPack.DbParams = parameters; } sql = string.IsNullOrEmpty(sql) ? this._sqlPack.Sql.ToString().TrimEnd(';') : sql.TrimEnd(';'); - //SQLServer、Oracle - if (this._sqlPack.DatabaseType == DatabaseType.SQLServer || this._sqlPack.DatabaseType == DatabaseType.Oracle) + //SQLServer + if (this._sqlPack.DatabaseType == DatabaseType.SQLServer) { if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) - { - sb.Append(sql); - sb.Append("SELECT COUNT(1) AS Records FROM T;"); - sb.Append(sql); - sb.Append($",R AS (SELECT ROW_NUMBER() OVER (ORDER BY T.{orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};"); - } + sb.Append($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;{sql} SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY {orderField}) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY;"); else - { - sb.Append($"SELECT COUNT(1) AS Records FROM ({sql}) AS T;SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY X.{orderField}) AS RowNumber,X.* FROM ({sql}) AS X) AS T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};"); - } + sb.Append($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;SELECT * INTO #TEMPORARY FROM ({sql}) AS T;SELECT COUNT(1) AS Total FROM #TEMPORARY;SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY {orderField}) AS RowNumber, * FROM #TEMPORARY) AS N WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY;"); } - //MySQL、SQLite - if (this._sqlPack.DatabaseType == DatabaseType.MySQL || this._sqlPack.DatabaseType == DatabaseType.SQLite) + //Oracle,注意Oracle需要分开查询总条数和分页数据 + if (this._sqlPack.DatabaseType == DatabaseType.Oracle) { - sb.Append($"SELECT COUNT(1) AS Records FROM ({sql}) AS T;SELECT * FROM ({sql}) AS X ORDER BY X.{orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} ORDER BY {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}"); + else + sb.Append($"SELECT * FROM (SELECT X.*,ROWNUM AS RowNumber FROM ({sql} ORDER BY {orderField}) X WHERE ROWNUM <= {pageSize * pageIndex}) T WHERE T.RowNumber >= {pageSize * (pageIndex - 1) + 1}"); + } + //MySQL,注意8.0版本才支持WITH语法 + if (this._sqlPack.DatabaseType == DatabaseType.MySQL) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"DROP TEMPORARY TABLE IF EXISTS $TEMPORARY;CREATE TEMPORARY TABLE $TEMPORARY SELECT * FROM ({sql}) AS T;SELECT COUNT(1) AS Total FROM $TEMPORARY;SELECT * FROM $TEMPORARY AS X ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};DROP TABLE $TEMPORARY;"); + } + //PostgreSQL + if (this._sqlPack.DatabaseType == DatabaseType.PostgreSQL) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"DROP TABLE IF EXISTS TEMPORARY_TABLE;CREATE TEMPORARY TABLE TEMPORARY_TABLE AS SELECT * FROM ({sql}) AS T;SELECT COUNT(1) AS Total FROM TEMPORARY_TABLE;SELECT * FROM TEMPORARY_TABLE AS X ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};DROP TABLE TEMPORARY_TABLE;"); + } + //SQLite + if (this._sqlPack.DatabaseType == DatabaseType.SQLite) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"SELECT COUNT(1) AS Total FROM ({sql}) AS T;SELECT * FROM ({sql}) AS X ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); } this._sqlPack.Sql.Clear().Append(sb); return this; @@ -844,35 +864,45 @@ public SqlBuilderCore PageByWith(int pageSize, int pageIndex, string orderFie if (parameters != null) this._sqlPack.DbParams = parameters; } sql = string.IsNullOrEmpty(sql) ? this._sqlPack.Sql.ToString().TrimEnd(';') : sql.TrimEnd(';'); - //SQLServer、Oracle - if (this._sqlPack.DatabaseType == DatabaseType.SQLServer || this._sqlPack.DatabaseType == DatabaseType.Oracle) + //SQLServer + if (this._sqlPack.DatabaseType == DatabaseType.SQLServer) { if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) - { - sb.Append(sql); - sb.Append("SELECT COUNT(1) AS Records FROM T;"); - sb.Append(sql); - sb.Append($",R AS (SELECT ROW_NUMBER() OVER (ORDER BY T.{orderField}) AS RowNumber,T.* FROM T) SELECT * FROM R WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};"); - } + sb.Append($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;{sql} SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY {orderField}) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY;"); else - { - sb.Append($"WITH T AS ({sql}) SELECT COUNT(1) AS Records FROM T;WITH X AS ({sql}),T AS (SELECT ROW_NUMBER() OVER (ORDER BY X.{orderField}) AS RowNumber,X.* FROM X) SELECT * FROM T WHERE RowNumber BETWEEN {(pageSize * (pageIndex - 1) + 1)} AND {(pageSize * pageIndex)};"); - } + sb.Append($"IF OBJECT_ID(N'TEMPDB..#TEMPORARY') IS NOT NULL DROP TABLE #TEMPORARY;WITH T AS ({sql}) SELECT * INTO #TEMPORARY FROM T;SELECT COUNT(1) AS Total FROM #TEMPORARY;WITH R AS (SELECT ROW_NUMBER() OVER (ORDER BY {orderField}) AS RowNumber,* FROM #TEMPORARY) SELECT * FROM R WHERE RowNumber BETWEEN {((pageIndex - 1) * pageSize + 1)} AND {(pageIndex * pageSize)};DROP TABLE #TEMPORARY;"); } - //MySQL 8.0开始支持,8.0之前的版本不支持WITH语法、SQLite - if (this._sqlPack.DatabaseType == DatabaseType.MySQL || this._sqlPack.DatabaseType == DatabaseType.SQLite) + //Oracle,注意Oracle需要分开查询总条数和分页数据 + if (this._sqlPack.DatabaseType == DatabaseType.Oracle) { if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) - { - sb.Append(sql); - sb.Append("SELECT COUNT(1) AS Records FROM T;"); - sb.Append(sql); - sb.Append($"SELECT * FROM T ORDER BY T.{orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); - } + sb.Append($"{sql},R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} ORDER BY {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}"); else - { - sb.Append($"WITH T AS ({sql}) SELECT COUNT(1) AS Records FROM T;WITH X AS ({sql}) SELECT * FROM X ORDER BY X.{orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); - } + sb.Append($"WITH T AS ({sql}),R AS (SELECT ROWNUM AS RowNumber,T.* FROM T WHERE ROWNUM <= {pageSize * pageIndex} ORDER BY {orderField}) SELECT * FROM R WHERE RowNumber>={pageSize * (pageIndex - 1) + 1}"); + } + //MySQL,注意8.0版本才支持WITH语法 + if (this._sqlPack.DatabaseType == DatabaseType.MySQL) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"WITH T AS ({sql}) SELECT COUNT(1) AS Total FROM T;WITH T AS ({sql}) SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + } + //PostgreSQL + if (this._sqlPack.DatabaseType == DatabaseType.PostgreSQL) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"WITH T AS ({sql}) SELECT COUNT(1) AS Total FROM T;WITH T AS ({sql}) SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + } + //SQLite + if (this._sqlPack.DatabaseType == DatabaseType.SQLite) + { + if (Regex.IsMatch(sql, "WITH", RegexOptions.IgnoreCase)) + sb.Append($"{sql} SELECT COUNT(1) AS Total FROM T;{sql} SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); + else + sb.Append($"WITH T AS ({sql}) SELECT COUNT(1) AS Total FROM T;WITH T AS ({sql}) SELECT * FROM T ORDER BY {orderField} LIMIT {pageSize} OFFSET {(pageSize * (pageIndex - 1))};"); } this._sqlPack.Sql.Clear().Append(sb); return this; @@ -923,7 +953,7 @@ public SqlBuilderCore Insert(Expression> expression = null, bool this._sqlPack.Clear(); this._sqlPack.IsSingleTable = true; this._sqlPack.IsEnableNullValue = isEnableNullValue; - this._sqlPack += $"INSERT INTO {this._sqlPack.GetTableName(typeof(T))} ({{0}}) VALUES "; + this._sqlPack += $"INSERT INTO {this._sqlPack.GetTableName(typeof(T))} ({{0}}) {(this._sqlPack.DatabaseType == DatabaseType.Oracle ? "SELECT" : "VALUES")} "; SqlBuilderProvider.Insert(expression.Body, this._sqlPack); return this; }