Browse Source

Add SqlFunc.Left Right LeftPad

pull/40/head
sunkaixuan 1 year ago
parent
commit
499529188b
  1. 20
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs
  2. 3
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs
  3. 12
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs
  4. 13
      Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs
  5. 12
      Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs
  6. 8
      Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs
  7. 22
      Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs

20
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs

@ -1031,5 +1031,25 @@ namespace SqlSugar
var parameterNameB = mode.Args[1].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}) ";
}
} }
} }

3
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs

@ -116,5 +116,8 @@ namespace SqlSugar
string WeekOfYear(MethodCallExpressionModel mode); string WeekOfYear(MethodCallExpressionModel mode);
string TrimEnd(MethodCallExpressionModel mode); string TrimEnd(MethodCallExpressionModel mode);
string TrimStart(MethodCallExpressionModel mode); string TrimStart(MethodCallExpressionModel mode);
string Left(MethodCallExpressionModel mode);
string Right(MethodCallExpressionModel mode);
string PadLeft(MethodCallExpressionModel mode);
} }
} }

12
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs

@ -13,6 +13,18 @@ namespace SqlSugar
{ {
throw new NotSupportedException("Can only be used in expressions"); 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) public static bool Like(string fieldName, string likeValue)
{ {
throw new NotSupportedException("Can only be used in expressions"); throw new NotSupportedException("Can only be used in expressions");

13
Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs

@ -240,5 +240,18 @@ namespace SqlSugar
var parameterNameB = mode.Args[1].MemberName; var parameterNameB = mode.Args[1].MemberName;
return $" LTRIM({parameterNameA}, {parameterNameB}) "; 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}) ";
}
} }
} }

12
Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs

@ -366,5 +366,17 @@ namespace SqlSugar
var parameterNameB = mode.Args[1].MemberName; var parameterNameB = mode.Args[1].MemberName;
return $" LTRIM({parameterNameA}, {parameterNameB}) "; 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}) ";
}
} }
} }

8
Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs

@ -122,6 +122,14 @@ namespace SqlSugar
var parameterNameB = mode.Args[1].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 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}) ";
}
} }

22
Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs

@ -297,5 +297,27 @@ namespace SqlSugar
var parameterNameB = mode.Args[1].MemberName; var parameterNameB = mode.Args[1].MemberName;
return $" CASE WHEN SUBSTR({parameterNameA}, 1, 1) ={parameterNameB} THEN SUBSTR({parameterNameA}, 2) ELSE {parameterNameA} END "; 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}) ";
}
} }
} }

Loading…
Cancel
Save