1 /* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights
2    reserved.
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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
23 
24 #ifndef SQL_TABLE_INCLUDED
25 #define SQL_TABLE_INCLUDED
26 
27 #include "my_global.h"                          /* my_bool */
28 #include "my_pthread.h"
29 #include "m_ctype.h"                            /* CHARSET_INFO */
30 #include "mysql_com.h"                          /* enum_field_types */
31 
32 class Alter_info;
33 class Alter_table_ctx;
34 class Create_field;
35 struct TABLE_LIST;
36 class THD;
37 struct TABLE;
38 struct handlerton;
39 typedef struct st_ha_check_opt HA_CHECK_OPT;
40 typedef struct st_ha_create_information HA_CREATE_INFO;
41 typedef struct st_key KEY;
42 typedef struct st_key_cache KEY_CACHE;
43 typedef struct st_lock_param_type ALTER_PARTITION_PARAM_TYPE;
44 typedef struct st_mysql_lex_string LEX_STRING;
45 typedef struct st_order ORDER;
46 
47 enum ddl_log_entry_code
48 {
49   /*
50     DDL_LOG_EXECUTE_CODE:
51       This is a code that indicates that this is a log entry to
52       be executed, from this entry a linked list of log entries
53       can be found and executed.
54     DDL_LOG_ENTRY_CODE:
55       An entry to be executed in a linked list from an execute log
56       entry.
57     DDL_IGNORE_LOG_ENTRY_CODE:
58       An entry that is to be ignored
59   */
60   DDL_LOG_EXECUTE_CODE = 'e',
61   DDL_LOG_ENTRY_CODE = 'l',
62   DDL_IGNORE_LOG_ENTRY_CODE = 'i'
63 };
64 
65 enum ddl_log_action_code
66 {
67   /*
68     The type of action that a DDL_LOG_ENTRY_CODE entry is to
69     perform.
70     DDL_LOG_DELETE_ACTION:
71       Delete an entity
72     DDL_LOG_RENAME_ACTION:
73       Rename an entity
74     DDL_LOG_REPLACE_ACTION:
75       Rename an entity after removing the previous entry with the
76       new name, that is replace this entry.
77     DDL_LOG_EXCHANGE_ACTION:
78       Exchange two entities by renaming them a -> tmp, b -> a, tmp -> b.
79   */
80   DDL_LOG_DELETE_ACTION = 'd',
81   DDL_LOG_RENAME_ACTION = 'r',
82   DDL_LOG_REPLACE_ACTION = 's',
83   DDL_LOG_EXCHANGE_ACTION = 'e'
84 };
85 
86 enum enum_ddl_log_exchange_phase {
87   EXCH_PHASE_NAME_TO_TEMP= 0,
88   EXCH_PHASE_FROM_TO_NAME= 1,
89   EXCH_PHASE_TEMP_TO_FROM= 2
90 };
91 
92 
93 typedef struct st_ddl_log_entry
94 {
95   const char *name;
96   const char *from_name;
97   const char *handler_name;
98   const char *tmp_name;
99   uint next_entry;
100   uint entry_pos;
101   enum ddl_log_entry_code entry_type;
102   enum ddl_log_action_code action_type;
103   /*
104     Most actions have only one phase. REPLACE does however have two
105     phases. The first phase removes the file with the new name if
106     there was one there before and the second phase renames the
107     old name to the new name. EXCHANGE have three phases.
108   */
109   char phase;
110 } DDL_LOG_ENTRY;
111 
112 typedef struct st_ddl_log_memory_entry
113 {
114   uint entry_pos;
115   struct st_ddl_log_memory_entry *next_log_entry;
116   struct st_ddl_log_memory_entry *prev_log_entry;
117   struct st_ddl_log_memory_entry *next_active_log_entry;
118 } DDL_LOG_MEMORY_ENTRY;
119 
120 
121 enum enum_explain_filename_mode
122 {
123   EXPLAIN_ALL_VERBOSE= 0,
124   EXPLAIN_PARTITIONS_VERBOSE,
125   EXPLAIN_PARTITIONS_AS_COMMENT
126 };
127 
128 /* Maximum length of GEOM_POINT Field */
129 #define MAX_LEN_GEOM_POINT_FIELD   25
130 
131 /* depends on errmsg.txt Database `db`, Table `t` ... */
132 #define EXPLAIN_FILENAME_MAX_EXTRA_LENGTH 63
133 
134 #define MYSQL50_TABLE_NAME_PREFIX         "#mysql50#"
135 #define MYSQL50_TABLE_NAME_PREFIX_LENGTH  9
136 
137 #define WFRM_WRITE_SHADOW 1
138 #define WFRM_INSTALL_SHADOW 2
139 #define WFRM_PACK_FRM 4
140 #define WFRM_KEEP_SHARE 8
141 
142 /* Flags for conversion functions. */
143 static const uint FN_FROM_IS_TMP=  1 << 0;
144 static const uint FN_TO_IS_TMP=    1 << 1;
145 static const uint FN_IS_TMP=       FN_FROM_IS_TMP | FN_TO_IS_TMP;
146 static const uint NO_FRM_RENAME=   1 << 2;
147 static const uint FRM_ONLY=        1 << 3;
148 /** Don't remove table in engine. Remove only .FRM and maybe .PAR files. */
149 static const uint NO_HA_TABLE=     1 << 4;
150 /** Don't resolve MySQL's fake "foo.sym" symbolic directory names. */
151 static const uint SKIP_SYMDIR_ACCESS= 1 << 5;
152 /** Don't check foreign key constraints while renaming table */
153 static const uint NO_FK_CHECKS=    1 << 6;
154 
155 uint filename_to_tablename(const char *from, char *to, uint to_length
156 #ifndef DBUG_OFF
157                            , bool stay_quiet = false
158 #endif /* DBUG_OFF */
159                            );
160 uint tablename_to_filename(const char *from, char *to, uint to_length);
161 uint check_n_cut_mysql50_prefix(const char *from, char *to, uint to_length);
162 bool check_mysql50_prefix(const char *name);
163 uint build_table_filename(char *buff, size_t bufflen, const char *db,
164                           const char *table, const char *ext,
165                           uint flags, bool *was_truncated);
166 // For caller's who are mostly sure that path do not truncate
build_table_filename(char * buff,size_t bufflen,const char * db,const char * table,const char * ext,uint flags)167 uint inline build_table_filename(char *buff, size_t bufflen, const char *db,
168                           const char *table, const char *ext, uint flags)
169 {
170     bool truncated_not_used;
171     return build_table_filename(buff, bufflen, db, table, ext, flags,
172                                 &truncated_not_used);
173 }
174 uint build_table_shadow_filename(char *buff, size_t bufflen,
175                                  ALTER_PARTITION_PARAM_TYPE *lpt);
176 uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen);
177 bool mysql_create_table(THD *thd, TABLE_LIST *create_table,
178                         HA_CREATE_INFO *create_info,
179                         Alter_info *alter_info);
180 bool mysql_create_table_no_lock(THD *thd, const char *db,
181                                 const char *table_name,
182                                 HA_CREATE_INFO *create_info,
183                                 Alter_info *alter_info,
184                                 uint select_field_count,
185                                 bool *is_trans);
186 int mysql_discard_or_import_tablespace(THD *thd,
187                                        TABLE_LIST *table_list,
188                                        bool discard);
189 bool mysql_prepare_alter_table(THD *thd, TABLE *table,
190                                HA_CREATE_INFO *create_info,
191                                Alter_info *alter_info,
192                                Alter_table_ctx *alter_ctx);
193 bool mysql_trans_prepare_alter_copy_data(THD *thd);
194 bool mysql_trans_commit_alter_copy_data(THD *thd);
195 bool mysql_alter_table(THD *thd, char *new_db, char *new_name,
196                        HA_CREATE_INFO *create_info,
197                        TABLE_LIST *table_list,
198                        Alter_info *alter_info,
199                        uint order_num, ORDER *order, bool ignore);
200 bool mysql_compare_tables(TABLE *table,
201                           Alter_info *alter_info,
202                           HA_CREATE_INFO *create_info,
203                           bool *metadata_equal);
204 bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list, bool table_copy);
205 bool mysql_create_like_table(THD *thd, TABLE_LIST *table,
206                              TABLE_LIST *src_table,
207                              HA_CREATE_INFO *create_info);
208 bool mysql_rename_table(handlerton *base, const char *old_db,
209                         const char * old_name, const char *new_db,
210                         const char * new_name, uint flags);
211 
212 bool mysql_backup_table(THD* thd, TABLE_LIST* table_list);
213 bool mysql_restore_table(THD* thd, TABLE_LIST* table_list);
214 
215 bool mysql_checksum_table(THD* thd, TABLE_LIST* table_list,
216                           HA_CHECK_OPT* check_opt);
217 bool mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists,
218                     my_bool drop_temporary);
219 int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
220                             bool drop_temporary, bool drop_view,
221                             bool log_query);
222 bool quick_rm_table(THD *thd, handlerton *base, const char *db,
223                     const char *table_name, uint flags);
224 void close_cached_table(THD *thd, TABLE *table);
225 bool fill_field_definition(THD *thd,
226                            class sp_head *sp,
227                            enum enum_field_types field_type,
228                            Create_field *field_def);
229 int prepare_create_field(Create_field *sql_field,
230 			 uint *blob_columns,
231 			 longlong table_flags);
232 const CHARSET_INFO* get_sql_field_charset(Create_field *sql_field,
233                                           HA_CREATE_INFO *create_info);
234 bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags);
235 int write_bin_log(THD *thd, bool clear_error,
236                   char const *query, ulong query_length,
237                   bool is_trans= FALSE);
238 bool write_ddl_log_entry(DDL_LOG_ENTRY *ddl_log_entry,
239                            DDL_LOG_MEMORY_ENTRY **active_entry);
240 bool write_execute_ddl_log_entry(uint first_entry,
241                                    bool complete,
242                                    DDL_LOG_MEMORY_ENTRY **active_entry);
243 bool deactivate_ddl_log_entry(uint entry_no);
244 void release_ddl_log_memory_entry(DDL_LOG_MEMORY_ENTRY *log_entry);
245 bool sync_ddl_log();
246 void release_ddl_log();
247 void execute_ddl_log_recovery();
248 bool execute_ddl_log_entry(THD *thd, uint first_entry);
249 bool validate_comment_length(THD *thd, const char *comment_str,
250                              size_t *comment_len, uint max_len,
251                              uint err_code, const char *comment_name);
252 
253 template<typename T> class List;
254 void promote_first_timestamp_column(List<Create_field> *column_definitions);
255 
256 /*
257   These prototypes where under INNODB_COMPATIBILITY_HOOKS.
258 */
259 uint explain_filename(THD* thd, const char *from, char *to, uint to_length,
260                       enum_explain_filename_mode explain_mode);
261 
262 
263 extern MYSQL_PLUGIN_IMPORT const char *primary_key_name;
264 extern mysql_mutex_t LOCK_gdl;
265 
266 #endif /* SQL_TABLE_INCLUDED */
267