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

namespace DataHelpers.SQL {

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

public static string TXT( 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();
}

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

public static SqlParameter In( string paramName, object value ) =>
   new SqlParameter(){
      ParameterName = paramName,
      Direction = ParameterDirection.Input,
      IsNullable = false,
      Value = value };

public static SqlParameter InOut( string paramName, object value ) =>
   new SqlParameter(){
      ParameterName = paramName,
      Direction = ParameterDirection.InputOutput,
      IsNullable = false,
      Value = value };

public static SqlParameter RetValue() =>
   new SqlParameter(){
      IsNullable = false,
      Direction = ParameterDirection.ReturnValue };

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

public static SqlParameter In( string paramName, DbType dbType, object value ) =>
   new SqlParameter(){
      ParameterName = paramName,
      DbType = dbType,
      Direction = ParameterDirection.Input,
      IsNullable = false,
      Value = value };

public static SqlParameter Out( string paramName, DbType dbType ) =>
   new SqlParameter(){
      ParameterName = paramName,
      DbType = dbType,
      Direction = ParameterDirection.Output,
      IsNullable = false };

public static SqlParameter InOut( string paramName, DbType dbType, object value ) =>
   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 };

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

public static SqlParameter AddIn( SqlCommand command, string paramName, object value ) =>
   command.Parameters.Add( In( paramName, value ) );

public static SqlParameter AddInOut( SqlCommand command, string paramName, object value ) =>
   command.Parameters.Add( InOut( paramName, value ) );

public static SqlParameter AddRetValue( SqlCommand command ) =>
   command.Parameters.Add( RetValue() );

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

public static SqlParameter AddIn( SqlCommand command, string paramName,
                                  DbType dbType, object value ) =>
   command.Parameters.Add( In( paramName, dbType, value ) );

public static SqlParameter AddOut( SqlCommand command, string paramName, DbType dbType ) =>
   command.Parameters.Add( Out( paramName, dbType ) );

public static SqlParameter AddInOut( SqlCommand command, string paramName,
                                     DbType dbType, object value ) =>
   command.Parameters.Add( InOut( paramName, dbType, value ) );

public static SqlParameter AddRetValue( SqlCommand command, DbType dbType ) =>
   command.Parameters.Add( RetValue( dbType ) );

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

} // DataHelpers.SQL
