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

namespace DataHelpers.SQL {

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

public static List<RSet_Item>? RSetToList<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 ( dataReader.Read() ) {
            var item = new RSet_Item();
            item.AssignProperties( dataReader );
            list.Add( item );
         }
      }
   }
   catch
   {
      u.ops.Try( disposeReader );
      throw;
   }
   disposeReader();
   return list;
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ObjectDisposedException" />
public static List<RSet_Item>? DoReaderCommand<RSet_Item>( [u.req] ref SqlCommand? command )
where RSet_Item: HIFromDataReaderItem, new()
{
   u.arg_check.NN( command, nameof(command) );
   return RSetToList<RSet_Item>( HSqlCommandDispose.ExecuteReader( ref command ), true );
}

public static List<RSet_Item>? DoReaderCommand<RSet_Item>( SqlConnection connection,
                                                           string commandText )
where RSet_Item: HIFromDataReaderItem, new() =>
   RSetToList<RSet_Item>( new HSqlOperator(connection).ExecuteReader( commandText ), true );

public static List<RSet_Item>? DoReaderCommand<RSet_Item>( SqlConnection connection,
                                                           string commandText,
                                                           params SqlParameter[] sqlParams )
where RSet_Item: HIFromDataReaderItem, new() =>
   RSetToList<RSet_Item>(
      new HSqlOperator( connection ).ExecuteReader( commandText, sqlParams ),
      true );

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

public static List<RSet_Item>? DoReaderCommandFmt<RSet_Item>( SqlConnection connection,
                                                              string commandTextFmt,
                                                              params object?[] args )
where RSet_Item: HIFromDataReaderItem, new() =>
   RSetToList<RSet_Item>(
      new HSqlOperator( connection ).ExecuteReaderFmt( commandTextFmt, args ),
      true );

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

} // DataHelpers.SQL
