예제 #1
0
        public override void ChangeDatabase(string db)
        {
            CheckClosed();

            if (string.IsNullOrEmpty(db))
            {
                throw new InvalidOperationException("Database name is not valid.");
            }

            var oldConnectionString = _connectionString;

            try
            {
                var csb = new FbConnectionStringBuilder(_connectionString);

                /* Close current connection	*/
                Close();

                /* Set up the new Database	*/
                csb.Database     = db;
                ConnectionString = csb.ToString();

                /* Open	new	connection	*/
                Open();
            }
            catch (IscException ex)
            {
                ConnectionString = oldConnectionString;
                throw FbException.Create(ex);
            }
        }
예제 #2
0
        public void Connect()
        {
            if (Charset.GetCharset(_options.Charset) == null)
            {
                throw FbException.Create("Invalid character set specified");
            }

            try
            {
                _db            = ClientFactory.CreateDatabase(_options);
                _db.Charset    = Charset.GetCharset(_options.Charset);
                _db.Dialect    = _options.Dialect;
                _db.PacketSize = _options.PacketSize;

                var dpb = BuildDpb(_db, _options);

                if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
                {
                    _db.AttachWithTrustedAuth(dpb, _options.Database, _options.CryptKey);
                }
                else
                {
                    _db.Attach(dpb, _options.Database, _options.CryptKey);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #3
0
        private async Task ChangeDatabaseImpl(string db, AsyncWrappingCommonArgs async)
        {
            CheckClosed();

            if (string.IsNullOrEmpty(db))
            {
                throw new InvalidOperationException("Database name is not valid.");
            }

            var oldConnectionString = _connectionString;

            try
            {
                var csb = new FbConnectionStringBuilder(_connectionString);

                /* Close current connection	*/
                await CloseImpl(async).ConfigureAwait(false);

                /* Set up the new Database	*/
                csb.Database     = db;
                ConnectionString = csb.ToString();

                /* Open	new	connection	*/
                await OpenImpl(async).ConfigureAwait(false);
            }
            catch (IscException ex)
            {
                ConnectionString = oldConnectionString;
                throw FbException.Create(ex);
            }
        }
예제 #4
0
 protected override void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         _disposed = true;
         if (_transaction != null)
         {
             if (!_isCompleted)
             {
                 try
                 {
                     _transaction.Dispose2();
                 }
                 catch (IscException ex)
                 {
                     throw FbException.Create(ex);
                 }
             }
         }
         _connection  = null;
         _transaction = null;
         _isCompleted = true;
     }
     base.Dispose(disposing);
 }
예제 #5
0
        public override async Task ChangeDatabaseAsync(string db, CancellationToken cancellationToken = default)
#endif
        {
            CheckClosed();

            if (string.IsNullOrEmpty(db))
            {
                throw new InvalidOperationException("Database name is not valid.");
            }

            var oldConnectionString = _connectionString;

            try
            {
                var csb = new FbConnectionStringBuilder(_connectionString);

                /* Close current connection	*/
                await CloseAsync().ConfigureAwait(false);

                /* Set up the new Database	*/
                csb.Database     = db;
                ConnectionString = csb.ToString();

                /* Open	new	connection	*/
                await OpenAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (IscException ex)
            {
                ConnectionString = oldConnectionString;
                throw FbException.Create(ex);
            }
        }
예제 #6
0
        public async Task ConnectAsync(CancellationToken cancellationToken = default)
        {
            if (Charset.GetCharset(_options.Charset) == null)
            {
                throw FbException.Create("Invalid character set specified");
            }

            try
            {
                _db = await ClientFactory.CreateDatabaseAsync(_options, cancellationToken).ConfigureAwait(false);

                _db.Charset    = Charset.GetCharset(_options.Charset);
                _db.Dialect    = _options.Dialect;
                _db.PacketSize = _options.PacketSize;

                var dpb = BuildDpb(_db, _options);

                if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
                {
                    await _db.AttachWithTrustedAuthAsync(dpb, _options.Database, _options.CryptKey, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await _db.AttachAsync(dpb, _options.Database, _options.CryptKey, cancellationToken).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #7
0
 public override async ValueTask DisposeAsync()
 {
     if (!_disposed)
     {
         _disposed = true;
         if (_transaction != null)
         {
             if (!_isCompleted)
             {
                 try
                 {
                     await _transaction.Dispose2Async(CancellationToken.None).ConfigureAwait(false);
                 }
                 catch (IscException ex)
                 {
                     throw FbException.Create(ex);
                 }
             }
         }
         _connection  = null;
         _transaction = null;
         _isCompleted = true;
     }
     await base.DisposeAsync().ConfigureAwait(false);
 }
예제 #8
0
        public async Task Connect(AsyncWrappingCommonArgs async)
        {
            if (Charset.GetCharset(_options.Charset) == null)
            {
                throw FbException.Create("Invalid character set specified");
            }

            try
            {
                _db = await ClientFactory.CreateDatabase(_options, async).ConfigureAwait(false);

                _db.Charset    = Charset.GetCharset(_options.Charset);
                _db.Dialect    = _options.Dialect;
                _db.PacketSize = _options.PacketSize;

                var dpb = BuildDpb(_db, _options);

                if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
                {
                    await _db.AttachWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey, async).ConfigureAwait(false);
                }
                else
                {
                    await _db.Attach(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey, async).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
 private async Task CancelEventsImpl(AsyncWrappingCommonArgs async)
 {
     try
     {
         await _revent.CancelEvents(async).ConfigureAwait(false);
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #10
0
 public void CancelEvents()
 {
     try
     {
         _revent.CancelEvents();
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #11
0
 public async Task CancelEventsAsync(CancellationToken cancellationToken = default)
 {
     try
     {
         await _revent.CancelEventsAsync(cancellationToken).ConfigureAwait(false);
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #12
0
 private static T CheckedGetValue <T>(Func <AsyncWrappingCommonArgs, ValueTask <T> > getter)
 {
     try
     {
         return(getter(new AsyncWrappingCommonArgs(false)).GetAwaiter().GetResult());
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #13
0
 private static T CheckedGetValue2 <T>(Func <T> getter)
 {
     try
     {
         return(getter());
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #14
0
 public async Task RollbackRetainingAsync(CancellationToken cancellationToken = default)
 {
     EnsureCompleted();
     try
     {
         await _transaction.RollbackRetainingAsync(cancellationToken).ConfigureAwait(false);
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #15
0
 public void RollbackRetaining()
 {
     EnsureCompleted();
     try
     {
         _transaction.RollbackRetaining();
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
 private async Task RollbackRetainingImpl(AsyncWrappingCommonArgs async)
 {
     EnsureCompleted();
     try
     {
         await _transaction.RollbackRetaining(async).ConfigureAwait(false);
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #17
0
 public override void Rollback()
 {
     EnsureCompleted();
     try
     {
         _transaction.Rollback();
         CompleteTransaction();
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
 internal async Task RollbackImpl(AsyncWrappingCommonArgs async)
 {
     EnsureCompleted();
     try
     {
         await _transaction.Rollback(async).ConfigureAwait(false);
         await CompleteTransaction(async).ConfigureAwait(false);
     }
     catch (IscException ex)
     {
         throw FbException.Create(ex);
     }
 }
예제 #19
0
        public override async Task RollbackAsync(CancellationToken cancellationToken = default)
#endif
        {
            EnsureCompleted();
            try
            {
                await _transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
                await CompleteTransactionAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #20
0
        public async Task QueueEventsAsync(ICollection <string> events, CancellationToken cancellationToken = default)
        {
            if (_revent == null)
            {
                throw new InvalidOperationException($"{nameof(FbRemoteEvent)} must be opened.");
            }

            try
            {
                await _revent.QueueEventsAsync(events, cancellationToken).ConfigureAwait(false);
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
        private async Task QueueEventsImpl(ICollection <string> events, AsyncWrappingCommonArgs async)
        {
            if (_revent == null)
            {
                throw new InvalidOperationException($"{nameof(FbRemoteEvent)} must be opened.");
            }

            try
            {
                await _revent.QueueEvents(events, async).ConfigureAwait(false);
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #22
0
        public void QueueEvents(ICollection <string> events)
        {
            if (_revent == null)
            {
                throw new InvalidOperationException($"{nameof(FbRemoteEvent)} must be opened.");
            }

            try
            {
                _revent.QueueEvents(events);
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
        private async Task RollbackImpl(string savePointName, AsyncWrappingCommonArgs async)
        {
            EnsureSavePointName(savePointName);
            EnsureCompleted();
            try
            {
                var command = new FbCommand($"ROLLBACK WORK TO SAVEPOINT {savePointName}", _connection, this);
#if NET48 || NETSTANDARD2_0
                using (command)
#else
                await using (command.ConfigureAwait(false))
#endif
                {
                    await async.AsyncSyncCall(command.ExecuteNonQueryAsync, command.ExecuteNonQuery).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
 internal static Exception Create(string message, Exception innerException)
 {
     message ??= innerException?.Message;
     if (innerException is IscException iscException)
     {
         if (iscException.ErrorCode == IscCodes.isc_cancelled)
         {
             return(new OperationCanceledException(message, innerException));
         }
         else
         {
             var result = new FbException(message, innerException);
             result.ProcessIscExceptionErrors(iscException);
             return(result);
         }
     }
     else
     {
         return(new FbException(message, innerException));
     }
 }
예제 #25
0
        public FbTransaction BeginTransaction(FbTransactionOptions options, string transactionName)
        {
            EnsureActiveTransaction();

            try
            {
                _activeTransaction = new FbTransaction(_owningConnection, IsolationLevel.Unspecified);
                _activeTransaction.BeginTransaction(options);

                if (transactionName != null)
                {
                    _activeTransaction.Save(transactionName);
                }
            }
            catch (IscException ex)
            {
                DisposeTransaction();
                throw FbException.Create(ex);
            }

            return(_activeTransaction);
        }
예제 #26
0
        public override void Rollback(string savePointName)
#endif
        {
            EnsureSavePointName(savePointName);
            EnsureCompleted();
            try
            {
                var command = new FbCommand($"ROLLBACK WORK TO SAVEPOINT {savePointName}", _connection, this);
#if NET48 || NETSTANDARD2_0
                using (command)
#else
                using (command)
#endif
                {
                    command.ExecuteNonQuery();
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #27
0
        public override async Task RollbackAsync(string savePointName, CancellationToken cancellationToken = default)
#endif
        {
            EnsureSavePointName(savePointName);
            EnsureCompleted();
            try
            {
                var command = new FbCommand($"ROLLBACK WORK TO SAVEPOINT {savePointName}", _connection, this);
#if NET48 || NETSTANDARD2_0
                using (command)
#else
                await using (command.ConfigureAwait(false))
#endif
                {
                    await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
        internal async Task <int> ExecuteNonQueryImpl(AsyncWrappingCommonArgs async)
        {
            CheckCommand();

            try
            {
                using (async.EnterExplicitCancel(Cancel))
                {
                    await ExecuteCommand(CommandBehavior.Default, false, async).ConfigureAwait(false);

                    if (_statement.StatementType == DbStatementType.StoredProcedure)
                    {
                        await SetOutputParameters(async).ConfigureAwait(false);
                    }

                    await CommitImplicitTransaction(async).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                await RollbackImplicitTransaction(async).ConfigureAwait(false);

                throw FbException.Create(ex);
            }
            catch
            {
                await RollbackImplicitTransaction(async).ConfigureAwait(false);

                throw;
            }

            return(_statement.StatementType switch
            {
                DbStatementType.Insert => RecordsAffected,
                DbStatementType.Update => RecordsAffected,
                DbStatementType.Delete => RecordsAffected,
                _ => - 1,
            });
예제 #29
0
        public async Task <FbTransaction> BeginTransactionAsync(FbTransactionOptions options, string transactionName, CancellationToken cancellationToken = default)
        {
            EnsureActiveTransaction();

            try
            {
                _activeTransaction = new FbTransaction(_owningConnection, IsolationLevel.Unspecified);
                await _activeTransaction.BeginTransactionAsync(options, cancellationToken).ConfigureAwait(false);

                if (transactionName != null)
                {
                    _activeTransaction.Save(transactionName);
                }
            }
            catch (IscException ex)
            {
                await DisposeTransactionAsync(cancellationToken).ConfigureAwait(false);

                throw FbException.Create(ex);
            }

            return(_activeTransaction);
        }
예제 #30
0
        public static async Task CreateDatabaseAsync(string connectionString, int pageSize = 4096, bool forcedWrites = true, bool overwrite = false, CancellationToken cancellationToken = default)
        {
            var options = new ConnectionString(connectionString);

            options.Validate();

            try
            {
                var db = new FbConnectionInternal(options);
                try
                {
                    await db.CreateDatabaseAsync(pageSize, forcedWrites, overwrite, cancellationToken).ConfigureAwait(false);
                }
                finally
                {
                    await db.DisconnectAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
예제 #31
0
		private void WriteToEventLog(FbException e, string action)
		{
			EventLog log = new EventLog();
			log.Source = eventSource;
			log.Log = eventLog;

			StringBuilder sb = new StringBuilder(50);
			sb.AppendFormat("An exception occurred. Please check the Event Log.{0}", Environment.NewLine);
			sb.AppendLine();
			sb.AppendFormat("Action: {0}{1}", action, Environment.NewLine);
			sb.AppendLine();
			sb.AppendFormat("Exception: {0}{1}", e.ToString(), Environment.NewLine);

			log.WriteEntry(sb.ToString());
		}