public RelationalDataReader ExecuteReader(RelationalCommandParameterObject parameterObject)
            {
                var connection  = parameterObject.Connection;
                var errorNumber = PreExecution(connection);

                var result = _realRelationalCommand.ExecuteReader(parameterObject);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
Example #2
0
            public Task <int> ExecuteNonQueryAsync(
                IRelationalConnection connection, IReadOnlyDictionary <string, object> parameterValues,
                CancellationToken cancellationToken = new CancellationToken())
            {
                var errorNumber = PreExecution(connection);

                var result = _realRelationalCommand.ExecuteNonQueryAsync(connection, parameterValues, cancellationToken);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
Example #3
0
            public RelationalDataReader ExecuteReader(
                IRelationalConnection connection, IReadOnlyDictionary <string, object> parameterValues)
            {
                var errorNumber = PreExecution(connection);

                var result = _realRelationalCommand.ExecuteReader(connection, parameterValues);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
Example #4
0
            public async Task <object> ExecuteScalarAsync(
                RelationalCommandParameterObject parameterObject,
                CancellationToken cancellationToken = new CancellationToken())
            {
                var connection  = parameterObject.Connection;
                var errorNumber = PreExecution(connection);

                var result = await _realRelationalCommand.ExecuteScalarAsync(parameterObject, cancellationToken);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
Example #5
0
            public async Task <RelationalDataReader> ExecuteReaderAsync(
                IRelationalConnection connection, IReadOnlyDictionary <string, object> parameterValues,
                CancellationToken cancellationToken = new CancellationToken())
            {
                var errorNumber = PreExecution(connection);

                var result = await _realRelationalCommand.ExecuteReaderAsync(connection, parameterValues, cancellationToken);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    result.Dispose(); // Normally, in non-test case, reader is disposed by using in caller code
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
            public async Task <object> ExecuteScalarAsync(
                IRelationalConnection connection, IReadOnlyDictionary <string, object> parameterValues,
                IDiagnosticsLogger <DbLoggerCategory.Database.Command> logger,
                CancellationToken cancellationToken = new CancellationToken())
            {
                var errorNumber = PreExecution(connection);

                var result = await _realRelationalCommand.ExecuteScalarAsync(connection, parameterValues, logger, cancellationToken);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
            public object ExecuteScalar(
                IRelationalConnection connection,
                IReadOnlyDictionary <string, object> parameterValues,
                IDiagnosticsLogger <DbLoggerCategory.Database.Command> logger)
            {
                var errorNumber = PreExecution(connection);

                var result = _realRelationalCommand.ExecuteScalar(connection, parameterValues, logger);

                if (errorNumber.HasValue)
                {
                    connection.DbConnection.Close();
                    throw SqlExceptionFactory.CreateSqlException(errorNumber.Value);
                }

                return(result);
            }
Example #8
0
        private void PreOpen()
        {
            if (DbConnection.State == ConnectionState.Open)
            {
                return;
            }

            OpenCount++;
            if (OpenFailures.Count <= 0)
            {
                return;
            }

            var fail = OpenFailures.Dequeue();

            if (fail.HasValue)
            {
                throw SqlExceptionFactory.CreateSqlException(ErrorNumber);
            }
        }
            private int?PreExecution(IRelationalConnection connection)
            {
                int?errorNumber    = null;
                var testConnection = (TestSqlServerConnection)connection;

                testConnection.ExecutionCount++;
                if (testConnection.ExecutionFailures.Count > 0)
                {
                    var fail = testConnection.ExecutionFailures.Dequeue();
                    if (fail.HasValue)
                    {
                        if (fail.Value)
                        {
                            testConnection.DbConnection.Close();
                            throw SqlExceptionFactory.CreateSqlException(testConnection.ErrorNumber);
                        }
                        errorNumber = testConnection.ErrorNumber;
                    }
                }
                return(errorNumber);
            }
Example #10
0
        public override void Commit()
        {
            if (_testConnection.CommitFailures.Count > 0)
            {
                var fail = _testConnection.CommitFailures.Dequeue();
                if (fail.HasValue)
                {
                    if (fail.Value)
                    {
                        this.GetDbTransaction().Rollback();
                    }
                    else
                    {
                        this.GetDbTransaction().Commit();
                    }
                    _testConnection.DbConnection.Close();
                    throw SqlExceptionFactory.CreateSqlException(_testConnection.ErrorNumber, _testConnection.ConnectionId);
                }
            }

            base.Commit();
        }
Example #11
0
        public override async Task CommitAsync(CancellationToken cancellationToken = default)
        {
            if (_testConnection.CommitFailures.Count > 0)
            {
                var fail = _testConnection.CommitFailures.Dequeue();
                if (fail.HasValue)
                {
                    if (fail.Value)
                    {
                        await this.GetDbTransaction().RollbackAsync(cancellationToken);
                    }
                    else
                    {
                        await this.GetDbTransaction().CommitAsync(cancellationToken);
                    }

                    await _testConnection.DbConnection.CloseAsync();

                    throw SqlExceptionFactory.CreateSqlException(_testConnection.ErrorNumber, _testConnection.ConnectionId);
                }
            }

            await base.CommitAsync(cancellationToken);
        }