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

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

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

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

            if (throwNotSupportedException)
            {
                throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
            }
            else
            {
                return(false);
            }
        }
示例#2
0
        static bool TryGetCastTargetDbTypeString(Type sourceType, Type targetType, out string dbTypeString, bool throwNotSupportedException = true)
        {
            dbTypeString = null;

            sourceType = Utils.GetUnderlyingType(sourceType);
            targetType = Utils.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 (CSharpType_DbType_Mappings.TryGetValue(targetType, out dbTypeString))
            {
                return(true);
            }

            if (throwNotSupportedException)
            {
                throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
            }
            else
            {
                return(false);
            }
        }
示例#3
0
        static void AppendAggregateFunctionWithCast(SqlGenerator generator, DbExpression exp, Type retType, string functionName)
        {
            Type unType = Utils.GetUnderlyingType(retType);

            string dbTypeString = null;

            if (unType != UtilConstants.TypeOfDecimal /* We don't know the precision and scale,so,we can not cast exp to decimal,otherwise cause problems. */ && CSharpType_DbType_Mappings.TryGetValue(unType, out dbTypeString))
            {
                generator._sqlBuilder.Append("CAST(");
            }

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

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