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.

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