From da44efc748e463f271fc7f944b4fdb0940237972 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 16 Jul 2023 16:50:29 +0800 Subject: [PATCH] Synchronization code --- .../QueryableProvider/QueryableExecuteSql.cs | 11 ++++++- .../QueryableExecuteSqlAsync.cs | 13 ++++++-- .../DbMethods/DefaultDbMethod.cs | 30 +++++++++++++++++-- .../ExpressionsToSql/DbMethods/IDbMethods.cs | 3 ++ .../ExpressionsToSql/DbMethods/SqlFunc.cs | 17 +++++++++++ .../MemberInitExpressionResolve.cs | 2 +- .../SqlSugar/Interface/IQueryable.cs | 4 +++ .../Dm/SqlBuilder/DmExpressionContext.cs | 13 ++++++++ .../SqlBuilder/OracleExpressionContext.cs | 12 ++++++++ .../SqlBuilder/SqlServerExpressionContext.cs | 8 +++++ .../SqlBuilder/SqliteExpressionContext.cs | 22 ++++++++++++++ 11 files changed, 129 insertions(+), 6 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs index 1a9aa5a54..23a848d00 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs @@ -353,7 +353,11 @@ namespace SqlSugar var list = this.ToList(); return TreeAndFilterIds(childListExpression, parentIdExpression, rootValue, childIds, ref list) ?? new List(); } - + public virtual DataTable ToDataTableByEntity() + { + var list = this.ToList(); + return this.Context.Utilities.ListToDataTable(list); + } public virtual DataTable ToDataTable() { QueryBuilder.ResultType = typeof(SugarCacheDataTable); @@ -392,6 +396,11 @@ namespace SqlSugar } return ToDataTable(); } + public DataTable ToDataTableByEntityPage(int pageNumber, int pageSize, ref int totalNumber) + { + var list=this.ToPageList(pageNumber, pageSize,ref totalNumber); + return this.Context.Utilities.ListToDataTable(list); + } public virtual DataTable ToDataTablePage(int pageIndex, int pageSize, ref int totalNumber) { _RestoreMapping = false; diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSqlAsync.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSqlAsync.cs index 4e48d6d06..7efc75dd2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSqlAsync.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSqlAsync.cs @@ -10,7 +10,7 @@ using System.Reflection; using System.Dynamic; using System.Threading.Tasks; using System.Threading; - + namespace SqlSugar { @@ -350,7 +350,11 @@ namespace SqlSugar this.Context.MappingTables = oldMapping; return await this.Clone().ToJsonPageAsync(pageIndex, pageSize); } - + public async virtual Task ToDataTableByEntityAsync() + { + var list =await this.ToListAsync(); + return this.Context.Utilities.ListToDataTable(list); + } public async Task ToDataTableAsync() { QueryBuilder.ResultType = typeof(SugarCacheDataTable); @@ -381,6 +385,11 @@ namespace SqlSugar this.Context.MappingTables = oldMapping; return await this.Clone().ToDataTablePageAsync(pageIndex, pageSize); } + public async Task ToDataTableByEntityPageAsync(int pageNumber, int pageSize, RefAsync totalNumber) + { + var list =await this.ToPageListAsync(pageNumber, pageSize, totalNumber); + return this.Context.Utilities.ListToDataTable(list); + } public async Task> ToOffsetPageAsync(int pageIndex, int pageSize, RefAsync totalNumber) { if (this.Context.CurrentConnectionConfig.DbType != DbType.SqlServer) diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index f37b7c0bb..839694662 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -770,6 +770,12 @@ namespace SqlSugar var value1 = MergeString(value, "','"); var value2 = MergeString("','", value); var value3 = MergeString("','", value, "','"); + if (model.Args.Count == 3) + { + value1 = value1.Replace("','", model.Args[2].MemberName+"" ); + value2 = value2.Replace("','", model.Args[2].MemberName + "" ); + value3 = value3.Replace("','", model.Args[2].MemberName + "" ); + } var likeString1 = StartsWith(new MethodCallExpressionModel() { Args = new List() { new MethodCallExpressionArgs(){ IsMember=true, MemberName=fullString }, @@ -1016,14 +1022,34 @@ namespace SqlSugar { var parameterNameA = mode.Args[0].MemberName; var parameterNameB= mode.Args[1].MemberName; - return @$" CASE WHEN RIGHT({parameterNameA}, 1) = {parameterNameB} THEN LEFT({parameterNameA}, LENGTH({parameterNameA}) - 1) ELSE {parameterNameA} END "; + return $" CASE WHEN RIGHT({parameterNameA}, 1) = {parameterNameB} THEN LEFT({parameterNameA}, LENGTH({parameterNameA}) - 1) ELSE {parameterNameA} END "; } public virtual string TrimStart(MethodCallExpressionModel mode) { var parameterNameA = mode.Args[0].MemberName; var parameterNameB = mode.Args[1].MemberName; - return @$" CASE WHEN LEFT({parameterNameA}, 1) = {parameterNameB} THEN RIGHT({parameterNameA}, LEN({parameterNameA}) - 1) ELSE {parameterNameA} END "; + return $" CASE WHEN LEFT({parameterNameA}, 1) = {parameterNameB} THEN RIGHT({parameterNameA}, LEN({parameterNameA}) - 1) ELSE {parameterNameA} END "; + } + + public virtual string Left(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" LEFT({parameterNameA},{parameterNameB}) "; + } + public virtual string Right(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" RIGHT({parameterNameA},{parameterNameB}) "; + } + public virtual string PadLeft(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + var parameterNameC = mode.Args[2].MemberName; + return $" LPAD({parameterNameA},{parameterNameB},{parameterNameC}) "; } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 55acf760a..7d0fd7305 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -116,5 +116,8 @@ namespace SqlSugar string WeekOfYear(MethodCallExpressionModel mode); string TrimEnd(MethodCallExpressionModel mode); string TrimStart(MethodCallExpressionModel mode); + string Left(MethodCallExpressionModel mode); + string Right(MethodCallExpressionModel mode); + string PadLeft(MethodCallExpressionModel mode); } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index e05b3ebe4..228052380 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -13,6 +13,18 @@ namespace SqlSugar { throw new NotSupportedException("Can only be used in expressions"); } + public static string Left(string value,int number) + { + throw new NotSupportedException("Can only be used in expressions"); + } + public static string Right(string value,int number) + { + throw new NotSupportedException("Can only be used in expressions"); + } + public static string PadLeft(string value,int number, char padChar) + { + throw new NotSupportedException("Can only be used in expressions"); + } public static bool Like(string fieldName, string likeValue) { throw new NotSupportedException("Can only be used in expressions"); @@ -344,6 +356,11 @@ namespace SqlSugar throw new NotSupportedException("Can only be used in expressions"); } + public static bool SplitIn(string CommaSegmentationString, string inValue,char splitChar) + { + throw new NotSupportedException("Can only be used in expressions"); + } + public static bool ListAny(List listConstant, Expression> expression) { throw new NotSupportedException("Can only be used in expressions"); diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs index d007d510c..413447cfe 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs @@ -53,7 +53,7 @@ namespace SqlSugar var type = expression.Type; var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name); var item = memberAssignment.Expression; - + item = ExpressionTool.RemoveConvert(item); //Column IsJson Handler if (memberAssignment.Member.CustomAttributes != null) { diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index f0b86d691..cb38bb419 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -234,12 +234,16 @@ namespace SqlSugar List ToTree(Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds); Task> ToTreeAsync(Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds); DataTable ToDataTable(); + DataTable ToDataTableByEntity(); Task ToDataTableAsync(); + Task ToDataTableByEntityAsync(); DataTable ToDataTablePage(int pageNumber, int pageSize); Task ToDataTablePageAsync(int pageNumber, int pageSize); DataTable ToDataTablePage(int pageNumber, int pageSize, ref int totalNumber); + DataTable ToDataTableByEntityPage(int pageNumber, int pageSize, ref int totalNumber); DataTable ToDataTablePage(int pageNumber, int pageSize, ref int totalNumber,ref int totalPage); Task ToDataTablePageAsync(int pageNumber, int pageSize, RefAsync totalNumber); + Task ToDataTableByEntityPageAsync(int pageNumber, int pageSize, RefAsync totalNumber); List ToOffsetPage(int pageNumber, int pageSize); List ToOffsetPage(int pageNumber, int pageSize,ref int totalNumber); diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs b/Src/Asp.NetCore2/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs index 528f781fa..142765cd2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs @@ -240,5 +240,18 @@ namespace SqlSugar var parameterNameB = mode.Args[1].MemberName; return $" LTRIM({parameterNameA}, {parameterNameB}) "; } + + public override string Left(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, 1, {parameterNameB}) "; + } + public override string Right(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, -2, {parameterNameB}) "; + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs b/Src/Asp.NetCore2/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs index f5b6d4814..b2111fe63 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs @@ -366,5 +366,17 @@ namespace SqlSugar var parameterNameB = mode.Args[1].MemberName; return $" LTRIM({parameterNameA}, {parameterNameB}) "; } + public override string Left(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, 1, {parameterNameB}) "; + } + public override string Right(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, -2, {parameterNameB}) "; + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs index 61f56d1b3..9352ef4a3 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs @@ -122,6 +122,14 @@ namespace SqlSugar var parameterNameB = mode.Args[1].MemberName; return $" CASE WHEN LEFT({parameterNameA}, 1) = {parameterNameB} THEN RIGHT({parameterNameA}, LEN({parameterNameA}) - 1) ELSE {parameterNameA} END "; } + + public override string PadLeft(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + var parameterNameC = mode.Args[2].MemberName; + return $" CONCAT(REPLICATE({parameterNameC}, {parameterNameB} - LEN({parameterNameA})), {parameterNameA}) "; + } } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs b/Src/Asp.NetCore2/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs index 2073c15bb..33b23b964 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs @@ -297,5 +297,27 @@ namespace SqlSugar var parameterNameB = mode.Args[1].MemberName; return $" CASE WHEN SUBSTR({parameterNameA}, 1, 1) ={parameterNameB} THEN SUBSTR({parameterNameA}, 2) ELSE {parameterNameA} END "; } + + public override string PadLeft(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + var parameterNameC = mode.Args[2].MemberName; + var value = new string[mode.Args[1].MemberValue.ObjToInt()].Select(it=> parameterNameC); + return $"substr({string.Join("||", value)} || {parameterNameA}, {parameterNameB}*-1) "; + } + + public override string Left(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, 1, {parameterNameB}) "; + } + public override string Right(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + var parameterNameB = mode.Args[1].MemberName; + return $" SUBSTR({parameterNameA}, -2, {parameterNameB}) "; + } } }