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

namespace CommonHelpers {

//**************************************************************************************************
[HDbOrmType]
public static class HSPCommandDispose {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
public static Task<SqlDataReader> ExecuteReader_A( SqlCommand command ) =>
   ExecuteReaderVerify_A( null, command );
/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
public static Task<SqlDataReader> ExecuteReader_A( SqlCommand command,
                                                   [HOptional] CommandBehavior behaviour ) =>
   ExecuteReaderVerify_A( null, command, behaviour );

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
public static Task<int> ExecuteNonQuery_A(SqlCommand command) =>
   ExecuteNonQueryVerify_A( null, command );

//==================================================================================================

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
public static Task<SqlDataReader> ExecuteReaderVerify_A(
   [HOptional] HSPResultParameters? resultParameters,
   SqlCommand command ) =>
   ExecuteReaderVerify_A( resultParameters, command, CommandBehavior.Default );

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
public static async Task<SqlDataReader> ExecuteReaderVerify_A(
   [HOptional] HSPResultParameters? resultParameters,
   SqlCommand command,
   [HOptional] CommandBehavior behaviour )
{
   using ( HArgChecking.VerifyNotNull( command ) )
   {
      var rdr = await command.ExecuteReaderAsync( behaviour ).ConfigureAwait(false);
      if ( resultParameters != null )
         HSP.VerifyResultParameters( resultParameters );
      return rdr;
   }
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
/// <exception cref="InvalidCastException" />
/// <exception cref="HDatabaseException" />
public static async Task<int> ExecuteNonQueryVerify_A(
   [HOptional] HSPResultParameters? resultParameters,
   SqlCommand command )
{
   using ( HArgChecking.VerifyNotNull( command ) )
   {
      var rdr = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
      if ( resultParameters != null )
         HSP.VerifyResultParameters( resultParameters );
      return rdr;
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HSPCommandDispose
//**************************************************************************************************

} // CommonHelpers
