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

namespace CommonHelpers {

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

/// <exception cref="ArgumentException" />
[DefaultValue(0)]
public static int CommandTimeout
{
   get {
      return _CommandTimeout_;
   }
   set {
      if ( value < 0 )
         throw new ArgumentException();
      _CommandTimeout_ = value;
   }
}

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

/// <exception cref="ArgumentNullException" />
public static SqlCommand CreateSPCommand(SqlConnection connection,string commandText) =>
   CreateSPCommandEx( connection, null, commandText );

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

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

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

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

/// <exception cref="ArgumentNullException" />
public static SqlCommand CreateSPCommandEx( SqlConnection connection,
                                            SqlTransaction? transaction,
                                            string commandText )
{
   HArgChecking.VerifyNotNulls( commandText, connection );
   var cmd = transaction != null ?
      new SqlCommand( commandText, connection, transaction ) :
      new SqlCommand( commandText, connection );
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandTimeout = CommandTimeout;
   return cmd;
}

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

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

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

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

static string LFnStoredProcOrEmpty([HOptional] string? procedureName) =>
   procedureName != null ?
      HString.FormatAsText(
         "\n({0}: [{1}].)",
         HLocal.TEXT( "Stored procedure", "Хранимая процедура" ),
         procedureName
      ) : "";

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

static volatile int _CommandTimeout_;

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

} // CommonHelpers
