Ejemplo n.º 1
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var expressionBuilder = new StringBuilder();
            var exp      = expressionNode as BinaryExpression;
            var leftExp  = SqlDbQueryExpressionFactory.GetNodeParser(exp.Left, ModelStrategy);
            var rightExp = SqlDbQueryExpressionFactory.GetNodeParser(exp.Right, ModelStrategy);

            expressionBuilder.Append(leftExp.Parse(parameterDictionary, exp.Left));

            switch (exp.NodeType)
            {
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
                expressionBuilder.Append(" + ");
                break;

            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
                expressionBuilder.Append(" - ");
                break;

            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
                expressionBuilder.Append(" * ");
                break;

            case ExpressionType.Divide:
                expressionBuilder.Append(" / ");
                break;
            }

            expressionBuilder.Append(rightExp.Parse(parameterDictionary, exp.Right));

            return(expressionBuilder.ToString());
        }
Ejemplo n.º 2
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var  exp = expressionNode as BinaryExpression;
            var  expressionBuilder = new StringBuilder();
            var  leftExp = SqlDbQueryExpressionFactory.GetNodeParser(exp.Left, ModelStrategy);
            var  rightExp = SqlDbQueryExpressionFactory.GetNodeParser(exp.Right, ModelStrategy);
            bool leftNull = false, rightNull = false;

            if ((exp.Left is ConstantExpression))
            {
                leftNull = (exp.Left as ConstantExpression).Value == null;
            }
            if ((exp.Right is ConstantExpression))
            {
                rightNull = (exp.Right as ConstantExpression).Value == null;
            }

            expressionBuilder.Append("(");
            expressionBuilder.Append(leftExp.Parse(parameterDictionary, exp.Left));

            if (leftNull || rightNull)
            {
                expressionBuilder.Append(" IS ");
            }
            else
            {
                expressionBuilder.Append(" = ");
            }

            expressionBuilder.Append(rightExp.Parse(parameterDictionary, exp.Right));
            expressionBuilder.Append(")");

            return(expressionBuilder.ToString());
        }
Ejemplo n.º 3
0
        public string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode, IDbModelStrategy modelStrategy)
        {
            var queryNode = SqlDbQueryExpressionFactory.GetNodeParser(expressionNode, modelStrategy);

            if (queryNode == null)
            {
                throw new NotSupportedException("ERROR_NODE_TYPE_NOT_SUPPORTED");
            }

            return(queryNode.Parse(parameterDictionary, expressionNode));
        }
Ejemplo n.º 4
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var expressionBuilder = new StringBuilder();
            var exp      = expressionNode as BinaryExpression;
            var leftExp  = SqlDbQueryExpressionFactory.GetNodeParser(exp.Left, ModelStrategy);
            var rightExp = SqlDbQueryExpressionFactory.GetNodeParser(exp.Right, ModelStrategy);

            expressionBuilder.Append(leftExp.Parse(parameterDictionary, exp.Left));
            expressionBuilder.Append(" / ");
            expressionBuilder.Append(rightExp.Parse(parameterDictionary, exp.Right));

            return(expressionBuilder.ToString());
        }
Ejemplo n.º 5
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var expressionBuilder         = new StringBuilder();
            IDbQueryExpressionNode parser = null;

            if (expressionNode is UnaryExpression)
            {
                var unaryExp = expressionNode as UnaryExpression;
                parser = SqlDbQueryExpressionFactory.GetNodeParser(unaryExp.Operand, ModelStrategy);
                expressionBuilder.Append(parser.Parse(parameterDictionary, unaryExp.Operand));
            }
            else
            {
                throw new NotSupportedException();
            }

            return(expressionBuilder.ToString());
        }
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var sqlsb  = new StringBuilder();
            var newExp = expressionNode as NewExpression;

            if (_convertPropertyNameToDbName)
            {
                foreach (var memberAccessItem in newExp.Arguments)
                {
                    var memberAccessNode = SqlDbQueryExpressionFactory.GetNodeParser(memberAccessItem, ModelStrategy);

                    if (sqlsb.Length == 0)
                    {
                        sqlsb.Append(memberAccessNode.Parse(parameterDictionary, memberAccessItem));
                    }
                    else
                    {
                        sqlsb.Append(", ");
                        sqlsb.Append(memberAccessNode.Parse(parameterDictionary, memberAccessItem));
                    }
                }
            }
            else
            {
                foreach (var memberAccessItem in newExp.Arguments)
                {
                    SqlDbQueryExpressionNode memberAccessNode = new SqlDbQueryMemberAccessExpressionNode();
                    memberAccessNode.ModelStrategy = ModelStrategy;

                    if (sqlsb.Length == 0)
                    {
                        sqlsb.Append(memberAccessNode.Parse(parameterDictionary, memberAccessItem));
                    }
                    else
                    {
                        sqlsb.Append(", ");
                        sqlsb.Append(memberAccessNode.Parse(parameterDictionary, memberAccessItem));
                    }
                }
            }

            return(sqlsb.ToString());
        }
Ejemplo n.º 7
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var exp = expressionNode as UnaryExpression;
            var expressionBuilder  = new StringBuilder();
            var opExpressionParser = SqlDbQueryExpressionFactory.GetNodeParser(exp.Operand, ModelStrategy);

            if (exp.Operand.Type == typeof(bool))
            {
                string pname = ParameterUtils.CreateNewParameter(ref parameterDictionary);

                expressionBuilder.Append("(");
                expressionBuilder.Append(opExpressionParser.Parse(parameterDictionary, exp.Operand));
                expressionBuilder.Append(string.Format(" = @{0})", pname));
                parameterDictionary[pname] = 0;
            }
            else
            {
                expressionBuilder.Append("( NOT (");
                expressionBuilder.Append(opExpressionParser.Parse(parameterDictionary, exp.Operand));
                expressionBuilder.Append(") )");
            }

            return(expressionBuilder.ToString());
        }
        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");
        }
Ejemplo n.º 9
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var exp = expressionNode as MemberExpression;
            var expressionBuilder = new StringBuilder();

            if (exp.Member.MemberType == MemberTypes.Property)
            {
                if (exp.Expression == null)
                {
                    var callExpression = new SqlDbQueryCallExpressionNode()
                    {
                        ModelStrategy = ModelStrategy
                    };

                    expressionBuilder.Append(callExpression.Parse(parameterDictionary, exp));
                }
                else
                {
                    var  expParser = SqlDbQueryExpressionFactory.GetNodeParser(exp.Expression, ModelStrategy);
                    bool handled   = false;

                    foreach (var functionNode in _supportedFunctionNodes)
                    {
                        if (functionNode.CheckForHandle(exp.Member.Name))
                        {
                            ((SqlDbQueryExpressionNode)functionNode).ModelStrategy = ModelStrategy;
                            expressionBuilder.Append(functionNode.Parse(parameterDictionary, exp.Expression));
                            handled = true;
                            break;
                        }
                    }

                    if (!handled)
                    {
                        // get alias type.
                        Type t = exp.Expression.Type;

                        // check this is field (must render as parameter, not field)
                        // solution referenced from
                        // http://stackoverflow.com/questions/6635678/how-to-recognize-a-lambda-memberexpression-of-type-field-reference
                        bool isQueryParameterProperty =
                            exp.Member is PropertyInfo && !(exp.Expression.NodeType == ExpressionType.Parameter);
                        var propQuery = ModelStrategy.PropertyStrategies.Where(c => c.GetPropertyInfo().Name == exp.Member.Name);

                        if (propQuery.Any())
                        {
                            if (!isQueryParameterProperty)
                            {
                                //if (this._requireToRenderAlias)
                                //{
                                //    expressionBuilder.Append(this.ModelStrategy.GetTableAlias());
                                //    expressionBuilder.Append(".");
                                //}

                                expressionBuilder.Append(propQuery.First().GetParameterName());
                            }
                            else
                            {
                                MemberExpression memberExp = exp.Expression as MemberExpression;
                                string           pname     = ParameterUtils.CreateNewParameter(ref parameterDictionary);
                                object           value     = null;

                                if ((memberExp == null && exp.Expression is ConstantExpression))
                                {
                                    value = ExtractValueFromConstantExpression(
                                        (exp.Expression as ConstantExpression), exp.Member.Name, exp.Member.Name);
                                }
                                else if (memberExp.Expression is ConstantExpression)
                                {
                                    value = ExtractValueFromConstantExpression(
                                        (memberExp.Expression as ConstantExpression), memberExp.Member.Name, exp.Member.Name);
                                }
                                else
                                {
                                    value = ExecuteLambdaExpression(exp);
                                }

                                if (value.GetType() == typeof(DateTime))
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = Convert.ToDateTime(value);
                                }
                                else if (value.GetType() == typeof(Guid))
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = new Guid(value.ToString());
                                }
                                else if (exp.Type == typeof(byte[]) || exp.Type == typeof(byte))
                                {
                                    if (value != null)
                                    {
                                        byte[] byteArrayData = null;

                                        if (exp.Type == typeof(byte[]))
                                        {
                                            byteArrayData = (byte[])value;
                                        }
                                        else
                                        {
                                            byteArrayData = new byte[] { (byte)value }
                                        };

                                        expressionBuilder.Append(string.Format("@{0}", pname));
                                        parameterDictionary[pname] = byteArrayData;

                                        //expressionBuilder.Append("CONVERT(varbinary(max), ");
                                        //expressionBuilder.Append("0x" + byteArrayData.ToHexString(true));
                                        //expressionBuilder.Append(")");
                                    }
                                }
                                else if (value.GetType().IsValueType)
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = value;
                                }
                                else
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = value.ToString();
                                }
                            }
                        }
                        else
                        {
                            if (!isQueryParameterProperty)
                            {
                                //if (this._requireToRenderAlias)
                                //{
                                //    expressionBuilder.Append(this.ModelStrategy.GetTableAlias());
                                //    expressionBuilder.Append(".");
                                //}

                                expressionBuilder.Append(exp.Member.Name);
                            }
                            else
                            {
                                MemberExpression memberExp = exp.Expression as MemberExpression;
                                string           pname     = ParameterUtils.CreateNewParameter(ref parameterDictionary);
                                object           value     = null;

                                if (memberExp.Expression is ConstantExpression)
                                {
                                    var constantExp = memberExp.Expression as ConstantExpression;
                                    value = ExtractValueFromConstantExpression(
                                        constantExp, memberExp.Member.Name, exp.Member.Name);
                                }
                                else
                                {
                                    value = ExecuteLambdaExpression(exp);
                                }

                                if (value.GetType() == typeof(DateTime))
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = Convert.ToDateTime(value);
                                }
                                else if (value.GetType() == typeof(Guid))
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = new Guid(value.ToString());
                                }
                                else if (exp.Type == typeof(byte[]) || exp.Type == typeof(byte))
                                {
                                    if (value != null)
                                    {
                                        byte[] byteArrayData = null;

                                        if (exp.Type == typeof(byte[]))
                                        {
                                            byteArrayData = (byte[])value;
                                        }
                                        else
                                        {
                                            byteArrayData = new byte[] { (byte)value }
                                        };

                                        expressionBuilder.Append(string.Format("@{0}", pname));
                                        parameterDictionary[pname] = byteArrayData;

                                        //expressionBuilder.Append("CONVERT(varbinary(max), ");
                                        //expressionBuilder.Append("0x" + byteArrayData.ToHexString(true));
                                        //expressionBuilder.Append(")");
                                    }
                                }
                                else if (exp.Type.IsArray)
                                {
                                    object[] array   = value as object[];
                                    bool     isFirst = true;

                                    expressionBuilder.Append("(");

                                    foreach (object arrayValue in array)
                                    {
                                        string arrayPName = ParameterUtils.CreateNewParameter(ref parameterDictionary);

                                        if (!isFirst)
                                        {
                                            expressionBuilder.Append(",");
                                            expressionBuilder.Append(string.Format("@{0}", arrayPName));

                                            if (arrayValue is string)
                                            {
                                                parameterDictionary[arrayPName] = arrayValue.ToString();
                                            }
                                            else
                                            {
                                                parameterDictionary[arrayPName] = arrayValue;
                                            }
                                        }
                                        else
                                        {
                                            expressionBuilder.Append(string.Format("@{0}", arrayPName));

                                            if (arrayValue is string)
                                            {
                                                parameterDictionary[arrayPName] = arrayValue.ToString();
                                            }
                                            else
                                            {
                                                parameterDictionary[arrayPName] = arrayValue;
                                            }

                                            isFirst = false;
                                        }
                                    }

                                    expressionBuilder.Append(")");
                                }
                                else if (value.GetType().IsValueType)
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = value;
                                }
                                else
                                {
                                    expressionBuilder.Append(string.Format("@{0}", pname));
                                    parameterDictionary[pname] = value.ToString();
                                }
                            }
                        }
                    }
                }
            }
            else if (exp.Member.MemberType == MemberTypes.Field)
            {
                object v     = Expression.Lambda(exp).Compile().DynamicInvoke();
                string pname = ParameterUtils.CreateNewParameter(ref parameterDictionary);

                if (exp.Type == typeof(string))
                {
                    if (v != null)
                    {
                        expressionBuilder.Append(string.Format("@{0}", pname));
                        parameterDictionary[pname] = v.ToString();
                    }
                    else
                    {
                        parameterDictionary[pname] = DBNull.Value;
                    }
                }
                else if (exp.Type == typeof(DateTime))
                {
                    if (v != null)
                    {
                        expressionBuilder.Append(string.Format("@{0}", pname));
                        parameterDictionary[pname] = Convert.ToDateTime(v);
                    }
                }
                else if (exp.Type == typeof(byte[]) || exp.Type == typeof(byte))
                {
                    if (v != null)
                    {
                        byte[] byteArrayData = null;

                        if (exp.Type == typeof(byte[]))
                        {
                            byteArrayData = (byte[])v;
                        }
                        else
                        {
                            byteArrayData = new byte[] { (byte)v }
                        };

                        expressionBuilder.Append(string.Format("@{0}", pname));
                        parameterDictionary[pname] = Convert.ToDateTime(v);

                        //expressionBuilder.Append("CONVERT(varbinary(max), ");
                        //expressionBuilder.Append("0x" + byteArrayData.ToHexString(true));
                        //expressionBuilder.Append(")");
                    }
                }
                else if (exp.Type.IsArray)
                {
                    object[] array   = v as object[];
                    bool     isFirst = true;

                    expressionBuilder.Append("(");

                    foreach (object arrayValue in array)
                    {
                        string arrayPName = ParameterUtils.CreateNewParameter(ref parameterDictionary);

                        if (!isFirst)
                        {
                            expressionBuilder.Append(",");
                            expressionBuilder.Append(string.Format("@{0}", arrayPName));

                            if (arrayValue is string)
                            {
                                parameterDictionary[arrayPName] = arrayValue.ToString();
                            }
                            else
                            {
                                parameterDictionary[arrayPName] = arrayValue;
                            }
                        }
                        else
                        {
                            expressionBuilder.Append(string.Format("@{0}", arrayPName));

                            if (arrayValue is string)
                            {
                                parameterDictionary[arrayPName] = arrayValue.ToString();
                            }
                            else
                            {
                                parameterDictionary[arrayPName] = arrayValue;
                            }

                            isFirst = false;
                        }
                    }

                    expressionBuilder.Append(")");
                }
                else
                {
                    expressionBuilder.Append(v.ToString());
                }
            }
            else
            {
                var propQuery = ModelStrategy.PropertyStrategies.Where(c => c.GetPropertyInfo().Name == exp.Member.Name);

                if (propQuery.Any())
                {
                    //if (this._requireToRenderAlias)
                    //{
                    //    expressionBuilder.Append(this.ModelStrategy.GetTableAlias());
                    //    expressionBuilder.Append(".");
                    //}

                    expressionBuilder.Append(propQuery.First().GetParameterName());
                }
                else
                {
                    //if (this._requireToRenderAlias)
                    //{
                    //    expressionBuilder.Append(this.ModelStrategy.GetTableAlias());
                    //    expressionBuilder.Append(".");
                    //}

                    expressionBuilder.Append(exp.Member.Name);
                }
            }

            return(expressionBuilder.ToString());
        }