Exemplo n.º 1
0
 static void Method_LongCount(DbMethodCallExpression exp, SqlGenerator generator)
 {
     EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
     Aggregate_LongCount(generator);
 }
Exemplo n.º 2
0
 static void Method_Average(DbMethodCallExpression exp, SqlGenerator generator)
 {
     EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
     Aggregate_Average(generator, exp.Arguments.First(), exp.Method.ReturnType);
 }
Exemplo n.º 3
0
        static void Method_DiffMicroseconds(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethodDeclaringType(exp, typeof(DbFunctions), UtilConstants.TypeOfSql);

            DbFunction_DATEDIFF(generator, "MICROSECOND", exp.Arguments[0], exp.Arguments[1]);
        }
Exemplo n.º 4
0
        static void StringConcat(DbBinaryExpression exp, SqlGenerator generator)
        {
            MethodInfo method = exp.Method;

            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 == UtilConstants.MethodInfo_String_Concat_String_String || e.Method == UtilConstants.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 != UtilConstants.TypeOfString)
                {
                    // 需要 cast type
                    opBody = DbExpression.Convert(opBody, UtilConstants.TypeOfString);
                }

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

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

                DbExpression thenExp = DbConstantExpression.StringEmpty;
                DbCaseWhenExpression.WhenThenExpressionPair whenThenPair = new DbCaseWhenExpression.WhenThenExpressionPair(equalNullExp, thenExp);

                List <DbCaseWhenExpression.WhenThenExpressionPair> whenThenExps = new List <DbCaseWhenExpression.WhenThenExpressionPair>(1);
                whenThenExps.Add(whenThenPair);

                DbExpression elseExp = opBody;

                DbCaseWhenExpression caseWhenExpression = DbExpression.CaseWhen(whenThenExps, elseExp, UtilConstants.TypeOfString);

                operandExps.Add(caseWhenExpression);
            }

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

            generator._sqlBuilder.Append("CONCAT(");

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

                operandExps[i].Accept(generator);
            }

            generator._sqlBuilder.Append(")");

            generator._sqlBuilder.Append(" END");
        }
Exemplo n.º 5
0
 static void Aggregate_Average(DbAggregateExpression exp, SqlGenerator generator)
 {
     Aggregate_Average(generator, exp.Parameters.First(), exp.Method.ReturnType);
 }
Exemplo n.º 6
0
        static void Method_DiffMilliseconds(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethodDeclaringType(exp, typeof(DbFunctions), UtilConstants.TypeOfSql);

            throw UtilExceptions.NotSupportedMethod(exp.Method);
        }
Exemplo n.º 7
0
        static void Method_Contains(DbMethodCallExpression exp, SqlGenerator generator)
        {
            MethodInfo method = exp.Method;

            if (method.DeclaringType == UtilConstants.TypeOfString)
            {
                Method_String_Contains(exp, generator);
                return;
            }

            List <DbExpression> exps    = new List <DbExpression>();
            IEnumerable         values  = null;
            DbExpression        operand = null;

            Type declaringType = method.DeclaringType;

            if (typeof(IList).IsAssignableFrom(declaringType) || (declaringType.IsGenericType && typeof(ICollection <>).MakeGenericType(declaringType.GetGenericArguments()).IsAssignableFrom(declaringType)))
            {
                if (exp.Object.NodeType == DbExpressionType.SqlQuery)
                {
                    /* where Id in(select id from T) */

                    operand = exp.Arguments[0];
                    In(generator, (DbSqlQueryExpression)exp.Object, operand);
                    return;
                }

                DbMemberExpression memberExp = exp.Object as DbMemberExpression;

                if (memberExp == null || !memberExp.IsEvaluable())
                {
                    throw new NotSupportedException(exp.ToString());
                }

                values  = DbExpressionExtension.Evaluate(memberExp) as IEnumerable; //Enumerable
                operand = exp.Arguments[0];
                goto constructInState;
            }
            if (method.IsStatic && declaringType == typeof(Enumerable) && exp.Arguments.Count == 2)
            {
                DbExpression arg0 = exp.Arguments[0];
                if (arg0.NodeType == DbExpressionType.SqlQuery)
                {
                    /* where Id in(select id from T) */

                    operand = exp.Arguments[1];
                    In(generator, (DbSqlQueryExpression)arg0, operand);
                    return;
                }

                DbMemberExpression memberExp = arg0 as DbMemberExpression;

                if (memberExp == null || !memberExp.IsEvaluable())
                {
                    throw new NotSupportedException(exp.ToString());
                }

                values  = DbExpressionExtension.Evaluate(memberExp) as IEnumerable;
                operand = exp.Arguments[1];
                goto constructInState;
            }

            throw UtilExceptions.NotSupportedMethod(exp.Method);

constructInState:
            foreach (object value in values)
            {
                if (value == null)
                {
                    exps.Add(DbExpression.Constant(null, operand.Type));
                }
                else
                {
                    Type valueType = value.GetType();
                    if (valueType.IsEnum)
                    {
                        valueType = Enum.GetUnderlyingType(valueType);
                    }

                    if (Utils.IsToStringableNumericType(valueType))
                    {
                        exps.Add(DbExpression.Constant(value));
                    }
                    else
                    {
                        exps.Add(DbExpression.Parameter(value));
                    }
                }
            }
            In(generator, exps, operand);
        }
Exemplo n.º 8
0
        static void Method_DbFunctions_DiffMinutes(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMinutes);

            DbFunction_DATEDIFF(generator, "MINUTE", exp.Arguments[0], exp.Arguments[1]);
        }
Exemplo n.º 9
0
 static void Aggregate_Average(SqlGenerator generator, DbExpression exp, Type retType)
 {
     AppendAggregateFunction(generator, exp, retType, "AVG", true);
 }
Exemplo n.º 10
0
        static void Method_DbFunctions_DiffMilliseconds(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMilliseconds);

            throw UtilExceptions.NotSupportedMethod(exp.Method);
        }
Exemplo n.º 11
0
 static void Aggregate_Sum(SqlGenerator generator, DbExpression exp, Type retType)
 {
     AppendAggregateFunction(generator, exp, retType, "SUM", true);
 }
Exemplo n.º 12
0
 static void Aggregate_Min(SqlGenerator generator, DbExpression exp, Type retType)
 {
     AppendAggregateFunction(generator, exp, retType, "MIN", false);
 }
Exemplo n.º 13
0
 static void Aggregate_LongCount(SqlGenerator generator)
 {
     generator._sqlBuilder.Append("COUNT(1)");
 }
Exemplo n.º 14
0
        static void Method_DateTime_AddSeconds(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);

            DbFunction_DATEADD(generator, "SECOND", exp);
        }
Exemplo n.º 15
0
 static void Method_Count(DbMethodCallExpression exp, SqlGenerator generator)
 {
     EnsureMethodDeclaringType(exp, typeof(AggregateFunctions), UtilConstants.TypeOfSql);
     Aggregate_Count(generator);
 }
Exemplo n.º 16
0
        static void Method_Guid_NewGuid(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethod(exp, UtilConstants.MethodInfo_Guid_NewGuid);

            generator._sqlBuilder.Append("UUID()");
        }
Exemplo n.º 17
0
 static void Method_Min(DbMethodCallExpression exp, SqlGenerator generator)
 {
     EnsureMethodDeclaringType(exp, typeof(AggregateFunctions), UtilConstants.TypeOfSql);
     Aggregate_Min(generator, exp.Arguments.First(), exp.Method.ReturnType);
 }
Exemplo n.º 18
0
        static void Method_DbFunctions_DiffMicroseconds(DbMethodCallExpression exp, SqlGenerator generator)
        {
            EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMicroseconds);

            DbFunction_DATEDIFF(generator, "MICROSECOND", exp.Arguments[0], exp.Arguments[1]);
        }
Exemplo n.º 19
0
 static void Aggregate_LongCount(DbAggregateExpression exp, SqlGenerator generator)
 {
     Aggregate_LongCount(generator);
 }