You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1571 lines
43 KiB

Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
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
20 years ago
Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
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
20 years ago
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
20 years ago
20 years ago
20 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
20 years ago
Patch for the following bugs: - BUG#11986: Stored routines and triggers can fail if the code has a non-ascii symbol - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars - BUG#19443: INFORMATION_SCHEMA does not support charsets properly - BUG#21249: Character set of SP-var can be ignored - BUG#25212: Character set of string constant is ignored (stored routines) - BUG#25221: Character set of string constant is ignored (triggers) There were a few general problems that caused these bugs: 1. Character set information of the original (definition) query for views, triggers, stored routines and events was lost. 2. mysqldump output query in client character set, which can be inappropriate to encode definition-query. 3. INFORMATION_SCHEMA used strings with mixed encodings to display object definition; 1. No query-definition-character set. In order to compile query into execution code, some extra data (such as environment variables or the database character set) is used. The problem here was that this context was not preserved. So, on the next load it can differ from the original one, thus the result will be different. The context contains the following data: - client character set; - connection collation (character set and collation); - collation of the owner database; The fix is to store this context and use it each time we parse (compile) and execute the object (stored routine, trigger, ...). 2. Wrong mysqldump-output. The original query can contain several encodings (by means of character set introducers). The problem here was that we tried to convert original query to the mysqldump-client character set. Moreover, we stored queries in different character sets for different objects (views, for one, used UTF8, triggers used original character set). The solution is - to store definition queries in the original character set; - to change SHOW CREATE statement to output definition query in the binary character set (i.e. without any conversion); - introduce SHOW CREATE TRIGGER statement; - to dump special statements to switch the context to the original one before dumping and restore it afterwards. Note, in order to preserve the database collation at the creation time, additional ALTER DATABASE might be used (to temporary switch the database collation back to the original value). In this case, ALTER DATABASE privilege will be required. This is a backward-incompatible change. 3. INFORMATION_SCHEMA showed non-UTF8 strings The fix is to generate UTF8-query during the parsing, store it in the object and show it in the INFORMATION_SCHEMA. Basically, the idea is to create a copy of the original query convert it to UTF8. Character set introducers are removed and all text literals are converted to UTF8. This UTF8 query is intended to provide user-readable output. It must not be used to recreate the object. Specialized SHOW CREATE statements should be used for this. The reason for this limitation is the following: the original query can contain symbols from several character sets (by means of character set introducers). Example: - original query: CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1; - UTF8 query (for INFORMATION_SCHEMA): CREATE VIEW v1 AS SELECT 'Hello' AS c1;
19 years ago
  1. /* Copyright (C) 2004-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #define MYSQL_LEX 1
  13. #include "mysql_priv.h"
  14. #include "events.h"
  15. #include "event_data_objects.h"
  16. #include "event_db_repository.h"
  17. #include "sp_head.h"
  18. /**
  19. @addtogroup Event_Scheduler
  20. @{
  21. */
  22. /*************************************************************************/
  23. /**
  24. Event_creation_ctx -- creation context of events.
  25. */
  26. class Event_creation_ctx :public Stored_program_creation_ctx,
  27. public Sql_alloc
  28. {
  29. public:
  30. static bool load_from_db(THD *thd,
  31. MEM_ROOT *event_mem_root,
  32. const char *db_name,
  33. const char *event_name,
  34. TABLE *event_tbl,
  35. Stored_program_creation_ctx **ctx);
  36. public:
  37. virtual Stored_program_creation_ctx *clone(MEM_ROOT *mem_root)
  38. {
  39. return new (mem_root)
  40. Event_creation_ctx(m_client_cs, m_connection_cl, m_db_cl);
  41. }
  42. protected:
  43. virtual Object_creation_ctx *create_backup_ctx(THD *thd) const
  44. {
  45. /*
  46. We can avoid usual backup/restore employed in stored programs since we
  47. know that this is a top level statement and the worker thread is
  48. allocated exclusively to execute this event.
  49. */
  50. return NULL;
  51. }
  52. private:
  53. Event_creation_ctx(CHARSET_INFO *client_cs,
  54. CHARSET_INFO *connection_cl,
  55. CHARSET_INFO *db_cl)
  56. : Stored_program_creation_ctx(client_cs, connection_cl, db_cl)
  57. { }
  58. };
  59. /**************************************************************************
  60. Event_creation_ctx implementation.
  61. **************************************************************************/
  62. bool
  63. Event_creation_ctx::load_from_db(THD *thd,
  64. MEM_ROOT *event_mem_root,
  65. const char *db_name,
  66. const char *event_name,
  67. TABLE *event_tbl,
  68. Stored_program_creation_ctx **ctx)
  69. {
  70. /* Load character set/collation attributes. */
  71. CHARSET_INFO *client_cs;
  72. CHARSET_INFO *connection_cl;
  73. CHARSET_INFO *db_cl;
  74. bool invalid_creation_ctx= FALSE;
  75. if (load_charset(event_mem_root,
  76. event_tbl->field[ET_FIELD_CHARACTER_SET_CLIENT],
  77. thd->variables.character_set_client,
  78. &client_cs))
  79. {
  80. sql_print_warning("Event '%s'.'%s': invalid value "
  81. "in column mysql.event.character_set_client.",
  82. (const char *) db_name,
  83. (const char *) event_name);
  84. invalid_creation_ctx= TRUE;
  85. }
  86. if (load_collation(event_mem_root,
  87. event_tbl->field[ET_FIELD_COLLATION_CONNECTION],
  88. thd->variables.collation_connection,
  89. &connection_cl))
  90. {
  91. sql_print_warning("Event '%s'.'%s': invalid value "
  92. "in column mysql.event.collation_connection.",
  93. (const char *) db_name,
  94. (const char *) event_name);
  95. invalid_creation_ctx= TRUE;
  96. }
  97. if (load_collation(event_mem_root,
  98. event_tbl->field[ET_FIELD_DB_COLLATION],
  99. NULL,
  100. &db_cl))
  101. {
  102. sql_print_warning("Event '%s'.'%s': invalid value "
  103. "in column mysql.event.db_collation.",
  104. (const char *) db_name,
  105. (const char *) event_name);
  106. invalid_creation_ctx= TRUE;
  107. }
  108. /*
  109. If we failed to resolve the database collation, load the default one
  110. from the disk.
  111. */
  112. if (!db_cl)
  113. db_cl= get_default_db_collation(thd, db_name);
  114. /* Create the context. */
  115. *ctx= new Event_creation_ctx(client_cs, connection_cl, db_cl);
  116. return invalid_creation_ctx;
  117. }
  118. /*************************************************************************/
  119. /*
  120. Initiliazes dbname and name of an Event_queue_element_for_exec
  121. object
  122. SYNOPSIS
  123. Event_queue_element_for_exec::init()
  124. RETURN VALUE
  125. FALSE OK
  126. TRUE Error (OOM)
  127. */
  128. bool
  129. Event_queue_element_for_exec::init(LEX_STRING db, LEX_STRING n)
  130. {
  131. if (!(dbname.str= my_strndup(db.str, dbname.length= db.length, MYF(MY_WME))))
  132. return TRUE;
  133. if (!(name.str= my_strndup(n.str, name.length= n.length, MYF(MY_WME))))
  134. {
  135. my_free((uchar*) dbname.str, MYF(0));
  136. return TRUE;
  137. }
  138. return FALSE;
  139. }
  140. /*
  141. Destructor
  142. SYNOPSIS
  143. Event_queue_element_for_exec::~Event_queue_element_for_exec()
  144. */
  145. Event_queue_element_for_exec::~Event_queue_element_for_exec()
  146. {
  147. my_free((uchar*) dbname.str, MYF(0));
  148. my_free((uchar*) name.str, MYF(0));
  149. }
  150. /*
  151. Constructor
  152. SYNOPSIS
  153. Event_basic::Event_basic()
  154. */
  155. Event_basic::Event_basic()
  156. {
  157. DBUG_ENTER("Event_basic::Event_basic");
  158. /* init memory root */
  159. init_alloc_root(&mem_root, 256, 512);
  160. dbname.str= name.str= NULL;
  161. dbname.length= name.length= 0;
  162. time_zone= NULL;
  163. DBUG_VOID_RETURN;
  164. }
  165. /*
  166. Destructor
  167. SYNOPSIS
  168. Event_basic::Event_basic()
  169. */
  170. Event_basic::~Event_basic()
  171. {
  172. DBUG_ENTER("Event_basic::~Event_basic");
  173. free_root(&mem_root, MYF(0));
  174. DBUG_VOID_RETURN;
  175. }
  176. /*
  177. Short function to load a char column into a LEX_STRING
  178. SYNOPSIS
  179. Event_basic::load_string_field()
  180. field_name The field( enum_events_table_field is not actually used
  181. because it's unknown in event_data_objects.h)
  182. fields The Field array
  183. field_value The value
  184. */
  185. bool
  186. Event_basic::load_string_fields(Field **fields, ...)
  187. {
  188. bool ret= FALSE;
  189. va_list args;
  190. enum enum_events_table_field field_name;
  191. LEX_STRING *field_value;
  192. DBUG_ENTER("Event_basic::load_string_fields");
  193. va_start(args, fields);
  194. field_name= (enum enum_events_table_field) va_arg(args, int);
  195. while (field_name < ET_FIELD_COUNT)
  196. {
  197. field_value= va_arg(args, LEX_STRING *);
  198. if ((field_value->str= get_field(&mem_root, fields[field_name])) == NullS)
  199. {
  200. ret= TRUE;
  201. break;
  202. }
  203. field_value->length= strlen(field_value->str);
  204. field_name= (enum enum_events_table_field) va_arg(args, int);
  205. }
  206. va_end(args);
  207. DBUG_RETURN(ret);
  208. }
  209. bool
  210. Event_basic::load_time_zone(THD *thd, const LEX_STRING tz_name)
  211. {
  212. String str(tz_name.str, &my_charset_latin1);
  213. time_zone= my_tz_find(thd, &str);
  214. return (time_zone == NULL);
  215. }
  216. /*
  217. Constructor
  218. SYNOPSIS
  219. Event_queue_element::Event_queue_element()
  220. */
  221. Event_queue_element::Event_queue_element():
  222. status_changed(FALSE), last_executed_changed(FALSE),
  223. on_completion(Event_parse_data::ON_COMPLETION_DROP),
  224. status(Event_parse_data::ENABLED), expression(0), dropped(FALSE),
  225. execution_count(0)
  226. {
  227. DBUG_ENTER("Event_queue_element::Event_queue_element");
  228. starts= ends= execute_at= last_executed= 0;
  229. starts_null= ends_null= execute_at_null= TRUE;
  230. DBUG_VOID_RETURN;
  231. }
  232. /*
  233. Destructor
  234. SYNOPSIS
  235. Event_queue_element::Event_queue_element()
  236. */
  237. Event_queue_element::~Event_queue_element()
  238. {
  239. }
  240. /*
  241. Constructor
  242. SYNOPSIS
  243. Event_timed::Event_timed()
  244. */
  245. Event_timed::Event_timed():
  246. created(0), modified(0), sql_mode(0)
  247. {
  248. DBUG_ENTER("Event_timed::Event_timed");
  249. init();
  250. DBUG_VOID_RETURN;
  251. }
  252. /*
  253. Destructor
  254. SYNOPSIS
  255. Event_timed::~Event_timed()
  256. */
  257. Event_timed::~Event_timed()
  258. {
  259. }
  260. /*
  261. Constructor
  262. SYNOPSIS
  263. Event_job_data::Event_job_data()
  264. */
  265. Event_job_data::Event_job_data()
  266. :sql_mode(0)
  267. {
  268. }
  269. /*
  270. Init all member variables
  271. SYNOPSIS
  272. Event_timed::init()
  273. */
  274. void
  275. Event_timed::init()
  276. {
  277. DBUG_ENTER("Event_timed::init");
  278. definer_user.str= definer_host.str= body.str= comment.str= NULL;
  279. definer_user.length= definer_host.length= body.length= comment.length= 0;
  280. sql_mode= 0;
  281. DBUG_VOID_RETURN;
  282. }
  283. /**
  284. Load an event's body from a row from mysql.event.
  285. @details This method is silent on errors and should behave like that.
  286. Callers should handle throwing of error messages. The reason is that the
  287. class should not know about how to deal with communication.
  288. @return Operation status
  289. @retval FALSE OK
  290. @retval TRUE Error
  291. */
  292. bool
  293. Event_job_data::load_from_row(THD *thd, TABLE *table)
  294. {
  295. char *ptr;
  296. size_t len;
  297. LEX_STRING tz_name;
  298. DBUG_ENTER("Event_job_data::load_from_row");
  299. if (!table)
  300. DBUG_RETURN(TRUE);
  301. if (table->s->fields < ET_FIELD_COUNT)
  302. DBUG_RETURN(TRUE);
  303. if (load_string_fields(table->field,
  304. ET_FIELD_DB, &dbname,
  305. ET_FIELD_NAME, &name,
  306. ET_FIELD_BODY, &body,
  307. ET_FIELD_DEFINER, &definer,
  308. ET_FIELD_TIME_ZONE, &tz_name,
  309. ET_FIELD_COUNT))
  310. DBUG_RETURN(TRUE);
  311. if (load_time_zone(thd, tz_name))
  312. DBUG_RETURN(TRUE);
  313. Event_creation_ctx::load_from_db(thd, &mem_root, dbname.str, name.str, table,
  314. &creation_ctx);
  315. ptr= strchr(definer.str, '@');
  316. if (! ptr)
  317. ptr= definer.str;
  318. len= ptr - definer.str;
  319. definer_user.str= strmake_root(&mem_root, definer.str, len);
  320. definer_user.length= len;
  321. len= definer.length - len - 1;
  322. /* 1:because of @ */
  323. definer_host.str= strmake_root(&mem_root, ptr + 1, len);
  324. definer_host.length= len;
  325. sql_mode= (ulong) table->field[ET_FIELD_SQL_MODE]->val_int();
  326. DBUG_RETURN(FALSE);
  327. }
  328. /**
  329. Load an event's body from a row from mysql.event.
  330. @details This method is silent on errors and should behave like that.
  331. Callers should handle throwing of error messages. The reason is that the
  332. class should not know about how to deal with communication.
  333. @return Operation status
  334. @retval FALSE OK
  335. @retval TRUE Error
  336. */
  337. bool
  338. Event_queue_element::load_from_row(THD *thd, TABLE *table)
  339. {
  340. char *ptr;
  341. MYSQL_TIME time;
  342. LEX_STRING tz_name;
  343. DBUG_ENTER("Event_queue_element::load_from_row");
  344. if (!table)
  345. DBUG_RETURN(TRUE);
  346. if (table->s->fields < ET_FIELD_COUNT)
  347. DBUG_RETURN(TRUE);
  348. if (load_string_fields(table->field,
  349. ET_FIELD_DB, &dbname,
  350. ET_FIELD_NAME, &name,
  351. ET_FIELD_DEFINER, &definer,
  352. ET_FIELD_TIME_ZONE, &tz_name,
  353. ET_FIELD_COUNT))
  354. DBUG_RETURN(TRUE);
  355. if (load_time_zone(thd, tz_name))
  356. DBUG_RETURN(TRUE);
  357. starts_null= table->field[ET_FIELD_STARTS]->is_null();
  358. my_bool not_used= FALSE;
  359. if (!starts_null)
  360. {
  361. table->field[ET_FIELD_STARTS]->get_date(&time, TIME_NO_ZERO_DATE);
  362. starts= my_tz_OFFSET0->TIME_to_gmt_sec(&time,&not_used);
  363. }
  364. ends_null= table->field[ET_FIELD_ENDS]->is_null();
  365. if (!ends_null)
  366. {
  367. table->field[ET_FIELD_ENDS]->get_date(&time, TIME_NO_ZERO_DATE);
  368. ends= my_tz_OFFSET0->TIME_to_gmt_sec(&time,&not_used);
  369. }
  370. if (!table->field[ET_FIELD_INTERVAL_EXPR]->is_null())
  371. expression= table->field[ET_FIELD_INTERVAL_EXPR]->val_int();
  372. else
  373. expression= 0;
  374. /*
  375. If neigher STARTS and ENDS is set, then both fields are empty.
  376. Hence, if ET_FIELD_EXECUTE_AT is empty there is an error.
  377. */
  378. execute_at_null= table->field[ET_FIELD_EXECUTE_AT]->is_null();
  379. DBUG_ASSERT(!(starts_null && ends_null && !expression && execute_at_null));
  380. if (!expression && !execute_at_null)
  381. {
  382. if (table->field[ET_FIELD_EXECUTE_AT]->get_date(&time,
  383. TIME_NO_ZERO_DATE))
  384. DBUG_RETURN(TRUE);
  385. execute_at= my_tz_OFFSET0->TIME_to_gmt_sec(&time,&not_used);
  386. }
  387. /*
  388. We load the interval type from disk as string and then map it to
  389. an integer. This decouples the values of enum interval_type
  390. and values actually stored on disk. Therefore the type can be
  391. reordered without risking incompatibilities of data between versions.
  392. */
  393. if (!table->field[ET_FIELD_TRANSIENT_INTERVAL]->is_null())
  394. {
  395. int i;
  396. char buff[MAX_FIELD_WIDTH];
  397. String str(buff, sizeof(buff), &my_charset_bin);
  398. LEX_STRING tmp;
  399. table->field[ET_FIELD_TRANSIENT_INTERVAL]->val_str(&str);
  400. if (!(tmp.length= str.length()))
  401. DBUG_RETURN(TRUE);
  402. tmp.str= str.c_ptr_safe();
  403. i= find_string_in_array(interval_type_to_name, &tmp, system_charset_info);
  404. if (i < 0)
  405. DBUG_RETURN(TRUE);
  406. interval= (interval_type) i;
  407. }
  408. if (!table->field[ET_FIELD_LAST_EXECUTED]->is_null())
  409. {
  410. table->field[ET_FIELD_LAST_EXECUTED]->get_date(&time,
  411. TIME_NO_ZERO_DATE);
  412. last_executed= my_tz_OFFSET0->TIME_to_gmt_sec(&time,&not_used);
  413. }
  414. last_executed_changed= FALSE;
  415. if ((ptr= get_field(&mem_root, table->field[ET_FIELD_STATUS])) == NullS)
  416. DBUG_RETURN(TRUE);
  417. DBUG_PRINT("load_from_row", ("Event [%s] is [%s]", name.str, ptr));
  418. /* Set event status (ENABLED | SLAVESIDE_DISABLED | DISABLED) */
  419. switch (ptr[0])
  420. {
  421. case 'E' :
  422. status = Event_parse_data::ENABLED;
  423. break;
  424. case 'S' :
  425. status = Event_parse_data::SLAVESIDE_DISABLED;
  426. break;
  427. case 'D' :
  428. default:
  429. status = Event_parse_data::DISABLED;
  430. break;
  431. }
  432. if ((ptr= get_field(&mem_root, table->field[ET_FIELD_ORIGINATOR])) == NullS)
  433. DBUG_RETURN(TRUE);
  434. originator = table->field[ET_FIELD_ORIGINATOR]->val_int();
  435. /* ToDo : Andrey . Find a way not to allocate ptr on event_mem_root */
  436. if ((ptr= get_field(&mem_root,
  437. table->field[ET_FIELD_ON_COMPLETION])) == NullS)
  438. DBUG_RETURN(TRUE);
  439. on_completion= (ptr[0]=='D'? Event_parse_data::ON_COMPLETION_DROP:
  440. Event_parse_data::ON_COMPLETION_PRESERVE);
  441. DBUG_RETURN(FALSE);
  442. }
  443. /**
  444. Load an event's body from a row from mysql.event.
  445. @details This method is silent on errors and should behave like that.
  446. Callers should handle throwing of error messages. The reason is that the
  447. class should not know about how to deal with communication.
  448. @return Operation status
  449. @retval FALSE OK
  450. @retval TRUE Error
  451. */
  452. bool
  453. Event_timed::load_from_row(THD *thd, TABLE *table)
  454. {
  455. char *ptr;
  456. size_t len;
  457. DBUG_ENTER("Event_timed::load_from_row");
  458. if (Event_queue_element::load_from_row(thd, table))
  459. DBUG_RETURN(TRUE);
  460. if (load_string_fields(table->field,
  461. ET_FIELD_BODY, &body,
  462. ET_FIELD_BODY_UTF8, &body_utf8,
  463. ET_FIELD_COUNT))
  464. DBUG_RETURN(TRUE);
  465. if (Event_creation_ctx::load_from_db(thd, &mem_root, dbname.str, name.str,
  466. table, &creation_ctx))
  467. {
  468. push_warning_printf(thd,
  469. MYSQL_ERROR::WARN_LEVEL_WARN,
  470. ER_EVENT_INVALID_CREATION_CTX,
  471. ER(ER_EVENT_INVALID_CREATION_CTX),
  472. (const char *) dbname.str,
  473. (const char *) name.str);
  474. }
  475. ptr= strchr(definer.str, '@');
  476. if (! ptr)
  477. ptr= definer.str;
  478. len= ptr - definer.str;
  479. definer_user.str= strmake_root(&mem_root, definer.str, len);
  480. definer_user.length= len;
  481. len= definer.length - len - 1;
  482. /* 1:because of @ */
  483. definer_host.str= strmake_root(&mem_root, ptr + 1, len);
  484. definer_host.length= len;
  485. created= table->field[ET_FIELD_CREATED]->val_int();
  486. modified= table->field[ET_FIELD_MODIFIED]->val_int();
  487. comment.str= get_field(&mem_root, table->field[ET_FIELD_COMMENT]);
  488. if (comment.str != NullS)
  489. comment.length= strlen(comment.str);
  490. else
  491. comment.length= 0;
  492. sql_mode= (ulong) table->field[ET_FIELD_SQL_MODE]->val_int();
  493. DBUG_RETURN(FALSE);
  494. }
  495. /*
  496. add_interval() adds a specified interval to time 'ltime' in time
  497. zone 'time_zone', and returns the result converted to the number of
  498. seconds since epoch (aka Unix time; in UTC time zone). Zero result
  499. means an error.
  500. */
  501. static
  502. my_time_t
  503. add_interval(MYSQL_TIME *ltime, const Time_zone *time_zone,
  504. interval_type scale, INTERVAL interval)
  505. {
  506. if (date_add_interval(ltime, scale, interval))
  507. return 0;
  508. my_bool not_used;
  509. return time_zone->TIME_to_gmt_sec(ltime, &not_used);
  510. }
  511. /*
  512. Computes the sum of a timestamp plus interval.
  513. SYNOPSIS
  514. get_next_time()
  515. time_zone event time zone
  516. next the sum
  517. start add interval_value to this time
  518. time_now current time
  519. i_value quantity of time type interval to add
  520. i_type type of interval to add (SECOND, MINUTE, HOUR, WEEK ...)
  521. RETURN VALUE
  522. 0 OK
  523. 1 Error
  524. NOTES
  525. 1) If the interval is conversible to SECOND, like MINUTE, HOUR, DAY, WEEK.
  526. Then we use TIMEDIFF()'s implementation as underlying and number of
  527. seconds as resolution for computation.
  528. 2) In all other cases - MONTH, QUARTER, YEAR we use MONTH as resolution
  529. and PERIOD_DIFF()'s implementation
  530. */
  531. static
  532. bool get_next_time(const Time_zone *time_zone, my_time_t *next,
  533. my_time_t start, my_time_t time_now,
  534. int i_value, interval_type i_type)
  535. {
  536. DBUG_ENTER("get_next_time");
  537. DBUG_PRINT("enter", ("start: %lu now: %lu", (long) start, (long) time_now));
  538. DBUG_ASSERT(start <= time_now);
  539. longlong months=0, seconds=0;
  540. switch (i_type) {
  541. case INTERVAL_YEAR:
  542. months= i_value*12;
  543. break;
  544. case INTERVAL_QUARTER:
  545. /* Has already been converted to months */
  546. case INTERVAL_YEAR_MONTH:
  547. case INTERVAL_MONTH:
  548. months= i_value;
  549. break;
  550. case INTERVAL_WEEK:
  551. /* WEEK has already been converted to days */
  552. case INTERVAL_DAY:
  553. seconds= i_value*24*3600;
  554. break;
  555. case INTERVAL_DAY_HOUR:
  556. case INTERVAL_HOUR:
  557. seconds= i_value*3600;
  558. break;
  559. case INTERVAL_DAY_MINUTE:
  560. case INTERVAL_HOUR_MINUTE:
  561. case INTERVAL_MINUTE:
  562. seconds= i_value*60;
  563. break;
  564. case INTERVAL_DAY_SECOND:
  565. case INTERVAL_HOUR_SECOND:
  566. case INTERVAL_MINUTE_SECOND:
  567. case INTERVAL_SECOND:
  568. seconds= i_value;
  569. break;
  570. case INTERVAL_DAY_MICROSECOND:
  571. case INTERVAL_HOUR_MICROSECOND:
  572. case INTERVAL_MINUTE_MICROSECOND:
  573. case INTERVAL_SECOND_MICROSECOND:
  574. case INTERVAL_MICROSECOND:
  575. /*
  576. We should return an error here so SHOW EVENTS/ SELECT FROM I_S.EVENTS
  577. would give an error then.
  578. */
  579. DBUG_RETURN(1);
  580. break;
  581. case INTERVAL_LAST:
  582. DBUG_ASSERT(0);
  583. }
  584. DBUG_PRINT("info", ("seconds: %ld months: %ld", (long) seconds, (long) months));
  585. MYSQL_TIME local_start;
  586. MYSQL_TIME local_now;
  587. /* Convert times from UTC to local. */
  588. {
  589. time_zone->gmt_sec_to_TIME(&local_start, start);
  590. time_zone->gmt_sec_to_TIME(&local_now, time_now);
  591. }
  592. INTERVAL interval;
  593. bzero(&interval, sizeof(interval));
  594. my_time_t next_time= 0;
  595. if (seconds)
  596. {
  597. longlong seconds_diff;
  598. long microsec_diff;
  599. bool negative= calc_time_diff(&local_now, &local_start, 1,
  600. &seconds_diff, &microsec_diff);
  601. if (!negative)
  602. {
  603. /*
  604. The formula below returns the interval that, when added to
  605. local_start, will always give the time in the future.
  606. */
  607. interval.second= seconds_diff - seconds_diff % seconds + seconds;
  608. next_time= add_interval(&local_start, time_zone,
  609. INTERVAL_SECOND, interval);
  610. if (next_time == 0)
  611. goto done;
  612. }
  613. if (next_time <= time_now)
  614. {
  615. /*
  616. If 'negative' is true above, then 'next_time == 0', and
  617. 'next_time <= time_now' is also true. If negative is false,
  618. then next_time was set, but perhaps to the value that is less
  619. then time_now. See below for elaboration.
  620. */
  621. DBUG_ASSERT(negative || next_time > 0);
  622. /*
  623. If local_now < local_start, i.e. STARTS time is in the future
  624. according to the local time (it always in the past according
  625. to UTC---this is a prerequisite of this function), then
  626. STARTS is almost always in the past according to the local
  627. time too. However, in the time zone that has backward
  628. Daylight Saving Time shift, the following may happen: suppose
  629. we have a backward DST shift at certain date after 2:59:59,
  630. i.e. local time goes 1:59:59, 2:00:00, ... , 2:59:59, (shift
  631. here) 2:00:00 (again), ... , 2:59:59 (again), 3:00:00, ... .
  632. Now suppose the time has passed the first 2:59:59, has been
  633. shifted backward, and now is (the second) 2:20:00. The user
  634. does CREATE EVENT with STARTS 'current-date 2:40:00'. Local
  635. time 2:40:00 from create statement is treated by time
  636. functions as the first such time, so according to UTC it comes
  637. before the second 2:20:00. But according to local time it is
  638. obviously in the future, so we end up in this branch.
  639. Since we are in the second pass through 2:00:00--2:59:59, and
  640. any local time form this interval is treated by system
  641. functions as the time from the first pass, we have to find the
  642. time for the next execution that is past the DST-affected
  643. interval (past the second 2:59:59 for our example,
  644. i.e. starting from 3:00:00). We do this in the loop until the
  645. local time is mapped onto future UTC time. 'start' time is in
  646. the past, so we may use 'do { } while' here, and add the first
  647. interval right away.
  648. Alternatively, it could be that local_now >= local_start. Now
  649. for the example above imagine we do CREATE EVENT with STARTS
  650. 'current-date 2:10:00'. Local start 2:10 is in the past (now
  651. is local 2:20), so we add an interval, and get next execution
  652. time, say, 2:40. It is in the future according to local time,
  653. but, again, since we are in the second pass through
  654. 2:00:00--2:59:59, 2:40 will be converted into UTC time in the
  655. past. So we will end up in this branch again, and may add
  656. intervals in a 'do { } while' loop.
  657. Note that for any given event we may end up here only if event
  658. next execution time will map to the time interval that is
  659. passed twice, and only if the server was started during the
  660. second pass, or the event is being created during the second
  661. pass. After that, we never will get here (unless we again
  662. start the server during the second pass). In other words,
  663. such a condition is extremely rare.
  664. */
  665. interval.second= seconds;
  666. do
  667. {
  668. next_time= add_interval(&local_start, time_zone,
  669. INTERVAL_SECOND, interval);
  670. if (next_time == 0)
  671. goto done;
  672. }
  673. while (next_time <= time_now);
  674. }
  675. }
  676. else
  677. {
  678. long diff_months= (long) (local_now.year - local_start.year)*12 +
  679. (local_now.month - local_start.month);
  680. /*
  681. Unlike for seconds above, the formula below returns the interval
  682. that, when added to the local_start, will give the time in the
  683. past, or somewhere in the current month. We are interested in
  684. the latter case, to see if this time has already passed, or is
  685. yet to come this month.
  686. Note that the time is guaranteed to be in the past unless
  687. (diff_months % months == 0), but no good optimization is
  688. possible here, because (diff_months % months == 0) is what will
  689. happen most of the time, as get_next_time() will be called right
  690. after the execution of the event. We could pass last_executed
  691. time to this function, and see if the execution has already
  692. happened this month, but for that we will have to convert
  693. last_executed from seconds since epoch to local broken-down
  694. time, and this will greatly reduce the effect of the
  695. optimization. So instead we keep the code simple and clean.
  696. */
  697. interval.month= (ulong) (diff_months - diff_months % months);
  698. next_time= add_interval(&local_start, time_zone,
  699. INTERVAL_MONTH, interval);
  700. if (next_time == 0)
  701. goto done;
  702. if (next_time <= time_now)
  703. {
  704. interval.month= (ulong) months;
  705. next_time= add_interval(&local_start, time_zone,
  706. INTERVAL_MONTH, interval);
  707. if (next_time == 0)
  708. goto done;
  709. }
  710. }
  711. DBUG_ASSERT(time_now < next_time);
  712. *next= next_time;
  713. done:
  714. DBUG_PRINT("info", ("next_time: %ld", (long) next_time));
  715. DBUG_RETURN(next_time == 0);
  716. }
  717. /*
  718. Computes next execution time.
  719. SYNOPSIS
  720. Event_queue_element::compute_next_execution_time()
  721. RETURN VALUE
  722. FALSE OK
  723. TRUE Error
  724. NOTES
  725. The time is set in execute_at, if no more executions the latter is
  726. set to 0.
  727. */
  728. bool
  729. Event_queue_element::compute_next_execution_time()
  730. {
  731. my_time_t time_now;
  732. DBUG_ENTER("Event_queue_element::compute_next_execution_time");
  733. DBUG_PRINT("enter", ("starts: %lu ends: %lu last_executed: %lu this: 0x%lx",
  734. (long) starts, (long) ends, (long) last_executed,
  735. (long) this));
  736. if (status != Event_parse_data::ENABLED)
  737. {
  738. DBUG_PRINT("compute_next_execution_time",
  739. ("Event %s is DISABLED", name.str));
  740. goto ret;
  741. }
  742. /* If one-time, no need to do computation */
  743. if (!expression)
  744. {
  745. /* Let's check whether it was executed */
  746. if (last_executed)
  747. {
  748. DBUG_PRINT("info",("One-time event %s.%s of was already executed",
  749. dbname.str, name.str));
  750. dropped= (on_completion == Event_parse_data::ON_COMPLETION_DROP);
  751. DBUG_PRINT("info",("One-time event will be dropped: %d.", dropped));
  752. status= Event_parse_data::DISABLED;
  753. status_changed= TRUE;
  754. }
  755. goto ret;
  756. }
  757. time_now= (my_time_t) current_thd->query_start();
  758. DBUG_PRINT("info",("NOW: [%lu]", (ulong) time_now));
  759. /* if time_now is after ends don't execute anymore */
  760. if (!ends_null && ends < time_now)
  761. {
  762. DBUG_PRINT("info", ("NOW after ENDS, don't execute anymore"));
  763. /* time_now is after ends. don't execute anymore */
  764. execute_at= 0;
  765. execute_at_null= TRUE;
  766. if (on_completion == Event_parse_data::ON_COMPLETION_DROP)
  767. dropped= TRUE;
  768. DBUG_PRINT("info", ("Dropped: %d", dropped));
  769. status= Event_parse_data::DISABLED;
  770. status_changed= TRUE;
  771. goto ret;
  772. }
  773. /*
  774. Here time_now is before or equals ends if the latter is set.
  775. Let's check whether time_now is before starts.
  776. If so schedule for starts.
  777. */
  778. if (!starts_null && time_now <= starts)
  779. {
  780. if (time_now == starts && starts == last_executed)
  781. {
  782. /*
  783. do nothing or we will schedule for second time execution at starts.
  784. */
  785. }
  786. else
  787. {
  788. DBUG_PRINT("info", ("STARTS is future, NOW <= STARTS,sched for STARTS"));
  789. /*
  790. starts is in the future
  791. time_now before starts. Scheduling for starts
  792. */
  793. execute_at= starts;
  794. execute_at_null= FALSE;
  795. goto ret;
  796. }
  797. }
  798. if (!starts_null && !ends_null)
  799. {
  800. /*
  801. Both starts and m_ends are set and time_now is between them (incl.)
  802. If last_executed is set then increase with m_expression. The new MYSQL_TIME is
  803. after m_ends set execute_at to 0. And check for on_completion
  804. If not set then schedule for now.
  805. */
  806. DBUG_PRINT("info", ("Both STARTS & ENDS are set"));
  807. if (!last_executed)
  808. {
  809. DBUG_PRINT("info", ("Not executed so far."));
  810. }
  811. {
  812. my_time_t next_exec;
  813. if (get_next_time(time_zone, &next_exec, starts, time_now,
  814. (int) expression, interval))
  815. goto err;
  816. /* There was previous execution */
  817. if (ends < next_exec)
  818. {
  819. DBUG_PRINT("info", ("Next execution of %s after ENDS. Stop executing.",
  820. name.str));
  821. /* Next execution after ends. No more executions */
  822. execute_at= 0;
  823. execute_at_null= TRUE;
  824. if (on_completion == Event_parse_data::ON_COMPLETION_DROP)
  825. dropped= TRUE;
  826. status= Event_parse_data::DISABLED;
  827. status_changed= TRUE;
  828. }
  829. else
  830. {
  831. DBUG_PRINT("info",("Next[%lu]", (ulong) next_exec));
  832. execute_at= next_exec;
  833. execute_at_null= FALSE;
  834. }
  835. }
  836. goto ret;
  837. }
  838. else if (starts_null && ends_null)
  839. {
  840. /* starts is always set, so this is a dead branch !! */
  841. DBUG_PRINT("info", ("Neither STARTS nor ENDS are set"));
  842. /*
  843. Both starts and m_ends are not set, so we schedule for the next
  844. based on last_executed.
  845. */
  846. if (last_executed)
  847. {
  848. my_time_t next_exec;
  849. if (get_next_time(time_zone, &next_exec, starts, time_now,
  850. (int) expression, interval))
  851. goto err;
  852. execute_at= next_exec;
  853. DBUG_PRINT("info",("Next[%lu]", (ulong) next_exec));
  854. }
  855. else
  856. {
  857. /* last_executed not set. Schedule the event for now */
  858. DBUG_PRINT("info", ("Execute NOW"));
  859. execute_at= time_now;
  860. }
  861. execute_at_null= FALSE;
  862. }
  863. else
  864. {
  865. /* either starts or m_ends is set */
  866. if (!starts_null)
  867. {
  868. DBUG_PRINT("info", ("STARTS is set"));
  869. /*
  870. - starts is set.
  871. - starts is not in the future according to check made before
  872. Hence schedule for starts + m_expression in case last_executed
  873. is not set, otherwise to last_executed + m_expression
  874. */
  875. if (!last_executed)
  876. {
  877. DBUG_PRINT("info", ("Not executed so far."));
  878. }
  879. {
  880. my_time_t next_exec;
  881. if (get_next_time(time_zone, &next_exec, starts, time_now,
  882. (int) expression, interval))
  883. goto err;
  884. execute_at= next_exec;
  885. DBUG_PRINT("info",("Next[%lu]", (ulong) next_exec));
  886. }
  887. execute_at_null= FALSE;
  888. }
  889. else
  890. {
  891. /* this is a dead branch, because starts is always set !!! */
  892. DBUG_PRINT("info", ("STARTS is not set. ENDS is set"));
  893. /*
  894. - m_ends is set
  895. - m_ends is after time_now or is equal
  896. Hence check for m_last_execute and increment with m_expression.
  897. If last_executed is not set then schedule for now
  898. */
  899. if (!last_executed)
  900. execute_at= time_now;
  901. else
  902. {
  903. my_time_t next_exec;
  904. if (get_next_time(time_zone, &next_exec, starts, time_now,
  905. (int) expression, interval))
  906. goto err;
  907. if (ends < next_exec)
  908. {
  909. DBUG_PRINT("info", ("Next execution after ENDS. Stop executing."));
  910. execute_at= 0;
  911. execute_at_null= TRUE;
  912. status= Event_parse_data::DISABLED;
  913. status_changed= TRUE;
  914. if (on_completion == Event_parse_data::ON_COMPLETION_DROP)
  915. dropped= TRUE;
  916. }
  917. else
  918. {
  919. DBUG_PRINT("info", ("Next[%lu]", (ulong) next_exec));
  920. execute_at= next_exec;
  921. execute_at_null= FALSE;
  922. }
  923. }
  924. }
  925. goto ret;
  926. }
  927. ret:
  928. DBUG_PRINT("info", ("ret: 0 execute_at: %lu", (long) execute_at));
  929. DBUG_RETURN(FALSE);
  930. err:
  931. DBUG_PRINT("info", ("ret=1"));
  932. DBUG_RETURN(TRUE);
  933. }
  934. /*
  935. Set the internal last_executed MYSQL_TIME struct to now. NOW is the
  936. time according to thd->query_start(), so the THD's clock.
  937. SYNOPSIS
  938. Event_queue_element::mark_last_executed()
  939. thd thread context
  940. */
  941. void
  942. Event_queue_element::mark_last_executed(THD *thd)
  943. {
  944. last_executed= (my_time_t) thd->query_start();
  945. last_executed_changed= TRUE;
  946. execution_count++;
  947. }
  948. /*
  949. Saves status and last_executed_at to the disk if changed.
  950. SYNOPSIS
  951. Event_queue_element::update_timing_fields()
  952. thd - thread context
  953. RETURN VALUE
  954. FALSE OK
  955. TRUE Error while opening mysql.event for writing or during
  956. write on disk
  957. */
  958. bool
  959. Event_queue_element::update_timing_fields(THD *thd)
  960. {
  961. Event_db_repository *db_repository= Events::get_db_repository();
  962. int ret;
  963. DBUG_ENTER("Event_queue_element::update_timing_fields");
  964. DBUG_PRINT("enter", ("name: %*s", (int) name.length, name.str));
  965. /* No need to update if nothing has changed */
  966. if (!(status_changed || last_executed_changed))
  967. DBUG_RETURN(0);
  968. ret= db_repository->update_timing_fields_for_event(thd,
  969. dbname, name,
  970. last_executed_changed,
  971. last_executed,
  972. status_changed,
  973. (ulonglong) status);
  974. last_executed_changed= status_changed= FALSE;
  975. DBUG_RETURN(ret);
  976. }
  977. static
  978. void
  979. append_datetime(String *buf, Time_zone *time_zone, my_time_t secs,
  980. const char *name, uint len)
  981. {
  982. char dtime_buff[20*2+32];/* +32 to make my_snprintf_{8bit|ucs2} happy */
  983. buf->append(STRING_WITH_LEN(" "));
  984. buf->append(name, len);
  985. buf->append(STRING_WITH_LEN(" '"));
  986. /*
  987. Pass the buffer and the second param tells fills the buffer and
  988. returns the number of chars to copy.
  989. */
  990. MYSQL_TIME time;
  991. time_zone->gmt_sec_to_TIME(&time, secs);
  992. buf->append(dtime_buff, my_datetime_to_str(&time, dtime_buff));
  993. buf->append(STRING_WITH_LEN("'"));
  994. }
  995. /*
  996. Get SHOW CREATE EVENT as string
  997. SYNOPSIS
  998. Event_timed::get_create_event(THD *thd, String *buf)
  999. thd Thread
  1000. buf String*, should be already allocated. CREATE EVENT goes inside.
  1001. RETURN VALUE
  1002. 0 OK
  1003. EVEX_MICROSECOND_UNSUP Error (for now if mysql.event has been
  1004. tampered and MICROSECONDS interval or
  1005. derivative has been put there.
  1006. */
  1007. int
  1008. Event_timed::get_create_event(THD *thd, String *buf)
  1009. {
  1010. char tmp_buf[2 * STRING_BUFFER_USUAL_SIZE];
  1011. String expr_buf(tmp_buf, sizeof(tmp_buf), system_charset_info);
  1012. expr_buf.length(0);
  1013. DBUG_ENTER("get_create_event");
  1014. DBUG_PRINT("ret_info",("body_len=[%d]body=[%s]",
  1015. (int) body.length, body.str));
  1016. if (expression && Events::reconstruct_interval_expression(&expr_buf, interval,
  1017. expression))
  1018. DBUG_RETURN(EVEX_MICROSECOND_UNSUP);
  1019. buf->append(STRING_WITH_LEN("CREATE EVENT "));
  1020. append_identifier(thd, buf, name.str, name.length);
  1021. if (expression)
  1022. {
  1023. buf->append(STRING_WITH_LEN(" ON SCHEDULE EVERY "));
  1024. buf->append(expr_buf);
  1025. buf->append(' ');
  1026. LEX_STRING *ival= &interval_type_to_name[interval];
  1027. buf->append(ival->str, ival->length);
  1028. if (!starts_null)
  1029. append_datetime(buf, time_zone, starts, STRING_WITH_LEN("STARTS"));
  1030. if (!ends_null)
  1031. append_datetime(buf, time_zone, ends, STRING_WITH_LEN("ENDS"));
  1032. }
  1033. else
  1034. {
  1035. append_datetime(buf, time_zone, execute_at,
  1036. STRING_WITH_LEN("ON SCHEDULE AT"));
  1037. }
  1038. if (on_completion == Event_parse_data::ON_COMPLETION_DROP)
  1039. buf->append(STRING_WITH_LEN(" ON COMPLETION NOT PRESERVE "));
  1040. else
  1041. buf->append(STRING_WITH_LEN(" ON COMPLETION PRESERVE "));
  1042. if (status == Event_parse_data::ENABLED)
  1043. buf->append(STRING_WITH_LEN("ENABLE"));
  1044. else if (status == Event_parse_data::SLAVESIDE_DISABLED)
  1045. buf->append(STRING_WITH_LEN("DISABLE ON SLAVE"));
  1046. else
  1047. buf->append(STRING_WITH_LEN("DISABLE"));
  1048. if (comment.length)
  1049. {
  1050. buf->append(STRING_WITH_LEN(" COMMENT "));
  1051. append_unescaped(buf, comment.str, comment.length);
  1052. }
  1053. buf->append(STRING_WITH_LEN(" DO "));
  1054. buf->append(body.str, body.length);
  1055. DBUG_RETURN(0);
  1056. }
  1057. /**
  1058. Get an artificial stored procedure to parse as an event definition.
  1059. */
  1060. bool
  1061. Event_job_data::construct_sp_sql(THD *thd, String *sp_sql)
  1062. {
  1063. LEX_STRING buffer;
  1064. const uint STATIC_SQL_LENGTH= 44;
  1065. DBUG_ENTER("Event_job_data::construct_sp_sql");
  1066. /*
  1067. Allocate a large enough buffer on the thread execution memory
  1068. root to avoid multiple [re]allocations on system heap
  1069. */
  1070. buffer.length= STATIC_SQL_LENGTH + name.length + body.length;
  1071. if (! (buffer.str= (char*) thd->alloc(buffer.length)))
  1072. DBUG_RETURN(TRUE);
  1073. sp_sql->set(buffer.str, buffer.length, system_charset_info);
  1074. sp_sql->length(0);
  1075. sp_sql->append(C_STRING_WITH_LEN("CREATE "));
  1076. sp_sql->append(C_STRING_WITH_LEN("PROCEDURE "));
  1077. /*
  1078. Let's use the same name as the event name to perhaps produce a
  1079. better error message in case it is a part of some parse error.
  1080. We're using append_identifier here to successfully parse
  1081. events with reserved names.
  1082. */
  1083. append_identifier(thd, sp_sql, name.str, name.length);
  1084. /*
  1085. The default SQL security of a stored procedure is DEFINER. We
  1086. have already activated the security context of the event, so
  1087. let's execute the procedure with the invoker rights to save on
  1088. resets of security contexts.
  1089. */
  1090. sp_sql->append(C_STRING_WITH_LEN("() SQL SECURITY INVOKER "));
  1091. sp_sql->append(body.str, body.length);
  1092. DBUG_RETURN(thd->is_fatal_error);
  1093. }
  1094. /**
  1095. Get DROP EVENT statement to binlog the drop of ON COMPLETION NOT
  1096. PRESERVE event.
  1097. */
  1098. bool
  1099. Event_job_data::construct_drop_event_sql(THD *thd, String *sp_sql)
  1100. {
  1101. LEX_STRING buffer;
  1102. const uint STATIC_SQL_LENGTH= 14;
  1103. DBUG_ENTER("Event_job_data::construct_drop_event_sql");
  1104. buffer.length= STATIC_SQL_LENGTH + name.length*2 + dbname.length*2;
  1105. if (! (buffer.str= (char*) thd->alloc(buffer.length)))
  1106. DBUG_RETURN(TRUE);
  1107. sp_sql->set(buffer.str, buffer.length, system_charset_info);
  1108. sp_sql->length(0);
  1109. sp_sql->append(C_STRING_WITH_LEN("DROP EVENT "));
  1110. append_identifier(thd, sp_sql, dbname.str, dbname.length);
  1111. sp_sql->append('.');
  1112. append_identifier(thd, sp_sql, name.str, name.length);
  1113. DBUG_RETURN(thd->is_fatal_error);
  1114. }
  1115. /**
  1116. Compiles and executes the event (the underlying sp_head object)
  1117. @retval TRUE error (reported to the error log)
  1118. @retval FALSE success
  1119. */
  1120. bool
  1121. Event_job_data::execute(THD *thd, bool drop)
  1122. {
  1123. String sp_sql;
  1124. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  1125. Security_context event_sctx, *save_sctx= NULL;
  1126. #endif
  1127. List<Item> empty_item_list;
  1128. bool ret= TRUE;
  1129. DBUG_ENTER("Event_job_data::execute");
  1130. mysql_reset_thd_for_next_command(thd);
  1131. /*
  1132. MySQL parser currently assumes that current database is either
  1133. present in THD or all names in all statements are fully specified.
  1134. And yet not fully specified names inside stored programs must be
  1135. be supported, even if the current database is not set:
  1136. CREATE PROCEDURE db1.p1() BEGIN CREATE TABLE t1; END//
  1137. -- in this example t1 should be always created in db1 and the statement
  1138. must parse even if there is no current database.
  1139. To support this feature and still address the parser limitation,
  1140. we need to set the current database here.
  1141. We don't have to call mysql_change_db, since the checks performed
  1142. in it are unnecessary for the purpose of parsing, and
  1143. mysql_change_db will be invoked anyway later, to activate the
  1144. procedure database before it's executed.
  1145. */
  1146. thd->set_db(dbname.str, dbname.length);
  1147. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  1148. if (event_sctx.change_security_context(thd,
  1149. &definer_user, &definer_host,
  1150. &dbname, &save_sctx))
  1151. {
  1152. sql_print_error("Event Scheduler: "
  1153. "[%s].[%s.%s] execution failed, "
  1154. "failed to authenticate the user.",
  1155. definer.str, dbname.str, name.str);
  1156. goto end_no_lex_start;
  1157. }
  1158. #endif
  1159. if (check_access(thd, EVENT_ACL, dbname.str,
  1160. 0, 0, 0, is_schema_db(dbname.str)))
  1161. {
  1162. /*
  1163. This aspect of behavior is defined in the worklog,
  1164. and this is how triggers work too: if TRIGGER
  1165. privilege is revoked from trigger definer,
  1166. triggers are not executed.
  1167. */
  1168. sql_print_error("Event Scheduler: "
  1169. "[%s].[%s.%s] execution failed, "
  1170. "user no longer has EVENT privilege.",
  1171. definer.str, dbname.str, name.str);
  1172. goto end_no_lex_start;
  1173. }
  1174. if (construct_sp_sql(thd, &sp_sql))
  1175. goto end_no_lex_start;
  1176. /*
  1177. Set up global thread attributes to reflect the properties of
  1178. this Event. We can simply reset these instead of usual
  1179. backup/restore employed in stored programs since we know that
  1180. this is a top level statement and the worker thread is
  1181. allocated exclusively to execute this event.
  1182. */
  1183. thd->variables.sql_mode= sql_mode;
  1184. thd->variables.time_zone= time_zone;
  1185. thd->set_query(sp_sql.c_ptr_safe(), sp_sql.length());
  1186. {
  1187. Parser_state parser_state(thd, thd->query(), thd->query_length());
  1188. lex_start(thd);
  1189. if (parse_sql(thd, & parser_state, creation_ctx))
  1190. {
  1191. sql_print_error("Event Scheduler: "
  1192. "%serror during compilation of %s.%s",
  1193. thd->is_fatal_error ? "fatal " : "",
  1194. (const char *) dbname.str, (const char *) name.str);
  1195. goto end;
  1196. }
  1197. }
  1198. {
  1199. sp_head *sphead= thd->lex->sphead;
  1200. DBUG_ASSERT(sphead);
  1201. if (thd->enable_slow_log)
  1202. sphead->m_flags|= sp_head::LOG_SLOW_STATEMENTS;
  1203. sphead->m_flags|= sp_head::LOG_GENERAL_LOG;
  1204. sphead->set_info(0, 0, &thd->lex->sp_chistics, sql_mode);
  1205. sphead->set_creation_ctx(creation_ctx);
  1206. sphead->optimize();
  1207. ret= sphead->execute_procedure(thd, &empty_item_list);
  1208. /*
  1209. There is no pre-locking and therefore there should be no
  1210. tables open and locked left after execute_procedure.
  1211. */
  1212. }
  1213. end:
  1214. if (thd->lex->sphead) /* NULL only if a parse error */
  1215. {
  1216. delete thd->lex->sphead;
  1217. thd->lex->sphead= NULL;
  1218. }
  1219. end_no_lex_start:
  1220. if (drop && !thd->is_fatal_error)
  1221. {
  1222. /*
  1223. We must do it here since here we're under the right authentication
  1224. ID of the event definer.
  1225. */
  1226. sql_print_information("Event Scheduler: Dropping %s.%s",
  1227. (const char *) dbname.str, (const char *) name.str);
  1228. /*
  1229. Construct a query for the binary log, to ensure the event is dropped
  1230. on the slave
  1231. */
  1232. if (construct_drop_event_sql(thd, &sp_sql))
  1233. ret= 1;
  1234. else
  1235. {
  1236. ulong saved_master_access;
  1237. thd->set_query(sp_sql.c_ptr_safe(), sp_sql.length());
  1238. /*
  1239. NOTE: even if we run in read-only mode, we should be able to lock
  1240. the mysql.event table for writing. In order to achieve this, we
  1241. should call mysql_lock_tables() under the super-user.
  1242. */
  1243. saved_master_access= thd->security_ctx->master_access;
  1244. thd->security_ctx->master_access |= SUPER_ACL;
  1245. ret= Events::drop_event(thd, dbname, name, FALSE);
  1246. thd->security_ctx->master_access= saved_master_access;
  1247. }
  1248. }
  1249. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  1250. if (save_sctx)
  1251. event_sctx.restore_security_context(thd, save_sctx);
  1252. #endif
  1253. lex_end(thd->lex);
  1254. thd->lex->unit.cleanup();
  1255. thd->end_statement();
  1256. thd->cleanup_after_query();
  1257. /* Avoid races with SHOW PROCESSLIST */
  1258. thd->set_query(NULL, 0);
  1259. DBUG_PRINT("info", ("EXECUTED %s.%s ret: %d", dbname.str, name.str, ret));
  1260. DBUG_RETURN(ret);
  1261. }
  1262. /*
  1263. Checks whether two events are in the same schema
  1264. SYNOPSIS
  1265. event_basic_db_equal()
  1266. db Schema
  1267. et Compare et->dbname to `db`
  1268. RETURN VALUE
  1269. TRUE Equal
  1270. FALSE Not equal
  1271. */
  1272. bool
  1273. event_basic_db_equal(LEX_STRING db, Event_basic *et)
  1274. {
  1275. return !sortcmp_lex_string(et->dbname, db, system_charset_info);
  1276. }
  1277. /*
  1278. Checks whether an event has equal `db` and `name`
  1279. SYNOPSIS
  1280. event_basic_identifier_equal()
  1281. db Schema
  1282. name Name
  1283. et The event object
  1284. RETURN VALUE
  1285. TRUE Equal
  1286. FALSE Not equal
  1287. */
  1288. bool
  1289. event_basic_identifier_equal(LEX_STRING db, LEX_STRING name, Event_basic *b)
  1290. {
  1291. return !sortcmp_lex_string(name, b->name, system_charset_info) &&
  1292. !sortcmp_lex_string(db, b->dbname, system_charset_info);
  1293. }
  1294. /**
  1295. @} (End of group Event_Scheduler)
  1296. */