~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

  • Committer: Brian Aker
  • Date: 2009-02-12 22:45:08 UTC
  • Revision ID: brian@tangent.org-20090212224508-mrd9jwgn1zjdpqdk
Minor refactoring (we will need to disconnect the code from the include
file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
  @defgroup Semantic_Analysis Semantic Analysis
25
25
*/
26
26
 
27
 
#include "sql_udf.h"
 
27
#include <drizzled/sql_udf.h>
 
28
#include <drizzled/name_resolution_context.h>
 
29
#include <drizzled/item/subselect.h>
 
30
#include <drizzled/item/param.h>
 
31
#include <drizzled/item/outer_ref.h>
 
32
#include <drizzled/table_list.h>
 
33
#include <drizzled/function/math/real.h>
 
34
#include <drizzled/alter_drop.h>
 
35
#include <drizzled/alter_column.h>
 
36
#include <drizzled/key.h>
 
37
#include <drizzled/foreign_key.h>
 
38
#include <drizzled/item/param.h>
 
39
 
 
40
class select_result_interceptor;
 
41
class virtual_column_info;
28
42
 
29
43
/* YACC and LEX Definitions */
30
44
 
31
45
/* These may not be declared yet */
32
46
class Table_ident;
33
 
class sql_exchange;
34
 
class LEX_COLUMN;
 
47
class file_exchange;
 
48
class Lex_Column;
 
49
class Item_outer_ref;
35
50
 
36
 
#ifdef DRIZZLE_SERVER
37
51
/*
38
52
  The following hack is needed because mysql_yacc.cc does not define
39
53
  YYSTYPE before including this file
40
54
*/
41
55
 
42
 
#include "set_var.h"
43
 
 
44
 
#ifdef DRIZZLE_YACC
45
 
#define LEX_YYSTYPE void *
46
 
#else
47
 
#if defined(DRIZZLE_LEX)
48
 
#include "lex_symbol.h"
49
 
#include "sql_yacc.h"
50
 
#define LEX_YYSTYPE YYSTYPE *
51
 
#else
52
 
#define LEX_YYSTYPE void *
53
 
#endif
54
 
#endif
55
 
#endif
56
 
 
57
 
/*
58
 
  When a command is added here, be sure it's also added in mysqld.cc
59
 
  in "struct show_var_st status_vars[]= {" ...
60
 
 
61
 
  If the command returns a result set or is not allowed in stored
62
 
  functions or triggers, please also make sure that
63
 
  sp_get_flags_for_command (sp_head.cc) returns proper flags for the
64
 
  added SQLCOM_.
65
 
*/
66
 
 
67
 
enum enum_sql_command {
68
 
  SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
69
 
  SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
70
 
  SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
71
 
  SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
72
 
  SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
73
 
  SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
74
 
  SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
75
 
  SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
76
 
  SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
77
 
  SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
78
 
  SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
79
 
  SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
80
 
  SQLCOM_OPTIMIZE, SQLCOM_CHECK,
81
 
  SQLCOM_ASSIGN_TO_KEYCACHE,
82
 
  SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
83
 
  SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
84
 
  SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
85
 
  SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
86
 
  SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
87
 
  SQLCOM_RENAME_TABLE,  
88
 
  SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
89
 
  SQLCOM_SHOW_OPEN_TABLES,
90
 
  SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
91
 
  SQLCOM_SHOW_BINLOG_EVENTS,
92
 
  SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
93
 
  SQLCOM_CHECKSUM,
94
 
  SQLCOM_BINLOG_BASE64_EVENT,
95
 
  SQLCOM_SHOW_PLUGINS,
96
 
  /*
97
 
    When a command is added here, be sure it's also added in mysqld.cc
98
 
    in "struct show_var_st status_vars[]= {" ...
99
 
  */
100
 
  /* This should be the last !!! */
101
 
  SQLCOM_END
102
 
};
 
56
#ifdef DRIZZLE_SERVER
 
57
# include <drizzled/set_var.h>
 
58
# include <drizzled/item/func.h>
 
59
# ifdef DRIZZLE_YACC
 
60
#  define LEX_YYSTYPE void *
 
61
# else
 
62
#  if defined(DRIZZLE_LEX)
 
63
#   include <drizzled/lex_symbol.h>
 
64
#   include <drizzled/sql_yacc.h>
 
65
#   define LEX_YYSTYPE YYSTYPE *
 
66
#  else
 
67
#   define LEX_YYSTYPE void *
 
68
#  endif /* defined(DRIZZLE_LEX) */
 
69
# endif /* DRIZZLE_YACC */
 
70
#endif /* DRIZZLE_SERVER */
103
71
 
104
72
// describe/explain types
105
73
#define DESCRIBE_NORMAL         1
120
88
  char *server_name, *host, *db, *username, *password, *scheme, *owner;
121
89
} LEX_SERVER_OPTIONS;
122
90
 
123
 
typedef struct st_lex_master_info
124
 
{
125
 
  char *host, *user, *password, *log_file_name;
126
 
  uint32_t port, connect_retry;
127
 
  float heartbeat_period;
128
 
  uint64_t pos;
129
 
  uint32_t server_id;
130
 
  /*
131
 
    Enum is used for making it possible to detect if the user
132
 
    changed variable or if it should be left at old value
133
 
   */
134
 
  enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE}
135
 
    ssl, ssl_verify_server_cert, heartbeat_opt;
136
 
  char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
137
 
  char *relay_log_name;
138
 
  uint32_t relay_log_pos;
139
 
} LEX_MASTER_INFO;
140
 
 
141
91
 
142
92
enum sub_select_type
143
93
{
145
95
  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
146
96
};
147
97
 
148
 
enum olap_type 
 
98
enum olap_type
149
99
{
150
100
  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
151
101
};
155
105
  NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
156
106
};
157
107
 
158
 
/* 
 
108
/*
159
109
  String names used to print a statement with index hints.
160
110
  Keep in sync with index_hint_type.
161
111
*/
181
131
  enum index_hint_type type;
182
132
  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
183
133
  index_clause_map clause;
184
 
  /* 
185
 
    The index name. Empty (str=NULL) name represents an empty list 
186
 
    USE INDEX () clause 
187
 
  */ 
 
134
  /*
 
135
    The index name. Empty (str=NULL) name represents an empty list
 
136
    USE INDEX () clause
 
137
  */
188
138
  LEX_STRING key_name;
189
139
 
190
140
  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
195
145
    key_name.length= length;
196
146
  }
197
147
 
198
 
  void print(THD *thd, String *str);
199
 
}; 
200
 
 
201
 
/* 
202
 
  The state of the lex parsing for selects 
203
 
   
 
148
  void print(Session *session, String *str);
 
149
};
 
150
 
 
151
/*
 
152
  The state of the lex parsing for selects
 
153
 
204
154
   master and slaves are pointers to select_lex.
205
155
   master is pointer to upper level node.
206
156
   slave is pointer to lower level node
312
262
 
313
263
*/
314
264
 
315
 
/* 
316
 
    Base class for st_select_lex (SELECT_LEX) & 
317
 
    st_select_lex_unit (SELECT_LEX_UNIT)
 
265
/*
 
266
    Base class for Select_Lex (Select_Lex) &
 
267
    Select_Lex_Unit (Select_Lex_Unit)
318
268
*/
319
 
struct st_lex;
320
 
class st_select_lex;
321
 
class st_select_lex_unit;
322
 
class st_select_lex_node {
 
269
class LEX;
 
270
class Select_Lex;
 
271
class Select_Lex_Unit;
 
272
class Select_Lex_Node {
323
273
protected:
324
 
  st_select_lex_node *next, **prev,   /* neighbor list */
 
274
  Select_Lex_Node *next, **prev,   /* neighbor list */
325
275
    *master, *slave,                  /* vertical links */
326
 
    *link_next, **link_prev;          /* list of whole SELECT_LEX */
 
276
    *link_next, **link_prev;          /* list of whole Select_Lex */
327
277
public:
328
278
 
329
279
  uint64_t options;
347
297
  }
348
298
  static void *operator new(size_t size, MEM_ROOT *mem_root)
349
299
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
350
 
  static void operator delete(void *ptr __attribute__((unused)),
351
 
                              size_t size __attribute__((unused)))
 
300
  static void operator delete(void *, size_t)
352
301
  { TRASH(ptr, size); }
353
 
  static void operator delete(void *ptr __attribute__((unused)),
354
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
302
  static void operator delete(void *, MEM_ROOT *)
355
303
  {}
356
 
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
357
 
  virtual ~st_select_lex_node() {}
358
 
  inline st_select_lex_node* get_master() { return master; }
 
304
  Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
 
305
  virtual ~Select_Lex_Node() {}
 
306
  inline Select_Lex_Node* get_master() { return master; }
359
307
  virtual void init_query();
360
308
  virtual void init_select();
361
 
  void include_down(st_select_lex_node *upper);
362
 
  void include_neighbour(st_select_lex_node *before);
363
 
  void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
364
 
  void include_global(st_select_lex_node **plink);
 
309
  void include_down(Select_Lex_Node *upper);
 
310
  void include_neighbour(Select_Lex_Node *before);
 
311
  void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
 
312
  void include_global(Select_Lex_Node **plink);
365
313
  void exclude();
366
314
 
367
 
  virtual st_select_lex_unit* master_unit()= 0;
368
 
  virtual st_select_lex* outer_select()= 0;
369
 
  virtual st_select_lex* return_after_parsing()= 0;
 
315
  virtual Select_Lex_Unit* master_unit()= 0;
 
316
  virtual Select_Lex* outer_select()= 0;
 
317
  virtual Select_Lex* return_after_parsing()= 0;
370
318
 
371
319
  virtual bool set_braces(bool value);
372
320
  virtual bool inc_in_sum_expr();
374
322
  virtual TableList* get_table_list();
375
323
  virtual List<Item>* get_item_list();
376
324
  virtual uint32_t get_table_join_options();
377
 
  virtual TableList *add_table_to_list(THD *thd, Table_ident *table,
 
325
  virtual TableList *add_table_to_list(Session *session, Table_ident *table,
378
326
                                        LEX_STRING *alias,
379
327
                                        uint32_t table_options,
380
328
                                        thr_lock_type flags= TL_UNLOCK,
381
329
                                        List<Index_hint> *hints= 0,
382
330
                                        LEX_STRING *option= 0);
383
 
  virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
 
331
  virtual void set_lock_for_tables(thr_lock_type)
384
332
  {}
385
333
 
386
 
  friend class st_select_lex_unit;
387
 
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
 
334
  friend class Select_Lex_Unit;
 
335
  friend bool mysql_new_select(LEX *lex, bool move_down);
388
336
private:
389
337
  void fast_exclude();
390
338
};
391
 
typedef class st_select_lex_node SELECT_LEX_NODE;
392
339
 
393
 
/* 
394
 
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
395
 
   SELECT_LEXs
 
340
/*
 
341
   Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
 
342
   Select_Lexs
396
343
*/
397
 
class THD;
 
344
class Session;
398
345
class select_result;
399
346
class JOIN;
400
347
class select_union;
401
 
class st_select_lex_unit: public st_select_lex_node {
 
348
class Select_Lex_Unit: public Select_Lex_Node {
402
349
protected:
403
350
  TableList result_table_list;
404
351
  select_union *union_result;
418
365
  List<Item> item_list;
419
366
  /*
420
367
    list of types of items inside union (used for union & derived tables)
421
 
    
 
368
 
422
369
    Item_type_holders from which this list consist may have pointers to Field,
423
370
    pointers is valid only after preparing SELECTS of this unit and before
424
371
    any SELECT of this unit execution
432
379
    Pointer to 'last' select or pointer to unit where stored
433
380
    global parameters for union
434
381
  */
435
 
  st_select_lex *global_parameters;
 
382
  Select_Lex *global_parameters;
436
383
  //node on wich we should return current_select pointer after parsing subquery
437
 
  st_select_lex *return_to;
 
384
  Select_Lex *return_to;
438
385
  /* LIMIT clause runtime counters */
439
386
  ha_rows select_limit_cnt, offset_limit_cnt;
440
387
  /* not NULL if unit used in subselect, point to subselect item */
441
388
  Item_subselect *item;
442
389
  /* thread handler */
443
 
  THD *thd;
 
390
  Session *session;
444
391
  /*
445
 
    SELECT_LEX for hidden SELECT in onion which process global
 
392
    Select_Lex for hidden SELECT in onion which process global
446
393
    ORDER BY and LIMIT
447
394
  */
448
 
  st_select_lex *fake_select_lex;
 
395
  Select_Lex *fake_select_lex;
449
396
 
450
 
  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
 
397
  Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
451
398
  bool describe; /* union exec() called for EXPLAIN */
452
399
 
453
400
  void init_query();
454
 
  st_select_lex_unit* master_unit();
455
 
  st_select_lex* outer_select();
456
 
  st_select_lex* first_select()
457
 
  {
458
 
    return reinterpret_cast<st_select_lex*>(slave);
459
 
  }
460
 
  st_select_lex_unit* next_unit()
461
 
  {
462
 
    return reinterpret_cast<st_select_lex_unit*>(next);
463
 
  }
464
 
  st_select_lex* return_after_parsing() { return return_to; }
 
401
  Select_Lex_Unit* master_unit();
 
402
  Select_Lex* outer_select();
 
403
  Select_Lex* first_select()
 
404
  {
 
405
    return reinterpret_cast<Select_Lex*>(slave);
 
406
  }
 
407
  Select_Lex_Unit* next_unit()
 
408
  {
 
409
    return reinterpret_cast<Select_Lex_Unit*>(next);
 
410
  }
 
411
  Select_Lex* return_after_parsing() { return return_to; }
465
412
  void exclude_level();
466
413
  void exclude_tree();
467
414
 
468
415
  /* UNION methods */
469
 
  bool prepare(THD *thd, select_result *result, uint32_t additional_options);
 
416
  bool prepare(Session *session, select_result *result, uint32_t additional_options);
470
417
  bool exec();
471
418
  bool cleanup();
472
419
  inline void unclean() { cleaned= 0; }
474
421
 
475
422
  void print(String *str, enum_query_type query_type);
476
423
 
477
 
  bool add_fake_select_lex(THD *thd);
478
 
  void init_prepare_fake_select_lex(THD *thd);
 
424
  bool add_fake_select_lex(Session *session);
 
425
  void init_prepare_fake_select_lex(Session *session);
479
426
  bool change_result(select_result_interceptor *result,
480
427
                     select_result_interceptor *old_result);
481
 
  void set_limit(st_select_lex *values);
482
 
  void set_thd(THD *thd_arg) { thd= thd_arg; }
483
 
  inline bool is_union (); 
 
428
  void set_limit(Select_Lex *values);
 
429
  void set_session(Session *session_arg) { session= session_arg; }
 
430
  inline bool is_union ();
484
431
 
485
 
  friend void lex_start(THD *thd);
 
432
  friend void lex_start(Session *session);
486
433
  friend int subselect_union_engine::exec();
487
434
 
488
435
  List<Item> *get_unit_column_types();
489
436
};
490
437
 
491
 
typedef class st_select_lex_unit SELECT_LEX_UNIT;
492
 
 
493
438
/*
494
 
  SELECT_LEX - store information of parsed SELECT statment
 
439
  Select_Lex - store information of parsed SELECT statment
495
440
*/
496
 
class st_select_lex: public st_select_lex_node
 
441
class Select_Lex: public Select_Lex_Node
497
442
{
498
443
public:
499
444
  Name_resolution_context context;
502
447
  /* Saved values of the WHERE and HAVING clauses*/
503
448
  Item::cond_result cond_value, having_value;
504
449
  /* point on lex in which it was created, used in view subquery detection */
505
 
  st_lex *parent_lex;
 
450
  LEX *parent_lex;
506
451
  enum olap_type olap;
507
452
  /* FROM clause - points to the beginning of the TableList::next_local list. */
508
453
  SQL_LIST            table_list;
510
455
  List<Item>          item_list;  /* list of fields & expressions */
511
456
  List<String>        interval_list;
512
457
  bool                is_item_list_lookup;
513
 
  /* 
514
 
    Despite their names, the following are used in unions. This should 
 
458
  /*
 
459
    Despite their names, the following are used in unions. This should
515
460
    be rewritten. -Brian
516
461
  */
517
462
  List<Item_real_func> *ftfunc_list;
543
488
  uint32_t select_n_having_items;
544
489
  uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
545
490
  uint32_t between_count; /* number of between predicates in where/having/on      */
546
 
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */   
 
491
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
547
492
  /*
548
493
    Number of fields used in select list or where clause of current select
549
494
    and all inner subselects.
551
496
  uint32_t select_n_where_fields;
552
497
  enum_parsing_place parsing_place; /* where we are parsing expression */
553
498
  bool with_sum_func;   /* sum function indicator */
554
 
  /* 
555
 
    PS or SP cond natural joins was alredy processed with permanent
556
 
    arena and all additional items which we need alredy stored in it
557
 
  */
558
 
  bool conds_processed_with_permanent_arena;
559
499
 
560
500
  uint32_t table_join_options;
561
501
  uint32_t in_sum_expr;
562
502
  uint32_t select_number; /* number of select (used for EXPLAIN) */
563
503
  int nest_level;     /* nesting level of select */
564
 
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */ 
 
504
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
565
505
  uint32_t with_wild; /* item list contain '*' */
566
506
  bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
567
507
  /* true when having fix field called in processing of this SELECT */
590
530
  int cur_pos_in_select_list;
591
531
 
592
532
  List<udf_func>     udf_list;                  /* udf function calls stack */
593
 
  /* 
 
533
  /*
594
534
    This is a copy of the original JOIN USING list that comes from
595
535
    the parser. The parser :
596
536
      1. Sets the natural_join of the second TableList in the join
597
 
         and the st_select_lex::prev_join_using.
 
537
         and the Select_Lex::prev_join_using.
598
538
      2. Makes a parent TableList and sets its is_natural_join/
599
539
       join_using_fields members.
600
540
      3. Uses the wrapper TableList as a table in the upper level.
616
556
  uint8_t full_group_by_flag;
617
557
  void init_query();
618
558
  void init_select();
619
 
  st_select_lex_unit* master_unit();
620
 
  st_select_lex_unit* first_inner_unit() 
621
 
  { 
622
 
    return (st_select_lex_unit*) slave; 
623
 
  }
624
 
  st_select_lex* outer_select();
625
 
  st_select_lex* next_select() { return (st_select_lex*) next; }
626
 
  st_select_lex* next_select_in_list() 
627
 
  {
628
 
    return (st_select_lex*) link_next;
629
 
  }
630
 
  st_select_lex_node** next_select_in_list_addr()
 
559
  Select_Lex_Unit* master_unit();
 
560
  Select_Lex_Unit* first_inner_unit()
 
561
  {
 
562
    return (Select_Lex_Unit*) slave;
 
563
  }
 
564
  Select_Lex* outer_select();
 
565
  Select_Lex* next_select() { return (Select_Lex*) next; }
 
566
  Select_Lex* next_select_in_list()
 
567
  {
 
568
    return (Select_Lex*) link_next;
 
569
  }
 
570
  Select_Lex_Node** next_select_in_list_addr()
631
571
  {
632
572
    return &link_next;
633
573
  }
634
 
  st_select_lex* return_after_parsing()
 
574
  Select_Lex* return_after_parsing()
635
575
  {
636
576
    return master_unit()->return_after_parsing();
637
577
  }
638
578
 
639
 
  void mark_as_dependent(st_select_lex *last);
 
579
  void mark_as_dependent(Select_Lex *last);
640
580
 
641
581
  bool set_braces(bool value);
642
582
  bool inc_in_sum_expr();
643
583
  uint32_t get_in_sum_expr();
644
584
 
645
 
  bool add_item_to_list(THD *thd, Item *item);
646
 
  bool add_group_to_list(THD *thd, Item *item, bool asc);
647
 
  bool add_order_to_list(THD *thd, Item *item, bool asc);
648
 
  TableList* add_table_to_list(THD *thd, Table_ident *table,
 
585
  bool add_item_to_list(Session *session, Item *item);
 
586
  bool add_group_to_list(Session *session, Item *item, bool asc);
 
587
  bool add_order_to_list(Session *session, Item *item, bool asc);
 
588
  TableList* add_table_to_list(Session *session, Table_ident *table,
649
589
                                LEX_STRING *alias,
650
590
                                uint32_t table_options,
651
591
                                thr_lock_type flags= TL_UNLOCK,
652
592
                                List<Index_hint> *hints= 0,
653
593
                                LEX_STRING *option= 0);
654
594
  TableList* get_table_list();
655
 
  bool init_nested_join(THD *thd);
656
 
  TableList *end_nested_join(THD *thd);
657
 
  TableList *nest_last_join(THD *thd);
 
595
  bool init_nested_join(Session *session);
 
596
  TableList *end_nested_join(Session *session);
 
597
  TableList *nest_last_join(Session *session);
658
598
  void add_joined_table(TableList *table);
659
599
  TableList *convert_right_join();
660
600
  List<Item>* get_item_list();
668
608
  }
669
609
  /*
670
610
    This method created for reiniting LEX in mysql_admin_table() and can be
671
 
    used only if you are going remove all SELECT_LEX & units except belonger
 
611
    used only if you are going remove all Select_Lex & units except belonger
672
612
    to LEX (LEX::unit & LEX::select, for other purposes there are
673
 
    SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
 
613
    Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
674
614
  */
675
615
  void cut_subtree() { slave= 0; }
676
616
  bool test_limit();
677
617
 
678
 
  friend void lex_start(THD *thd);
679
 
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
 
618
  friend void lex_start(Session *session);
 
619
  Select_Lex() : n_sum_items(0), n_child_sum_items(0) {}
680
620
  void make_empty_select()
681
621
  {
682
622
    init_query();
683
623
    init_select();
684
624
  }
685
 
  bool setup_ref_array(THD *thd, uint32_t order_group_num);
686
 
  void print(THD *thd, String *str, enum_query_type query_type);
 
625
  bool setup_ref_array(Session *session, uint32_t order_group_num);
 
626
  void print(Session *session, String *str, enum_query_type query_type);
687
627
  static void print_order(String *str,
688
628
                          order_st *order,
689
629
                          enum_query_type query_type);
690
 
  void print_limit(THD *thd, String *str, enum_query_type query_type);
691
 
  void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
 
630
  void print_limit(Session *session, String *str, enum_query_type query_type);
 
631
  void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
692
632
  /*
693
633
    Destroy the used execution plan (JOIN) of this subtree (this
694
 
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
 
634
    Select_Lex and all nested Select_Lexes and Select_Lex_Units).
695
635
  */
696
636
  bool cleanup();
697
637
  /*
702
642
 
703
643
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
704
644
 
705
 
  /* 
 
645
  /*
706
646
   Add a index hint to the tagged list of hints. The type and clause of the
707
 
   hint will be the current ones (set by set_index_hint()) 
 
647
   hint will be the current ones (set by set_index_hint())
708
648
  */
709
 
  bool add_index_hint (THD *thd, char *str, uint32_t length);
 
649
  bool add_index_hint (Session *session, char *str, uint32_t length);
710
650
 
711
651
  /* make a list to hold index hints */
712
 
  void alloc_index_hints (THD *thd);
 
652
  void alloc_index_hints (Session *session);
713
653
  /* read and clear the index hints */
714
 
  List<Index_hint>* pop_index_hints(void) 
 
654
  List<Index_hint>* pop_index_hints(void)
715
655
  {
716
656
    List<Index_hint> *hints= index_hints;
717
657
    index_hints= NULL;
720
660
 
721
661
  void clear_index_hints(void) { index_hints= NULL; }
722
662
 
723
 
private:  
 
663
private:
724
664
  /* current index hint kind. used in filling up index_hints */
725
665
  enum index_hint_type current_index_hint_type;
726
666
  index_clause_map current_index_hint_clause;
727
667
  /* a list of USE/FORCE/IGNORE INDEX */
728
668
  List<Index_hint> *index_hints;
729
669
};
730
 
typedef class st_select_lex SELECT_LEX;
731
670
 
732
 
inline bool st_select_lex_unit::is_union ()
733
 
734
 
  return first_select()->next_select() && 
 
671
inline bool Select_Lex_Unit::is_union ()
 
672
{
 
673
  return first_select()->next_select() &&
735
674
    first_select()->next_select()->linkage == UNION_TYPE;
736
675
}
737
676
 
840
779
    0 - indicates that this query does not need prelocking.
841
780
  */
842
781
  TableList **query_tables_own_last;
843
 
  /*
844
 
    Set of stored routines called by statement.
845
 
    (Note that we use lazy-initialization for this hash).
846
 
  */
847
 
  enum { START_SROUTINES_HASH_SIZE= 16 };
848
 
  HASH sroutines;
849
 
  /*
850
 
    List linking elements of 'sroutines' set. Allows you to add new elements
851
 
    to this set as you iterate through the list of existing elements.
852
 
    'sroutines_list_own_last' is pointer to ::next member of last element of
853
 
    this list which represents routine which is explicitly used by query.
854
 
    'sroutines_list_own_elements' number of explicitly used routines.
855
 
    We use these two members for restoring of 'sroutines_list' to the state
856
 
    in which it was right after query parsing.
857
 
  */
858
 
  SQL_LIST sroutines_list;
859
 
  unsigned char    **sroutines_list_own_last;
860
 
  uint32_t     sroutines_list_own_elements;
861
782
 
862
783
  /*
863
784
    These constructor and destructor serve for creation/destruction
898
819
      query_tables_own_last= 0;
899
820
    }
900
821
  }
901
 
 
902
 
  /**
903
 
     Has the parser/scanner detected that this statement is unsafe?
904
 
   */
905
 
  inline bool is_stmt_unsafe() const {
906
 
    return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
907
 
  }
908
 
 
909
 
  /**
910
 
     Flag the current (top-level) statement as unsafe.
911
 
 
912
 
     The flag will be reset after the statement has finished.
913
 
 
914
 
   */
915
 
  inline void set_stmt_unsafe() {
916
 
    binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
917
 
  }
918
 
 
919
 
  inline void clear_stmt_unsafe() {
920
 
    binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
921
 
  }
922
 
 
923
 
  /**
924
 
    true if the parsed tree contains references to stored procedures
925
 
    or functions, false otherwise
926
 
  */
927
 
  bool uses_stored_routines() const
928
 
  { return sroutines_list.elements != 0; }
929
 
 
930
 
private:
931
 
  enum enum_binlog_stmt_flag {
932
 
    BINLOG_STMT_FLAG_UNSAFE,
933
 
    BINLOG_STMT_FLAG_COUNT
934
 
  };
935
 
 
936
 
  /*
937
 
    Tells if the parsing stage detected properties of the statement,
938
 
    for example: that some items require row-based binlogging to give
939
 
    a reliable binlog/replication, or if we will use stored functions
940
 
    or triggers which themselves need require row-based binlogging.
941
 
  */
942
 
  uint32_t binlog_stmt_flags;
943
822
};
944
823
 
945
824
 
950
829
 
951
830
struct st_parsing_options
952
831
{
953
 
  bool allows_variable;
954
 
  bool allows_select_into;
955
832
  bool allows_select_procedure;
956
 
  bool allows_derived;
957
833
 
958
834
  st_parsing_options() { reset(); }
959
835
  void reset();
1001
877
class Lex_input_stream
1002
878
{
1003
879
public:
1004
 
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
 
880
  Lex_input_stream(Session *session, const char* buff, unsigned int length);
1005
881
  ~Lex_input_stream();
1006
882
 
1007
883
  /**
1229
1105
    return m_body_utf8_ptr - m_body_utf8;
1230
1106
  }
1231
1107
 
1232
 
  void body_utf8_start(THD *thd, const char *begin_ptr);
 
1108
  void body_utf8_start(Session *session, const char *begin_ptr);
1233
1109
  void body_utf8_append(const char *ptr);
1234
1110
  void body_utf8_append(const char *ptr, const char *end_ptr);
1235
 
  void body_utf8_append_literal(THD *thd,
 
1111
  void body_utf8_append_literal(Session *session,
1236
1112
                                const LEX_STRING *txt,
1237
1113
                                const CHARSET_INFO * const txt_cs,
1238
1114
                                const char *end_ptr);
1239
1115
 
1240
1116
  /** Current thread. */
1241
 
  THD *m_thd;
 
1117
  Session *m_session;
1242
1118
 
1243
1119
  /** Current line number. */
1244
1120
  uint32_t yylineno;
1340
1216
    Starting position of the TEXT_STRING or IDENT in the pre-processed
1341
1217
    buffer.
1342
1218
 
1343
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1219
    NOTE: this member must be used within DRIZZLElex() function only.
1344
1220
  */
1345
1221
  const char *m_cpp_text_start;
1346
1222
 
1348
1224
    Ending position of the TEXT_STRING or IDENT in the pre-processed
1349
1225
    buffer.
1350
1226
 
1351
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1227
    NOTE: this member must be used within DRIZZLElex() function only.
1352
1228
    */
1353
1229
  const char *m_cpp_text_end;
1354
1230
 
1355
1231
  /**
1356
1232
    Character set specified by the character-set-introducer.
1357
1233
 
1358
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1234
    NOTE: this member must be used within DRIZZLElex() function only.
1359
1235
  */
1360
1236
  const CHARSET_INFO *m_underscore_cs;
1361
1237
};
1362
1238
 
1363
1239
 
1364
 
/* The state of the lex parsing. This is saved in the THD struct */
 
1240
/* The state of the lex parsing. This is saved in the Session struct */
1365
1241
 
1366
 
typedef struct st_lex : public Query_tables_list
 
1242
class LEX : public Query_tables_list
1367
1243
{
1368
 
  SELECT_LEX_UNIT unit;                         /* most upper unit */
1369
 
  SELECT_LEX select_lex;                        /* first SELECT_LEX */
1370
 
  /* current SELECT_LEX in parsing */
1371
 
  SELECT_LEX *current_select;
1372
 
  /* list of all SELECT_LEX */
1373
 
  SELECT_LEX *all_selects_list;
 
1244
public:
 
1245
  Select_Lex_Unit unit;                         /* most upper unit */
 
1246
  Select_Lex select_lex;                        /* first Select_Lex */
 
1247
  /* current Select_Lex in parsing */
 
1248
  Select_Lex *current_select;
 
1249
  /* list of all Select_Lex */
 
1250
  Select_Lex *all_selects_list;
1374
1251
 
1375
1252
  char *length,*dec,*change;
1376
1253
  LEX_STRING name;
1377
 
  char *help_arg;
1378
 
  char* to_log;                                 /* For PURGE MASTER LOGS TO */
1379
 
  char* x509_subject,*x509_issuer,*ssl_cipher;
1380
1254
  String *wild;
1381
 
  sql_exchange *exchange;
 
1255
  file_exchange *exchange;
1382
1256
  select_result *result;
1383
1257
  Item *default_value, *on_update_value;
1384
1258
  LEX_STRING comment, ident;
1385
1259
  XID *xid;
1386
1260
  unsigned char* yacc_yyss, *yacc_yyvs;
1387
 
  THD *thd;
1388
 
 
1389
 
  /* maintain a list of used plugins for this LEX */
1390
 
  DYNAMIC_ARRAY plugins;
1391
 
  plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
 
1261
  Session *session;
 
1262
  virtual_column_info *vcol_info;
1392
1263
 
1393
1264
  const CHARSET_INFO *charset;
1394
1265
  bool text_string_is_7bit;
1398
1269
  List<Key_part_spec> col_list;
1399
1270
  List<Key_part_spec> ref_list;
1400
1271
  List<String>        interval_list;
1401
 
  List<LEX_COLUMN>    columns;
 
1272
  List<Lex_Column>    columns;
1402
1273
  List<Item>          *insert_list,field_list,value_list,update_list;
1403
1274
  List<List_item>     many_values;
1404
1275
  List<set_var_base>  var_list;
1426
1297
  HA_CHECK_OPT   check_opt;                     // check/repair options
1427
1298
  HA_CREATE_INFO create_info;
1428
1299
  KEY_CREATE_INFO key_create_info;
1429
 
  LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
1430
 
  LEX_SERVER_OPTIONS server_options;
1431
1300
  uint32_t type;
1432
1301
  /*
1433
1302
    This variable is used in post-parse stage to declare that sum-functions,
1447
1316
    syntax error back.
1448
1317
  */
1449
1318
  bool expr_allows_subselect;
 
1319
  /*
 
1320
    A special command "PARSE_VCOL_EXPR" is defined for the parser
 
1321
    to translate an expression statement of a virtual column \
 
1322
    (stored in the *.frm file as a string) into an Item object.
 
1323
    The following flag is used to prevent other applications to use
 
1324
    this command.
 
1325
  */
 
1326
  bool parse_vcol_expr;
1450
1327
 
1451
1328
  thr_lock_type lock_option;
1452
1329
  enum enum_duplicates duplicates;
1466
1343
  enum Foreign_key::fk_match_opt fk_match_option;
1467
1344
  enum Foreign_key::fk_option fk_update_opt;
1468
1345
  enum Foreign_key::fk_option fk_delete_opt;
1469
 
  uint32_t slave_thd_opt, start_transaction_opt;
 
1346
  uint32_t slave_session_opt, start_transaction_opt;
1470
1347
  int nest_level;
1471
1348
  /*
1472
1349
    In LEX representing update which were transformed to multi-update
1490
1367
  Alter_info alter_info;
1491
1368
 
1492
1369
  /*
1493
 
    field_list was created for view and should be removed before PS/SP
1494
 
    rexecuton
1495
 
  */
1496
 
  bool empty_field_list_on_rset;
1497
 
 
1498
 
  /*
1499
1370
    Pointers to part of LOAD DATA statement that should be rewritten
1500
1371
    during replication ("LOCAL 'filename' REPLACE INTO" part).
1501
1372
  */
1502
1373
  const char *fname_start;
1503
1374
  const char *fname_end;
1504
 
  
 
1375
 
1505
1376
  /**
1506
 
    During name resolution search only in the table list given by 
 
1377
    During name resolution search only in the table list given by
1507
1378
    Name_resolution_context::first_name_resolution_table and
1508
1379
    Name_resolution_context::last_name_resolution_table
1509
 
    (see Item_field::fix_fields()). 
 
1380
    (see Item_field::fix_fields()).
1510
1381
  */
1511
1382
  bool use_only_table_context;
1512
 
  
 
1383
 
1513
1384
  bool escape_used;
1514
1385
  bool is_lex_started; /* If lex_start() did run. For debugging. */
1515
1386
 
1516
 
  st_lex();
 
1387
  LEX();
1517
1388
 
1518
 
  virtual ~st_lex()
 
1389
  virtual ~LEX()
1519
1390
  {
1520
1391
    destroy_query_tables_list();
1521
 
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1522
 
    delete_dynamic(&plugins);
1523
1392
  }
1524
1393
 
1525
1394
  TableList *unlink_first_table(bool *link_to_local);
1526
1395
  void link_first_table_back(TableList *first, bool link_to_local);
1527
1396
  void first_lists_tables_same();
1528
1397
 
1529
 
  bool can_be_merged();
1530
 
  bool can_use_merged();
1531
 
  bool can_not_use_merged();
1532
1398
  bool only_view_structure();
1533
1399
  bool need_correct_ident();
1534
1400
 
1551
1417
    return context_stack.head();
1552
1418
  }
1553
1419
  /*
1554
 
    Restore the LEX and THD in case of a parse error.
 
1420
    Restore the LEX and Session in case of a parse error.
1555
1421
  */
1556
 
  static void cleanup_lex_after_parse_error(THD *thd);
1557
 
 
1558
 
  void reset_n_backup_query_tables_list(Query_tables_list *backup);
1559
 
  void restore_backup_query_tables_list(Query_tables_list *backup);
1560
 
 
1561
 
  bool table_or_sp_used();
 
1422
  static void cleanup_lex_after_parse_error(Session *session);
1562
1423
 
1563
1424
  /**
1564
1425
    @brief check if the statement is a single-level join
1565
1426
    @return result of the check
1566
 
      @retval true  The statement doesn't contain subqueries, unions and 
 
1427
      @retval true  The statement doesn't contain subqueries, unions and
1567
1428
                    stored procedure calls.
1568
1429
      @retval false There are subqueries, UNIONs or stored procedure calls.
1569
1430
  */
1570
 
  bool is_single_level_stmt() 
1571
 
  { 
1572
 
    /* 
 
1431
  bool is_single_level_stmt()
 
1432
  {
 
1433
    /*
1573
1434
      This check exploits the fact that the last added to all_select_list is
1574
 
      on its top. So select_lex (as the first added) will be at the tail 
 
1435
      on its top. So select_lex (as the first added) will be at the tail
1575
1436
      of the list.
1576
 
    */ 
1577
 
    if (&select_lex == all_selects_list && !sroutines.records)
 
1437
    */
 
1438
    if (&select_lex == all_selects_list)
1578
1439
    {
1579
1440
      assert(!all_selects_list->next_select_in_list());
1580
1441
      return true;
1581
1442
    }
1582
1443
    return false;
1583
1444
  }
1584
 
} LEX;
 
1445
};
1585
1446
 
1586
 
struct st_lex_local: public st_lex
 
1447
struct st_lex_local: public LEX
1587
1448
{
1588
1449
  static void *operator new(size_t size) throw()
1589
1450
  {
1593
1454
  {
1594
1455
    return (void*) alloc_root(mem_root, (uint32_t) size);
1595
1456
  }
1596
 
  static void operator delete(void *ptr __attribute__((unused)),
1597
 
                              size_t size __attribute__((unused)))
 
1457
  static void operator delete(void *, size_t)
1598
1458
  { TRASH(ptr, size); }
1599
 
  static void operator delete(void *ptr __attribute__((unused)),
1600
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
1459
  static void operator delete(void *, MEM_ROOT *)
1601
1460
  { /* Never called */ }
1602
1461
};
1603
1462
 
1604
 
extern void lex_init(void);
1605
 
extern void lex_free(void);
1606
 
extern void lex_start(THD *thd);
 
1463
extern void lex_start(Session *session);
1607
1464
extern void lex_end(LEX *lex);
1608
1465
 
1609
1466
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);