Commit c3542ceb authored by unknown's avatar unknown
Browse files

fix for bug#16642 (Events: No INFORMATION_SCHEMA.EVENTS table)

post-review change - use pointer instead of copy on the stack.
WL#1034 (Internal CRON)
 This patch adds INFORMATION_SCHEMA.EVENTS table with the following format:
  EVENT_CATALOG  - MYSQL_TYPE_STRING  (Always NULL)
  EVENT_SCHEMA   - MYSQL_TYPE_STRING  (the database)
  EVENT_NAME     - MYSQL_TYPE_STRING  (the name)
  DEFINER        - MYSQL_TYPE_STRING  (user@host)
  EVENT_BODY     - MYSQL_TYPE_STRING  (the body from mysql.event)
  EVENT_TYPE     - MYSQL_TYPE_STRING  ("ONE TIME" | "RECURRING")
  EXECUTE_AT     - MYSQL_TYPE_TIMESTAMP (set for "ONE TIME" otherwise NULL)
  INTERVAL_VALUE - MYSQL_TYPE_LONG    (set for RECURRING otherwise NULL)
  INTERVAL_FIELD - MYSQL_TYPE_STRING  (set for RECURRING otherwise NULL)
  SQL_MODE       - MYSQL_TYPE_STRING  (for now NULL)
  STARTS         - MYSQL_TYPE_TIMESTAMP (starts from mysql.event)
  ENDS           - MYSQL_TYPE_TIMESTAMP (ends from mysql.event)
  STATUS         - MYSQL_TYPE_STRING  (ENABLED | DISABLED)
  ON_COMPLETION  - MYSQL_TYPE_STRING  (NOT PRESERVE | PRESERVE)
  CREATED        - MYSQL_TYPE_TIMESTAMP
  LAST_ALTERED   - MYSQL_TYPE_TIMESTAMP
  LAST_EXECUTED  - MYSQL_TYPE_TIMESTAMP
  EVENT_COMMENT  - MYSQL_TYPE_STRING

  SQL_MODE is NULL for now, because the value is still not stored in mysql.event .
Support will be added as a fix for another bug.

 This patch also adds SHOW [FULL] EVENTS [FROM db] [LIKE pattern]
1. SHOW EVENTS shows always only the events on the same user,
   because the PK of mysql.event is (definer, db, name) several 
   users may have event with the same name -> no information disclosure.
2. SHOW FULL EVENTS - shows the events (in the current db as SHOW EVENTS)
   of all users. The user has to have PROCESS privilege, if not then
   SHOW FULL EVENTS behave like SHOW EVENTS.
3. If [FROM db] is specified then this db is considered.
4. Event names can be filtered with LIKE pattern.
  SHOW EVENTS returns table with the following columns, which are subset of
  the data which is returned by SELECT * FROM I_S.EVENTS
   Db
   Name
   Definer 
   Type
   Execute at
   Interval value
   Interval field 
   Starts 
   Ends
   Status


mysql-test/lib/init_db.sql:
  change the PK - (definer, db, name)
  quicker searches when SHOW EVENTS;
  allow also different users to have events with the same name -> 
  no information disclosure
mysql-test/r/events.result:
  result of new tests
mysql-test/r/information_schema.result:
  result of new tests
mysql-test/r/information_schema_db.result:
  result of new tests
mysql-test/r/system_mysql_db.result:
  result of new tests
mysql-test/t/events.test:
  new tests for information_schema.events
scripts/mysql_create_system_tables.sh:
  change the PK of mysql.event to (definer, db, name)
scripts/mysql_fix_privilege_tables.sql:
  change the PK of mysql.event to (definer, db, name)
sql/event.cc:
  pass around the definer of the event because of the new PK
  which is (definer, db, name). It's needed for index searching.
sql/event.h:
  - make enum evex_table_field again public so it can be used
  in sql_show.cc
  - make created and modified ulonglong, because they should be such
  - make public evex_open_event_table so it can be used in sql_show.cc
sql/event_executor.cc:
  - cosmetics
sql/event_priv.h:
  - moved enum evex_table_field and evex_open_event_table()
    to event.h (made them therefore public)
sql/event_timed.cc:
  - in event_timed::init_definer() always fill this.definer with
    the concatenated value of definer_user@definer_host. Makes
    later the work easier.
  - pass around the definer wherever is needed for searching 
    (new prototype of evex_db_find_evex_aux)
sql/mysqld.cc:
  - add counter for SHOW EVENTS
sql/sql_lex.h:
  - register SHOW EVENTS as command
sql/sql_parse.cc:
  - handle SCH_EVENTS (I_S.EVENTS like SCH_TRIGGERS)
  - make additional check in case of SHOW EVENTS (check for EVENT on
    the current database. if it is null check_access() gives appropriate
    message back.
sql/sql_show.cc:
  - add INFORMATION_SCHEMA.EVENTS and SHOW EVENTS
  - I_S.EVENTS.SQL_MODE is NULL for now -> not implemented. Trudy
    asked to be added so bug #16642 can be completely closed. There
    is another bug report which will fix the lack of storage of
    SQL_MODE during event creation.
sql/sql_yacc.yy:
  - always call event_timed::init_definer() when CREATE/ALTER/DROP
    EVENT but not when just compiling the body of the event because
    in this case this operation is not needed, it takes memory and
    CPU time and at the end the result is not used. event_timed::definer
    is used only on SQLCOM_CREATE/ALTER/DROP_EVENT execution not on
    statement compilation.
  - add SHOW [FULL] EVENTS [FROM db] [LIKE pattern]
    in case of FULL and the user has PROCESS privilege then he will see
    also others' events in the current database, otherwise the output
    is the same as of SHOW EVENTS. Because the events are per DB only
    the events from the current database are shown. pattern is applied
    against event name. FROM db is self explanatory.
sql/table.h:
  add SCH_EVENTS as part of INFORMATION_SCHEMA
parent 098af0ae
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -596,7 +596,7 @@ CREATE TABLE event (
  status ENUM('ENABLED','DISABLED') NOT NULL default 'ENABLED',
  on_completion ENUM('DROP','PRESERVE') NOT NULL default 'DROP',
  comment varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL default '',
  PRIMARY KEY  (db,name)
  PRIMARY KEY  (definer, db, name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT 'Events';

CREATE DATABASE IF NOT EXISTS cluster_replication;
+96 −0
Original line number Diff line number Diff line
@@ -27,6 +27,102 @@ set event_scheduler=0;
ERROR HY000: Variable 'event_scheduler' is a GLOBAL variable and should be set with SET GLOBAL
set global event_scheduler=2;
ERROR 42000: Variable 'event_scheduler' can't be set to the value of '2'
create event one_event on schedule every 10 second do select 123;
SHOW EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	one_event	root@localhost	RECURRING	NULL	10	INTERVAL_SECOND	#	#	ENABLED
SELECT EVENT_CATALOG, EVENT_SCHEMA, EVENT_NAME, DEFINER, EVENT_BODY, EVENT_TYPE, EXECUTE_AT, INTERVAL_VALUE, INTERVAL_FIELD, STATUS,ON_COMPLETION, EVENT_COMMENT from information_schema.events;
EVENT_CATALOG	EVENT_SCHEMA	EVENT_NAME	DEFINER	EVENT_BODY	EVENT_TYPE	EXECUTE_AT	INTERVAL_VALUE	INTERVAL_FIELD	STATUS	ON_COMPLETION	EVENT_COMMENT
NULL	events_test	one_event	root@localhost	 select 123	RECURRING	NULL	10	INTERVAL_SECOND	ENABLED	NOT PRESERVE	
CREATE DATABASE events_test2;
CREATE USER ev_test@localhost;
GRANT ALL ON events_test.* to ev_test@localhost;
GRANT ALL on events_test2.* to ev_test@localhost;
REVOKE EVENT ON events_test2.* FROM ev_test@localhost;
REVOKE PROCESS on *.* from ev_test@localhost;
select "NEW CONNECTION";
NEW CONNECTION
NEW CONNECTION
SELECT USER(), DATABASE();
USER()	DATABASE()
ev_test@localhost	events_test2
SHOW GRANTS;
Grants for ev_test@localhost
GRANT USAGE ON *.* TO 'ev_test'@'localhost'
GRANT ALL PRIVILEGES ON `events_test`.* TO 'ev_test'@'localhost'
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE ON `events_test2`.* TO 'ev_test'@'localhost'
select "Here comes an error:";
Here comes an error:
Here comes an error:
SHOW EVENTS;
ERROR 42000: Access denied for user 'ev_test'@'localhost' to database 'events_test2'
USE events_test;
select "Now the list should be empty:";
Now the list should be empty:
Now the list should be empty:
SHOW EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
select concat("Let's create some new events from the name of ",user());
concat("Let's create some new events from the name of ",user())
Let's create some new events from the name of ev_test@localhost
create event one_event on schedule every 20 second do select 123;
create event two_event on schedule every 20 second on completion not preserve comment "two event" do select 123;
create event three_event on schedule every 20 second on completion preserve comment "three event" do select 123;
select "Now we should see 3 events:";
Now we should see 3 events:
Now we should see 3 events:
SHOW EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	one_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	three_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	two_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
select "This should show us only 3 events:";
This should show us only 3 events:
This should show us only 3 events:
SHOW FULL EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	one_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	three_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	two_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
select "This should show us only 2 events:";
This should show us only 2 events:
This should show us only 2 events:
SHOW FULL EVENTS LIKE 't%event';
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	three_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	two_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
select "This should show us no events:";
This should show us no events:
This should show us no events:
SHOW FULL EVENTS FROM test LIKE '%';
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
DROP DATABASE events_test2;
select "should see 1 event:";
should see 1 event:
should see 1 event:
SHOW EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	one_event	root@localhost	RECURRING	NULL	10	INTERVAL_SECOND	#	#	ENABLED
select "we should see 4 events now:";
we should see 4 events now:
we should see 4 events now:
SHOW FULL EVENTS;
Db	Name	Definer	Type	Execute at	Interval value	Interval field	Starts	Ends	Status
events_test	one_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	three_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	two_event	ev_test@localhost	RECURRING	NULL	20	INTERVAL_SECOND	#	#	ENABLED
events_test	one_event	root@localhost	RECURRING	NULL	10	INTERVAL_SECOND	#	#	ENABLED
SELECT EVENT_CATALOG, EVENT_SCHEMA, EVENT_NAME, DEFINER, EVENT_BODY, EVENT_TYPE, EXECUTE_AT, INTERVAL_VALUE, INTERVAL_FIELD, STATUS,ON_COMPLETION, EVENT_COMMENT from information_schema.events;
EVENT_CATALOG	EVENT_SCHEMA	EVENT_NAME	DEFINER	EVENT_BODY	EVENT_TYPE	EXECUTE_AT	INTERVAL_VALUE	INTERVAL_FIELD	STATUS	ON_COMPLETION	EVENT_COMMENT
NULL	events_test	one_event	ev_test@localhost	 select 123	RECURRING	NULL	20	INTERVAL_SECOND	ENABLED	NOT PRESERVE	
NULL	events_test	three_event	ev_test@localhost	 select 123	RECURRING	NULL	20	INTERVAL_SECOND	ENABLED	PRESERVE	three event
NULL	events_test	two_event	ev_test@localhost	 select 123	RECURRING	NULL	20	INTERVAL_SECOND	ENABLED	NOT PRESERVE	two event
NULL	events_test	one_event	root@localhost	 select 123	RECURRING	NULL	10	INTERVAL_SECOND	ENABLED	NOT PRESERVE	
drop event one_event;
drop event two_event;
drop event three_event;
drop user ev_test@localhost;
drop event one_event;
set global event_scheduler=0;
select count(*) from mysql.event;
count(*)
+11 −2
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
COLUMN_PRIVILEGES
ENGINES
EVENTS
KEY_COLUMN_USAGE
PARTITIONS
PLUGINS
@@ -734,7 +735,7 @@ CREATE TABLE t_crashme ( f1 BIGINT);
CREATE VIEW a1 (t_CRASHME) AS SELECT f1 FROM t_crashme GROUP BY f1;
CREATE VIEW a2 AS SELECT t_CRASHME FROM a1;
count(*)
109
110
drop view a2, a1;
drop table t_crashme;
select table_schema,table_name, column_name from
@@ -742,6 +743,8 @@ information_schema.columns
where data_type = 'longtext';
table_schema	table_name	column_name
information_schema	COLUMNS	COLUMN_TYPE
information_schema	EVENTS	EVENT_BODY
information_schema	EVENTS	SQL_MODE
information_schema	PARTITIONS	PARTITION_EXPRESSION
information_schema	PARTITIONS	SUBPARTITION_EXPRESSION
information_schema	PARTITIONS	PARTITION_DESCRIPTION
@@ -756,6 +759,12 @@ information_schema VIEWS VIEW_DEFINITION
select table_name, column_name, data_type from information_schema.columns
where data_type = 'datetime';
table_name	column_name	data_type
EVENTS	EXECUTE_AT	datetime
EVENTS	STARTS	datetime
EVENTS	ENDS	datetime
EVENTS	CREATED	datetime
EVENTS	LAST_ALTERED	datetime
EVENTS	LAST_EXECUTED	datetime
PARTITIONS	CREATE_TIME	datetime
PARTITIONS	UPDATE_TIME	datetime
PARTITIONS	CHECK_TIME	datetime
@@ -817,7 +826,7 @@ flush privileges;
SELECT table_schema, count(*) FROM information_schema.TABLES GROUP BY TABLE_SCHEMA;
table_schema	count(*)
cluster_replication	1
information_schema	19
information_schema	20
mysql	21
create table t1 (i int, j int);
create trigger trg1 before insert on t1 for each row
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
COLUMN_PRIVILEGES
ENGINES
EVENTS
KEY_COLUMN_USAGE
PARTITIONS
PLUGINS
+20 −20
Original line number Diff line number Diff line
@@ -186,6 +186,26 @@ proc CREATE TABLE `proc` (
  `comment` char(64) character set utf8 collate utf8_bin NOT NULL default '',
  PRIMARY KEY  (`db`,`name`,`type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Stored Procedures'
show create table event;
Table	Create Table
event	CREATE TABLE `event` (
  `db` char(64) character set utf8 collate utf8_bin NOT NULL default '',
  `name` char(64) character set utf8 collate utf8_bin NOT NULL default '',
  `body` longblob NOT NULL,
  `definer` char(77) character set utf8 collate utf8_bin NOT NULL default '',
  `execute_at` datetime default NULL,
  `interval_value` int(11) default NULL,
  `interval_field` enum('YEAR','QUARTER','MONTH','DAY','HOUR','MINUTE','WEEK','SECOND','MICROSECOND','YEAR_MONTH','DAY_HOUR','DAY_MINUTE','DAY_SECOND','HOUR_MINUTE','HOUR_SECOND','MINUTE_SECOND','DAY_MICROSECOND','HOUR_MICROSECOND','MINUTE_MICROSECOND','SECOND_MICROSECOND') default NULL,
  `created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL default '0000-00-00 00:00:00',
  `last_executed` datetime default NULL,
  `starts` datetime default NULL,
  `ends` datetime default NULL,
  `status` enum('ENABLED','DISABLED') NOT NULL default 'ENABLED',
  `on_completion` enum('DROP','PRESERVE') NOT NULL default 'DROP',
  `comment` varchar(64) character set utf8 collate utf8_bin NOT NULL default '',
  PRIMARY KEY  (`definer`,`db`,`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Events'
show create table general_log;
Table	Create Table
general_log	CREATE TABLE `general_log` (
@@ -211,25 +231,5 @@ slow_log CREATE TABLE `slow_log` (
  `server_id` int(11) default NULL,
  `sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
show create table event;
Table	Create Table
event	CREATE TABLE `event` (
  `db` char(64) character set utf8 collate utf8_bin NOT NULL default '',
  `name` char(64) character set utf8 collate utf8_bin NOT NULL default '',
  `body` longblob NOT NULL,
  `definer` char(77) character set utf8 collate utf8_bin NOT NULL default '',
  `execute_at` datetime default NULL,
  `interval_value` int(11) default NULL,
  `interval_field` enum('YEAR','QUARTER','MONTH','DAY','HOUR','MINUTE','WEEK','SECOND','MICROSECOND','YEAR_MONTH','DAY_HOUR','DAY_MINUTE','DAY_SECOND','HOUR_MINUTE','HOUR_SECOND','MINUTE_SECOND','DAY_MICROSECOND','HOUR_MICROSECOND','MINUTE_MICROSECOND','SECOND_MICROSECOND') default NULL,
  `created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL default '0000-00-00 00:00:00',
  `last_executed` datetime default NULL,
  `starts` datetime default NULL,
  `ends` datetime default NULL,
  `status` enum('ENABLED','DISABLED') NOT NULL default 'ENABLED',
  `on_completion` enum('DROP','PRESERVE') NOT NULL default 'DROP',
  `comment` varchar(64) character set utf8 collate utf8_bin NOT NULL default '',
  PRIMARY KEY  (`db`,`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Events'
show tables;
Tables_in_test
Loading