~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

  • Committer: Mark Atwood
  • Date: 2011-10-27 05:08:12 UTC
  • mfrom: (2445.1.11 rf)
  • Revision ID: me@mark.atwood.name-20111027050812-1icvs72lb0u4xdc4
mergeĀ lp:~olafvdspek/drizzle/refactor8

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLED_SQL_LEX_H
21
 
#define DRIZZLED_SQL_LEX_H
 
20
#pragma once
22
21
 
23
22
/**
24
23
  @defgroup Semantic_Analysis Semantic Analysis
25
24
*/
26
25
#include <drizzled/message/table.pb.h>
27
 
 
28
 
#include "drizzled/plugin/function.h"
29
 
#include "drizzled/name_resolution_context.h"
30
 
#include "drizzled/item/subselect.h"
31
 
#include "drizzled/table_list.h"
32
 
#include "drizzled/function/math/real.h"
33
 
#include "drizzled/alter_drop.h"
34
 
#include "drizzled/alter_column.h"
35
 
#include "drizzled/alter_info.h"
36
 
#include "drizzled/key_part_spec.h"
37
 
#include "drizzled/index_hint.h"
38
 
#include "drizzled/statement.h"
39
 
#include "drizzled/optimizer/explain_plan.h"
 
26
#include <drizzled/name_resolution_context.h>
 
27
#include <drizzled/table_list.h>
 
28
#include <drizzled/function/math/real.h>
 
29
#include <drizzled/key_part_spec.h>
 
30
#include <drizzled/index_hint.h>
 
31
#include <drizzled/optimizer/explain_plan.h>
40
32
 
41
33
#include <bitset>
42
34
#include <string>
43
35
 
44
 
namespace drizzled
45
 
{
46
 
 
47
 
class select_result_interceptor;
48
 
 
49
 
/* YACC and LEX Definitions */
50
 
 
51
 
/* These may not be declared yet */
52
 
class Table_ident;
53
 
class file_exchange;
54
 
class Lex_Column;
55
 
class Item_outer_ref;
56
 
 
57
 
} /* namespace drizzled */
58
 
 
59
36
/*
60
37
  The following hack is needed because mysql_yacc.cc does not define
61
38
  YYSTYPE before including this file
71
48
#  if defined(DRIZZLE_LEX)
72
49
#   include <drizzled/foreign_key.h>
73
50
#   include <drizzled/lex_symbol.h>
 
51
#   include <drizzled/comp_creator.h>
74
52
#   include <drizzled/sql_yacc.h>
75
53
#   define LEX_YYSTYPE YYSTYPE *
76
54
#  else
229
207
    Base class for Select_Lex (Select_Lex) &
230
208
    Select_Lex_Unit (Select_Lex_Unit)
231
209
*/
232
 
class LEX;
233
 
class Select_Lex;
234
 
class Select_Lex_Unit;
235
210
class Select_Lex_Node {
236
211
protected:
237
212
  Select_Lex_Node *next, **prev,   /* neighbor list */
250
225
      UNCACHEABLE_PREPARE
251
226
  */
252
227
  std::bitset<8> uncacheable;
253
 
  enum sub_select_type linkage;
 
228
  sub_select_type linkage;
254
229
  bool no_table_names_allowed; /* used for global order by */
255
230
  bool no_error; /* suppress error message (convert it to warnings) */
256
231
 
259
234
    return memory::sql_alloc(size);
260
235
  }
261
236
  static void *operator new(size_t size, memory::Root *mem_root)
262
 
  { return (void*) mem_root->alloc_root((uint32_t) size); }
263
 
  static void operator delete(void *, size_t)
 
237
  { return mem_root->alloc(size); }
 
238
  static void operator delete(void*, size_t)
264
239
  {  }
265
 
  static void operator delete(void *, memory::Root *)
 
240
  static void operator delete(void*, memory::Root*)
266
241
  {}
267
242
  Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
268
243
  virtual ~Select_Lex_Node() {}
285
260
  virtual TableList* get_table_list();
286
261
  virtual List<Item>* get_item_list();
287
262
  virtual TableList *add_table_to_list(Session *session, Table_ident *table,
288
 
                                       LEX_STRING *alias,
 
263
                                       lex_string_t *alias,
289
264
                                       const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
290
265
                                       thr_lock_type flags= TL_UNLOCK,
291
266
                                       List<Index_hint> *hints= 0,
292
 
                                       LEX_STRING *option= 0);
 
267
                                       lex_string_t *option= 0);
293
268
  virtual void set_lock_for_tables(thr_lock_type)
294
269
  {}
295
270
 
296
271
  friend class Select_Lex_Unit;
297
 
  friend bool mysql_new_select(LEX *lex, bool move_down);
 
272
  friend bool new_select(LEX *lex, bool move_down);
298
273
private:
299
274
  void fast_exclude();
300
275
};
303
278
   Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
304
279
   Select_Lexs
305
280
*/
306
 
class Session;
307
 
class select_result;
308
 
class Join;
309
 
class select_union;
310
281
class Select_Lex_Unit: public Select_Lex_Node {
311
282
protected:
312
283
  TableList result_table_list;
382
353
  inline void unclean() { cleaned= 0; }
383
354
  void reinit_exec_mechanism();
384
355
 
385
 
  void print(String *str, enum_query_type query_type);
 
356
  void print(String *str);
386
357
 
387
358
  bool add_fake_select_lex(Session *session);
388
359
  void init_prepare_fake_select_lex(Session *session);
390
361
                     select_result_interceptor *old_result);
391
362
  void set_limit(Select_Lex *values);
392
363
  void set_session(Session *session_arg) { session= session_arg; }
393
 
  inline bool is_union ();
 
364
  inline bool is_union();
394
365
 
395
 
  friend void lex_start(Session *session);
396
 
  friend int subselect_union_engine::exec();
 
366
  friend void lex_start(Session*);
397
367
 
398
368
  List<Item> *get_unit_column_types();
399
369
};
401
371
/*
402
372
  Select_Lex - store information of parsed SELECT statment
403
373
*/
404
 
class Select_Lex: public Select_Lex_Node
 
374
class Select_Lex : public Select_Lex_Node
405
375
{
406
376
public:
407
377
 
449
419
    n_sum_items(0),
450
420
    n_child_sum_items(0),
451
421
    explicit_limit(0),
 
422
    is_cross(false),
452
423
    subquery_in_having(0),
453
424
    is_correlated(0),
454
425
    exclude_from_table_unique_test(0),
463
434
  }
464
435
 
465
436
  Name_resolution_context context;
466
 
  char *db;
 
437
  const char *db;
467
438
  /* An Item representing the WHERE clause */
468
439
  Item *where;
469
440
  /* An Item representing the HAVING clause */
473
444
  Item::cond_result having_value;
474
445
  /* point on lex in which it was created, used in view subquery detection */
475
446
  LEX *parent_lex;
476
 
  enum olap_type olap;
 
447
  olap_type olap;
477
448
  /* FROM clause - points to the beginning of the TableList::next_local list. */
478
449
  SQL_LIST table_list;
479
450
  SQL_LIST group_list; /* GROUP BY clause. */
491
462
    by TableList::next_leaf, so leaf_tables points to the left-most leaf.
492
463
  */
493
464
  TableList *leaf_tables;
494
 
  enum drizzled::optimizer::select_type type; /* type of select for EXPLAIN */
 
465
  drizzled::optimizer::select_type type; /* type of select for EXPLAIN */
495
466
 
496
467
  SQL_LIST order_list;                /* ORDER clause */
497
468
  SQL_LIST *gorder_list;
533
504
 
534
505
  /* explicit LIMIT clause was used */
535
506
  bool explicit_limit;
 
507
 
 
508
  /* explicit CROSS JOIN was used */
 
509
  bool is_cross;
 
510
 
536
511
  /*
537
512
    there are subquery in HAVING clause => we can't close tables before
538
513
    query processing end even if we use temporary table
603
578
  bool inc_in_sum_expr();
604
579
  uint32_t get_in_sum_expr();
605
580
 
606
 
  bool add_item_to_list(Session *session, Item *item);
607
 
  bool add_group_to_list(Session *session, Item *item, bool asc);
608
 
  bool add_order_to_list(Session *session, Item *item, bool asc);
 
581
  void add_item_to_list(Session *session, Item *item);
 
582
  void add_group_to_list(Session *session, Item *item, bool asc);
 
583
  void add_order_to_list(Session *session, Item *item, bool asc);
609
584
  TableList* add_table_to_list(Session *session,
610
585
                               Table_ident *table,
611
 
                               LEX_STRING *alias,
 
586
                               lex_string_t *alias,
612
587
                               const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
613
588
                               thr_lock_type flags= TL_UNLOCK,
614
589
                               List<Index_hint> *hints= 0,
615
 
                               LEX_STRING *option= 0);
 
590
                               lex_string_t *option= 0);
616
591
  TableList* get_table_list();
617
 
  bool init_nested_join(Session *session);
 
592
  void init_nested_join(Session&);
618
593
  TableList *end_nested_join(Session *session);
619
594
  TableList *nest_last_join(Session *session);
620
595
  void add_joined_table(TableList *table);
628
603
    order_list.next= (unsigned char**) &order_list.first;
629
604
  }
630
605
  /*
631
 
    This method created for reiniting LEX in mysql_admin_table() and can be
 
606
    This method created for reiniting LEX in admin_table() and can be
632
607
    used only if you are going remove all Select_Lex & units except belonger
633
608
    to LEX (LEX::unit & LEX::select, for other purposes there are
634
609
    Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
639
614
  }
640
615
  bool test_limit();
641
616
 
642
 
  friend void lex_start(Session *session);
 
617
  friend void lex_start(Session*);
643
618
  void make_empty_select()
644
619
  {
645
620
    init_query();
646
621
    init_select();
647
622
  }
648
 
  bool setup_ref_array(Session *session, uint32_t order_group_num);
649
 
  void print(Session *session, String *str, enum_query_type query_type);
650
 
  static void print_order(String *str,
651
 
                          Order *order,
652
 
                          enum_query_type query_type);
653
 
  void print_limit(Session *session, String *str, enum_query_type query_type);
 
623
  void setup_ref_array(Session *session, uint32_t order_group_num);
 
624
  void print(Session *session, String *str);
 
625
  static void print_order(String *str, Order *order);
 
626
 
 
627
  void print_limit(Session *session, String *str);
654
628
  void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
655
629
  /*
656
630
    Destroy the used execution plan (JOIN) of this subtree (this
663
637
  */
664
638
  void cleanup_all_joins(bool full);
665
639
 
666
 
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
 
640
  void set_index_hint_type(index_hint_type type, index_clause_map clause);
667
641
 
668
642
  /*
669
643
   Add a index hint to the tagged list of hints. The type and clause of the
670
644
   hint will be the current ones (set by set_index_hint())
671
645
  */
672
 
  bool add_index_hint (Session *session, char *str, uint32_t length);
 
646
  void add_index_hint(Session*, const char*);
673
647
 
674
648
  /* make a list to hold index hints */
675
649
  void alloc_index_hints (Session *session);
676
650
  /* read and clear the index hints */
677
 
  List<Index_hint>* pop_index_hints(void)
 
651
  List<Index_hint>* pop_index_hints()
678
652
  {
679
653
    List<Index_hint> *hints= index_hints;
680
654
    index_hints= NULL;
681
655
    return hints;
682
656
  }
683
657
 
684
 
  void clear_index_hints(void) { index_hints= NULL; }
 
658
  void clear_index_hints() { index_hints= NULL; }
685
659
 
686
660
private:
687
661
  /* current index hint kind. used in filling up index_hints */
688
 
  enum index_hint_type current_index_hint_type;
 
662
  index_hint_type current_index_hint_type;
689
663
  index_clause_map current_index_hint_clause;
690
664
  /* a list of USE/FORCE/IGNORE INDEX */
691
665
  List<Index_hint> *index_hints;
692
666
};
693
667
 
694
 
inline bool Select_Lex_Unit::is_union ()
 
668
inline bool Select_Lex_Unit::is_union()
695
669
{
696
 
  return first_select()->next_select() &&
697
 
    first_select()->next_select()->linkage == UNION_TYPE;
 
670
  return first_select()->next_select() && first_select()->next_select()->linkage == UNION_TYPE;
698
671
}
699
672
 
700
673
enum xa_option_words
701
674
{
702
 
  XA_NONE
703
 
, XA_JOIN
704
 
, XA_RESUME
705
 
, XA_ONE_PHASE
706
 
, XA_SUSPEND
707
 
, XA_FOR_MIGRATE
 
675
  XA_NONE,
 
676
  XA_JOIN,
 
677
  XA_RESUME,
 
678
  XA_ONE_PHASE,
 
679
  XA_SUSPEND,
 
680
  XA_FOR_MIGRATE
708
681
};
709
682
 
710
 
extern const LEX_STRING null_lex_str;
711
 
 
712
683
/*
713
684
  Class representing list of all tables used by statement.
714
685
  It also contains information about stored functions used by statement
734
705
  */
735
706
  TableList **query_tables_own_last;
736
707
 
737
 
  /*
738
 
    These constructor and destructor serve for creation/destruction
739
 
    of Query_tables_list instances which are used as backup storage.
740
 
  */
741
 
  Query_tables_list() {}
742
 
  virtual ~Query_tables_list() {}
743
 
 
744
708
  /* Initializes (or resets) Query_tables_list object for "real" use. */
745
709
  void reset_query_tables_list(bool init);
746
710
 
795
759
 
796
760
} /* namespace drizzled */
797
761
 
798
 
#include "drizzled/lex_input_stream.h"
799
 
 
800
 
namespace drizzled
801
 
{
 
762
namespace drizzled {
802
763
 
803
764
/* The state of the lex parsing. This is saved in the Session struct */
804
765
class LEX : public Query_tables_list
811
772
  /* list of all Select_Lex */
812
773
  Select_Lex *all_selects_list;
813
774
 
814
 
  /* This is the "scale" for DECIMAL (S,P) notation */ 
815
 
  char *length;
 
775
  /* This is the "scale" for DECIMAL (S,P) notation */
 
776
  const char *length;
816
777
  /* This is the decimal precision in DECIMAL(S,P) notation */
817
 
  char *dec;
818
 
  
 
778
  const char *dec;
 
779
 
819
780
  /**
820
 
   * This is used kind of like the "ident" member variable below, as 
 
781
   * This is used kind of like the "ident" member variable below, as
821
782
   * a place to store certain names of identifiers.  Unfortunately, it
822
783
   * is used differently depending on the Command (SELECT on a derived
823
784
   * table vs CREATE)
824
785
   */
825
 
  LEX_STRING name;
 
786
  lex_string_t name;
826
787
  /* The string literal used in a LIKE expression */
827
788
  String *wild;
828
789
  file_exchange *exchange;
833
794
   * or a named savepoint.  It should probably be refactored out into
834
795
   * the eventual Command class built for the Keycache and Savepoint
835
796
   * commands.
836
 
   */ 
837
 
  LEX_STRING ident;
 
797
   */
 
798
  lex_string_t ident;
838
799
 
839
800
  unsigned char* yacc_yyss, *yacc_yyvs;
840
801
  /* The owning Session of this LEX */
841
802
  Session *session;
842
 
  const CHARSET_INFO *charset;
 
803
  const charset_info_st *charset;
843
804
  bool text_string_is_7bit;
844
805
  /* store original leaf_tables for INSERT SELECT and PS/SP */
845
806
  TableList *leaf_tables_insert;
850
811
  List<Lex_Column>    columns;
851
812
  List<Item>          *insert_list,field_list,value_list,update_list;
852
813
  List<List_item>     many_values;
853
 
  List<set_var_base>  var_list;
 
814
  SetVarVector  var_list;
854
815
  /*
855
816
    A stack of name resolution contexts for the query. This stack is used
856
817
    at parse time to set local name resolution contexts for various parts
930
891
     statement in a session. It's re-used by doing lex_end, lex_start
931
892
     in sql_lex.cc
932
893
  */
933
 
  virtual ~LEX()
934
 
  {
935
 
  }
 
894
  virtual ~LEX();
936
895
 
937
896
  TableList *unlink_first_table(bool *link_to_local);
938
897
  void link_first_table_back(TableList *first, bool link_to_local);
940
899
 
941
900
  void cleanup_after_one_table_open();
942
901
 
943
 
  bool push_context(Name_resolution_context *context)
 
902
  void push_context(Name_resolution_context *context)
944
903
  {
945
 
    return context_stack.push_front(context);
 
904
    context_stack.push_front(context);
946
905
  }
947
906
 
948
907
  void pop_context()
950
909
    context_stack.pop();
951
910
  }
952
911
 
953
 
  bool copy_db_to(char **p_db, size_t *p_db_length) const;
954
 
 
955
912
  Name_resolution_context *current_context()
956
913
  {
957
 
    return context_stack.head();
 
914
    return &context_stack.front();
958
915
  }
959
 
  /*
960
 
    Restore the LEX and Session in case of a parse error.
961
 
  */
962
 
  static void cleanup_lex_after_parse_error(Session *session);
963
916
 
964
917
  /**
965
918
    @brief check if the statement is a single-level join
995
948
  void reset()
996
949
  {
997
950
    sum_expr_used= false;
 
951
    _exists= false;
998
952
  }
999
953
 
1000
954
  void setSumExprUsed()
1010
964
  void start(Session *session);
1011
965
  void end();
1012
966
 
1013
 
private: 
 
967
  message::Table *table()
 
968
  {
 
969
    if (not _create_table)
 
970
      _create_table= new message::Table;
 
971
 
 
972
    return _create_table;
 
973
  }
 
974
 
 
975
  message::AlterTable *alter_table();
 
976
 
 
977
  message::Table::Field *field()
 
978
  {
 
979
    return _create_field;
 
980
  }
 
981
 
 
982
  void setField(message::Table::Field *arg)
 
983
  {
 
984
    _create_field= arg;
 
985
  }
 
986
 
 
987
  void setExists()
 
988
  {
 
989
    _exists= true;
 
990
  }
 
991
 
 
992
  bool exists() const
 
993
  {
 
994
    return _exists;
 
995
  }
 
996
 
 
997
private:
1014
998
  bool cacheable;
1015
999
  bool sum_expr_used;
 
1000
  message::Table *_create_table;
 
1001
  message::AlterTable *_alter_table;
 
1002
  message::Table::Field *_create_field;
 
1003
  bool _exists;
1016
1004
};
1017
1005
 
1018
1006
extern void lex_start(Session *session);
1019
 
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
1020
 
extern bool is_lex_native_function(const LEX_STRING *name);
1021
1007
 
1022
1008
/**
1023
1009
  @} (End of group Semantic_Analysis)
1026
1012
} /* namespace drizzled */
1027
1013
 
1028
1014
#endif /* DRIZZLE_SERVER */
1029
 
#endif /* DRIZZLED_SQL_LEX_H */