Example #1
0
        public async ValueTask <decimal> ReadDecimalAsync(int type, int scale, CancellationToken cancellationToken = default)
        {
            switch (type & ~1)
            {
            case IscCodes.SQL_SHORT:
                return(TypeDecoder.DecodeDecimal(await ReadInt16Async(cancellationToken).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_LONG:
                return(TypeDecoder.DecodeDecimal(await ReadInt32Async(cancellationToken).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_QUAD:
            case IscCodes.SQL_INT64:
                return(TypeDecoder.DecodeDecimal(await ReadInt64Async(cancellationToken).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_DOUBLE:
            case IscCodes.SQL_D_FLOAT:
                return(TypeDecoder.DecodeDecimal(await ReadDoubleAsync(cancellationToken).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_INT128:
                return(TypeDecoder.DecodeDecimal(await ReadInt128Async(cancellationToken).ConfigureAwait(false), scale, type));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), $"{nameof(type)}={type}");
            }
        }
Example #2
0
        public decimal ReadDecimal(int type, int scale)
        {
            switch (type & ~1)
            {
            case IscCodes.SQL_SHORT:
                return(TypeDecoder.DecodeDecimal(ReadInt16(), scale, type));

            case IscCodes.SQL_LONG:
                return(TypeDecoder.DecodeDecimal(ReadInt32(), scale, type));

            case IscCodes.SQL_QUAD:
            case IscCodes.SQL_INT64:
                return(TypeDecoder.DecodeDecimal(ReadInt64(), scale, type));

            case IscCodes.SQL_DOUBLE:
            case IscCodes.SQL_D_FLOAT:
                return(TypeDecoder.DecodeDecimal(ReadDouble(), scale, type));

            case IscCodes.SQL_INT128:
                return(TypeDecoder.DecodeDecimal(ReadInt128(), scale, type));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), $"{nameof(type)}={type}");
            }
        }
        public decimal ReadDecimal(int type, int scale)
        {
            var value = 0m;

            switch (type & ~1)
            {
            case IscCodes.SQL_SHORT:
                value = TypeDecoder.DecodeDecimal(ReadInt16(), scale, type);
                break;

            case IscCodes.SQL_LONG:
                value = TypeDecoder.DecodeDecimal(ReadInt32(), scale, type);
                break;

            case IscCodes.SQL_QUAD:
            case IscCodes.SQL_INT64:
                value = TypeDecoder.DecodeDecimal(ReadInt64(), scale, type);
                break;

            case IscCodes.SQL_DOUBLE:
            case IscCodes.SQL_D_FLOAT:
                value = Convert.ToDecimal(ReadDouble());
                break;
            }
            return(value);
        }
        public async Task <decimal> ReadDecimal(int type, int scale, AsyncWrappingCommonArgs async)
        {
            switch (type & ~1)
            {
            case IscCodes.SQL_SHORT:
                return(TypeDecoder.DecodeDecimal(await ReadInt16(async).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_LONG:
                return(TypeDecoder.DecodeDecimal(await ReadInt32(async).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_QUAD:
            case IscCodes.SQL_INT64:
                return(TypeDecoder.DecodeDecimal(await ReadInt64(async).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_DOUBLE:
            case IscCodes.SQL_D_FLOAT:
                return(TypeDecoder.DecodeDecimal(await ReadDouble(async).ConfigureAwait(false), scale, type));

            case IscCodes.SQL_INT128:
                return(TypeDecoder.DecodeDecimal(await ReadInt128(async).ConfigureAwait(false), scale, type));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), $"{nameof(type)}={type}");
            }
        }
Example #5
0
        protected override System.Array DecodeSlice(byte[] slice)
        {
            Array      sliceData     = null;
            int        slicePosition = 0;
            int        type          = 0;
            DbDataType dbType        = DbDataType.Array;
            Type       systemType    = this.GetSystemType();
            Charset    charset       = this.db.Charset;

            int[] lengths     = new int[this.Descriptor.Dimensions];
            int[] lowerBounds = new int[this.Descriptor.Dimensions];

            // Get upper and lower bounds of each dimension
            for (int i = 0; i < this.Descriptor.Dimensions; i++)
            {
                lowerBounds[i] = this.Descriptor.Bounds[i].LowerBound;
                lengths[i]     = this.Descriptor.Bounds[i].UpperBound;

                if (lowerBounds[i] == 0)
                {
                    lengths[i]++;
                }
            }

            // Create slice	arrays
            sliceData = Array.CreateInstance(systemType, lengths, lowerBounds);

            Array tempData = Array.CreateInstance(systemType, sliceData.Length);

            // Infer data types
            type   = TypeHelper.GetFbType(this.Descriptor.DataType);
            dbType = TypeHelper.GetDbDataType(this.Descriptor.DataType, 0, this.Descriptor.Scale);

            int itemLength = this.Descriptor.Length;

            for (int i = 0; i < tempData.Length; i++)
            {
                if (slicePosition >= slice.Length)
                {
                    break;
                }

                switch (dbType)
                {
                case DbDataType.Char:
                    tempData.SetValue(charset.GetString(slice, slicePosition, itemLength), i);
                    break;

                case DbDataType.VarChar:
                {
                    int index = slicePosition;
                    int count = 0;
                    while (slice[index++] != 0)
                    {
                        count++;
                    }
                    tempData.SetValue(charset.GetString(slice, slicePosition, count), i);

                    slicePosition += 2;
                }
                break;

                case DbDataType.SmallInt:
                    tempData.SetValue(BitConverter.ToInt16(slice, slicePosition), i);
                    break;

                case DbDataType.Integer:
                    tempData.SetValue(BitConverter.ToInt32(slice, slicePosition), i);
                    break;

                case DbDataType.BigInt:
                    tempData.SetValue(BitConverter.ToInt64(slice, slicePosition), i);
                    break;

                case DbDataType.Decimal:
                case DbDataType.Numeric:
                {
                    object evalue = null;

                    switch (type)
                    {
                    case IscCodes.SQL_SHORT:
                        evalue = BitConverter.ToInt16(slice, slicePosition);
                        break;

                    case IscCodes.SQL_LONG:
                        evalue = BitConverter.ToInt32(slice, slicePosition);
                        break;

                    case IscCodes.SQL_QUAD:
                    case IscCodes.SQL_INT64:
                        evalue = BitConverter.ToInt64(slice, slicePosition);
                        break;
                    }

                    decimal dvalue = TypeDecoder.DecodeDecimal(evalue, this.Descriptor.Scale, type);

                    tempData.SetValue(dvalue, i);
                }
                break;

                case DbDataType.Double:
                    tempData.SetValue(BitConverter.ToDouble(slice, slicePosition), i);
                    break;

                case DbDataType.Float:
                    tempData.SetValue(BitConverter.ToSingle(slice, slicePosition), i);
                    break;

                case DbDataType.Date:
                {
                    int idate = BitConverter.ToInt32(slice, slicePosition);

                    DateTime date = TypeDecoder.DecodeDate(idate);

                    tempData.SetValue(date, i);
                }
                break;

                case DbDataType.Time:
                {
                    int itime = BitConverter.ToInt32(slice, slicePosition);

                    TimeSpan time = TypeDecoder.DecodeTime(itime);

                    tempData.SetValue(time, i);
                }
                break;

                case DbDataType.TimeStamp:
                {
                    int idate = BitConverter.ToInt32(slice, slicePosition);
                    int itime = BitConverter.ToInt32(slice, slicePosition + 4);

                    DateTime date = TypeDecoder.DecodeDate(idate);
                    TimeSpan time = TypeDecoder.DecodeTime(itime);

                    DateTime timestamp = new System.DateTime(
                        date.Year, date.Month, date.Day,
                        time.Hours, time.Minutes, time.Seconds, time.Milliseconds);

                    tempData.SetValue(timestamp, i);
                }
                break;
                }

                slicePosition += itemLength;
            }

            if (systemType.IsPrimitive)
            {
                // For primitive types we can use System.Buffer	to copy	generated data to destination array
                Buffer.BlockCopy(tempData, 0, sliceData, 0, Buffer.ByteLength(tempData));
            }
            else
            {
                sliceData = tempData;
            }

            return(sliceData);
        }
Example #6
0
        protected override Array DecodeSlice(byte[] slice)
        {
            Array sliceData     = null;
            var   slicePosition = 0;
            var   type          = 0;
            var   dbType        = DbDataType.Array;
            var   systemType    = GetSystemType();
            var   charset       = _db.Charset;
            var   lengths       = new int[Descriptor.Dimensions];
            var   lowerBounds   = new int[Descriptor.Dimensions];

            for (var i = 0; i < Descriptor.Dimensions; i++)
            {
                lowerBounds[i] = Descriptor.Bounds[i].LowerBound;
                lengths[i]     = Descriptor.Bounds[i].UpperBound;

                if (lowerBounds[i] == 0)
                {
                    lengths[i]++;
                }
            }

            sliceData = Array.CreateInstance(systemType, lengths, lowerBounds);

            var tempData = Array.CreateInstance(systemType, sliceData.Length);

            type   = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType);
            dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, 0, Descriptor.Scale);

            int itemLength = Descriptor.Length;

            for (var i = 0; i < tempData.Length; i++)
            {
                if (slicePosition >= slice.Length)
                {
                    break;
                }

                switch (dbType)
                {
                case DbDataType.Char:
                    tempData.SetValue(charset.GetString(slice, slicePosition, itemLength), i);
                    break;

                case DbDataType.VarChar:
                {
                    var index = slicePosition;
                    var count = 0;
                    while (slice[index++] != 0)
                    {
                        count++;
                    }
                    tempData.SetValue(charset.GetString(slice, slicePosition, count), i);

                    slicePosition += 2;
                }
                break;

                case DbDataType.SmallInt:
                    tempData.SetValue(BitConverter.ToInt16(slice, slicePosition), i);
                    break;

                case DbDataType.Integer:
                    tempData.SetValue(BitConverter.ToInt32(slice, slicePosition), i);
                    break;

                case DbDataType.BigInt:
                    tempData.SetValue(BitConverter.ToInt64(slice, slicePosition), i);
                    break;

                case DbDataType.Decimal:
                case DbDataType.Numeric:
                {
                    object evalue = null;

                    switch (type)
                    {
                    case IscCodes.SQL_SHORT:
                        evalue = BitConverter.ToInt16(slice, slicePosition);
                        break;

                    case IscCodes.SQL_LONG:
                        evalue = BitConverter.ToInt32(slice, slicePosition);
                        break;

                    case IscCodes.SQL_QUAD:
                    case IscCodes.SQL_INT64:
                        evalue = BitConverter.ToInt64(slice, slicePosition);
                        break;
                    }

                    var dvalue = TypeDecoder.DecodeDecimal(evalue, Descriptor.Scale, type);

                    tempData.SetValue(dvalue, i);
                }
                break;

                case DbDataType.Double:
                    tempData.SetValue(BitConverter.ToDouble(slice, slicePosition), i);
                    break;

                case DbDataType.Float:
                    tempData.SetValue(BitConverter.ToSingle(slice, slicePosition), i);
                    break;

                case DbDataType.Date:
                {
                    var idate = BitConverter.ToInt32(slice, slicePosition);

                    var date = TypeDecoder.DecodeDate(idate);

                    tempData.SetValue(date, i);
                }
                break;

                case DbDataType.Time:
                {
                    var itime = BitConverter.ToInt32(slice, slicePosition);

                    var time = TypeDecoder.DecodeTime(itime);

                    tempData.SetValue(time, i);
                }
                break;

                case DbDataType.TimeStamp:
                {
                    var idate = BitConverter.ToInt32(slice, slicePosition);
                    var itime = BitConverter.ToInt32(slice, slicePosition + 4);

                    var date = TypeDecoder.DecodeDate(idate);
                    var time = TypeDecoder.DecodeTime(itime);

                    var timestamp = date.Add(time);

                    tempData.SetValue(timestamp, i);
                }
                break;
                }

                slicePosition += itemLength;
            }

            if (systemType.GetTypeInfo().IsPrimitive)
            {
                // For primitive types we can use System.Buffer	to copy	generated data to destination array
                Buffer.BlockCopy(tempData, 0, sliceData, 0, Buffer.ByteLength(tempData));
            }
            else
            {
                sliceData = tempData;
            }

            return(sliceData);
        }
    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);
            }
        }
    }