My Project
sql_lex.h
00001 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00015 
00020 #ifndef SQL_LEX_INCLUDED
00021 #define SQL_LEX_INCLUDED
00022 
00023 #include "violite.h"                            /* SSL_type */
00024 #include "sql_trigger.h"
00025 #include "item.h"               /* From item_subselect.h: subselect_union_engine */
00026 #include "thr_lock.h"                  /* thr_lock_type, TL_UNLOCK */
00027 #include "sql_array.h"
00028 #include "mem_root_array.h"
00029 #include "sql_alter.h"                // Alter_info
00030 
00031 /* YACC and LEX Definitions */
00032 
00033 /* These may not be declared yet */
00034 class Table_ident;
00035 class sql_exchange;
00036 class LEX_COLUMN;
00037 class sp_head;
00038 class sp_name;
00039 class sp_instr;
00040 class sp_pcontext;
00041 class st_alter_tablespace;
00042 class partition_info;
00043 class Event_parse_data;
00044 class set_var_base;
00045 class sys_var;
00046 class Item_func_match;
00047 class File_parser;
00048 class Key_part_spec;
00049 struct sql_digest_state;
00050 
00051 #ifdef MYSQL_SERVER
00052 /*
00053   There are 8 different type of table access so there is no more than
00054   combinations 2^8 = 256:
00055 
00056   . STMT_READS_TRANS_TABLE
00057 
00058   . STMT_READS_NON_TRANS_TABLE
00059 
00060   . STMT_READS_TEMP_TRANS_TABLE
00061 
00062   . STMT_READS_TEMP_NON_TRANS_TABLE
00063 
00064   . STMT_WRITES_TRANS_TABLE
00065 
00066   . STMT_WRITES_NON_TRANS_TABLE
00067 
00068   . STMT_WRITES_TEMP_TRANS_TABLE
00069 
00070   . STMT_WRITES_TEMP_NON_TRANS_TABLE
00071 
00072   The unsafe conditions for each combination is represented within a byte
00073   and stores the status of the option --binlog-direct-non-trans-updates,
00074   whether the trx-cache is empty or not, and whether the isolation level
00075   is lower than ISO_REPEATABLE_READ:
00076 
00077   . option (OFF/ON)
00078   . trx-cache (empty/not empty)
00079   . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
00080 
00081   bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
00082   bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
00083   bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
00084   bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
00085   bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
00086   bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
00087   bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
00088   bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
00089 */
00090 extern uint binlog_unsafe_map[256];
00091 /*
00092   Initializes the array with unsafe combinations and its respective
00093   conditions.
00094 */
00095 void binlog_unsafe_map_init();
00096 #endif
00097 
00101 struct sys_var_with_base
00102 {
00103   sys_var *var;
00104   LEX_STRING base_name;
00105 };
00106 
00107 #ifdef MYSQL_SERVER
00108 /*
00109   The following hack is needed because mysql_yacc.cc does not define
00110   YYSTYPE before including this file
00111 */
00112 #ifdef MYSQL_YACC
00113 #define LEX_YYSTYPE void *
00114 #else
00115 #include "lex_symbol.h"
00116 #if MYSQL_LEX
00117 #include "item_func.h"            /* Cast_target used in sql_yacc.h */
00118 #include "sql_get_diagnostics.h"  /* Types used in sql_yacc.h */
00119 #include "sql_yacc.h"
00120 #define LEX_YYSTYPE YYSTYPE *
00121 #else
00122 #define LEX_YYSTYPE void *
00123 #endif
00124 #endif
00125 #endif
00126 
00127 #include "sql_cmd.h"
00128 
00129 // describe/explain types
00130 #define DESCRIBE_NONE           0 // Not explain query
00131 #define DESCRIBE_NORMAL         1
00132 #define DESCRIBE_EXTENDED       2
00133 /*
00134   This is not within #ifdef because we want "EXPLAIN PARTITIONS ..." to produce
00135   additional "partitions" column even if partitioning is not compiled in.
00136 */
00137 #define DESCRIBE_PARTITIONS     4
00138 
00139 #ifdef MYSQL_SERVER
00140 
00141 enum enum_sp_suid_behaviour
00142 {
00143   SP_IS_DEFAULT_SUID= 0,
00144   SP_IS_NOT_SUID,
00145   SP_IS_SUID
00146 };
00147 
00148 enum enum_sp_data_access
00149 {
00150   SP_DEFAULT_ACCESS= 0,
00151   SP_CONTAINS_SQL,
00152   SP_NO_SQL,
00153   SP_READS_SQL_DATA,
00154   SP_MODIFIES_SQL_DATA
00155 };
00156 
00168 enum enum_sp_type
00169 {
00170   SP_TYPE_FUNCTION= 1,
00171   SP_TYPE_PROCEDURE,
00172   SP_TYPE_TRIGGER
00173 };
00174 
00175 /*
00176   Values for the type enum. This reflects the order of the enum declaration
00177   in the CREATE TABLE command. These values are used to enumerate object types
00178   for the ACL statements.
00179 
00180   These values were also used for enumerating stored program types. However, now
00181   enum_sp_type should be used for that instead of them.
00182 */
00183 #define TYPE_ENUM_FUNCTION  1
00184 #define TYPE_ENUM_PROCEDURE 2
00185 #define TYPE_ENUM_TRIGGER   3
00186 #define TYPE_ENUM_PROXY     4
00187 
00188 const LEX_STRING sp_data_access_name[]=
00189 {
00190   { C_STRING_WITH_LEN("") },
00191   { C_STRING_WITH_LEN("CONTAINS SQL") },
00192   { C_STRING_WITH_LEN("NO SQL") },
00193   { C_STRING_WITH_LEN("READS SQL DATA") },
00194   { C_STRING_WITH_LEN("MODIFIES SQL DATA") }
00195 };
00196 
00197 #define DERIVED_SUBQUERY        1
00198 #define DERIVED_VIEW            2
00199 
00200 enum enum_view_create_mode
00201 {
00202   VIEW_CREATE_NEW,              // check that there are not such VIEW/table
00203   VIEW_ALTER,                   // check that VIEW .frm with such name exists
00204   VIEW_CREATE_OR_REPLACE        // check only that there are not such table
00205 };
00206 
00207 enum enum_drop_mode
00208 {
00209   DROP_DEFAULT, // mode is not specified
00210   DROP_CASCADE, // CASCADE option
00211   DROP_RESTRICT // RESTRICT option
00212 };
00213 
00214 /* Options to add_table_to_list() */
00215 #define TL_OPTION_UPDATING      1
00216 #define TL_OPTION_FORCE_INDEX   2
00217 #define TL_OPTION_IGNORE_LEAVES 4
00218 #define TL_OPTION_ALIAS         8
00219 
00220 typedef List<Item> List_item;
00221 typedef Mem_root_array<ORDER*, true> Group_list_ptrs;
00222 
00223 /* SERVERS CACHE CHANGES */
00224 typedef struct st_lex_server_options
00225 {
00226   long port;
00227   uint server_name_length;
00228   char *server_name, *host, *db, *username, *password, *scheme, *socket, *owner;
00229 } LEX_SERVER_OPTIONS;
00230 
00231 
00239 typedef struct st_lex_master_info
00240 {
00241   char *host, *user, *password, *log_file_name, *bind_addr;
00242   uint port, connect_retry;
00243   float heartbeat_period;
00244   int sql_delay;
00245   ulonglong pos;
00246   ulong server_id, retry_count;
00247   char *gtid;
00248   enum {UNTIL_SQL_BEFORE_GTIDS= 0, UNTIL_SQL_AFTER_GTIDS} gtid_until_condition;
00249   bool until_after_gaps;
00250 
00251   /*
00252     Enum is used for making it possible to detect if the user
00253     changed variable or if it should be left at old value
00254    */
00255   enum {LEX_MI_UNCHANGED= 0, LEX_MI_DISABLE, LEX_MI_ENABLE}
00256     ssl, ssl_verify_server_cert, heartbeat_opt, repl_ignore_server_ids_opt, 
00257     retry_count_opt, auto_position;
00258   char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
00259   char *ssl_crl, *ssl_crlpath;
00260   char *relay_log_name;
00261   ulong relay_log_pos;
00262   DYNAMIC_ARRAY repl_ignore_server_ids;
00263   ulong server_ids_buffer[2];
00264   void set_unspecified();
00265 } LEX_MASTER_INFO;
00266 
00267 typedef struct st_lex_reset_slave
00268 {
00269   bool all;
00270 } LEX_RESET_SLAVE;
00271 
00272 enum sub_select_type
00273 {
00274   UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
00275   EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
00276 };
00277 
00278 enum olap_type 
00279 {
00280   UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
00281 };
00282 
00283 /* 
00284   String names used to print a statement with index hints.
00285   Keep in sync with index_hint_type.
00286 */
00287 extern const char * index_hint_type_name[];
00288 typedef uchar index_clause_map;
00289 
00290 /*
00291   Bits in index_clause_map : one for each possible FOR clause in
00292   USE/FORCE/IGNORE INDEX index hint specification
00293 */
00294 #define INDEX_HINT_MASK_JOIN  (1)
00295 #define INDEX_HINT_MASK_GROUP (1 << 1)
00296 #define INDEX_HINT_MASK_ORDER (1 << 2)
00297 
00298 #define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
00299                              INDEX_HINT_MASK_ORDER)
00300 
00301 /* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint  */
00302 class Index_hint : public Sql_alloc
00303 {
00304 public:
00305   /* The type of the hint : USE/FORCE/IGNORE */
00306   enum index_hint_type type;
00307   /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
00308   index_clause_map clause;
00309   /* 
00310     The index name. Empty (str=NULL) name represents an empty list 
00311     USE INDEX () clause 
00312   */ 
00313   LEX_STRING key_name;
00314 
00315   Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
00316               char *str, uint length) :
00317     type(type_arg), clause(clause_arg)
00318   {
00319     key_name.str= str;
00320     key_name.length= length;
00321   }
00322 
00323   void print(THD *thd, String *str);
00324 }; 
00325 
00326 /* 
00327   The state of the lex parsing for selects 
00328    
00329    master and slaves are pointers to select_lex.
00330    master is pointer to upper level node.
00331    slave is pointer to lower level node
00332    select_lex is a SELECT without union
00333    unit is container of either
00334      - One SELECT
00335      - UNION of selects
00336    select_lex and unit are both inherited form select_lex_node
00337    neighbors are two select_lex or units on the same level
00338 
00339    All select describing structures linked with following pointers:
00340    - list of neighbors (next/prev) (prev of first element point to slave
00341      pointer of upper structure)
00342      - For select this is a list of UNION's (or one element list)
00343      - For units this is a list of sub queries for the upper level select
00344 
00345    - pointer to master (master), which is
00346      If this is a unit
00347        - pointer to outer select_lex
00348      If this is a select_lex
00349        - pointer to outer unit structure for select
00350 
00351    - pointer to slave (slave), which is either:
00352      If this is a unit:
00353        - first SELECT that belong to this unit
00354      If this is a select_lex
00355        - first unit that belong to this SELECT (subquries or derived tables)
00356 
00357    - list of all select_lex (link_next/link_prev)
00358      This is to be used for things like derived tables creation, where we
00359      go through this list and create the derived tables.
00360 
00361    If unit contain several selects (UNION now, INTERSECT etc later)
00362    then it have special select_lex called fake_select_lex. It used for
00363    storing global parameters (like ORDER BY, LIMIT) and executing union.
00364    Subqueries used in global ORDER BY clause will be attached to this
00365    fake_select_lex, which will allow them correctly resolve fields of
00366    'upper' UNION and outer selects.
00367 
00368    For example for following query:
00369 
00370    select *
00371      from table1
00372      where table1.field IN (select * from table1_1_1 union
00373                             select * from table1_1_2)
00374      union
00375    select *
00376      from table2
00377      where table2.field=(select (select f1 from table2_1_1_1_1
00378                                    where table2_1_1_1_1.f2=table2_1_1.f3)
00379                            from table2_1_1
00380                            where table2_1_1.f1=table2.f2)
00381      union
00382    select * from table3;
00383 
00384    we will have following structure:
00385 
00386    select1: (select * from table1 ...)
00387    select2: (select * from table2 ...)
00388    select3: (select * from table3)
00389    select1.1.1: (select * from table1_1_1)
00390    ...
00391 
00392      main unit
00393      fake0
00394      select1 select2 select3
00395      |^^     |^
00396     s|||     ||master
00397     l|||     |+---------------------------------+
00398     a|||     +---------------------------------+|
00399     v|||master                         slave   ||
00400     e||+-------------------------+             ||
00401      V|            neighbor      |             V|
00402      unit1.1<+==================>unit1.2       unit2.1
00403      fake1.1
00404      select1.1.1 select 1.1.2    select1.2.1   select2.1.1
00405                                                |^
00406                                                ||
00407                                                V|
00408                                                unit2.1.1.1
00409                                                select2.1.1.1.1
00410 
00411 
00412    relation in main unit will be following:
00413    (bigger picture for:
00414       main unit
00415       fake0
00416       select1 select2 select3
00417    in the above picture)
00418 
00419          main unit
00420          |^^^^|fake_select_lex
00421          |||||+--------------------------------------------+
00422          ||||+--------------------------------------------+|
00423          |||+------------------------------+              ||
00424          ||+--------------+                |              ||
00425     slave||master         |                |              ||
00426          V|      neighbor |       neighbor |        master|V
00427          select1<========>select2<========>select3        fake0
00428 
00429     list of all select_lex will be following (as it will be constructed by
00430     parser):
00431 
00432     select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
00433                                                                           |
00434     +---------------------------------------------------------------------+
00435     |
00436     +->select1.1.1->select1.1.2
00437 
00438 */
00439 
00440 /* 
00441     Base class for st_select_lex (SELECT_LEX) & 
00442     st_select_lex_unit (SELECT_LEX_UNIT)
00443 */
00444 struct LEX;
00445 class st_select_lex;
00446 class st_select_lex_unit;
00447 
00448 
00449 class st_select_lex_node {
00450 protected:
00451   st_select_lex_node *next, **prev,   /* neighbor list */
00452     *master, *slave,                  /* vertical links */
00453     *link_next, **link_prev;          /* list of whole SELECT_LEX */
00454 public:
00455 
00456   ulonglong options;
00457 
00458   /*
00459     In sql_cache we store SQL_CACHE flag as specified by user to be
00460     able to restore SELECT statement from internal structures.
00461   */
00462   enum e_sql_cache { SQL_CACHE_UNSPECIFIED, SQL_NO_CACHE, SQL_CACHE };
00463   e_sql_cache sql_cache;
00464 
00465   /*
00466     result of this query can't be cached, bit field, can be :
00467       UNCACHEABLE_DEPENDENT
00468       UNCACHEABLE_RAND
00469       UNCACHEABLE_SIDEEFFECT
00470       UNCACHEABLE_EXPLAIN
00471       UNCACHEABLE_PREPARE
00472   */
00473   uint8 uncacheable;
00474   enum sub_select_type linkage;
00475   bool no_table_names_allowed; /* used for global order by */
00476   bool no_error; /* suppress error message (convert it to warnings) */
00477 
00478   static void *operator new(size_t size) throw ()
00479   {
00480     return sql_alloc(size);
00481   }
00482   static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
00483   { return (void*) alloc_root(mem_root, (uint) size); }
00484   static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
00485   static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
00486 
00487   // Ensures that at least all members used during cleanup() are initialized.
00488   st_select_lex_node()
00489     : next(NULL), prev(NULL),
00490       master(NULL), slave(NULL),
00491       link_next(NULL), link_prev(NULL),
00492       linkage(UNSPECIFIED_TYPE)
00493   {
00494   }
00495   virtual ~st_select_lex_node() {}
00496 
00497   inline st_select_lex_node* get_master() { return master; }
00498   virtual void init_query();
00499   virtual void init_select();
00500   void include_down(st_select_lex_node *upper);
00501   void include_neighbour(st_select_lex_node *before);
00502   void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
00503   void include_global(st_select_lex_node **plink);
00504   void exclude();
00505 
00506   virtual st_select_lex_unit* master_unit()= 0;
00507   virtual st_select_lex* outer_select()= 0;
00508 
00509   virtual bool set_braces(bool value);
00510   virtual bool inc_in_sum_expr();
00511   virtual uint get_in_sum_expr();
00512   virtual TABLE_LIST* get_table_list();
00513   virtual List<Item>* get_item_list();
00514   virtual ulong get_table_join_options();
00515   virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
00516                                         LEX_STRING *alias,
00517                                         ulong table_options,
00518                                         thr_lock_type flags= TL_UNLOCK,
00519                                         enum_mdl_type mdl_type= MDL_SHARED_READ,
00520                                         List<Index_hint> *hints= 0,
00521                                         List<String> *partition_names= 0,
00522                                         LEX_STRING *option= 0);
00523   virtual void set_lock_for_tables(thr_lock_type lock_type) {}
00524 
00525   friend class st_select_lex_unit;
00526   friend bool mysql_new_select(LEX *lex, bool move_down);
00527   friend bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
00528                               bool open_view_no_parse);
00529 private:
00530   void fast_exclude();
00531 };
00532 typedef class st_select_lex_node SELECT_LEX_NODE;
00533 
00534 /* 
00535    SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
00536    SELECT_LEXs
00537 */
00538 class THD;
00539 class select_result;
00540 class JOIN;
00541 class select_union;
00542 
00543 
00544 class st_select_lex_unit: public st_select_lex_node {
00545 protected:
00546   TABLE_LIST result_table_list;
00547   select_union *union_result;
00548   TABLE *table; /* temporary table using for appending UNION results */
00549 
00550   select_result *result;
00551   ulonglong found_rows_for_union;
00552   bool saved_error;
00553 
00554 public:
00555   // Ensures that at least all members used during cleanup() are initialized.
00556   st_select_lex_unit()
00557     : union_result(NULL), table(NULL), result(NULL),
00558       cleaned(false),
00559       fake_select_lex(NULL),
00560       explain_marker(0)
00561   {
00562   }
00563 
00564   bool  prepared, // prepare phase already performed for UNION (unit)
00565     optimized, // optimize phase already performed for UNION (unit)
00566     executed, // already executed
00567     cleaned;
00568 
00569   // list of fields which points to temporary table for union
00570   List<Item> item_list;
00571   /*
00572     list of types of items inside union (used for union & derived tables)
00573     
00574     Item_type_holders from which this list consist may have pointers to Field,
00575     pointers is valid only after preparing SELECTS of this unit and before
00576     any SELECT of this unit execution
00577 
00578     TODO:
00579     Possibly this member should be protected, and its direct use replaced
00580     by get_unit_column_types(). Check the places where it is used.
00581   */
00582   List<Item> types;
00583   /*
00584     Pointer to 'last' select or pointer to unit where stored
00585     global parameters for union
00586   */
00587   st_select_lex *global_parameters;
00588   /* LIMIT clause runtime counters */
00589   ha_rows select_limit_cnt, offset_limit_cnt;
00590   /* not NULL if unit used in subselect, point to subselect item */
00591   Item_subselect *item;
00592   /* thread handler */
00593   THD *thd;
00594   /*
00595     SELECT_LEX for hidden SELECT in onion which process global
00596     ORDER BY and LIMIT
00597   */
00598   st_select_lex *fake_select_lex;
00599 
00600   st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
00601   bool describe; /* union exec() called for EXPLAIN */
00602 
00612   int explain_marker;
00613 
00614   void init_query();
00615   st_select_lex_unit* master_unit();
00616   st_select_lex* outer_select();
00617   st_select_lex* first_select()
00618   {
00619     return reinterpret_cast<st_select_lex*>(slave);
00620   }
00621   st_select_lex_unit* next_unit()
00622   {
00623     return reinterpret_cast<st_select_lex_unit*>(next);
00624   }
00625   void exclude_level();
00626   void exclude_tree();
00627   inline select_result *get_result() { return result; }
00628 
00629   /* UNION methods */
00630   bool prepare(THD *thd, select_result *result, ulong additional_options);
00631   bool optimize();
00632   bool exec();
00633   bool explain();
00634   bool cleanup();
00635   bool cleanup_level();
00636   inline void unclean() { cleaned= 0; }
00637   void reinit_exec_mechanism();
00638 
00639   void print(String *str, enum_query_type query_type);
00640 
00641   bool add_fake_select_lex(THD *thd);
00642   bool init_prepare_fake_select_lex(THD *thd, bool no_const_tables);
00643   inline bool is_prepared() { return prepared; }
00644   bool change_result(select_result_interceptor *result,
00645                      select_result_interceptor *old_result);
00646   void set_limit(st_select_lex *values);
00647   void set_thd(THD *thd_arg) { thd= thd_arg; }
00648   inline bool is_union (); 
00649 
00650   friend void lex_start(THD *thd);
00651   friend bool subselect_union_engine::exec();
00652 
00653   List<Item> *get_unit_column_types();
00654   List<Item> *get_field_list();
00655 private:
00656   void invalidate();
00657 };
00658 
00659 typedef class st_select_lex_unit SELECT_LEX_UNIT;
00660 typedef Bounds_checked_array<Item*> Ref_ptr_array;
00661 
00662 /*
00663   SELECT_LEX - store information of parsed SELECT statment
00664 */
00665 class st_select_lex: public st_select_lex_node
00666 {
00667 public:
00668   Name_resolution_context context;
00669   /*
00670     Two fields used by semi-join transformations to know when semi-join is
00671     possible, and in which condition tree the subquery predicate is located.
00672   */
00673   enum Resolve_place { RESOLVE_NONE, RESOLVE_JOIN_NEST, RESOLVE_CONDITION,
00674                        RESOLVE_HAVING };
00675   Resolve_place resolve_place; // Indicates part of query being resolved
00676   TABLE_LIST *resolve_nest;    // Used when resolving outer join condition
00677   char *db;
00678   Item *where, *having;                         /* WHERE & HAVING clauses */
00679   Item *prep_where; /* saved WHERE clause for prepared statement processing */
00680   Item *prep_having;/* saved HAVING clause for prepared statement processing */
00689   Item::cond_result cond_value, having_value;
00690   /* point on lex in which it was created, used in view subquery detection */
00691   LEX *parent_lex;
00692   enum olap_type olap;
00693   /* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
00694   SQL_I_List<TABLE_LIST>  table_list;
00695 
00696   /*
00697     GROUP BY clause.
00698     This list may be mutated during optimization (by remove_const()),
00699     so for prepared statements, we keep a copy of the ORDER.next pointers in
00700     group_list_ptrs, and re-establish the original list before each execution.
00701   */
00702   SQL_I_List<ORDER>       group_list;
00703   Group_list_ptrs        *group_list_ptrs;
00704 
00711   List<Item>          item_list;
00712   List<String>        interval_list;
00713   bool                is_item_list_lookup;
00714   /* 
00715     Usualy it is pointer to ftfunc_list_alloc, but in union used to create fake
00716     select_lex for calling mysql_select under results of union
00717   */
00718   List<Item_func_match> *ftfunc_list;
00719   List<Item_func_match> ftfunc_list_alloc;
00720   JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
00721   List<TABLE_LIST> top_join_list; /* join list of the top level          */
00722   List<TABLE_LIST> *join_list;    /* list for the currently parsed join  */
00723   TABLE_LIST *embedding;          /* table embedding to the above list   */
00725   List<TABLE_LIST> sj_nests;
00726   //Dynamic_array<TABLE_LIST*> sj_nests; psergey-5:
00727   /*
00728     Beginning of the list of leaves in a FROM clause, where the leaves
00729     inlcude all base tables including view tables. The tables are connected
00730     by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
00731   */
00732   TABLE_LIST *leaf_tables;
00736   enum type_enum {
00737     SLT_NONE= 0,
00738     SLT_PRIMARY,
00739     SLT_SIMPLE,
00740     SLT_DERIVED,
00741     SLT_SUBQUERY,
00742     SLT_UNION,
00743     SLT_UNION_RESULT,
00744     SLT_MATERIALIZED,
00745   // Total:
00746     SLT_total 
00747   // Don't insert new types below this line!
00748   };
00749 
00750   /*
00751     ORDER BY clause.
00752     This list may be mutated during optimization (by remove_const()),
00753     so for prepared statements, we keep a copy of the ORDER.next pointers in
00754     order_list_ptrs, and re-establish the original list before each execution.
00755   */
00756   SQL_I_List<ORDER> order_list;
00757   Group_list_ptrs *order_list_ptrs;
00758 
00759   SQL_I_List<ORDER> gorder_list;
00760   Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
00761 
00763   Ref_ptr_array ref_pointer_array;
00764 
00766   uint derived_table_count;
00768   uint materialized_table_count;
00770   uint partitioned_table_count;
00771   /*
00772     number of items in select_list and HAVING clause used to get number
00773     bigger then can be number of entries that will be added to all item
00774     list during split_sum_func
00775   */
00776   uint select_n_having_items;
00777   uint cond_count;    /* number of arguments of and/or/xor in where/having/on */
00778   uint between_count; /* number of between predicates in where/having/on      */
00779   uint max_equal_elems; /* maximal number of elements in multiple equalities  */
00780   /*
00781     Number of fields used in select list or where clause of current select
00782     and all inner subselects.
00783   */
00784   uint select_n_where_fields;
00785   enum_parsing_place parsing_place; /* where we are parsing expression */
00786   bool with_sum_func;   /* sum function indicator */
00787 
00788   ulong table_join_options;
00789   uint in_sum_expr;
00790   uint select_number; /* number of select (used for EXPLAIN) */
00795   int nest_level;
00796   /* Circularly linked list of sum func in nested selects */
00797   Item_sum *inner_sum_func_list;
00798   uint with_wild; /* item list contain '*' */
00799   bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
00800   /* TRUE when having fix field called in processing of this SELECT */
00801   bool having_fix_field;
00802   /* TRUE when GROUP BY fix field called in processing of this SELECT */
00803   bool group_fix_field;
00804   /* List of references to fields referenced from inner selects */
00805   List<Item_outer_ref> inner_refs_list;
00806   /* Number of Item_sum-derived objects in this SELECT */
00807   uint n_sum_items;
00808   /* Number of Item_sum-derived objects in children and descendant SELECTs */
00809   uint n_child_sum_items;
00810 
00811   /* explicit LIMIT clause was used */
00812   bool explicit_limit;
00813   /*
00814     there are subquery in HAVING clause => we can't close tables before
00815     query processing end even if we use temporary table
00816   */
00817   bool subquery_in_having;
00818   /*
00819     This variable is required to ensure proper work of subqueries and
00820     stored procedures. Generally, one should use the states of
00821     Query_arena to determine if it's a statement prepare or first
00822     execution of a stored procedure. However, in case when there was an
00823     error during the first execution of a stored procedure, the SP body
00824     is not expelled from the SP cache. Therefore, a deeply nested
00825     subquery might be left unoptimized. So we need this per-subquery
00826     variable to inidicate the optimization/execution state of every
00827     subquery. Prepared statements work OK in that regard, as in
00828     case of an error during prepare the PS is not created.
00829   */
00830   bool first_execution;
00831   bool first_natural_join_processing;
00832   bool first_cond_optimization;
00833   /* do not wrap view fields with Item_ref */
00834   bool no_wrap_view_item;
00835   /* exclude this select from check of unique_table() */
00836   bool exclude_from_table_unique_test;
00837   /* List of table columns which are not under an aggregate function */
00838   List<Item_field> non_agg_fields;
00839 
00841   static const int ALL_FIELDS_UNDEF_POS= INT_MIN;
00842 
00853   int cur_pos_in_all_fields;
00854 
00855   List<udf_func>     udf_list;                  /* udf function calls stack */
00856 
00857   /* 
00858     This is a copy of the original JOIN USING list that comes from
00859     the parser. The parser :
00860       1. Sets the natural_join of the second TABLE_LIST in the join
00861          and the st_select_lex::prev_join_using.
00862       2. Makes a parent TABLE_LIST and sets its is_natural_join/
00863        join_using_fields members.
00864       3. Uses the wrapper TABLE_LIST as a table in the upper level.
00865     We cannot assign directly to join_using_fields in the parser because
00866     at stage (1.) the parent TABLE_LIST is not constructed yet and
00867     the assignment will override the JOIN USING fields of the lower level
00868     joins on the right.
00869   */
00870   List<String> *prev_join_using;
00875   table_map select_list_tables;
00877   st_select_lex *removed_select;
00878 
00879   void init_query();
00880   void init_select();
00881   st_select_lex_unit* master_unit();
00882   st_select_lex_unit* first_inner_unit()
00883   { 
00884     return (st_select_lex_unit*) slave; 
00885   }
00886   st_select_lex* outer_select();
00887   st_select_lex* next_select() { return (st_select_lex*) next; }
00888 
00889   st_select_lex* last_select() 
00890   { 
00891     st_select_lex* mylast= this;
00892     for (; mylast->next_select(); mylast= mylast->next_select())
00893     {}
00894     return mylast; 
00895   }
00896 
00897   st_select_lex* next_select_in_list() 
00898   {
00899     return (st_select_lex*) link_next;
00900   }
00901   st_select_lex_node** next_select_in_list_addr()
00902   {
00903     return &link_next;
00904   }
00905   void invalidate();
00906   void mark_as_dependent(st_select_lex *last);
00907 
00908   bool set_braces(bool value);
00909   bool inc_in_sum_expr();
00910   uint get_in_sum_expr();
00911 
00912   bool add_item_to_list(THD *thd, Item *item);
00913   bool add_group_to_list(THD *thd, Item *item, bool asc);
00914   bool add_ftfunc_to_list(Item_func_match *func);
00915   bool add_order_to_list(THD *thd, Item *item, bool asc);
00916   bool add_gorder_to_list(THD *thd, Item *item, bool asc);
00917   TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
00918                                 LEX_STRING *alias,
00919                                 ulong table_options,
00920                                 thr_lock_type flags= TL_UNLOCK,
00921                                 enum_mdl_type mdl_type= MDL_SHARED_READ,
00922                                 List<Index_hint> *hints= 0,
00923                                 List<String> *partition_names= 0,
00924                                 LEX_STRING *option= 0);
00925   TABLE_LIST* get_table_list();
00926   bool init_nested_join(THD *thd);
00927   TABLE_LIST *end_nested_join(THD *thd);
00928   TABLE_LIST *nest_last_join(THD *thd);
00929   void add_joined_table(TABLE_LIST *table);
00930   TABLE_LIST *convert_right_join();
00931   List<Item>* get_item_list();
00932   ulong get_table_join_options();
00933   void set_lock_for_tables(thr_lock_type lock_type);
00934   inline void init_order()
00935   {
00936     order_list.elements= 0;
00937     order_list.first= 0;
00938     order_list.next= &order_list.first;
00939   }
00940   /*
00941     This method created for reiniting LEX in mysql_admin_table() and can be
00942     used only if you are going remove all SELECT_LEX & units except belonger
00943     to LEX (LEX::unit & LEX::select, for other purposes there are
00944     SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
00945   */
00946   void cut_subtree() { slave= 0; }
00947   bool test_limit();
00948 
00949   friend void lex_start(THD *thd);
00950   st_select_lex() : group_list_ptrs(NULL), order_list_ptrs(NULL),
00951     n_sum_items(0), n_child_sum_items(0),
00952     cur_pos_in_all_fields(ALL_FIELDS_UNDEF_POS)
00953   {}
00954   void make_empty_select()
00955   {
00956     init_query();
00957     init_select();
00958   }
00959   bool setup_ref_array(THD *thd, uint order_group_num);
00960   void print(THD *thd, String *str, enum_query_type query_type);
00961   static void print_order(String *str,
00962                           ORDER *order,
00963                           enum_query_type query_type);
00964   void print_limit(THD *thd, String *str, enum_query_type query_type);
00965   void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
00966   /*
00967     Destroy the used execution plan (JOIN) of this subtree (this
00968     SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
00969   */
00970   bool cleanup();
00971   bool cleanup_level();
00972   /*
00973     Recursively cleanup the join of this select lex and of all nested
00974     select lexes.
00975   */
00976   void cleanup_all_joins(bool full);
00977 
00978   void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
00979 
00980   /* 
00981    Add a index hint to the tagged list of hints. The type and clause of the
00982    hint will be the current ones (set by set_index_hint()) 
00983   */
00984   bool add_index_hint (THD *thd, char *str, uint length);
00985 
00986   /* make a list to hold index hints */
00987   void alloc_index_hints (THD *thd);
00988   /* read and clear the index hints */
00989   List<Index_hint>* pop_index_hints(void) 
00990   {
00991     List<Index_hint> *hints= index_hints;
00992     index_hints= NULL;
00993     return hints;
00994   }
00995 
00996   void clear_index_hints(void) { index_hints= NULL; }
00997   bool handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*));
00998   bool is_part_of_union() { return master_unit()->is_union(); }
00999 
01000   /*
01001     For MODE_ONLY_FULL_GROUP_BY we need to maintain two flags:
01002      - Non-aggregated fields are used in this select.
01003      - Aggregate functions are used in this select.
01004     In MODE_ONLY_FULL_GROUP_BY only one of these may be true.
01005   */
01006   bool non_agg_field_used() const { return m_non_agg_field_used; }
01007   bool agg_func_used()      const { return m_agg_func_used; }
01008 
01009   void set_non_agg_field_used(bool val) { m_non_agg_field_used= val; }
01010   void set_agg_func_used(bool val)      { m_agg_func_used= val; }
01011 
01013   type_enum type(const THD *thd);
01014 
01016   const char *get_type_str(const THD *thd) { return type_str[type(thd)]; }
01017   static const char *get_type_str(type_enum type) { return type_str[type]; }
01018 
01019   bool is_dependent() const { return uncacheable & UNCACHEABLE_DEPENDENT; }
01020   bool is_cacheable() const
01021   {
01022     // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
01023     return !(uncacheable & ~UNCACHEABLE_EXPLAIN);
01024   }
01025   
01026 private:
01027   bool m_non_agg_field_used;
01028   bool m_agg_func_used;
01029 
01030   /* current index hint kind. used in filling up index_hints */
01031   enum index_hint_type current_index_hint_type;
01032   index_clause_map current_index_hint_clause;
01033   /* a list of USE/FORCE/IGNORE INDEX */
01034   List<Index_hint> *index_hints;
01035 
01036   static const char *type_str[SLT_total];
01037 };
01038 typedef class st_select_lex SELECT_LEX;
01039 
01040 inline bool st_select_lex_unit::is_union ()
01041 { 
01042   return first_select()->next_select() && 
01043     first_select()->next_select()->linkage == UNION_TYPE;
01044 }
01045 
01047 class Switch_resolve_place
01048 {
01049 public:
01050   Switch_resolve_place(SELECT_LEX::Resolve_place *rp_ptr,
01051                        SELECT_LEX::Resolve_place new_rp,
01052                        bool apply)
01053     : rp(NULL), saved_rp()
01054   {
01055     if (apply)
01056     {
01057       rp= rp_ptr;
01058       saved_rp= *rp;
01059       *rp= new_rp;
01060     }
01061   }
01062   ~Switch_resolve_place()
01063   {
01064     if (rp)
01065       *rp= saved_rp;
01066   }
01067 private:
01068   SELECT_LEX::Resolve_place *rp;
01069   SELECT_LEX::Resolve_place saved_rp;
01070 };
01071 
01072 
01073 typedef struct struct_slave_connection
01074 {
01075   char *user;
01076   char *password;
01077   char *plugin_auth;
01078   char *plugin_dir;
01079 
01080   void reset();
01081 } LEX_SLAVE_CONNECTION;
01082 
01083 struct st_sp_chistics
01084 {
01085   LEX_STRING comment;
01086   enum enum_sp_suid_behaviour suid;
01087   bool detistic;
01088   enum enum_sp_data_access daccess;
01089 };
01090 
01091 extern const LEX_STRING null_lex_str;
01092 extern const LEX_STRING empty_lex_str;
01093 
01094 struct st_trg_chistics
01095 {
01096   enum trg_action_time_type action_time;
01097   enum trg_event_type event;
01098 };
01099 
01100 extern sys_var *trg_new_row_fake_var;
01101 
01102 enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
01103                       XA_SUSPEND, XA_FOR_MIGRATE};
01104 
01105 extern const LEX_STRING null_lex_str;
01106 
01107 class Sroutine_hash_entry;
01108 
01109 /*
01110   Class representing list of all tables used by statement and other
01111   information which is necessary for opening and locking its tables,
01112   like SQL command for this statement.
01113 
01114   Also contains information about stored functions used by statement
01115   since during its execution we may have to add all tables used by its
01116   stored functions/triggers to this list in order to pre-open and lock
01117   them.
01118 
01119   Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
01120   methods to save and restore this information.
01121 */
01122 
01123 class Query_tables_list
01124 {
01125 public:
01132   enum_sql_command sql_command;
01133   /* Global list of all tables used by this statement */
01134   TABLE_LIST *query_tables;
01135   /* Pointer to next_global member of last element in the previous list. */
01136   TABLE_LIST **query_tables_last;
01137   /*
01138     If non-0 then indicates that query requires prelocking and points to
01139     next_global member of last own element in query table list (i.e. last
01140     table which was not added to it as part of preparation to prelocking).
01141     0 - indicates that this query does not need prelocking.
01142   */
01143   TABLE_LIST **query_tables_own_last;
01144   /*
01145     Set of stored routines called by statement.
01146     (Note that we use lazy-initialization for this hash).
01147   */
01148   enum { START_SROUTINES_HASH_SIZE= 16 };
01149   HASH sroutines;
01150   /*
01151     List linking elements of 'sroutines' set. Allows you to add new elements
01152     to this set as you iterate through the list of existing elements.
01153     'sroutines_list_own_last' is pointer to ::next member of last element of
01154     this list which represents routine which is explicitly used by query.
01155     'sroutines_list_own_elements' number of explicitly used routines.
01156     We use these two members for restoring of 'sroutines_list' to the state
01157     in which it was right after query parsing.
01158   */
01159   SQL_I_List<Sroutine_hash_entry> sroutines_list;
01160   Sroutine_hash_entry **sroutines_list_own_last;
01161   uint sroutines_list_own_elements;
01162 
01175   enum enum_lock_tables_state {
01176     LTS_NOT_LOCKED = 0,
01177     LTS_LOCKED
01178   };
01179   enum_lock_tables_state lock_tables_state;
01180   bool is_query_tables_locked()
01181   {
01182     return (lock_tables_state == LTS_LOCKED);
01183   }
01184 
01192   uint table_count;
01193 
01194   /*
01195     These constructor and destructor serve for creation/destruction
01196     of Query_tables_list instances which are used as backup storage.
01197   */
01198   Query_tables_list() {}
01199   ~Query_tables_list() {}
01200 
01201   /* Initializes (or resets) Query_tables_list object for "real" use. */
01202   void reset_query_tables_list(bool init);
01203   void destroy_query_tables_list();
01204   void set_query_tables_list(Query_tables_list *state)
01205   {
01206     *this= *state;
01207   }
01208 
01209   /*
01210     Direct addition to the list of query tables.
01211     If you are using this function, you must ensure that the table
01212     object, in particular table->db member, is initialized.
01213   */
01214   void add_to_query_tables(TABLE_LIST *table)
01215   {
01216     *(table->prev_global= query_tables_last)= table;
01217     query_tables_last= &table->next_global;
01218   }
01219   bool requires_prelocking()
01220   {
01221     return MY_TEST(query_tables_own_last);
01222   }
01223   void mark_as_requiring_prelocking(TABLE_LIST **tables_own_last)
01224   {
01225     query_tables_own_last= tables_own_last;
01226   }
01227   /* Return pointer to first not-own table in query-tables or 0 */
01228   TABLE_LIST* first_not_own_table()
01229   {
01230     return ( query_tables_own_last ? *query_tables_own_last : 0);
01231   }
01232   void chop_off_not_own_tables()
01233   {
01234     if (query_tables_own_last)
01235     {
01236       *query_tables_own_last= 0;
01237       query_tables_last= query_tables_own_last;
01238       query_tables_own_last= 0;
01239     }
01240   }
01241 
01243   TABLE_LIST *last_table()
01244   {
01245     /* Don't use offsetof() macro in order to avoid warnings. */
01246     return query_tables ?
01247            (TABLE_LIST*) ((char*) query_tables_last -
01248                           ((char*) &(query_tables->next_global) -
01249                            (char*) query_tables)) :
01250            0;
01251   }
01252 
01260   enum enum_binlog_stmt_unsafe {
01265     BINLOG_STMT_UNSAFE_LIMIT= 0,
01270     BINLOG_STMT_UNSAFE_INSERT_DELAYED,
01275     BINLOG_STMT_UNSAFE_SYSTEM_TABLE,
01285     BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS,
01289     BINLOG_STMT_UNSAFE_UDF,
01294     BINLOG_STMT_UNSAFE_SYSTEM_VARIABLE,
01298     BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION,
01299 
01305     BINLOG_STMT_UNSAFE_NONTRANS_AFTER_TRANS,
01306 
01311     BINLOG_STMT_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
01312 
01317     BINLOG_STMT_UNSAFE_MIXED_STATEMENT,
01318 
01324     BINLOG_STMT_UNSAFE_INSERT_IGNORE_SELECT,
01325 
01331     BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE,
01332 
01338     BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT,
01339 
01345     BINLOG_STMT_UNSAFE_REPLACE_SELECT,
01346 
01352     BINLOG_STMT_UNSAFE_CREATE_IGNORE_SELECT,
01353 
01359     BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT,
01360 
01367     BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC,
01368 
01374     BINLOG_STMT_UNSAFE_UPDATE_IGNORE,
01375 
01380     BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS,
01381 
01386     BINLOG_STMT_UNSAFE_AUTOINC_NOT_FIRST,
01387 
01388     /* The last element of this enumeration type. */
01389     BINLOG_STMT_UNSAFE_COUNT
01390   };
01395   static const int BINLOG_STMT_UNSAFE_ALL_FLAGS=
01396     ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
01397 
01401   static const int binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT];
01402 
01409   inline bool is_stmt_unsafe() const {
01410     return get_stmt_unsafe_flags() != 0;
01411   }
01412 
01420   inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
01421     DBUG_ENTER("set_stmt_unsafe");
01422     DBUG_ASSERT(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
01423     binlog_stmt_flags|= (1U << unsafe_type);
01424     DBUG_VOID_RETURN;
01425   }
01426 
01435   inline void set_stmt_unsafe_flags(uint32 flags) {
01436     DBUG_ENTER("set_stmt_unsafe_flags");
01437     DBUG_ASSERT((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
01438     binlog_stmt_flags|= flags;
01439     DBUG_VOID_RETURN;
01440   }
01441 
01448   inline uint32 get_stmt_unsafe_flags() const {
01449     DBUG_ENTER("get_stmt_unsafe_flags");
01450     DBUG_RETURN(binlog_stmt_flags & BINLOG_STMT_UNSAFE_ALL_FLAGS);
01451   }
01452 
01458   inline void clear_stmt_unsafe() {
01459     DBUG_ENTER("clear_stmt_unsafe");
01460     binlog_stmt_flags&= ~BINLOG_STMT_UNSAFE_ALL_FLAGS;
01461     DBUG_VOID_RETURN;
01462   }
01463 
01470   inline bool is_stmt_row_injection() const {
01471     return binlog_stmt_flags &
01472       (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
01473   }
01474 
01480   inline void set_stmt_row_injection() {
01481     DBUG_ENTER("set_stmt_row_injection");
01482     binlog_stmt_flags|=
01483       (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
01484     DBUG_VOID_RETURN;
01485   }
01486 
01487   enum enum_stmt_accessed_table
01488   {
01489     /*
01490        If a transactional table is about to be read. Note that
01491        a write implies a read.
01492     */
01493     STMT_READS_TRANS_TABLE= 0,
01494     /*
01495        If a non-transactional table is about to be read. Note that
01496        a write implies a read.
01497     */
01498     STMT_READS_NON_TRANS_TABLE,
01499     /*
01500        If a temporary transactional table is about to be read. Note
01501        that a write implies a read.
01502     */
01503     STMT_READS_TEMP_TRANS_TABLE,
01504     /*
01505        If a temporary non-transactional table is about to be read. Note
01506       that a write implies a read.
01507     */
01508     STMT_READS_TEMP_NON_TRANS_TABLE,
01509     /*
01510        If a transactional table is about to be updated.
01511     */
01512     STMT_WRITES_TRANS_TABLE,
01513     /*
01514        If a non-transactional table is about to be updated.
01515     */
01516     STMT_WRITES_NON_TRANS_TABLE,
01517     /*
01518        If a temporary transactional table is about to be updated.
01519     */
01520     STMT_WRITES_TEMP_TRANS_TABLE,
01521     /*
01522        If a temporary non-transactional table is about to be updated.
01523     */
01524     STMT_WRITES_TEMP_NON_TRANS_TABLE,
01525     /*
01526       The last element of the enumeration. Please, if necessary add
01527       anything before this.
01528     */
01529     STMT_ACCESS_TABLE_COUNT
01530   };
01531 
01532 #ifndef DBUG_OFF
01533   static inline const char *stmt_accessed_table_string(enum_stmt_accessed_table accessed_table)
01534   {
01535     switch (accessed_table)
01536     {
01537       case STMT_READS_TRANS_TABLE:
01538          return "STMT_READS_TRANS_TABLE";
01539       break;
01540       case STMT_READS_NON_TRANS_TABLE:
01541         return "STMT_READS_NON_TRANS_TABLE";
01542       break;
01543       case STMT_READS_TEMP_TRANS_TABLE:
01544         return "STMT_READS_TEMP_TRANS_TABLE";
01545       break;
01546       case STMT_READS_TEMP_NON_TRANS_TABLE:
01547         return "STMT_READS_TEMP_NON_TRANS_TABLE";
01548       break;  
01549       case STMT_WRITES_TRANS_TABLE:
01550         return "STMT_WRITES_TRANS_TABLE";
01551       break;
01552       case STMT_WRITES_NON_TRANS_TABLE:
01553         return "STMT_WRITES_NON_TRANS_TABLE";
01554       break;
01555       case STMT_WRITES_TEMP_TRANS_TABLE:
01556         return "STMT_WRITES_TEMP_TRANS_TABLE";
01557       break;
01558       case STMT_WRITES_TEMP_NON_TRANS_TABLE:
01559         return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
01560       break;
01561       case STMT_ACCESS_TABLE_COUNT:
01562       default:
01563         DBUG_ASSERT(0);
01564       break;
01565     }
01566     MY_ASSERT_UNREACHABLE();
01567     return "";
01568   }
01569 #endif  /* DBUG */
01570                
01571   #define BINLOG_DIRECT_ON 0xF0    /* unsafe when
01572                                       --binlog-direct-non-trans-updates
01573                                       is ON */
01574 
01575   #define BINLOG_DIRECT_OFF 0xF    /* unsafe when
01576                                       --binlog-direct-non-trans-updates
01577                                       is OFF */
01578 
01579   #define TRX_CACHE_EMPTY 0x33     /* unsafe when trx-cache is empty */
01580 
01581   #define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
01582 
01583   #define IL_LT_REPEATABLE 0xAA    /* unsafe when < ISO_REPEATABLE_READ */
01584 
01585   #define IL_GTE_REPEATABLE 0x55   /* unsafe when >= ISO_REPEATABLE_READ */
01586   
01594   inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table)
01595   {
01596     DBUG_ENTER("LEX::set_stmt_accessed_table");
01597 
01598     DBUG_ASSERT(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
01599     stmt_accessed_table_flag |= (1U << accessed_table);
01600 
01601     DBUG_VOID_RETURN;
01602   }
01603 
01615   inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table)
01616   {
01617     DBUG_ENTER("LEX::stmt_accessed_table");
01618 
01619     DBUG_ASSERT(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
01620 
01621     DBUG_RETURN((stmt_accessed_table_flag & (1U << accessed_table)) != 0);
01622   }
01623 
01624   /*
01625     Checks if a mixed statement is unsafe.
01626 
01627     
01628     @param in_multi_stmt_transaction_mode defines if there is an on-going
01629            multi-transactional statement.
01630     @param binlog_direct defines if --binlog-direct-non-trans-updates is
01631            active.
01632     @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
01633     @param trx_isolation defines the isolation level.
01634  
01635     @return
01636       @retval TRUE if the mixed statement is unsafe
01637       @retval FALSE otherwise
01638   */
01639   inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
01640                                    bool binlog_direct,
01641                                    bool trx_cache_is_not_empty,
01642                                    uint tx_isolation)
01643   {
01644     bool unsafe= FALSE;
01645 
01646     if (in_multi_stmt_transaction_mode)
01647     {
01648        uint condition=
01649          (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
01650          (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
01651          (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE : IL_LT_REPEATABLE);
01652 
01653       unsafe= (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
01654 
01655 #if !defined(DBUG_OFF)
01656       DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("RESULT %02X %02X %02X\n", condition,
01657               binlog_unsafe_map[stmt_accessed_table_flag],
01658               (binlog_unsafe_map[stmt_accessed_table_flag] & condition)));
01659  
01660       int type_in= 0;
01661       for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++)
01662       {
01663         if (stmt_accessed_table((enum_stmt_accessed_table) type_in))
01664           DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("ACCESSED %s ",
01665                   stmt_accessed_table_string((enum_stmt_accessed_table) type_in)));
01666       }
01667 #endif
01668     }
01669 
01670     if (stmt_accessed_table(STMT_WRITES_NON_TRANS_TABLE) &&
01671       stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
01672       tx_isolation < ISO_REPEATABLE_READ)
01673       unsafe= TRUE;
01674     else if (stmt_accessed_table(STMT_WRITES_TEMP_NON_TRANS_TABLE) &&
01675       stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
01676       tx_isolation < ISO_REPEATABLE_READ)
01677       unsafe= TRUE;
01678 
01679     return(unsafe);
01680   }
01681 
01686   bool uses_stored_routines() const
01687   { return sroutines_list.elements != 0; }
01688 
01689 private:
01690 
01696   enum enum_binlog_stmt_type {
01701     BINLOG_STMT_TYPE_ROW_INJECTION = 0,
01702 
01704     BINLOG_STMT_TYPE_COUNT
01705   };
01706 
01722   uint32 binlog_stmt_flags;
01723 
01728   uint32 stmt_accessed_table_flag;
01729 };
01730 
01731 
01732 /*
01733   st_parsing_options contains the flags for constructions that are
01734   allowed in the current statement.
01735 */
01736 
01737 struct st_parsing_options
01738 {
01739   bool allows_variable;
01740   bool allows_select_into;
01741   bool allows_select_procedure;
01742   bool allows_derived;
01743 
01744   st_parsing_options() { reset(); }
01745   void reset();
01746 };
01747 
01748 
01752 enum enum_comment_state
01753 {
01757   NO_COMMENT,
01762   PRESERVE_COMMENT,
01769   DISCARD_COMMENT
01770 };
01771 
01772 
01787 class Lex_input_stream
01788 {
01789 public:
01790   Lex_input_stream()
01791   {
01792   }
01793 
01794   ~Lex_input_stream()
01795   {
01796   }
01797 
01804   bool init(THD *thd, char *buff, unsigned int length);
01805 
01806   void reset(char *buff, unsigned int length);
01807 
01815   void set_echo(bool echo)
01816   {
01817     m_echo= echo;
01818   }
01819 
01820   void save_in_comment_state()
01821   {
01822     m_echo_saved= m_echo;
01823     in_comment_saved= in_comment;
01824   }
01825 
01826   void restore_in_comment_state()
01827   {
01828     m_echo= m_echo_saved;
01829     in_comment= in_comment_saved;
01830   }
01831 
01836   void skip_binary(int n)
01837   {
01838     if (m_echo)
01839     {
01840       memcpy(m_cpp_ptr, m_ptr, n);
01841       m_cpp_ptr += n;
01842     }
01843     m_ptr += n;
01844   }
01845 
01850   unsigned char yyGet()
01851   {
01852     char c= *m_ptr++;
01853     if (m_echo)
01854       *m_cpp_ptr++ = c;
01855     return c;
01856   }
01857 
01862   unsigned char yyGetLast()
01863   {
01864     return m_ptr[-1];
01865   }
01866 
01870   unsigned char yyPeek()
01871   {
01872     return m_ptr[0];
01873   }
01874 
01879   unsigned char yyPeekn(int n)
01880   {
01881     return m_ptr[n];
01882   }
01883 
01889   void yyUnget()
01890   {
01891     m_ptr--;
01892     if (m_echo)
01893       m_cpp_ptr--;
01894   }
01895 
01899   void yySkip()
01900   {
01901     if (m_echo)
01902       *m_cpp_ptr++ = *m_ptr++;
01903     else
01904       m_ptr++;
01905   }
01906 
01911   void yySkipn(int n)
01912   {
01913     if (m_echo)
01914     {
01915       memcpy(m_cpp_ptr, m_ptr, n);
01916       m_cpp_ptr += n;
01917     }
01918     m_ptr += n;
01919   }
01920 
01927   char *yyUnput(char ch)
01928   {
01929     *--m_ptr= ch;
01930     if (m_echo)
01931       m_cpp_ptr--;
01932     return m_ptr;
01933   }
01934 
01942   char *cpp_inject(char ch)
01943   {
01944     *m_cpp_ptr= ch;
01945     return ++m_cpp_ptr;
01946   }
01947 
01952   bool eof()
01953   {
01954     return (m_ptr >= m_end_of_query);
01955   }
01956 
01962   bool eof(int n)
01963   {
01964     return ((m_ptr + n) >= m_end_of_query);
01965   }
01966 
01968   const char *get_buf()
01969   {
01970     return m_buf;
01971   }
01972 
01974   const char *get_cpp_buf()
01975   {
01976     return m_cpp_buf;
01977   }
01978 
01980   const char *get_end_of_query()
01981   {
01982     return m_end_of_query;
01983   }
01984 
01986   void start_token()
01987   {
01988     m_tok_start_prev= m_tok_start;
01989     m_tok_start= m_ptr;
01990     m_tok_end= m_ptr;
01991 
01992     m_cpp_tok_start_prev= m_cpp_tok_start;
01993     m_cpp_tok_start= m_cpp_ptr;
01994     m_cpp_tok_end= m_cpp_ptr;
01995   }
01996 
02001   void restart_token()
02002   {
02003     m_tok_start= m_ptr;
02004     m_cpp_tok_start= m_cpp_ptr;
02005   }
02006 
02008   const char *get_tok_start()
02009   {
02010     return m_tok_start;
02011   }
02012 
02014   const char *get_cpp_tok_start()
02015   {
02016     return m_cpp_tok_start;
02017   }
02018 
02020   const char *get_tok_end()
02021   {
02022     return m_tok_end;
02023   }
02024 
02026   const char *get_cpp_tok_end()
02027   {
02028     return m_cpp_tok_end;
02029   }
02030 
02032   const char *get_tok_start_prev()
02033   {
02034     return m_tok_start_prev;
02035   }
02036 
02038   const char *get_ptr()
02039   {
02040     return m_ptr;
02041   }
02042 
02044   const char *get_cpp_ptr()
02045   {
02046     return m_cpp_ptr;
02047   }
02048 
02050   uint yyLength()
02051   {
02052     /*
02053       The assumption is that the lexical analyser is always 1 character ahead,
02054       which the -1 account for.
02055     */
02056     DBUG_ASSERT(m_ptr > m_tok_start);
02057     return (uint) ((m_ptr - m_tok_start) - 1);
02058   }
02059    
02061   const char *get_body_utf8_str()
02062   {
02063     return m_body_utf8;
02064   }
02065 
02067   uint get_body_utf8_length()
02068   {
02069     return (uint) (m_body_utf8_ptr - m_body_utf8);
02070   }
02071 
02072   void body_utf8_start(THD *thd, const char *begin_ptr);
02073   void body_utf8_append(const char *ptr);
02074   void body_utf8_append(const char *ptr, const char *end_ptr);
02075   void body_utf8_append_literal(THD *thd,
02076                                 const LEX_STRING *txt,
02077                                 const CHARSET_INFO *txt_cs,
02078                                 const char *end_ptr);
02079 
02081   THD *m_thd;
02082 
02084   uint yylineno;
02085 
02087   uint yytoklen;
02088 
02090   LEX_YYSTYPE yylval;
02091 
02098   int lookahead_token;
02099 
02101   LEX_YYSTYPE lookahead_yylval;
02102 
02103   void add_digest_token(uint token, LEX_YYSTYPE yylval);
02104 
02105   void reduce_digest_token(uint token_left, uint token_right);
02106 
02107 private:
02109   char *m_ptr;
02110 
02112   const char *m_tok_start;
02113 
02115   const char *m_tok_end;
02116 
02118   const char *m_end_of_query;
02119 
02121   const char *m_tok_start_prev;
02122 
02124   const char *m_buf;
02125 
02127   uint m_buf_length;
02128 
02130   bool m_echo;
02131   bool m_echo_saved;
02132 
02134   char *m_cpp_buf;
02135 
02137   char *m_cpp_ptr;
02138 
02143   const char *m_cpp_tok_start;
02144 
02149   const char *m_cpp_tok_start_prev;
02150 
02155   const char *m_cpp_tok_end;
02156 
02158   char *m_body_utf8;
02159 
02161   char *m_body_utf8_ptr;
02162 
02167   const char *m_cpp_utf8_processed_ptr;
02168 
02169 public:
02170 
02172   enum my_lex_states next_state;
02173 
02178   const char *found_semicolon;
02179 
02181   uchar tok_bitmap;
02182 
02184   bool ignore_space;
02185 
02190   bool stmt_prepare_mode;
02194   bool multi_statements;
02195 
02197   enum_comment_state in_comment;
02198   enum_comment_state in_comment_saved;
02199 
02206   const char *m_cpp_text_start;
02207 
02214   const char *m_cpp_text_end;
02215 
02221   CHARSET_INFO *m_underscore_cs;
02222 
02226   sql_digest_state* m_digest;
02227 };
02228 
02229 
02234 struct Proc_analyse_params: public Sql_alloc
02235 {
02236   uint max_tree_elements; //< maximum number of distinct values per column
02237   uint max_treemem; //< maximum amount of memory to allocate per column
02238 
02239   Proc_analyse_params()
02240     : max_tree_elements(256),
02241       max_treemem(8192)
02242   {}
02243 };
02244 
02245 
02246 /* The state of the lex parsing. This is saved in the THD struct */
02247 
02248 struct LEX: public Query_tables_list
02249 {
02250   SELECT_LEX_UNIT unit;                         /* most upper unit */
02251   SELECT_LEX select_lex;                        /* first SELECT_LEX */
02252   /* current SELECT_LEX in parsing */
02253   SELECT_LEX *current_select;
02254   /* list of all SELECT_LEX */
02255   SELECT_LEX *all_selects_list;
02256 
02257   char *length,*dec,*change;
02258   LEX_STRING name;
02259   char *help_arg;
02260   char* to_log;                                 /* For PURGE MASTER LOGS TO */
02261   char* x509_subject,*x509_issuer,*ssl_cipher;
02262   String *wild;
02263   sql_exchange *exchange;
02264   select_result *result;
02265   Item *default_value, *on_update_value;
02266   LEX_STRING comment, ident;
02267   LEX_USER *grant_user;
02268   XID *xid;
02269   THD *thd;
02270 
02271   /* maintain a list of used plugins for this LEX */
02272   DYNAMIC_ARRAY plugins;
02273   plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
02274 
02275   const CHARSET_INFO *charset;
02276   bool text_string_is_7bit;
02277   /* store original leaf_tables for INSERT SELECT and PS/SP */
02278   TABLE_LIST *leaf_tables_insert;
02279 
02281   LEX_STRING create_view_select;
02282 
02284   const char* raw_trg_on_table_name_begin;
02286   const char* raw_trg_on_table_name_end;
02287 
02288   /* Partition info structure filled in by PARTITION BY parse part */
02289   partition_info *part_info;
02290 
02291   /*
02292     The definer of the object being created (view, trigger, stored routine).
02293     I.e. the value of DEFINER clause.
02294   */
02295   LEX_USER *definer;
02296 
02297   List<Key_part_spec> col_list;
02298   List<Key_part_spec> ref_list;
02299   /*
02300     A list of strings is maintained to store the SET clause command user strings
02301     which are specified in load data operation.  This list will be used
02302     during the reconstruction of "load data" statement at the time of writing
02303     to binary log.
02304    */
02305   List<String>        load_set_str_list;
02306   List<String>        interval_list;
02307   List<LEX_USER>      users_list;
02308   List<LEX_COLUMN>    columns;
02309   List<Item>          *insert_list,field_list,value_list,update_list;
02310   List<List_item>     many_values;
02311   List<set_var_base>  var_list;
02312   List<Item_func_set_user_var> set_var_list; // in-query assignment list
02313   List<Item_param>    param_list;
02314   List<LEX_STRING>    view_list; // view list (list of field names in view)
02315   /*
02316     A stack of name resolution contexts for the query. This stack is used
02317     at parse time to set local name resolution contexts for various parts
02318     of a query. For example, in a JOIN ... ON (some_condition) clause the
02319     Items in 'some_condition' must be resolved only against the operands
02320     of the the join, and not against the whole clause. Similarly, Items in
02321     subqueries should be resolved against the subqueries (and outer queries).
02322     The stack is used in the following way: when the parser detects that
02323     all Items in some clause need a local context, it creates a new context
02324     and pushes it on the stack. All newly created Items always store the
02325     top-most context in the stack. Once the parser leaves the clause that
02326     required a local context, the parser pops the top-most context.
02327   */
02328   List<Name_resolution_context> context_stack;
02329 
02333   Proc_analyse_params *proc_analyse;
02334   SQL_I_List<TABLE_LIST> auxiliary_table_list, save_list;
02335   Create_field        *last_field;
02336   Item_sum *in_sum_func;
02337   udf_func udf;
02338   HA_CHECK_OPT   check_opt;                     // check/repair options
02339   HA_CREATE_INFO create_info;
02340   KEY_CREATE_INFO key_create_info;
02341   LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
02342   LEX_SLAVE_CONNECTION slave_connection;
02343   LEX_SERVER_OPTIONS server_options;
02344   USER_RESOURCES mqh;
02345   LEX_RESET_SLAVE reset_slave_info;
02346   ulong type;
02347   /*
02348     This variable is used in post-parse stage to declare that sum-functions,
02349     or functions which have sense only if GROUP BY is present, are allowed.
02350     For example in a query
02351     SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
02352     MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
02353     in the HAVING clause. Due to possible nesting of select construct
02354     the variable can contain 0 or 1 for each nest level.
02355   */
02356   nesting_map allow_sum_func;
02357 
02358   Sql_cmd *m_sql_cmd;
02359 
02360   /*
02361     Usually `expr` rule of yacc is quite reused but some commands better
02362     not support subqueries which comes standard with this rule, like
02363     KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
02364     syntax error back.
02365   */
02366   bool expr_allows_subselect;
02367 
02368   enum SSL_type ssl_type;                       /* defined in violite.h */
02369   enum enum_duplicates duplicates;
02370   enum enum_tx_isolation tx_isolation;
02371   enum xa_option_words xa_opt;
02372   enum enum_var_type option_type;
02373   enum enum_view_create_mode create_view_mode;
02374   enum enum_drop_mode drop_mode;
02375 
02376   uint profile_query_id;
02377   uint profile_options;
02378   uint uint_geom_type;
02379   uint grant, grant_tot_col, which_columns;
02380   enum Foreign_key::fk_match_opt fk_match_option;
02381   enum Foreign_key::fk_option fk_update_opt;
02382   enum Foreign_key::fk_option fk_delete_opt;
02383   uint slave_thd_opt, start_transaction_opt;
02384   int nest_level;
02385   uint8 describe;
02386   /*
02387     A flag that indicates what kinds of derived tables are present in the
02388     query (0 if no derived tables, otherwise a combination of flags
02389     DERIVED_SUBQUERY and DERIVED_VIEW).
02390   */
02391   uint8 derived_tables;
02392   uint8 create_view_algorithm;
02393   uint8 create_view_check;
02394   uint8 context_analysis_only;
02395   bool drop_if_exists, drop_temporary, local_file, one_shot_set;
02396   bool autocommit;
02397   bool verbose, no_write_to_binlog;
02398 
02399   enum enum_yes_no_unknown tx_chain, tx_release;
02400   bool safe_to_cache_query;
02401   bool subqueries, ignore;
02402   st_parsing_options parsing_options;
02403   Alter_info alter_info;
02404   /*
02405     For CREATE TABLE statement last element of table list which is not
02406     part of SELECT or LIKE part (i.e. either element for table we are
02407     creating or last of tables referenced by foreign keys).
02408   */
02409   TABLE_LIST *create_last_non_select_table;
02410   /* Prepared statements SQL syntax:*/
02411   LEX_STRING prepared_stmt_name; /* Statement name (in all queries) */
02412   /*
02413     Prepared statement query text or name of variable that holds the
02414     prepared statement (in PREPARE ... queries)
02415   */
02416   LEX_STRING prepared_stmt_code;
02417   /* If true, prepared_stmt_code is a name of variable that holds the query */
02418   bool prepared_stmt_code_is_varref;
02419   /* Names of user variables holding parameters (in EXECUTE) */
02420   List<LEX_STRING> prepared_stmt_params;
02421   sp_head *sphead;
02422   sp_name *spname;
02423   bool sp_lex_in_use;   /* Keep track on lex usage in SPs for error handling */
02424   bool all_privileges;
02425   bool proxy_priv;
02426   bool is_change_password;
02427   /*
02428     Temporary variable to distinguish SET PASSWORD command from others
02429     SQLCOM_SET_OPTION commands. Should be removed when WL#6409 is
02430     introduced.
02431   */
02432   bool is_set_password_sql;
02433   bool contains_plaintext_password;
02434 
02435 private:
02436   bool m_broken; 
02437 
02438 
02439   sp_pcontext *sp_current_parsing_ctx;
02440 
02441 public:
02442 
02443   bool is_broken() const { return m_broken; }
02452   void mark_broken(bool broken= true)
02453   {
02454     if (broken)
02455     {
02456       /*
02457         "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
02458         ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
02459         in constructions of sp_instr_cpush, and thus
02460         sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
02461         as broken only if tables are used.
02462       */
02463       if (is_metadata_used())
02464         m_broken= true;
02465     }
02466     else
02467       m_broken= false;
02468   }
02469 
02470   sp_pcontext *get_sp_current_parsing_ctx()
02471   { return sp_current_parsing_ctx; }
02472 
02473   void set_sp_current_parsing_ctx(sp_pcontext *ctx)
02474   { sp_current_parsing_ctx= ctx; }
02475 
02478   bool is_metadata_used() const
02479   { return query_tables != NULL || sroutines.records > 0; }
02480 
02481 public:
02482   st_sp_chistics sp_chistics;
02483 
02484   Event_parse_data *event_parse_data;
02485 
02486   bool only_view;       /* used for SHOW CREATE TABLE/VIEW */
02487   /*
02488     field_list was created for view and should be removed before PS/SP
02489     rexecuton
02490   */
02491   bool empty_field_list_on_rset;
02492   /*
02493     view created to be run from definer (standard behaviour)
02494   */
02495   uint8 create_view_suid;
02496 
02497   /*
02498     stmt_definition_begin is intended to point to the next word after
02499     DEFINER-clause in the following statements:
02500       - CREATE TRIGGER (points to "TRIGGER");
02501       - CREATE PROCEDURE (points to "PROCEDURE");
02502       - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
02503       - CREATE EVENT (points to "EVENT")
02504 
02505     This pointer is required to add possibly omitted DEFINER-clause to the
02506     DDL-statement before dumping it to the binlog.
02507 
02508     keyword_delayed_begin_offset is the offset to the beginning of the DELAYED
02509     keyword in INSERT DELAYED statement. keyword_delayed_end_offset is the
02510     offset to the character right after the DELAYED keyword.
02511   */
02512   union {
02513     const char *stmt_definition_begin;
02514     uint keyword_delayed_begin_offset;
02515   };
02516 
02517   union {
02518     const char *stmt_definition_end;
02519     uint keyword_delayed_end_offset;
02520   };
02521 
02528   bool use_only_table_context;
02529 
02530   /*
02531     Reference to a struct that contains information in various commands
02532     to add/create/drop/change table spaces.
02533   */
02534   st_alter_tablespace *alter_tablespace_info;
02535   
02536   bool escape_used;
02537   bool is_lex_started; /* If lex_start() did run. For debugging. */
02538 
02539   /*
02540     The set of those tables whose fields are referenced in all subqueries
02541     of the query.
02542     TODO: possibly this it is incorrect to have used tables in LEX because
02543     with subquery, it is not clear what does the field mean. To fix this
02544     we should aggregate used tables information for selected expressions
02545     into the select_lex.
02546   */
02547   table_map  used_tables;
02548 
02549   class Explain_format *explain_format;
02550 
02551   LEX();
02552 
02553   virtual ~LEX()
02554   {
02555     destroy_query_tables_list();
02556     plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
02557     delete_dynamic(&plugins);
02558   }
02559 
02560   inline bool is_ps_or_view_context_analysis()
02561   {
02562     return (context_analysis_only &
02563             (CONTEXT_ANALYSIS_ONLY_PREPARE |
02564              CONTEXT_ANALYSIS_ONLY_VIEW));
02565   }
02566 
02567   inline void uncacheable(uint8 cause)
02568   {
02569     safe_to_cache_query= 0;
02570 
02571     /*
02572       There are no sense to mark select_lex and union fields of LEX,
02573       but we should merk all subselects as uncacheable from current till
02574       most upper
02575     */
02576     SELECT_LEX *sl;
02577     SELECT_LEX_UNIT *un;
02578     for (sl= current_select, un= sl->master_unit();
02579          un != &unit;
02580          sl= sl->outer_select(), un= sl->master_unit())
02581     {
02582       sl->uncacheable|= cause;
02583       un->uncacheable|= cause;
02584     }
02585   }
02586   void set_trg_event_type_for_tables();
02587 
02588   TABLE_LIST *unlink_first_table(bool *link_to_local);
02589   void link_first_table_back(TABLE_LIST *first, bool link_to_local);
02590   void first_lists_tables_same();
02591 
02592   bool can_be_merged();
02593   bool can_use_merged();
02594   bool can_not_use_merged();
02595   bool only_view_structure();
02596   bool need_correct_ident();
02597   uint8 get_effective_with_check(TABLE_LIST *view);
02598   /*
02599     Is this update command where 'WHITH CHECK OPTION' clause is important
02600 
02601     SYNOPSIS
02602       LEX::which_check_option_applicable()
02603 
02604     RETURN
02605       TRUE   have to take 'WHITH CHECK OPTION' clause into account
02606       FALSE  'WHITH CHECK OPTION' clause do not need
02607   */
02608   inline bool which_check_option_applicable()
02609   {
02610     switch (sql_command) {
02611     case SQLCOM_UPDATE:
02612     case SQLCOM_UPDATE_MULTI:
02613     case SQLCOM_INSERT:
02614     case SQLCOM_INSERT_SELECT:
02615     case SQLCOM_REPLACE:
02616     case SQLCOM_REPLACE_SELECT:
02617     case SQLCOM_LOAD:
02618       return TRUE;
02619     default:
02620       return FALSE;
02621     }
02622   }
02623 
02624   void cleanup_after_one_table_open();
02625 
02626   bool push_context(Name_resolution_context *context)
02627   {
02628     return context_stack.push_front(context);
02629   }
02630 
02631   void pop_context()
02632   {
02633     context_stack.pop();
02634   }
02635 
02636   bool copy_db_to(char **p_db, size_t *p_db_length) const;
02637 
02638   Name_resolution_context *current_context()
02639   {
02640     return context_stack.head();
02641   }
02642   /*
02643     Restore the LEX and THD in case of a parse error.
02644   */
02645   static void cleanup_lex_after_parse_error(THD *thd);
02646 
02647   void reset_n_backup_query_tables_list(Query_tables_list *backup);
02648   void restore_backup_query_tables_list(Query_tables_list *backup);
02649 
02650   bool table_or_sp_used();
02651   bool is_partition_management() const;
02652 
02660   bool is_single_level_stmt() 
02661   { 
02662     /* 
02663       This check exploits the fact that the last added to all_select_list is
02664       on its top. So select_lex (as the first added) will be at the tail 
02665       of the list.
02666     */ 
02667     if (&select_lex == all_selects_list && !sroutines.records)
02668     {
02669       DBUG_ASSERT(!all_selects_list->next_select_in_list());
02670       return TRUE;
02671     }
02672     return FALSE;
02673   }
02674 };
02675 
02676 
02682 class Set_signal_information
02683 {
02684 public:
02686  Set_signal_information() {} 
02687 
02689   Set_signal_information(const Set_signal_information& set);
02690 
02692   ~Set_signal_information()
02693   {}
02694 
02696   void clear();
02697 
02703   Item *m_item[LAST_DIAG_SET_PROPERTY+1];
02704 };
02705 
02706 
02712 class Yacc_state
02713 {
02714 public:
02715   Yacc_state()
02716   {
02717     reset();
02718   }
02719 
02720   void reset()
02721   {
02722     yacc_yyss= NULL;
02723     yacc_yyvs= NULL;
02724     m_set_signal_info.clear();
02725     m_lock_type= TL_READ_DEFAULT;
02726     m_mdl_type= MDL_SHARED_READ;
02727     m_ha_rkey_mode= HA_READ_KEY_EXACT;
02728   }
02729 
02730   ~Yacc_state();
02731 
02736   void reset_before_substatement()
02737   {
02738     m_lock_type= TL_READ_DEFAULT;
02739     m_mdl_type= MDL_SHARED_READ;
02740     m_ha_rkey_mode= HA_READ_KEY_EXACT; /* Let us be future-proof. */
02741   }
02742 
02747   uchar *yacc_yyss;
02748 
02753   uchar *yacc_yyvs;
02754 
02759   Set_signal_information m_set_signal_info;
02760 
02778   thr_lock_type m_lock_type;
02779 
02784   enum_mdl_type m_mdl_type;
02785 
02787   enum ha_rkey_function m_ha_rkey_mode;
02788 
02789   /*
02790     TODO: move more attributes from the LEX structure here.
02791   */
02792 };
02793 
02797 struct Parser_input
02798 {
02799   bool m_compute_digest;
02800 
02801   Parser_input()
02802     : m_compute_digest(false)
02803   {}
02804 };
02805 
02812 class Parser_state
02813 {
02814 public:
02815   Parser_state()
02816     : m_yacc()
02817   {}
02818 
02825   bool init(THD *thd, char *buff, unsigned int length)
02826   {
02827     return m_lip.init(thd, buff, length);
02828   }
02829 
02830   ~Parser_state()
02831   {}
02832 
02833   Parser_input m_input;
02834   Lex_input_stream m_lip;
02835   Yacc_state m_yacc;
02836 
02840   PSI_digest_locker* m_digest_psi;
02841 
02842   void reset(char *found_semicolon, unsigned int length)
02843   {
02844     m_lip.reset(found_semicolon, length);
02845     m_yacc.reset();
02846   }
02847 };
02848 
02849 extern sql_digest_state *
02850 digest_add_token(sql_digest_state *state, uint token, LEX_YYSTYPE yylval);
02851 
02852 extern sql_digest_state *
02853 digest_reduce_token(sql_digest_state *state, uint token_left, uint token_right);
02854 
02855 struct st_lex_local: public LEX
02856 {
02857   static void *operator new(size_t size) throw()
02858   {
02859     return sql_alloc(size);
02860   }
02861   static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
02862   {
02863     return (void*) alloc_root(mem_root, (uint) size);
02864   }
02865   static void operator delete(void *ptr,size_t size)
02866   { TRASH(ptr, size); }
02867   static void operator delete(void *ptr, MEM_ROOT *mem_root)
02868   { /* Never called */ }
02869 };
02870 
02871 extern void lex_init(void);
02872 extern void lex_free(void);
02873 extern void lex_start(THD *thd);
02874 extern void lex_end(LEX *lex);
02875 extern int MYSQLlex(union YYSTYPE *yylval, class THD *thd);
02876 
02877 extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
02878 
02879 extern bool is_lex_native_function(const LEX_STRING *name);
02880 
02885 void my_missing_function_error(const LEX_STRING &token, const char *name);
02886 bool is_keyword(const char *name, uint len);
02887 bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
02888 
02889 #endif /* MYSQL_SERVER */
02890 #endif /* SQL_LEX_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines