Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add async methods in System.Data.Common, implement IAsyncDisposable
Browse files Browse the repository at this point in the history
Closes #35012
  • Loading branch information
roji committed May 30, 2019
1 parent 75329f4 commit 22dd87e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/System.Data.Common/ref/System.Data.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,9 +1848,11 @@ protected DbCommand() { }
public System.Threading.Tasks.Task<object> ExecuteScalarAsync() { throw null; }
public virtual System.Threading.Tasks.Task<object> ExecuteScalarAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public abstract void Prepare();
public virtual System.Threading.Tasks.Task PrepareAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
System.Data.IDbDataParameter System.Data.IDbCommand.CreateParameter() { throw null; }
System.Data.IDataReader System.Data.IDbCommand.ExecuteReader() { throw null; }
System.Data.IDataReader System.Data.IDbCommand.ExecuteReader(System.Data.CommandBehavior behavior) { throw null; }
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
}
public abstract partial class DbCommandBuilder : System.ComponentModel.Component
{
Expand Down Expand Up @@ -1912,7 +1914,9 @@ public virtual event System.Data.StateChangeEventHandler StateChange { add { } r
public System.Data.Common.DbTransaction BeginTransaction() { throw null; }
public System.Data.Common.DbTransaction BeginTransaction(System.Data.IsolationLevel isolationLevel) { throw null; }
public abstract void ChangeDatabase(string databaseName);
public virtual System.Threading.Tasks.Task ChangeDatabaseAsync(string databaseName, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public abstract void Close();
public virtual System.Threading.Tasks.Task CloseAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
public System.Data.Common.DbCommand CreateCommand() { throw null; }
protected abstract System.Data.Common.DbCommand CreateDbCommand();
public virtual void EnlistTransaction(System.Transactions.Transaction transaction) { }
Expand All @@ -1925,7 +1929,11 @@ protected virtual void OnStateChange(System.Data.StateChangeEventArgs stateChang
public virtual System.Threading.Tasks.Task OpenAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
System.Data.IDbTransaction System.Data.IDbConnection.BeginTransaction() { throw null; }
System.Data.IDbTransaction System.Data.IDbConnection.BeginTransaction(System.Data.IsolationLevel isolationLevel) { throw null; }
protected virtual System.Threading.Tasks.ValueTask<DbTransaction> BeginDbTransactionAsync(IsolationLevel isolationLevel, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.ValueTask<DbTransaction> BeginTransactionAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
public System.Threading.Tasks.ValueTask<DbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, System.Threading.CancellationToken cancellationToken = default) { throw null; }
System.Data.IDbCommand System.Data.IDbConnection.CreateCommand() { throw null; }
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
}
public partial class DbConnectionStringBuilder : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.ComponentModel.ICustomTypeDescriptor
{
Expand Down Expand Up @@ -2054,8 +2062,10 @@ protected DbDataReader() { }
public abstract int RecordsAffected { get; }
public virtual int VisibleFieldCount { get { throw null; } }
public virtual void Close() { }
public virtual System.Threading.Tasks.Task CloseAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public void Dispose() { }
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
protected virtual void Dispose(bool disposing) { }
public abstract bool GetBoolean(int ordinal);
public abstract byte GetByte(int ordinal);
Expand Down Expand Up @@ -2349,9 +2359,12 @@ protected DbTransaction() { }
public abstract System.Data.IsolationLevel IsolationLevel { get; }
System.Data.IDbConnection System.Data.IDbTransaction.Connection { get { throw null; } }
public abstract void Commit();
public virtual System.Threading.Tasks.Task CommitAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
public void Dispose() { }
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
protected virtual void Dispose(bool disposing) { }
public abstract void Rollback();
public virtual System.Threading.Tasks.Task RollbackAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
}
public enum GroupByBehavior
{
Expand Down
24 changes: 24 additions & 0 deletions src/System.Data.Common/src/System/Data/Common/DbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,29 @@ public virtual Task<object> ExecuteScalarAsync(CancellationToken cancellationTok
public abstract object ExecuteScalar();

public abstract void Prepare();

public virtual Task PrepareAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Prepare();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}

public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}
}
}
65 changes: 65 additions & 0 deletions src/System.Data.Common/src/System/Data/Common/DbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,75 @@ IDbTransaction IDbConnection.BeginTransaction() =>
IDbTransaction IDbConnection.BeginTransaction(IsolationLevel isolationLevel) =>
BeginDbTransaction(isolationLevel);

protected virtual ValueTask<DbTransaction> BeginDbTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<DbTransaction>(Task.FromCanceled<DbTransaction>(cancellationToken));
}

try
{
return new ValueTask<DbTransaction>(BeginDbTransaction(isolationLevel));
}
catch (Exception e)
{
return new ValueTask<DbTransaction>(Task.FromException<DbTransaction>(e));
}
}

public ValueTask<DbTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
=> BeginDbTransactionAsync(IsolationLevel.Unspecified, cancellationToken);

public ValueTask<DbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default)
=> BeginDbTransactionAsync(isolationLevel, cancellationToken);

public abstract void Close();

public virtual Task CloseAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Close();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}

public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}

public abstract void ChangeDatabase(string databaseName);

public virtual Task ChangeDatabaseAsync(string databaseName, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
ChangeDatabase(databaseName);
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}

public DbCommand CreateCommand() => CreateDbCommand();

IDbCommand IDbConnection.CreateCommand() => CreateDbCommand();
Expand Down
24 changes: 24 additions & 0 deletions src/System.Data.Common/src/System/Data/Common/DbDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ protected DbDataReader() : base() { }

public virtual void Close() { }

public virtual Task CloseAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Close();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}

[EditorBrowsable(EditorBrowsableState.Never)]
public void Dispose() => Dispose(true);

Expand All @@ -43,6 +61,12 @@ protected virtual void Dispose(bool disposing)
}
}

public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}

public abstract string GetDataTypeName(int ordinal);

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
45 changes: 45 additions & 0 deletions src/System.Data.Common/src/System/Data/Common/DbTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;

namespace System.Data.Common
{
public abstract class DbTransaction : MarshalByRefObject, IDbTransaction
Expand All @@ -18,10 +21,52 @@ protected DbTransaction() : base() { }

public abstract void Commit();

public virtual Task CommitAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Commit();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}

public void Dispose() => Dispose(true);

protected virtual void Dispose(bool disposing) { }

public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}

public abstract void Rollback();

public virtual Task RollbackAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Rollback();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}
}
}

0 comments on commit 22dd87e

Please sign in to comment.