My Project
sql_alter.h
00001 /* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights
00002    reserved.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; version 2 of the License.
00007 
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; if not, write to the Free Software
00015    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00016 
00017 #ifndef SQL_ALTER_TABLE_H
00018 #define SQL_ALTER_TABLE_H
00019 
00020 class Alter_drop;
00021 class Alter_column;
00022 class Key;
00023 
00024 
00030 class Alter_info
00031 {
00032 public:
00033   /*
00034     These flags are set by the parser and describes the type of
00035     operation(s) specified by the ALTER TABLE statement.
00036 
00037     They do *not* describe the type operation(s) to be executed
00038     by the storage engine. For example, we don't yet know the
00039     type of index to be added/dropped.
00040   */
00041 
00042   // Set for ADD [COLUMN]
00043   static const uint ALTER_ADD_COLUMN            = 1L <<  0;
00044 
00045   // Set for DROP [COLUMN]
00046   static const uint ALTER_DROP_COLUMN           = 1L <<  1;
00047 
00048   // Set for CHANGE [COLUMN] | MODIFY [CHANGE]
00049   // Set by mysql_recreate_table()
00050   static const uint ALTER_CHANGE_COLUMN         = 1L <<  2;
00051 
00052   // Set for ADD INDEX | ADD KEY | ADD PRIMARY KEY | ADD UNIQUE KEY |
00053   //         ADD UNIQUE INDEX | ALTER ADD [COLUMN]
00054   static const uint ALTER_ADD_INDEX             = 1L <<  3;
00055 
00056   // Set for DROP PRIMARY KEY | DROP FOREIGN KEY | DROP KEY | DROP INDEX
00057   static const uint ALTER_DROP_INDEX            = 1L <<  4;
00058 
00059   // Set for RENAME [TO]
00060   static const uint ALTER_RENAME                = 1L <<  5;
00061 
00062   // Set for ORDER BY
00063   static const uint ALTER_ORDER                 = 1L <<  6;
00064 
00065   // Set for table_options
00066   static const uint ALTER_OPTIONS               = 1L <<  7;
00067 
00068   // Set for ALTER [COLUMN] ... SET DEFAULT ... | DROP DEFAULT
00069   static const uint ALTER_CHANGE_COLUMN_DEFAULT = 1L <<  8;
00070 
00071   // Set for DISABLE KEYS | ENABLE KEYS
00072   static const uint ALTER_KEYS_ONOFF            = 1L <<  9;
00073 
00074   // Set for CONVERT TO CHARACTER SET
00075   static const uint ALTER_CONVERT               = 1L << 10;
00076 
00077   // Set for FORCE
00078   // Set for ENGINE(same engine)
00079   // Set by mysql_recreate_table()
00080   static const uint ALTER_RECREATE              = 1L << 11;
00081 
00082   // Set for ADD PARTITION
00083   static const uint ALTER_ADD_PARTITION         = 1L << 12;
00084 
00085   // Set for DROP PARTITION
00086   static const uint ALTER_DROP_PARTITION        = 1L << 13;
00087 
00088   // Set for COALESCE PARTITION
00089   static const uint ALTER_COALESCE_PARTITION    = 1L << 14;
00090 
00091   // Set for REORGANIZE PARTITION ... INTO
00092   static const uint ALTER_REORGANIZE_PARTITION  = 1L << 15;
00093 
00094   // Set for partition_options
00095   static const uint ALTER_PARTITION             = 1L << 16;
00096 
00097   // Set for LOAD INDEX INTO CACHE ... PARTITION
00098   // Set for CACHE INDEX ... PARTITION
00099   static const uint ALTER_ADMIN_PARTITION       = 1L << 17;
00100 
00101   // Set for REORGANIZE PARTITION
00102   static const uint ALTER_TABLE_REORG           = 1L << 18;
00103 
00104   // Set for REBUILD PARTITION
00105   static const uint ALTER_REBUILD_PARTITION     = 1L << 19;
00106 
00107   // Set for partitioning operations specifying ALL keyword
00108   static const uint ALTER_ALL_PARTITION         = 1L << 20;
00109 
00110   // Set for REMOVE PARTITIONING
00111   static const uint ALTER_REMOVE_PARTITIONING   = 1L << 21;
00112 
00113   // Set for ADD FOREIGN KEY
00114   static const uint ADD_FOREIGN_KEY             = 1L << 22;
00115 
00116   // Set for DROP FOREIGN KEY
00117   static const uint DROP_FOREIGN_KEY            = 1L << 23;
00118 
00119   // Set for EXCHANGE PARITION
00120   static const uint ALTER_EXCHANGE_PARTITION    = 1L << 24;
00121 
00122   // Set by Sql_cmd_alter_table_truncate_partition::execute()
00123   static const uint ALTER_TRUNCATE_PARTITION    = 1L << 25;
00124 
00125   // Set for ADD [COLUMN] FIRST | AFTER
00126   static const uint ALTER_COLUMN_ORDER          = 1L << 26;
00127 
00128 
00129   enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
00130 
00135   enum enum_alter_table_algorithm
00136   {
00137     // In-place if supported, copy otherwise.
00138     ALTER_TABLE_ALGORITHM_DEFAULT,
00139 
00140     // In-place if supported, error otherwise.
00141     ALTER_TABLE_ALGORITHM_INPLACE,
00142 
00143     // Copy if supported, error otherwise.
00144     ALTER_TABLE_ALGORITHM_COPY
00145   };
00146 
00147 
00152   enum enum_alter_table_lock
00153   {
00154     // Maximum supported level of concurency for the given operation.
00155     ALTER_TABLE_LOCK_DEFAULT,
00156 
00157     // Allow concurrent reads & writes. If not supported, give erorr.
00158     ALTER_TABLE_LOCK_NONE,
00159 
00160     // Allow concurrent reads only. If not supported, give error.
00161     ALTER_TABLE_LOCK_SHARED,
00162 
00163     // Block reads and writes.
00164     ALTER_TABLE_LOCK_EXCLUSIVE
00165   };
00166 
00167 
00168   // Columns and keys to be dropped.
00169   List<Alter_drop>              drop_list;
00170   // Columns for ALTER_COLUMN_CHANGE_DEFAULT.
00171   List<Alter_column>            alter_list;
00172   // List of keys, used by both CREATE and ALTER TABLE.
00173   List<Key>                     key_list;
00174   // List of columns, used by both CREATE and ALTER TABLE.
00175   List<Create_field>            create_list;
00176   // Type of ALTER TABLE operation.
00177   uint                          flags;
00178   // Enable or disable keys.
00179   enum_enable_or_disable        keys_onoff;
00180   // List of partitions.
00181   List<char>                    partition_names;
00182   // Number of partitions.
00183   uint                          num_parts;
00184   // Type of ALTER TABLE algorithm.
00185   enum_alter_table_algorithm    requested_algorithm;
00186   // Type of ALTER TABLE lock.
00187   enum_alter_table_lock         requested_lock;
00188 
00189 
00190   Alter_info() :
00191     flags(0),
00192     keys_onoff(LEAVE_AS_IS),
00193     num_parts(0),
00194     requested_algorithm(ALTER_TABLE_ALGORITHM_DEFAULT),
00195     requested_lock(ALTER_TABLE_LOCK_DEFAULT)
00196   {}
00197 
00198   void reset()
00199   {
00200     drop_list.empty();
00201     alter_list.empty();
00202     key_list.empty();
00203     create_list.empty();
00204     flags= 0;
00205     keys_onoff= LEAVE_AS_IS;
00206     num_parts= 0;
00207     partition_names.empty();
00208     requested_algorithm= ALTER_TABLE_ALGORITHM_DEFAULT;
00209     requested_lock= ALTER_TABLE_LOCK_DEFAULT;
00210   }
00211 
00212 
00228   Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
00229 
00230 
00240   bool set_requested_algorithm(const LEX_STRING *str);
00241 
00242 
00253   bool set_requested_lock(const LEX_STRING *str);
00254 
00255 private:
00256   Alter_info &operator=(const Alter_info &rhs); // not implemented
00257   Alter_info(const Alter_info &rhs);            // not implemented
00258 };
00259 
00260 
00262 class Alter_table_ctx
00263 {
00264 public:
00265   Alter_table_ctx();
00266 
00267   Alter_table_ctx(THD *thd, TABLE_LIST *table_list, uint tables_opened_arg,
00268                   char *new_db_arg, char *new_name_arg);
00269 
00273   bool is_database_changed() const
00274   { return (new_db != db); };
00275 
00279   bool is_table_renamed() const
00280   { return (is_database_changed() || new_name != table_name); };
00281 
00285   const char *get_new_filename() const
00286   {
00287     DBUG_ASSERT(!tmp_table);
00288     return new_filename;
00289   }
00290 
00294   const char *get_path() const
00295   {
00296     DBUG_ASSERT(!tmp_table);
00297     return path;
00298   }
00299 
00303   const char *get_new_path() const
00304   {
00305     DBUG_ASSERT(!tmp_table);
00306     return new_path;
00307   }
00308 
00312   const char *get_tmp_path() const
00313   { return tmp_path; }
00314 
00319   void set_fk_error_if_delete_row(FOREIGN_KEY_INFO *fk)
00320   {
00321     fk_error_if_delete_row= true;
00322     fk_error_id= fk->foreign_id->str;
00323     fk_error_table= fk->foreign_table->str;
00324   }
00325 
00326 public:
00327   Create_field *datetime_field;
00328   bool         error_if_not_empty;
00329   uint         tables_opened;
00330   char         *db;
00331   char         *table_name;
00332   char         *alias;
00333   char         *new_db;
00334   char         *new_name;
00335   char         *new_alias;
00336   char         tmp_name[80];
00342   bool         fk_error_if_delete_row;
00344   const char   *fk_error_id;
00346   const char   *fk_error_table;
00347 
00348 private:
00349   char new_filename[FN_REFLEN + 1];
00350   char new_alias_buff[FN_REFLEN + 1];
00351   char path[FN_REFLEN + 1];
00352   char new_path[FN_REFLEN + 1];
00353   char tmp_path[FN_REFLEN + 1];
00354 
00355 #ifndef DBUG_OFF
00356 
00357   bool tmp_table;
00358 #endif
00359 
00360   Alter_table_ctx &operator=(const Alter_table_ctx &rhs); // not implemented
00361   Alter_table_ctx(const Alter_table_ctx &rhs);            // not implemented
00362 };
00363 
00364 
00370 class Sql_cmd_common_alter_table : public Sql_cmd
00371 {
00372 protected:
00376   Sql_cmd_common_alter_table()
00377   {}
00378 
00379   virtual ~Sql_cmd_common_alter_table()
00380   {}
00381 
00382   virtual enum_sql_command sql_command_code() const
00383   {
00384     return SQLCOM_ALTER_TABLE;
00385   }
00386 };
00387 
00392 class Sql_cmd_alter_table : public Sql_cmd_common_alter_table
00393 {
00394 public:
00398   Sql_cmd_alter_table()
00399   {}
00400 
00401   ~Sql_cmd_alter_table()
00402   {}
00403 
00404   bool execute(THD *thd);
00405 };
00406 
00407 
00412 class Sql_cmd_discard_import_tablespace : public Sql_cmd_common_alter_table
00413 {
00414 public:
00415   enum enum_tablespace_op_type
00416   {
00417     DISCARD_TABLESPACE, IMPORT_TABLESPACE
00418   };
00419 
00420   Sql_cmd_discard_import_tablespace(enum_tablespace_op_type tablespace_op_arg)
00421     : m_tablespace_op(tablespace_op_arg)
00422   {}
00423 
00424   bool execute(THD *thd);
00425 
00426 private:
00427   const enum_tablespace_op_type m_tablespace_op;
00428 };
00429 
00430 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines