//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// DataHelpers.SQL satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using u = user_rtx;

namespace DataHelpers.SQL {

/// <summary>
/// </summary>
[h_db_orm_type]
public static class HSPCommandDispose {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

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

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

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

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

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

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

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

} // HDataHelpers.SQL
