Skip to content

Commit 607f98c

Browse files
authored
ADO.NET batching API (#54467)
Closes #28633
1 parent ad51267 commit 607f98c

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed

src/libraries/System.Data.Common/ref/System.Data.Common.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,56 @@ public void RemoveAt(string sourceTable) { }
19211921
System.Data.ITableMapping System.Data.ITableMappingCollection.Add(string sourceTableName, string dataSetTableName) { throw null; }
19221922
System.Data.ITableMapping System.Data.ITableMappingCollection.GetByDataSetTable(string dataSetTableName) { throw null; }
19231923
}
1924+
public abstract class DbBatch : System.IDisposable, System.IAsyncDisposable
1925+
{
1926+
public System.Data.Common.DbBatchCommandCollection BatchCommands { get { throw null; } }
1927+
protected abstract System.Data.Common.DbBatchCommandCollection DbBatchCommands { get; }
1928+
public abstract int Timeout { get; set; }
1929+
public System.Data.Common.DbConnection? Connection { get; set; }
1930+
protected abstract System.Data.Common.DbConnection? DbConnection { get; set; }
1931+
public System.Data.Common.DbTransaction? Transaction { get; set; }
1932+
protected abstract System.Data.Common.DbTransaction? DbTransaction { get; set; }
1933+
public System.Data.Common.DbDataReader ExecuteReader(System.Data.CommandBehavior behavior = System.Data.CommandBehavior.Default) { throw null; }
1934+
protected abstract System.Data.Common.DbDataReader ExecuteDbDataReader(System.Data.CommandBehavior behavior);
1935+
public System.Threading.Tasks.Task<System.Data.Common.DbDataReader> ExecuteReaderAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
1936+
public System.Threading.Tasks.Task<System.Data.Common.DbDataReader> ExecuteReaderAsync(System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken = default) { throw null; }
1937+
protected abstract System.Threading.Tasks.Task<System.Data.Common.DbDataReader> ExecuteDbDataReaderAsync(System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken);
1938+
public abstract int ExecuteNonQuery();
1939+
public abstract System.Threading.Tasks.Task<int> ExecuteNonQueryAsync(System.Threading.CancellationToken cancellationToken = default);
1940+
public abstract object? ExecuteScalar();
1941+
public abstract System.Threading.Tasks.Task<object?> ExecuteScalarAsync(System.Threading.CancellationToken cancellationToken = default);
1942+
public abstract void Prepare();
1943+
public abstract System.Threading.Tasks.Task PrepareAsync(System.Threading.CancellationToken cancellationToken = default);
1944+
public abstract void Cancel();
1945+
public System.Data.Common.DbBatchCommand CreateBatchCommand() { throw null; }
1946+
protected abstract System.Data.Common.DbBatchCommand CreateDbBatchCommand();
1947+
public virtual void Dispose() { throw null; }
1948+
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
1949+
}
1950+
public abstract class DbBatchCommand
1951+
{
1952+
public abstract string CommandText { get; set; }
1953+
public abstract CommandType CommandType { get; set; }
1954+
public abstract int RecordsAffected { get; }
1955+
public DbParameterCollection Parameters { get { throw null; } }
1956+
protected abstract DbParameterCollection DbParameterCollection { get; }
1957+
}
1958+
public abstract class DbBatchCommandCollection : System.Collections.Generic.IList<DbBatchCommand>
1959+
{
1960+
public abstract System.Collections.Generic.IEnumerator<System.Data.Common.DbBatchCommand> GetEnumerator();
1961+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
1962+
public abstract void Add(System.Data.Common.DbBatchCommand item);
1963+
public abstract void Clear();
1964+
public abstract bool Contains(System.Data.Common.DbBatchCommand item);
1965+
public abstract void CopyTo(System.Data.Common.DbBatchCommand[] array, int arrayIndex);
1966+
public abstract bool Remove(System.Data.Common.DbBatchCommand item);
1967+
public abstract int Count { get; }
1968+
public abstract bool IsReadOnly { get; }
1969+
public abstract int IndexOf(DbBatchCommand item);
1970+
public abstract void Insert(int index, DbBatchCommand item);
1971+
public abstract void RemoveAt(int index);
1972+
public abstract DbBatchCommand this[int index] { get; set; }
1973+
}
19241974
public abstract partial class DbColumn
19251975
{
19261976
protected DbColumn() { }
@@ -2079,6 +2129,9 @@ public virtual event System.Data.StateChangeEventHandler? StateChange { add { }
20792129
public virtual System.Threading.Tasks.Task ChangeDatabaseAsync(string databaseName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
20802130
public abstract void Close();
20812131
public virtual System.Threading.Tasks.Task CloseAsync() { throw null; }
2132+
public virtual bool CanCreateBatch { get { throw null; } }
2133+
public System.Data.Common.DbBatch CreateBatch() { throw null; }
2134+
protected virtual System.Data.Common.DbBatch CreateDbBatch() { throw null; }
20822135
public System.Data.Common.DbCommand CreateCommand() { throw null; }
20832136
protected abstract System.Data.Common.DbCommand CreateDbCommand();
20842137
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
@@ -2371,6 +2424,8 @@ protected DbException(string? message, System.Exception? innerException) { }
23712424
protected DbException(string? message, int errorCode) { }
23722425
public virtual bool IsTransient { get { throw null; } }
23732426
public virtual string? SqlState { get { throw null; } }
2427+
public System.Data.Common.DbBatchCommand? BatchCommand { get { throw null; } }
2428+
protected virtual System.Data.Common.DbBatchCommand? DbBatchCommand { get { throw null; } }
23742429
}
23752430
public static partial class DbMetaDataCollectionNames
23762431
{
@@ -2532,9 +2587,12 @@ public static void RegisterFactory(string providerInvariantName, [System.Diagnos
25322587
public abstract partial class DbProviderFactory
25332588
{
25342589
protected DbProviderFactory() { }
2590+
public virtual bool CanCreateBatch { get { throw null; } }
25352591
public virtual bool CanCreateCommandBuilder { get { throw null; } }
25362592
public virtual bool CanCreateDataAdapter { get { throw null; } }
25372593
public virtual bool CanCreateDataSourceEnumerator { get { throw null; } }
2594+
public virtual System.Data.Common.DbBatch CreateBatch() { throw null; }
2595+
public virtual System.Data.Common.DbBatchCommand CreateBatchCommand() { throw null; }
25382596
public virtual System.Data.Common.DbCommand? CreateCommand() { throw null; }
25392597
public virtual System.Data.Common.DbCommandBuilder? CreateCommandBuilder() { throw null; }
25402598
public virtual System.Data.Common.DbConnection? CreateConnection() { throw null; }

src/libraries/System.Data.Common/src/System.Data.Common.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@
183183
<Compile Include="System\Data\Common\DataTableMappingCollection.cs" />
184184
<Compile Include="System\Data\Common\DateTimeOffsetStorage.cs" />
185185
<Compile Include="System\Data\Common\DateTimeStorage.cs" />
186+
<Compile Include="System\Data\Common\DbBatch.cs" />
187+
<Compile Include="System\Data\Common\DbBatchCommand.cs" />
188+
<Compile Include="System\Data\Common\DbBatchCommandCollection.cs" />
186189
<Compile Include="System\Data\Common\DbColumn.cs" />
187190
<Compile Include="System\Data\Common\DbCommand.cs">
188191
<SubType>Component</SubType>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace System.Data.Common
8+
{
9+
public abstract class DbBatch : IDisposable, IAsyncDisposable
10+
{
11+
public DbBatchCommandCollection BatchCommands => DbBatchCommands;
12+
13+
protected abstract DbBatchCommandCollection DbBatchCommands { get; }
14+
15+
public abstract int Timeout { get; set; }
16+
17+
public DbConnection? Connection
18+
{
19+
get => DbConnection;
20+
set => DbConnection = value;
21+
}
22+
23+
protected abstract DbConnection? DbConnection { get; set; }
24+
25+
public DbTransaction? Transaction
26+
{
27+
get => DbTransaction;
28+
set => DbTransaction = value;
29+
}
30+
31+
protected abstract DbTransaction? DbTransaction { get; set; }
32+
33+
public DbDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default)
34+
=> ExecuteDbDataReader(behavior);
35+
36+
protected abstract DbDataReader ExecuteDbDataReader(CommandBehavior behavior);
37+
38+
public Task<DbDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default)
39+
=> ExecuteDbDataReaderAsync(CommandBehavior.Default, cancellationToken);
40+
41+
public Task<DbDataReader> ExecuteReaderAsync(
42+
CommandBehavior behavior,
43+
CancellationToken cancellationToken = default)
44+
=> ExecuteDbDataReaderAsync(behavior, cancellationToken);
45+
46+
protected abstract Task<DbDataReader> ExecuteDbDataReaderAsync(
47+
CommandBehavior behavior,
48+
CancellationToken cancellationToken);
49+
50+
public abstract int ExecuteNonQuery();
51+
52+
public abstract Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken = default);
53+
54+
public abstract object? ExecuteScalar();
55+
56+
public abstract Task<object?> ExecuteScalarAsync(CancellationToken cancellationToken = default);
57+
58+
public abstract void Prepare();
59+
60+
public abstract Task PrepareAsync(CancellationToken cancellationToken = default);
61+
62+
public abstract void Cancel();
63+
64+
public DbBatchCommand CreateBatchCommand() => CreateDbBatchCommand();
65+
66+
protected abstract DbBatchCommand CreateDbBatchCommand();
67+
68+
public virtual void Dispose() {}
69+
70+
public virtual ValueTask DisposeAsync()
71+
{
72+
Dispose();
73+
return default;
74+
}
75+
}
76+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.ComponentModel;
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
namespace System.Data.Common
8+
{
9+
public abstract class DbBatchCommand
10+
{
11+
public abstract string CommandText { get; set; }
12+
13+
public abstract CommandType CommandType { get; set; }
14+
15+
public abstract int RecordsAffected { get; }
16+
17+
public DbParameterCollection Parameters => DbParameterCollection;
18+
19+
protected abstract DbParameterCollection DbParameterCollection { get; }
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
7+
namespace System.Data.Common
8+
{
9+
public abstract class DbBatchCommandCollection : IList<DbBatchCommand>
10+
{
11+
public abstract IEnumerator<DbBatchCommand> GetEnumerator();
12+
13+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
14+
15+
public abstract void Add(DbBatchCommand item);
16+
17+
public abstract void Clear();
18+
19+
public abstract bool Contains(DbBatchCommand item);
20+
21+
public abstract void CopyTo(DbBatchCommand[] array, int arrayIndex);
22+
23+
public abstract bool Remove(DbBatchCommand item);
24+
25+
public abstract int Count { get; }
26+
27+
public abstract bool IsReadOnly { get; }
28+
29+
public abstract int IndexOf(DbBatchCommand item);
30+
31+
public abstract void Insert(int index, DbBatchCommand item);
32+
33+
public abstract void RemoveAt(int index);
34+
35+
public abstract DbBatchCommand this[int index] { get; set; }
36+
}
37+
}

src/libraries/System.Data.Common/src/System/Data/Common/DbConnection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ public virtual Task ChangeDatabaseAsync(string databaseName, CancellationToken c
128128
}
129129
}
130130

131+
public virtual bool CanCreateBatch => false;
132+
133+
public DbBatch CreateBatch() => CreateDbBatch();
134+
135+
protected virtual DbBatch CreateDbBatch() => throw new NotSupportedException();
136+
131137
public DbCommand CreateCommand() => CreateDbCommand();
132138

133139
IDbCommand IDbConnection.CreateCommand() => CreateDbCommand();

src/libraries/System.Data.Common/src/System/Data/Common/DbException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ protected DbException(System.Runtime.Serialization.SerializationInfo info, Syste
4141
/// A standard SQL 5-character return code, or <see langword="null" />.
4242
/// </returns>
4343
public virtual string? SqlState => null;
44+
45+
public DbBatchCommand? BatchCommand => DbBatchCommand;
46+
47+
protected virtual DbBatchCommand? DbBatchCommand => null;
4448
}
4549
}

src/libraries/System.Data.Common/src/System/Data/Common/DbProviderFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public abstract partial class DbProviderFactory
1313

1414
protected DbProviderFactory() { }
1515

16+
public virtual bool CanCreateBatch => false;
17+
1618
public virtual bool CanCreateDataSourceEnumerator => false;
1719

1820
public virtual bool CanCreateDataAdapter
@@ -47,6 +49,10 @@ public virtual bool CanCreateCommandBuilder
4749
}
4850
}
4951

52+
public virtual DbBatch CreateBatch() => throw new NotSupportedException();
53+
54+
public virtual DbBatchCommand CreateBatchCommand() => throw new NotSupportedException();
55+
5056
public virtual DbCommand? CreateCommand() => null;
5157

5258
public virtual DbCommandBuilder? CreateCommandBuilder() => null;

0 commit comments

Comments
 (0)