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.

1240 lines
33 KiB

23 years ago
23 years ago
21 years ago
Bug#8153 (Stored procedure with subquery and continue handler, wrong result) Before this fix, - a runtime error in a statement in a stored procedure with no error handlers was properly detected (as expected) - a runtime error in a statement with an error handler inherited from a non local runtime context (i.e., proc a with a handler, calling proc b) was properly detected (as expected) - a runtime error in a statement with a *local* error handler was executed as follows : a) the statement would succeed, regardless of the error condition, (bug) b) the error handler would be called (as expected). The root cause is that functions like my_messqge_sql would "forget" to set the thread flag thd->net.report_error to 1, because of the check involving sp_rcontext::found_handler_here(). Failure to set this flag would cause, later in the call stack, in Item_func::fix_fields() at line 190, the code to return FALSE and consider that executing the statement was successful. With this fix : - error handling code, that was duplicated in different places in the code, is now implemented in sp_rcontext::handle_error(), - handle_error() correctly sets thd->net.report_error when a handler is present, regardless of the handler location (local, or in the call stack). A test case, bug8153_subselect, has been written to demonstrate the change of behavior before and after the fix. Another test case, bug8153_function_a, as also been writen. This test has the same behavior before and after the fix. This test has been written to demonstrate that the previous expected result of procedure bug18787, was incorrect, since select no_such_function() should fail and therefore not produce a result. The incorrect result for bug18787 has the same root cause as Bug#8153, and the expected result has been adjusted.
20 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
22 years ago
23 years ago
22 years ago
22 years ago
23 years ago
23 years ago
23 years ago
21 years ago
21 years ago
23 years ago
23 years ago
21 years ago
21 years ago
21 years ago
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes Changes that requires code changes in other code of other storage engines. (Note that all changes are very straightforward and one should find all issues by compiling a --debug build and fixing all compiler errors and all asserts in field.cc while running the test suite), - New optional handler function introduced: reset() This is called after every DML statement to make it easy for a handler to statement specific cleanups. (The only case it's not called is if force the file to be closed) - handler::extra(HA_EXTRA_RESET) is removed. Code that was there before should be moved to handler::reset() - table->read_set contains a bitmap over all columns that are needed in the query. read_row() and similar functions only needs to read these columns - table->write_set contains a bitmap over all columns that will be updated in the query. write_row() and update_row() only needs to update these columns. The above bitmaps should now be up to date in all context (including ALTER TABLE, filesort()). The handler is informed of any changes to the bitmap after fix_fields() by calling the virtual function handler::column_bitmaps_signal(). If the handler does caching of these bitmaps (instead of using table->read_set, table->write_set), it should redo the caching in this code. as the signal() may be sent several times, it's probably best to set a variable in the signal and redo the caching on read_row() / write_row() if the variable was set. - Removed the read_set and write_set bitmap objects from the handler class - Removed all column bit handling functions from the handler class. (Now one instead uses the normal bitmap functions in my_bitmap.c instead of handler dedicated bitmap functions) - field->query_id is removed. One should instead instead check table->read_set and table->write_set if a field is used in the query. - handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now instead use table->read_set to check for which columns to retrieve. - If a handler needs to call Field->val() or Field->store() on columns that are not used in the query, one should install a temporary all-columns-used map while doing so. For this, we provide the following functions: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set); field->val(); dbug_tmp_restore_column_map(table->read_set, old_map); and similar for the write map: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); field->val(); dbug_tmp_restore_column_map(table->write_set, old_map); If this is not done, you will sooner or later hit a DBUG_ASSERT in the field store() / val() functions. (For not DBUG binaries, the dbug_tmp_restore_column_map() and dbug_tmp_restore_column_map() are inline dummy functions and should be optimized away be the compiler). - If one needs to temporary set the column map for all binaries (and not just to avoid the DBUG_ASSERT() in the Field::store() / Field::val() methods) one should use the functions tmp_use_all_columns() and tmp_restore_column_map() instead of the above dbug_ variants. - All 'status' fields in the handler base class (like records, data_file_length etc) are now stored in a 'stats' struct. This makes it easier to know what status variables are provided by the base handler. This requires some trivial variable names in the extra() function. - New virtual function handler::records(). This is called to optimize COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true. (stats.records is not supposed to be an exact value. It's only has to be 'reasonable enough' for the optimizer to be able to choose a good optimization path). - Non virtual handler::init() function added for caching of virtual constants from engine. - Removed has_transactions() virtual method. Now one should instead return HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support transactions. - The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument that is to be used with 'new handler_name()' to allocate the handler in the right area. The xxxx_create_handler() function is also responsible for any initialization of the object before returning. For example, one should change: static handler *myisam_create_handler(TABLE_SHARE *table) { return new ha_myisam(table); } -> static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root) { return new (mem_root) ha_myisam(table); } - New optional virtual function: use_hidden_primary_key(). This is called in case of an update/delete when (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined but we don't have a primary key. This allows the handler to take precisions in remembering any hidden primary key to able to update/delete any found row. The default handler marks all columns to be read. - handler::table_flags() now returns a ulonglong (to allow for more flags). - New/changed table_flags() - HA_HAS_RECORDS Set if ::records() is supported - HA_NO_TRANSACTIONS Set if engine doesn't support transactions - HA_PRIMARY_KEY_REQUIRED_FOR_DELETE Set if we should mark all primary key columns for read when reading rows as part of a DELETE statement. If there is no primary key, all columns are marked for read. - HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some cases (based on table->read_set) - HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION. - HA_DUPP_POS Renamed to HA_DUPLICATE_POS - HA_REQUIRES_KEY_COLUMNS_FOR_DELETE Set this if we should mark ALL key columns for read when when reading rows as part of a DELETE statement. In case of an update we will mark all keys for read for which key part changed value. - HA_STATS_RECORDS_IS_EXACT Set this if stats.records is exact. (This saves us some extra records() calls when optimizing COUNT(*)) - Removed table_flags() - HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if handler::records() gives an exact count() and HA_STATS_RECORDS_IS_EXACT if stats.records is exact. - HA_READ_RND_SAME Removed (no one supported this one) - Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk() - Renamed handler::dupp_pos to handler::dup_pos - Removed not used variable handler::sortkey Upper level handler changes: - ha_reset() now does some overall checks and calls ::reset() - ha_table_flags() added. This is a cached version of table_flags(). The cache is updated on engine creation time and updated on open. MySQL level changes (not obvious from the above): - DBUG_ASSERT() added to check that column usage matches what is set in the column usage bit maps. (This found a LOT of bugs in current column marking code). - In 5.1 before, all used columns was marked in read_set and only updated columns was marked in write_set. Now we only mark columns for which we need a value in read_set. - Column bitmaps are created in open_binary_frm() and open_table_from_share(). (Before this was in table.cc) - handler::table_flags() calls are replaced with handler::ha_table_flags() - For calling field->val() you must have the corresponding bit set in table->read_set. For calling field->store() you must have the corresponding bit set in table->write_set. (There are asserts in all store()/val() functions to catch wrong usage) - thd->set_query_id is renamed to thd->mark_used_columns and instead of setting this to an integer value, this has now the values: MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE Changed also all variables named 'set_query_id' to mark_used_columns. - In filesort() we now inform the handler of exactly which columns are needed doing the sort and choosing the rows. - The TABLE_SHARE object has a 'all_set' column bitmap one can use when one needs a column bitmap with all columns set. (This is used for table->use_all_columns() and other places) - The TABLE object has 3 column bitmaps: - def_read_set Default bitmap for columns to be read - def_write_set Default bitmap for columns to be written - tmp_set Can be used as a temporary bitmap when needed. The table object has also two pointer to bitmaps read_set and write_set that the handler should use to find out which columns are used in which way. - count() optimization now calls handler::records() instead of using handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true). - Added extra argument to Item::walk() to indicate if we should also traverse sub queries. - Added TABLE parameter to cp_buffer_from_ref() - Don't close tables created with CREATE ... SELECT but keep them in the table cache. (Faster usage of newly created tables). New interfaces: - table->clear_column_bitmaps() to initialize the bitmaps for tables at start of new statements. - table->column_bitmaps_set() to set up new column bitmaps and signal the handler about this. - table->column_bitmaps_set_no_signal() for some few cases where we need to setup new column bitmaps but don't signal the handler (as the handler has already been signaled about these before). Used for the momement only in opt_range.cc when doing ROR scans. - table->use_all_columns() to install a bitmap where all columns are marked as use in the read and the write set. - table->default_column_bitmaps() to install the normal read and write column bitmaps, but not signaling the handler about this. This is mainly used when creating TABLE instances. - table->mark_columns_needed_for_delete(), table->mark_columns_needed_for_delete() and table->mark_columns_needed_for_insert() to allow us to put additional columns in column usage maps if handler so requires. (The handler indicates what it neads in handler->table_flags()) - table->prepare_for_position() to allow us to tell handler that it needs to read primary key parts to be able to store them in future table->position() calls. (This replaces the table->file->ha_retrieve_all_pk function) - table->mark_auto_increment_column() to tell handler are going to update columns part of any auto_increment key. - table->mark_columns_used_by_index() to mark all columns that is part of an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow it to quickly know that it only needs to read colums that are part of the key. (The handler can also use the column map for detecting this, but simpler/faster handler can just monitor the extra() call). - table->mark_columns_used_by_index_no_reset() to in addition to other columns, also mark all columns that is used by the given key. - table->restore_column_maps_after_mark_index() to restore to default column maps after a call to table->mark_columns_used_by_index(). - New item function register_field_in_read_map(), for marking used columns in table->read_map. Used by filesort() to mark all used columns - Maintain in TABLE->merge_keys set of all keys that are used in query. (Simplices some optimization loops) - Maintain Field->part_of_key_not_clustered which is like Field->part_of_key but the field in the clustered key is not assumed to be part of all index. (used in opt_range.cc for faster loops) - dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map() tmp_use_all_columns() and tmp_restore_column_map() functions to temporally mark all columns as usable. The 'dbug_' version is primarily intended inside a handler when it wants to just call Field:store() & Field::val() functions, but don't need the column maps set for any other usage. (ie:: bitmap_is_set() is never called) - We can't use compare_records() to skip updates for handlers that returns a partial column set and the read_set doesn't cover all columns in the write set. The reason for this is that if we have a column marked only for write we can't in the MySQL level know if the value changed or not. The reason this worked before was that MySQL marked all to be written columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden bug'. - open_table_from_share() does not anymore setup temporary MEM_ROOT object as a thread specific variable for the handler. Instead we send the to-be-used MEMROOT to get_new_handler(). (Simpler, faster code) Bugs fixed: - Column marking was not done correctly in a lot of cases. (ALTER TABLE, when using triggers, auto_increment fields etc) (Could potentially result in wrong values inserted in table handlers relying on that the old column maps or field->set_query_id was correct) Especially when it comes to triggers, there may be cases where the old code would cause lost/wrong values for NDB and/or InnoDB tables. - Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags: OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG. This allowed me to remove some wrong warnings about: "Some non-transactional changed tables couldn't be rolled back" - Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset (thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose some warnings about "Some non-transactional changed tables couldn't be rolled back") - Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table() which could cause delete_table to report random failures. - Fixed core dumps for some tests when running with --debug - Added missing FN_LIBCHAR in mysql_rm_tmp_tables() (This has probably caused us to not properly remove temporary files after crash) - slow_logs was not properly initialized, which could maybe cause extra/lost entries in slow log. - If we get an duplicate row on insert, change column map to read and write all columns while retrying the operation. This is required by the definition of REPLACE and also ensures that fields that are only part of UPDATE are properly handled. This fixed a bug in NDB and REPLACE where REPLACE wrongly copied some column values from the replaced row. - For table handler that doesn't support NULL in keys, we would give an error when creating a primary key with NULL fields, even after the fields has been automaticly converted to NOT NULL. - Creating a primary key on a SPATIAL key, would fail if field was not declared as NOT NULL. Cleanups: - Removed not used condition argument to setup_tables - Removed not needed item function reset_query_id_processor(). - Field->add_index is removed. Now this is instead maintained in (field->flags & FIELD_IN_ADD_INDEX) - Field->fieldnr is removed (use field->field_index instead) - New argument to filesort() to indicate that it should return a set of row pointers (not used columns). This allowed me to remove some references to sql_command in filesort and should also enable us to return column results in some cases where we couldn't before. - Changed column bitmap handling in opt_range.cc to be aligned with TABLE bitmap, which allowed me to use bitmap functions instead of looping over all fields to create some needed bitmaps. (Faster and smaller code) - Broke up found too long lines - Moved some variable declaration at start of function for better code readability. - Removed some not used arguments from functions. (setup_fields(), mysql_prepare_insert_check_table()) - setup_fields() now takes an enum instead of an int for marking columns usage. - For internal temporary tables, use handler::write_row(), handler::delete_row() and handler::update_row() instead of handler::ha_xxxx() for faster execution. - Changed some constants to enum's and define's. - Using separate column read and write sets allows for easier checking of timestamp field was set by statement. - Remove calls to free_io_cache() as this is now done automaticly in ha_reset() - Don't build table->normalized_path as this is now identical to table->path (after bar's fixes to convert filenames) - Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to do comparision with the 'convert-dbug-for-diff' tool. Things left to do in 5.1: - We wrongly log failed CREATE TABLE ... SELECT in some cases when using row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result) Mats has promised to look into this. - Test that my fix for CREATE TABLE ... SELECT is indeed correct. (I added several test cases for this, but in this case it's better that someone else also tests this throughly). Lars has promosed to do this.
20 years ago
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes Changes that requires code changes in other code of other storage engines. (Note that all changes are very straightforward and one should find all issues by compiling a --debug build and fixing all compiler errors and all asserts in field.cc while running the test suite), - New optional handler function introduced: reset() This is called after every DML statement to make it easy for a handler to statement specific cleanups. (The only case it's not called is if force the file to be closed) - handler::extra(HA_EXTRA_RESET) is removed. Code that was there before should be moved to handler::reset() - table->read_set contains a bitmap over all columns that are needed in the query. read_row() and similar functions only needs to read these columns - table->write_set contains a bitmap over all columns that will be updated in the query. write_row() and update_row() only needs to update these columns. The above bitmaps should now be up to date in all context (including ALTER TABLE, filesort()). The handler is informed of any changes to the bitmap after fix_fields() by calling the virtual function handler::column_bitmaps_signal(). If the handler does caching of these bitmaps (instead of using table->read_set, table->write_set), it should redo the caching in this code. as the signal() may be sent several times, it's probably best to set a variable in the signal and redo the caching on read_row() / write_row() if the variable was set. - Removed the read_set and write_set bitmap objects from the handler class - Removed all column bit handling functions from the handler class. (Now one instead uses the normal bitmap functions in my_bitmap.c instead of handler dedicated bitmap functions) - field->query_id is removed. One should instead instead check table->read_set and table->write_set if a field is used in the query. - handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now instead use table->read_set to check for which columns to retrieve. - If a handler needs to call Field->val() or Field->store() on columns that are not used in the query, one should install a temporary all-columns-used map while doing so. For this, we provide the following functions: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set); field->val(); dbug_tmp_restore_column_map(table->read_set, old_map); and similar for the write map: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); field->val(); dbug_tmp_restore_column_map(table->write_set, old_map); If this is not done, you will sooner or later hit a DBUG_ASSERT in the field store() / val() functions. (For not DBUG binaries, the dbug_tmp_restore_column_map() and dbug_tmp_restore_column_map() are inline dummy functions and should be optimized away be the compiler). - If one needs to temporary set the column map for all binaries (and not just to avoid the DBUG_ASSERT() in the Field::store() / Field::val() methods) one should use the functions tmp_use_all_columns() and tmp_restore_column_map() instead of the above dbug_ variants. - All 'status' fields in the handler base class (like records, data_file_length etc) are now stored in a 'stats' struct. This makes it easier to know what status variables are provided by the base handler. This requires some trivial variable names in the extra() function. - New virtual function handler::records(). This is called to optimize COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true. (stats.records is not supposed to be an exact value. It's only has to be 'reasonable enough' for the optimizer to be able to choose a good optimization path). - Non virtual handler::init() function added for caching of virtual constants from engine. - Removed has_transactions() virtual method. Now one should instead return HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support transactions. - The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument that is to be used with 'new handler_name()' to allocate the handler in the right area. The xxxx_create_handler() function is also responsible for any initialization of the object before returning. For example, one should change: static handler *myisam_create_handler(TABLE_SHARE *table) { return new ha_myisam(table); } -> static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root) { return new (mem_root) ha_myisam(table); } - New optional virtual function: use_hidden_primary_key(). This is called in case of an update/delete when (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined but we don't have a primary key. This allows the handler to take precisions in remembering any hidden primary key to able to update/delete any found row. The default handler marks all columns to be read. - handler::table_flags() now returns a ulonglong (to allow for more flags). - New/changed table_flags() - HA_HAS_RECORDS Set if ::records() is supported - HA_NO_TRANSACTIONS Set if engine doesn't support transactions - HA_PRIMARY_KEY_REQUIRED_FOR_DELETE Set if we should mark all primary key columns for read when reading rows as part of a DELETE statement. If there is no primary key, all columns are marked for read. - HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some cases (based on table->read_set) - HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION. - HA_DUPP_POS Renamed to HA_DUPLICATE_POS - HA_REQUIRES_KEY_COLUMNS_FOR_DELETE Set this if we should mark ALL key columns for read when when reading rows as part of a DELETE statement. In case of an update we will mark all keys for read for which key part changed value. - HA_STATS_RECORDS_IS_EXACT Set this if stats.records is exact. (This saves us some extra records() calls when optimizing COUNT(*)) - Removed table_flags() - HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if handler::records() gives an exact count() and HA_STATS_RECORDS_IS_EXACT if stats.records is exact. - HA_READ_RND_SAME Removed (no one supported this one) - Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk() - Renamed handler::dupp_pos to handler::dup_pos - Removed not used variable handler::sortkey Upper level handler changes: - ha_reset() now does some overall checks and calls ::reset() - ha_table_flags() added. This is a cached version of table_flags(). The cache is updated on engine creation time and updated on open. MySQL level changes (not obvious from the above): - DBUG_ASSERT() added to check that column usage matches what is set in the column usage bit maps. (This found a LOT of bugs in current column marking code). - In 5.1 before, all used columns was marked in read_set and only updated columns was marked in write_set. Now we only mark columns for which we need a value in read_set. - Column bitmaps are created in open_binary_frm() and open_table_from_share(). (Before this was in table.cc) - handler::table_flags() calls are replaced with handler::ha_table_flags() - For calling field->val() you must have the corresponding bit set in table->read_set. For calling field->store() you must have the corresponding bit set in table->write_set. (There are asserts in all store()/val() functions to catch wrong usage) - thd->set_query_id is renamed to thd->mark_used_columns and instead of setting this to an integer value, this has now the values: MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE Changed also all variables named 'set_query_id' to mark_used_columns. - In filesort() we now inform the handler of exactly which columns are needed doing the sort and choosing the rows. - The TABLE_SHARE object has a 'all_set' column bitmap one can use when one needs a column bitmap with all columns set. (This is used for table->use_all_columns() and other places) - The TABLE object has 3 column bitmaps: - def_read_set Default bitmap for columns to be read - def_write_set Default bitmap for columns to be written - tmp_set Can be used as a temporary bitmap when needed. The table object has also two pointer to bitmaps read_set and write_set that the handler should use to find out which columns are used in which way. - count() optimization now calls handler::records() instead of using handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true). - Added extra argument to Item::walk() to indicate if we should also traverse sub queries. - Added TABLE parameter to cp_buffer_from_ref() - Don't close tables created with CREATE ... SELECT but keep them in the table cache. (Faster usage of newly created tables). New interfaces: - table->clear_column_bitmaps() to initialize the bitmaps for tables at start of new statements. - table->column_bitmaps_set() to set up new column bitmaps and signal the handler about this. - table->column_bitmaps_set_no_signal() for some few cases where we need to setup new column bitmaps but don't signal the handler (as the handler has already been signaled about these before). Used for the momement only in opt_range.cc when doing ROR scans. - table->use_all_columns() to install a bitmap where all columns are marked as use in the read and the write set. - table->default_column_bitmaps() to install the normal read and write column bitmaps, but not signaling the handler about this. This is mainly used when creating TABLE instances. - table->mark_columns_needed_for_delete(), table->mark_columns_needed_for_delete() and table->mark_columns_needed_for_insert() to allow us to put additional columns in column usage maps if handler so requires. (The handler indicates what it neads in handler->table_flags()) - table->prepare_for_position() to allow us to tell handler that it needs to read primary key parts to be able to store them in future table->position() calls. (This replaces the table->file->ha_retrieve_all_pk function) - table->mark_auto_increment_column() to tell handler are going to update columns part of any auto_increment key. - table->mark_columns_used_by_index() to mark all columns that is part of an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow it to quickly know that it only needs to read colums that are part of the key. (The handler can also use the column map for detecting this, but simpler/faster handler can just monitor the extra() call). - table->mark_columns_used_by_index_no_reset() to in addition to other columns, also mark all columns that is used by the given key. - table->restore_column_maps_after_mark_index() to restore to default column maps after a call to table->mark_columns_used_by_index(). - New item function register_field_in_read_map(), for marking used columns in table->read_map. Used by filesort() to mark all used columns - Maintain in TABLE->merge_keys set of all keys that are used in query. (Simplices some optimization loops) - Maintain Field->part_of_key_not_clustered which is like Field->part_of_key but the field in the clustered key is not assumed to be part of all index. (used in opt_range.cc for faster loops) - dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map() tmp_use_all_columns() and tmp_restore_column_map() functions to temporally mark all columns as usable. The 'dbug_' version is primarily intended inside a handler when it wants to just call Field:store() & Field::val() functions, but don't need the column maps set for any other usage. (ie:: bitmap_is_set() is never called) - We can't use compare_records() to skip updates for handlers that returns a partial column set and the read_set doesn't cover all columns in the write set. The reason for this is that if we have a column marked only for write we can't in the MySQL level know if the value changed or not. The reason this worked before was that MySQL marked all to be written columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden bug'. - open_table_from_share() does not anymore setup temporary MEM_ROOT object as a thread specific variable for the handler. Instead we send the to-be-used MEMROOT to get_new_handler(). (Simpler, faster code) Bugs fixed: - Column marking was not done correctly in a lot of cases. (ALTER TABLE, when using triggers, auto_increment fields etc) (Could potentially result in wrong values inserted in table handlers relying on that the old column maps or field->set_query_id was correct) Especially when it comes to triggers, there may be cases where the old code would cause lost/wrong values for NDB and/or InnoDB tables. - Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags: OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG. This allowed me to remove some wrong warnings about: "Some non-transactional changed tables couldn't be rolled back" - Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset (thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose some warnings about "Some non-transactional changed tables couldn't be rolled back") - Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table() which could cause delete_table to report random failures. - Fixed core dumps for some tests when running with --debug - Added missing FN_LIBCHAR in mysql_rm_tmp_tables() (This has probably caused us to not properly remove temporary files after crash) - slow_logs was not properly initialized, which could maybe cause extra/lost entries in slow log. - If we get an duplicate row on insert, change column map to read and write all columns while retrying the operation. This is required by the definition of REPLACE and also ensures that fields that are only part of UPDATE are properly handled. This fixed a bug in NDB and REPLACE where REPLACE wrongly copied some column values from the replaced row. - For table handler that doesn't support NULL in keys, we would give an error when creating a primary key with NULL fields, even after the fields has been automaticly converted to NOT NULL. - Creating a primary key on a SPATIAL key, would fail if field was not declared as NOT NULL. Cleanups: - Removed not used condition argument to setup_tables - Removed not needed item function reset_query_id_processor(). - Field->add_index is removed. Now this is instead maintained in (field->flags & FIELD_IN_ADD_INDEX) - Field->fieldnr is removed (use field->field_index instead) - New argument to filesort() to indicate that it should return a set of row pointers (not used columns). This allowed me to remove some references to sql_command in filesort and should also enable us to return column results in some cases where we couldn't before. - Changed column bitmap handling in opt_range.cc to be aligned with TABLE bitmap, which allowed me to use bitmap functions instead of looping over all fields to create some needed bitmaps. (Faster and smaller code) - Broke up found too long lines - Moved some variable declaration at start of function for better code readability. - Removed some not used arguments from functions. (setup_fields(), mysql_prepare_insert_check_table()) - setup_fields() now takes an enum instead of an int for marking columns usage. - For internal temporary tables, use handler::write_row(), handler::delete_row() and handler::update_row() instead of handler::ha_xxxx() for faster execution. - Changed some constants to enum's and define's. - Using separate column read and write sets allows for easier checking of timestamp field was set by statement. - Remove calls to free_io_cache() as this is now done automaticly in ha_reset() - Don't build table->normalized_path as this is now identical to table->path (after bar's fixes to convert filenames) - Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to do comparision with the 'convert-dbug-for-diff' tool. Things left to do in 5.1: - We wrongly log failed CREATE TABLE ... SELECT in some cases when using row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result) Mats has promised to look into this. - Test that my fix for CREATE TABLE ... SELECT is indeed correct. (I added several test cases for this, but in this case it's better that someone else also tests this throughly). Lars has promosed to do this.
20 years ago
  1. /* Copyright (C) 2000-2003 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. /*
  13. Low level functions for storing data to be send to the MySQL client
  14. The actual communction is handled by the net_xxx functions in net_serv.cc
  15. */
  16. #ifdef USE_PRAGMA_IMPLEMENTATION
  17. #pragma implementation // gcc: Class implementation
  18. #endif
  19. #include "mysql_priv.h"
  20. #include "sp_rcontext.h"
  21. #include <stdarg.h>
  22. static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
  23. void net_send_error_packet(THD *thd, uint sql_errno, const char *err);
  24. #ifndef EMBEDDED_LIBRARY
  25. static void write_eof_packet(THD *thd, NET *net);
  26. #endif
  27. #ifndef EMBEDDED_LIBRARY
  28. bool Protocol::net_store_data(const char *from, uint length)
  29. #else
  30. bool Protocol_binary::net_store_data(const char *from, uint length)
  31. #endif
  32. {
  33. ulong packet_length=packet->length();
  34. /*
  35. The +9 comes from that strings of length longer than 16M require
  36. 9 bytes to be stored (see net_store_length).
  37. */
  38. if (packet_length+9+length > packet->alloced_length() &&
  39. packet->realloc(packet_length+9+length))
  40. return 1;
  41. char *to=(char*) net_store_length((char*) packet->ptr()+packet_length,
  42. length);
  43. memcpy(to,from,length);
  44. packet->length((uint) (to+length-packet->ptr()));
  45. return 0;
  46. }
  47. /*
  48. Send a error string to client
  49. Design note:
  50. net_printf_error and net_send_error are low-level functions
  51. that shall be used only when a new connection is being
  52. established or at server startup.
  53. For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
  54. critical that every error that can be intercepted is issued in one
  55. place only, my_message_sql.
  56. */
  57. void net_send_error(THD *thd, uint sql_errno, const char *err)
  58. {
  59. NET *net= &thd->net;
  60. bool generate_warning= thd->killed != THD::KILL_CONNECTION;
  61. DBUG_ENTER("net_send_error");
  62. DBUG_PRINT("enter",("sql_errno: %d err: %s", sql_errno,
  63. err ? err : net->last_error[0] ?
  64. net->last_error : "NULL"));
  65. DBUG_ASSERT(!thd->spcont);
  66. if (net && net->no_send_error)
  67. {
  68. thd->clear_error();
  69. thd->is_fatal_error= 0; // Error message is given
  70. DBUG_PRINT("info", ("sending error messages prohibited"));
  71. DBUG_VOID_RETURN;
  72. }
  73. thd->query_error= 1; // needed to catch query errors during replication
  74. if (!err)
  75. {
  76. if (sql_errno)
  77. err=ER(sql_errno);
  78. else
  79. {
  80. if ((err=net->last_error)[0])
  81. {
  82. sql_errno=net->last_errno;
  83. generate_warning= 0; // This warning has already been given
  84. }
  85. else
  86. {
  87. sql_errno=ER_UNKNOWN_ERROR;
  88. err=ER(sql_errno); /* purecov: inspected */
  89. }
  90. }
  91. }
  92. if (generate_warning)
  93. {
  94. /* Error that we have not got with my_error() */
  95. push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, sql_errno, err);
  96. }
  97. net_send_error_packet(thd, sql_errno, err);
  98. thd->is_fatal_error= 0; // Error message is given
  99. thd->net.report_error= 0;
  100. /* Abort multi-result sets */
  101. thd->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
  102. DBUG_VOID_RETURN;
  103. }
  104. /*
  105. Write error package and flush to client
  106. It's a little too low level, but I don't want to use another buffer for
  107. this
  108. Design note:
  109. net_printf_error and net_send_error are low-level functions
  110. that shall be used only when a new connection is being
  111. established or at server startup.
  112. For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
  113. critical that every error that can be intercepted is issued in one
  114. place only, my_message_sql.
  115. */
  116. void
  117. net_printf_error(THD *thd, uint errcode, ...)
  118. {
  119. va_list args;
  120. uint length,offset;
  121. const char *format;
  122. #ifndef EMBEDDED_LIBRARY
  123. const char *text_pos;
  124. int head_length= NET_HEADER_SIZE;
  125. #else
  126. char text_pos[1024];
  127. #endif
  128. NET *net= &thd->net;
  129. DBUG_ENTER("net_printf_error");
  130. DBUG_PRINT("enter",("message: %u",errcode));
  131. DBUG_ASSERT(!thd->spcont);
  132. if (net && net->no_send_error)
  133. {
  134. thd->clear_error();
  135. thd->is_fatal_error= 0; // Error message is given
  136. DBUG_PRINT("info", ("sending error messages prohibited"));
  137. DBUG_VOID_RETURN;
  138. }
  139. thd->query_error= 1; // needed to catch query errors during replication
  140. #ifndef EMBEDDED_LIBRARY
  141. query_cache_abort(net); // Safety
  142. #endif
  143. va_start(args,errcode);
  144. /*
  145. The following is needed to make net_printf_error() work with 0 argument
  146. for errorcode and use the argument after that as the format string. This
  147. is useful for rare errors that are not worth the hassle to put in
  148. errmsg.sys, but at the same time, the message is not fixed text
  149. */
  150. if (errcode)
  151. format= ER(errcode);
  152. else
  153. {
  154. format=va_arg(args,char*);
  155. errcode= ER_UNKNOWN_ERROR;
  156. }
  157. offset= (net->return_errno ?
  158. ((thd->client_capabilities & CLIENT_PROTOCOL_41) ?
  159. 2+SQLSTATE_LENGTH+1 : 2) : 0);
  160. #ifndef EMBEDDED_LIBRARY
  161. text_pos=(char*) net->buff + head_length + offset + 1;
  162. length= (uint) ((char*)net->buff_end - text_pos);
  163. #else
  164. length=sizeof(text_pos)-1;
  165. #endif
  166. length=my_vsnprintf(my_const_cast(char*) (text_pos),
  167. min(length, sizeof(net->last_error)),
  168. format,args);
  169. va_end(args);
  170. /* Replication slave relies on net->last_* to see if there was error */
  171. net->last_errno= errcode;
  172. strmake(net->last_error, text_pos, sizeof(net->last_error)-1);
  173. #ifndef EMBEDDED_LIBRARY
  174. if (net->vio == 0)
  175. {
  176. if (thd->bootstrap)
  177. {
  178. /*
  179. In bootstrap it's ok to print on stderr
  180. This may also happen when we get an error from a slave thread
  181. */
  182. fprintf(stderr,"ERROR: %d %s\n",errcode,text_pos);
  183. thd->fatal_error();
  184. }
  185. DBUG_VOID_RETURN;
  186. }
  187. int3store(net->buff,length+1+offset);
  188. net->buff[3]= (net->compress) ? 0 : (uchar) (net->pkt_nr++);
  189. net->buff[head_length]=(uchar) 255; // Error package
  190. if (offset)
  191. {
  192. uchar *pos= net->buff+head_length+1;
  193. int2store(pos, errcode);
  194. if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  195. {
  196. pos[2]= '#'; /* To make the protocol backward compatible */
  197. memcpy(pos+3, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
  198. }
  199. }
  200. VOID(net_real_write(net,(char*) net->buff,length+head_length+1+offset));
  201. #else
  202. net->last_errno= errcode;
  203. strmake(net->last_error, text_pos, length);
  204. strmake(net->sqlstate, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
  205. #endif
  206. if (thd->killed != THD::KILL_CONNECTION)
  207. push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, errcode,
  208. text_pos ? text_pos : ER(errcode));
  209. thd->is_fatal_error=0; // Error message is given
  210. DBUG_VOID_RETURN;
  211. }
  212. /*
  213. Return ok to the client.
  214. SYNOPSIS
  215. send_ok()
  216. thd Thread handler
  217. affected_rows Number of rows changed by statement
  218. id Auto_increment id for first row (if used)
  219. message Message to send to the client (Used by mysql_status)
  220. DESCRIPTION
  221. The ok packet has the following structure
  222. 0 Marker (1 byte)
  223. affected_rows Stored in 1-9 bytes
  224. id Stored in 1-9 bytes
  225. server_status Copy of thd->server_status; Can be used by client
  226. to check if we are inside an transaction
  227. New in 4.0 protocol
  228. warning_count Stored in 2 bytes; New in 4.1 protocol
  229. message Stored as packed length (1-9 bytes) + message
  230. Is not stored if no message
  231. If net->no_send_ok return without sending packet
  232. */
  233. #ifndef EMBEDDED_LIBRARY
  234. void
  235. send_ok(THD *thd, ha_rows affected_rows, ulonglong id, const char *message)
  236. {
  237. NET *net= &thd->net;
  238. char buff[MYSQL_ERRMSG_SIZE+10],*pos;
  239. DBUG_ENTER("send_ok");
  240. if (net->no_send_ok || !net->vio) // hack for re-parsing queries
  241. {
  242. DBUG_PRINT("info", ("no send ok: %s, vio present: %s",
  243. (net->no_send_ok ? "YES" : "NO"),
  244. (net->vio ? "YES" : "NO")));
  245. DBUG_VOID_RETURN;
  246. }
  247. buff[0]=0; // No fields
  248. pos=net_store_length(buff+1,affected_rows);
  249. pos=net_store_length(pos, id);
  250. if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  251. {
  252. DBUG_PRINT("info",
  253. ("affected_rows: %lu id: %lu status: %u warning_count: %u",
  254. (ulong) affected_rows,
  255. (ulong) id,
  256. (uint) (thd->server_status & 0xffff),
  257. (uint) thd->total_warn_count));
  258. int2store(pos,thd->server_status);
  259. pos+=2;
  260. /* We can only return up to 65535 warnings in two bytes */
  261. uint tmp= min(thd->total_warn_count, 65535);
  262. int2store(pos, tmp);
  263. pos+= 2;
  264. }
  265. else if (net->return_status) // For 4.0 protocol
  266. {
  267. int2store(pos,thd->server_status);
  268. pos+=2;
  269. }
  270. if (message)
  271. pos=net_store_data((char*) pos, message, strlen(message));
  272. VOID(my_net_write(net,buff,(uint) (pos-buff)));
  273. VOID(net_flush(net));
  274. /* We can't anymore send an error to the client */
  275. thd->net.report_error= 0;
  276. thd->net.no_send_error= 1;
  277. DBUG_PRINT("info", ("OK sent, so no more error sending allowed"));
  278. DBUG_VOID_RETURN;
  279. }
  280. static char eof_buff[1]= { (char) 254 }; /* Marker for end of fields */
  281. /*
  282. Send eof (= end of result set) to the client
  283. SYNOPSIS
  284. send_eof()
  285. thd Thread handler
  286. no_flush Set to 1 if there will be more data to the client,
  287. like in send_fields().
  288. DESCRIPTION
  289. The eof packet has the following structure
  290. 254 Marker (1 byte)
  291. warning_count Stored in 2 bytes; New in 4.1 protocol
  292. status_flag Stored in 2 bytes;
  293. For flags like SERVER_MORE_RESULTS_EXISTS
  294. Note that the warning count will not be sent if 'no_flush' is set as
  295. we don't want to report the warning count until all data is sent to the
  296. client.
  297. */
  298. void
  299. send_eof(THD *thd)
  300. {
  301. NET *net= &thd->net;
  302. DBUG_ENTER("send_eof");
  303. if (net->vio != 0 && !net->no_send_eof)
  304. {
  305. write_eof_packet(thd, net);
  306. VOID(net_flush(net));
  307. thd->net.no_send_error= 1;
  308. DBUG_PRINT("info", ("EOF sent, so no more error sending allowed"));
  309. }
  310. DBUG_VOID_RETURN;
  311. }
  312. /*
  313. Format EOF packet according to the current protocol and
  314. write it to the network output buffer.
  315. */
  316. static void write_eof_packet(THD *thd, NET *net)
  317. {
  318. if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  319. {
  320. uchar buff[5];
  321. /*
  322. Don't send warn count during SP execution, as the warn_list
  323. is cleared between substatements, and mysqltest gets confused
  324. */
  325. uint tmp= (thd->spcont ? 0 : min(thd->total_warn_count, 65535));
  326. buff[0]= 254;
  327. int2store(buff+1, tmp);
  328. /*
  329. The following test should never be true, but it's better to do it
  330. because if 'is_fatal_error' is set the server is not going to execute
  331. other queries (see the if test in dispatch_command / COM_QUERY)
  332. */
  333. if (thd->is_fatal_error)
  334. thd->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
  335. int2store(buff+3, thd->server_status);
  336. VOID(my_net_write(net, (char*) buff, 5));
  337. }
  338. else
  339. VOID(my_net_write(net, eof_buff, 1));
  340. }
  341. /*
  342. Please client to send scrambled_password in old format.
  343. SYNOPSYS
  344. send_old_password_request()
  345. thd thread handle
  346. RETURN VALUE
  347. 0 ok
  348. !0 error
  349. */
  350. bool send_old_password_request(THD *thd)
  351. {
  352. NET *net= &thd->net;
  353. return my_net_write(net, eof_buff, 1) || net_flush(net);
  354. }
  355. void net_send_error_packet(THD *thd, uint sql_errno, const char *err)
  356. {
  357. NET *net= &thd->net;
  358. uint length;
  359. char buff[MYSQL_ERRMSG_SIZE+2], *pos;
  360. DBUG_ENTER("send_error_packet");
  361. if (net->vio == 0)
  362. {
  363. if (thd->bootstrap)
  364. {
  365. /* In bootstrap it's ok to print on stderr */
  366. fprintf(stderr,"ERROR: %d %s\n",sql_errno,err);
  367. }
  368. DBUG_VOID_RETURN;
  369. }
  370. if (net->return_errno)
  371. { // new client code; Add errno before message
  372. int2store(buff,sql_errno);
  373. pos= buff+2;
  374. if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  375. {
  376. /* The first # is to make the protocol backward compatible */
  377. buff[2]= '#';
  378. pos= strmov(buff+3, mysql_errno_to_sqlstate(sql_errno));
  379. }
  380. length= (uint) (strmake(pos, err, MYSQL_ERRMSG_SIZE-1) - buff);
  381. err=buff;
  382. }
  383. else
  384. {
  385. length=(uint) strlen(err);
  386. set_if_smaller(length,MYSQL_ERRMSG_SIZE-1);
  387. }
  388. VOID(net_write_command(net,(uchar) 255, "", 0, (char*) err,length));
  389. DBUG_VOID_RETURN;
  390. }
  391. #endif /* EMBEDDED_LIBRARY */
  392. /*
  393. Faster net_store_length when we know that length is less than 65536.
  394. We keep a separate version for that range because it's widely used in
  395. libmysql.
  396. uint is used as agrument type because of MySQL type conventions:
  397. uint for 0..65536
  398. ulong for 0..4294967296
  399. ulonglong for bigger numbers.
  400. */
  401. static char *net_store_length_fast(char *pkg, uint length)
  402. {
  403. uchar *packet=(uchar*) pkg;
  404. if (length < 251)
  405. {
  406. *packet=(uchar) length;
  407. return (char*) packet+1;
  408. }
  409. *packet++=252;
  410. int2store(packet,(uint) length);
  411. return (char*) packet+2;
  412. }
  413. /****************************************************************************
  414. Functions used by the protocol functions (like send_ok) to store strings
  415. and numbers in the header result packet.
  416. ****************************************************************************/
  417. /* The following will only be used for short strings < 65K */
  418. char *net_store_data(char *to,const char *from, uint length)
  419. {
  420. to=net_store_length_fast(to,length);
  421. memcpy(to,from,length);
  422. return to+length;
  423. }
  424. char *net_store_data(char *to,int32 from)
  425. {
  426. char buff[20];
  427. uint length=(uint) (int10_to_str(from,buff,10)-buff);
  428. to=net_store_length_fast(to,length);
  429. memcpy(to,buff,length);
  430. return to+length;
  431. }
  432. char *net_store_data(char *to,longlong from)
  433. {
  434. char buff[22];
  435. uint length=(uint) (longlong10_to_str(from,buff,10)-buff);
  436. to=net_store_length_fast(to,length);
  437. memcpy(to,buff,length);
  438. return to+length;
  439. }
  440. /*****************************************************************************
  441. Default Protocol functions
  442. *****************************************************************************/
  443. void Protocol::init(THD *thd_arg)
  444. {
  445. thd=thd_arg;
  446. packet= &thd->packet;
  447. convert= &thd->convert_buffer;
  448. #ifndef DBUG_OFF
  449. field_types= 0;
  450. #endif
  451. }
  452. bool Protocol::flush()
  453. {
  454. #ifndef EMBEDDED_LIBRARY
  455. return net_flush(&thd->net);
  456. #else
  457. return 0;
  458. #endif
  459. }
  460. /*
  461. Send name and type of result to client.
  462. SYNOPSIS
  463. send_fields()
  464. THD Thread data object
  465. list List of items to send to client
  466. flag Bit mask with the following functions:
  467. 1 send number of rows
  468. 2 send default values
  469. 4 don't write eof packet
  470. DESCRIPTION
  471. Sum fields has table name empty and field_name.
  472. RETURN VALUES
  473. 0 ok
  474. 1 Error (Note that in this case the error is not sent to the client)
  475. */
  476. #ifndef EMBEDDED_LIBRARY
  477. bool Protocol::send_fields(List<Item> *list, uint flags)
  478. {
  479. List_iterator_fast<Item> it(*list);
  480. Item *item;
  481. char buff[80];
  482. String tmp((char*) buff,sizeof(buff),&my_charset_bin);
  483. Protocol_text prot(thd);
  484. String *local_packet= prot.storage_packet();
  485. CHARSET_INFO *thd_charset= thd->variables.character_set_results;
  486. DBUG_ENTER("send_fields");
  487. if (flags & SEND_NUM_ROWS)
  488. { // Packet with number of elements
  489. char *pos=net_store_length(buff, list->elements);
  490. (void) my_net_write(&thd->net, buff,(uint) (pos-buff));
  491. }
  492. #ifndef DBUG_OFF
  493. field_types= (enum_field_types*) thd->alloc(sizeof(field_types) *
  494. list->elements);
  495. uint count= 0;
  496. #endif
  497. while ((item=it++))
  498. {
  499. char *pos;
  500. CHARSET_INFO *cs= system_charset_info;
  501. Send_field field;
  502. item->make_field(&field);
  503. /* Keep things compatible for old clients */
  504. if (field.type == MYSQL_TYPE_VARCHAR)
  505. field.type= MYSQL_TYPE_VAR_STRING;
  506. prot.prepare_for_resend();
  507. if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  508. {
  509. if (prot.store(STRING_WITH_LEN("def"), cs, thd_charset) ||
  510. prot.store(field.db_name, (uint) strlen(field.db_name),
  511. cs, thd_charset) ||
  512. prot.store(field.table_name, (uint) strlen(field.table_name),
  513. cs, thd_charset) ||
  514. prot.store(field.org_table_name, (uint) strlen(field.org_table_name),
  515. cs, thd_charset) ||
  516. prot.store(field.col_name, (uint) strlen(field.col_name),
  517. cs, thd_charset) ||
  518. prot.store(field.org_col_name, (uint) strlen(field.org_col_name),
  519. cs, thd_charset) ||
  520. local_packet->realloc(local_packet->length()+12))
  521. goto err;
  522. /* Store fixed length fields */
  523. pos= (char*) local_packet->ptr()+local_packet->length();
  524. *pos++= 12; // Length of packed fields
  525. if (item->collation.collation == &my_charset_bin || thd_charset == NULL)
  526. {
  527. /* No conversion */
  528. int2store(pos, field.charsetnr);
  529. int4store(pos+2, field.length);
  530. }
  531. else
  532. {
  533. /* With conversion */
  534. uint max_char_len;
  535. int2store(pos, thd_charset->number);
  536. /*
  537. For TEXT/BLOB columns, field_length describes the maximum data
  538. length in bytes. There is no limit to the number of characters
  539. that a TEXT column can store, as long as the data fits into
  540. the designated space.
  541. For the rest of textual columns, field_length is evaluated as
  542. char_count * mbmaxlen, where character count is taken from the
  543. definition of the column. In other words, the maximum number
  544. of characters here is limited by the column definition.
  545. */
  546. max_char_len= (field.type >= (int) MYSQL_TYPE_TINY_BLOB &&
  547. field.type <= (int) MYSQL_TYPE_BLOB) ?
  548. field.length / item->collation.collation->mbminlen :
  549. field.length / item->collation.collation->mbmaxlen;
  550. int4store(pos+2, max_char_len * thd_charset->mbmaxlen);
  551. }
  552. pos[6]= field.type;
  553. int2store(pos+7,field.flags);
  554. pos[9]= (char) field.decimals;
  555. pos[10]= 0; // For the future
  556. pos[11]= 0; // For the future
  557. pos+= 12;
  558. }
  559. else
  560. {
  561. if (prot.store(field.table_name, (uint) strlen(field.table_name),
  562. cs, thd_charset) ||
  563. prot.store(field.col_name, (uint) strlen(field.col_name),
  564. cs, thd_charset) ||
  565. local_packet->realloc(local_packet->length()+10))
  566. goto err;
  567. pos= (char*) local_packet->ptr()+local_packet->length();
  568. #ifdef TO_BE_DELETED_IN_6
  569. if (!(thd->client_capabilities & CLIENT_LONG_FLAG))
  570. {
  571. pos[0]=3;
  572. int3store(pos+1,field.length);
  573. pos[4]=1;
  574. pos[5]=field.type;
  575. pos[6]=2;
  576. pos[7]= (char) field.flags;
  577. pos[8]= (char) field.decimals;
  578. pos+= 9;
  579. }
  580. else
  581. #endif
  582. {
  583. pos[0]=3;
  584. int3store(pos+1,field.length);
  585. pos[4]=1;
  586. pos[5]=field.type;
  587. pos[6]=3;
  588. int2store(pos+7,field.flags);
  589. pos[9]= (char) field.decimals;
  590. pos+= 10;
  591. }
  592. }
  593. local_packet->length((uint) (pos - local_packet->ptr()));
  594. if (flags & SEND_DEFAULTS)
  595. item->send(&prot, &tmp); // Send default value
  596. if (prot.write())
  597. break; /* purecov: inspected */
  598. #ifndef DBUG_OFF
  599. field_types[count++]= field.type;
  600. #endif
  601. }
  602. if (flags & SEND_EOF)
  603. write_eof_packet(thd, &thd->net);
  604. DBUG_RETURN(prepare_for_send(list));
  605. err:
  606. my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES),
  607. MYF(0)); /* purecov: inspected */
  608. DBUG_RETURN(1); /* purecov: inspected */
  609. }
  610. bool Protocol::write()
  611. {
  612. DBUG_ENTER("Protocol::write");
  613. DBUG_RETURN(my_net_write(&thd->net, packet->ptr(), packet->length()));
  614. }
  615. #endif /* EMBEDDED_LIBRARY */
  616. /*
  617. Send \0 end terminated string
  618. SYNOPSIS
  619. store()
  620. from NullS or \0 terminated string
  621. NOTES
  622. In most cases one should use store(from, length) instead of this function
  623. RETURN VALUES
  624. 0 ok
  625. 1 error
  626. */
  627. bool Protocol::store(const char *from, CHARSET_INFO *cs)
  628. {
  629. if (!from)
  630. return store_null();
  631. uint length= strlen(from);
  632. return store(from, length, cs);
  633. }
  634. /*
  635. Send a set of strings as one long string with ',' in between
  636. */
  637. bool Protocol::store(I_List<i_string>* str_list)
  638. {
  639. char buf[256];
  640. String tmp(buf, sizeof(buf), &my_charset_bin);
  641. uint32 len;
  642. I_List_iterator<i_string> it(*str_list);
  643. i_string* s;
  644. tmp.length(0);
  645. while ((s=it++))
  646. {
  647. tmp.append(s->ptr);
  648. tmp.append(',');
  649. }
  650. if ((len= tmp.length()))
  651. len--; // Remove last ','
  652. return store((char*) tmp.ptr(), len, tmp.charset());
  653. }
  654. /****************************************************************************
  655. Functions to handle the simple (default) protocol where everything is
  656. This protocol is the one that is used by default between the MySQL server
  657. and client when you are not using prepared statements.
  658. All data are sent as 'packed-string-length' followed by 'string-data'
  659. ****************************************************************************/
  660. #ifndef EMBEDDED_LIBRARY
  661. void Protocol_text::prepare_for_resend()
  662. {
  663. packet->length(0);
  664. #ifndef DBUG_OFF
  665. field_pos= 0;
  666. #endif
  667. }
  668. bool Protocol_text::store_null()
  669. {
  670. #ifndef DBUG_OFF
  671. field_pos++;
  672. #endif
  673. char buff[1];
  674. buff[0]= (char)251;
  675. return packet->append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
  676. }
  677. #endif
  678. /*
  679. Auxilary function to convert string to the given character set
  680. and store in network buffer.
  681. */
  682. bool Protocol::store_string_aux(const char *from, uint length,
  683. CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
  684. {
  685. /* 'tocs' is set 0 when client issues SET character_set_results=NULL */
  686. if (tocs && !my_charset_same(fromcs, tocs) &&
  687. fromcs != &my_charset_bin &&
  688. tocs != &my_charset_bin)
  689. {
  690. uint dummy_errors;
  691. return convert->copy(from, length, fromcs, tocs, &dummy_errors) ||
  692. net_store_data(convert->ptr(), convert->length());
  693. }
  694. return net_store_data(from, length);
  695. }
  696. bool Protocol_text::store(const char *from, uint length,
  697. CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
  698. {
  699. #ifndef DBUG_OFF
  700. DBUG_ASSERT(field_types == 0 ||
  701. field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
  702. field_types[field_pos] == MYSQL_TYPE_BIT ||
  703. field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL ||
  704. (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
  705. field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
  706. field_pos++;
  707. #endif
  708. return store_string_aux(from, length, fromcs, tocs);
  709. }
  710. bool Protocol_text::store(const char *from, uint length,
  711. CHARSET_INFO *fromcs)
  712. {
  713. CHARSET_INFO *tocs= this->thd->variables.character_set_results;
  714. #ifndef DBUG_OFF
  715. DBUG_ASSERT(field_types == 0 ||
  716. field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
  717. field_types[field_pos] == MYSQL_TYPE_BIT ||
  718. field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL ||
  719. (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
  720. field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
  721. field_pos++;
  722. #endif
  723. return store_string_aux(from, length, fromcs, tocs);
  724. }
  725. bool Protocol_text::store_tiny(longlong from)
  726. {
  727. #ifndef DBUG_OFF
  728. DBUG_ASSERT(field_types == 0 || field_types[field_pos] == MYSQL_TYPE_TINY);
  729. field_pos++;
  730. #endif
  731. char buff[20];
  732. return net_store_data((char*) buff,
  733. (uint) (int10_to_str((int) from,buff, -10)-buff));
  734. }
  735. bool Protocol_text::store_short(longlong from)
  736. {
  737. #ifndef DBUG_OFF
  738. DBUG_ASSERT(field_types == 0 ||
  739. field_types[field_pos] == MYSQL_TYPE_YEAR ||
  740. field_types[field_pos] == MYSQL_TYPE_SHORT);
  741. field_pos++;
  742. #endif
  743. char buff[20];
  744. return net_store_data((char*) buff,
  745. (uint) (int10_to_str((int) from,buff, -10)-buff));
  746. }
  747. bool Protocol_text::store_long(longlong from)
  748. {
  749. #ifndef DBUG_OFF
  750. DBUG_ASSERT(field_types == 0 ||
  751. field_types[field_pos] == MYSQL_TYPE_INT24 ||
  752. field_types[field_pos] == MYSQL_TYPE_LONG);
  753. field_pos++;
  754. #endif
  755. char buff[20];
  756. return net_store_data((char*) buff,
  757. (uint) (int10_to_str((long int)from,buff, (from <0)?-10:10)-buff));
  758. }
  759. bool Protocol_text::store_longlong(longlong from, bool unsigned_flag)
  760. {
  761. #ifndef DBUG_OFF
  762. DBUG_ASSERT(field_types == 0 ||
  763. field_types[field_pos] == MYSQL_TYPE_LONGLONG);
  764. field_pos++;
  765. #endif
  766. char buff[22];
  767. return net_store_data((char*) buff,
  768. (uint) (longlong10_to_str(from,buff,
  769. unsigned_flag ? 10 : -10)-
  770. buff));
  771. }
  772. bool Protocol_text::store_decimal(const my_decimal *d)
  773. {
  774. #ifndef DBUG_OFF
  775. DBUG_ASSERT(field_types == 0 ||
  776. field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL);
  777. field_pos++;
  778. #endif
  779. char buff[DECIMAL_MAX_STR_LENGTH];
  780. String str(buff, sizeof(buff), &my_charset_bin);
  781. (void) my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
  782. return net_store_data(str.ptr(), str.length());
  783. }
  784. bool Protocol_text::store(float from, uint32 decimals, String *buffer)
  785. {
  786. #ifndef DBUG_OFF
  787. DBUG_ASSERT(field_types == 0 ||
  788. field_types[field_pos] == MYSQL_TYPE_FLOAT);
  789. field_pos++;
  790. #endif
  791. buffer->set_real((double) from, decimals, thd->charset());
  792. return net_store_data((char*) buffer->ptr(), buffer->length());
  793. }
  794. bool Protocol_text::store(double from, uint32 decimals, String *buffer)
  795. {
  796. #ifndef DBUG_OFF
  797. DBUG_ASSERT(field_types == 0 ||
  798. field_types[field_pos] == MYSQL_TYPE_DOUBLE);
  799. field_pos++;
  800. #endif
  801. buffer->set_real(from, decimals, thd->charset());
  802. return net_store_data((char*) buffer->ptr(), buffer->length());
  803. }
  804. bool Protocol_text::store(Field *field)
  805. {
  806. if (field->is_null())
  807. return store_null();
  808. #ifndef DBUG_OFF
  809. field_pos++;
  810. #endif
  811. char buff[MAX_FIELD_WIDTH];
  812. String str(buff,sizeof(buff), &my_charset_bin);
  813. CHARSET_INFO *tocs= this->thd->variables.character_set_results;
  814. #ifndef DBUG_OFF
  815. TABLE *table= field->table;
  816. my_bitmap_map *old_map= 0;
  817. if (table->file)
  818. old_map= dbug_tmp_use_all_columns(table, table->read_set);
  819. #endif
  820. field->val_str(&str);
  821. #ifndef DBUG_OFF
  822. if (old_map)
  823. dbug_tmp_restore_column_map(table->read_set, old_map);
  824. #endif
  825. return store_string_aux(str.ptr(), str.length(), str.charset(), tocs);
  826. }
  827. /*
  828. TODO:
  829. Second_part format ("%06") needs to change when
  830. we support 0-6 decimals for time.
  831. */
  832. bool Protocol_text::store(MYSQL_TIME *tm)
  833. {
  834. #ifndef DBUG_OFF
  835. DBUG_ASSERT(field_types == 0 ||
  836. field_types[field_pos] == MYSQL_TYPE_DATETIME ||
  837. field_types[field_pos] == MYSQL_TYPE_TIMESTAMP);
  838. field_pos++;
  839. #endif
  840. char buff[40];
  841. uint length;
  842. length= my_sprintf(buff,(buff, "%04d-%02d-%02d %02d:%02d:%02d",
  843. (int) tm->year,
  844. (int) tm->month,
  845. (int) tm->day,
  846. (int) tm->hour,
  847. (int) tm->minute,
  848. (int) tm->second));
  849. if (tm->second_part)
  850. length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
  851. return net_store_data((char*) buff, length);
  852. }
  853. bool Protocol_text::store_date(MYSQL_TIME *tm)
  854. {
  855. #ifndef DBUG_OFF
  856. DBUG_ASSERT(field_types == 0 ||
  857. field_types[field_pos] == MYSQL_TYPE_DATE);
  858. field_pos++;
  859. #endif
  860. char buff[MAX_DATE_STRING_REP_LENGTH];
  861. int length= my_date_to_str(tm, buff);
  862. return net_store_data(buff, (uint) length);
  863. }
  864. /*
  865. TODO:
  866. Second_part format ("%06") needs to change when
  867. we support 0-6 decimals for time.
  868. */
  869. bool Protocol_text::store_time(MYSQL_TIME *tm)
  870. {
  871. #ifndef DBUG_OFF
  872. DBUG_ASSERT(field_types == 0 ||
  873. field_types[field_pos] == MYSQL_TYPE_TIME);
  874. field_pos++;
  875. #endif
  876. char buff[40];
  877. uint length;
  878. uint day= (tm->year || tm->month) ? 0 : tm->day;
  879. length= my_sprintf(buff,(buff, "%s%02ld:%02d:%02d",
  880. tm->neg ? "-" : "",
  881. (long) day*24L+(long) tm->hour,
  882. (int) tm->minute,
  883. (int) tm->second));
  884. if (tm->second_part)
  885. length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
  886. return net_store_data((char*) buff, length);
  887. }
  888. /****************************************************************************
  889. Functions to handle the binary protocol used with prepared statements
  890. Data format:
  891. [ok:1] reserved ok packet
  892. [null_field:(field_count+7+2)/8] reserved to send null data. The size is
  893. calculated using:
  894. bit_fields= (field_count+7+2)/8;
  895. 2 bits are reserved for identifying type
  896. of package.
  897. [[length]data] data field (the length applies only for
  898. string/binary/time/timestamp fields and
  899. rest of them are not sent as they have
  900. the default length that client understands
  901. based on the field type
  902. [..]..[[length]data] data
  903. ****************************************************************************/
  904. bool Protocol_binary::prepare_for_send(List<Item> *item_list)
  905. {
  906. Protocol::prepare_for_send(item_list);
  907. bit_fields= (field_count+9)/8;
  908. if (packet->alloc(bit_fields+1))
  909. return 1;
  910. /* prepare_for_resend will be called after this one */
  911. return 0;
  912. }
  913. void Protocol_binary::prepare_for_resend()
  914. {
  915. packet->length(bit_fields+1);
  916. bzero((char*) packet->ptr(), 1+bit_fields);
  917. field_pos=0;
  918. }
  919. bool Protocol_binary::store(const char *from, uint length,
  920. CHARSET_INFO *fromcs)
  921. {
  922. CHARSET_INFO *tocs= thd->variables.character_set_results;
  923. field_pos++;
  924. return store_string_aux(from, length, fromcs, tocs);
  925. }
  926. bool Protocol_binary::store(const char *from,uint length,
  927. CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
  928. {
  929. field_pos++;
  930. return store_string_aux(from, length, fromcs, tocs);
  931. }
  932. bool Protocol_binary::store_null()
  933. {
  934. uint offset= (field_pos+2)/8+1, bit= (1 << ((field_pos+2) & 7));
  935. /* Room for this as it's allocated in prepare_for_send */
  936. char *to= (char*) packet->ptr()+offset;
  937. *to= (char) ((uchar) *to | (uchar) bit);
  938. field_pos++;
  939. return 0;
  940. }
  941. bool Protocol_binary::store_tiny(longlong from)
  942. {
  943. char buff[1];
  944. field_pos++;
  945. buff[0]= (uchar) from;
  946. return packet->append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
  947. }
  948. bool Protocol_binary::store_short(longlong from)
  949. {
  950. field_pos++;
  951. char *to= packet->prep_append(2, PACKET_BUFFER_EXTRA_ALLOC);
  952. if (!to)
  953. return 1;
  954. int2store(to, (int) from);
  955. return 0;
  956. }
  957. bool Protocol_binary::store_long(longlong from)
  958. {
  959. field_pos++;
  960. char *to= packet->prep_append(4, PACKET_BUFFER_EXTRA_ALLOC);
  961. if (!to)
  962. return 1;
  963. int4store(to, from);
  964. return 0;
  965. }
  966. bool Protocol_binary::store_longlong(longlong from, bool unsigned_flag)
  967. {
  968. field_pos++;
  969. char *to= packet->prep_append(8, PACKET_BUFFER_EXTRA_ALLOC);
  970. if (!to)
  971. return 1;
  972. int8store(to, from);
  973. return 0;
  974. }
  975. bool Protocol_binary::store_decimal(const my_decimal *d)
  976. {
  977. #ifndef DBUG_OFF
  978. DBUG_ASSERT(field_types == 0 ||
  979. field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL);
  980. field_pos++;
  981. #endif
  982. char buff[DECIMAL_MAX_STR_LENGTH];
  983. String str(buff, sizeof(buff), &my_charset_bin);
  984. (void) my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
  985. return store(str.ptr(), str.length(), str.charset());
  986. }
  987. bool Protocol_binary::store(float from, uint32 decimals, String *buffer)
  988. {
  989. field_pos++;
  990. char *to= packet->prep_append(4, PACKET_BUFFER_EXTRA_ALLOC);
  991. if (!to)
  992. return 1;
  993. float4store(to, from);
  994. return 0;
  995. }
  996. bool Protocol_binary::store(double from, uint32 decimals, String *buffer)
  997. {
  998. field_pos++;
  999. char *to= packet->prep_append(8, PACKET_BUFFER_EXTRA_ALLOC);
  1000. if (!to)
  1001. return 1;
  1002. float8store(to, from);
  1003. return 0;
  1004. }
  1005. bool Protocol_binary::store(Field *field)
  1006. {
  1007. /*
  1008. We should not increment field_pos here as send_binary() will call another
  1009. protocol function to do this for us
  1010. */
  1011. if (field->is_null())
  1012. return store_null();
  1013. return field->send_binary(this);
  1014. }
  1015. bool Protocol_binary::store(MYSQL_TIME *tm)
  1016. {
  1017. char buff[12],*pos;
  1018. uint length;
  1019. field_pos++;
  1020. pos= buff+1;
  1021. int2store(pos, tm->year);
  1022. pos[2]= (uchar) tm->month;
  1023. pos[3]= (uchar) tm->day;
  1024. pos[4]= (uchar) tm->hour;
  1025. pos[5]= (uchar) tm->minute;
  1026. pos[6]= (uchar) tm->second;
  1027. int4store(pos+7, tm->second_part);
  1028. if (tm->second_part)
  1029. length=11;
  1030. else if (tm->hour || tm->minute || tm->second)
  1031. length=7;
  1032. else if (tm->year || tm->month || tm->day)
  1033. length=4;
  1034. else
  1035. length=0;
  1036. buff[0]=(char) length; // Length is stored first
  1037. return packet->append(buff, length+1, PACKET_BUFFER_EXTRA_ALLOC);
  1038. }
  1039. bool Protocol_binary::store_date(MYSQL_TIME *tm)
  1040. {
  1041. tm->hour= tm->minute= tm->second=0;
  1042. tm->second_part= 0;
  1043. return Protocol_binary::store(tm);
  1044. }
  1045. bool Protocol_binary::store_time(MYSQL_TIME *tm)
  1046. {
  1047. char buff[13], *pos;
  1048. uint length;
  1049. field_pos++;
  1050. pos= buff+1;
  1051. pos[0]= tm->neg ? 1 : 0;
  1052. if (tm->hour >= 24)
  1053. {
  1054. /* Fix if we come from Item::send */
  1055. uint days= tm->hour/24;
  1056. tm->hour-= days*24;
  1057. tm->day+= days;
  1058. }
  1059. int4store(pos+1, tm->day);
  1060. pos[5]= (uchar) tm->hour;
  1061. pos[6]= (uchar) tm->minute;
  1062. pos[7]= (uchar) tm->second;
  1063. int4store(pos+8, tm->second_part);
  1064. if (tm->second_part)
  1065. length=12;
  1066. else if (tm->hour || tm->minute || tm->second || tm->day)
  1067. length=8;
  1068. else
  1069. length=0;
  1070. buff[0]=(char) length; // Length is stored first
  1071. return packet->append(buff, length+1, PACKET_BUFFER_EXTRA_ALLOC);
  1072. }