1 /* Copyright (c) 2006, 2014, Oracle and/or its affiliates.
2    Copyright (c) 2011, 2017, MariaDB Corporation.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU 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  02110-1335  USA */
16 
17 #ifndef SQL_TABLE_INCLUDED
18 #define SQL_TABLE_INCLUDED
19 
20 #include <my_sys.h>                             // pthread_mutex_t
21 #include "m_string.h"                           // LEX_CUSTRING
22 
23 class Alter_info;
24 class Alter_table_ctx;
25 class Column_definition;
26 class Create_field;
27 struct TABLE_LIST;
28 class THD;
29 struct TABLE;
30 struct handlerton;
31 class handler;
32 typedef struct st_ha_check_opt HA_CHECK_OPT;
33 struct HA_CREATE_INFO;
34 struct Table_specification_st;
35 typedef struct st_key KEY;
36 typedef struct st_key_cache KEY_CACHE;
37 typedef struct st_lock_param_type ALTER_PARTITION_PARAM_TYPE;
38 typedef struct st_order ORDER;
39 
40 enum ddl_log_entry_code
verify_context(native_handle_type handle)41 {
42   /*
43     DDL_LOG_EXECUTE_CODE:
44       This is a code that indicates that this is a log entry to
45       be executed, from this entry a linked list of log entries
46       can be found and executed.
47     DDL_LOG_ENTRY_CODE:
48       An entry to be executed in a linked list from an execute log
49       entry.
50     DDL_IGNORE_LOG_ENTRY_CODE:
51       An entry that is to be ignored
52   */
53   DDL_LOG_EXECUTE_CODE = 'e',
54   DDL_LOG_ENTRY_CODE = 'l',
55   DDL_IGNORE_LOG_ENTRY_CODE = 'i'
56 };
57 
58 enum ddl_log_action_code
59 {
60   /*
61     The type of action that a DDL_LOG_ENTRY_CODE entry is to
62     perform.
63     DDL_LOG_DELETE_ACTION:
64       Delete an entity
65     DDL_LOG_RENAME_ACTION:
66       Rename an entity
67     DDL_LOG_REPLACE_ACTION:
68       Rename an entity after removing the previous entry with the
69       new name, that is replace this entry.
70     DDL_LOG_EXCHANGE_ACTION:
71       Exchange two entities by renaming them a -> tmp, b -> a, tmp -> b.
72   */
73   DDL_LOG_DELETE_ACTION = 'd',
74   DDL_LOG_RENAME_ACTION = 'r',
75   DDL_LOG_REPLACE_ACTION = 's',
76   DDL_LOG_EXCHANGE_ACTION = 'e'
77 };
78 
79 enum enum_ddl_log_exchange_phase {
80   EXCH_PHASE_NAME_TO_TEMP= 0,
81   EXCH_PHASE_FROM_TO_NAME= 1,
82   EXCH_PHASE_TEMP_TO_FROM= 2
83 };
84 
85 
86 typedef struct st_ddl_log_entry
87 {
88   const char *name;
89   const char *from_name;
90   const char *handler_name;
91   const char *tmp_name;
92   uint next_entry;
93   uint entry_pos;
94   enum ddl_log_entry_code entry_type;
95   enum ddl_log_action_code action_type;
96   /*
97     Most actions have only one phase. REPLACE does however have two
98     phases. The first phase removes the file with the new name if
99     there was one there before and the second phase renames the
100     old name to the new name.
101   */
102   char phase;
103 } DDL_LOG_ENTRY;
104 
105 typedef struct st_ddl_log_memory_entry
106 {
107   uint entry_pos;
108   struct st_ddl_log_memory_entry *next_log_entry;
109   struct st_ddl_log_memory_entry *prev_log_entry;
110   struct st_ddl_log_memory_entry *next_active_log_entry;
111 } DDL_LOG_MEMORY_ENTRY;
112 
113 
114 enum enum_explain_filename_mode
115 {
116   EXPLAIN_ALL_VERBOSE= 0,
117   EXPLAIN_PARTITIONS_VERBOSE,
118   EXPLAIN_PARTITIONS_AS_COMMENT
119 };
120 
121 /* Maximum length of GEOM_POINT Field */
122 #define MAX_LEN_GEOM_POINT_FIELD   25
123 
124 /* depends on errmsg.txt Database `db`, Table `t` ... */
125 #define EXPLAIN_FILENAME_MAX_EXTRA_LENGTH 63
126 
127 #define WFRM_WRITE_SHADOW 1
128 #define WFRM_INSTALL_SHADOW 2
129 #define WFRM_KEEP_SHARE 4
130 
131 /* Flags for conversion functions. */
132 static const uint FN_FROM_IS_TMP=  1 << 0;
133 static const uint FN_TO_IS_TMP=    1 << 1;
134 static const uint FN_IS_TMP=       FN_FROM_IS_TMP | FN_TO_IS_TMP;
135 static const uint NO_FRM_RENAME=   1 << 2;
136 static const uint FRM_ONLY=        1 << 3;
137 /** Don't remove table in engine. Remove only .FRM and maybe .PAR files. */
138 static const uint NO_HA_TABLE=     1 << 4;
139 /** Don't resolve MySQL's fake "foo.sym" symbolic directory names. */
140 static const uint SKIP_SYMDIR_ACCESS= 1 << 5;
141 /** Don't check foreign key constraints while renaming table */
142 static const uint NO_FK_CHECKS=    1 << 6;
143 
144 uint filename_to_tablename(const char *from, char *to, size_t to_length,
145                            bool stay_quiet = false);
146 uint tablename_to_filename(const char *from, char *to, size_t to_length);
147 uint check_n_cut_mysql50_prefix(const char *from, char *to, size_t to_length);
148 bool check_mysql50_prefix(const char *name);
149 uint build_table_filename(char *buff, size_t bufflen, const char *db,
150                           const char *table, const char *ext, uint flags);
151 uint build_table_shadow_filename(char *buff, size_t bufflen,
152                                  ALTER_PARTITION_PARAM_TYPE *lpt);
153 uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen);
154 bool mysql_create_table(THD *thd, TABLE_LIST *create_table,
155                         Table_specification_st *create_info,
156                         Alter_info *alter_info);
157 
158 /*
159   mysql_create_table_no_lock can be called in one of the following
160   mutually exclusive situations:
161 
162   - Just a normal ordinary CREATE TABLE statement that explicitly
163     defines the table structure.
164 
165   - CREATE TABLE ... SELECT. It is special, because only in this case,
166     the list of fields is allowed to have duplicates, as long as one of the
167     duplicates comes from the select list, and the other doesn't. For
168     example in
169 
170        CREATE TABLE t1 (a int(5) NOT NUL) SELECT b+10 as a FROM t2;
171 
172     the list in alter_info->create_list will have two fields `a`.
173 
174   - ALTER TABLE, that creates a temporary table #sql-xxx, which will be later
175     renamed to replace the original table.
176 
177   - ALTER TABLE as above, but which only modifies the frm file, it only
178     creates an frm file for the #sql-xxx, the table in the engine is not
179     created.
180 
181   - Assisted discovery, CREATE TABLE statement without the table structure.
182 
183   These situations are distinguished by the following "create table mode"
184   values, where a CREATE ... SELECT is denoted by any non-negative number
185   (which should be the number of fields in the SELECT ... part), and other
186   cases use constants as defined below.
187 */
188 #define C_CREATE_SELECT(X)        ((X) > 0 ? (X) : 0)
189 #define C_ORDINARY_CREATE         0
190 #define C_ALTER_TABLE            -1
191 #define C_ALTER_TABLE_FRM_ONLY   -2
192 #define C_ASSISTED_DISCOVERY     -3
193 
194 int mysql_create_table_no_lock(THD *thd, const LEX_CSTRING *db,
195                                const LEX_CSTRING *table_name,
196                                Table_specification_st *create_info,
197                                Alter_info *alter_info, bool *is_trans,
198                                int create_table_mode, TABLE_LIST *table);
199 
200 handler *mysql_create_frm_image(THD *thd,
201                                 const LEX_CSTRING *db,
202                                 const LEX_CSTRING *table_name,
203                                 HA_CREATE_INFO *create_info,
204                                 Alter_info *alter_info,
205                                 int create_table_mode,
206                                 KEY **key_info,
207                                 uint *key_count,
208                                 LEX_CUSTRING *frm);
209 
210 int mysql_discard_or_import_tablespace(THD *thd,
211                                        TABLE_LIST *table_list,
212                                        bool discard);
213 
214 bool mysql_prepare_alter_table(THD *thd, TABLE *table,
215                                HA_CREATE_INFO *create_info,
216                                Alter_info *alter_info,
217                                Alter_table_ctx *alter_ctx);
218 bool mysql_trans_prepare_alter_copy_data(THD *thd);
219 bool mysql_trans_commit_alter_copy_data(THD *thd);
220 bool mysql_alter_table(THD *thd, const LEX_CSTRING *new_db, const LEX_CSTRING *new_name,
221                        HA_CREATE_INFO *create_info,
222                        TABLE_LIST *table_list,
223                        Alter_info *alter_info,
224                        uint order_num, ORDER *order, bool ignore);
225 bool mysql_compare_tables(TABLE *table,
226                           Alter_info *alter_info,
227                           HA_CREATE_INFO *create_info,
228                           bool *metadata_equal);
229 bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list, bool table_copy);
230 bool mysql_create_like_table(THD *thd, TABLE_LIST *table,
231                              TABLE_LIST *src_table,
232                              Table_specification_st *create_info);
233 bool mysql_rename_table(handlerton *base, const LEX_CSTRING *old_db,
234                         const LEX_CSTRING *old_name, const LEX_CSTRING *new_db,
235                         const LEX_CSTRING *new_name, uint flags);
236 
237 bool mysql_backup_table(THD* thd, TABLE_LIST* table_list);
238 bool mysql_restore_table(THD* thd, TABLE_LIST* table_list);
239 
240 bool mysql_checksum_table(THD* thd, TABLE_LIST* table_list,
241                           HA_CHECK_OPT* check_opt);
242 bool mysql_rm_table(THD *thd,TABLE_LIST *tables, bool if_exists,
243                     bool drop_temporary, bool drop_sequence);
244 int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
245                             bool drop_temporary, bool drop_view,
246                             bool drop_sequence,
247                             bool log_query, bool dont_free_locks);
248 bool log_drop_table(THD *thd, const LEX_CSTRING *db_name,
249                     const LEX_CSTRING *table_name, bool temporary_table);
250 bool quick_rm_table(THD *thd, handlerton *base, const LEX_CSTRING *db,
251                     const LEX_CSTRING *table_name, uint flags,
252                     const char *table_path=0);
253 void close_cached_table(THD *thd, TABLE *table);
254 void sp_prepare_create_field(THD *thd, Column_definition *sql_field);
255 CHARSET_INFO* get_sql_field_charset(Column_definition *sql_field,
256                                     HA_CREATE_INFO *create_info);
257 bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags);
258 int write_bin_log(THD *thd, bool clear_error,
259                   char const *query, ulong query_length,
260                   bool is_trans= FALSE);
261 bool write_ddl_log_entry(DDL_LOG_ENTRY *ddl_log_entry,
262                            DDL_LOG_MEMORY_ENTRY **active_entry);
263 bool write_execute_ddl_log_entry(uint first_entry,
264                                    bool complete,
265                                    DDL_LOG_MEMORY_ENTRY **active_entry);
266 bool deactivate_ddl_log_entry(uint entry_no);
267 void release_ddl_log_memory_entry(DDL_LOG_MEMORY_ENTRY *log_entry);
268 bool sync_ddl_log();
269 void release_ddl_log();
270 void execute_ddl_log_recovery();
271 bool execute_ddl_log_entry(THD *thd, uint first_entry);
272 
273 template<typename T> class List;
274 void promote_first_timestamp_column(List<Create_field> *column_definitions);
275 
276 /*
277   These prototypes where under INNODB_COMPATIBILITY_HOOKS.
278 */
279 uint explain_filename(THD* thd, const char *from, char *to, uint to_length,
280                       enum_explain_filename_mode explain_mode);
281 
282 
283 extern MYSQL_PLUGIN_IMPORT const char *primary_key_name;
284 extern mysql_mutex_t LOCK_gdl;
285 
286 bool check_engine(THD *, const char *, const char *, HA_CREATE_INFO *);
287 
288 #endif /* SQL_TABLE_INCLUDED */
289