//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace CommonHelpers {

//**************************************************************************************************
partial class HOuterConnection {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="InvalidOperationException" />
public static async Task<HOuterConnection> Open_A()
{
   var instance = new HOuterConnection();
   var conn = new SqlConnection( instance.ConnectionString );
   await conn.OpenAsync().ConfigureAwait(false);
   instance._Connection_ = conn;
   instance.ValidateDisposable();
   SetCurrent( instance );
   return instance;
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="InvalidOperationException" />
/// <exception cref="SqlException" />
public static Task<HOuterConnection> OpenTransaction_A() =>
   OpenTransaction_A( conn => conn.BeginTransaction() );
/// <exception cref="InvalidOperationException" />
/// <exception cref="SqlException" />
public static Task<HOuterConnection> OpenTransaction_A( IsolationLevel isolationLevel ) =>
   OpenTransaction_A( conn => conn.BeginTransaction( isolationLevel ) );
/// <exception cref="InvalidOperationException" />
/// <exception cref="SqlException" />
public static Task<HOuterConnection> OpenTransaction_A( string transactionName ) =>
   OpenTransaction_A( conn => conn.BeginTransaction( transactionName ) );
/// <exception cref="InvalidOperationException" />
/// <exception cref="SqlException" />
public static Task<HOuterConnection> OpenTransaction_A( IsolationLevel isolationLevel,
                                                        string transactionName ) =>
   OpenTransaction_A( conn => conn.BeginTransaction( isolationLevel, transactionName ) );

/// <exception cref="InvalidOperationException" />
/// <exception cref="SqlException" />
static async Task<HOuterConnection> OpenTransaction_A(
   Func<SqlConnection,SqlTransaction> beginTransaction )
{
   var instance = new HOuterConnection();
   var conn = new SqlConnection( instance.ConnectionString );
   await conn.OpenAsync().ConfigureAwait(false);
   try {
      instance._Transaction_ = beginTransaction( conn );
   } catch {
      HOps.Try(()=>{ conn.Close(); });
      throw;
   }
   instance._Connection_ = conn;
   instance.ValidateDisposable();
   SetCurrent( instance );
   return instance;
}

//--------------------------------------------------------------------------------------------------
#if !LEGACY
/// <exception cref="InvalidOperationException" />
/// <exception cref="Exception" />
public async Task CommitTransaction_A()
{
   var tran = TransactionNN;
   await tran.CommitAsync().ConfigureAwait(false);
   _Transaction_ = null;
}

/// <exception cref="InvalidOperationException" />
/// <exception cref="Exception" />
public async Task RollbackTransaction_A()
{
   VerifyInTransaction();
   await TransactionNN.RollbackAsync();
   _Transaction_ = null;
}
#endif // !LEGACY
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HOuterConnection
//**************************************************************************************************

} // CommonHelpers
