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

namespace DataHelpers.SQL {

/// <summary>
/// </summary>
public static partial class HDB {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

public static SqlCommand CreateSPCommand( SqlConnection connection, string commandText ) =>
   CreateSPCommandEx( connection, null, commandText );

/// <exception cref="InvalidOperationException" />
public static SqlCommand CreateSPCommand( SqlConnection connection,
                                          string commandText,
                                          params SqlParameter[] sqlParams ) =>
   CreateSPCommandEx( connection, null, commandText, sqlParams );

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

/// <exception cref="InvalidOperationException" />
public static SqlCommand CreateSPCommandFmt( SqlConnection connection,
                                             string commandTextFmt,
                                             params object?[] args ) =>
   CreateSPCommandExFmt( connection, null, commandTextFmt, args );

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

public static SqlCommand CreateSPCommandEx( SqlConnection connection,
                                            SqlTransaction? transaction,
                                            string commandText )
{
   var cmd = transaction != null ?
      new SqlCommand( commandText, connection, transaction ) :
      new SqlCommand( commandText, connection );
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandTimeout = CommandTimeout;
   return cmd;
}

/// <exception cref="InvalidOperationException" />
public static SqlCommand CreateSPCommandEx( SqlConnection connection,
                                            SqlTransaction? transaction,
                                            string commandText,
                                            params SqlParameter[] sqlParams )
{
   var cmd = CreateSPCommandEx( connection, transaction, commandText );
   foreach ( var param in sqlParams )
      cmd.Parameters.Add( param );
   return cmd;
}

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

/// <exception cref="InvalidOperationException" />
public static SqlCommand CreateSPCommandExFmt( SqlConnection connection,
                                               SqlTransaction? transaction,
                                               string commandTextFmt,
                                               params object?[] args ) =>
   CreateSPCommandEx( connection, transaction, commandTextFmt.FMT( args ) );

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

static string LFnStoredProcOrEmpty( string? procedureName ) =>
   procedureName != null ?
      u.str.FMT(
         "\n({0}: [{1}].)",
         u.local.T( "Stored procedure", "Хранимая процедура" ),
         procedureName
      ) : "";

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

static volatile int _CommandTimeout_;

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

} // DataHelpers.SQL
