--source ../include/direct_left_right_join_nullable_init.inc

--echo
--echo drop and create databases
--connection master_1
--disable_warnings
DROP DATABASE IF EXISTS auto_test_local;
CREATE DATABASE auto_test_local;
USE auto_test_local;
  --connection child2_1
    SET @old_log_output = @@global.log_output;
    SET GLOBAL log_output = 'TABLE,FILE';
  DROP DATABASE IF EXISTS auto_test_remote;
  CREATE DATABASE auto_test_remote;
  USE auto_test_remote;
--enable_warnings

--echo
--echo test select 1
--connection master_1
SELECT 1;
  --connection child2_1
  SELECT 1;

--echo
--echo create table and insert
  --connection child2_1
    --disable_query_log
    echo CHILD2_1_DROP_TABLES;
    echo CHILD2_1_DROP_TABLES6;
    echo CHILD2_1_DROP_TABLES4;
    echo CHILD2_1_DROP_TABLES3;
    echo CHILD2_1_CREATE_TABLES;
    echo CHILD2_1_CREATE_TABLES6;
    echo CHILD2_1_CREATE_TABLES4;
    echo CHILD2_1_CREATE_TABLES3;
  --disable_warnings
DROP TABLE IF EXISTS ta_r;
DROP TABLE IF EXISTS ta_r_3;
DROP TABLE IF EXISTS ta_r_auto_inc;
DROP TABLE IF EXISTS ta_r_no_idx;
  --enable_warnings
CREATE TABLE ta_r (
a INT DEFAULT 10,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10',
PRIMARY KEY(a),
KEY idx1(b)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE ta_r_3 (
a INT DEFAULT 10,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10'
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE ta_r_auto_inc (
a INT AUTO_INCREMENT,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10',
PRIMARY KEY(a)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE ta_r_no_idx (
a INT DEFAULT 10,
b CHAR(1) DEFAULT 'c',
c DATETIME DEFAULT '1999-10-10 10:10:10'
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    --enable_query_log
    TRUNCATE TABLE mysql.general_log;
--connection master_1
--disable_warnings
DROP TABLE IF EXISTS tbl_a;
--enable_warnings
--disable_query_log
echo CREATE TABLE tbl_a (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10',
    KEY idx0(a),
    KEY idx1(b)
) MASTER_1_ENGINE MASTER_1_CHARSET MASTER_1_COMMENT_2_1;
eval
CREATE TABLE tbl_a (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10',
    KEY idx0(a),
    KEY idx1(b)
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='database "auto_test_remote", table "ta_r"'
    CONNECTION='host "localhost", socket "$CHILD2_1_MYSOCK", user "root",
    password ""';
echo CREATE TABLE tbl_b (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10'
) MASTER_1_ENGINE MASTER_1_CHARSET MASTER_1_COMMENT5_2_1;
eval
CREATE TABLE tbl_b (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10'
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='database "auto_test_remote", table "ta_r_3"'
    CONNECTION='host "localhost", socket "$CHILD2_1_MYSOCK", user "root",
    password ""';
echo CREATE TABLE tbl_c (
    a INT AUTO_INCREMENT,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10',
    KEY idx0(a),
    KEY idx1(b),
    KEY idx2(c)
) MASTER_1_ENGINE MASTER_1_CHARSET MASTER_1_COMMENT3_2_1;
eval
CREATE TABLE tbl_c (
    a INT AUTO_INCREMENT,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10',
    KEY idx0(a),
    KEY idx1(b),
    KEY idx2(c)
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='database "auto_test_remote", table "ta_r_auto_inc"'
    CONNECTION='host "localhost", socket "$CHILD2_1_MYSOCK", user "root",
    password ""';
echo CREATE TABLE tbl_d (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10'
) MASTER_1_ENGINE MASTER_1_CHARSET MASTER_1_COMMENT2_2_1;
eval
CREATE TABLE tbl_d (
    a INT DEFAULT 10,
    b CHAR(1) DEFAULT 'c',
    c DATETIME DEFAULT '1999-10-10 10:10:10'
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='database "auto_test_remote", table "ta_r_no_idx"'
    CONNECTION='host "localhost", socket "$CHILD2_1_MYSOCK", user "root",
    password "", prt "2000000"';
--enable_query_log
--disable_ps_protocol
insert into tbl_a values (1,'a','2000/01/01'),(2,'b','2000/01/02');
insert into tbl_b values (1,'a','2000/01/01'),(2,'b','2000/01/02'),(3,'c','2000/01/03');
insert into tbl_c values (1,'a','2000/01/01'),(2,'b','2000/01/02'),(3,'c','2000/01/03'),(4,'d','2000/01/04');
insert into tbl_d values (1,'a','2000/01/01'),(2,'b','2000/01/02'),(3,'c','2000/01/03'),(4,'d','2000/01/04'),(5,'e','2000/01/05');
--enable_ps_protocol

--echo
--echo select test
  --connection child2_1
    TRUNCATE TABLE mysql.general_log;

--connection master_1
--disable_ps2_protocol
SELECT a.a, c.b, c.c, d.a FROM tbl_a a left join tbl_b b on a.a = b.a left join tbl_c c on b.c = c.c right join tbl_d d on c.b = d.b ORDER BY d.a DESC;

  --connection child2_1
    --disable_view_protocol
SELECT argument FROM mysql.general_log WHERE command_type != 'Execute' AND argument LIKE '%select %';
    --enable_view_protocol
SELECT a, b, date_format(c, '%Y-%m-%d %H:%i:%s') FROM ta_r ORDER BY a;
--enable_ps2_protocol

--echo
--echo deinit
--disable_warnings
--connection master_1
DROP DATABASE IF EXISTS auto_test_local;
  --connection child2_1
  DROP DATABASE IF EXISTS auto_test_remote;
    SET GLOBAL log_output = @old_log_output;
--enable_warnings
--source ../include/direct_left_right_join_nullable_deinit.inc
--echo
--echo end of test
