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