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

namespace CommonHelpers {

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

/// <exception cref="ArgumentNullException" />
[HOptionalResult]
public static List<RSet_Item>? RSetToList<RSet_Item>( SqlDataReader dataReader,
                                                      bool closeReader = false )
where RSet_Item: HIFromDataReaderItem, new()
{
   HArgChecking.VerifyNotNull( dataReader );
   Action disposeReader = delegate{
      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
   {
      HOps.Try( disposeReader );
      throw;
   }
   disposeReader();
   return list;
}

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

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

/// <exception cref="ArgumentNullException" />
[HOptionalResult]
public static List<RSet_Item>? DoReaderCommand<RSet_Item>( SqlConnection connection,
                                                           string commandText )
where RSet_Item: HIFromDataReaderItem, new()
{
   HArgChecking.VerifyNotNulls( connection, commandText );
   return RSetToList<RSet_Item>( new HSqlOperator(connection).ExecuteReader( commandText ), true );
}
/// <exception cref="ArgumentNullException" />
[HOptionalResult]
public static List<RSet_Item>? DoReaderCommand<RSet_Item>( SqlConnection connection,
                                                           string commandText,
                                                           params SqlParameter[] sqlParams )
where RSet_Item: HIFromDataReaderItem, new()
{
   HArgChecking.VerifyNotNulls( connection, commandText, sqlParams );
   return RSetToList<RSet_Item>(
      new HSqlOperator( connection ).ExecuteReader( commandText, sqlParams ),
      true );
}

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

/// <exception cref="ArgumentNullException" />
[HOptionalResult]
public static List<RSet_Item>? DoReaderCommandFmt<RSet_Item>( SqlConnection connection,
                                                              string commandTextFmt,
                                                              params object?[] parameters )
where RSet_Item: HIFromDataReaderItem, new()
{
   HArgChecking.VerifyNotNulls( connection, commandTextFmt, parameters );
   return RSetToList<RSet_Item>(
      new HSqlOperator( connection ).ExecuteReaderFmt( commandTextFmt, parameters ),
      true );
}

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

} // CommonHelpers
