Ejemplo n.º 1
0
        public virtual void DropDatabase()
        {
            lock (SyncObject)
            {
                try
                {
                    XdrStream.Write(IscCodes.op_drop_database);
                    XdrStream.Write(_handle);
                    XdrStream.Flush();

                    ReadResponse();

                    _handle = 0;
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
                }
                finally
                {
                    try
                    {
                        CloseConnection();
                    }
                    catch
                    { }
                }
            }
        }
        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;

                if (TransactionCount > 0)
                {
                    throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
                }

                try
                {
                    CloseEventManager();

                    var detach = _handle != -1;
                    if (detach)
                    {
                        XdrStream.Write(IscCodes.op_detach);
                        XdrStream.Write(_handle);
                    }
                    XdrStream.Write(IscCodes.op_disconnect);
                    XdrStream.Flush();
                    if (detach)
                    {
                        ReadResponse();
                    }

                    CloseConnection();

#warning Here
                    _xdrStream?.Dispose();
                }
                catch (IOException ex)
                {
                    try
                    {
                        CloseConnection();
                    }
                    catch (IOException ex2)
                    {
                        throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
                    }
                    throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
                }
                finally
                {
                    _xdrStream        = null;
                    _connection       = null;
                    _charset          = null;
                    _eventManager     = null;
                    _serverVersion    = null;
                    _dialect          = 0;
                    _handle           = -1;
                    _packetSize       = 0;
                    _warningMessage   = null;
                    _transactionCount = 0;
                }
            }
        }
Ejemplo n.º 3
0
        protected virtual byte[] WriteParameters()
        {
            if (_parameters == null)
            {
                return(null);
            }

            using (var xdr = new XdrStream(_database.Charset))
            {
                for (var i = 0; i < _parameters.Count; i++)
                {
                    var field = _parameters[i];
                    try
                    {
                        WriteRawParameter(xdr, field);
                        xdr.Write(field.NullFlag);
                    }
                    catch (IOException ex)
                    {
                        throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
                    }
                }

                return(xdr.ToArray());
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// isc_database_info
        /// </summary>
        private void DatabaseInfo(byte[] items, byte[] buffer, int bufferLength)
        {
            lock (SyncObject)
            {
                try
                {
                    // see src/remote/protocol.h for packet	definition (p_info struct)
                    XdrStream.Write(IscCodes.op_info_database);               //	operation
                    XdrStream.Write(_handle);                                 //	db_handle
                    XdrStream.Write(0);                                       //	incarnation
                    XdrStream.WriteBuffer(items, items.Length);               //	items
                    XdrStream.Write(bufferLength);                            //	result buffer length

                    XdrStream.Flush();

                    GenericResponse response = (GenericResponse)ReadResponse();

                    int responseLength = bufferLength;

                    if (response.Data.Length < bufferLength)
                    {
                        responseLength = response.Data.Length;
                    }

                    Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength);
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
                }
            }
        }
Ejemplo n.º 5
0
        public virtual void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
        {
            lock (SyncObject)
            {
                try
                {
                    SendCreateToBuffer(dpb, database);
                    XdrStream.Flush();

                    try
                    {
                        ProcessCreateResponse(ReadGenericResponse());

                        Detach();
                    }
                    catch (IscException)
                    {
                        try
                        {
                            CloseConnection();
                        }
                        catch
                        { }
                        throw;
                    }
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
                }
            }
        }
Ejemplo n.º 6
0
        private void DatabaseInfo(byte[] items, byte[] buffer, int bufferLength)
        {
            try
            {
                XdrStream.Write(IscCodes.op_info_database);
                XdrStream.Write(_handle);
                XdrStream.Write(Incarnation);
                XdrStream.WriteBuffer(items, items.Length);
                XdrStream.Write(bufferLength);

                XdrStream.Flush();

                GenericResponse response = (GenericResponse)ReadResponse();

                int responseLength = bufferLength;

                if (response.Data.Length < bufferLength)
                {
                    responseLength = response.Data.Length;
                }

                Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength);
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
            }
        }
Ejemplo n.º 7
0
        public void QueueEvents(RemoteEvent remoteEvent)
        {
            try
            {
                if (_eventManager == null)
                {
                    ConnectionRequest(out var auxHandle, out var ipAddress, out var portNumber);
                    _eventManager = new GdsEventManager(auxHandle, ipAddress, portNumber);
                    var dummy = _eventManager.WaitForEventsAsync(remoteEvent);
                }

                remoteEvent.LocalId++;

                EventParameterBuffer epb = remoteEvent.BuildEpb();
                byte[] epbData           = epb.ToArray();

                XdrStream.Write(IscCodes.op_que_events);
                XdrStream.Write(_handle);
                XdrStream.WriteBuffer(epbData);
                XdrStream.Write(AddressOfAstRoutine);
                XdrStream.Write(ArgumentToAstRoutine);
                XdrStream.Write(remoteEvent.LocalId);

                XdrStream.Flush();

                GenericResponse response = (GenericResponse)ReadResponse();

                remoteEvent.RemoteId = response.ObjectHandle;
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
            }
        }
Ejemplo n.º 8
0
 public GdsDatabase(GdsConnection connection)
 {
     _connection = connection;
     _charset    = Charset.DefaultCharset;
     _dialect    = 3;
     _packetSize = 8192;
     _xdrStream  = _connection.CreateXdrStream();
 }
Ejemplo n.º 9
0
        public virtual void ConnectionRequest(out int auxHandle, out string ipAddress, out int portNumber)
        {
            lock (SyncObject)
            {
                try
                {
                    XdrStream.Write(IscCodes.op_connect_request);
                    XdrStream.Write(IscCodes.P_REQ_async);                        // Connection type
                    XdrStream.Write(_handle);                                     // Related object
                    XdrStream.Write(0);                                           // Partner identification

                    XdrStream.Flush();

                    ReadOperation();

                    auxHandle = XdrStream.ReadInt32();

                    // garbage
                    XdrStream.ReadBytes(8);

                    int respLen = XdrStream.ReadInt32();
                    respLen += respLen % 4;

                    // sin_family
                    XdrStream.ReadBytes(2);
                    respLen -= 2;

                    // sin_port
                    byte[] buffer = XdrStream.ReadBytes(2);
                    portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 0));
                    respLen   -= 2;

                    // * The address returned by the server may be incorrect if it is behind a NAT box
                    // * so we must use the address that was used to connect the main socket, not the
                    // * address reported by the server.
                    // sin_addr
                    buffer = XdrStream.ReadBytes(4);
                    //ipAddress = string.Format(
                    //    CultureInfo.InvariantCulture,
                    //    "{0}.{1}.{2}.{3}",
                    //    buffer[0], buffer[1], buffer[2], buffer[3]);
                    ipAddress = _connection.IPAddress.ToString();
                    respLen  -= 4;

                    // garbage
                    XdrStream.ReadBytes(respLen);

                    // Read Status Vector
                    XdrStream.ReadStatusVector();
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
                }
            }
        }
Ejemplo n.º 10
0
 protected virtual void SendAttachToBuffer(DatabaseParameterBuffer dpb, string database)
 {
     XdrStream.Write(IscCodes.op_attach);
     XdrStream.Write(0);
     if (!string.IsNullOrEmpty(Password))
     {
         dpb.Append(IscCodes.isc_dpb_password, Password);
     }
     XdrStream.WriteBuffer(Encoding.Default.GetBytes(database));
     XdrStream.WriteBuffer(dpb.ToArray());
 }
Ejemplo n.º 11
0
        public virtual void Detach()
        {
            lock (SyncObject)
            {
                if (TransactionCount > 0)
                {
                    throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
                }

                try
                {
                    if (_handle != 0)
                    {
                        XdrStream.Write(IscCodes.op_detach);
                        XdrStream.Write(_handle);
                    }
                    XdrStream.Write(IscCodes.op_disconnect);
                    XdrStream.Flush();

                    // Close the Event Manager
                    CloseEventManager();

                    // Disconnect
                    CloseConnection();

#warning Here
                    // Close Input and Output streams
                    _xdrStream?.Close();

                    // Clear members
                    _transactionCount = 0;
                    _handle           = 0;
                    _dialect          = 0;
                    _packetSize       = 0;
                    _xdrStream        = null;
                    _charset          = null;
                    _connection       = null;
                    _serverVersion    = null;
                }
                catch (IOException ex)
                {
                    try
                    {
                        CloseConnection();
                    }
                    catch (IOException ex2)
                    {
                        throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
                    }

                    throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
                }
            }
        }
Ejemplo n.º 12
0
        public GdsDatabase(GdsConnection connection)
        {
            this.connection = connection;
            this.charset = Charset.DefaultCharset;
            this.dialect = 3;
            this.packetSize = 8192;
            this.inputStream = this.connection.CreateXdrStream();
            this.outputStream = this.connection.CreateXdrStream();

            GC.SuppressFinalize(this);
        }
        public GdsDatabase(GdsConnection connection)
        {
            _connection   = connection;
            _charset      = Charset.DefaultCharset;
            _dialect      = 3;
            _packetSize   = 8192;
            _inputStream  = _connection.CreateXdrStream();
            _outputStream = _connection.CreateXdrStream();

            GC.SuppressFinalize(this);
        }
Ejemplo n.º 14
0
        public GdsDatabase(GdsConnection connection)
        {
            this.connection   = connection;
            this.charset      = Charset.DefaultCharset;
            this.dialect      = 3;
            this.packetSize   = 8192;
            this.inputStream  = this.connection.CreateXdrStream();
            this.outputStream = this.connection.CreateXdrStream();

            GC.SuppressFinalize(this);
        }
 protected virtual void SendCreateToBuffer(DatabaseParameterBuffer dpb, string database)
 {
     XdrStream.Write(IscCodes.op_create);
     XdrStream.Write(DatabaseObjectId);
     if (!string.IsNullOrEmpty(Password))
     {
         dpb.Append(IscCodes.isc_dpb_password, Password);
     }
     XdrStream.WriteBuffer(Encoding2.Default.GetBytes(database));
     XdrStream.WriteBuffer(dpb.ToArraySegment());
 }
Ejemplo n.º 16
0
        protected virtual void SendCreateToBuffer(DatabaseParameterBuffer dpb, string database)
        {
            XdrStream.Write(IscCodes.op_create);
#warning Some constant for default database object ID
            XdrStream.Write(0);
            if (!string.IsNullOrEmpty(Password))
            {
                dpb.Append(IscCodes.isc_dpb_password, Password);
            }
            XdrStream.WriteBuffer(Encoding.Default.GetBytes(database));
            XdrStream.WriteBuffer(dpb.ToArray());
        }
Ejemplo n.º 17
0
        public virtual void ConnectionRequest(out int auxHandle, out string ipAddress, out int portNumber)
        {
            try
            {
                XdrStream.Write(IscCodes.op_connect_request);
                XdrStream.Write(IscCodes.P_REQ_async);
                XdrStream.Write(_handle);
                XdrStream.Write(PartnerIdentification);

                XdrStream.Flush();

                ReadOperation();

                auxHandle = XdrStream.ReadInt32();

                var garbage1 = new byte[8];
                XdrStream.ReadBytes(garbage1, 8);

                var respLen = XdrStream.ReadInt32();
                respLen += respLen % 4;

                var sin_family = new byte[2];
                XdrStream.ReadBytes(sin_family, 2);
                respLen -= 2;

                var sin_port = new byte[2];
                XdrStream.ReadBytes(sin_port, 2);
                portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(sin_port, 0));
                respLen   -= 2;

                // * The address returned by the server may be incorrect if it is behind a NAT box
                // * so we must use the address that was used to connect the main socket, not the
                // * address reported by the server.
                var sin_addr = new byte[4];
                XdrStream.ReadBytes(sin_addr, 4);
                //ipAddress = string.Format(
                //    CultureInfo.InvariantCulture,
                //    "{0}.{1}.{2}.{3}",
                //    buffer[0], buffer[1], buffer[2], buffer[3]);
                ipAddress = _connection.IPAddress.ToString();
                respLen  -= 4;

                var garbage2 = new byte[respLen];
                XdrStream.ReadBytes(garbage2, respLen);

                XdrStream.ReadStatusVector();
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
            }
        }
Ejemplo n.º 18
0
 public virtual void ReleaseObject(int op, int id)
 {
     try
     {
         DoReleaseObjectPacket(op, id);
         XdrStream.Flush();
         ProcessReleaseObjectResponse(ReadResponse());
     }
     catch (IOException ex)
     {
         throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
     }
 }
Ejemplo n.º 19
0
 public virtual void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
 {
     try
     {
         SendCreateToBuffer(dpb, database);
         XdrStream.Flush();
         ProcessCreateResponse(ReadGenericResponse());
     }
     catch (IOException ex)
     {
         throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
     }
 }
Ejemplo n.º 20
0
        public void CancelEvents(RemoteEvent events)
        {
            try
            {
                XdrStream.Write(IscCodes.op_cancel_events);
                XdrStream.Write(_handle);
                XdrStream.Write(events.LocalId);

                XdrStream.Flush();

                ReadResponse();
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
            }
        }
Ejemplo n.º 21
0
        protected void SendExecuteToBuffer()
        {
            // this may throw error, so it needs to be before any writing
            byte[] descriptor = null;
            if (this.parameters != null)
            {
                using (XdrStream xdr = new XdrStream(this.database.Charset))
                {
                    xdr.Write(this.parameters);
                    descriptor = xdr.ToArray();
                }
            }

            // Write the message
            if (this.statementType == DbStatementType.StoredProcedure)
            {
                this.database.Write(IscCodes.op_execute2);
            }
            else
            {
                this.database.Write(IscCodes.op_execute);
            }

            this.database.Write(this.handle);
            this.database.Write(this.transaction.Handle);

            if (this.parameters != null)
            {
                this.database.WriteBuffer(this.parameters.ToBlrArray());
                this.database.Write(0); // Message number
                this.database.Write(1); // Number of messages
                this.database.Write(descriptor, 0, descriptor.Length);
            }
            else
            {
                this.database.WriteBuffer(null);
                this.database.Write(0);
                this.database.Write(0);
            }

            if (this.statementType == DbStatementType.StoredProcedure)
            {
                this.database.WriteBuffer((this.fields == null) ? null : this.fields.ToBlrArray());
                this.database.Write(0); // Output message number
            }
        }
Ejemplo n.º 22
0
        public virtual void DropDatabase()
        {
            try
            {
                XdrStream.Write(IscCodes.op_drop_database);
                XdrStream.Write(_handle);
                XdrStream.Flush();

                ReadResponse();

                _handle = -1;
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
            }
        }
Ejemplo n.º 23
0
        public void QueueEvents(RemoteEvent events)
        {
            if (_eventManager == null)
            {
                string ipAddress  = string.Empty;
                int    portNumber = 0;
                int    auxHandle  = 0;

                ConnectionRequest(out auxHandle, out ipAddress, out portNumber);

                _eventManager = new GdsEventManager(auxHandle, ipAddress, portNumber);
            }

            lock (SyncObject)
            {
                try
                {
                    events.LocalId = Interlocked.Increment(ref _eventsId);

                    // Enqueue events in the event manager
                    _eventManager.QueueEvents(events);

                    EventParameterBuffer epb = events.ToEpb();

                    XdrStream.Write(IscCodes.op_que_events);
                    XdrStream.Write(_handle);                                     // Database object id
                    XdrStream.WriteBuffer(epb.ToArray());                         // Event description block
                    XdrStream.Write(0);                                           // Address of ast routine
                    XdrStream.Write(0);                                           // Argument to ast routine
                    XdrStream.Write(events.LocalId);                              // Client side id of remote event

                    XdrStream.Flush();

                    GenericResponse response = (GenericResponse)ReadResponse();

                    // Update event	Remote event ID
                    events.RemoteId = response.ObjectHandle;
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
                }
            }
        }
Ejemplo n.º 24
0
        public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
        {
            try
            {
                SendAttachToBuffer(dpb, database);
                XdrStream.Flush();
                ProcessAttachResponse(ReadGenericResponse());
            }
            catch (IscException)
            {
                SafelyDetach();
                throw;
            }
            catch (IOException ex)
            {
                SafelyDetach();
                throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
            }

            AfterAttachActions();
        }
Ejemplo n.º 25
0
        public void CancelEvents(RemoteEvent events)
        {
            lock (SyncObject)
            {
                try
                {
                    XdrStream.Write(IscCodes.op_cancel_events);
                    XdrStream.Write(_handle);                                 // Database object id
                    XdrStream.Write(events.LocalId);                          // Event ID

                    XdrStream.Flush();

                    ReadResponse();

                    _eventManager.CancelEvents(events);
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
                }
            }
        }
Ejemplo n.º 26
0
        private byte[] EncodeSliceArray(Array sourceArray)
        {
            DbDataType dbType  = DbDataType.Array;
            Charset    charset = _database.Charset;
            int        subType = (Descriptor.Scale < 0) ? 2 : 0;
            int        type    = 0;

            using (XdrStream xdr = new XdrStream(_database.Charset))
            {
                type   = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType);
                dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, subType, Descriptor.Scale);

                foreach (object source in sourceArray)
                {
                    switch (dbType)
                    {
                    case DbDataType.Char:
                        byte[] buffer = charset.GetBytes(source.ToString());
                        xdr.WriteOpaque(buffer, Descriptor.Length);
                        break;

                    case DbDataType.VarChar:
                        xdr.Write((string)source);
                        break;

                    case DbDataType.SmallInt:
                        xdr.Write((short)source);
                        break;

                    case DbDataType.Integer:
                        xdr.Write((int)source);
                        break;

                    case DbDataType.BigInt:
                        xdr.Write((long)source);
                        break;

                    case DbDataType.Decimal:
                    case DbDataType.Numeric:
                        xdr.Write((decimal)source, type, Descriptor.Scale);
                        break;

                    case DbDataType.Float:
                        xdr.Write((float)source);
                        break;

                    case DbDataType.Double:
                        xdr.Write((double)source);
                        break;

                    case DbDataType.Date:
                        xdr.WriteDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat));
                        break;

                    case DbDataType.Time:
                        xdr.WriteTime((TimeSpan)source);
                        break;

                    case DbDataType.TimeStamp:
                        xdr.Write(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat));
                        break;

                    default:
                        throw TypeHelper.InvalidDataType((int)dbType);
                    }
                }

                return(xdr.ToArray());
            }
        }
Ejemplo n.º 27
0
        private byte[] ReceiveSliceResponse(ArrayDesc desc)
        {
            try
            {
                int operation = _database.ReadOperation();

                if (operation == IscCodes.op_slice)
                {
                    bool isVariying = false;
                    int  elements   = 0;
                    int  length     = _database.XdrStream.ReadInt32();

                    length = _database.XdrStream.ReadInt32();

                    switch (desc.DataType)
                    {
                    case IscCodes.blr_text:
                    case IscCodes.blr_text2:
                    case IscCodes.blr_cstring:
                    case IscCodes.blr_cstring2:
                        elements = length / desc.Length;
                        length  += elements * ((4 - desc.Length) & 3);
                        break;

                    case IscCodes.blr_varying:
                    case IscCodes.blr_varying2:
                        elements   = length / desc.Length;
                        isVariying = true;
                        break;

                    case IscCodes.blr_short:
                        length = length * desc.Length;
                        break;
                    }

                    if (isVariying)
                    {
                        using (XdrStream xdr = new XdrStream())
                        {
                            for (int i = 0; i < elements; i++)
                            {
                                byte[] buffer = _database.XdrStream.ReadOpaque(_database.XdrStream.ReadInt32());

                                xdr.WriteBuffer(buffer, buffer.Length);
                            }

                            return(xdr.ToArray());
                        }
                    }
                    else
                    {
                        return(_database.XdrStream.ReadOpaque(length));
                    }
                }
                else
                {
                    _database.SetOperation(operation);
                    _database.ReadResponse();

                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
            }
        }
Ejemplo n.º 28
0
        protected override Array DecodeSlice(byte[] slice)
        {
            DbDataType dbType     = DbDataType.Array;
            Array      sliceData  = null;
            Array      tempData   = null;
            Type       systemType = GetSystemType();

            int[] lengths     = new int[Descriptor.Dimensions];
            int[] lowerBounds = new int[Descriptor.Dimensions];
            int   type        = 0;
            int   index       = 0;

            for (int 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);
            tempData  = Array.CreateInstance(systemType, sliceData.Length);

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

            using (XdrStream xdr = new XdrStream(slice, _database.Charset))
            {
                while (xdr.Position < xdr.Length)
                {
                    switch (dbType)
                    {
                    case DbDataType.Char:
                        tempData.SetValue(xdr.ReadString(Descriptor.Length), index);
                        break;

                    case DbDataType.VarChar:
                        tempData.SetValue(xdr.ReadString(), index);
                        break;

                    case DbDataType.SmallInt:
                        tempData.SetValue(xdr.ReadInt16(), index);
                        break;

                    case DbDataType.Integer:
                        tempData.SetValue(xdr.ReadInt32(), index);
                        break;

                    case DbDataType.BigInt:
                        tempData.SetValue(xdr.ReadInt64(), index);
                        break;

                    case DbDataType.Numeric:
                    case DbDataType.Decimal:
                        tempData.SetValue(xdr.ReadDecimal(type, Descriptor.Scale), index);
                        break;

                    case DbDataType.Float:
                        tempData.SetValue(xdr.ReadSingle(), index);
                        break;

                    case DbDataType.Double:
                        tempData.SetValue(xdr.ReadDouble(), index);
                        break;

                    case DbDataType.Date:
                        tempData.SetValue(xdr.ReadDate(), index);
                        break;

                    case DbDataType.Time:
                        tempData.SetValue(xdr.ReadTime(), index);
                        break;

                    case DbDataType.TimeStamp:
                        tempData.SetValue(xdr.ReadDateTime(), index);
                        break;
                    }

                    index++;
                }

#if NET40
                if (systemType.IsPrimitive)
#else
                if (systemType.GetTypeInfo().IsPrimitive)
#endif
                {
                    // 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 GdsDatabase(GdsConnection connection)
		{
			_connection = connection;
			_charset = Charset.DefaultCharset;
			_dialect = 3;
			_packetSize = 8192;
			_inputStream = _connection.CreateXdrStream();
			_outputStream = _connection.CreateXdrStream();

			GC.SuppressFinalize(this);
		}
Ejemplo n.º 30
0
 protected void DoReleaseObjectPacket(int op, int id)
 {
     XdrStream.Write(op);
     XdrStream.Write(id);
 }
		public virtual void Detach()
		{
			lock (SyncObject)
			{
				if (TransactionCount > 0)
				{
					throw new IscException(IscCodes.isc_open_trans, TransactionCount);
				}

				try
				{
					if (_handle != 0)
					{
						Write(IscCodes.op_detach);
						Write(_handle);
					}
					Write(IscCodes.op_disconnect);
					Flush();

					// Close the Event Manager
					CloseEventManager();

					// Disconnect
					CloseConnection();

					// Close Input and Output streams
					if (_inputStream != null)
					{
						_inputStream.Close();
					}
					if (_outputStream != null)
					{
						_outputStream.Close();
					}

					// Clear members
					_transactionCount = 0;
					_handle = 0;
					_dialect = 0;
					_packetSize = 0;
					_operation = 0;
					_outputStream = null;
					_inputStream = null;
					_charset = null;
					_connection = null;
					_serverVersion = null;
				}
				catch (IOException)
				{
					try
					{
						CloseConnection();
					}
					catch (IOException)
					{
						throw new IscException(IscCodes.isc_network_error);
					}

					throw new IscException(IscCodes.isc_network_error);
				}
			}
		}
		public virtual void Detach()
		{
			lock (SyncObject)
			{
				if (TransactionCount > 0)
				{
					throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
				}

				try
				{
					if (_handle != 0)
					{
						XdrStream.Write(IscCodes.op_detach);
						XdrStream.Write(_handle);
					}
					XdrStream.Write(IscCodes.op_disconnect);
					XdrStream.Flush();

					// Close the Event Manager
					CloseEventManager();

					// Disconnect
					CloseConnection();

#warning Here
					// Close Input and Output streams
					_xdrStream?.Close();

					// Clear members
					_transactionCount = 0;
					_handle = 0;
					_dialect = 0;
					_packetSize = 0;
					_operation = 0;
					_xdrStream = null;
					_charset = null;
					_connection = null;
					_serverVersion = null;
				}
				catch (IOException ex)
				{
					try
					{
						CloseConnection();
					}
					catch (IOException ex2)
					{
						throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
					}

					throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
				}
			}
		}
		public GdsDatabase(GdsConnection connection)
		{
			_connection = connection;
			_charset = Charset.DefaultCharset;
			_dialect = 3;
			_packetSize = 8192;
			_xdrStream = _connection.CreateXdrStream();
		}
Ejemplo n.º 34
0
        public virtual void Detach()
        {
            lock (this.SyncObject)
            {
                if (this.TransactionCount > 0)
                {
                    throw new IscException(IscCodes.isc_open_trans, this.TransactionCount);
                }

                try
                {
                    if (this.handle != 0)
                    {
                        this.Write(IscCodes.op_detach);
                        this.Write(this.handle);
                    }
                    this.Write(IscCodes.op_disconnect);
                    this.Flush();

                    // Close the Event Manager
                    this.CloseEventManager();

                    // Disconnect
                    this.CloseConnection();

                    // Close Input and Output streams
                    if (this.inputStream != null)
                    {
                        this.inputStream.Close();
                    }
                    if (this.outputStream != null)
                    {
                        this.outputStream.Close();
                    }

                    // Clear members
                    this.transactionCount = 0;
                    this.handle = 0;
                    this.dialect = 0;
                    this.packetSize = 0;
                    this.operation = 0;
                    this.outputStream = null;
                    this.inputStream = null;
                    this.charset = null;
                    this.connection = null;
                    this.serverVersion = null;
                }
                catch (IOException)
                {
                    try
                    {
                        this.CloseConnection();
                    }
                    catch (IOException)
                    {
                        throw new IscException(IscCodes.isc_network_error);
                    }

                    throw new IscException(IscCodes.isc_network_error);
                }
            }
        }
Ejemplo n.º 35
0
        protected override System.Array DecodeSlice(byte[] slice)
        {
            DbDataType dbType     = DbDataType.Array;
            Array      sliceData  = null;
            Array      tempData   = null;
            Type       systemType = this.GetSystemType();

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

            // 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 arrays
#if     (NET_CF)
            sliceData = Array.CreateInstance(systemType, lengths);
#else
            sliceData = Array.CreateInstance(systemType, lengths, lowerBounds);
#endif
            tempData = Array.CreateInstance(systemType, sliceData.Length);

            // Infer Firebird and Db datatypes
            type   = TypeHelper.GetFbType(this.Descriptor.DataType);
            dbType = TypeHelper.GetDbDataType(this.Descriptor.DataType, 0, this.Descriptor.Scale);

            // Decode slice	data
            XdrStream xdr = new XdrStream(slice, this.database.Charset);

            while (xdr.Position < xdr.Length)
            {
                switch (dbType)
                {
                case DbDataType.Char:
                    tempData.SetValue(xdr.ReadString(this.Descriptor.Length), index);
                    break;

                case DbDataType.VarChar:
                    tempData.SetValue(xdr.ReadString(), index);
                    break;

                case DbDataType.SmallInt:
                    tempData.SetValue(xdr.ReadInt16(), index);
                    break;

                case DbDataType.Integer:
                    tempData.SetValue(xdr.ReadInt32(), index);
                    break;

                case DbDataType.BigInt:
                    tempData.SetValue(xdr.ReadInt64(), index);
                    break;

                case DbDataType.Numeric:
                case DbDataType.Decimal:
                    tempData.SetValue(xdr.ReadDecimal(type, this.Descriptor.Scale), index);
                    break;

                case DbDataType.Float:
                    tempData.SetValue(xdr.ReadSingle(), index);
                    break;

                case DbDataType.Double:
                    tempData.SetValue(xdr.ReadDouble(), index);
                    break;

                case DbDataType.Date:
                    tempData.SetValue(xdr.ReadDate(), index);
                    break;

                case DbDataType.Time:
                    tempData.SetValue(xdr.ReadTime(), index);
                    break;

                case DbDataType.TimeStamp:
                    tempData.SetValue(xdr.ReadDateTime(), index);
                    break;
                }

                index++;
            }

            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;
            }

            // Close XDR stream
            xdr.Close();

            return(sliceData);
        }
Ejemplo n.º 36
0
        protected void WriteRawParameter(XdrStream xdr, DbField field)
        {
            if (field.DbDataType != DbDataType.Null)
            {
                field.FixNull();

                switch (field.DbDataType)
                {
                case DbDataType.Char:
                    if (field.Charset.IsOctetsCharset)
                    {
                        xdr.WriteOpaque(field.DbValue.GetBinary(), field.Length);
                    }
                    else
                    {
                        var svalue = field.DbValue.GetString();

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

                        xdr.WriteOpaque(field.Charset.GetBytes(svalue), field.Length);
                    }
                    break;

                case DbDataType.VarChar:
                    if (field.Charset.IsOctetsCharset)
                    {
                        xdr.WriteBuffer(field.DbValue.GetBinary());
                    }
                    else
                    {
                        var svalue = field.DbValue.GetString();

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

                        xdr.WriteBuffer(field.Charset.GetBytes(svalue));
                    }
                    break;

                case DbDataType.SmallInt:
                    xdr.Write(field.DbValue.GetInt16());
                    break;

                case DbDataType.Integer:
                    xdr.Write(field.DbValue.GetInt32());
                    break;

                case DbDataType.BigInt:
                case DbDataType.Array:
                case DbDataType.Binary:
                case DbDataType.Text:
                    xdr.Write(field.DbValue.GetInt64());
                    break;

                case DbDataType.Decimal:
                case DbDataType.Numeric:
                    xdr.Write(field.DbValue.GetDecimal(), field.DataType, field.NumericScale);
                    break;

                case DbDataType.Float:
                    xdr.Write(field.DbValue.GetFloat());
                    break;

                case DbDataType.Guid:
                    xdr.Write(field.DbValue.GetGuid());
                    break;

                case DbDataType.Double:
                    xdr.Write(field.DbValue.GetDouble());
                    break;

                case DbDataType.Date:
                    xdr.Write(field.DbValue.GetDate());
                    break;

                case DbDataType.Time:
                    xdr.Write(field.DbValue.GetTime());
                    break;

                case DbDataType.TimeStamp:
                    xdr.Write(field.DbValue.GetDate());
                    xdr.Write(field.DbValue.GetTime());
                    break;

                case DbDataType.Boolean:
                    xdr.Write(field.DbValue.GetBoolean());
                    break;

                default:
                    throw IscException.ForStrParam($"Unknown SQL data type: {field.DataType}.");
                }
            }
        }
Ejemplo n.º 37
0
		protected void SendExecuteToBuffer()
		{
			// this may throw error, so it needs to be before any writing
			byte[] descriptor = null;
			if (this.parameters != null)
			{
				using (XdrStream xdr = new XdrStream(this.database.Charset))
				{
					xdr.Write(this.parameters);
					descriptor = xdr.ToArray();
				}
			}

			// Write the message
			if (this.statementType == DbStatementType.StoredProcedure)
			{
				this.database.Write(IscCodes.op_execute2);
			}
			else
			{
				this.database.Write(IscCodes.op_execute);
			}

			this.database.Write(this.handle);
			this.database.Write(this.transaction.Handle);

			if (this.parameters != null)
			{
				this.database.WriteBuffer(this.parameters.ToBlrArray());
				this.database.Write(0);	// Message number
				this.database.Write(1);	// Number of messages
				this.database.Write(descriptor, 0, descriptor.Length);
			}
			else
			{
				this.database.WriteBuffer(null);
				this.database.Write(0);
				this.database.Write(0);
			}

			if (this.statementType == DbStatementType.StoredProcedure)
			{
				this.database.WriteBuffer((this.fields == null) ? null : this.fields.ToBlrArray());
				this.database.Write(0);	// Output message number
			}
		}
		protected override System.Array DecodeSlice(byte[] slice)
		{
			DbDataType	dbType		= DbDataType.Array;
			Array		sliceData	= null;
			Array		tempData	= null;
			Type		systemType	= GetSystemType();
			int[]		lengths		= new int[Descriptor.Dimensions];
			int[]		lowerBounds = new int[Descriptor.Dimensions];
			int			type		= 0;
			int			index		= 0;

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

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

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

			// Infer Firebird and Db datatypes
			type	= TypeHelper.GetFbType(Descriptor.DataType);
			dbType	= TypeHelper.GetDbDataType(Descriptor.DataType, 0, Descriptor.Scale);

			// Decode slice	data
			XdrStream xdr = new XdrStream(slice, _database.Charset);

			while (xdr.Position < xdr.Length)
			{
				switch (dbType)
				{
					case DbDataType.Char:
						tempData.SetValue(xdr.ReadString(Descriptor.Length), index);
						break;

					case DbDataType.VarChar:
						tempData.SetValue(xdr.ReadString(), index);
						break;

					case DbDataType.SmallInt:
						tempData.SetValue(xdr.ReadInt16(), index);
						break;

					case DbDataType.Integer:
						tempData.SetValue(xdr.ReadInt32(), index);
						break;

					case DbDataType.BigInt:
						tempData.SetValue(xdr.ReadInt64(), index);
						break;

					case DbDataType.Numeric:
					case DbDataType.Decimal:
						tempData.SetValue(xdr.ReadDecimal(type, Descriptor.Scale), index);
						break;

					case DbDataType.Float:
						tempData.SetValue(xdr.ReadSingle(), index);
						break;

					case DbDataType.Double:
						tempData.SetValue(xdr.ReadDouble(), index);
						break;

					case DbDataType.Date:
						tempData.SetValue(xdr.ReadDate(), index);
						break;

					case DbDataType.Time:
						tempData.SetValue(xdr.ReadTime(), index);
						break;

					case DbDataType.TimeStamp:
						tempData.SetValue(xdr.ReadDateTime(), index);
						break;
				}

				index++;
			}

			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;
			}

			// Close XDR stream
			xdr.Close();

			return sliceData;
		}
		private byte[] ReceiveSliceResponse(ArrayDesc desc)
		{
			try
			{
				int operation = _database.ReadOperation();

				if (operation == IscCodes.op_slice)
				{
					// Read	slice length
					bool	isVariying = false;
					int		elements = 0;
					int		length = _database.ReadInt32();

					length = _database.ReadInt32();

					switch (desc.DataType)
					{
						case IscCodes.blr_text:
						case IscCodes.blr_text2:
						case IscCodes.blr_cstring:
						case IscCodes.blr_cstring2:
							elements = length / desc.Length;
							length += elements * ((4 - desc.Length) & 3);
							break;

						case IscCodes.blr_varying:
						case IscCodes.blr_varying2:
							elements = length / desc.Length;
							isVariying = true;
							break;

						case IscCodes.blr_short:
							length = length * desc.Length;
							break;
					}

					if (isVariying)
					{
						XdrStream xdr = new XdrStream();

						for (int i = 0; i < elements; i++)
						{
							byte[] buffer = _database.ReadOpaque(_database.ReadInt32());

							xdr.WriteBuffer(buffer, buffer.Length);
						}

						return xdr.ToArray();
					}
					else
					{
						return _database.ReadOpaque(length);
					}
				}
				else
				{
					_database.SetOperation(operation);
					_database.ReadResponse();

					return null;
				}
			}
			catch (IOException)
			{
				throw new IscException(IscCodes.isc_net_read_err);
			}
		}
		private byte[] EncodeSliceArray(Array sourceArray)
		{
			DbDataType	dbType	= DbDataType.Array;
			Charset		charset = _database.Charset;
			XdrStream	xdr		= new XdrStream(_database.Charset);
			int         subType = (Descriptor.Scale < 0) ? 2 : 0;
			int			type	= 0;

			type = TypeHelper.GetFbType(Descriptor.DataType);
			dbType = TypeHelper.GetDbDataType(Descriptor.DataType, subType, Descriptor.Scale);

			foreach (object source in sourceArray)
			{
				switch (dbType)
				{
					case DbDataType.Char:
						byte[] buffer = charset.GetBytes(source.ToString());
						xdr.WriteOpaque(buffer, Descriptor.Length);
						break;

					case DbDataType.VarChar:
						xdr.Write((string)source);
						break;

					case DbDataType.SmallInt:
						xdr.Write((short)source);
						break;

					case DbDataType.Integer:
						xdr.Write((int)source);
						break;

					case DbDataType.BigInt:
						xdr.Write((long)source);
						break;

					case DbDataType.Decimal:
					case DbDataType.Numeric:
						xdr.Write((decimal)source, type, Descriptor.Scale);
						break;

					case DbDataType.Float:
						xdr.Write((float)source);
						break;

					case DbDataType.Double:
						xdr.Write((double)source);
						break;

					case DbDataType.Date:
						xdr.WriteDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat));
						break;

					case DbDataType.Time:
						xdr.WriteTime((TimeSpan)source);
						break;

					case DbDataType.TimeStamp:
						xdr.Write(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat));
						break;

					default:
						throw new NotSupportedException("Unknown data type");
				}
			}

			return xdr.ToArray();
		}