~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
/**
24
24
  @defgroup Semantic_Analysis Semantic Analysis
25
25
*/
 
26
#include "drizzled/sql_udf.h"
 
27
#include "drizzled/name_resolution_context.h"
 
28
#include "drizzled/item/subselect.h"
 
29
#include "drizzled/item/param.h"
 
30
#include "drizzled/item/outer_ref.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/key.h"
 
36
#include "drizzled/foreign_key.h"
 
37
#include "drizzled/item/param.h"
 
38
#include "drizzled/index_hint.h"
26
39
 
27
 
#include "sql_udf.h"
 
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
 
/* 
159
 
  String names used to print a statement with index hints.
160
 
  Keep in sync with index_hint_type.
161
 
*/
162
 
extern const char * index_hint_type_name[];
163
 
typedef unsigned char index_clause_map;
164
 
 
165
108
/*
166
 
  Bits in index_clause_map : one for each possible FOR clause in
167
 
  USE/FORCE/IGNORE INDEX index hint specification
168
 
*/
169
 
#define INDEX_HINT_MASK_JOIN  (1)
170
 
#define INDEX_HINT_MASK_GROUP (1 << 1)
171
 
#define INDEX_HINT_MASK_ORDER (1 << 2)
172
 
 
173
 
#define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
174
 
                             INDEX_HINT_MASK_ORDER)
175
 
 
176
 
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint  */
177
 
class Index_hint : public Sql_alloc
178
 
{
179
 
public:
180
 
  /* The type of the hint : USE/FORCE/IGNORE */
181
 
  enum index_hint_type type;
182
 
  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
183
 
  index_clause_map clause;
184
 
  /* 
185
 
    The index name. Empty (str=NULL) name represents an empty list 
186
 
    USE INDEX () clause 
187
 
  */ 
188
 
  LEX_STRING key_name;
189
 
 
190
 
  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
191
 
              char *str, uint32_t length) :
192
 
    type(type_arg), clause(clause_arg)
193
 
  {
194
 
    key_name.str= str;
195
 
    key_name.length= length;
196
 
  }
197
 
 
198
 
  void print(THD *thd, String *str);
199
 
}; 
200
 
 
201
 
/* 
202
 
  The state of the lex parsing for selects 
203
 
   
 
109
  The state of the lex parsing for selects
 
110
 
204
111
   master and slaves are pointers to select_lex.
205
112
   master is pointer to upper level node.
206
113
   slave is pointer to lower level node
312
219
 
313
220
*/
314
221
 
315
 
/* 
316
 
    Base class for st_select_lex (SELECT_LEX) & 
317
 
    st_select_lex_unit (SELECT_LEX_UNIT)
 
222
/*
 
223
    Base class for Select_Lex (Select_Lex) &
 
224
    Select_Lex_Unit (Select_Lex_Unit)
318
225
*/
319
 
struct st_lex;
320
 
class st_select_lex;
321
 
class st_select_lex_unit;
322
 
class st_select_lex_node {
 
226
class LEX;
 
227
class Select_Lex;
 
228
class Select_Lex_Unit;
 
229
class Select_Lex_Node {
323
230
protected:
324
 
  st_select_lex_node *next, **prev,   /* neighbor list */
 
231
  Select_Lex_Node *next, **prev,   /* neighbor list */
325
232
    *master, *slave,                  /* vertical links */
326
 
    *link_next, **link_prev;          /* list of whole SELECT_LEX */
 
233
    *link_next, **link_prev;          /* list of whole Select_Lex */
327
234
public:
328
235
 
329
236
  uint64_t options;
347
254
  }
348
255
  static void *operator new(size_t size, MEM_ROOT *mem_root)
349
256
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
350
 
  static void operator delete(void *ptr __attribute__((unused)),
351
 
                              size_t size __attribute__((unused)))
 
257
  static void operator delete(void *, size_t)
352
258
  { TRASH(ptr, size); }
353
 
  static void operator delete(void *ptr __attribute__((unused)),
354
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
259
  static void operator delete(void *, MEM_ROOT *)
355
260
  {}
356
 
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
357
 
  virtual ~st_select_lex_node() {}
358
 
  inline st_select_lex_node* get_master() { return master; }
 
261
  Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
 
262
  virtual ~Select_Lex_Node() {}
 
263
  inline Select_Lex_Node* get_master() { return master; }
359
264
  virtual void init_query();
360
265
  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);
 
266
  void include_down(Select_Lex_Node *upper);
 
267
  void include_neighbour(Select_Lex_Node *before);
 
268
  void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
 
269
  void include_global(Select_Lex_Node **plink);
365
270
  void exclude();
366
271
 
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;
 
272
  virtual Select_Lex_Unit* master_unit()= 0;
 
273
  virtual Select_Lex* outer_select()= 0;
 
274
  virtual Select_Lex* return_after_parsing()= 0;
370
275
 
371
276
  virtual bool set_braces(bool value);
372
277
  virtual bool inc_in_sum_expr();
374
279
  virtual TableList* get_table_list();
375
280
  virtual List<Item>* get_item_list();
376
281
  virtual uint32_t get_table_join_options();
377
 
  virtual TableList *add_table_to_list(THD *thd, Table_ident *table,
 
282
  virtual TableList *add_table_to_list(Session *session, Table_ident *table,
378
283
                                        LEX_STRING *alias,
379
284
                                        uint32_t table_options,
380
285
                                        thr_lock_type flags= TL_UNLOCK,
381
286
                                        List<Index_hint> *hints= 0,
382
287
                                        LEX_STRING *option= 0);
383
 
  virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
 
288
  virtual void set_lock_for_tables(thr_lock_type)
384
289
  {}
385
290
 
386
 
  friend class st_select_lex_unit;
387
 
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
 
291
  friend class Select_Lex_Unit;
 
292
  friend bool mysql_new_select(LEX *lex, bool move_down);
388
293
private:
389
294
  void fast_exclude();
390
295
};
391
 
typedef class st_select_lex_node SELECT_LEX_NODE;
392
296
 
393
 
/* 
394
 
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
395
 
   SELECT_LEXs
 
297
/*
 
298
   Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
 
299
   Select_Lexs
396
300
*/
397
 
class THD;
 
301
class Session;
398
302
class select_result;
399
303
class JOIN;
400
304
class select_union;
401
 
class st_select_lex_unit: public st_select_lex_node {
 
305
class Select_Lex_Unit: public Select_Lex_Node {
402
306
protected:
403
307
  TableList result_table_list;
404
308
  select_union *union_result;
418
322
  List<Item> item_list;
419
323
  /*
420
324
    list of types of items inside union (used for union & derived tables)
421
 
    
 
325
 
422
326
    Item_type_holders from which this list consist may have pointers to Field,
423
327
    pointers is valid only after preparing SELECTS of this unit and before
424
328
    any SELECT of this unit execution
432
336
    Pointer to 'last' select or pointer to unit where stored
433
337
    global parameters for union
434
338
  */
435
 
  st_select_lex *global_parameters;
 
339
  Select_Lex *global_parameters;
436
340
  //node on wich we should return current_select pointer after parsing subquery
437
 
  st_select_lex *return_to;
 
341
  Select_Lex *return_to;
438
342
  /* LIMIT clause runtime counters */
439
343
  ha_rows select_limit_cnt, offset_limit_cnt;
440
344
  /* not NULL if unit used in subselect, point to subselect item */
441
345
  Item_subselect *item;
442
346
  /* thread handler */
443
 
  THD *thd;
 
347
  Session *session;
444
348
  /*
445
 
    SELECT_LEX for hidden SELECT in onion which process global
 
349
    Select_Lex for hidden SELECT in onion which process global
446
350
    ORDER BY and LIMIT
447
351
  */
448
 
  st_select_lex *fake_select_lex;
 
352
  Select_Lex *fake_select_lex;
449
353
 
450
 
  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
 
354
  Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
451
355
  bool describe; /* union exec() called for EXPLAIN */
452
356
 
453
357
  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; }
 
358
  Select_Lex_Unit* master_unit();
 
359
  Select_Lex* outer_select();
 
360
  Select_Lex* first_select()
 
361
  {
 
362
    return reinterpret_cast<Select_Lex*>(slave);
 
363
  }
 
364
  Select_Lex_Unit* next_unit()
 
365
  {
 
366
    return reinterpret_cast<Select_Lex_Unit*>(next);
 
367
  }
 
368
  Select_Lex* return_after_parsing() { return return_to; }
465
369
  void exclude_level();
466
370
  void exclude_tree();
467
371
 
468
372
  /* UNION methods */
469
 
  bool prepare(THD *thd, select_result *result, uint32_t additional_options);
 
373
  bool prepare(Session *session, select_result *result,
 
374
               uint64_t additional_options);
470
375
  bool exec();
471
376
  bool cleanup();
472
377
  inline void unclean() { cleaned= 0; }
474
379
 
475
380
  void print(String *str, enum_query_type query_type);
476
381
 
477
 
  bool add_fake_select_lex(THD *thd);
478
 
  void init_prepare_fake_select_lex(THD *thd);
 
382
  bool add_fake_select_lex(Session *session);
 
383
  void init_prepare_fake_select_lex(Session *session);
479
384
  bool change_result(select_result_interceptor *result,
480
385
                     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 (); 
 
386
  void set_limit(Select_Lex *values);
 
387
  void set_session(Session *session_arg) { session= session_arg; }
 
388
  inline bool is_union ();
484
389
 
485
 
  friend void lex_start(THD *thd);
 
390
  friend void lex_start(Session *session);
486
391
  friend int subselect_union_engine::exec();
487
392
 
488
393
  List<Item> *get_unit_column_types();
489
394
};
490
395
 
491
 
typedef class st_select_lex_unit SELECT_LEX_UNIT;
492
 
 
493
396
/*
494
 
  SELECT_LEX - store information of parsed SELECT statment
 
397
  Select_Lex - store information of parsed SELECT statment
495
398
*/
496
 
class st_select_lex: public st_select_lex_node
 
399
class Select_Lex: public Select_Lex_Node
497
400
{
498
401
public:
499
402
  Name_resolution_context context;
502
405
  /* Saved values of the WHERE and HAVING clauses*/
503
406
  Item::cond_result cond_value, having_value;
504
407
  /* point on lex in which it was created, used in view subquery detection */
505
 
  st_lex *parent_lex;
 
408
  LEX *parent_lex;
506
409
  enum olap_type olap;
507
410
  /* FROM clause - points to the beginning of the TableList::next_local list. */
508
411
  SQL_LIST            table_list;
510
413
  List<Item>          item_list;  /* list of fields & expressions */
511
414
  List<String>        interval_list;
512
415
  bool                is_item_list_lookup;
513
 
  /* 
514
 
    Despite their names, the following are used in unions. This should 
 
416
  /*
 
417
    Despite their names, the following are used in unions. This should
515
418
    be rewritten. -Brian
516
419
  */
517
420
  List<Item_real_func> *ftfunc_list;
543
446
  uint32_t select_n_having_items;
544
447
  uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
545
448
  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  */   
 
449
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
547
450
  /*
548
451
    Number of fields used in select list or where clause of current select
549
452
    and all inner subselects.
551
454
  uint32_t select_n_where_fields;
552
455
  enum_parsing_place parsing_place; /* where we are parsing expression */
553
456
  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
457
 
560
458
  uint32_t table_join_options;
561
459
  uint32_t in_sum_expr;
562
460
  uint32_t select_number; /* number of select (used for EXPLAIN) */
563
 
  int nest_level;     /* nesting level of select */
564
 
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */ 
 
461
  int8_t nest_level;     /* nesting level of select */
 
462
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
565
463
  uint32_t with_wild; /* item list contain '*' */
566
464
  bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
567
465
  /* true when having fix field called in processing of this SELECT */
589
487
  /* index in the select list of the expression currently being fixed */
590
488
  int cur_pos_in_select_list;
591
489
 
592
 
  List<udf_func>     udf_list;                  /* udf function calls stack */
593
 
  /* 
 
490
  List<Function_builder>     udf_list;                  /* udf function calls stack */
 
491
  /*
594
492
    This is a copy of the original JOIN USING list that comes from
595
493
    the parser. The parser :
596
494
      1. Sets the natural_join of the second TableList in the join
597
 
         and the st_select_lex::prev_join_using.
 
495
         and the Select_Lex::prev_join_using.
598
496
      2. Makes a parent TableList and sets its is_natural_join/
599
497
       join_using_fields members.
600
498
      3. Uses the wrapper TableList as a table in the upper level.
616
514
  uint8_t full_group_by_flag;
617
515
  void init_query();
618
516
  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()
 
517
  Select_Lex_Unit* master_unit();
 
518
  Select_Lex_Unit* first_inner_unit()
 
519
  {
 
520
    return (Select_Lex_Unit*) slave;
 
521
  }
 
522
  Select_Lex* outer_select();
 
523
  Select_Lex* next_select() { return (Select_Lex*) next; }
 
524
  Select_Lex* next_select_in_list()
 
525
  {
 
526
    return (Select_Lex*) link_next;
 
527
  }
 
528
  Select_Lex_Node** next_select_in_list_addr()
631
529
  {
632
530
    return &link_next;
633
531
  }
634
 
  st_select_lex* return_after_parsing()
 
532
  Select_Lex* return_after_parsing()
635
533
  {
636
534
    return master_unit()->return_after_parsing();
637
535
  }
638
536
 
639
 
  void mark_as_dependent(st_select_lex *last);
 
537
  void mark_as_dependent(Select_Lex *last);
640
538
 
641
539
  bool set_braces(bool value);
642
540
  bool inc_in_sum_expr();
643
541
  uint32_t get_in_sum_expr();
644
542
 
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,
 
543
  bool add_item_to_list(Session *session, Item *item);
 
544
  bool add_group_to_list(Session *session, Item *item, bool asc);
 
545
  bool add_order_to_list(Session *session, Item *item, bool asc);
 
546
  TableList* add_table_to_list(Session *session, Table_ident *table,
649
547
                                LEX_STRING *alias,
650
548
                                uint32_t table_options,
651
549
                                thr_lock_type flags= TL_UNLOCK,
652
550
                                List<Index_hint> *hints= 0,
653
551
                                LEX_STRING *option= 0);
654
552
  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);
 
553
  bool init_nested_join(Session *session);
 
554
  TableList *end_nested_join(Session *session);
 
555
  TableList *nest_last_join(Session *session);
658
556
  void add_joined_table(TableList *table);
659
557
  TableList *convert_right_join();
660
558
  List<Item>* get_item_list();
668
566
  }
669
567
  /*
670
568
    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
 
569
    used only if you are going remove all Select_Lex & units except belonger
672
570
    to LEX (LEX::unit & LEX::select, for other purposes there are
673
 
    SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
 
571
    Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
674
572
  */
675
573
  void cut_subtree() { slave= 0; }
676
574
  bool test_limit();
677
575
 
678
 
  friend void lex_start(THD *thd);
679
 
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
 
576
  friend void lex_start(Session *session);
 
577
  Select_Lex() : n_sum_items(0), n_child_sum_items(0) {}
680
578
  void make_empty_select()
681
579
  {
682
580
    init_query();
683
581
    init_select();
684
582
  }
685
 
  bool setup_ref_array(THD *thd, uint32_t order_group_num);
686
 
  void print(THD *thd, String *str, enum_query_type query_type);
 
583
  bool setup_ref_array(Session *session, uint32_t order_group_num);
 
584
  void print(Session *session, String *str, enum_query_type query_type);
687
585
  static void print_order(String *str,
688
586
                          order_st *order,
689
587
                          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);
 
588
  void print_limit(Session *session, String *str, enum_query_type query_type);
 
589
  void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
692
590
  /*
693
591
    Destroy the used execution plan (JOIN) of this subtree (this
694
 
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
 
592
    Select_Lex and all nested Select_Lexes and Select_Lex_Units).
695
593
  */
696
594
  bool cleanup();
697
595
  /*
702
600
 
703
601
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
704
602
 
705
 
  /* 
 
603
  /*
706
604
   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()) 
 
605
   hint will be the current ones (set by set_index_hint())
708
606
  */
709
 
  bool add_index_hint (THD *thd, char *str, uint32_t length);
 
607
  bool add_index_hint (Session *session, char *str, uint32_t length);
710
608
 
711
609
  /* make a list to hold index hints */
712
 
  void alloc_index_hints (THD *thd);
 
610
  void alloc_index_hints (Session *session);
713
611
  /* read and clear the index hints */
714
 
  List<Index_hint>* pop_index_hints(void) 
 
612
  List<Index_hint>* pop_index_hints(void)
715
613
  {
716
614
    List<Index_hint> *hints= index_hints;
717
615
    index_hints= NULL;
720
618
 
721
619
  void clear_index_hints(void) { index_hints= NULL; }
722
620
 
723
 
private:  
 
621
private:
724
622
  /* current index hint kind. used in filling up index_hints */
725
623
  enum index_hint_type current_index_hint_type;
726
624
  index_clause_map current_index_hint_clause;
727
625
  /* a list of USE/FORCE/IGNORE INDEX */
728
626
  List<Index_hint> *index_hints;
729
627
};
730
 
typedef class st_select_lex SELECT_LEX;
731
628
 
732
 
inline bool st_select_lex_unit::is_union ()
733
 
734
 
  return first_select()->next_select() && 
 
629
inline bool Select_Lex_Unit::is_union ()
 
630
{
 
631
  return first_select()->next_select() &&
735
632
    first_select()->next_select()->linkage == UNION_TYPE;
736
633
}
737
634
 
840
737
    0 - indicates that this query does not need prelocking.
841
738
  */
842
739
  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
740
 
862
741
  /*
863
742
    These constructor and destructor serve for creation/destruction
898
777
      query_tables_own_last= 0;
899
778
    }
900
779
  }
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
780
};
944
781
 
945
782
 
950
787
 
951
788
struct st_parsing_options
952
789
{
953
 
  bool allows_variable;
954
 
  bool allows_select_into;
955
790
  bool allows_select_procedure;
956
 
  bool allows_derived;
957
791
 
958
792
  st_parsing_options() { reset(); }
959
793
  void reset();
1001
835
class Lex_input_stream
1002
836
{
1003
837
public:
1004
 
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
 
838
  Lex_input_stream(Session *session, const char* buff, unsigned int length);
1005
839
  ~Lex_input_stream();
1006
840
 
1007
841
  /**
1229
1063
    return m_body_utf8_ptr - m_body_utf8;
1230
1064
  }
1231
1065
 
1232
 
  void body_utf8_start(THD *thd, const char *begin_ptr);
 
1066
  void body_utf8_start(Session *session, const char *begin_ptr);
1233
1067
  void body_utf8_append(const char *ptr);
1234
1068
  void body_utf8_append(const char *ptr, const char *end_ptr);
1235
 
  void body_utf8_append_literal(THD *thd,
 
1069
  void body_utf8_append_literal(Session *session,
1236
1070
                                const LEX_STRING *txt,
1237
1071
                                const CHARSET_INFO * const txt_cs,
1238
1072
                                const char *end_ptr);
1239
1073
 
1240
1074
  /** Current thread. */
1241
 
  THD *m_thd;
 
1075
  Session *m_session;
1242
1076
 
1243
1077
  /** Current line number. */
1244
1078
  uint32_t yylineno;
1340
1174
    Starting position of the TEXT_STRING or IDENT in the pre-processed
1341
1175
    buffer.
1342
1176
 
1343
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1177
    NOTE: this member must be used within DRIZZLElex() function only.
1344
1178
  */
1345
1179
  const char *m_cpp_text_start;
1346
1180
 
1348
1182
    Ending position of the TEXT_STRING or IDENT in the pre-processed
1349
1183
    buffer.
1350
1184
 
1351
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1185
    NOTE: this member must be used within DRIZZLElex() function only.
1352
1186
    */
1353
1187
  const char *m_cpp_text_end;
1354
1188
 
1355
1189
  /**
1356
1190
    Character set specified by the character-set-introducer.
1357
1191
 
1358
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1192
    NOTE: this member must be used within DRIZZLElex() function only.
1359
1193
  */
1360
1194
  const CHARSET_INFO *m_underscore_cs;
1361
1195
};
1362
1196
 
1363
1197
 
1364
 
/* The state of the lex parsing. This is saved in the THD struct */
 
1198
/* The state of the lex parsing. This is saved in the Session struct */
1365
1199
 
1366
 
typedef struct st_lex : public Query_tables_list
 
1200
class LEX : public Query_tables_list
1367
1201
{
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;
 
1202
public:
 
1203
  Select_Lex_Unit unit;                         /* most upper unit */
 
1204
  Select_Lex select_lex;                        /* first Select_Lex */
 
1205
  /* current Select_Lex in parsing */
 
1206
  Select_Lex *current_select;
 
1207
  /* list of all Select_Lex */
 
1208
  Select_Lex *all_selects_list;
1374
1209
 
1375
1210
  char *length,*dec,*change;
1376
1211
  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
1212
  String *wild;
1381
 
  sql_exchange *exchange;
 
1213
  file_exchange *exchange;
1382
1214
  select_result *result;
1383
1215
  Item *default_value, *on_update_value;
1384
1216
  LEX_STRING comment, ident;
1385
1217
  XID *xid;
1386
1218
  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];
 
1219
  Session *session;
 
1220
  virtual_column_info *vcol_info;
1392
1221
 
1393
1222
  const CHARSET_INFO *charset;
1394
1223
  bool text_string_is_7bit;
1398
1227
  List<Key_part_spec> col_list;
1399
1228
  List<Key_part_spec> ref_list;
1400
1229
  List<String>        interval_list;
1401
 
  List<LEX_COLUMN>    columns;
 
1230
  List<Lex_Column>    columns;
1402
1231
  List<Item>          *insert_list,field_list,value_list,update_list;
1403
1232
  List<List_item>     many_values;
1404
1233
  List<set_var_base>  var_list;
1419
1248
  List<Name_resolution_context> context_stack;
1420
1249
  List<LEX_STRING>     db_list;
1421
1250
 
1422
 
  SQL_LIST            proc_list, auxiliary_table_list, save_list;
 
1251
  SQL_LIST            auxiliary_table_list, save_list;
1423
1252
  Create_field        *last_field;
1424
1253
  Item_sum *in_sum_func;
1425
 
  udf_func udf;
 
1254
  Function_builder *udf;
1426
1255
  HA_CHECK_OPT   check_opt;                     // check/repair options
1427
1256
  HA_CREATE_INFO create_info;
1428
1257
  KEY_CREATE_INFO key_create_info;
1429
 
  LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
1430
 
  LEX_SERVER_OPTIONS server_options;
1431
1258
  uint32_t type;
1432
1259
  /*
1433
1260
    This variable is used in post-parse stage to declare that sum-functions,
1447
1274
    syntax error back.
1448
1275
  */
1449
1276
  bool expr_allows_subselect;
 
1277
  /*
 
1278
    A special command "PARSE_VCOL_EXPR" is defined for the parser
 
1279
    to translate an expression statement of a virtual column \
 
1280
    (stored in the *.frm file as a string) into an Item object.
 
1281
    The following flag is used to prevent other applications to use
 
1282
    this command.
 
1283
  */
 
1284
  bool parse_vcol_expr;
1450
1285
 
1451
1286
  thr_lock_type lock_option;
1452
1287
  enum enum_duplicates duplicates;
1466
1301
  enum Foreign_key::fk_match_opt fk_match_option;
1467
1302
  enum Foreign_key::fk_option fk_update_opt;
1468
1303
  enum Foreign_key::fk_option fk_delete_opt;
1469
 
  uint32_t slave_thd_opt, start_transaction_opt;
 
1304
  uint32_t slave_session_opt, start_transaction_opt;
1470
1305
  int nest_level;
1471
1306
  /*
1472
1307
    In LEX representing update which were transformed to multi-update
1480
1315
    query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
1481
1316
  */
1482
1317
  uint8_t derived_tables;
1483
 
  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
 
1318
  bool drop_if_exists, drop_temporary, one_shot_set;
1484
1319
  bool autocommit;
1485
1320
  bool verbose;
1486
1321
 
1490
1325
  Alter_info alter_info;
1491
1326
 
1492
1327
  /*
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
1328
    Pointers to part of LOAD DATA statement that should be rewritten
1500
1329
    during replication ("LOCAL 'filename' REPLACE INTO" part).
1501
1330
  */
1502
1331
  const char *fname_start;
1503
1332
  const char *fname_end;
1504
 
  
 
1333
 
1505
1334
  /**
1506
 
    During name resolution search only in the table list given by 
 
1335
    During name resolution search only in the table list given by
1507
1336
    Name_resolution_context::first_name_resolution_table and
1508
1337
    Name_resolution_context::last_name_resolution_table
1509
 
    (see Item_field::fix_fields()). 
 
1338
    (see Item_field::fix_fields()).
1510
1339
  */
1511
1340
  bool use_only_table_context;
1512
 
  
 
1341
 
1513
1342
  bool escape_used;
1514
1343
  bool is_lex_started; /* If lex_start() did run. For debugging. */
1515
1344
 
1516
 
  st_lex();
 
1345
  LEX();
1517
1346
 
1518
 
  virtual ~st_lex()
 
1347
  virtual ~LEX()
1519
1348
  {
1520
1349
    destroy_query_tables_list();
1521
 
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1522
 
    delete_dynamic(&plugins);
1523
1350
  }
1524
1351
 
1525
1352
  TableList *unlink_first_table(bool *link_to_local);
1526
1353
  void link_first_table_back(TableList *first, bool link_to_local);
1527
1354
  void first_lists_tables_same();
1528
1355
 
1529
 
  bool can_be_merged();
1530
 
  bool can_use_merged();
1531
 
  bool can_not_use_merged();
1532
1356
  bool only_view_structure();
1533
1357
  bool need_correct_ident();
1534
1358
 
1551
1375
    return context_stack.head();
1552
1376
  }
1553
1377
  /*
1554
 
    Restore the LEX and THD in case of a parse error.
 
1378
    Restore the LEX and Session in case of a parse error.
1555
1379
  */
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();
 
1380
  static void cleanup_lex_after_parse_error(Session *session);
1562
1381
 
1563
1382
  /**
1564
1383
    @brief check if the statement is a single-level join
1565
1384
    @return result of the check
1566
 
      @retval true  The statement doesn't contain subqueries, unions and 
 
1385
      @retval true  The statement doesn't contain subqueries, unions and
1567
1386
                    stored procedure calls.
1568
1387
      @retval false There are subqueries, UNIONs or stored procedure calls.
1569
1388
  */
1570
 
  bool is_single_level_stmt() 
1571
 
  { 
1572
 
    /* 
 
1389
  bool is_single_level_stmt()
 
1390
  {
 
1391
    /*
1573
1392
      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 
 
1393
      on its top. So select_lex (as the first added) will be at the tail
1575
1394
      of the list.
1576
 
    */ 
1577
 
    if (&select_lex == all_selects_list && !sroutines.records)
 
1395
    */
 
1396
    if (&select_lex == all_selects_list)
1578
1397
    {
1579
1398
      assert(!all_selects_list->next_select_in_list());
1580
1399
      return true;
1581
1400
    }
1582
1401
    return false;
1583
1402
  }
1584
 
} LEX;
 
1403
};
1585
1404
 
1586
 
struct st_lex_local: public st_lex
 
1405
struct st_lex_local: public LEX
1587
1406
{
1588
1407
  static void *operator new(size_t size) throw()
1589
1408
  {
1593
1412
  {
1594
1413
    return (void*) alloc_root(mem_root, (uint32_t) size);
1595
1414
  }
1596
 
  static void operator delete(void *ptr __attribute__((unused)),
1597
 
                              size_t size __attribute__((unused)))
 
1415
  static void operator delete(void *, size_t)
1598
1416
  { TRASH(ptr, size); }
1599
 
  static void operator delete(void *ptr __attribute__((unused)),
1600
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
1417
  static void operator delete(void *, MEM_ROOT *)
1601
1418
  { /* Never called */ }
1602
1419
};
1603
1420
 
1604
 
extern void lex_init(void);
1605
 
extern void lex_free(void);
1606
 
extern void lex_start(THD *thd);
 
1421
extern void lex_start(Session *session);
1607
1422
extern void lex_end(LEX *lex);
1608
1423
 
1609
1424
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);