|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Data; |
| 3 | +using System.Data.Common; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Npgsql |
| 8 | +{ |
| 9 | + /// <inheritdoc /> |
| 10 | + public class NpgsqlBatch : DbBatch |
| 11 | + { |
| 12 | + readonly NpgsqlCommand _command; |
| 13 | + |
| 14 | + /// <inheritdoc /> |
| 15 | + protected override DbBatchCommandCollection DbBatchCommands => BatchCommands; |
| 16 | + |
| 17 | + /// <inheritdoc cref="DbBatch.BatchCommands"/> |
| 18 | + public new NpgsqlBatchCommandCollection BatchCommands { get; } |
| 19 | + |
| 20 | + /// <inheritdoc /> |
| 21 | + public override int Timeout |
| 22 | + { |
| 23 | + get => _command.CommandTimeout; |
| 24 | + set => _command.CommandTimeout = value; |
| 25 | + } |
| 26 | + |
| 27 | + /// <inheritdoc cref="DbBatch.Connection"/> |
| 28 | + public new NpgsqlConnection? Connection |
| 29 | + { |
| 30 | + get => _command.Connection; |
| 31 | + set => _command.Connection = value; |
| 32 | + } |
| 33 | + |
| 34 | + /// <inheritdoc /> |
| 35 | + protected override DbConnection? DbConnection |
| 36 | + { |
| 37 | + get => Connection; |
| 38 | + set => Connection = (NpgsqlConnection?)value; |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc cref="DbBatch.Transaction"/> |
| 42 | + public new NpgsqlTransaction? Transaction |
| 43 | + { |
| 44 | + get => _command.Transaction; |
| 45 | + set => _command.Transaction = value; |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + protected override DbTransaction? DbTransaction |
| 50 | + { |
| 51 | + get => Transaction; |
| 52 | + set => Transaction = (NpgsqlTransaction?)value; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new <see cref="NpgsqlBatch"/>. |
| 57 | + /// </summary> |
| 58 | + /// <param name="connection">A <see cref="NpgsqlConnection"/> that represents the connection to a PostgreSQL server.</param> |
| 59 | + /// <param name="transaction">The <see cref="NpgsqlTransaction"/> in which the <see cref="NpgsqlCommand"/> executes.</param> |
| 60 | + public NpgsqlBatch(NpgsqlConnection? connection = null, NpgsqlTransaction? transaction = null) |
| 61 | + { |
| 62 | + var batchCommands = new List<NpgsqlBatchCommand>(5); |
| 63 | + _command = new(batchCommands); |
| 64 | + BatchCommands = new NpgsqlBatchCommandCollection(batchCommands); |
| 65 | + |
| 66 | + Connection = connection; |
| 67 | + Transaction = transaction; |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + protected override DbBatchCommand CreateDbBatchCommand() |
| 72 | + => new NpgsqlBatchCommand(); |
| 73 | + |
| 74 | + /// <inheritdoc /> |
| 75 | + protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) |
| 76 | + => ExecuteReader(behavior); |
| 77 | + |
| 78 | + /// <inheritdoc cref="DbBatch.ExecuteReader"/> |
| 79 | + public new NpgsqlDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default) |
| 80 | + => _command.ExecuteReader(); |
| 81 | + |
| 82 | + /// <inheritdoc /> |
| 83 | + protected override async Task<DbDataReader> ExecuteDbDataReaderAsync( |
| 84 | + CommandBehavior behavior, |
| 85 | + CancellationToken cancellationToken) |
| 86 | + => await ExecuteReaderAsync(cancellationToken); |
| 87 | + |
| 88 | + /// <inheritdoc cref="DbBatch.ExecuteReaderAsync(CancellationToken)"/> |
| 89 | + public new Task<NpgsqlDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default) |
| 90 | + => _command.ExecuteReaderAsync(cancellationToken); |
| 91 | + |
| 92 | + /// <inheritdoc cref="DbBatch.ExecuteReaderAsync(CommandBehavior,CancellationToken)"/> |
| 93 | + public new Task<NpgsqlDataReader> ExecuteReaderAsync( |
| 94 | + CommandBehavior behavior, |
| 95 | + CancellationToken cancellationToken = default) |
| 96 | + => _command.ExecuteReaderAsync(behavior, cancellationToken); |
| 97 | + |
| 98 | + /// <inheritdoc /> |
| 99 | + public override int ExecuteNonQuery() |
| 100 | + => _command.ExecuteNonQuery(); |
| 101 | + |
| 102 | + /// <inheritdoc /> |
| 103 | + public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken = default) |
| 104 | + => _command.ExecuteNonQueryAsync(cancellationToken); |
| 105 | + |
| 106 | + /// <inheritdoc /> |
| 107 | + public override object? ExecuteScalar() |
| 108 | + => _command.ExecuteScalar(); |
| 109 | + |
| 110 | + /// <inheritdoc /> |
| 111 | + public override Task<object?> ExecuteScalarAsync(CancellationToken cancellationToken = default) |
| 112 | + => _command.ExecuteScalarAsync(cancellationToken); |
| 113 | + |
| 114 | + /// <inheritdoc /> |
| 115 | + public override void Prepare() |
| 116 | + => _command.Prepare(); |
| 117 | + |
| 118 | + /// <inheritdoc /> |
| 119 | + public override Task PrepareAsync(CancellationToken cancellationToken = default) |
| 120 | + => _command.PrepareAsync(cancellationToken); |
| 121 | + |
| 122 | + /// <inheritdoc /> |
| 123 | + public override void Cancel() => _command.Cancel(); |
| 124 | + } |
| 125 | +} |
0 commit comments