diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index 72ae2b34e..839694662 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -1031,5 +1031,25 @@ 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 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.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 55acf760a..7d0fd7305 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index 63a548c21..228052380 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.Net/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"); diff --git a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs index 528f781fa..142765cd2 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs index f5b6d4814..b2111fe63 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs index 61f56d1b3..9352ef4a3 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs +++ b/Src/Asp.Net/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.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs index 2073c15bb..33b23b964 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs +++ b/Src/Asp.Net/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}) "; + } } }