Beispiel #1
0
        public override int Compare(ISqlObject x, ISqlObject y)
        {
            if (x == null)
            {
                throw new ArgumentNullException("x");
            }

            if (!(x is ISqlString) ||
                !(y is ISqlString))
            {
                throw new ArgumentException("Cannot compare objects that are not strings.");
            }

            if (x.IsNull && y.IsNull)
            {
                return(0);
            }
            if (x.IsNull && !y.IsNull)
            {
                return(1);
            }
            if (!x.IsNull && y.IsNull)
            {
                return(-1);
            }

            // If lexicographical ordering,
            if (Locale == null)
            {
                return(LexicographicalOrder((ISqlString)x, (ISqlString)y));
            }

            return(Collator.Compare(x.ToString(), y.ToString()));
        }
Beispiel #2
0
        public override int Compare(ISqlObject x, ISqlObject y)
        {
            if (x == null)
                throw new ArgumentNullException("x");

            if (!(x is ISqlString) ||
                !(y is ISqlString))
                throw new ArgumentException("Cannot compare objects that are not strings.");

            if (x.IsNull && y.IsNull)
                return 0;
            if (x.IsNull && !y.IsNull)
                return 1;
            if (!x.IsNull && y.IsNull)
                return -1;

            // If lexicographical ordering,
            if (Locale == null)
                return LexicographicalOrder((ISqlString)x, (ISqlString)y);

            return Collator.Compare(x.ToString(), y.ToString());
        }
Beispiel #3
0
        /// <inheritdoc/>
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            if (value is SqlLongString) {
                var clob = (SqlLongString) value;
                switch (destType.TypeCode) {
                    case SqlTypeCode.LongVarChar:
                    case SqlTypeCode.Clob: {
                        if (clob.IsNull)
                            return SqlLongString.Null;

                        var destStringType = (StringType) destType;
                        if (MaxSize > destStringType.MaxSize)
                            throw new InvalidCastException(String.Format("The destination type '{0}' is not large enough.", destStringType));
                        if (clob.Length > destStringType.MaxSize)
                            throw new InvalidCastException(String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length, destStringType));

                        return clob;
                    }
                    case SqlTypeCode.LongVarBinary:
                    case SqlTypeCode.Blob: {
                        if (clob.IsNull)
                            return SqlLongBinary.Null;

                        var destBinaryType = (BinaryType) destType;
                        if (clob.Length > destBinaryType.MaxSize)
                            throw new InvalidCastException(
                                String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length,
                                    destBinaryType));

                            return new SqlLongBinary(clob.LargeObject);
                    }
                    default:
                        throw new InvalidCastException(String.Format("Cannot cast a CLOB of type '{0}' to '{1}'.", this, destType));
                }
            }

            string str = value.ToString();
            var sqlType = destType.TypeCode;
            ISqlObject castedValue;

            if (value.IsNull)
                return SqlNull.Value;

            switch (sqlType) {
                case (SqlTypeCode.Bit):
                case (SqlTypeCode.Boolean):
                    castedValue = ToBoolean(str);
                    break;
                case (SqlTypeCode.TinyInt):
                case (SqlTypeCode.SmallInt):
                case (SqlTypeCode.Integer): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToInt32());
                    }

                    break;
                }
                case (SqlTypeCode.BigInt): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToInt64());
                    }

                    break;
                }
                case (SqlTypeCode.Float):
                case (SqlTypeCode.Double): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToDouble());
                    }

                    break;
                }
                case (SqlTypeCode.Real):
                case (SqlTypeCode.Numeric):
                case (SqlTypeCode.Decimal):
                    castedValue = ToNumber(str);
                    break;
                case (SqlTypeCode.Char):
                    castedValue = new SqlString(str.PadRight(((StringType)destType).MaxSize));
                    break;
                case (SqlTypeCode.VarChar):
                case (SqlTypeCode.String):
                    castedValue = new SqlString(str);
                    break;
                case (SqlTypeCode.Date):
                    castedValue = ToDate(str);
                    break;
                case (SqlTypeCode.Time):
                    castedValue = ToTime(str);
                    break;
                case (SqlTypeCode.TimeStamp):
                    castedValue = ToTimeStamp(str);
                    break;
                case (SqlTypeCode.DateTime):
                    castedValue = ToDateTime(str);
                    break;
                case (SqlTypeCode.Binary):
                case (SqlTypeCode.VarBinary):
                case (SqlTypeCode.LongVarBinary):
                    castedValue = new SqlBinary(Encoding.Unicode.GetBytes(str));
                    break;
                case (SqlTypeCode.Null):
                    castedValue = SqlNull.Value;
                    break;
                default:
                    throw new InvalidCastException();
            }

            return castedValue;
        }
Beispiel #4
0
        /// <inheritdoc/>
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            if (value is SqlLongString)
            {
                var clob = (SqlLongString)value;
                switch (destType.TypeCode)
                {
                case SqlTypeCode.LongVarChar:
                case SqlTypeCode.Clob: {
                    if (clob.IsNull)
                    {
                        return(SqlLongString.Null);
                    }

                    var destStringType = (StringType)destType;
                    if (MaxSize > destStringType.MaxSize)
                    {
                        throw new InvalidCastException(String.Format("The destination type '{0}' is not large enough.", destStringType));
                    }
                    if (clob.Length > destStringType.MaxSize)
                    {
                        throw new InvalidCastException(String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length, destStringType));
                    }

                    return(clob);
                }

                case SqlTypeCode.LongVarBinary:
                case SqlTypeCode.Blob: {
                    if (clob.IsNull)
                    {
                        return(SqlLongBinary.Null);
                    }

                    var destBinaryType = (BinaryType)destType;
                    if (clob.Length > destBinaryType.MaxSize)
                    {
                        throw new InvalidCastException(
                                  String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length,
                                                destBinaryType));
                    }

                    return(new SqlLongBinary(clob.LargeObject));
                }

                default:
                    throw new InvalidCastException(String.Format("Cannot cast a CLOB of type '{0}' to '{1}'.", this, destType));
                }
            }

            string     str     = value.ToString();
            var        sqlType = destType.TypeCode;
            ISqlObject castedValue;

            if (value.IsNull)
            {
                return(SqlNull.Value);
            }

            switch (sqlType)
            {
            case (SqlTypeCode.Bit):
            case (SqlTypeCode.Boolean):
                castedValue = ToBoolean(str);
                break;

            case (SqlTypeCode.TinyInt):
            case (SqlTypeCode.SmallInt):
            case (SqlTypeCode.Integer): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt32());
                }

                break;
            }

            case (SqlTypeCode.BigInt): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt64());
                }

                break;
            }

            case (SqlTypeCode.Float):
            case (SqlTypeCode.Double): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToDouble());
                }

                break;
            }

            case (SqlTypeCode.Real):
            case (SqlTypeCode.Numeric):
            case (SqlTypeCode.Decimal):
                castedValue = ToNumber(str);
                break;

            case (SqlTypeCode.Char):
                castedValue = new SqlString(str.PadRight(((StringType)destType).MaxSize));
                break;

            case (SqlTypeCode.VarChar):
            case (SqlTypeCode.String):
                castedValue = new SqlString(str);
                break;

            case (SqlTypeCode.Date):
                castedValue = ToDate(str);
                break;

            case (SqlTypeCode.Time):
                castedValue = ToTime(str);
                break;

            case (SqlTypeCode.TimeStamp):
                castedValue = ToTimeStamp(str);
                break;

            case (SqlTypeCode.DateTime):
                castedValue = ToDateTime(str);
                break;

            case (SqlTypeCode.Binary):
            case (SqlTypeCode.VarBinary):
            case (SqlTypeCode.LongVarBinary):
                castedValue = new SqlBinary(Encoding.Unicode.GetBytes(str));
                break;

            case (SqlTypeCode.Null):
                castedValue = SqlNull.Value;
                break;

            default:
                throw new InvalidCastException();
            }

            return(castedValue);
        }