--source inc.inc

show variables like 'test_sql_discovery%';
set sql_quote_show_create=0;
let $mysqld_datadir= `select @@datadir`;

--error ER_CANT_CREATE_TABLE
create table t1 (a int) engine=test_sql_discovery;

--error ER_NO_SUCH_TABLE
select * from t1;

set @@test_sql_discovery_statement='t1:foobar bwa-ha-ha';
--error ER_NO_SUCH_TABLE
select * from t0;
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

#
# test different invalid discovering statements
#

set @@test_sql_discovery_statement='t1:select 1';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a int primary key) partition by hash(id) partitions 2';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a int) union=(t3,t4)';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 like t2';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 select * from t2';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a int) index directory="/tmp"';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a int) data directory="/tmp"';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a int) engine=myisam';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create temporary table t1 (a int)';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table if not exists t1 (a int)';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

set @@test_sql_discovery_statement='t1:create table t1 (a uint)';
--error ER_SQL_DISCOVER_ERROR
select * from t1;
show warnings;

#
# this should work:
#
set @@test_sql_discovery_statement='t1:create table t1 (a int)';
select * from t1;
show create table t1;
drop table t1;

# table name in the create table statement is ignored
set @@test_sql_discovery_statement='t1:create table t2 (a int)';
select * from t1;
--error ER_NO_SUCH_TABLE
select * from t2;
drop table t1;

set @@test_sql_discovery_statement='t1:create table t1 (a int) engine=test_sql_discovery';
select * from t1;
drop table t1;

# and something more complex
set @@test_sql_discovery_statement='t1:
create table t1 (
  a int not null default 5 primary key,
  b timestamp,
  c tinyblob,
  d decimal(5,2),
  e varchar(30) character set ascii,
  f geometry not null,
  index (d,b),
  unique index (c(10)),
  fulltext (e),
  spatial (f)
) comment="abc" default character set utf8 max_rows=100 min_rows=10 checksum=1';
show status like 'handler_discover';
show create table t1;
show status like 'handler_discover';
--echo ----
--list_files $mysqld_datadir/test t*
--echo ----
show open tables from test;
select * from t1;
show status like 'handler_discover';
flush tables;
select * from t1;
show status like 'handler_discover';
drop table t1;

set @@test_sql_discovery_write_frm=0;
set @@test_sql_discovery_statement='t1:create table t1 (a int)';
show status like 'handler_discover';
show create table t1;
show status like 'handler_discover';
--echo ----
--list_files $mysqld_datadir/test t*
--echo ----
show open tables from test;
select * from t1;
show status like 'handler_discover';
flush tables;
select * from t1;
show status like 'handler_discover';
drop table t1;
show status like 'handler_discover';

