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

namespace CommonHelpers {

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

public static string TXT([HOptional] string? value)
{
   if ( value == null )
      return "null";
   value = value.TrimEnd();
   var sb = new StringBuilder( value.Length );
   sb.Append( "'" );
   foreach (var ch in value)
      if ( ch == '\'' )
         sb.Append( "''" );
      else
         sb.Append( ch );
   return sb.ToString();
}

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

/// <exception cref="ArgumentNullException" />
public static SqlParameter In(string paramName,object value)
{
   HArgChecking.VerifyNotNulls( paramName, value );
   return new SqlParameter(){
      ParameterName = paramName,
      Direction = ParameterDirection.Input,
      IsNullable = false,
      Value = value };
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter InOut(string paramName,object value)
{
   HArgChecking.VerifyNotNulls( paramName, value );
   return new SqlParameter(){
      ParameterName = paramName,
      Direction = ParameterDirection.InputOutput,
      IsNullable = false,
      Value = value };
}
public static SqlParameter RetValue() =>
   new SqlParameter(){
      IsNullable = false,
      Direction = ParameterDirection.ReturnValue };

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

/// <exception cref="ArgumentNullException" />
public static SqlParameter In(string paramName,DbType dbType,object value)
{
   HArgChecking.VerifyNotNulls( paramName, value );
   return new SqlParameter(){
      ParameterName = paramName,
      DbType = dbType,
      Direction = ParameterDirection.Input,
      IsNullable = false,
      Value = value };
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter Out(string paramName,DbType dbType)
{
   HArgChecking.VerifyNotNull( paramName );
   return new SqlParameter(){
      ParameterName = paramName,
      DbType = dbType,
      Direction = ParameterDirection.Output,
      IsNullable = false };
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter InOut(string paramName,DbType dbType,object value)
{
   HArgChecking.VerifyNotNulls( paramName, value );
   return new SqlParameter(){
      ParameterName = paramName,
      DbType = dbType,
      Direction = ParameterDirection.InputOutput,
      IsNullable = false,
      Value = value };
}
public static SqlParameter RetValue(DbType dbType) =>
   new SqlParameter(){
      DbType = dbType,
      Direction = ParameterDirection.ReturnValue,
      IsNullable = false };

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

/// <exception cref="ArgumentNullException" />
public static SqlParameter AddIn(SqlCommand command,string paramName,object value)
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( In( paramName, value ) );
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter AddInOut(SqlCommand command,string paramName,object value)
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( InOut( paramName, value ) );
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter AddRetValue(SqlCommand command)
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( RetValue() );
}

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

/// <exception cref="ArgumentNullException" />
public static SqlParameter AddIn( SqlCommand command, string paramName,
                                  DbType dbType, object value )
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( In( paramName, dbType, value ) );
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter AddOut(SqlCommand command,string paramName,DbType dbType)
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( Out( paramName, dbType ) );
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter AddInOut( SqlCommand command, string paramName,
                                     DbType dbType, object value )
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( InOut( paramName, dbType, value ) );
}
/// <exception cref="ArgumentNullException" />
public static SqlParameter AddRetValue(SqlCommand command,DbType dbType)
{
   HArgChecking.VerifyNotNull( command );
   return command.Parameters.Add( RetValue( dbType ) );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HSqlParam
//**************************************************************************************************

} // CommonHelpers
