Esempio n. 1
0
        public void SetValue(byte[] buffer)
        {
            if (buffer == null || NullFlag == -1)
            {
                DbValue.SetValue(DBNull.Value);
            }
            else
            {
                switch (SqlType)
                {
                case IscCodes.SQL_TEXT:
                case IscCodes.SQL_VARYING:
                    if (DbDataType == DbDataType.Guid)
                    {
                        DbValue.SetValue(TypeDecoder.DecodeGuid(buffer));
                    }
                    else
                    {
                        if (Charset.IsOctetsCharset)
                        {
                            DbValue.SetValue(buffer);
                        }
                        else
                        {
                            var s = Charset.GetString(buffer, 0, buffer.Length);

                            if ((Length % Charset.BytesPerCharacter) == 0 &&
                                s.Length > CharCount)
                            {
                                s = s.Substring(0, CharCount);
                            }

                            DbValue.SetValue(s);
                        }
                    }
                    break;

                case IscCodes.SQL_SHORT:
                    if (_numericScale < 0)
                    {
                        DbValue.SetValue(TypeDecoder.DecodeDecimal(BitConverter.ToInt16(buffer, 0), _numericScale, _dataType));
                    }
                    else
                    {
                        DbValue.SetValue(BitConverter.ToInt16(buffer, 0));
                    }
                    break;

                case IscCodes.SQL_LONG:
                    if (_numericScale < 0)
                    {
                        DbValue.SetValue(TypeDecoder.DecodeDecimal(BitConverter.ToInt32(buffer, 0), _numericScale, _dataType));
                    }
                    else
                    {
                        DbValue.SetValue(BitConverter.ToInt32(buffer, 0));
                    }
                    break;

                case IscCodes.SQL_FLOAT:
                    DbValue.SetValue(BitConverter.ToSingle(buffer, 0));
                    break;

                case IscCodes.SQL_DOUBLE:
                case IscCodes.SQL_D_FLOAT:
                    DbValue.SetValue(BitConverter.ToDouble(buffer, 0));
                    break;

                case IscCodes.SQL_QUAD:
                case IscCodes.SQL_INT64:
                case IscCodes.SQL_BLOB:
                case IscCodes.SQL_ARRAY:
                    if (_numericScale < 0)
                    {
                        DbValue.SetValue(TypeDecoder.DecodeDecimal(BitConverter.ToInt64(buffer, 0), _numericScale, _dataType));
                    }
                    else
                    {
                        DbValue.SetValue(BitConverter.ToInt64(buffer, 0));
                    }
                    break;

                case IscCodes.SQL_TIMESTAMP:
                {
                    var date = TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0));
                    var time = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 4));
                    DbValue.SetValue(date.Add(time));
                    break;
                }

                case IscCodes.SQL_TYPE_TIME:
                    DbValue.SetValue(TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 0)));
                    break;

                case IscCodes.SQL_TYPE_DATE:
                    DbValue.SetValue(TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0)));
                    break;

                case IscCodes.SQL_BOOLEAN:
                    DbValue.SetValue(TypeDecoder.DecodeBoolean(buffer));
                    break;

                case IscCodes.SQL_TIMESTAMP_TZ:
                {
                    var date = TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0));
                    var time = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 4));
                    var tzId = BitConverter.ToUInt16(buffer, 8);
                    var dt   = DateTime.SpecifyKind(date.Add(time), DateTimeKind.Utc);
                    DbValue.SetValue(TypeHelper.CreateZonedDateTime(dt, tzId, null));
                    break;
                }

                case IscCodes.SQL_TIMESTAMP_TZ_EX:
                {
                    var date   = TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0));
                    var time   = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 4));
                    var tzId   = BitConverter.ToUInt16(buffer, 8);
                    var offset = BitConverter.ToInt16(buffer, 10);
                    var dt     = DateTime.SpecifyKind(date.Add(time), DateTimeKind.Utc);
                    DbValue.SetValue(TypeHelper.CreateZonedDateTime(dt, tzId, offset));
                    break;
                }

                case IscCodes.SQL_TIME_TZ:
                {
                    var time = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 0));
                    var tzId = BitConverter.ToUInt16(buffer, 4);
                    DbValue.SetValue(TypeHelper.CreateZonedTime(time, tzId, null));
                    break;
                }

                case IscCodes.SQL_TIME_TZ_EX:
                {
                    var time   = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 0));
                    var tzId   = BitConverter.ToUInt16(buffer, 4);
                    var offset = BitConverter.ToInt16(buffer, 6);
                    DbValue.SetValue(TypeHelper.CreateZonedTime(time, tzId, offset));
                    break;
                }

                case IscCodes.SQL_DEC16:
                    DbValue.SetValue(DecimalCodec.DecFloat16.ParseBytes(buffer));
                    break;

                case IscCodes.SQL_DEC34:
                    DbValue.SetValue(DecimalCodec.DecFloat34.ParseBytes(buffer));
                    break;

                case IscCodes.SQL_INT128:
                    if (_numericScale < 0)
                    {
                        DbValue.SetValue(TypeDecoder.DecodeDecimal(Int128Helper.GetInt128(buffer), _numericScale, _dataType));
                    }
                    else
                    {
                        DbValue.SetValue(Int128Helper.GetInt128(buffer));
                    }
                    break;

                default:
                    throw TypeHelper.InvalidDataType(SqlType);
                }
            }
        }
Esempio n. 2
0
        public void SetValue(byte[] buffer)
        {
            if (buffer == null || NullFlag == -1)
            {
                Value = DBNull.Value;
            }
            else
            {
                switch (SqlType)
                {
                case IscCodes.SQL_TEXT:
                case IscCodes.SQL_VARYING:
                    if (DbDataType == DbDataType.Guid)
                    {
                        Value = TypeDecoder.DecodeGuid(buffer);
                    }
                    else
                    {
                        if (Charset.IsOctetsCharset)
                        {
                            Value = buffer;
                        }
                        else
                        {
                            var s = Charset.GetString(buffer, 0, buffer.Length);

                            if ((Length % Charset.BytesPerCharacter) == 0 &&
                                s.Length > CharCount)
                            {
                                s = s.Substring(0, CharCount);
                            }

                            Value = s;
                        }
                    }
                    break;

                case IscCodes.SQL_SHORT:
                    if (_numericScale < 0)
                    {
                        Value = TypeDecoder.DecodeDecimal(
                            BitConverter.ToInt16(buffer, 0),
                            _numericScale,
                            _dataType);
                    }
                    else
                    {
                        Value = BitConverter.ToInt16(buffer, 0);
                    }
                    break;

                case IscCodes.SQL_LONG:
                    if (NumericScale < 0)
                    {
                        Value = TypeDecoder.DecodeDecimal(
                            BitConverter.ToInt32(buffer, 0),
                            _numericScale,
                            _dataType);
                    }
                    else
                    {
                        Value = BitConverter.ToInt32(buffer, 0);
                    }
                    break;

                case IscCodes.SQL_FLOAT:
                    Value = BitConverter.ToSingle(buffer, 0);
                    break;

                case IscCodes.SQL_DOUBLE:
                case IscCodes.SQL_D_FLOAT:
                    Value = BitConverter.ToDouble(buffer, 0);
                    break;

                case IscCodes.SQL_QUAD:
                case IscCodes.SQL_INT64:
                case IscCodes.SQL_BLOB:
                case IscCodes.SQL_ARRAY:
                    if (NumericScale < 0)
                    {
                        Value = TypeDecoder.DecodeDecimal(
                            BitConverter.ToInt64(buffer, 0),
                            _numericScale,
                            _dataType);
                    }
                    else
                    {
                        Value = BitConverter.ToInt64(buffer, 0);
                    }
                    break;

                case IscCodes.SQL_TIMESTAMP:
                    var date = TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0));
                    var time = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 4));

                    Value = date.Add(time);
                    break;

                case IscCodes.SQL_TYPE_TIME:
                    Value = TypeDecoder.DecodeTime(BitConverter.ToInt32(buffer, 0));
                    break;

                case IscCodes.SQL_TYPE_DATE:
                    Value = TypeDecoder.DecodeDate(BitConverter.ToInt32(buffer, 0));
                    break;

                case IscCodes.SQL_BOOLEAN:
                    Value = TypeDecoder.DecodeBoolean(buffer);
                    break;

                default:
                    throw TypeHelper.InvalidDataType(SqlType);
                }
            }
        }
Esempio n. 3
0
        public byte[] GetBytes()
        {
            if (IsDBNull())
            {
                int length = _field.Length;

                if (Field.SqlType == IscCodes.SQL_VARYING)
                {
                    // Add two bytes more for store	value length
                    length += 2;
                }

                return(new byte[length]);
            }


            switch (Field.DbDataType)
            {
            case DbDataType.Char:
            {
                var    buffer = new byte[Field.Length];
                byte[] bytes;

                if (Field.Charset.IsOctetsCharset)
                {
                    bytes = GetBinary();
                }
                else
                {
                    var svalue = GetString();

                    if ((Field.Length % Field.Charset.BytesPerCharacter) == 0 &&
                        svalue.Length > Field.CharCount)
                    {
                        throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation });
                    }

                    bytes = Field.Charset.GetBytes(svalue);
                }

                for (var i = 0; i < buffer.Length; i++)
                {
                    buffer[i] = (byte)' ';
                }
                Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);
                return(buffer);
            }

            case DbDataType.VarChar:
            {
                var    buffer = new byte[Field.Length + 2];
                byte[] bytes;

                if (Field.Charset.IsOctetsCharset)
                {
                    bytes = GetBinary();
                }
                else
                {
                    var svalue = GetString();

                    if ((Field.Length % Field.Charset.BytesPerCharacter) == 0 &&
                        svalue.Length > Field.CharCount)
                    {
                        throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation });
                    }

                    bytes = Field.Charset.GetBytes(svalue);
                }

                Buffer.BlockCopy(BitConverter.GetBytes((short)bytes.Length), 0, buffer, 0, 2);
                Buffer.BlockCopy(bytes, 0, buffer, 2, bytes.Length);
                return(buffer);
            }

            case DbDataType.Numeric:
            case DbDataType.Decimal:
                return(GetNumericBytes());

            case DbDataType.SmallInt:
                return(BitConverter.GetBytes(GetInt16()));

            case DbDataType.Integer:
                return(BitConverter.GetBytes(GetInt32()));

            case DbDataType.Array:
            case DbDataType.Binary:
            case DbDataType.Text:
            case DbDataType.BigInt:
                return(BitConverter.GetBytes(GetInt64()));

            case DbDataType.Float:
                return(BitConverter.GetBytes(GetFloat()));

            case DbDataType.Double:
                return(BitConverter.GetBytes(GetDouble()));

            case DbDataType.Date:
                return(BitConverter.GetBytes(TypeEncoder.EncodeDate(GetDateTime())));

            case DbDataType.Time:
                return(BitConverter.GetBytes(GetTime()));

            case DbDataType.TimeStamp:
                var dt   = GetDateTime();
                var date = BitConverter.GetBytes(TypeEncoder.EncodeDate(dt));
                var time = BitConverter.GetBytes(TypeEncoder.EncodeTime(TypeHelper.DateTimeToTimeSpan(dt)));

                var result = new byte[8];

                Buffer.BlockCopy(date, 0, result, 0, date.Length);
                Buffer.BlockCopy(time, 0, result, 4, time.Length);

                return(result);

            case DbDataType.Guid:
                return(GetGuid().ToByteArray());

            case DbDataType.Boolean:
                return(BitConverter.GetBytes(GetBoolean()));

            default:
                throw TypeHelper.InvalidDataType((int)Field.DbDataType);
            }
        }