public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            if (expressionNode is MemberExpression)
            {
                var name = ((MemberExpression)expressionNode).Member.Name;

                if (ModelStrategy == null)
                {
                    return(name);
                }
                var propQuery = ModelStrategy.PropertyStrategies.Where(c => c.GetPropertyInfo().Name == name).ToList();

                return((propQuery.Any()) ? propQuery.First().GetParameterName() : name);
            }

            if (expressionNode is UnaryExpression)
            {
                var exp = (UnaryExpression)expressionNode;

                if (exp.Operand is MemberExpression)
                {
                    var me = (MemberExpression)exp.Operand;

                    if (me.Expression.NodeType == ExpressionType.Constant)
                    {
                        var constantExp = new SqlDbQueryConstantExpressionNode()
                        {
                            ModelStrategy = ModelStrategy
                        };

                        var value = Expression.Lambda(exp).Compile().DynamicInvoke();
                        return(constantExp.Parse(parameterDictionary, Expression.Constant(value)));
                    }

                    var name = me.Member.Name;

                    if (ModelStrategy == null)
                    {
                        return(name);
                    }

                    var propQuery = ModelStrategy.PropertyStrategies.Where(c => c.GetPropertyInfo().Name == name).ToList();

                    return(propQuery.Any() ? propQuery.First().GetParameterName() : name);
                }

                if (exp.Operand is BinaryExpression)
                {
                    var binaryExp = (BinaryExpression)exp.Operand;
                    var binaryExpStringBuilder = new StringBuilder();

                    // handle left expression.
                    var leftExpNode  = SqlDbQueryExpressionFactory.GetNodeParser(binaryExp.Left, ModelStrategy);
                    var rightExpNode = SqlDbQueryExpressionFactory.GetNodeParser(binaryExp.Right, ModelStrategy);

                    binaryExpStringBuilder.Append(leftExpNode.Parse(parameterDictionary, binaryExp.Left));

                    switch (binaryExp.NodeType)
                    {
                    case ExpressionType.Equal:
                        binaryExpStringBuilder.Append(" = ");
                        break;

                    case ExpressionType.NotEqual:
                        binaryExpStringBuilder.Append(" <> ");
                        break;

                    case ExpressionType.GreaterThan:
                        binaryExpStringBuilder.Append(" > ");
                        break;

                    case ExpressionType.GreaterThanOrEqual:
                        binaryExpStringBuilder.Append(" >= ");
                        break;

                    case ExpressionType.LessThan:
                        binaryExpStringBuilder.Append(" < ");
                        break;

                    case ExpressionType.LessThanOrEqual:
                        binaryExpStringBuilder.Append(" <= ");
                        break;

                    case ExpressionType.AndAlso:
                        binaryExpStringBuilder.Append(" AND ");
                        break;

                    case ExpressionType.OrElse:
                        binaryExpStringBuilder.Append(" OR ");
                        break;

                    default:
                        throw new NotSupportedException("ERROR_NOT_SUPPORTED_CONDITION_EXPRESSION_TYPE");
                    }

                    binaryExpStringBuilder.Append(rightExpNode.Parse(parameterDictionary, binaryExp.Right));

                    return(binaryExpStringBuilder.ToString());
                }

                if (exp.Operand is MethodCallExpression)
                {
                    var callExpressionNode = new SqlDbQueryCallExpressionNode()
                    {
                        ModelStrategy = ModelStrategy
                    };
                    return(callExpressionNode.Parse(parameterDictionary, exp.Operand));
                }

                SqlDbQueryExpressionNode node;

                if (exp.NodeType == ExpressionType.Constant)
                {
                    node = new SqlDbQueryConstantExpressionNode()
                    {
                        ModelStrategy = ModelStrategy
                    };

                    return(node.Parse(parameterDictionary, exp));
                }

                if (exp.NodeType != ExpressionType.Convert)
                {
                    throw new NotSupportedException("ERROR_NOT_SUPPORTED_UNARY_EXPRESSION_TYPE");
                }

                node = new SqlDbQueryConstantExpressionNode()
                {
                    ModelStrategy = ModelStrategy
                };

                return(node.Parse(parameterDictionary, Expression.Constant(Expression.Lambda(exp).Compile().DynamicInvoke())));
            }

            throw new NotSupportedException("ERROR_NOT_SUPPORTED_EXPRESSION_TYPE");
        }
예제 #2
0
        public static IDbQueryExpressionNode GetNodeParser(Expression expression, IDbModelStrategy modelStrategy)
        {
            SqlDbQueryExpressionNode expressionNode = null;

            switch (expression.NodeType)
            {
            case ExpressionType.NotEqual:
                expressionNode = new SqlDbQueryNotEqualExpressionNode();
                break;

            case ExpressionType.Not:
                expressionNode = new SqlDbQueryNotExpressionNode();
                break;

            case ExpressionType.Equal:
                expressionNode = new SqlDbQueryEqualExpressionNode();
                break;

            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
                expressionNode = new SqlDbQueryGreaterThanExpressionNode();
                break;

            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
                expressionNode = new SqlDbQueryLessThanExpressionNode();
                break;

            case ExpressionType.Add:
            case ExpressionType.AddChecked:
                expressionNode = new SqlDbQueryAddExpressionNode();
                break;

            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
                expressionNode = new SqlDbQuerySubstractExpressionNode();
                break;

            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
                expressionNode = new SqlDbQueryMultiplyExpressionNode();
                break;

            case ExpressionType.Divide:
                expressionNode = new SqlDbQueryDivideExpressionNode();
                break;

            case ExpressionType.MemberAccess:
                expressionNode = new SqlDbQueryMemberAccessExpressionNode();
                break;

            case ExpressionType.AndAlso:
                expressionNode = new SqlDbQueryAndAlsoExpressionNode();
                break;

            case ExpressionType.OrElse:
                expressionNode = new SqlDbQueryOrElseExpressionNode();
                break;

            case ExpressionType.Constant:
                expressionNode = new SqlDbQueryConstantExpressionNode();
                break;

            case ExpressionType.Call:
                expressionNode = new SqlDbQueryCallExpressionNode();
                break;

            case ExpressionType.Parameter:
                expressionNode = new SqlDbQueryParameterExpressionNode();
                break;

            case ExpressionType.Convert:
                expressionNode = new SqlDbQueryConvertExpressionNode();
                break;

            default:
                throw new NotSupportedException("ERROR_GIVEN_QUERY_EXPRESSION_TYPE_NOT_SUPPORTED");
            }

            expressionNode.ModelStrategy = modelStrategy;
            return(expressionNode);
        }