From c60a4c3563a1c012088873bcf1c17686758a4466 Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Thu, 5 Aug 2021 20:26:18 +0800 Subject: [PATCH] SqlQueryable functionality is optimized --- .../Abstract/QueryableProvider/QueryableProvider.cs | 1 + .../Abstract/SqlBuilderProvider/QueryBuilder.cs | 10 ++++++++++ .../Abstract/SugarProvider/SqlSugarProvider.cs | 4 +++- .../Realization/Dm/SqlBuilder/DmQueryBuilder.cs | 1 + .../Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs | 1 + .../Oracle/SqlBuilder/OracleQueryBuilder.cs | 1 + .../PostgreSQL/SqlBuilder/PostgreSQLQueryBuilder.cs | 1 + .../SqlServer/SqlBuilder/SqlServerQueryBuilder.cs | 7 ++++--- .../Sqlite/SqlBuilder/SqliteQueryBuilder.cs | 1 + 9 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index c827cbaec..8ff3c2bf2 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -2419,6 +2419,7 @@ namespace SqlSugar asyncQueryableBuilder.AsTables = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AsTables); asyncQueryableBuilder.DisableTop = this.QueryBuilder.DisableTop; asyncQueryableBuilder.Offset = this.QueryBuilder.Offset; + asyncQueryableBuilder.IsSqlQuery = this.QueryBuilder.IsSqlQuery; } protected int SetCacheTime(int cacheDurationInSeconds) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 88637850a..d71335ab3 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -35,6 +35,7 @@ namespace SqlSugar #region Splicing basic public List IgnoreColumns { get; set; } public bool IsCount { get; set; } + public bool IsSqlQuery { get; set; } public int? Skip { get; set; } public int ExternalPageIndex { get; set; } public int ExternalPageSize { get; set; } @@ -475,6 +476,15 @@ namespace SqlSugar { return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]"); } + public string GetSqlQuerySql(string result) + { + if (this.IsSqlQuery && (Skip == null && Take == null)) + { + result = System.Text.RegularExpressions.Regex.Match(result, @"^SELECT t\.\* FROM \((.*)\) t $").Groups[1].Value; + } + + return result; + } #endregion #region Get SQL Partial diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 6caf3759d..da6dfa7c8 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -578,7 +578,9 @@ namespace SqlSugar public ISugarQueryable SqlQueryable(string sql) where T : class, new() { var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); - return this.Context.Queryable().AS(sqlBuilder.GetPackTable(sql, sqlBuilder.GetDefaultShortName())).With(SqlWith.Null).Select(sqlBuilder.GetDefaultShortName() + ".*"); + var result= this.Context.Queryable().AS(sqlBuilder.GetPackTable(sql, sqlBuilder.GetDefaultShortName())).With(SqlWith.Null).Select(sqlBuilder.GetDefaultShortName() + ".*"); + result.QueryBuilder.IsSqlQuery = true; + return result; } #endregion diff --git a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmQueryBuilder.cs index 955459c97..38868e522 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmQueryBuilder.cs @@ -48,6 +48,7 @@ namespace SqlSugar result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true); } this.OrderByValue = oldOrderBy; + result = GetSqlQuerySql(result); return result; } public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false) diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs index 27432a327..179050eeb 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs @@ -61,6 +61,7 @@ namespace SqlSugar result = sql.ToString(); } this.OrderByValue = oldOrderValue; + result = GetSqlQuerySql(result); return result; } private string ToCountSqlString() diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleQueryBuilder.cs index f6d86e93c..89d2e5261 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleQueryBuilder.cs @@ -51,6 +51,7 @@ namespace SqlSugar result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true); } this.OrderByValue = oldOrderBy; + result = GetSqlQuerySql(result); return result; } public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false) diff --git a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLQueryBuilder.cs index 22231800e..51e1cad28 100644 --- a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLQueryBuilder.cs @@ -61,6 +61,7 @@ namespace SqlSugar result = sql.ToString(); } this.OrderByValue = oldOrderValue; + result = GetSqlQuerySql(result); return result; } diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs index 53f6807d7..cb1a1de8f 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs @@ -28,7 +28,7 @@ namespace SqlSugar { this.OrderByValue = this.PartitionByValue + this.OrderByValue; } - var isFirst = (Skip == 0 || Skip == null) && Take == 1&&DisableTop==false; + var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false; var isRowNumber = (Skip != null || Take != null) && !isFirst; var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString); string groupByValue = GetGroupByString + HavingInfos; @@ -48,14 +48,15 @@ namespace SqlSugar result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true); } this.OrderByValue = oldOrderBy; - if (!string.IsNullOrEmpty(this.Offset)) + if (!string.IsNullOrEmpty(this.Offset)) { - if (this.OrderByValue.IsNullOrEmpty()) + if (this.OrderByValue.IsNullOrEmpty()) { result += " ORDER BY GETDATE() "; } result += this.Offset; } + result = GetSqlQuerySql(result); return result; } } diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteQueryBuilder.cs index dc31cd5a6..1133f74b3 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteQueryBuilder.cs @@ -61,6 +61,7 @@ namespace SqlSugar result= sql.ToString(); } this.OrderByValue = oldOrderBy; + result = GetSqlQuerySql(result); return result; }