Drizzled Public API Documentation

sql_lex.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00025 #include <drizzled/message/table.pb.h>
00026 #include <drizzled/name_resolution_context.h>
00027 #include <drizzled/table_list.h>
00028 #include <drizzled/function/math/real.h>
00029 #include <drizzled/key_part_spec.h>
00030 #include <drizzled/index_hint.h>
00031 #include <drizzled/optimizer/explain_plan.h>
00032 
00033 #include <bitset>
00034 #include <string>
00035 
00036 /*
00037   The following hack is needed because mysql_yacc.cc does not define
00038   YYSTYPE before including this file
00039 */
00040 
00041 #ifdef DRIZZLE_SERVER
00042 /* set_var should change to set_var here ... */
00043 # include <drizzled/sys_var.h>
00044 # include <drizzled/item/func.h>
00045 # ifdef DRIZZLE_YACC
00046 #  define LEX_YYSTYPE void *
00047 # else
00048 #  if defined(DRIZZLE_LEX)
00049 #   include <drizzled/foreign_key.h>
00050 #   include <drizzled/lex_symbol.h>
00051 #   include <drizzled/comp_creator.h>
00052 #   include <drizzled/sql_yacc.hh>
00053 #   define LEX_YYSTYPE YYSTYPE *
00054 #  else
00055 #   define LEX_YYSTYPE void *
00056 #  endif /* defined(DRIZZLE_LEX) */
00057 # endif /* DRIZZLE_YACC */
00058 #endif /* DRIZZLE_SERVER */
00059 
00060 // describe/explain types
00061 #define DESCRIBE_NORMAL   1
00062 #define DESCRIBE_EXTENDED 2
00063 
00064 #ifdef DRIZZLE_SERVER
00065 
00066 #define DERIVED_NONE  0
00067 #define DERIVED_SUBQUERY  1
00068 
00069 namespace drizzled
00070 {
00071 
00072 typedef List<Item> List_item;
00073 
00074 enum sub_select_type
00075 {
00076   UNSPECIFIED_TYPE,
00077   UNION_TYPE,
00078   INTERSECT_TYPE,
00079   EXCEPT_TYPE,
00080   GLOBAL_OPTIONS_TYPE,
00081   DERIVED_TABLE_TYPE,
00082   OLAP_TYPE
00083 };
00084 
00085 enum olap_type
00086 {
00087   UNSPECIFIED_OLAP_TYPE,
00088   CUBE_TYPE,
00089   ROLLUP_TYPE
00090 };
00091 
00092 /*
00093   The state of the lex parsing for selects
00094 
00095    master and slaves are pointers to select_lex.
00096    master is pointer to upper level node.
00097    slave is pointer to lower level node
00098    select_lex is a SELECT without union
00099    unit is container of either
00100      - One SELECT
00101      - UNION of selects
00102    select_lex and unit are both inherited form select_lex_node
00103    neighbors are two select_lex or units on the same level
00104 
00105    All select describing structures linked with following pointers:
00106    - list of neighbors (next/prev) (prev of first element point to slave
00107      pointer of upper structure)
00108      - For select this is a list of UNION's (or one element list)
00109      - For units this is a list of sub queries for the upper level select
00110 
00111    - pointer to master (master), which is
00112      If this is a unit
00113        - pointer to outer select_lex
00114      If this is a select_lex
00115        - pointer to outer unit structure for select
00116 
00117    - pointer to slave (slave), which is either:
00118      If this is a unit:
00119        - first SELECT that belong to this unit
00120      If this is a select_lex
00121        - first unit that belong to this SELECT (subquries or derived tables)
00122 
00123    - list of all select_lex (link_next/link_prev)
00124      This is to be used for things like derived tables creation, where we
00125      go through this list and create the derived tables.
00126 
00127    If unit contain several selects (UNION now, INTERSECT etc later)
00128    then it have special select_lex called fake_select_lex. It used for
00129    storing global parameters (like ORDER BY, LIMIT) and executing union.
00130    Subqueries used in global ORDER BY clause will be attached to this
00131    fake_select_lex, which will allow them correctly resolve fields of
00132    'upper' UNION and outer selects.
00133 
00134    For example for following query:
00135 
00136    select *
00137      from table1
00138      where table1.field IN (select * from table1_1_1 union
00139                             select * from table1_1_2)
00140      union
00141    select *
00142      from table2
00143      where table2.field=(select (select f1 from table2_1_1_1_1
00144                                    where table2_1_1_1_1.f2=table2_1_1.f3)
00145                            from table2_1_1
00146                            where table2_1_1.f1=table2.f2)
00147      union
00148    select * from table3;
00149 
00150    we will have following structure:
00151 
00152    select1: (select * from table1 ...)
00153    select2: (select * from table2 ...)
00154    select3: (select * from table3)
00155    select1.1.1: (select * from table1_1_1)
00156    ...
00157 
00158      main unit
00159      fake0
00160      select1 select2 select3
00161      |^^     |^
00162     s|||     ||master
00163     l|||     |+---------------------------------+
00164     a|||     +---------------------------------+|
00165     v|||master                         slave   ||
00166     e||+-------------------------+             ||
00167      V|            neighbor      |             V|
00168      unit1.1<+==================>unit1.2       unit2.1
00169      fake1.1
00170      select1.1.1 select 1.1.2    select1.2.1   select2.1.1
00171                                                |^
00172                                                ||
00173                                                V|
00174                                                unit2.1.1.1
00175                                                select2.1.1.1.1
00176 
00177 
00178    relation in main unit will be following:
00179    (bigger picture for:
00180       main unit
00181       fake0
00182       select1 select2 select3
00183    in the above picture)
00184 
00185          main unit
00186          |^^^^|fake_select_lex
00187          |||||+--------------------------------------------+
00188          ||||+--------------------------------------------+|
00189          |||+------------------------------+              ||
00190          ||+--------------+                |              ||
00191     slave||master         |                |              ||
00192          V|      neighbor |       neighbor |        master|V
00193          select1<========>select2<========>select3        fake0
00194 
00195     list of all select_lex will be following (as it will be constructed by
00196     parser):
00197 
00198     select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
00199                                                                           |
00200     +---------------------------------------------------------------------+
00201     |
00202     +->select1.1.1->select1.1.2
00203 
00204 */
00205 
00206 /*
00207     Base class for Select_Lex (Select_Lex) &
00208     Select_Lex_Unit (Select_Lex_Unit)
00209 */
00210 class Select_Lex_Node {
00211 protected:
00212   Select_Lex_Node *next, **prev,   /* neighbor list */
00213     *master, *slave,                  /* vertical links */
00214     *link_next, **link_prev;          /* list of whole Select_Lex */
00215 public:
00216 
00217   uint64_t options;
00218 
00219   /*
00220     result of this query can't be cached, bit field, can be :
00221       UNCACHEABLE_DEPENDENT
00222       UNCACHEABLE_RAND
00223       UNCACHEABLE_SIDEEFFECT
00224       UNCACHEABLE_EXPLAIN
00225       UNCACHEABLE_PREPARE
00226   */
00227   std::bitset<8> uncacheable;
00228   sub_select_type linkage;
00229   bool no_table_names_allowed; /* used for global order by */
00230   bool no_error; /* suppress error message (convert it to warnings) */
00231 
00232   static void *operator new(size_t size)
00233   {
00234     return memory::sql_alloc(size);
00235   }
00236   static void *operator new(size_t size, memory::Root *mem_root)
00237   { return mem_root->alloc(size); }
00238   static void operator delete(void*, size_t)
00239   {  }
00240   static void operator delete(void*, memory::Root*)
00241   {}
00242   Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
00243   virtual ~Select_Lex_Node() {}
00244   inline Select_Lex_Node* get_master() { return master; }
00245   virtual void init_query();
00246   virtual void init_select();
00247   void include_down(Select_Lex_Node *upper);
00248   void include_neighbour(Select_Lex_Node *before);
00249   void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
00250   void include_global(Select_Lex_Node **plink);
00251   void exclude();
00252 
00253   virtual Select_Lex_Unit* master_unit()= 0;
00254   virtual Select_Lex* outer_select()= 0;
00255   virtual Select_Lex* return_after_parsing()= 0;
00256 
00257   virtual bool set_braces(bool value);
00258   virtual bool inc_in_sum_expr();
00259   virtual uint32_t get_in_sum_expr();
00260   virtual TableList* get_table_list();
00261   virtual List<Item>* get_item_list();
00262   virtual TableList *add_table_to_list(Session *session, Table_ident *table,
00263                                        lex_string_t *alias,
00264                                        const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00265                                        thr_lock_type flags= TL_UNLOCK,
00266                                        List<Index_hint> *hints= 0,
00267                                        lex_string_t *option= 0);
00268   virtual void set_lock_for_tables(thr_lock_type)
00269   {}
00270 
00271   friend class Select_Lex_Unit;
00272   friend bool new_select(LEX *lex, bool move_down);
00273 private:
00274   void fast_exclude();
00275 };
00276 
00277 /*
00278    Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
00279    Select_Lexs
00280 */
00281 class Select_Lex_Unit: public Select_Lex_Node {
00282 protected:
00283   TableList result_table_list;
00284   select_union *union_result;
00285   Table *table; /* temporary table using for appending UNION results */
00286 
00287   select_result *result;
00288   uint64_t found_rows_for_union;
00289   bool saved_error;
00290 
00291 public:
00292   bool  prepared, // prepare phase already performed for UNION (unit)
00293     optimized, // optimize phase already performed for UNION (unit)
00294     executed, // already executed
00295     cleaned;
00296 
00297   // list of fields which points to temporary table for union
00298   List<Item> item_list;
00299   /*
00300     list of types of items inside union (used for union & derived tables)
00301 
00302     Item_type_holders from which this list consist may have pointers to Field,
00303     pointers is valid only after preparing SELECTS of this unit and before
00304     any SELECT of this unit execution
00305 
00306     TODO:
00307     Possibly this member should be protected, and its direct use replaced
00308     by get_unit_column_types(). Check the places where it is used.
00309   */
00310   List<Item> types;
00311   /*
00312     Pointer to 'last' select or pointer to unit where stored
00313     global parameters for union
00314   */
00315   Select_Lex *global_parameters;
00316   //node on wich we should return current_select pointer after parsing subquery
00317   Select_Lex *return_to;
00318   /* LIMIT clause runtime counters */
00319   ha_rows select_limit_cnt, offset_limit_cnt;
00320   /* not NULL if unit used in subselect, point to subselect item */
00321   Item_subselect *item;
00322   /* thread handler */
00323   Session *session;
00324   /*
00325     Select_Lex for hidden SELECT in onion which process global
00326     ORDER BY and LIMIT
00327   */
00328   Select_Lex *fake_select_lex;
00329 
00330   Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
00331   bool describe; /* union exec() called for EXPLAIN */
00332 
00333   void init_query();
00334   Select_Lex_Unit* master_unit();
00335   Select_Lex* outer_select();
00336   Select_Lex* first_select()
00337   {
00338     return reinterpret_cast<Select_Lex*>(slave);
00339   }
00340   Select_Lex_Unit* next_unit()
00341   {
00342     return reinterpret_cast<Select_Lex_Unit*>(next);
00343   }
00344   Select_Lex* return_after_parsing() { return return_to; }
00345   void exclude_level();
00346   void exclude_tree();
00347 
00348   /* UNION methods */
00349   bool prepare(Session *session, select_result *result,
00350                uint64_t additional_options);
00351   bool exec();
00352   bool cleanup();
00353   inline void unclean() { cleaned= 0; }
00354   void reinit_exec_mechanism();
00355 
00356   void print(String *str);
00357 
00358   bool add_fake_select_lex(Session *session);
00359   void init_prepare_fake_select_lex(Session *session);
00360   bool change_result(select_result_interceptor *result,
00361                      select_result_interceptor *old_result);
00362   void set_limit(Select_Lex *values);
00363   void set_session(Session *session_arg) { session= session_arg; }
00364   inline bool is_union();
00365 
00366   friend void lex_start(Session*);
00367 
00368   List<Item> *get_unit_column_types();
00369 };
00370 
00371 /*
00372   Select_Lex - store information of parsed SELECT statment
00373 */
00374 class Select_Lex : public Select_Lex_Node
00375 {
00376 public:
00377 
00378   Select_Lex() :
00379     context(),
00380     db(0),
00381     where(0),
00382     having(0),
00383     cond_value(),
00384     having_value(),
00385     parent_lex(0),
00386     olap(UNSPECIFIED_OLAP_TYPE),
00387     table_list(),
00388     group_list(),
00389     item_list(),
00390     interval_list(),
00391     is_item_list_lookup(false),
00392     join(0),
00393     top_join_list(),
00394     join_list(0),
00395     embedding(0),
00396     sj_nests(),
00397     leaf_tables(0),
00398     type(optimizer::ST_PRIMARY),
00399     order_list(),
00400     gorder_list(0),
00401     select_limit(0),
00402     offset_limit(0),
00403     ref_pointer_array(0),
00404     select_n_having_items(0),
00405     cond_count(0),
00406     between_count(0),
00407     max_equal_elems(0),
00408     select_n_where_fields(0),
00409     parsing_place(NO_MATTER),
00410     with_sum_func(0),
00411     in_sum_expr(0),
00412     select_number(0),
00413     nest_level(0),
00414     inner_sum_func_list(0),
00415     with_wild(0),
00416     braces(0),
00417     having_fix_field(0),
00418     inner_refs_list(),
00419     n_sum_items(0),
00420     n_child_sum_items(0),
00421     explicit_limit(0),
00422     is_cross(false),
00423     subquery_in_having(0),
00424     is_correlated(0),
00425     exclude_from_table_unique_test(0),
00426     non_agg_fields(),
00427     cur_pos_in_select_list(0),
00428     prev_join_using(0),
00429     full_group_by_flag(),
00430     current_index_hint_type(INDEX_HINT_IGNORE),
00431     current_index_hint_clause(),
00432     index_hints(0)
00433   {
00434   }
00435 
00436   Name_resolution_context context;
00437   const char *db;
00438   /* An Item representing the WHERE clause */
00439   Item *where;
00440   /* An Item representing the HAVING clause */
00441   Item *having;
00442   /* Saved values of the WHERE and HAVING clauses*/
00443   Item::cond_result cond_value;
00444   Item::cond_result having_value;
00445   /* point on lex in which it was created, used in view subquery detection */
00446   LEX *parent_lex;
00447   olap_type olap;
00448   /* FROM clause - points to the beginning of the TableList::next_local list. */
00449   SQL_LIST table_list;
00450   SQL_LIST group_list; /* GROUP BY clause. */
00451   List<Item> item_list;  /* list of fields & expressions */
00452   List<String> interval_list;
00453   bool is_item_list_lookup;
00454   Join *join; /* after Join::prepare it is pointer to corresponding JOIN */
00455   List<TableList> top_join_list; /* join list of the top level          */
00456   List<TableList> *join_list;    /* list for the currently parsed join  */
00457   TableList *embedding;          /* table embedding to the above list   */
00458   List<TableList> sj_nests;
00459   /*
00460     Beginning of the list of leaves in a FROM clause, where the leaves
00461     inlcude all base tables including view tables. The tables are connected
00462     by TableList::next_leaf, so leaf_tables points to the left-most leaf.
00463   */
00464   TableList *leaf_tables;
00465   drizzled::optimizer::select_type type; /* type of select for EXPLAIN */
00466 
00467   SQL_LIST order_list;                /* ORDER clause */
00468   SQL_LIST *gorder_list;
00469   Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
00470   /* Arrays of pointers to top elements of all_fields list */
00471   Item **ref_pointer_array;
00472 
00473   /*
00474     number of items in select_list and HAVING clause used to get number
00475     bigger then can be number of entries that will be added to all item
00476     list during split_sum_func
00477   */
00478   uint32_t select_n_having_items;
00479   uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
00480   uint32_t between_count; /* number of between predicates in where/having/on      */
00481   uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
00482   /*
00483     Number of fields used in select list or where clause of current select
00484     and all inner subselects.
00485   */
00486   uint32_t select_n_where_fields;
00487   enum_parsing_place parsing_place; /* where we are parsing expression */
00488   bool with_sum_func;   /* sum function indicator */
00489 
00490   uint32_t in_sum_expr;
00491   uint32_t select_number; /* number of select (used for EXPLAIN) */
00492   int8_t nest_level;     /* nesting level of select */
00493   Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
00494   uint32_t with_wild; /* item list contain '*' */
00495   bool braces;    /* SELECT ... UNION (SELECT ... ) <- this braces */
00496   /* true when having fix field called in processing of this SELECT */
00497   bool having_fix_field;
00498   /* List of references to fields referenced from inner selects */
00499   List<Item_outer_ref> inner_refs_list;
00500   /* Number of Item_sum-derived objects in this SELECT */
00501   uint32_t n_sum_items;
00502   /* Number of Item_sum-derived objects in children and descendant SELECTs */
00503   uint32_t n_child_sum_items;
00504 
00505   /* explicit LIMIT clause was used */
00506   bool explicit_limit;
00507 
00508   /* explicit CROSS JOIN was used */
00509   bool is_cross;
00510 
00511   /*
00512     there are subquery in HAVING clause => we can't close tables before
00513     query processing end even if we use temporary table
00514   */
00515   bool subquery_in_having;
00516   /* true <=> this SELECT is correlated w.r.t. some ancestor select */
00517   bool is_correlated;
00518   /* exclude this select from check of unique_table() */
00519   bool exclude_from_table_unique_test;
00520   /* List of fields that aren't under an aggregate function */
00521   List<Item_field> non_agg_fields;
00522   /* index in the select list of the expression currently being fixed */
00523   int cur_pos_in_select_list;
00524 
00525   /*
00526     This is a copy of the original JOIN USING list that comes from
00527     the parser. The parser :
00528       1. Sets the natural_join of the second TableList in the join
00529          and the Select_Lex::prev_join_using.
00530       2. Makes a parent TableList and sets its is_natural_join/
00531        join_using_fields members.
00532       3. Uses the wrapper TableList as a table in the upper level.
00533     We cannot assign directly to join_using_fields in the parser because
00534     at stage (1.) the parent TableList is not constructed yet and
00535     the assignment will override the JOIN USING fields of the lower level
00536     joins on the right.
00537   */
00538   List<String> *prev_join_using;
00539   /*
00540     Bitmap used in the ONLY_FULL_GROUP_BY_MODE to prevent mixture of aggregate
00541     functions and non aggregated fields when GROUP BY list is absent.
00542     Bits:
00543       0 - non aggregated fields are used in this select,
00544           defined as NON_AGG_FIELD_USED.
00545       1 - aggregate functions are used in this select,
00546           defined as SUM_FUNC_USED.
00547   */
00548   std::bitset<2> full_group_by_flag;
00549 
00550   void init_query();
00551   void init_select();
00552   Select_Lex_Unit* master_unit();
00553   Select_Lex_Unit* first_inner_unit()
00554   {
00555     return (Select_Lex_Unit*) slave;
00556   }
00557   Select_Lex* outer_select();
00558   Select_Lex* next_select()
00559   {
00560     return (Select_Lex*) next;
00561   }
00562   Select_Lex* next_select_in_list()
00563   {
00564     return (Select_Lex*) link_next;
00565   }
00566   Select_Lex_Node** next_select_in_list_addr()
00567   {
00568     return &link_next;
00569   }
00570   Select_Lex* return_after_parsing()
00571   {
00572     return master_unit()->return_after_parsing();
00573   }
00574 
00575   void mark_as_dependent(Select_Lex *last);
00576 
00577   bool set_braces(bool value);
00578   bool inc_in_sum_expr();
00579   uint32_t get_in_sum_expr();
00580 
00581   void add_item_to_list(Session *session, Item *item);
00582   void add_group_to_list(Session *session, Item *item, bool asc);
00583   void add_order_to_list(Session *session, Item *item, bool asc);
00584   TableList* add_table_to_list(Session *session,
00585                                Table_ident *table,
00586                                lex_string_t *alias,
00587                                const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00588                                thr_lock_type flags= TL_UNLOCK,
00589                                List<Index_hint> *hints= 0,
00590                                lex_string_t *option= 0);
00591   TableList* get_table_list();
00592   void init_nested_join(Session&);
00593   TableList *end_nested_join();
00594   TableList *nest_last_join(Session*);
00595   void add_joined_table(TableList *table);
00596   TableList *convert_right_join();
00597   List<Item>* get_item_list();
00598   void set_lock_for_tables(thr_lock_type lock_type);
00599   inline void init_order()
00600   {
00601     order_list.elements= 0;
00602     order_list.first= 0;
00603     order_list.next= (unsigned char**) &order_list.first;
00604   }
00605   /*
00606     This method created for reiniting LEX in admin_table() and can be
00607     used only if you are going remove all Select_Lex & units except belonger
00608     to LEX (LEX::unit & LEX::select, for other purposes there are
00609     Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
00610   */
00611   void cut_subtree()
00612   {
00613     slave= 0;
00614   }
00615   bool test_limit();
00616 
00617   friend void lex_start(Session*);
00618   void make_empty_select()
00619   {
00620     init_query();
00621     init_select();
00622   }
00623   void setup_ref_array(Session *session, uint32_t order_group_num);
00624   void print(Session *session, String *str);
00625   static void print_order(String *str, Order *order);
00626 
00627   void print_limit(Session *session, String *str);
00628   void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
00629   /*
00630     Destroy the used execution plan (JOIN) of this subtree (this
00631     Select_Lex and all nested Select_Lexes and Select_Lex_Units).
00632   */
00633   bool cleanup();
00634   /*
00635     Recursively cleanup the join of this select lex and of all nested
00636     select lexes.
00637   */
00638   void cleanup_all_joins(bool full);
00639 
00640   void set_index_hint_type(index_hint_type type, index_clause_map clause);
00641 
00642   /*
00643    Add a index hint to the tagged list of hints. The type and clause of the
00644    hint will be the current ones (set by set_index_hint())
00645   */
00646   void add_index_hint(Session*, const char*);
00647 
00648   /* make a list to hold index hints */
00649   void alloc_index_hints (Session *session);
00650   /* read and clear the index hints */
00651   List<Index_hint>* pop_index_hints()
00652   {
00653     List<Index_hint> *hints= index_hints;
00654     index_hints= NULL;
00655     return hints;
00656   }
00657 
00658   void clear_index_hints() { index_hints= NULL; }
00659 
00660 private:
00661   /* current index hint kind. used in filling up index_hints */
00662   index_hint_type current_index_hint_type;
00663   index_clause_map current_index_hint_clause;
00664   /* a list of USE/FORCE/IGNORE INDEX */
00665   List<Index_hint> *index_hints;
00666 };
00667 
00668 inline bool Select_Lex_Unit::is_union()
00669 {
00670   return first_select()->next_select() && first_select()->next_select()->linkage == UNION_TYPE;
00671 }
00672 
00673 enum xa_option_words
00674 {
00675   XA_NONE,
00676   XA_JOIN,
00677   XA_RESUME,
00678   XA_ONE_PHASE,
00679   XA_SUSPEND,
00680   XA_FOR_MIGRATE
00681 };
00682 
00683 /*
00684   Class representing list of all tables used by statement.
00685   It also contains information about stored functions used by statement
00686   since during its execution we may have to add all tables used by its
00687   stored functions/triggers to this list in order to pre-open and lock
00688   them.
00689 
00690   Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
00691   methods to save and restore this information.
00692 */
00693 class Query_tables_list
00694 {
00695 public:
00696   /* Global list of all tables used by this statement */
00697   TableList *query_tables;
00698   /* Pointer to next_global member of last element in the previous list. */
00699   TableList **query_tables_last;
00700   /*
00701     If non-0 then indicates that query requires prelocking and points to
00702     next_global member of last own element in query table list (i.e. last
00703     table which was not added to it as part of preparation to prelocking).
00704     0 - indicates that this query does not need prelocking.
00705   */
00706   TableList **query_tables_own_last;
00707 
00708   /* Initializes (or resets) Query_tables_list object for "real" use. */
00709   void reset_query_tables_list(bool init);
00710 
00711   /*
00712     Direct addition to the list of query tables.
00713     If you are using this function, you must ensure that the table
00714     object, in particular table->db member, is initialized.
00715   */
00716   void add_to_query_tables(TableList *table)
00717   {
00718     *(table->prev_global= query_tables_last)= table;
00719     query_tables_last= &table->next_global;
00720   }
00721   /* Return pointer to first not-own table in query-tables or 0 */
00722   TableList* first_not_own_table()
00723   {
00724     return ( query_tables_own_last ? *query_tables_own_last : 0);
00725   }
00726   void chop_off_not_own_tables()
00727   {
00728     if (query_tables_own_last)
00729     {
00730       *query_tables_own_last= 0;
00731       query_tables_last= query_tables_own_last;
00732       query_tables_own_last= 0;
00733     }
00734   }
00735 };
00736 
00740 enum enum_comment_state
00741 {
00745   NO_COMMENT,
00750   PRESERVE_COMMENT,
00757   DISCARD_COMMENT
00758 };
00759 
00760 } /* namespace drizzled */
00761 
00762 namespace drizzled {
00763 
00764 /* The state of the lex parsing. This is saved in the Session struct */
00765 class LEX : public Query_tables_list
00766 {
00767 public:
00768   Select_Lex_Unit unit;                         /* most upper unit */
00769   Select_Lex select_lex;                        /* first Select_Lex */
00770   /* current Select_Lex in parsing */
00771   Select_Lex *current_select;
00772   /* list of all Select_Lex */
00773   Select_Lex *all_selects_list;
00774 
00775   /* This is the "scale" for DECIMAL (S,P) notation */
00776   const char *length;
00777   /* This is the decimal precision in DECIMAL(S,P) notation */
00778   const char *dec;
00779 
00786   lex_string_t name;
00787   /* The string literal used in a LIKE expression */
00788   String *wild;
00789   file_exchange *exchange;
00790   select_result *result;
00791 
00798   lex_string_t ident;
00799 
00800   unsigned char* yacc_yyss, *yacc_yyvs;
00801   /* The owning Session of this LEX */
00802   Session *session;
00803   const charset_info_st *charset;
00804   bool text_string_is_7bit;
00805   /* store original leaf_tables for INSERT SELECT and PS/SP */
00806   TableList *leaf_tables_insert;
00807 
00808   List<Key_part_spec> col_list;
00809   List<Key_part_spec> ref_list;
00810   List<String>        interval_list;
00811   List<Lex_Column>    columns;
00812   List<Item>        *insert_list,field_list,value_list,update_list;
00813   List<List_item>     many_values;
00814   SetVarVector  var_list;
00815   /*
00816     A stack of name resolution contexts for the query. This stack is used
00817     at parse time to set local name resolution contexts for various parts
00818     of a query. For example, in a JOIN ... ON (some_condition) clause the
00819     Items in 'some_condition' must be resolved only against the operands
00820     of the the join, and not against the whole clause. Similarly, Items in
00821     subqueries should be resolved against the subqueries (and outer queries).
00822     The stack is used in the following way: when the parser detects that
00823     all Items in some clause need a local context, it creates a new context
00824     and pushes it on the stack. All newly created Items always store the
00825     top-most context in the stack. Once the parser leaves the clause that
00826     required a local context, the parser pops the top-most context.
00827   */
00828   List<Name_resolution_context> context_stack;
00829 
00830   SQL_LIST auxiliary_table_list;
00831   SQL_LIST save_list;
00832   CreateField *last_field;
00833   Item_sum *in_sum_func;
00834   plugin::Function *udf;
00835   uint32_t type;
00836   /*
00837     This variable is used in post-parse stage to declare that sum-functions,
00838     or functions which have sense only if GROUP BY is present, are allowed.
00839     For example in a query
00840     SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
00841     MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
00842     in the HAVING clause. Due to possible nesting of select construct
00843     the variable can contain 0 or 1 for each nest level.
00844   */
00845   nesting_map allow_sum_func;
00846   enum_sql_command sql_command;
00847   statement::Statement *statement;
00848   /*
00849     Usually `expr` rule of yacc is quite reused but some commands better
00850     not support subqueries which comes standard with this rule, like
00851     KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
00852     syntax error back.
00853   */
00854   bool expr_allows_subselect;
00855 
00856   thr_lock_type lock_option;
00857   enum enum_duplicates duplicates;
00858   union {
00859     enum ha_rkey_function ha_rkey_mode;
00860     enum xa_option_words xa_opt;
00861   };
00862   sql_var_t option_type;
00863 
00864   int nest_level;
00865   uint8_t describe;
00866   /*
00867     A flag that indicates what kinds of derived tables are present in the
00868     query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
00869   */
00870   uint8_t derived_tables;
00871 
00872   /* Was the IGNORE symbol found in statement */
00873   bool ignore;
00874 
00881   bool use_only_table_context;
00882 
00883   /* Was the ESCAPE keyword used? */
00884   bool escape_used;
00885   bool is_lex_started; /* If lex_start() did run. For debugging. */
00886 
00887   LEX();
00888 
00889   /* Note that init and de-init mostly happen in lex_start and lex_end
00890      and not here. This is because LEX isn't delete/new for each new
00891      statement in a session. It's re-used by doing lex_end, lex_start
00892      in sql_lex.cc
00893   */
00894   virtual ~LEX();
00895 
00896   TableList *unlink_first_table(bool *link_to_local);
00897   void link_first_table_back(TableList *first, bool link_to_local);
00898   void first_lists_tables_same();
00899 
00900   void cleanup_after_one_table_open();
00901 
00902   void push_context(Name_resolution_context *context)
00903   {
00904     context_stack.push_front(context);
00905   }
00906 
00907   void pop_context()
00908   {
00909     context_stack.pop();
00910   }
00911 
00912   Name_resolution_context *current_context()
00913   {
00914     return &context_stack.front();
00915   }
00916 
00924   bool is_single_level_stmt()
00925   {
00926     /*
00927       This check exploits the fact that the last added to all_select_list is
00928       on its top. So select_lex (as the first added) will be at the tail
00929       of the list.
00930     */
00931     if (&select_lex == all_selects_list)
00932     {
00933       assert(!all_selects_list->next_select_in_list());
00934       return true;
00935     }
00936     return false;
00937   }
00938   bool is_cross; // CROSS keyword was used
00939   bool isCacheable()
00940   {
00941     return cacheable;
00942   }
00943   void setCacheable(bool val)
00944   {
00945     cacheable= val;
00946   }
00947 
00948   void reset()
00949   {
00950     sum_expr_used= false;
00951     _exists= false;
00952   }
00953 
00954   void setSumExprUsed()
00955   {
00956     sum_expr_used= true;
00957   }
00958 
00959   bool isSumExprUsed()
00960   {
00961     return sum_expr_used;
00962   }
00963 
00964   void start(Session *session);
00965   void end();
00966 
00967   message::Table *table()
00968   {
00969     if (not _create_table)
00970       _create_table= new message::Table;
00971 
00972     return _create_table;
00973   }
00974 
00975   message::AlterTable *alter_table();
00976 
00977   message::Table::Field *field()
00978   {
00979     return _create_field;
00980   }
00981 
00982   void setField(message::Table::Field *arg)
00983   {
00984     _create_field= arg;
00985   }
00986 
00987   void setExists()
00988   {
00989     _exists= true;
00990   }
00991 
00992   bool exists() const
00993   {
00994     return _exists;
00995   }
00996 
00997 private:
00998   bool cacheable;
00999   bool sum_expr_used;
01000   message::Table *_create_table;
01001   message::AlterTable *_alter_table;
01002   message::Table::Field *_create_field;
01003   bool _exists;
01004 };
01005 
01006 extern void lex_start(Session *session);
01007 
01012 } /* namespace drizzled */
01013 
01014 #endif /* DRIZZLE_SERVER */