00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 namespace drizzled {
00037
00038 namespace plugin { class Function; }
00039 namespace statement { class Statement; }
00040
00041 class st_lex_symbol;
00042 class select_result_interceptor;
00043
00044
00045
00046
00047 class Table_ident;
00048 class file_exchange;
00049 class Lex_Column;
00050 class Item_outer_ref;
00051
00052 }
00053
00054
00055
00056
00057
00058
00059 #ifdef DRIZZLE_SERVER
00060
00061 # include <drizzled/sys_var.h>
00062 # include <drizzled/item/func.h>
00063 # ifdef DRIZZLE_YACC
00064 # define LEX_YYSTYPE void *
00065 # else
00066 # if defined(DRIZZLE_LEX)
00067 # include <drizzled/foreign_key.h>
00068 # include <drizzled/lex_symbol.h>
00069 # include <drizzled/comp_creator.h>
00070 # include <drizzled/sql_yacc.h>
00071 # define LEX_YYSTYPE YYSTYPE *
00072 # else
00073 # define LEX_YYSTYPE void *
00074 # endif
00075 # endif
00076 #endif
00077
00078
00079 #define DESCRIBE_NORMAL 1
00080 #define DESCRIBE_EXTENDED 2
00081
00082 #ifdef DRIZZLE_SERVER
00083
00084 #define DERIVED_NONE 0
00085 #define DERIVED_SUBQUERY 1
00086
00087 namespace drizzled
00088 {
00089
00090 typedef List<Item> List_item;
00091
00092 enum sub_select_type
00093 {
00094 UNSPECIFIED_TYPE,
00095 UNION_TYPE,
00096 INTERSECT_TYPE,
00097 EXCEPT_TYPE,
00098 GLOBAL_OPTIONS_TYPE,
00099 DERIVED_TABLE_TYPE,
00100 OLAP_TYPE
00101 };
00102
00103 enum olap_type
00104 {
00105 UNSPECIFIED_OLAP_TYPE,
00106 CUBE_TYPE,
00107 ROLLUP_TYPE
00108 };
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 class LEX;
00229 class Select_Lex;
00230 class Select_Lex_Unit;
00231 class Select_Lex_Node {
00232 protected:
00233 Select_Lex_Node *next, **prev,
00234 *master, *slave,
00235 *link_next, **link_prev;
00236 public:
00237
00238 uint64_t options;
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 std::bitset<8> uncacheable;
00249 sub_select_type linkage;
00250 bool no_table_names_allowed;
00251 bool no_error;
00252
00253 static void *operator new(size_t size)
00254 {
00255 return memory::sql_alloc(size);
00256 }
00257 static void *operator new(size_t size, memory::Root *mem_root)
00258 { return (void*) mem_root->alloc_root((uint32_t) size); }
00259 static void operator delete(void *, size_t)
00260 { }
00261 static void operator delete(void *, memory::Root *)
00262 {}
00263 Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
00264 virtual ~Select_Lex_Node() {}
00265 inline Select_Lex_Node* get_master() { return master; }
00266 virtual void init_query();
00267 virtual void init_select();
00268 void include_down(Select_Lex_Node *upper);
00269 void include_neighbour(Select_Lex_Node *before);
00270 void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
00271 void include_global(Select_Lex_Node **plink);
00272 void exclude();
00273
00274 virtual Select_Lex_Unit* master_unit()= 0;
00275 virtual Select_Lex* outer_select()= 0;
00276 virtual Select_Lex* return_after_parsing()= 0;
00277
00278 virtual bool set_braces(bool value);
00279 virtual bool inc_in_sum_expr();
00280 virtual uint32_t get_in_sum_expr();
00281 virtual TableList* get_table_list();
00282 virtual List<Item>* get_item_list();
00283 virtual TableList *add_table_to_list(Session *session, Table_ident *table,
00284 LEX_STRING *alias,
00285 const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00286 thr_lock_type flags= TL_UNLOCK,
00287 List<Index_hint> *hints= 0,
00288 LEX_STRING *option= 0);
00289 virtual void set_lock_for_tables(thr_lock_type)
00290 {}
00291
00292 friend class Select_Lex_Unit;
00293 friend bool new_select(LEX *lex, bool move_down);
00294 private:
00295 void fast_exclude();
00296 };
00297
00298
00299
00300
00301
00302 class Session;
00303 class select_result;
00304 class Join;
00305 class select_union;
00306 class Select_Lex_Unit: public Select_Lex_Node {
00307 protected:
00308 TableList result_table_list;
00309 select_union *union_result;
00310 Table *table;
00311
00312 select_result *result;
00313 uint64_t found_rows_for_union;
00314 bool saved_error;
00315
00316 public:
00317 bool prepared,
00318 optimized,
00319 executed,
00320 cleaned;
00321
00322
00323 List<Item> item_list;
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 List<Item> types;
00336
00337
00338
00339
00340 Select_Lex *global_parameters;
00341
00342 Select_Lex *return_to;
00343
00344 ha_rows select_limit_cnt, offset_limit_cnt;
00345
00346 Item_subselect *item;
00347
00348 Session *session;
00349
00350
00351
00352
00353 Select_Lex *fake_select_lex;
00354
00355 Select_Lex *union_distinct;
00356 bool describe;
00357
00358 void init_query();
00359 Select_Lex_Unit* master_unit();
00360 Select_Lex* outer_select();
00361 Select_Lex* first_select()
00362 {
00363 return reinterpret_cast<Select_Lex*>(slave);
00364 }
00365 Select_Lex_Unit* next_unit()
00366 {
00367 return reinterpret_cast<Select_Lex_Unit*>(next);
00368 }
00369 Select_Lex* return_after_parsing() { return return_to; }
00370 void exclude_level();
00371 void exclude_tree();
00372
00373
00374 bool prepare(Session *session, select_result *result,
00375 uint64_t additional_options);
00376 bool exec();
00377 bool cleanup();
00378 inline void unclean() { cleaned= 0; }
00379 void reinit_exec_mechanism();
00380
00381 void print(String *str);
00382
00383 bool add_fake_select_lex(Session *session);
00384 void init_prepare_fake_select_lex(Session *session);
00385 bool change_result(select_result_interceptor *result,
00386 select_result_interceptor *old_result);
00387 void set_limit(Select_Lex *values);
00388 void set_session(Session *session_arg) { session= session_arg; }
00389 inline bool is_union ();
00390
00391 friend void lex_start(Session *session);
00392
00393 List<Item> *get_unit_column_types();
00394 };
00395
00396
00397
00398
00399 class Select_Lex: public Select_Lex_Node
00400 {
00401 public:
00402
00403 Select_Lex() :
00404 context(),
00405 db(0),
00406 where(0),
00407 having(0),
00408 cond_value(),
00409 having_value(),
00410 parent_lex(0),
00411 olap(UNSPECIFIED_OLAP_TYPE),
00412 table_list(),
00413 group_list(),
00414 item_list(),
00415 interval_list(),
00416 is_item_list_lookup(false),
00417 join(0),
00418 top_join_list(),
00419 join_list(0),
00420 embedding(0),
00421 sj_nests(),
00422 leaf_tables(0),
00423 type(optimizer::ST_PRIMARY),
00424 order_list(),
00425 gorder_list(0),
00426 select_limit(0),
00427 offset_limit(0),
00428 ref_pointer_array(0),
00429 select_n_having_items(0),
00430 cond_count(0),
00431 between_count(0),
00432 max_equal_elems(0),
00433 select_n_where_fields(0),
00434 parsing_place(NO_MATTER),
00435 with_sum_func(0),
00436 in_sum_expr(0),
00437 select_number(0),
00438 nest_level(0),
00439 inner_sum_func_list(0),
00440 with_wild(0),
00441 braces(0),
00442 having_fix_field(0),
00443 inner_refs_list(),
00444 n_sum_items(0),
00445 n_child_sum_items(0),
00446 explicit_limit(0),
00447 is_cross(false),
00448 subquery_in_having(0),
00449 is_correlated(0),
00450 exclude_from_table_unique_test(0),
00451 non_agg_fields(),
00452 cur_pos_in_select_list(0),
00453 prev_join_using(0),
00454 full_group_by_flag(),
00455 current_index_hint_type(INDEX_HINT_IGNORE),
00456 current_index_hint_clause(),
00457 index_hints(0)
00458 {
00459 }
00460
00461 Name_resolution_context context;
00462 char *db;
00463
00464 Item *where;
00465
00466 Item *having;
00467
00468 Item::cond_result cond_value;
00469 Item::cond_result having_value;
00470
00471 LEX *parent_lex;
00472 olap_type olap;
00473
00474 SQL_LIST table_list;
00475 SQL_LIST group_list;
00476 List<Item> item_list;
00477 List<String> interval_list;
00478 bool is_item_list_lookup;
00479 Join *join;
00480 List<TableList> top_join_list;
00481 List<TableList> *join_list;
00482 TableList *embedding;
00483 List<TableList> sj_nests;
00484
00485
00486
00487
00488
00489 TableList *leaf_tables;
00490 drizzled::optimizer::select_type type;
00491
00492 SQL_LIST order_list;
00493 SQL_LIST *gorder_list;
00494 Item *select_limit, *offset_limit;
00495
00496 Item **ref_pointer_array;
00497
00498
00499
00500
00501
00502
00503 uint32_t select_n_having_items;
00504 uint32_t cond_count;
00505 uint32_t between_count;
00506 uint32_t max_equal_elems;
00507
00508
00509
00510
00511 uint32_t select_n_where_fields;
00512 enum_parsing_place parsing_place;
00513 bool with_sum_func;
00514
00515 uint32_t in_sum_expr;
00516 uint32_t select_number;
00517 int8_t nest_level;
00518 Item_sum *inner_sum_func_list;
00519 uint32_t with_wild;
00520 bool braces;
00521
00522 bool having_fix_field;
00523
00524 List<Item_outer_ref> inner_refs_list;
00525
00526 uint32_t n_sum_items;
00527
00528 uint32_t n_child_sum_items;
00529
00530
00531 bool explicit_limit;
00532
00533
00534 bool is_cross;
00535
00536
00537
00538
00539
00540 bool subquery_in_having;
00541
00542 bool is_correlated;
00543
00544 bool exclude_from_table_unique_test;
00545
00546 List<Item_field> non_agg_fields;
00547
00548 int cur_pos_in_select_list;
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 List<String> *prev_join_using;
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 std::bitset<2> full_group_by_flag;
00574
00575 void init_query();
00576 void init_select();
00577 Select_Lex_Unit* master_unit();
00578 Select_Lex_Unit* first_inner_unit()
00579 {
00580 return (Select_Lex_Unit*) slave;
00581 }
00582 Select_Lex* outer_select();
00583 Select_Lex* next_select()
00584 {
00585 return (Select_Lex*) next;
00586 }
00587 Select_Lex* next_select_in_list()
00588 {
00589 return (Select_Lex*) link_next;
00590 }
00591 Select_Lex_Node** next_select_in_list_addr()
00592 {
00593 return &link_next;
00594 }
00595 Select_Lex* return_after_parsing()
00596 {
00597 return master_unit()->return_after_parsing();
00598 }
00599
00600 void mark_as_dependent(Select_Lex *last);
00601
00602 bool set_braces(bool value);
00603 bool inc_in_sum_expr();
00604 uint32_t get_in_sum_expr();
00605
00606 bool add_item_to_list(Session *session, Item *item);
00607 bool add_group_to_list(Session *session, Item *item, bool asc);
00608 bool add_order_to_list(Session *session, Item *item, bool asc);
00609 TableList* add_table_to_list(Session *session,
00610 Table_ident *table,
00611 LEX_STRING *alias,
00612 const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00613 thr_lock_type flags= TL_UNLOCK,
00614 List<Index_hint> *hints= 0,
00615 LEX_STRING *option= 0);
00616 TableList* get_table_list();
00617 bool init_nested_join(Session *session);
00618 TableList *end_nested_join(Session *session);
00619 TableList *nest_last_join(Session *session);
00620 void add_joined_table(TableList *table);
00621 TableList *convert_right_join();
00622 List<Item>* get_item_list();
00623 void set_lock_for_tables(thr_lock_type lock_type);
00624 inline void init_order()
00625 {
00626 order_list.elements= 0;
00627 order_list.first= 0;
00628 order_list.next= (unsigned char**) &order_list.first;
00629 }
00630
00631
00632
00633
00634
00635
00636 void cut_subtree()
00637 {
00638 slave= 0;
00639 }
00640 bool test_limit();
00641
00642 friend void lex_start(Session *session);
00643 void make_empty_select()
00644 {
00645 init_query();
00646 init_select();
00647 }
00648 bool setup_ref_array(Session *session, uint32_t order_group_num);
00649 void print(Session *session, String *str);
00650 static void print_order(String *str, Order *order);
00651
00652 void print_limit(Session *session, String *str);
00653 void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
00654
00655
00656
00657
00658 bool cleanup();
00659
00660
00661
00662
00663 void cleanup_all_joins(bool full);
00664
00665 void set_index_hint_type(index_hint_type type, index_clause_map clause);
00666
00667
00668
00669
00670
00671 bool add_index_hint (Session *session, char *str, uint32_t length);
00672
00673
00674 void alloc_index_hints (Session *session);
00675
00676 List<Index_hint>* pop_index_hints(void)
00677 {
00678 List<Index_hint> *hints= index_hints;
00679 index_hints= NULL;
00680 return hints;
00681 }
00682
00683 void clear_index_hints(void) { index_hints= NULL; }
00684
00685 private:
00686
00687 index_hint_type current_index_hint_type;
00688 index_clause_map current_index_hint_clause;
00689
00690 List<Index_hint> *index_hints;
00691 };
00692
00693 inline bool Select_Lex_Unit::is_union ()
00694 {
00695 return first_select()->next_select() &&
00696 first_select()->next_select()->linkage == UNION_TYPE;
00697 }
00698
00699 enum xa_option_words
00700 {
00701 XA_NONE
00702 , XA_JOIN
00703 , XA_RESUME
00704 , XA_ONE_PHASE
00705 , XA_SUSPEND
00706 , XA_FOR_MIGRATE
00707 };
00708
00709 extern const LEX_STRING null_lex_str;
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721 class Query_tables_list
00722 {
00723 public:
00724
00725 TableList *query_tables;
00726
00727 TableList **query_tables_last;
00728
00729
00730
00731
00732
00733
00734 TableList **query_tables_own_last;
00735
00736
00737
00738
00739
00740 Query_tables_list() {}
00741 virtual ~Query_tables_list() {}
00742
00743
00744 void reset_query_tables_list(bool init);
00745
00746
00747
00748
00749
00750
00751 void add_to_query_tables(TableList *table)
00752 {
00753 *(table->prev_global= query_tables_last)= table;
00754 query_tables_last= &table->next_global;
00755 }
00756
00757 TableList* first_not_own_table()
00758 {
00759 return ( query_tables_own_last ? *query_tables_own_last : 0);
00760 }
00761 void chop_off_not_own_tables()
00762 {
00763 if (query_tables_own_last)
00764 {
00765 *query_tables_own_last= 0;
00766 query_tables_last= query_tables_own_last;
00767 query_tables_own_last= 0;
00768 }
00769 }
00770 };
00771
00775 enum enum_comment_state
00776 {
00780 NO_COMMENT,
00785 PRESERVE_COMMENT,
00792 DISCARD_COMMENT
00793 };
00794
00795 }
00796
00797 #include <drizzled/lex_input_stream.h>
00798
00799 namespace drizzled
00800 {
00801
00802
00803 class LEX : public Query_tables_list
00804 {
00805 public:
00806 Select_Lex_Unit unit;
00807 Select_Lex select_lex;
00808
00809 Select_Lex *current_select;
00810
00811 Select_Lex *all_selects_list;
00812
00813
00814 char *length;
00815
00816 char *dec;
00817
00824 LEX_STRING name;
00825
00826 String *wild;
00827 file_exchange *exchange;
00828 select_result *result;
00829
00836 LEX_STRING ident;
00837
00838 unsigned char* yacc_yyss, *yacc_yyvs;
00839
00840 Session *session;
00841 const CHARSET_INFO *charset;
00842 bool text_string_is_7bit;
00843
00844 TableList *leaf_tables_insert;
00845
00846 List<Key_part_spec> col_list;
00847 List<Key_part_spec> ref_list;
00848 List<String> interval_list;
00849 List<Lex_Column> columns;
00850 List<Item> *insert_list,field_list,value_list,update_list;
00851 List<List_item> many_values;
00852 SetVarVector var_list;
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866 List<Name_resolution_context> context_stack;
00867
00868 SQL_LIST auxiliary_table_list;
00869 SQL_LIST save_list;
00870 CreateField *last_field;
00871 Item_sum *in_sum_func;
00872 plugin::Function *udf;
00873 uint32_t type;
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883 nesting_map allow_sum_func;
00884 enum_sql_command sql_command;
00885 statement::Statement *statement;
00886
00887
00888
00889
00890
00891
00892 bool expr_allows_subselect;
00893
00894 thr_lock_type lock_option;
00895 enum enum_duplicates duplicates;
00896 union {
00897 enum ha_rkey_function ha_rkey_mode;
00898 enum xa_option_words xa_opt;
00899 };
00900 sql_var_t option_type;
00901
00902 int nest_level;
00903 uint8_t describe;
00904
00905
00906
00907
00908 uint8_t derived_tables;
00909
00910
00911 bool ignore;
00912
00919 bool use_only_table_context;
00920
00921
00922 bool escape_used;
00923 bool is_lex_started;
00924
00925 LEX();
00926
00927
00928
00929
00930
00931
00932 virtual ~LEX();
00933
00934 TableList *unlink_first_table(bool *link_to_local);
00935 void link_first_table_back(TableList *first, bool link_to_local);
00936 void first_lists_tables_same();
00937
00938 void cleanup_after_one_table_open();
00939
00940 bool push_context(Name_resolution_context *context)
00941 {
00942 return context_stack.push_front(context);
00943 }
00944
00945 void pop_context()
00946 {
00947 context_stack.pop();
00948 }
00949
00950 bool copy_db_to(char **p_db, size_t *p_db_length) const;
00951
00952 Name_resolution_context *current_context()
00953 {
00954 return &context_stack.front();
00955 }
00956
00964 bool is_single_level_stmt()
00965 {
00966
00967
00968
00969
00970
00971 if (&select_lex == all_selects_list)
00972 {
00973 assert(!all_selects_list->next_select_in_list());
00974 return true;
00975 }
00976 return false;
00977 }
00978 bool is_cross;
00979 bool isCacheable()
00980 {
00981 return cacheable;
00982 }
00983 void setCacheable(bool val)
00984 {
00985 cacheable= val;
00986 }
00987
00988 void reset()
00989 {
00990 sum_expr_used= false;
00991 _exists= false;
00992 }
00993
00994 void setSumExprUsed()
00995 {
00996 sum_expr_used= true;
00997 }
00998
00999 bool isSumExprUsed()
01000 {
01001 return sum_expr_used;
01002 }
01003
01004 void start(Session *session);
01005 void end();
01006
01007 message::Table *table()
01008 {
01009 if (not _create_table)
01010 _create_table= new message::Table;
01011
01012 return _create_table;
01013 }
01014
01015 message::Table::Field *field()
01016 {
01017 return _create_field;
01018 }
01019
01020 void setField(message::Table::Field *arg)
01021 {
01022 _create_field= arg;
01023 }
01024
01025 void setExists()
01026 {
01027 _exists= true;
01028 }
01029
01030 bool exists() const
01031 {
01032 return _exists;
01033 }
01034
01035 private:
01036 bool cacheable;
01037 bool sum_expr_used;
01038 message::Table *_create_table;
01039 message::Table::Field *_create_field;
01040 bool _exists;
01041 };
01042
01043 extern void lex_start(Session *session);
01044 extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
01045 extern bool is_lex_native_function(const LEX_STRING *name);
01046
01047 bool check_for_sql_keyword(drizzled::st_lex_symbol const&);
01048 bool check_for_sql_keyword(drizzled::lex_string_t const&);
01049
01054 }
01055
01056 #endif