MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_alter.h
1 /* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #ifndef SQL_ALTER_TABLE_H
17 #define SQL_ALTER_TABLE_H
18 
19 class Alter_drop;
20 class Alter_column;
21 class Key;
22 
23 
30 {
31 public:
32  /*
33  These flags are set by the parser and describes the type of
34  operation(s) specified by the ALTER TABLE statement.
35 
36  They do *not* describe the type operation(s) to be executed
37  by the storage engine. For example, we don't yet know the
38  type of index to be added/dropped.
39  */
40 
41  // Set for ADD [COLUMN]
42  static const uint ALTER_ADD_COLUMN = 1L << 0;
43 
44  // Set for DROP [COLUMN]
45  static const uint ALTER_DROP_COLUMN = 1L << 1;
46 
47  // Set for CHANGE [COLUMN] | MODIFY [CHANGE]
48  // Set by mysql_recreate_table()
49  static const uint ALTER_CHANGE_COLUMN = 1L << 2;
50 
51  // Set for ADD INDEX | ADD KEY | ADD PRIMARY KEY | ADD UNIQUE KEY |
52  // ADD UNIQUE INDEX | ALTER ADD [COLUMN]
53  static const uint ALTER_ADD_INDEX = 1L << 3;
54 
55  // Set for DROP PRIMARY KEY | DROP FOREIGN KEY | DROP KEY | DROP INDEX
56  static const uint ALTER_DROP_INDEX = 1L << 4;
57 
58  // Set for RENAME [TO]
59  static const uint ALTER_RENAME = 1L << 5;
60 
61  // Set for ORDER BY
62  static const uint ALTER_ORDER = 1L << 6;
63 
64  // Set for table_options
65  static const uint ALTER_OPTIONS = 1L << 7;
66 
67  // Set for ALTER [COLUMN] ... SET DEFAULT ... | DROP DEFAULT
68  static const uint ALTER_CHANGE_COLUMN_DEFAULT = 1L << 8;
69 
70  // Set for DISABLE KEYS | ENABLE KEYS
71  static const uint ALTER_KEYS_ONOFF = 1L << 9;
72 
73  // Set for CONVERT TO CHARACTER SET
74  static const uint ALTER_CONVERT = 1L << 10;
75 
76  // Set for FORCE
77  // Set by mysql_recreate_table()
78  static const uint ALTER_RECREATE = 1L << 11;
79 
80  // Set for ADD PARTITION
81  static const uint ALTER_ADD_PARTITION = 1L << 12;
82 
83  // Set for DROP PARTITION
84  static const uint ALTER_DROP_PARTITION = 1L << 13;
85 
86  // Set for COALESCE PARTITION
87  static const uint ALTER_COALESCE_PARTITION = 1L << 14;
88 
89  // Set for REORGANIZE PARTITION ... INTO
90  static const uint ALTER_REORGANIZE_PARTITION = 1L << 15;
91 
92  // Set for partition_options
93  static const uint ALTER_PARTITION = 1L << 16;
94 
95  // Set for LOAD INDEX INTO CACHE ... PARTITION
96  // Set for CACHE INDEX ... PARTITION
97  static const uint ALTER_ADMIN_PARTITION = 1L << 17;
98 
99  // Set for REORGANIZE PARTITION
100  static const uint ALTER_TABLE_REORG = 1L << 18;
101 
102  // Set for REBUILD PARTITION
103  static const uint ALTER_REBUILD_PARTITION = 1L << 19;
104 
105  // Set for partitioning operations specifying ALL keyword
106  static const uint ALTER_ALL_PARTITION = 1L << 20;
107 
108  // Set for REMOVE PARTITIONING
109  static const uint ALTER_REMOVE_PARTITIONING = 1L << 21;
110 
111  // Set for ADD FOREIGN KEY
112  static const uint ADD_FOREIGN_KEY = 1L << 22;
113 
114  // Set for DROP FOREIGN KEY
115  static const uint DROP_FOREIGN_KEY = 1L << 23;
116 
117  // Set for EXCHANGE PARITION
118  static const uint ALTER_EXCHANGE_PARTITION = 1L << 24;
119 
120  // Set by Sql_cmd_alter_table_truncate_partition::execute()
121  static const uint ALTER_TRUNCATE_PARTITION = 1L << 25;
122 
123  // Set for ADD [COLUMN] FIRST | AFTER
124  static const uint ALTER_COLUMN_ORDER = 1L << 26;
125 
126 
127  enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
128 
134  {
135  // In-place if supported, copy otherwise.
136  ALTER_TABLE_ALGORITHM_DEFAULT,
137 
138  // In-place if supported, error otherwise.
139  ALTER_TABLE_ALGORITHM_INPLACE,
140 
141  // Copy if supported, error otherwise.
142  ALTER_TABLE_ALGORITHM_COPY
143  };
144 
145 
151  {
152  // Maximum supported level of concurency for the given operation.
153  ALTER_TABLE_LOCK_DEFAULT,
154 
155  // Allow concurrent reads & writes. If not supported, give erorr.
156  ALTER_TABLE_LOCK_NONE,
157 
158  // Allow concurrent reads only. If not supported, give error.
159  ALTER_TABLE_LOCK_SHARED,
160 
161  // Block reads and writes.
162  ALTER_TABLE_LOCK_EXCLUSIVE
163  };
164 
165 
166  // Columns and keys to be dropped.
167  List<Alter_drop> drop_list;
168  // Columns for ALTER_COLUMN_CHANGE_DEFAULT.
169  List<Alter_column> alter_list;
170  // List of keys, used by both CREATE and ALTER TABLE.
171  List<Key> key_list;
172  // List of columns, used by both CREATE and ALTER TABLE.
173  List<Create_field> create_list;
174  // Type of ALTER TABLE operation.
175  uint flags;
176  // Enable or disable keys.
177  enum_enable_or_disable keys_onoff;
178  // List of partitions.
179  List<char> partition_names;
180  // Number of partitions.
181  uint num_parts;
182  // Type of ALTER TABLE algorithm.
183  enum_alter_table_algorithm requested_algorithm;
184  // Type of ALTER TABLE lock.
185  enum_alter_table_lock requested_lock;
186 
187 
188  Alter_info() :
189  flags(0),
190  keys_onoff(LEAVE_AS_IS),
191  num_parts(0),
192  requested_algorithm(ALTER_TABLE_ALGORITHM_DEFAULT),
193  requested_lock(ALTER_TABLE_LOCK_DEFAULT)
194  {}
195 
196  void reset()
197  {
198  drop_list.empty();
199  alter_list.empty();
200  key_list.empty();
201  create_list.empty();
202  flags= 0;
203  keys_onoff= LEAVE_AS_IS;
204  num_parts= 0;
205  partition_names.empty();
206  requested_algorithm= ALTER_TABLE_ALGORITHM_DEFAULT;
207  requested_lock= ALTER_TABLE_LOCK_DEFAULT;
208  }
209 
210 
226  Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
227 
228 
238  bool set_requested_algorithm(const LEX_STRING *str);
239 
240 
251  bool set_requested_lock(const LEX_STRING *str);
252 
253 private:
254  Alter_info &operator=(const Alter_info &rhs); // not implemented
255  Alter_info(const Alter_info &rhs); // not implemented
256 };
257 
258 
261 {
262 public:
263  Alter_table_ctx();
264 
265  Alter_table_ctx(THD *thd, TABLE_LIST *table_list, uint tables_opened_arg,
266  char *new_db_arg, char *new_name_arg);
267 
271  bool is_database_changed() const
272  { return (new_db != db); };
273 
277  bool is_table_renamed() const
278  { return (is_database_changed() || new_name != table_name); };
279 
283  const char *get_new_filename() const
284  {
285  DBUG_ASSERT(!tmp_table);
286  return new_filename;
287  }
288 
292  const char *get_path() const
293  {
294  DBUG_ASSERT(!tmp_table);
295  return path;
296  }
297 
301  const char *get_new_path() const
302  {
303  DBUG_ASSERT(!tmp_table);
304  return new_path;
305  }
306 
310  const char *get_tmp_path() const
311  { return tmp_path; }
312 
318  {
320  fk_error_id= fk->foreign_id->str;
321  fk_error_table= fk->foreign_table->str;
322  }
323 
324 public:
325  Create_field *datetime_field;
326  bool error_if_not_empty;
327  uint tables_opened;
328  char *db;
329  char *table_name;
330  char *alias;
331  char *new_db;
332  char *new_name;
333  char *new_alias;
334  char tmp_name[80];
342  const char *fk_error_id;
344  const char *fk_error_table;
345 
346 private:
347  char new_filename[FN_REFLEN + 1];
348  char new_alias_buff[FN_REFLEN + 1];
349  char path[FN_REFLEN + 1];
350  char new_path[FN_REFLEN + 1];
351  char tmp_path[FN_REFLEN + 1];
352 
353 #ifndef DBUG_OFF
354 
355  bool tmp_table;
356 #endif
357 
358  Alter_table_ctx &operator=(const Alter_table_ctx &rhs); // not implemented
359  Alter_table_ctx(const Alter_table_ctx &rhs); // not implemented
360 };
361 
362 
369 {
370 protected:
375  {}
376 
377  virtual ~Sql_cmd_common_alter_table()
378  {}
379 
380  virtual enum_sql_command sql_command_code() const
381  {
382  return SQLCOM_ALTER_TABLE;
383  }
384 };
385 
391 {
392 public:
397  {}
398 
400  {}
401 
402  bool execute(THD *thd);
403 };
404 
405 
411 {
412 public:
413  enum enum_tablespace_op_type
414  {
415  DISCARD_TABLESPACE, IMPORT_TABLESPACE
416  };
417 
418  Sql_cmd_discard_import_tablespace(enum_tablespace_op_type tablespace_op_arg)
419  : m_tablespace_op(tablespace_op_arg)
420  {}
421 
422  bool execute(THD *thd);
423 
424 private:
425  const enum_tablespace_op_type m_tablespace_op;
426 };
427 
428 #endif