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

namespace DataHelpers.SQL {

/// <summary>
/// </summary>
[u.volatile_access]
public partial class HSqlOperator {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public SqlConnection Connection { get; }

/// <exception cref="ArgumentOutOfRangeException" />
[u.def(0)]
public int CommandTimeout {
   get => _commandTimeout;
   set => _commandTimeout = u.arg_check.NotNegative_PV( value ); }

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

public HSqlOperator( SqlConnection connection ) =>
   Connection = connection;

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

public int ExecuteNonQuery( string commandText ) =>
   ExecuteNonQuery( null, commandText );
public int ExecuteNonQuery( string commandText, params SqlParameter[] sqlParams ) =>
   ExecuteNonQuery( null, commandText, sqlParams );
public int ExecuteNonQueryFmt( string commandTextFmt, params object?[] args ) =>
   ExecuteNonQueryFmt( null, commandTextFmt, args );

public int ExecuteNonQuery( SqlTransaction? transaction, string commandText )
{
   using ( var cmd = PrepareCommand( transaction, commandText ) )
      return cmd.ExecuteNonQuery();
}
public int ExecuteNonQuery( SqlTransaction? transaction,
                            string commandText, params SqlParameter[] sqlParams )
{
   using ( var cmd = PrepareCommand( transaction, commandText, sqlParams) )
      return cmd.ExecuteNonQuery();
}
public int ExecuteNonQueryFmt( SqlTransaction? transaction,
                               string commandText, params object?[] args ) =>
   ExecuteNonQuery( transaction, commandText.FMT( args ) );

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

public SqlDataReader ExecuteReader( string commandText ) =>
   ExecuteReader( null, CommandBehavior.Default, commandText );
public SqlDataReader ExecuteReader( string commandText, params SqlParameter[] sqlParams ) =>
   ExecuteReader( CommandBehavior.Default, commandText, sqlParams );
public SqlDataReader ExecuteReaderFmt( string commandTextFmt, params object?[] args ) =>
   ExecuteReaderFmt( null, CommandBehavior.Default, commandTextFmt, args );

public SqlDataReader ExecuteReader( SqlTransaction? transaction, string commandText ) =>
   ExecuteReader( transaction, CommandBehavior.Default, commandText );
public SqlDataReader ExecuteReader( SqlTransaction? transaction,
                                    string commandText, params SqlParameter[] sqlParams ) =>
   ExecuteReader( transaction, CommandBehavior.Default, commandText, sqlParams );
public SqlDataReader ExecuteReaderFmt( SqlTransaction? transaction,
                                       string commandText, params object?[] args ) =>
   ExecuteReaderFmt( transaction, CommandBehavior.Default, commandText, args );

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

public SqlDataReader ExecuteReader( [u.opt] CommandBehavior behaviour, string commandText ) =>
   ExecuteReader( null, behaviour, commandText );
public SqlDataReader ExecuteReader( [u.opt] CommandBehavior behaviour,
                                    string commandText, params SqlParameter[] sqlParams ) =>
   ExecuteReader( null, behaviour, commandText, sqlParams );
public SqlDataReader ExecuteReaderFmt( [u.opt] CommandBehavior behaviour,
                                       string commandTextFmt, params object?[] args ) =>
   ExecuteReaderFmt( null, behaviour, commandTextFmt, args );

public SqlDataReader ExecuteReader( SqlTransaction? transaction,
                                    [u.opt] CommandBehavior behaviour,
                                    string commandText )
{
   using ( var cmd = PrepareCommand( transaction, commandText ) )
      return cmd.ExecuteReader( behaviour );
}
public SqlDataReader ExecuteReader( SqlTransaction? transaction,
                                    [u.opt] CommandBehavior behaviour,
                                    string commandText, params SqlParameter[] sqlParams )
{
   using ( var cmd = PrepareCommand( transaction, commandText, sqlParams ) )
      return cmd.ExecuteReader( behaviour );
}
public SqlDataReader ExecuteReaderFmt( SqlTransaction? transaction,
                                       [u.opt] CommandBehavior behaviour,
                                       string commandTextFmt, params object?[] args ) =>
   ExecuteReader( transaction, behaviour, commandTextFmt.FMT( args ) );

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

public SqlCommand PrepareCommand( string commandText ) =>
   PrepareCommand( null, commandText );
public SqlCommand PrepareCommand( string commandText, params SqlParameter[] sqlParams ) =>
   PrepareCommand( null, commandText, sqlParams );

public SqlCommand PrepareCommandFmt( string commandTextFmt, params object?[] args ) =>
   PrepareCommandFmt( null, commandTextFmt, args );

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

public SqlCommand PrepareCommand( SqlTransaction? transaction, string commandText )
{
   var cmd = transaction != null ?
      new SqlCommand( commandText, Connection, transaction ) :
      new SqlCommand( commandText, Connection );
   cmd.CommandTimeout = CommandTimeout;
   return cmd;
}
public SqlCommand PrepareCommand( SqlTransaction? transaction,
                                  string commandText, params SqlParameter[] sqlParams )
{
   var cmd = PrepareCommand( transaction, commandText );
   foreach ( var param in sqlParams )
      cmd.Parameters.Add( param );
   return cmd;
}

public SqlCommand PrepareCommandFmt( SqlTransaction? transaction,
                                     string commandTextFmt, params object?[] args ) =>
   PrepareCommand( transaction, commandTextFmt.FMT( args ) );

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

volatile int _commandTimeout;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HSqlOperator

} // DataHelpers.SQL
