using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Npgsql.Internal;
namespace Npgsql;
///
public class NpgsqlBatch : DbBatch
{
internal const int DefaultBatchCommandsSize = 5;
private protected NpgsqlCommand Command { get; }
///
protected override DbBatchCommandCollection DbBatchCommands => BatchCommands;
///
public new NpgsqlBatchCommandCollection BatchCommands { get; }
///
public override int Timeout
{
get => Command.CommandTimeout;
set => Command.CommandTimeout = value;
}
///
public new NpgsqlConnection? Connection
{
get => Command.Connection;
set => Command.Connection = value;
}
///
protected override DbConnection? DbConnection
{
get => Connection;
set => Connection = (NpgsqlConnection?)value;
}
///
public new NpgsqlTransaction? Transaction
{
get => Command.Transaction;
set => Command.Transaction = value;
}
///
protected override DbTransaction? DbTransaction
{
get => Transaction;
set => Transaction = (NpgsqlTransaction?)value;
}
///
/// Controls whether to place error barriers between all batch commands within this batch. Default to .
///
///
///
/// By default, any exception in a command causes later commands in the batch to be skipped, and earlier commands to be rolled back.
/// Enabling error barriers ensures that errors do not affect other commands in the batch.
///
///
/// Note that if the batch is executed within an explicit transaction, the first error places the transaction in a failed state,
/// causing all later commands to fail in any case. As a result, this option is useful mainly when there is no explicit transaction.
///
///
/// At the PostgreSQL wire protocol level, this corresponds to inserting a Sync message between each command, rather than grouping
/// all the batch's commands behind a single terminating Sync.
///
///
/// To control error barriers on a command-by-command basis, see .
///
///
public bool EnableErrorBarriers
{
get => Command.EnableErrorBarriers;
set => Command.EnableErrorBarriers = value;
}
///
/// Marks all of the batch's result columns as either known or unknown.
/// Unknown results column are requested them from PostgreSQL in text format, and Npgsql makes no
/// attempt to parse them. They will be accessible as strings only.
///
internal bool AllResultTypesAreUnknown
{
get => Command.AllResultTypesAreUnknown;
set => Command.AllResultTypesAreUnknown = value;
}
///
/// Initializes a new .
///
/// A that represents the connection to a PostgreSQL server.
/// The in which the executes.
public NpgsqlBatch(NpgsqlConnection? connection = null, NpgsqlTransaction? transaction = null)
{
GC.SuppressFinalize(this);
Command = new(this, DefaultBatchCommandsSize);
BatchCommands = new NpgsqlBatchCommandCollection(Command.InternalBatchCommands);
Connection = connection;
Transaction = transaction;
}
internal NpgsqlBatch(NpgsqlConnector connector)
{
GC.SuppressFinalize(this);
Command = new(this, connector, DefaultBatchCommandsSize);
BatchCommands = new NpgsqlBatchCommandCollection(Command.InternalBatchCommands);
}
private protected NpgsqlBatch(Func commandFactory, NpgsqlConnection connection)
{
GC.SuppressFinalize(this);
Command = commandFactory(connection, this);
BatchCommands = new NpgsqlBatchCommandCollection(Command.InternalBatchCommands);
}
///
protected override DbBatchCommand CreateDbBatchCommand() => CreateBatchCommand();
///
public new NpgsqlBatchCommand CreateBatchCommand()
=> new NpgsqlBatchCommand();
///
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
=> ExecuteReader(behavior);
///
public new NpgsqlDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default)
=> Command.ExecuteReader(behavior);
///
protected override async Task ExecuteDbDataReaderAsync(
CommandBehavior behavior,
CancellationToken cancellationToken)
=> await ExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
///
public new Task ExecuteReaderAsync(CancellationToken cancellationToken = default)
=> Command.ExecuteReaderAsync(cancellationToken);
///
public new Task ExecuteReaderAsync(
CommandBehavior behavior,
CancellationToken cancellationToken = default)
=> Command.ExecuteReaderAsync(behavior, cancellationToken);
///
public override int ExecuteNonQuery()
=> Command.ExecuteNonQuery();
///
public override Task ExecuteNonQueryAsync(CancellationToken cancellationToken = default)
=> Command.ExecuteNonQueryAsync(cancellationToken);
///
public override object? ExecuteScalar()
=> Command.ExecuteScalar();
///
public override Task