//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// 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 {

static partial class HDB {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static async Task<List<RSet_Item>?> RSetToList_A<RSet_Item>( SqlDataReader dataReader,
                                                                    bool closeReader = false )
where RSet_Item: HIFromDataReaderItem, new()
{
   void disposeReader() {
      if ( closeReader ) {
         dataReader.Close();
         dataReader.Dispose();
      }};
   List<RSet_Item>? list = null;
   try {
      if ( dataReader.FieldCount > 0 ) {
         list = new List<RSet_Item>();
         while ( await dataReader.ReadAsync().ConfigureAwait(false) ) {
            var item = new RSet_Item();
            item.AssignProperties( dataReader );
            list.Add( item );
         }
      }
   }
   catch {
      u.ops.Try( disposeReader );
      throw;
   }
   disposeReader();
   return list;
}

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

/// <exception cref="ObjectDisposedException" />
public static async Task<List<RSet_Item>?> DoReaderCommand_A<RSet_Item>( SqlCommand command )
where RSet_Item: HIFromDataReaderItem, new() =>
   await RSetToList_A<RSet_Item>(
      await HSqlCommandDispose.ExecuteReader_A( command ).ConfigureAwait(false),
      true ).ConfigureAwait(false);

public static async Task<List<RSet_Item>?> DoReaderCommand_A<RSet_Item>( SqlConnection connection,
                                                                         string commandText )
where RSet_Item: HIFromDataReaderItem, new() =>
   await RSetToList_A<RSet_Item>(
      await new HSqlOperator( connection ).ExecuteReader_A(
         commandText ).ConfigureAwait(false),
      true ).ConfigureAwait(false);

public static async Task<List<RSet_Item>?> DoReaderCommand_A<RSet_Item>(
   SqlConnection connection, string commandText, params SqlParameter[] sqlParams )
where RSet_Item: HIFromDataReaderItem, new() =>
   await RSetToList_A<RSet_Item>(
      await new HSqlOperator( connection ).ExecuteReader_A(
         commandText, sqlParams ).ConfigureAwait(false),
      true ).ConfigureAwait(false);

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

public static async Task<List<RSet_Item>?> DoReaderCommandFmt_A<RSet_Item>(
   SqlConnection connection, string commandTextFmt, params object?[] args )
where RSet_Item: HIFromDataReaderItem, new() =>
   await RSetToList_A<RSet_Item>(
      await new HSqlOperator( connection ).ExecuteReaderFmt_A(
         commandTextFmt, args ).ConfigureAwait(false),
      true ).ConfigureAwait(false);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HDB

} // DataHelpers.SQL
