Example #1
0
 protected override string SubstringStatement(PartialSqlString columnName, int startIndex, int length)
 {
     // Substring function doesn't work with parameters
     if (length >= 0)
     {
         return(string.Format("substring({0} FROM {1} FOR {2})", columnName, startIndex, length));
     }
     else
     {
         return(string.Format("substring({0} FROM {1})", columnName, startIndex));
     }
 }
Example #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(CreateParam(result)));
                }

                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 = CreateParam(Enum.ToObject(enumType, numvericVal));
                    }
                    else
                    {
                        right = CreateParam(right);
                    }
                }
                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 = CreateParam(Enum.ToObject(enumType, numvericVal));
                    }
                    else
                    {
                        left = CreateParam(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 = CreateParam(left);
                }
                else if (right as PartialSqlString == null)
                {
                    right = CreateParam(right);
                }
            }

            if (operand == "=" && right.ToString().Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                operand = "is";
            }
            else if (operand == "<>" && right.ToString().Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                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 + ")"));
            }
        }