private static void VisitBinary(BinaryExpression node, SQLQueryPredicate predicate)
        {
            var parameterName = node.Left.NodeType == ExpressionType.Convert ? VisitMember(((UnaryExpression)node.Left).Operand as MemberExpression).ToString()
                                                                             : VisitMember(node.Left as MemberExpression).ToString();

            predicate.ParameterSQLName = SqlMapperExtensions.EncaseWithSquareBrackets(parameterName);
            switch (node.Right.NodeType)
            {
            case ExpressionType.Convert:
                predicate.ParameterSQLValue = VisitMember((node.Right as UnaryExpression).Operand as MemberExpression);
                break;

            case ExpressionType.Constant:
                predicate.ParameterSQLValue = VisitConstant(node.Right as ConstantExpression);
                break;

            case ExpressionType.MemberAccess:
                predicate.ParameterSQLValue = VisitMember(node.Right as MemberExpression);
                break;

            default:
                throw new NotSupportedException($"The query expression type {node.Right.NodeType} is not supported");
            }
            predicate.ParameterArgumentSQLName = parameterName;
        }
Exemple #2
0
 internal void AddPredicate(SQLQueryPredicate predicate, string andOr)
 {
     Predicates.Add(predicate);
     if (!string.IsNullOrEmpty(andOr))
     {
         ArgumentJoiningMap[predicate] = andOr;
     }
 }
Exemple #3
0
        private static SQLQuery GetQuery(IEnumerable <SQLPredicateComponent> predicates)
        {
            var result = new SQLQuery();

            foreach (var predicate in predicates)
            {
                result.AddPredicate(SQLQueryPredicate.Build(predicate.Expression), predicate.FollowingOperator);
            }
            return(result);
        }
        public static SQLQueryPredicate Build(Expression expression)
        {
            var result = new SQLQueryPredicate {
                SourceExpression = expression
            };
            string parameterName;

            switch (expression.NodeType)
            {
            case ExpressionType.GreaterThanOrEqual:
                result.SQLOperator = ">=";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.LessThanOrEqual:
                result.SQLOperator = "<=";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.LessThan:
                result.SQLOperator = "<";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.GreaterThan:
                result.SQLOperator = ">";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.Equal:
                result.SQLOperator = "=";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.Not:
                return(Build(Expression.Equal(((UnaryExpression)expression).Operand, Expression.Constant(false))));

            case ExpressionType.NotEqual:
                result.SQLOperator = "<>";
                VisitBinary(expression as BinaryExpression, result);
                break;

            case ExpressionType.Lambda:
                return(Build((expression as LambdaExpression).Body));

            case ExpressionType.Convert:
                return(Build((expression as UnaryExpression).Operand));

            case ExpressionType.Constant:
                parameterName                   = VisitMember(expression as MemberExpression).ToString();
                result.ParameterSQLName         = SqlMapperExtensions.EncaseWithSquareBrackets(parameterName);
                result.ParameterSQLValue        = VisitConstant(expression as ConstantExpression);
                result.ParameterArgumentSQLName = parameterName;
                break;

            case ExpressionType.MemberAccess:
                parameterName           = VisitMember(expression as MemberExpression).ToString();
                result.ParameterSQLName = SqlMapperExtensions.EncaseWithSquareBrackets(parameterName);
                PropertyInfo boolMemberAccess;
                if (TryCheckForMemberAccessBoolExpression(expression, out boolMemberAccess))
                {
                    result.SQLOperator       = "=";
                    result.ParameterSQLValue = true;
                }
                else
                {
                    // ???
                    throw new NotImplementedException();
                }
                result.ParameterArgumentSQLName = parameterName;
                break;

            default:
                throw new NotSupportedException($"The query expression type {expression.NodeType} is not supported");
            }
            return(result);
        }