Beispiel #1
0
        public void Process(DbMethodCallExpression exp, SqlGenerator generator)
        {
            DbExpression arg = exp.Arguments[0];
            DbExpression e   = DbExpression.Convert(arg, exp.Method.ReturnType);

            e.Accept(generator);
        }
        public override DbExpression Visit(DbConvertExpression exp)
        {
            exp = DbExpression.Convert(exp.Operand.Accept(this), exp.Type);

            if (!IsConstantOrParameter(exp.Operand))
            {
                return(exp);
            }

            return(DbExpression.Parameter(exp.Evaluate(), exp.Type));
        }
Beispiel #3
0
        public void Process(DbMethodCallExpression exp, SqlGenerator generator)
        {
            if (exp.Object.Type == PublicConstants.TypeOfString)
            {
                exp.Object.Accept(generator);
                return;
            }

            DbConvertExpression c = DbExpression.Convert(exp.Object, PublicConstants.TypeOfString);

            c.Accept(generator);
        }
Beispiel #4
0
        public void Process(DbMethodCallExpression exp, SqlGenerator generator)
        {
            DbExpression arg = exp.Arguments[0];
            DbExpression e   = DbExpression.Convert(arg, exp.Method.ReturnType);

            if (exp.Method.ReturnType == PublicConstants.TypeOfBoolean)
            {
                e.Accept(generator);
                generator.SqlBuilder.Append(" = ");
                DbConstantExpression.True.Accept(generator);
            }
            else
            {
                e.Accept(generator);
            }
        }
Beispiel #5
0
        public void Process(DbMethodCallExpression exp, SqlGenerator generator)
        {
            MethodInfo method = exp.Method;

            if (method.DeclaringType == UtilConstants.TypeOfSql)
            {
                Method_Sql_Equals(exp, generator);
                return;
            }

            DbExpression right = exp.Arguments[0];

            if (right.Type != exp.Object.Type)
            {
                right = DbExpression.Convert(right, exp.Object.Type);
            }

            DbExpression.Equal(exp.Object, right).Accept(generator);
        }
        public override object Visit(DbConvertExpression exp)
        {
            object operandValue = exp.Operand.Accept(this);

            //(int)null
            if (operandValue == null)
            {
                //(int)null
                if (exp.Type.IsValueType && !exp.Type.IsNullable())
                {
                    throw new NullReferenceException();
                }

                return(null);
            }

            Type operandValueType = operandValue.GetType();

            if (exp.Type == operandValueType || exp.Type.IsAssignableFrom(operandValueType))
            {
                return(operandValue);
            }

            Type underlyingType;

            if (exp.Type.IsNullable(out underlyingType))
            {
                //(int?)int
                if (underlyingType == operandValueType)
                {
                    var constructor = exp.Type.GetConstructor(new Type[] { operandValueType });
                    var val         = constructor.Invoke(new object[] { operandValue });
                    return(val);
                }
                else
                {
                    //如果不等,则诸如:(long?)int / (long?)int?  -->  (long?)((long)int) / (long?)((long)int?)
                    var c  = DbExpression.Convert(DbExpression.Constant(operandValue), underlyingType);
                    var cc = DbExpression.Convert(c, exp.Type);
                    return(this.Visit(cc));
                }
            }

            //(int)int?
            if (operandValueType.IsNullable(out underlyingType))
            {
                if (underlyingType == exp.Type)
                {
                    var pro = operandValueType.GetProperty("Value");
                    var val = pro.GetValue(operandValue, null);
                    return(val);
                }
                else
                {
                    //如果不等,则诸如:(long)int?  -->  (long)((long)int)
                    var c  = DbExpression.Convert(DbExpression.Constant(operandValue), underlyingType);
                    var cc = DbExpression.Convert(c, exp.Type);
                    return(this.Visit(cc));
                }
            }

            if (exp.Type.IsEnum)
            {
                return(Enum.ToObject(exp.Type, operandValue));
            }

            //(long)int
            if (operandValue is IConvertible)
            {
                return(Convert.ChangeType(operandValue, exp.Type));
            }

            throw new NotSupportedException(string.Format("Does not support the type '{0}' converted to type '{1}'.", operandValueType.FullName, exp.Type.FullName));
        }
Beispiel #7
0
        static void StringConcat(DbBinaryExpression exp, SqlGeneratorBase generator)
        {
            List <DbExpression> operands = new List <DbExpression>();

            operands.Add(exp.Right);

            DbExpression    left = exp.Left;
            DbAddExpression e    = null;

            while ((e = (left as DbAddExpression)) != null && (e.Method == PublicConstants.MethodInfo_String_Concat_String_String || e.Method == PublicConstants.MethodInfo_String_Concat_Object_Object))
            {
                operands.Add(e.Right);
                left = e.Left;
            }

            operands.Add(left);

            DbExpression        whenExp     = null;
            List <DbExpression> operandExps = new List <DbExpression>(operands.Count);

            for (int i = operands.Count - 1; i >= 0; i--)
            {
                DbExpression operand = operands[i];
                DbExpression opBody  = operand;
                if (opBody.Type != PublicConstants.TypeOfString)
                {
                    // 需要 cast type
                    opBody = DbExpression.Convert(opBody, PublicConstants.TypeOfString);
                }

                DbExpression equalNullExp = DbExpression.Equal(opBody, PublicConstants.DbConstant_Null_String);

                if (whenExp == null)
                {
                    whenExp = equalNullExp;
                }
                else
                {
                    whenExp = DbExpression.And(whenExp, equalNullExp);
                }

                operandExps.Add(opBody);
            }

            generator.SqlBuilder.Append("CASE", " WHEN ");
            whenExp.Accept(generator);
            generator.SqlBuilder.Append(" THEN ");
            DbConstantExpression.Null.Accept(generator);
            generator.SqlBuilder.Append(" ELSE ");

            generator.SqlBuilder.Append("(");

            for (int i = 0; i < operandExps.Count; i++)
            {
                if (i > 0)
                {
                    generator.SqlBuilder.Append(" + ");
                }

                generator.SqlBuilder.Append("ISNULL(");
                operandExps[i].Accept(generator);
                generator.SqlBuilder.Append(",");
                DbConstantExpression.StringEmpty.Accept(generator);
                generator.SqlBuilder.Append(")");
            }

            generator.SqlBuilder.Append(")");

            generator.SqlBuilder.Append(" END");
        }
Beispiel #8
0
 public override DbExpression Visit(DbConvertExpression exp)
 {
     exp = DbExpression.Convert(exp.Operand.Accept(this), exp.Type);
     return(exp);
 }