Ejemplo n.º 1
0
        public override string Parse(IDictionary <string, object> parameterDictionary, Expression expressionNode)
        {
            var expressionBuilder = new StringBuilder();
            var exp = expressionNode as ConstantExpression;

            if (exp.Value == null)
            {
                expressionBuilder.Append("NULL");
            }
            else
            {
                // create parameter.
                string pname = ParameterUtils.CreateNewParameter(ref parameterDictionary);
                parameterDictionary[pname] = exp.Value.ToString();
                expressionBuilder.Append("@" + pname);

                //if (exp.Value.GetType() == typeof(string))
                //{
                //    ParameterDictionary[pname] = exp.Value.ToString();
                //    expressionBuilder.Append("@" + pname);
                //}
                //else if (exp.Value.GetType() == typeof(DateTime))
                //{
                //    ParameterDictionary[pname] = Convert.ToDateTime(exp.Value);
                //    expressionBuilder.Append("@" + pname);
                //}
                //else
            }

            return(expressionBuilder.ToString());
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
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());
        }