--source include/not_embedded.inc
--source include/have_log_bin.inc

reset master; # clear binlogs

if (!$TEST_SQL_SERVICE_SO) {
  skip No TEST_SQL_SERVICE plugin;
}

# An unfortunate wait for check-testcase.test to complete disconnect.
let count_sessions= 1;
source include/wait_until_count_sessions.inc;

install plugin test_sql_service soname 'test_sql_service';
show status like 'test_sql_service_passed';

set global test_sql_service_execute_sql_global= 'create table test.t1 select 1 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_local=  'insert into test.t1 select 2 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 3 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 4 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 5 as a, @@sql_auto_is_null';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 6 as a, @@sql_auto_is_null';
select * from t1 order by a;
drop table t1;
SET SQL_LOG_BIN=0;

set global test_sql_service_run_test= 1;
show status like 'test_sql_service_passed';

set global test_sql_service_execute_sql_local= 'create table test.t1(id int)';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_local= 'insert into test.t1 values (1), (2)';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_local= 'select * from test.t1';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_local= 'drop table test.t1';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_local= 'drop table test.t1';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_global= 'create table test.t1(id int)';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_global= 'insert into test.t1 values (1), (2)';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_global= 'select * from test.t1';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_global= 'drop table test.t1';
show status like 'test_sql_query_result';

set global test_sql_service_execute_sql_global= 'drop table test.t1';
show status like 'test_sql_query_result';

create table t1 (id int, time timestamp not null default now());
insert into t1 values (1, NULL), (2, NULL), (3, NULL), (4, NULL), (5, NULL);
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
drop table t1;

uninstall plugin test_sql_service;

# Check that statements were executed/binlogged in correct order.
source include/show_binlog_events.inc;
# --replace_column 2 # 5 #
# --replace_regex /xid=[0-9]+/xid=XX/ /GTID [0-9]+-[0-9]+-[0-9]+/GTID #-#-#/
# SHOW BINLOG EVENTS LIMIT 3,100;