Ejemplo n.º 1
0
        private byte[] GetNumericBytes()
        {
            decimal value   = GetDecimal();
            object  numeric = TypeEncoder.EncodeDecimal(value, Field.NumericScale, Field.DataType);

            switch (_field.SqlType)
            {
            case IscCodes.SQL_SHORT:
                return(BitConverter.GetBytes((short)numeric));

            case IscCodes.SQL_LONG:
                return(BitConverter.GetBytes((int)numeric));

            case IscCodes.SQL_INT64:
            case IscCodes.SQL_QUAD:
                return(BitConverter.GetBytes((long)numeric));

            case IscCodes.SQL_DOUBLE:
                return(BitConverter.GetBytes(GetDouble()));

            default:
                return(null);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        public byte[] GetBytes()
        {
            if (this.IsDBNull())
            {
                int length = field.Length;

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

                return(new byte[length]);
            }


            switch (this.Field.DbDataType)
            {
            case DbDataType.Char:
                if (this.Field.Charset.IsOctetsCharset)
                {
                    return((byte[])this.value);
                }
                else
                {
                    string svalue = this.GetString();

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

                    byte[] buffer = new byte[this.Field.Length];
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        buffer[i] = 32;
                    }

                    byte[] bytes = this.Field.Charset.GetBytes(svalue);

                    Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);

                    return(buffer);
                }

            case DbDataType.VarChar:
                if (this.Field.Charset.IsOctetsCharset)
                {
                    return((byte[])this.value);
                }
                else
                {
                    string svalue = this.GetString();

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

                    byte[] sbuffer = this.Field.Charset.GetBytes(svalue);
                    byte[] buffer  = new byte[this.Field.Length + 2];

                    // Copy	length
                    Buffer.BlockCopy(BitConverter.GetBytes((short)sbuffer.Length), 0, buffer, 0, 2);

                    // Copy	string value
                    Buffer.BlockCopy(sbuffer, 0, buffer, 2, sbuffer.Length);

                    return(buffer);
                }

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

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

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

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

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

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

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

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

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

                byte[] 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(this.GetGuid().ToByteArray());

            default:
                throw new NotSupportedException("Unknown data type");
            }
        }
Ejemplo n.º 4
0
 public int GetDate()
 {
     return(TypeEncoder.EncodeDate(GetDateTime()));
 }
Ejemplo n.º 5
0
 public int EncodeTime()
 {
     return(TypeEncoder.EncodeTime(this.GetDateTime()));
 }