예제 #1
0
        static bool TryGetCastTargetDbTypeString(Type sourceType, Type targetType, out string dbTypeString, bool throwNotSupportedException = true)
        {
            dbTypeString = null;

            sourceType = ReflectionExtension.GetUnderlyingType(sourceType);
            targetType = ReflectionExtension.GetUnderlyingType(targetType);

            if (sourceType == targetType)
            {
                return(false);
            }

            if (CastTypeMap.TryGetValue(targetType, out dbTypeString))
            {
                return(true);
            }

            if (throwNotSupportedException)
            {
                throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
        public static void Aggregate_Average(SqlGenerator generator, DbExpression exp, Type retType)
        {
            string targetDbType = null;

            Type underlyingType = ReflectionExtension.GetUnderlyingType(retType);

            if (underlyingType != exp.Type.GetUnderlyingType())
            {
                CastTypeMap.TryGetValue(underlyingType, out targetDbType);
            }

            generator._sqlBuilder.Append("AVG", "(");
            if (string.IsNullOrEmpty(targetDbType))
            {
                exp.Accept(generator);
            }
            else
            {
                generator._sqlBuilder.Append("CAST(");
                exp.Accept(generator);
                generator._sqlBuilder.Append(" AS ", targetDbType, ")");
            }

            generator._sqlBuilder.Append(")");
        }
예제 #3
0
        static bool TryGetCastTargetDbTypeString(Type sourceType, Type targetType, out string dbTypeString, bool throwNotSupportedException = true)
        {
            dbTypeString = null;

            sourceType = ReflectionExtension.GetUnderlyingType(sourceType);
            targetType = ReflectionExtension.GetUnderlyingType(targetType);

            if (sourceType == targetType)
                return false;

            if (targetType == UtilConstants.TypeOfDecimal)
            {
                //Casting to Decimal is not supported when missing the precision and scale information.I have no idea to deal with this case now.
                if (sourceType != UtilConstants.TypeOfInt16 && sourceType != UtilConstants.TypeOfInt32 && sourceType != UtilConstants.TypeOfInt64 && sourceType != UtilConstants.TypeOfByte)
                {
                    if (throwNotSupportedException)
                        throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
                    else
                        return false;
                }
            }

            if (CastTypeMap.TryGetValue(targetType, out dbTypeString))
            {
                return true;
            }

            if (throwNotSupportedException)
                throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
            else
                return false;
        }
예제 #4
0
        static void AppendAggregateFunction(SqlGenerator generator, DbExpression exp, Type retType, string functionName, bool withCast)
        {
            string dbTypeString = null;

            if (withCast == true)
            {
                Type underlyingType = ReflectionExtension.GetUnderlyingType(retType);
                if (CastTypeMap.TryGetValue(underlyingType, out dbTypeString))
                {
                    generator._sqlBuilder.Append("CAST(");
                }
            }

            generator._sqlBuilder.Append(functionName, "(");
            exp.Accept(generator);
            generator._sqlBuilder.Append(")");

            if (dbTypeString != null)
            {
                generator._sqlBuilder.Append(" AS ", dbTypeString, ")");
            }
        }
예제 #5
0
        static void AppendAggregateFunction(SqlGenerator generator, DbExpression exp, Type retType, string functionName, bool withCast)
        {
            string dbTypeString = null;

            if (withCast == true)
            {
                Type underlyingType = ReflectionExtension.GetUnderlyingType(retType);
                if (underlyingType != PublicConstants.TypeOfDecimal /* We don't know the precision and scale,so,we can not cast exp to decimal,otherwise maybe cause problems. */ && CastTypeMap.TryGetValue(underlyingType, out dbTypeString))
                {
                    generator._sqlBuilder.Append("CAST(");
                }
            }

            generator._sqlBuilder.Append(functionName, "(");
            exp.Accept(generator);
            generator._sqlBuilder.Append(")");

            if (dbTypeString != null)
            {
                generator._sqlBuilder.Append(" AS ", dbTypeString, ")");
            }
        }