@echo off
::==================================================================================================
:: T-SQL LANGUAGE EXTENSIONS AS PREPROCESSOR MACROS (FOR IMPORT THROUGH "SQLAUX.cmd"):
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: SQLAUX scripting library | Author: Sergei Y.Kitaev
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if not "%~1"=="(InternalCall)" goto INCORRECT_INTERNAL_CALL
::==================================================================================================
:: AUXILIARY TEMPORARY DEFS (FOR SCRIPT TRANSLATION):
::--------------------------------------------------------------------------------------------------

set _sql_settings_to_on_aux_=ansi_nulls,ansi_null_dflt_on,quoted_identifier,arithabort,nocount
set _sql_settings_to_off_aux_=implicit_transactions
rem set _sql_settings_to_off_aux_=

set _sql_settings_continuation_aux_=set !_sql_settings_to_on_aux_! on;
if defined _sql_settings_to_off_aux_ (
set _sql_settings_continuation_aux_=!_sql_settings_continuation_aux_! set !_sql_settings_to_off_aux_! off;
)
set _sql_settings_applying_aux_=!_sql_settings_continuation_aux_!
if "!xact_abort_on_for_begin_macro_aux!"=="1"  goto XACT_ABORT_ON
if "!xact_abort_on_for_begin_macro_aux!"=="0"  goto XACT_ABORT_OFF
if "!xact_abort_on_for_begin_macro_aux!"=="-1" goto XACT_ABORT_END
if "!xact_abort_on_for_begin_macro_aux!"==""   goto XACT_ABORT_OFF
goto INCORRECT_VAR_XACT_ABORT_ON_FOR_BEGIN_MACRO_AUX
:XACT_ABORT_ON
set _sql_settings_applying_aux_=set xact_abort on; !_sql_settings_applying_aux_!
goto XACT_ABORT_END
:XACT_ABORT_OFF
set _sql_settings_applying_aux_=set xact_abort off; !_sql_settings_applying_aux_!
:XACT_ABORT_END

set _apply_sql_settings_xact_abort_on_aux_=set xact_abort on; !_sql_settings_continuation_aux_!
set _apply_sql_settings_xact_abort_off_aux_=set xact_abort off; !_sql_settings_continuation_aux_!

set _declare_script_variables_aux_=declare @initial_tran_count_aux int, @is_local_tran_open_aux bit;
set _declare_script_variables_aux_=!_declare_script_variables_aux_! select @initial_tran_count_aux=@@trancount, @is_local_tran_open_aux=0;

set _obtain_exception_message_aux_=declare @exception_msg_aux nvarchar(max); set @exception_msg_aux=error_message();
set _raise_error_aux_=raiserror (@exception_msg_aux,11,1);

set _sqlfile_readonly_aux_=
if not "!sqlfile_readonly_aux!"=="0" set _sqlfile_readonly_aux_= if 0=1 print '$(SQLFILE_READONLY)';

::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

set _begin_transaction_aux_=begin transaction; set @is_local_tran_open_aux=1;
set _commit_transaction_aux_=commit transaction; set @is_local_tran_open_aux=0;
set _rollback_transaction_aux_=rollback transaction; set @is_local_tran_open_aux=0;

::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

set _lt_nesting_forbidden_msg_aux_='Local transaction nesting is forbidden. (Macro: BEGIN_TRANSACTION_AUX)'
set _lt_not_open_to_commit_msg_aux_='Open local transaction is absent. (Macro: COMMIT_TRANSACTION_AUX)'
set _lt_not_open_to_rollback_msg_aux_='Open local transaction is absent. (Macro: ROLLBACK_TRANSACTION_AUX)'
set _lt_not_closed_msg_aux_='Not closed local transaction was detected, previously opened by BEGIN_TRANSACTION_AUX macro.'

set _check_lt_before_open_aux_=if @is_local_tran_open_aux=1 raiserror (!_lt_nesting_forbidden_msg_aux_!,11,1);
set _check_lt_before_commit_aux_=if @is_local_tran_open_aux=0 raiserror (!_lt_not_open_to_commit_msg_aux_!,11,1);
set _check_lt_before_rollback_aux_=if @is_local_tran_open_aux=0 raiserror (!_lt_not_open_to_rollback_msg_aux_!,11,1);
set _check_lt_not_open_aux_=if @is_local_tran_open_aux=1 raiserror (!_lt_not_closed_msg_aux_!,11,1);

::==================================================================================================
:: MAIN KEYWORDS (SQL-EXTENSIONS):
::--------------------------------------------------------------------------------------------------

set BEGIN_AUX=begin begin try !_sql_settings_applying_aux_! !_declare_script_variables_aux_! end try begin catch throw; end catch;
set BEGIN_AUX=!BEGIN_AUX! begin try!_sqlfile_readonly_aux_!

set BEGIN_WITH_XACT_ABORT_ON_AUX=begin begin try !_apply_sql_settings_xact_abort_on_aux_! !_declare_script_variables_aux_! end try begin catch throw; end catch;
set BEGIN_WITH_XACT_ABORT_ON_AUX=!BEGIN_WITH_XACT_ABORT_ON_AUX! begin try!_sqlfile_readonly_aux_!

set BEGIN_WITH_XACT_ABORT_OFF_AUX=begin begin try !_apply_sql_settings_xact_abort_off_aux_! !_declare_script_variables_aux_! end try begin catch throw; end catch;
set BEGIN_WITH_XACT_ABORT_OFF_AUX=!BEGIN_WITH_XACT_ABORT_OFF_AUX! begin try!_sqlfile_readonly_aux_!

set BEGIN_WITHOUT_XACT_ABORT_AUX=begin begin try !_sql_settings_continuation_aux_! !_declare_script_variables_aux_! end try begin catch throw; end catch;
set BEGIN_WITHOUT_XACT_ABORT_AUX=!BEGIN_WITHOUT_XACT_ABORT_AUX! begin try!_sqlfile_readonly_aux_!

set END_AUX=!_check_lt_not_open_aux_! end try begin catch if @is_local_tran_open_aux=1 and (@@trancount^>
set END_AUX=!END_AUX!@initial_tran_count_aux or xact_state()=-1) !_rollback_transaction_aux_! throw; end catch; end

set END_NOTHROW_AUX=!_check_lt_not_open_aux_! end try begin catch !_obtain_exception_message_aux_! if @is_local_tran_open_aux=1 and (@@trancount^>
set END_NOTHROW_AUX=!END_NOTHROW_AUX!@initial_tran_count_aux or xact_state()=-1) !_rollback_transaction_aux_! !_raise_error_aux_! end catch; end

set BEGIN_TRANSACTION_AUX=begin !_check_lt_before_open_aux_! !_begin_transaction_aux_! end
set COMMIT_TRANSACTION_AUX=begin !_check_lt_before_commit_aux_! !_commit_transaction_aux_! end
set COMMIT_OPEN_LT_AUX=begin if @is_local_tran_open_aux=1 begin !_commit_transaction_aux_! end end

set ROLLBACK_TRANSACTION_AUX=begin !_check_lt_before_rollback_aux_! !_rollback_transaction_aux_! end
set ROLLBACK_OPEN_LT_AUX=begin if @is_local_tran_open_aux=1 and @@trancount^>0 !_rollback_transaction_aux_! end
set ROLLBACK_UNCOMMITTABLE_LT_AUX=begin if @is_local_tran_open_aux=1 and xact_state()=-1 !_rollback_transaction_aux_! end

::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

set INITIAL_TRANCOUNT_AUX=@initial_tran_count_aux
set IS_IN_OPEN_LT_AUX=(@is_local_tran_open_aux=1)
set RESET_OPEN_LT_FLAG_AUX=begin @is_local_tran_open_aux=0; end

set NOP_AUX=while 0=0 begin break end
set LOOP_AUX=while 0=0

set TRUE_AUX=(0=0)
set FALSE_AUX=(0=1)

set YES_AUX=convert(bit,1)
set NO_AUX=convert(bit,0)

set SUCCESS_AUX=(0)
set FAILURE_AUX=(1)

set FLOAT_1_AUX=convert(float,1)

::--------------------------------------------------------------------------------------------------

set IS_XACT_ABORT_ON_AUX=(@@options^&16384^>0)
set IS_NUMERIC_ROUNDABORT_ON_AUX=(@@options^&8192^>0)
set IS_CONCAT_NULL_YIELDS_NULL_ON_AUX=(@@options^&4096^>0)
set IS_ANSI_NULL_DFLT_OFF_ON_AUX=(@@options^&2048^>0)
set IS_ANSI_NULL_DFLT_ON_ON_AUX=(@@options^&1024^>0)
set IS_NOCOUNT_ON_AUX=(@@options^&512^>0)
set IS_QUOTED_IDENTIFIER_ON_AUX=(@@options^&256^>0)
set IS_ARITHIGNORE_ON_AUX=(@@options^&128^>0)
set IS_ARITHABORT_ON_AUX=(@@options^&64^>0)
set IS_ANSI_NULLS_ON_AUX=(@@options^&32^>0)
set IS_ANSI_PADDING_ON_AUX=(@@options^&16^>0)
set IS_ANSI_WARNING_ON_AUX=(@@options^&8^>0)
set IS_CURSOR_CLOSE_ON_COMMIT_ON_AUX=(@@options^&4^>0)
set IS_IMPLICIT_TRANSACTIONS_ON_AUX=(@@options^&2^>0)
set IS_DISABLE_DEF_CNST_CHK_ON_AUX=(@@options^&1^>0)

set XACT_ABORT_AUX=convert(bit,IIF(@@options^&16384^>0,1,0)
set NUMERIC_ROUNDABORT_AUX=convert(bit,IIF(@@options^&8192^>0,1,0)
set CONCAT_NULL_YIELDS_NULL_AUX=convert(bit,IIF(@@options^&4096^>0,1,0)
set ANSI_NULL_DFLT_OFF_AUX=convert(bit,IIF(@@options^&2048^>0,1,0)
set ANSI_NULL_DFLT_ON_AUX=convert(bit,IIF(@@options^&1024^>0,1,0)
set NOCOUNT_AUX=convert(bit,IIF(@@options^&512^>0,1,0)
set QUOTED_IDENTIFIER_AUX=convert(bit,IIF(@@options^&256^>0,1,0)
set ARITHIGNORE_AUX=convert(bit,IIF(@@options^&128^>0,1,0)
set ARITHABORT_AUX=convert(bit,IIF(@@options^&64^>0,1,0)
set ANSI_NULLS_AUX=convert(bit,IIF(@@options^&32^>0,1,0)
set ANSI_PADDING_AUX=convert(bit,IIF(@@options^&16^>0,1,0)
set ANSI_WARNING_AUX=convert(bit,IIF(@@options^&8^>0,1,0)
set CURSOR_CLOSE_ON_COMMIT_AUX=convert(bit,IIF(@@options^&4^>0,1,0)
set IMPLICIT_TRANSACTIONS_AUX=convert(bit,IIF(@@options^&2^>0,1,0)
set DISABLE_DEF_CNST_CHK_AUX=convert(bit,IIF(@@options^&1^>0,1,0)

::==================================================================================================
:: CONDITIONAL "TEST" AND "NONTEST" BLOCKS SUPPORT ("TEST IMPLEMENTATION"):
::--------------------------------------------------------------------------------------------------

set TEST_IMPLEMENTATION_AUX=!FALSE_AUX!
set BEGIN_TEST_AUX=begin !NOP_AUX!;/*$(BEGIN_TEST_AUX)
set END_TEST_AUX=$(END_TEST_AUX)*/end
set BEGIN_NONTEST_AUX=begin !NOP_AUX!;/*BEGIN_NONTEST_AUX*/
set END_NONTEST_AUX=/*END_NONTEST_AUX*/end

if not "!test_implementation_sign_aux!"=="1" goto TEST_IMPLEMENTATION_END

set TEST_IMPLEMENTATION_AUX=!TRUE_AUX!
set BEGIN_TEST_AUX=begin !NOP_AUX!;/*BEGIN_TEST_AUX*/
set END_TEST_AUX=/*END_TEST_AUX*/end
set BEGIN_NONTEST_AUX=begin !NOP_AUX!;/*BEGIN_NONTEST_AUX
set END_NONTEST_AUX=END_NONTEST_AUX*/end

:TEST_IMPLEMENTATION_END

::==================================================================================================
:: CONDITIONAL "METAIMPLEMENTATION" BLOCK SUPPORT:
::--------------------------------------------------------------------------------------------------

set META_IMPLEMENTATION_AUX=!FALSE_AUX!
set BEGIN_META_AUX=begin !NOP_AUX!;/*$(BEGIN_META_AUX)
set END_META_AUX=$(END_META_AUX)*/end
set BEGIN_NONMETA_AUX=/*BEGIN_NONMETA_AUX*/!BEGIN_AUX!
set END_NONMETA_AUX=!END_AUX!/*END_NONMETA_AUX*/

if not "!meta_implementation_sign_aux!"=="1" goto META_IMPLEMENTATION_END

set META_IMPLEMENTATION_AUX=!TRUE_AUX!
set BEGIN_META_AUX=/*BEGIN_META_AUX*/!BEGIN_AUX!
set END_META_AUX=!END_AUX!/*END_META_AUX*/
set BEGIN_NONMETA_AUX=begin !NOP_AUX!;/*$(BEGIN_NONMETA_AUX)
set END_NONMETA_AUX=$(END_NONMETA_AUX)*/end

:META_IMPLEMENTATION_END

::==================================================================================================
:: WIDELY USED SPECIAL CHARACTERS AND SEQUENCIES:
::--------------------------------------------------------------------------------------------------

set LAQUO_AUX=char(171)
set RAQUO_AUX=char(187)

set LANG_AUX=char(139)
set RANG_AUX=char(155)

set LSQM_AUX=char(145)
set RSQM_AUX=char(146)

set LDQUO_AUX=char(147)
set RDQUO_AUX=char(148)

set BULL_AUX=char(149)
set MIDDOT_AUX=char(183)

set PLUSMN_AUX=char(177)
set HELLIP_AUX=char(133)
set DEG_AUX=char(176)
set SECT_AUX=char(167)

set COPY_AUX=char(169)
set REG_AUX=char(174)

set MDASH_AUX=char(151)
set NDASH_AUX=char(150)

::--------------------------------------------------------------------------------------------------

set CR_AUX=char(13)
set LF_AUX=char(10)

set CR_LF_AUX=!CR_AUX!,!LF_AUX!

::--------------------------------------------------------------------------------------------------

set NL_AUX=(!CR_AUX!+!LF_AUX!)
set NLNL_AUX=(!NL_AUX!+!NL_AUX!)

set LARROW_AUX=('^<'+!MDASH_AUX!)
set RARROW_AUX=(!MDASH_AUX!+'^>')

::--------------------------------------------------------------------------------------------------

set BACKSLASH_AUX='\'

::==================================================================================================
:: NAKED CHARACTERS AND SEQUENCIES (NOT EMBRACED WITH SINGLE QUOTES):
::--------------------------------------------------------------------------------------------------

set _BACKSLASH_AUX=\
set _DOLLAR_AUX=$

::==================================================================================================
:: AUXILIARY INSERTIONS TO SQL:
::--------------------------------------------------------------------------------------------------

set NewLineAndErrorMessage_AUX=!NL_AUX!+ERROR_MESSAGE()
set AppendErrorMessageLine_AUX=set @ErrorMessage = case when ISNULL(@ErrorMessage,'')^<^>'' then ISNULL(@ErrorMessage,'')+!NL_AUX! else '' end +
set AppendScriptLine_AUX=set @ScriptText = case when ISNULL(@ScriptText,'')^<^>'' then ISNULL(@ScriptText,'')+!NL_AUX! else '' end +

::--------------------------------------------------------------------------------------------------
goto :EOF
::==================================================================================================
:INCORRECT_VAR_XACT_ABORT_ON_FOR_BEGIN_MACRO_AUX
echo.
echo ## Incorrect value of "xact_abort_on_for_begin_macro_aux" variable (!xact_abort_on_for_begin_macro_aux!).
echo ## Admissible values are: "1" (on), "0" (off), "-1" (not use) and "" (off, same as 0).
goto ERROR
::==================================================================================================
:INCORRECT_INTERNAL_CALL
echo.
echo ## Incorrect internal call of "%~nx0" (auxiliary batch).
goto ERROR
::==================================================================================================
:ERROR
echo 
pause
EXIT 255
::==================================================================================================
