###############################################################################
#  In this test case, we check how changes to temporary tables are written
#  to the binary log.
#
#
# B - Begin
# C - Commit
# R - Rollback
#
#
# T - Inserts a row into a T-table
# N-Temp - Inserts a row into a temporary N-table.
#
#
# T-SELECT-N-Temp - Selects from a temporary N-table and inserts
#                   into a T-table.
# N-SELECT-N-Temp - Selects from a temporary N-table and inserts
#                   into a N-table.
# T-SELECT-T-Temp - Selects from a temporary T-table and inserts
#                   into a T-table.
# N-SELECT-T-Temp - Selects from a temporary T-table and inserts
#                   into a N-table.

# Create-N-Temp - Creates a temporary N-table if a temporary N-table
#                 was dropped before
# Create-T-Temp - Creates a temporary T-table if a temporary T-table
#                 was dropped before
#
#
# Drop-Temp-T-Temp - Drops a temporary T-table if there is any
# Drop-Temp-N-Temp - Drops a temporary N-table if there is any
# Drop-Temp-TN-Temp - Drops both a temporary T-table and N-table if there
#                     is any
# Drop-Temp-TT-Temp - Drops two temporary T-tables if there is any
# Drop-Temp-NN-Temp - Drops two temporary N-tables if there is any
# Drop-Temp-Xe-Temp - Tries to drop a temporary table that does not exist
# Drop-Temp-NXe-Temp - Drops a temporary N-table if there is any and
#                      a temporary table that does not exist
# Drop-Temp-TXe-Temp - Drops a temporary T-table if there is any and
#                      a temporary table that does not exist
#
#
# Drop-Temp-If-Xe-Temp - Tries to drop a temporary table that does not exist
# Drop-Temp-If-TXe-Temp - Drops a temporary T-table if there is any and
#                         a temporary table that does not exist
#
#
# Drop-T - Drops a T-table if there is any
# Drop-N - Drops a  N-table if there is any
# Drop-Xe - Tries to drop a table that does not exist
# Drop-TXe - Drops a T-table if there is any and a table that does
#            not exist
# Drop-NXe - Drops a N-table if there is any and a table that does
#            not exist
# Drop-TN - Drops both a T-table and N-table if there is any
# Drop-TT - Drops two T-tables if there is any
# Drop-NN - Drops two N-tables if there is any
# Drop-N-TN-Temp - Drops a N-table and both a temporary T-table and
#                  N-table if there is any
#
#
# Drop-If-Xe - Tries to drop a table that does not exist
# Drop-If-TXe - Drops a T-table if there is any and a table that does
#               not exist
#
#
# This test uses the commands available at:
#   extra/rpl_tests/rpl_drop_create_temp_table.inc
#
###############################################################################


--echo #########################################################################
--echo #                            CONFIGURATION
--echo #########################################################################

call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");

--let $tot_table= 2
SET @commands= 'configure';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo #########################################################################
--echo #           1 - Tables dropped by "DROP TEMPORARY TABLE"
--echo #########################################################################
connection master;

--echo
--echo #
--echo #1) Generates in the binlog what follows:
--echo #
SET @commands= 'Drop-Temp-T-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-N-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-Xe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-If-Xe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-TXe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-If-TXe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-NXe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-If-NXe-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-TN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-TT-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-NN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo #
--echo #2) Generates in the binlog what follows:
--echo #
SET @commands= 'B T Drop-Temp-T-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-T-Temp N Drop-Temp-T-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-N-Temp N Drop-Temp-N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-Xe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-Xe-Temp N Drop-Temp-Xe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-Xe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-Xe-Temp N Drop-Temp-If-Xe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TXe-Temp N Drop-Temp-TXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-TXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-TXe-Temp N Drop-Temp-If-TXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NXe-Temp N Drop-Temp-NXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-NXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-NXe-Temp N Drop-Temp-If-NXe-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TN-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TN-Temp N Drop-Temp-TN-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TT-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TT-Temp N Drop-Temp-TT-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp Ne C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp Te C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp NeT-trig C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo #
--echo #3) Generates in the binlog what follows:
--echo #
SET @commands= 'B T Drop-Temp-T-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-T-Temp N Drop-Temp-T-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-N-Temp N Drop-Temp-N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-Xe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-Xe-Temp N Drop-Temp-Xe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-Xe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-Xe-Temp N Drop-Temp-If-Xe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TXe-Temp N Drop-Temp-TXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-TXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-TXe-Temp N Drop-Temp-If-TXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NXe-Temp N Drop-Temp-NXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-NXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-If-NXe-Temp N Drop-Temp-If-NXe-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TN-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TN-Temp N Drop-Temp-TN-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TT-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-TT-Temp N Drop-Temp-TT-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp Ne R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp Te R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Temp-NN-Temp N Drop-Temp-NN-Temp NeT-trig R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo #########################################################################
--echo #                   2 - Tables dropped by "DROP TABLE"
--echo #########################################################################
connection master;

--echo
--echo #
--echo #1) Generates in the binlog what follows:
--echo #
SET @commands= 'Drop-T';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-N';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Xe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-If-Xe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-TXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-If-TXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-NXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-If-NXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-TN';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-TT';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-NN';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-N-TN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-TN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo #
--echo #2) Generates in the binlog what follows:
--echo #
SET @commands= 'B T Drop-T';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-N';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-Xe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-If-Xe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-TXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-If-TXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-NXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-If-NXe';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-TN';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-TT';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-NN';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-N-TN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B T Drop-TN-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo #########################################################################
--echo #                      3 - CREATE TEMPORARY TABLE
--echo #########################################################################
connection master;

--echo
--echo #
--echo #1) Generates in the binlog what follows:
--echo #
SET @commands= 'Drop-Temp-N-Temp Create-N-Temp Drop-Temp-N-Temp Drop-Temp-N-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo #
--echo #2) Generates in the binlog what follows:
--echo #
SET @commands= 'B T Drop-Temp-N-Temp Create-N-Temp Drop-Temp-N-Temp Drop-Temp-N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp Ne C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp Te C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp NeT-trig C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo #
--echo #3) Generates in the binlog what follows:
--echo #
SET @commands= 'B T Drop-Temp-N-Temp Create-N-Temp Drop-Temp-N-Temp Drop-Temp-N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp Ne R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp Te R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B Drop-Temp-T-Temp Create-T-Temp Drop-Temp-T-Temp Drop-Temp-T-Temp NeT-trig R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo #########################################################################
--echo #                     4 -  CHANGING TEMPORARY TABLES
--echo #########################################################################
connection master;

--echo
--echo #
--echo #1) Generates in the binlog what follows:
--echo #
SET @commands= 'B N N-Temp N-SELECT-N-Temp N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-N-Temp N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp N-SELECT-T-Temp N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp Ne C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp Te C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp NeT-trig C';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo
--echo
--echo #
--echo #2) Generates in the binlog what follows:
--echo #
SET @commands= 'B N N-Temp N-SELECT-N-Temp N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-N-Temp N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp N-SELECT-T-Temp N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp Ne R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp Te R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

SET @commands= 'B N N-Temp T-SELECT-T-Temp N-Temp NeT-trig R';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc

--echo ###################################################################################
--echo #                               CHECK CONSISTENCY
--echo ###################################################################################
connection master;
sync_slave_with_master;

let $MYSQLD_DATADIR= `SELECT @@datadir`;

if (`select @@session.binlog_format != 'STATEMENT'`)
{
  --exec $MYSQL_DUMP --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLD_DATADIR/test-temporary-master.sql
  --exec $MYSQL_DUMP_SLAVE --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLD_DATADIR/test-temporary-slave.sql
  --diff_files $MYSQLD_DATADIR/test-temporary-master.sql $MYSQLD_DATADIR/test-temporary-slave.sql
}

--echo #########################################################################
--echo #                           CLEAN
--echo #########################################################################
SET @commands= 'clean';
--source extra/rpl_tests/rpl_drop_create_temp_table.inc
