Browse Source

Synchronization code

pull/32/head
sunkaixuan 2 years ago
parent
commit
033501bf6b
  1. 6
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs
  2. 16
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ListAnyParameter.cs
  3. 20
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ParameterExpressionVisitor.cs
  4. 77
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs
  5. 1
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs
  6. 5
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs
  7. 4
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs
  8. 32
      Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs

6
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs

@ -8,6 +8,12 @@ namespace SqlSugar
{
public class ExpressionTool
{
public static bool IsVariable(Expression expr)
{
var ps = new ParameterExpressionVisitor();
ps.Visit(expr);
return ps.Parameters.Count==0;
}
public static bool IsComparisonOperatorBool(BinaryExpression binaryExp)
{
return binaryExp.NodeType.IsIn(ExpressionType.Equal,

16
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ListAnyParameter.cs

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class ListAnyParameter
{
public string Name { get; internal set; }
public string Sql { get; internal set; }
public List<EntityColumnInfo> Columns { get; internal set; }
public Func<string,string> ConvetColumnFunc { get; internal set; }
}
}

20
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ParameterExpressionVisitor.cs

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class ParameterExpressionVisitor : ExpressionVisitor
{
public List<ParameterExpression> Parameters { get; } = new List<ParameterExpression>();
protected override Expression VisitParameter(ParameterExpression node)
{
Parameters.Add(node);
return base.VisitParameter(node);
}
}
}

77
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs

@ -732,6 +732,83 @@ namespace SqlSugar
});
return $" ({likeString1} or {likeString2} or {likeString3} or {fullString}={value} ) ";
}
public string ListAny(MethodCallExpressionModel model)
{
StringBuilder sb = new StringBuilder();
if (model.Args[0].MemberValue!=null&&(model.Args[0].MemberValue as IList).Count>0)
{
sb.Append(" ( ");
var listPar = model.Args[1].MemberValue as ListAnyParameter;
foreach (var item in (model.Args[0].MemberValue as IList))
{
var sql = listPar.Sql;
if (sb.Length > 3)
{
sb.Append("OR");
}
foreach (var columnInfo in listPar.Columns)
{
var replace = listPar.ConvetColumnFunc($"{listPar.Name}.{columnInfo.DbColumnName}");
if(sql.Contains(replace))
{
var value = columnInfo.PropertyInfo.GetValue(item);
var newValue = "null";
if (value != null)
{
if (UtilMethods.IsNumber(columnInfo.UnderType.Name))
{
newValue = value.ToString();
}
else if(columnInfo.UnderType==SqlSugar.UtilConstants.GuidType)
{
newValue = ToGuid(new MethodCallExpressionModel()
{
Args=new List<MethodCallExpressionArgs>()
{
new MethodCallExpressionArgs(){
MemberValue=value.ToSqlValue(),
MemberName=value.ToSqlValue()
}
}
});
}
else if (columnInfo.UnderType == SqlSugar.UtilConstants.DateType)
{
newValue = ToDate(new MethodCallExpressionModel()
{
Args = new List<MethodCallExpressionArgs>()
{
new MethodCallExpressionArgs(){
MemberValue=UtilMethods.GetConvertValue( value).ToSqlValue(),
MemberName=UtilMethods.GetConvertValue( value).ToSqlValue()
}
}
});
}
else
{
newValue = value.ToSqlValue();
}
}
sql = sql.Replace(replace, newValue);
}
}
sb.Append(sql);
}
sb.Append(" ) ");
}
var result = sb.ToString();
if (result.IsNullOrEmpty())
{
return " 1=2 ";
}
else
{
return result;
}
}
public virtual string GetTableWithDataBase(string dataBaseName,string tableName)
{
return $"{dataBaseName}.{tableName}";

1
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs

@ -104,6 +104,7 @@ namespace SqlSugar
string JsonArrayAny(MethodCallExpressionModel model);
string CompareTo(MethodCallExpressionModel model);
string SplitIn(MethodCallExpressionModel model);
string ListAny(MethodCallExpressionModel model);
string GetTableWithDataBase(string databaseName,string tableName);
}
}

5
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs

@ -284,5 +284,10 @@ namespace SqlSugar
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool ListAny<T>(List<T> listConstant, Expression<Func<T,bool>> expression)
{
throw new NotSupportedException("Can only be used in expressions");
}
}
}

4
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -160,6 +160,10 @@ namespace SqlSugar
{
var method = express.Method;
string name = method.Name;
if (name == "Any" &&ExpressionTool.IsVariable(express.Arguments[0]))
{
name = "ListAny";
}
var args = express.Arguments.Cast<Expression>().ToList();
MethodCallExpressionModel model = new MethodCallExpressionModel();
model.Args = new List<MethodCallExpressionArgs>();

32
Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs

@ -252,6 +252,31 @@ namespace SqlSugar
}
model.Args.Add(argItem);
}
else if (name == "ListAny"&& item is LambdaExpression)
{
var sql =GetNewExpressionValue(item,ResolveExpressType.WhereMultiple);
var lamExp = (item as LambdaExpression);
var pExp = lamExp.Parameters[0];
var pname = pExp.Name;
model.Args.Add(new MethodCallExpressionArgs() {
MemberValue=new ListAnyParameter() {
Sql= sql,
Name=pname,
Columns=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(pExp.Type).Columns,
ConvetColumnFunc = this.Context.GetTranslationColumnName
}
});
if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null)
{
ParameterExpressionVisitor visitor = new ParameterExpressionVisitor();
visitor.Visit(lamExp);
var tableParamter=visitor.Parameters.FirstOrDefault(it => it.Name != pname);
if (tableParamter != null)
{
this.Context.SingleTableNameSubqueryShortName = tableParamter.Name;
}
}
}
else
{
AppendModel(parameter, model, item);
@ -792,6 +817,9 @@ namespace SqlSugar
return this.Context.DbMehtods.CompareTo(model);
case "SplitIn":
return this.Context.DbMehtods.SplitIn(model);
case "ListAny":
this.Context.Parameters.RemoveAll(it => model.Args[0].MemberName.ObjToString().Contains(it.ParameterName));
return this.Context.DbMehtods.ListAny(model);
default:
break;
}
@ -830,6 +858,10 @@ namespace SqlSugar
{
return true;
}
if (expression.Method.Name == "Any"&& ExpressionTool.IsVariable(expression.Arguments[0]) )
{
return true;
}
if (expression.Method.Name == "Format" && expression.Method.DeclaringType == UtilConstants.StringType)
{
return true;

Loading…
Cancel
Save