--echo
--echo
connection master;

if ($is_temporary)
{
  --let $_temporary=TEMPORARY
}

CREATE TABLE t2(c1 INT, c2 char(10));
INSERT INTO t2 VALUES(1, 'abc'), (2, 'abc');

--echo
--echo # The original query should be binlogged if the table does not exist.
--echo # ------------------------------------------------------------------
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS t1 (c1 INT , c2 INT, c3 char(10), c4 INT KEY)
  SELECT 'abc' AS c3, 1 AS c4;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # The statement should be binlogged as two events. one is 
--echo # 'CREATE $_temporary TABLE IF NOT EXISTS ..', another one is
--echo # 'INSERT ... SELECT'.
--echo # ------------------------------------------------------------------
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  SELECT 'abc', 2;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # Verify if it can be binlogged with right database name when the table
--echo # is not in the default database
--echo
--disable_warnings
DROP DATABASE IF EXISTS db1;
--enable_warnings
CREATE DATABASE db1;
USE db1;

let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS test.t1
  SELECT 'abc', 20;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:test.t1,slave:test.t1
  --source include/diff_tables.inc
}
USE test;
DROP DATABASE db1;

--echo
--echo # It should be binlogged as 'REPLACE ... SELECT'
--echo # if the original statement has option REPLACE
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  REPLACE SELECT '123', 2;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # It should be binlogged as 'INSERT IGNORE... SELECT'
--echo # if the original statement has option IGNORE 
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  IGNORE SELECT '123', 2;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # Nothing should be binlogged if error happens and no any row is inserted
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
--error ER_DUP_ENTRY
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  SELECT '123', 2;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # Verify it can binlog well when there are some braces('(')
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  (SELECT '123', 3) UNION (SELECT '123', 4);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  REPLACE (SELECT 'abc', 3) UNION (SELECT 'abc', 4);
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  IGNORE (SELECT '123', 3) UNION (SELECT '123', 4);
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

if (!$is_temporary)
{
  --echo
  --echo # Throw a warning that table already exists and don't insert anything
  --echo
  CREATE VIEW t3 AS SELECT * FROM t2;
  let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

  CREATE TABLE IF NOT EXISTS t3
    SELECT '123', 2;
  source include/show_binlog_events.inc;
  DROP VIEW t3;
}

--echo
--echo # The statement can be binlogged correctly when it is in a SP/EVENT/TRIGGER
--echo

--disable_warnings
DROP PROCEDURE IF EXISTS p1;
--enable_warnings
eval CREATE PROCEDURE p1(IN a INT)
  CREATE $_temporary TABLE IF NOT EXISTS t1 SELECT '123', a;

let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
call p1(500);
call p1(600);
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}
DROP PROCEDURE p1;

--echo
--echo # The statement can be binlogged correctly when it is in a prepared statement
--echo
eval PREPARE stm FROM "CREATE $_temporary TABLE IF NOT EXISTS t1 SELECT '123', ?";

let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);
SET @a= 700;
EXECUTE stm USING @a;
SET @a= 800;
EXECUTE stm USING @a;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

--echo
--echo # The statement can be binlogged correctly when it is in a conditional comment
--echo
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo # The whole statement in a conditional comment
eval /*!CREATE $_temporary TABLE IF NOT EXISTS t1
  SELECT 'abc', 900*/;
source include/show_binlog_events.inc;
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo
--echo # There is an long comment before SELECT
eval /*!CREATE $_temporary /*blabla*/ TABLE IF NOT EXISTS t1
  SELECT 'abc', 901*/;
source include/show_binlog_events.inc;
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo
--echo # Conditional comment starts just from SELECT
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  /*!SELECT 'abc',*/ 902;
source include/show_binlog_events.inc;
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo
--echo # Only SELECT keyword is in the conditional comment
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  /*!SELECT*/ /*!'abc',*/ 904;
source include/show_binlog_events.inc;
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo
--echo # Conditional comment is after SELECT keyword
eval CREATE $_temporary TABLE IF NOT EXISTS t1
  SELECT /*!'abc',*/ 903;
source include/show_binlog_events.inc;
let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1);

--echo
--echo # Conditional comment ends just before SELECT keyword
eval /*!CREATE $_temporary TABLE IF NOT EXISTS t1
  */SELECT 'abc', 905;
source include/show_binlog_events.inc;

--sync_slave_with_master
--connection master

if (!$is_temporary)
{
  --let $diff_tables= master:t1,slave:t1
  --source include/diff_tables.inc
}

DROP TABLE t2;
eval DROP $_temporary TABLE t1;

