Ejemplo n.º 1
0
 internal override SqlExpression VisitUnaryOperator(SqlUnary uo)
 {
     uo.Operand = VisitExpression(uo.Operand);
     if (uo.NodeType == SqlNodeType.Convert)
     {
         IProviderType sqlType         = uo.Operand.SqlType;
         IProviderType newSqlType      = uo.SqlType;
         Type          nonNullableType = TypeSystem.GetNonNullableType(uo.Operand.ClrType);
         Type          clrType         = TypeSystem.GetNonNullableType(uo.ClrType);
         if (clrType == typeof(char))
         {
             if (nonNullableType == typeof(bool))
             {
                 throw Error.ConvertToCharFromBoolNotSupported();
             }
             if (sqlType.IsNumeric)
             {
                 return(sql.FunctionCall(uo.ClrType, "NCHAR", new[] { uo.Operand }, uo.SourceExpression));
             }
             if (!StringConversionIsSafe(sqlType, newSqlType))
             {
                 throw Error.UnsafeStringConversion(sqlType.ToQueryString(), newSqlType.ToQueryString());
             }
             if (StringConversionIsNeeded(sqlType, newSqlType))
             {
                 uo.SetSqlType(sql.TypeProvider.From(uo.ClrType, sqlType.HasSizeOrIsLarge ? sqlType.Size : null));
             }
             return(uo);
         }
         if (((nonNullableType == typeof(char)) && (sqlType.IsChar || sqlType.IsString)) && newSqlType.IsNumeric)
         {
             //return sql.FunctionCall(clrType, sql.TypeProvider.From(typeof(int)), "UNICODE", new[] { uo.Operand }, uo.SourceExpression);
             return(sql.UNICODE(clrType, uo));
         }
         if (clrType != typeof(string))
         {
             return(uo);
         }
         if (nonNullableType == typeof(double))
         {
             return(ConvertDoubleToString(uo.Operand, uo.ClrType));
         }
         if (nonNullableType == typeof(bool))
         {
             return(ConvertBitToString(uo.Operand, uo.ClrType));
         }
         if (!StringConversionIsSafe(sqlType, newSqlType))
         {
             throw Error.UnsafeStringConversion(sqlType.ToQueryString(), newSqlType.ToQueryString());
         }
         if (StringConversionIsNeeded(sqlType, newSqlType))
         {
             uo.SetSqlType(sql.TypeProvider.From(uo.ClrType, sqlType.HasSizeOrIsLarge ? sqlType.Size : null));
         }
     }
     return(uo);
 }
Ejemplo n.º 2
0
            internal override SqlExpression VisitTreat(SqlUnary t)
            {
                t.Operand = this.VisitExpression(t.Operand);
                Type clrType = t.ClrType;
                Type type    = this.model.GetMetaType(t.Operand.ClrType).InheritanceRoot.Type;

                clrType = TypeSystem.GetNonNullableType(clrType);
                type    = TypeSystem.GetNonNullableType(type);
                if (clrType == type)
                {
                    return(t.Operand);
                }
                if (clrType.IsAssignableFrom(type))
                {
                    t.Operand.SetClrType(clrType);
                    return(t.Operand);
                }
                if ((!clrType.IsAssignableFrom(type) && !type.IsAssignableFrom(clrType)) &&
                    (!clrType.IsInterface && !type.IsInterface))
                {
                    return(this.sql.TypedLiteralNull(clrType, t.SourceExpression));
                }
                return(t);
            }
Ejemplo n.º 3
0
            private static bool CanDbConvert(Type from, Type to)
            {
                from = TypeSystem.GetNonNullableType(from);
                to   = TypeSystem.GetNonNullableType(to);
                if (from == to)
                {
                    return(true);
                }
                if (to.IsAssignableFrom(from))
                {
                    return(true);
                }
                TypeCode typeCode = Type.GetTypeCode(to);
                TypeCode code2    = Type.GetTypeCode(from);

                switch (typeCode)
                {
                case TypeCode.Int16:
                    return((code2 == TypeCode.Byte) || (code2 == TypeCode.SByte));

                case TypeCode.UInt16:
                    return((code2 == TypeCode.Byte) || (code2 == TypeCode.SByte));

                case TypeCode.Int32:
                    switch (code2)
                    {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Int16:
                        return(true);
                    }
                    return(code2 == TypeCode.UInt16);

                case TypeCode.UInt32:
                    switch (code2)
                    {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Int16:
                        return(true);
                    }
                    return(code2 == TypeCode.UInt16);

                case TypeCode.Int64:
                    switch (code2)
                    {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                    case TypeCode.Int32:
                        return(true);
                    }
                    return(code2 == TypeCode.UInt32);

                case TypeCode.UInt64:
                    switch (code2)
                    {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                    case TypeCode.Int32:
                        return(true);
                    }
                    return(code2 == TypeCode.UInt32);

                case TypeCode.Double:
                    return(code2 == TypeCode.Single);

                case TypeCode.Decimal:
                    if (code2 != TypeCode.Single)
                    {
                        return(code2 == TypeCode.Double);
                    }
                    return(true);
                }
                return(false);
            }