public static StringBuilder BuildInCondition(MemberExpression memberExpression, Expression valueExpression, WhereClauseAdhesive adhesive) { var memberInfo = memberExpression.Member; string fieldName = adhesive.SqlAdapter.FormatColumnName(memberInfo); string parameterName = EnsureParameter(memberInfo, adhesive); object value = ConstantExtractor.ParseConstant(valueExpression); adhesive.Parameters.Add($"@{parameterName}", value); return(new StringBuilder(string.Format("{0} in {1}", fieldName, $"@{parameterName}"))); }
public static StringBuilder BuildLikeOrEqualCondition(MethodCallExpression methodCallExpression, WhereClauseAdhesive adhesive) { string symbol; string valueSymbol; switch (methodCallExpression.Method.Name) { case "Equals": symbol = "= {0}"; valueSymbol = "{0}"; break; case "StartsWith": symbol = "like {0}"; valueSymbol = "{0}%"; break; case "EndsWith": symbol = "like {0}"; valueSymbol = "%{0}"; break; case "Contains": symbol = "like {0}"; valueSymbol = "%{0}%"; break; default: throw new NotSupportedException($"Not support method name:{methodCallExpression.Method.Name}"); } if (methodCallExpression.Object is MemberExpression memberExpression) { var memberInfo = memberExpression.Member; string fieldName = adhesive.SqlAdapter.FormatColumnName(memberInfo); string parameterName = EnsureParameter(memberInfo, adhesive); object value = ConstantExtractor.ParseConstant(methodCallExpression.Arguments[0]); adhesive.Parameters.Add($"@{parameterName}", string.Format(valueSymbol, value)); return(new StringBuilder(string.Format($"{fieldName} {symbol}", $"@{parameterName}"))); } else { throw new NotSupportedException(); } }
public static StringBuilder Parse(Expression expression, WhereClauseAdhesive adhesive) { if (expression is BinaryExpression binaryExpression) { if (IsLogicType(binaryExpression.NodeType)) { StringBuilder sqlBuilder = new StringBuilder(); var leftClause = Parse(binaryExpression.Left, adhesive); sqlBuilder.Append($"({leftClause})"); sqlBuilder.Append($" {binaryExpression.NodeType.ToLogicSymbol()} "); var rightClause = Parse(binaryExpression.Right, adhesive); sqlBuilder.Append($"({rightClause})"); return(sqlBuilder); } else if (binaryExpression.Left is UnaryExpression convertExpression && convertExpression.NodeType == ExpressionType.Convert && convertExpression.Operand.Type.IsEnum && convertExpression.Operand is MemberExpression enumMemberExpression && IsDataComparator(binaryExpression.NodeType)) { //Support the enum Property, For example: u.UserType == UserType.Admin return(ConditionBuilder.BuildCondition(enumMemberExpression.Member, adhesive, binaryExpression.NodeType, ConstantExtractor.ParseConstant(binaryExpression.Right))); } else if (binaryExpression.Left is MemberExpression memberExpression && IsDataComparator(binaryExpression.NodeType)) { //Basic case, For example: u.Age > 18 return(ConditionBuilder.BuildCondition(memberExpression.Member, adhesive, binaryExpression.NodeType, ConstantExtractor.ParseConstant(binaryExpression.Right))); }