Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 public TimeSpan ReadTime()
 {
     return(TypeDecoder.DecodeTime(ReadInt32()));
 }
        protected override Task <Array> DecodeSlice(byte[] slice, AsyncWrappingCommonArgs async)
        {
            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(Task.FromResult(sliceData));
        }
 public async Task <TimeSpan> ReadTime(AsyncWrappingCommonArgs async)
 {
     return(TypeDecoder.DecodeTime(await ReadInt32(async).ConfigureAwait(false)));
 }