Exemple #1
0
 public async Task CloseAsync()
 {
     if (connection.State != System.Data.ConnectionState.Closed)
     {
         await connection.CloseAsync();
     }
 }
    public override async Task CloseAsync()
#endif
    {
        if (!IsClosed)
        {
            _isClosed = true;
            if (_command != null && !_command.IsDisposed)
            {
                if (_command.CommandType == CommandType.StoredProcedure)
                {
                    await _command.SetOutputParametersAsync(CancellationToken.None).ConfigureAwait(false);
                }
                if (_command.HasImplicitTransaction)
                {
                    await _command.CommitImplicitTransactionAsync(CancellationToken.None).ConfigureAwait(false);
                }
                _command.ActiveReader = null;
            }
            if (_connection != null && IsCommandBehavior(CommandBehavior.CloseConnection))
            {
                await _connection.CloseAsync().ConfigureAwait(false);
            }
            _position    = StartPosition;
            _command     = null;
            _connection  = null;
            _row         = null;
            _schemaTable = null;
            _fields      = null;
        }
    }
Exemple #3
0
        /// <summary>
        /// Starts the ordered execution of the SQL statements that are in <see cref="SqlStatements"/> collection.
        /// </summary>
        /// <param name="autoCommit">Specifies if the transaction should be committed after a DDL command execution</param>
        public async Task ExecuteAsync(bool autoCommit = true, CancellationToken cancellationToken = default)
        {
            if ((_statements?.Count ?? 0) == 0)
            {
                throw new InvalidOperationException("There are no commands for execution.");
            }

            _shouldClose = false;

            foreach (var statement in Statements)
            {
                if (!(statement.StatementType == SqlStatementType.Connect ||
                      statement.StatementType == SqlStatementType.CreateDatabase ||
                      statement.StatementType == SqlStatementType.Disconnect ||
                      statement.StatementType == SqlStatementType.DropDatabase ||
                      statement.StatementType == SqlStatementType.SetAutoDDL ||
                      statement.StatementType == SqlStatementType.SetDatabase ||
                      statement.StatementType == SqlStatementType.SetNames ||
                      statement.StatementType == SqlStatementType.SetSQLDialect))
                {
                    await ProvideCommandAsync(cancellationToken).ConfigureAwait(false);

                    _sqlCommand.CommandText = statement.Text;
                    if (_sqlTransaction == null && !(statement.StatementType == SqlStatementType.Commit || statement.StatementType == SqlStatementType.Rollback))
                    {
                        _sqlTransaction = await _sqlConnection.BeginTransactionAsync(FbTransaction.DefaultIsolationLevel, null, cancellationToken).ConfigureAwait(false);
                    }
                    _sqlCommand.Transaction = _sqlTransaction;
                }

                try
                {
                    switch (statement.StatementType)
                    {
                    case SqlStatementType.AlterCharacterSet:
                    case SqlStatementType.AlterDatabase:
                    case SqlStatementType.AlterDomain:
                    case SqlStatementType.AlterException:
                    case SqlStatementType.AlterFunction:
                    case SqlStatementType.AlterIndex:
                    case SqlStatementType.AlterPackage:
                    case SqlStatementType.AlterProcedure:
                    case SqlStatementType.AlterRole:
                    case SqlStatementType.AlterSequence:
                    case SqlStatementType.AlterTable:
                    case SqlStatementType.AlterTrigger:
                    case SqlStatementType.AlterView:
                    case SqlStatementType.CommentOn:
                    case SqlStatementType.CreateCollation:
                    case SqlStatementType.CreateDomain:
                    case SqlStatementType.CreateException:
                    case SqlStatementType.CreateFunction:
                    case SqlStatementType.CreateGenerator:
                    case SqlStatementType.CreateIndex:
                    case SqlStatementType.CreatePackage:
                    case SqlStatementType.CreatePackageBody:
                    case SqlStatementType.CreateProcedure:
                    case SqlStatementType.CreateRole:
                    case SqlStatementType.CreateSequence:
                    case SqlStatementType.CreateShadow:
                    case SqlStatementType.CreateTable:
                    case SqlStatementType.CreateTrigger:
                    case SqlStatementType.CreateView:
                    case SqlStatementType.DeclareCursor:
                    case SqlStatementType.DeclareExternalFunction:
                    case SqlStatementType.DeclareFilter:
                    case SqlStatementType.DeclareStatement:
                    case SqlStatementType.DeclareTable:
                    case SqlStatementType.Delete:
                    case SqlStatementType.DropCollation:
                    case SqlStatementType.DropDomain:
                    case SqlStatementType.DropException:
                    case SqlStatementType.DropExternalFunction:
                    case SqlStatementType.DropFunction:
                    case SqlStatementType.DropFilter:
                    case SqlStatementType.DropGenerator:
                    case SqlStatementType.DropIndex:
                    case SqlStatementType.DropPackage:
                    case SqlStatementType.DropPackageBody:
                    case SqlStatementType.DropProcedure:
                    case SqlStatementType.DropSequence:
                    case SqlStatementType.DropRole:
                    case SqlStatementType.DropShadow:
                    case SqlStatementType.DropTable:
                    case SqlStatementType.DropTrigger:
                    case SqlStatementType.DropView:
                    case SqlStatementType.EventInit:
                    case SqlStatementType.EventWait:
                    case SqlStatementType.Execute:
                    case SqlStatementType.ExecuteImmediate:
                    case SqlStatementType.ExecuteProcedure:
                    case SqlStatementType.Grant:
                    case SqlStatementType.Insert:
                    case SqlStatementType.InsertCursor:
                    case SqlStatementType.Merge:
                    case SqlStatementType.Open:
                    case SqlStatementType.Prepare:
                    case SqlStatementType.Revoke:
                    case SqlStatementType.RecreateFunction:
                    case SqlStatementType.RecreatePackage:
                    case SqlStatementType.RecreatePackageBody:
                    case SqlStatementType.RecreateProcedure:
                    case SqlStatementType.RecreateTable:
                    case SqlStatementType.RecreateTrigger:
                    case SqlStatementType.RecreateView:
                    case SqlStatementType.SetGenerator:
                    case SqlStatementType.Update:
                    case SqlStatementType.Whenever:
                        OnCommandExecuting(_sqlCommand, statement.StatementType);

                        var rowsAffected = await ExecuteCommandAsync(autoCommit, cancellationToken).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, rowsAffected);
                        break;

                    case SqlStatementType.ExecuteBlock:
                    case SqlStatementType.Select:
                        (await ProvideCommandAsync(cancellationToken).ConfigureAwait(false)).CommandText = statement.Text;

                        OnCommandExecuting(_sqlCommand, statement.StatementType);

                        var dataReader = await _sqlCommand.ExecuteReaderAsync(CommandBehavior.Default, cancellationToken).ConfigureAwait(false);

                        try
                        {
                            _requiresNewConnection = false;

                            OnCommandExecuted(dataReader, statement.Text, statement.StatementType, -1);
                        }
                        finally
                        {
#if NET48 || NETSTANDARD2_0
                            dataReader.Dispose();
#else
                            await dataReader.DisposeAsync().ConfigureAwait(false);
#endif
                        }
                        break;

                    case SqlStatementType.Commit:
                        OnCommandExecuting(null, statement.StatementType);

                        await CommitTransactionAsync(cancellationToken).ConfigureAwait(false);

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Rollback:
                        OnCommandExecuting(null, statement.StatementType);

                        await RollbackTransactionAsync(cancellationToken).ConfigureAwait(false);

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.CreateDatabase:
                        OnCommandExecuting(null, statement.StatementType);

                        await CreateDatabaseAsync(statement.CleanText, cancellationToken).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.DropDatabase:
                        OnCommandExecuting(null, statement.StatementType);

                        await FbConnection.DropDatabaseAsync(_connectionString.ToString(), cancellationToken).ConfigureAwait(false);

                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Connect:
                        OnCommandExecuting(null, statement.StatementType);

                        await ConnectToDatabaseAsync(statement.CleanText, cancellationToken).ConfigureAwait(false);

                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Disconnect:
                        OnCommandExecuting(null, statement.StatementType);

                        await _sqlConnection.CloseAsync().ConfigureAwait(false);

                        FbConnection.ClearPool(_sqlConnection);
                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetAutoDDL:
                        OnCommandExecuting(null, statement.StatementType);

                        SetAutoDdl(statement.CleanText, ref autoCommit);
                        _requiresNewConnection = false;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetNames:
                        OnCommandExecuting(null, statement.StatementType);

                        SetNames(statement.CleanText);
                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.SetSQLDialect:
                        OnCommandExecuting(null, statement.StatementType);

                        SetSqlDialect(statement.CleanText);
                        _requiresNewConnection = true;

                        OnCommandExecuted(null, statement.Text, statement.StatementType, -1);
                        break;

                    case SqlStatementType.Fetch:
                    case SqlStatementType.Describe:
                        break;

                    case SqlStatementType.SetDatabase:
                    case SqlStatementType.SetStatistics:
                    case SqlStatementType.SetTransaction:
                    case SqlStatementType.ShowSQLDialect:
                        throw new NotImplementedException();
                    }
                }
                catch (Exception ex)
                {
                    await DisposeCommandAsync(cancellationToken).ConfigureAwait(false);
                    await RollbackTransactionAsync(cancellationToken).ConfigureAwait(false);
                    await CloseConnectionAsync(cancellationToken).ConfigureAwait(false);

                    var message = string.Format("An exception was thrown when executing command: {1}.{0}Batch execution aborted.{0}The returned message was: {2}.",
                                                Environment.NewLine,
                                                statement.Text,
                                                ex.Message);
                    throw FbException.Create(message, ex);
                }
            }

            await DisposeCommandAsync(cancellationToken).ConfigureAwait(false);
            await CommitTransactionAsync(cancellationToken).ConfigureAwait(false);
            await CloseConnectionAsync(cancellationToken).ConfigureAwait(false);
        }