Esempio n. 1
0
        protected virtual object VisitBinary(BinaryExpression b)
        {
            object left, right;
            var    operand = BindOperant(b.NodeType); //sep= " " ??

            if (operand == "AND" || operand == "OR")
            {
                var m = b.Left as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    left = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    left = Visit(b.Left);
                }

                m = b.Right as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    right = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    right = Visit(b.Right);
                }

                if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(new PartialSqlString(OrmLiteConfig.DialectProvider.GetQuotedValue(result, result.GetType())));
                }

                if (left as PartialSqlString == null)
                {
                    left = ((bool)left) ? GetTrueExpression() : GetFalseExpression();
                }
                if (right as PartialSqlString == null)
                {
                    right = ((bool)right) ? GetTrueExpression() : GetFalseExpression();
                }
            }
            else
            {
                left  = Visit(b.Left);
                right = Visit(b.Right);

                var leftEnum           = left as EnumMemberAccess;
                var rightEnum          = right as EnumMemberAccess;
                var rightNeedsCoercing = leftEnum != null && rightEnum == null;
                var leftNeedsCoercing  = rightEnum != null && leftEnum == null;

                if (rightNeedsCoercing)
                {
                    var rightPartialSql = right as PartialSqlString;
                    if (rightPartialSql == null)
                    {
                        right = ConvertToEnum(leftEnum.EnumType, right.ToString(), right);
                    }
                }
                else if (leftNeedsCoercing)
                {
                    var leftPartialSql = left as PartialSqlString;
                    if (leftPartialSql == null)
                    {
                        left = ConvertToEnum(rightEnum.EnumType, left.ToString(), left);
                    }
                }
                else if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(result);
                }
                else if (left as PartialSqlString == null)
                {
                    left = OrmLiteConfig.DialectProvider.GetQuotedValue(left, left != null ? left.GetType() : null);
                }
                else if (right as PartialSqlString == null)
                {
                    right = OrmLiteConfig.DialectProvider.GetQuotedValue(right, right != null ? right.GetType() : null);
                }
            }

            if (operand == "=" && right.ToString().Equals("null", StringComparison.OrdinalIgnoreCase))
            {
                operand = "is";
            }
            else if (operand == "<>" && right.ToString().Equals("null", StringComparison.OrdinalIgnoreCase))
            {
                operand = "is not";
            }

            switch (operand)
            {
            case "MOD":
            case "COALESCE":
                return(new PartialSqlString(string.Format("{0}({1},{2})", operand, left, right)));

            default:
                return(new PartialSqlString("(" + left + sep + operand + sep + right + ")"));
            }
        }
Esempio n. 2
0
        protected virtual object VisitBinary(BinaryExpression b)
        {
            object left, right;
            var    operand = BindOperant(b.NodeType); //sep= " " ??

            if (operand == "AND" || operand == "OR")
            {
                var m = b.Left as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    left = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    left = Visit(b.Left);
                }

                m = b.Right as MemberExpression;
                if (m != null && m.Expression != null &&
                    m.Expression.NodeType == ExpressionType.Parameter)
                {
                    right = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue()));
                }
                else
                {
                    right = Visit(b.Right);
                }

                if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(new PartialSqlString(OrmLiteConfig.DialectProvider.GetQuotedValue(result, result.GetType())));
                }

                if (left as PartialSqlString == null)
                {
                    left = ((bool)left) ? GetTrueExpression() : GetFalseExpression();
                }
                if (right as PartialSqlString == null)
                {
                    right = ((bool)right) ? GetTrueExpression() : GetFalseExpression();
                }
            }
            else
            {
                left  = Visit(b.Left);
                right = Visit(b.Right);

                if (left as EnumMemberAccess != null && right as PartialSqlString == null)
                {
                    var enumType = ((EnumMemberAccess)left).EnumType;

                    //enum value was returned by Visit(b.Right)
                    long numvericVal;
                    if (Int64.TryParse(right.ToString(), out numvericVal))
                    {
                        right = OrmLiteConfig.DialectProvider.GetQuotedValue(Enum.ToObject(enumType, numvericVal).ToString(),
                                                                             typeof(string));
                    }
                    else
                    {
                        right = OrmLiteConfig.DialectProvider.GetQuotedValue(right, right.GetType());
                    }
                }
                else if (right as EnumMemberAccess != null && left as PartialSqlString == null)
                {
                    var enumType = ((EnumMemberAccess)right).EnumType;

                    //enum value was returned by Visit(b.Left)
                    long numvericVal;
                    if (Int64.TryParse(left.ToString(), out numvericVal))
                    {
                        left = OrmLiteConfig.DialectProvider.GetQuotedValue(Enum.ToObject(enumType, numvericVal).ToString(),
                                                                            typeof(string));
                    }
                    else
                    {
                        left = OrmLiteConfig.DialectProvider.GetQuotedValue(left, left.GetType());
                    }
                }
                else if (left as PartialSqlString == null && right as PartialSqlString == null)
                {
                    var result = Expression.Lambda(b).Compile().DynamicInvoke();
                    return(result);
                }
                else if (left as PartialSqlString == null)
                {
                    left = OrmLiteConfig.DialectProvider.GetQuotedValue(left, left != null ? left.GetType() : null);
                }
                else if (right as PartialSqlString == null)
                {
                    right = OrmLiteConfig.DialectProvider.GetQuotedValue(right, right != null ? right.GetType() : null);
                }
            }

            if (operand == "=" && right.ToString() == "null")
            {
                operand = "is";
            }
            else if (operand == "<>" && right.ToString() == "null")
            {
                operand = "is not";
            }

            switch (operand)
            {
            case "MOD":
            case "COALESCE":
                return(new PartialSqlString(string.Format("{0}({1},{2})", operand, left, right)));

            default:
                return(new PartialSqlString("(" + left + sep + operand + sep + right + ")"));
            }
        }