Exemplo n.º 1
0
        public byte[] GetBinary()
        {
            if (_value is long l)
            {
                _value = GetBlobData(l);
            }
            if (_value is Guid guid)
            {
                return(TypeEncoder.EncodeGuid(guid));
            }

            return((byte[])_value);
        }
Exemplo 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 if (Field.Charset.IsNoneCharset)
                {
                    var bvalue = Field.Charset.GetBytes(GetString());
                    if (bvalue.Length > Field.Length)
                    {
                        throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation });
                    }
                    bytes = bvalue;
                }
                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 if (Field.Charset.IsNoneCharset)
                {
                    var bvalue = Field.Charset.GetBytes(GetString());
                    if (bvalue.Length > Field.Length)
                    {
                        throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation });
                    }
                    bytes = bvalue;
                }
                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(TypeEncoder.EncodeGuid(GetGuid()));

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

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