--source include/not_embedded.inc
--source include/have_perfschema.inc

--disable_warnings
drop table if exists test.setup_objects;
--enable_warnings

# Make sure only table io / table lock is instrumented and timed
update performance_schema.setup_instruments set enabled='NO';
update performance_schema.setup_instruments set enabled='YES', timed='YES'
  where name in ('wait/io/table/sql/handler', 'wait/lock/table/sql/handler');

# Remove noise from previous tests
truncate table performance_schema.events_waits_history_long;

# Save the setup
create table test.setup_objects as select * from performance_schema.setup_objects;
truncate table performance_schema.setup_objects;

insert into performance_schema.setup_objects
  values ('TABLE', 'db1', 't1', 'YES', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db1', 't2', 'YES', 'NO');

insert into performance_schema.setup_objects
  values ('TABLE', 'db1', '%', 'YES', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db2', 't1', 'YES', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db2', 't2', 'YES', 'NO');

insert into performance_schema.setup_objects
  values ('TABLE', 'db3', 't1', 'YES', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db3', 't2', 'NO', 'NO');

insert into performance_schema.setup_objects
  values ('TABLE', 'db3', '%', 'NO', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db4', 't1', 'NO', 'YES');

insert into performance_schema.setup_objects
  values ('TABLE', 'db4', '%', 'YES', 'NO');

# Also test insert of invalid enum values

--error ER_NO_REFERENCED_ROW_2
insert into performance_schema.setup_objects
  values ('SOMETHING', 'bad1', 'bad1', 'YES', 'NO');
show warnings;

--error ER_NO_REFERENCED_ROW_2
insert into performance_schema.setup_objects
  values ('TABLE', 'bad2', 'bad2', 'MAYBE', 'NO');
show warnings;

--error ER_NO_REFERENCED_ROW_2
insert into performance_schema.setup_objects
  values ('TABLE', 'bad3', 'bad3', 'YES', 'MAYBE NOT');
show warnings;

select * from performance_schema.setup_objects
  order by OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME;

# Also test update of invalid enum values
# Note: do not use SHOW WARNINGS in the test,
# because error messages such as:
#   "Data truncated for column 'OBJECT_TYPE' at row <N>"
# do not always use the same row number <N>.

# ER_WRONG_PERFSCHEMA_USAGE because of privileges, expected.
--error ER_WRONG_PERFSCHEMA_USAGE
update performance_schema.setup_objects
  set OBJECT_TYPE='SOMETHING' where OBJECT_SCHEMA='db1' and OBJECT_NAME='t1';

--error ER_NO_REFERENCED_ROW_2
update performance_schema.setup_objects
  set ENABLED='MAYBE' where OBJECT_SCHEMA='db1' and OBJECT_NAME='t1';

--error ER_NO_REFERENCED_ROW_2
update performance_schema.setup_objects
  set TIMED='MAYBE NOT' where OBJECT_SCHEMA='db1' and OBJECT_NAME='t1';

select * from performance_schema.setup_objects
  order by OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME;

--disable_warnings
drop database if exists db1;
drop database if exists db2;
drop database if exists db3;
drop database if exists db4;
drop database if exists db5;
--enable_warnings

create database db1;
create database db2;
create database db3;
create database db4;
create database db5;

create table db1.t1(a int);
create table db1.t2(a int);
create table db1.t3(a int);
create table db1.t4(a int);
create table db1.t5(a int);

create table db2.t1(a int);
create table db2.t2(a int);
create table db2.t3(a int);
create table db2.t4(a int);
create table db2.t5(a int);

create table db3.t1(a int);
create table db3.t2(a int);
create table db3.t3(a int);
create table db3.t4(a int);
create table db3.t5(a int);

create table db4.t1(a int);
create table db4.t2(a int);
create table db4.t3(a int);
create table db4.t4(a int);
create table db4.t5(a int);

create table db5.t1(a int);

select * from db1.t1;
select * from db1.t2;
select * from db1.t3;
select * from db1.t4;
select * from db1.t5;

select * from db2.t1;
select * from db2.t2;
select * from db2.t3;
select * from db2.t4;
select * from db2.t5;

select * from db3.t1;
select * from db3.t2;
select * from db3.t3;
select * from db3.t4;
select * from db3.t5;

select * from db4.t1;
select * from db4.t2;
select * from db4.t3;
select * from db4.t4;
select * from db4.t5;

select * from db5.t1;

create table db5.t2(a int);
select * from db3.t2;

# Verify what is instrumented
select distinct OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA 
  from performance_schema.events_waits_history_long
  where OBJECT_SCHEMA like "db%"
  group by OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA
  order by OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA;

# Verify what is instrumented and timed
select distinct OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA 
  from performance_schema.events_waits_history_long
  where OBJECT_SCHEMA like "db%" and TIMER_END is not NULL
  group by OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA
  order by OBJECT_TYPE, OBJECT_NAME, OBJECT_SCHEMA;

update performance_schema.setup_objects
  set timed='YES' where OBJECT_SCHEMA = '%';

create table db5.t3(a int);
select * from db3.t3;

truncate table performance_schema.setup_objects;

select count(*) from performance_schema.setup_objects;

drop database db1;
drop database db2;
drop database db3;
drop database db4;
drop database db5;

# Restore the setup
truncate table performance_schema.setup_objects;
insert into performance_schema.setup_objects select * from test.setup_objects;
drop table test.setup_objects;

update performance_schema.setup_instruments set enabled='YES', timed='YES';

