|
| 1 | +/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or |
| 4 | + modify it under the terms of the GNU General Public License as |
| 5 | + published by the Free Software Foundation; version 2 of the |
| 6 | + License. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, but |
| 9 | + WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | + General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU General Public License |
| 14 | + along with this program; if not, write to the Free Software |
| 15 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 16 | + 02110-1301 USA */ |
| 17 | + |
| 18 | +#include "log.h" |
| 19 | +#include "rpl_table_access.h" |
| 20 | + |
| 21 | +bool System_table_access::open_table(THD* thd, const LEX_STRING dbstr, |
| 22 | + const LEX_STRING tbstr, |
| 23 | + uint max_num_field, |
| 24 | + enum thr_lock_type lock_type, |
| 25 | + TABLE** table, |
| 26 | + Open_tables_backup* backup) |
| 27 | +{ |
| 28 | + TABLE_LIST tables; |
| 29 | + Query_tables_list query_tables_list_backup; |
| 30 | + |
| 31 | + DBUG_ENTER("System_table_access::open_table"); |
| 32 | + before_open(thd); |
| 33 | + |
| 34 | + /* |
| 35 | + We need to use new Open_tables_state in order not to be affected |
| 36 | + by LOCK TABLES/prelocked mode. |
| 37 | + Also in order not to break execution of current statement we also |
| 38 | + have to backup/reset/restore Query_tables_list part of LEX, which |
| 39 | + is accessed and updated in the process of opening and locking |
| 40 | + tables. |
| 41 | + */ |
| 42 | + thd->lex->reset_n_backup_query_tables_list(&query_tables_list_backup); |
| 43 | + thd->reset_n_backup_open_tables_state(backup); |
| 44 | + |
| 45 | + tables.init_one_table(dbstr.str, dbstr.length, tbstr.str, tbstr.length, |
| 46 | + tbstr.str, lock_type); |
| 47 | + |
| 48 | + tables.open_strategy= TABLE_LIST::OPEN_IF_EXISTS; |
| 49 | + |
| 50 | + if (!open_n_lock_single_table(thd, &tables, tables.lock_type, m_flags)) |
| 51 | + { |
| 52 | + close_thread_tables(thd); |
| 53 | + thd->restore_backup_open_tables_state(backup); |
| 54 | + thd->lex->restore_backup_query_tables_list(&query_tables_list_backup); |
| 55 | + if (thd->is_operating_gtid_table) |
| 56 | + sql_print_warning("Gtid table is not ready to be used. Table '%s.%s' " |
| 57 | + "cannot be opened.", dbstr.str, tbstr.str); |
| 58 | + else |
| 59 | + my_error(ER_NO_SUCH_TABLE, MYF(0), dbstr.str, tbstr.str); |
| 60 | + DBUG_RETURN(true); |
| 61 | + } |
| 62 | + |
| 63 | + if (tables.table->s->fields < max_num_field) |
| 64 | + { |
| 65 | + /* |
| 66 | + Safety: this can only happen if someone started the server and then |
| 67 | + altered the table. |
| 68 | + */ |
| 69 | + ha_rollback_trans(thd, false); |
| 70 | + close_thread_tables(thd); |
| 71 | + thd->restore_backup_open_tables_state(backup); |
| 72 | + thd->lex->restore_backup_query_tables_list(&query_tables_list_backup); |
| 73 | + my_error(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2, MYF(0), |
| 74 | + tables.table->s->db.str, tables.table->s->table_name.str, |
| 75 | + max_num_field, tables.table->s->fields); |
| 76 | + DBUG_RETURN(true); |
| 77 | + } |
| 78 | + |
| 79 | + thd->lex->restore_backup_query_tables_list(&query_tables_list_backup); |
| 80 | + |
| 81 | + *table= tables.table; |
| 82 | + tables.table->use_all_columns(); |
| 83 | + DBUG_RETURN(false); |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +void System_table_access::close_table(THD *thd, TABLE* table, |
| 88 | + Open_tables_backup *backup, |
| 89 | + bool error, bool need_commit) |
| 90 | +{ |
| 91 | + Query_tables_list query_tables_list_backup; |
| 92 | + |
| 93 | + DBUG_ENTER("System_table_access::close_table"); |
| 94 | + |
| 95 | + if (table) |
| 96 | + { |
| 97 | + if (error) |
| 98 | + ha_rollback_trans(thd, false); |
| 99 | + else |
| 100 | + { |
| 101 | + /* |
| 102 | + To make the commit not to block with global read lock set |
| 103 | + "ignore_global_read_lock" flag to true. |
| 104 | + */ |
| 105 | + ha_commit_trans(thd, false, true); |
| 106 | + } |
| 107 | + if (need_commit) |
| 108 | + { |
| 109 | + if (error) |
| 110 | + ha_rollback_trans(thd, true); |
| 111 | + else |
| 112 | + { |
| 113 | + /* |
| 114 | + To make the commit not to block with global read lock set |
| 115 | + "ignore_global_read_lock" flag to true. |
| 116 | + */ |
| 117 | + ha_commit_trans(thd, true, true); |
| 118 | + } |
| 119 | + } |
| 120 | + /* |
| 121 | + In order not to break execution of current statement we have to |
| 122 | + backup/reset/restore Query_tables_list part of LEX, which is |
| 123 | + accessed and updated in the process of closing tables. |
| 124 | + */ |
| 125 | + thd->lex->reset_n_backup_query_tables_list(&query_tables_list_backup); |
| 126 | + close_thread_tables(thd); |
| 127 | + thd->lex->restore_backup_query_tables_list(&query_tables_list_backup); |
| 128 | + thd->restore_backup_open_tables_state(backup); |
| 129 | + } |
| 130 | + |
| 131 | + DBUG_VOID_RETURN; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +THD *System_table_access::create_thd() |
| 136 | +{ |
| 137 | + THD *thd= NULL; |
| 138 | + thd= new THD; |
| 139 | + thd->thread_stack= (char*) &thd; |
| 140 | + thd->store_globals(); |
| 141 | + thd->security_ctx->skip_grants(); |
| 142 | + |
| 143 | + return(thd); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +void System_table_access::drop_thd(THD *thd) |
| 148 | +{ |
| 149 | + DBUG_ENTER("System_table_access::drop_thd"); |
| 150 | + |
| 151 | + delete thd; |
| 152 | + my_pthread_setspecific_ptr(THR_THD, NULL); |
| 153 | + |
| 154 | + DBUG_VOID_RETURN; |
| 155 | +} |
| 156 | + |
0 commit comments