~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

  • Committer: Brian Aker
  • Date: 2009-08-21 18:46:31 UTC
  • mto: This revision was merged to the branch mainline in revision 1123.
  • Revision ID: brian@gaz-20090821184631-e11gja79070fvhk4
Cleanup around page checksum removal

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 DRIZZLE_SERVER_SQL_LEX_H
21
 
#define DRIZZLE_SERVER_SQL_LEX_H
 
20
#ifndef DRIZZLED_SQL_LEX_H
 
21
#define DRIZZLED_SQL_LEX_H
22
22
 
23
23
/**
24
24
  @defgroup Semantic_Analysis Semantic Analysis
25
25
*/
26
 
 
27
 
#include "sql_udf.h"
 
26
#include <drizzled/message/table.pb.h>
 
27
 
 
28
#include "drizzled/slot/function.h"
 
29
#include "drizzled/name_resolution_context.h"
 
30
#include "drizzled/item/subselect.h"
 
31
#include "drizzled/item/param.h"
 
32
#include "drizzled/item/outer_ref.h"
 
33
#include "drizzled/table_list.h"
 
34
#include "drizzled/function/math/real.h"
 
35
#include "drizzled/alter_drop.h"
 
36
#include "drizzled/alter_column.h"
 
37
#include "drizzled/key.h"
 
38
#include "drizzled/foreign_key.h"
 
39
#include "drizzled/item/param.h"
 
40
#include "drizzled/index_hint.h"
 
41
#include "drizzled/statement.h"
 
42
 
 
43
#include <bitset>
 
44
 
 
45
class select_result_interceptor;
28
46
 
29
47
/* YACC and LEX Definitions */
30
48
 
31
49
/* These may not be declared yet */
32
50
class Table_ident;
33
 
class sql_exchange;
34
 
class LEX_COLUMN;
 
51
class file_exchange;
 
52
class Lex_Column;
 
53
class Item_outer_ref;
35
54
 
36
 
#ifdef DRIZZLE_SERVER
37
55
/*
38
56
  The following hack is needed because mysql_yacc.cc does not define
39
57
  YYSTYPE before including this file
40
58
*/
41
59
 
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
 
};
 
60
#ifdef DRIZZLE_SERVER
 
61
# include <drizzled/set_var.h>
 
62
# include <drizzled/item/func.h>
 
63
# ifdef DRIZZLE_YACC
 
64
#  define LEX_YYSTYPE void *
 
65
# else
 
66
#  if defined(DRIZZLE_LEX)
 
67
#   include <drizzled/lex_symbol.h>
 
68
#   include <drizzled/sql_yacc.h>
 
69
#   define LEX_YYSTYPE YYSTYPE *
 
70
#  else
 
71
#   define LEX_YYSTYPE void *
 
72
#  endif /* defined(DRIZZLE_LEX) */
 
73
# endif /* DRIZZLE_YACC */
 
74
#endif /* DRIZZLE_SERVER */
103
75
 
104
76
// describe/explain types
105
77
#define DESCRIBE_NORMAL         1
112
84
 
113
85
typedef List<Item> List_item;
114
86
 
115
 
/* SERVERS CACHE CHANGES */
116
 
typedef struct st_lex_server_options
117
 
{
118
 
  int32_t port;
119
 
  uint32_t server_name_length;
120
 
  char *server_name, *host, *db, *username, *password, *scheme, *owner;
121
 
} LEX_SERVER_OPTIONS;
122
 
 
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
 
 
142
87
enum sub_select_type
143
88
{
144
 
  UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
145
 
  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
 
89
  UNSPECIFIED_TYPE,
 
90
  UNION_TYPE,
 
91
  INTERSECT_TYPE,
 
92
  EXCEPT_TYPE,
 
93
  GLOBAL_OPTIONS_TYPE,
 
94
  DERIVED_TABLE_TYPE,
 
95
  OLAP_TYPE
146
96
};
147
97
 
148
 
enum olap_type 
 
98
enum olap_type
149
99
{
150
 
  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
 
100
  UNSPECIFIED_OLAP_TYPE,
 
101
  CUBE_TYPE,
 
102
  ROLLUP_TYPE
151
103
};
152
104
 
153
105
enum tablespace_op_type
154
106
{
155
 
  NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
 
107
  NO_TABLESPACE_OP,
 
108
  DISCARD_TABLESPACE,
 
109
  IMPORT_TABLESPACE
156
110
};
157
111
 
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
112
/*
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
 
   
 
113
  The state of the lex parsing for selects
 
114
 
204
115
   master and slaves are pointers to select_lex.
205
116
   master is pointer to upper level node.
206
117
   slave is pointer to lower level node
312
223
 
313
224
*/
314
225
 
315
 
/* 
316
 
    Base class for st_select_lex (SELECT_LEX) & 
317
 
    st_select_lex_unit (SELECT_LEX_UNIT)
 
226
/*
 
227
    Base class for Select_Lex (Select_Lex) &
 
228
    Select_Lex_Unit (Select_Lex_Unit)
318
229
*/
319
 
struct st_lex;
320
 
class st_select_lex;
321
 
class st_select_lex_unit;
322
 
class st_select_lex_node {
 
230
class LEX;
 
231
class Select_Lex;
 
232
class Select_Lex_Unit;
 
233
class Select_Lex_Node {
323
234
protected:
324
 
  st_select_lex_node *next, **prev,   /* neighbor list */
 
235
  Select_Lex_Node *next, **prev,   /* neighbor list */
325
236
    *master, *slave,                  /* vertical links */
326
 
    *link_next, **link_prev;          /* list of whole SELECT_LEX */
 
237
    *link_next, **link_prev;          /* list of whole Select_Lex */
327
238
public:
328
239
 
329
240
  uint64_t options;
347
258
  }
348
259
  static void *operator new(size_t size, MEM_ROOT *mem_root)
349
260
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
350
 
  static void operator delete(void *ptr __attribute__((unused)),
351
 
                              size_t size __attribute__((unused)))
 
261
  static void operator delete(void *, size_t)
352
262
  { TRASH(ptr, size); }
353
 
  static void operator delete(void *ptr __attribute__((unused)),
354
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
263
  static void operator delete(void *, MEM_ROOT *)
355
264
  {}
356
 
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
357
 
  virtual ~st_select_lex_node() {}
358
 
  inline st_select_lex_node* get_master() { return master; }
 
265
  Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
 
266
  virtual ~Select_Lex_Node() {}
 
267
  inline Select_Lex_Node* get_master() { return master; }
359
268
  virtual void init_query();
360
269
  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);
 
270
  void include_down(Select_Lex_Node *upper);
 
271
  void include_neighbour(Select_Lex_Node *before);
 
272
  void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
 
273
  void include_global(Select_Lex_Node **plink);
365
274
  void exclude();
366
275
 
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;
 
276
  virtual Select_Lex_Unit* master_unit()= 0;
 
277
  virtual Select_Lex* outer_select()= 0;
 
278
  virtual Select_Lex* return_after_parsing()= 0;
370
279
 
371
280
  virtual bool set_braces(bool value);
372
281
  virtual bool inc_in_sum_expr();
374
283
  virtual TableList* get_table_list();
375
284
  virtual List<Item>* get_item_list();
376
285
  virtual uint32_t get_table_join_options();
377
 
  virtual TableList *add_table_to_list(THD *thd, Table_ident *table,
 
286
  virtual TableList *add_table_to_list(Session *session, Table_ident *table,
378
287
                                        LEX_STRING *alias,
379
288
                                        uint32_t table_options,
380
289
                                        thr_lock_type flags= TL_UNLOCK,
381
290
                                        List<Index_hint> *hints= 0,
382
291
                                        LEX_STRING *option= 0);
383
 
  virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
 
292
  virtual void set_lock_for_tables(thr_lock_type)
384
293
  {}
385
294
 
386
 
  friend class st_select_lex_unit;
387
 
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
 
295
  friend class Select_Lex_Unit;
 
296
  friend bool mysql_new_select(LEX *lex, bool move_down);
388
297
private:
389
298
  void fast_exclude();
390
299
};
391
 
typedef class st_select_lex_node SELECT_LEX_NODE;
392
300
 
393
 
/* 
394
 
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
395
 
   SELECT_LEXs
 
301
/*
 
302
   Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
 
303
   Select_Lexs
396
304
*/
397
 
class THD;
 
305
class Session;
398
306
class select_result;
399
307
class JOIN;
400
308
class select_union;
401
 
class st_select_lex_unit: public st_select_lex_node {
 
309
class Select_Lex_Unit: public Select_Lex_Node {
402
310
protected:
403
311
  TableList result_table_list;
404
312
  select_union *union_result;
418
326
  List<Item> item_list;
419
327
  /*
420
328
    list of types of items inside union (used for union & derived tables)
421
 
    
 
329
 
422
330
    Item_type_holders from which this list consist may have pointers to Field,
423
331
    pointers is valid only after preparing SELECTS of this unit and before
424
332
    any SELECT of this unit execution
432
340
    Pointer to 'last' select or pointer to unit where stored
433
341
    global parameters for union
434
342
  */
435
 
  st_select_lex *global_parameters;
 
343
  Select_Lex *global_parameters;
436
344
  //node on wich we should return current_select pointer after parsing subquery
437
 
  st_select_lex *return_to;
 
345
  Select_Lex *return_to;
438
346
  /* LIMIT clause runtime counters */
439
347
  ha_rows select_limit_cnt, offset_limit_cnt;
440
348
  /* not NULL if unit used in subselect, point to subselect item */
441
349
  Item_subselect *item;
442
350
  /* thread handler */
443
 
  THD *thd;
 
351
  Session *session;
444
352
  /*
445
 
    SELECT_LEX for hidden SELECT in onion which process global
 
353
    Select_Lex for hidden SELECT in onion which process global
446
354
    ORDER BY and LIMIT
447
355
  */
448
 
  st_select_lex *fake_select_lex;
 
356
  Select_Lex *fake_select_lex;
449
357
 
450
 
  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
 
358
  Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
451
359
  bool describe; /* union exec() called for EXPLAIN */
452
360
 
453
361
  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; }
 
362
  Select_Lex_Unit* master_unit();
 
363
  Select_Lex* outer_select();
 
364
  Select_Lex* first_select()
 
365
  {
 
366
    return reinterpret_cast<Select_Lex*>(slave);
 
367
  }
 
368
  Select_Lex_Unit* next_unit()
 
369
  {
 
370
    return reinterpret_cast<Select_Lex_Unit*>(next);
 
371
  }
 
372
  Select_Lex* return_after_parsing() { return return_to; }
465
373
  void exclude_level();
466
374
  void exclude_tree();
467
375
 
468
376
  /* UNION methods */
469
 
  bool prepare(THD *thd, select_result *result, uint32_t additional_options);
 
377
  bool prepare(Session *session, select_result *result,
 
378
               uint64_t additional_options);
470
379
  bool exec();
471
380
  bool cleanup();
472
381
  inline void unclean() { cleaned= 0; }
474
383
 
475
384
  void print(String *str, enum_query_type query_type);
476
385
 
477
 
  bool add_fake_select_lex(THD *thd);
478
 
  void init_prepare_fake_select_lex(THD *thd);
 
386
  bool add_fake_select_lex(Session *session);
 
387
  void init_prepare_fake_select_lex(Session *session);
479
388
  bool change_result(select_result_interceptor *result,
480
389
                     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 (); 
 
390
  void set_limit(Select_Lex *values);
 
391
  void set_session(Session *session_arg) { session= session_arg; }
 
392
  inline bool is_union ();
484
393
 
485
 
  friend void lex_start(THD *thd);
 
394
  friend void lex_start(Session *session);
486
395
  friend int subselect_union_engine::exec();
487
396
 
488
397
  List<Item> *get_unit_column_types();
489
398
};
490
399
 
491
 
typedef class st_select_lex_unit SELECT_LEX_UNIT;
492
 
 
493
400
/*
494
 
  SELECT_LEX - store information of parsed SELECT statment
 
401
  Select_Lex - store information of parsed SELECT statment
495
402
*/
496
 
class st_select_lex: public st_select_lex_node
 
403
class Select_Lex: public Select_Lex_Node
497
404
{
498
405
public:
499
406
  Name_resolution_context context;
500
407
  char *db;
501
 
  Item *where, *having;                         /* WHERE & HAVING clauses */
 
408
  /* An Item representing the WHERE clause */
 
409
  Item *where;
 
410
  /* An Item representing the HAVING clause */
 
411
  Item *having;
502
412
  /* Saved values of the WHERE and HAVING clauses*/
503
 
  Item::cond_result cond_value, having_value;
 
413
  Item::cond_result cond_value;
 
414
  Item::cond_result having_value;
504
415
  /* point on lex in which it was created, used in view subquery detection */
505
 
  st_lex *parent_lex;
 
416
  LEX *parent_lex;
506
417
  enum olap_type olap;
507
418
  /* FROM clause - points to the beginning of the TableList::next_local list. */
508
 
  SQL_LIST            table_list;
509
 
  SQL_LIST            group_list; /* GROUP BY clause. */
510
 
  List<Item>          item_list;  /* list of fields & expressions */
511
 
  List<String>        interval_list;
512
 
  bool                is_item_list_lookup;
513
 
  /* 
514
 
    Despite their names, the following are used in unions. This should 
515
 
    be rewritten. -Brian
516
 
  */
517
 
  List<Item_real_func> *ftfunc_list;
518
 
  List<Item_real_func> ftfunc_list_alloc;
 
419
  SQL_LIST table_list;
 
420
  SQL_LIST group_list; /* GROUP BY clause. */
 
421
  List<Item> item_list;  /* list of fields & expressions */
 
422
  List<String> interval_list;
 
423
  bool is_item_list_lookup;
519
424
  JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
520
425
  List<TableList> top_join_list; /* join list of the top level          */
521
426
  List<TableList> *join_list;    /* list for the currently parsed join  */
532
437
  SQL_LIST order_list;                /* ORDER clause */
533
438
  SQL_LIST *gorder_list;
534
439
  Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
535
 
  // Arrays of pointers to top elements of all_fields list
 
440
  /* Arrays of pointers to top elements of all_fields list */
536
441
  Item **ref_pointer_array;
537
442
 
538
443
  /*
543
448
  uint32_t select_n_having_items;
544
449
  uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
545
450
  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  */   
 
451
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
547
452
  /*
548
453
    Number of fields used in select list or where clause of current select
549
454
    and all inner subselects.
551
456
  uint32_t select_n_where_fields;
552
457
  enum_parsing_place parsing_place; /* where we are parsing expression */
553
458
  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
459
 
560
460
  uint32_t table_join_options;
561
461
  uint32_t in_sum_expr;
562
462
  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 */ 
 
463
  int8_t nest_level;     /* nesting level of select */
 
464
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
565
465
  uint32_t with_wild; /* item list contain '*' */
566
 
  bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
 
466
  bool braces;          /* SELECT ... UNION (SELECT ... ) <- this braces */
567
467
  /* true when having fix field called in processing of this SELECT */
568
468
  bool having_fix_field;
569
469
  /* List of references to fields referenced from inner selects */
589
489
  /* index in the select list of the expression currently being fixed */
590
490
  int cur_pos_in_select_list;
591
491
 
592
 
  List<udf_func>     udf_list;                  /* udf function calls stack */
593
 
  /* 
 
492
  /*
594
493
    This is a copy of the original JOIN USING list that comes from
595
494
    the parser. The parser :
596
495
      1. Sets the natural_join of the second TableList in the join
597
 
         and the st_select_lex::prev_join_using.
 
496
         and the Select_Lex::prev_join_using.
598
497
      2. Makes a parent TableList and sets its is_natural_join/
599
498
       join_using_fields members.
600
499
      3. Uses the wrapper TableList as a table in the upper level.
613
512
      1 - aggregate functions are used in this select,
614
513
          defined as SUM_FUNC_USED.
615
514
  */
616
 
  uint8_t full_group_by_flag;
 
515
  std::bitset<2> full_group_by_flag;
617
516
  void init_query();
618
517
  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()
 
518
  Select_Lex_Unit* master_unit();
 
519
  Select_Lex_Unit* first_inner_unit()
 
520
  {
 
521
    return (Select_Lex_Unit*) slave;
 
522
  }
 
523
  Select_Lex* outer_select();
 
524
  Select_Lex* next_select()
 
525
  {
 
526
    return (Select_Lex*) next;
 
527
  }
 
528
  Select_Lex* next_select_in_list()
 
529
  {
 
530
    return (Select_Lex*) link_next;
 
531
  }
 
532
  Select_Lex_Node** next_select_in_list_addr()
631
533
  {
632
534
    return &link_next;
633
535
  }
634
 
  st_select_lex* return_after_parsing()
 
536
  Select_Lex* return_after_parsing()
635
537
  {
636
538
    return master_unit()->return_after_parsing();
637
539
  }
638
540
 
639
 
  void mark_as_dependent(st_select_lex *last);
 
541
  void mark_as_dependent(Select_Lex *last);
640
542
 
641
543
  bool set_braces(bool value);
642
544
  bool inc_in_sum_expr();
643
545
  uint32_t get_in_sum_expr();
644
546
 
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,
649
 
                                LEX_STRING *alias,
650
 
                                uint32_t table_options,
651
 
                                thr_lock_type flags= TL_UNLOCK,
652
 
                                List<Index_hint> *hints= 0,
653
 
                                LEX_STRING *option= 0);
 
547
  bool add_item_to_list(Session *session, Item *item);
 
548
  bool add_group_to_list(Session *session, Item *item, bool asc);
 
549
  bool add_order_to_list(Session *session, Item *item, bool asc);
 
550
  TableList* add_table_to_list(Session *session,
 
551
                               Table_ident *table,
 
552
                               LEX_STRING *alias,
 
553
                               uint32_t table_options,
 
554
                               thr_lock_type flags= TL_UNLOCK,
 
555
                               List<Index_hint> *hints= 0,
 
556
                               LEX_STRING *option= 0);
654
557
  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);
 
558
  bool init_nested_join(Session *session);
 
559
  TableList *end_nested_join(Session *session);
 
560
  TableList *nest_last_join(Session *session);
658
561
  void add_joined_table(TableList *table);
659
562
  TableList *convert_right_join();
660
563
  List<Item>* get_item_list();
668
571
  }
669
572
  /*
670
573
    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
 
574
    used only if you are going remove all Select_Lex & units except belonger
672
575
    to LEX (LEX::unit & LEX::select, for other purposes there are
673
 
    SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
 
576
    Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
674
577
  */
675
 
  void cut_subtree() { slave= 0; }
 
578
  void cut_subtree()
 
579
  {
 
580
    slave= 0;
 
581
  }
676
582
  bool test_limit();
677
583
 
678
 
  friend void lex_start(THD *thd);
679
 
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
 
584
  friend void lex_start(Session *session);
 
585
  Select_Lex() : n_sum_items(0), n_child_sum_items(0) {}
680
586
  void make_empty_select()
681
587
  {
682
588
    init_query();
683
589
    init_select();
684
590
  }
685
 
  bool setup_ref_array(THD *thd, uint32_t order_group_num);
686
 
  void print(THD *thd, String *str, enum_query_type query_type);
 
591
  bool setup_ref_array(Session *session, uint32_t order_group_num);
 
592
  void print(Session *session, String *str, enum_query_type query_type);
687
593
  static void print_order(String *str,
688
594
                          order_st *order,
689
595
                          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);
 
596
  void print_limit(Session *session, String *str, enum_query_type query_type);
 
597
  void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
692
598
  /*
693
599
    Destroy the used execution plan (JOIN) of this subtree (this
694
 
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
 
600
    Select_Lex and all nested Select_Lexes and Select_Lex_Units).
695
601
  */
696
602
  bool cleanup();
697
603
  /*
702
608
 
703
609
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
704
610
 
705
 
  /* 
 
611
  /*
706
612
   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()) 
 
613
   hint will be the current ones (set by set_index_hint())
708
614
  */
709
 
  bool add_index_hint (THD *thd, char *str, uint32_t length);
 
615
  bool add_index_hint (Session *session, char *str, uint32_t length);
710
616
 
711
617
  /* make a list to hold index hints */
712
 
  void alloc_index_hints (THD *thd);
 
618
  void alloc_index_hints (Session *session);
713
619
  /* read and clear the index hints */
714
 
  List<Index_hint>* pop_index_hints(void) 
 
620
  List<Index_hint>* pop_index_hints(void)
715
621
  {
716
622
    List<Index_hint> *hints= index_hints;
717
623
    index_hints= NULL;
720
626
 
721
627
  void clear_index_hints(void) { index_hints= NULL; }
722
628
 
723
 
private:  
 
629
private:
724
630
  /* current index hint kind. used in filling up index_hints */
725
631
  enum index_hint_type current_index_hint_type;
726
632
  index_clause_map current_index_hint_clause;
727
633
  /* a list of USE/FORCE/IGNORE INDEX */
728
634
  List<Index_hint> *index_hints;
729
635
};
730
 
typedef class st_select_lex SELECT_LEX;
731
636
 
732
 
inline bool st_select_lex_unit::is_union ()
733
 
734
 
  return first_select()->next_select() && 
 
637
inline bool Select_Lex_Unit::is_union ()
 
638
{
 
639
  return first_select()->next_select() &&
735
640
    first_select()->next_select()->linkage == UNION_TYPE;
736
641
}
737
642
 
738
 
#define ALTER_ADD_COLUMN        (1L << 0)
739
 
#define ALTER_DROP_COLUMN       (1L << 1)
740
 
#define ALTER_CHANGE_COLUMN     (1L << 2)
741
 
#define ALTER_COLUMN_STORAGE    (1L << 3)
742
 
#define ALTER_COLUMN_FORMAT     (1L << 4)
743
 
#define ALTER_COLUMN_ORDER      (1L << 5)
744
 
#define ALTER_ADD_INDEX         (1L << 6)
745
 
#define ALTER_DROP_INDEX        (1L << 7)
746
 
#define ALTER_RENAME            (1L << 8)
747
 
#define ALTER_ORDER             (1L << 9)
748
 
#define ALTER_OPTIONS           (1L << 10)
749
 
#define ALTER_COLUMN_DEFAULT    (1L << 11)
750
 
#define ALTER_KEYS_ONOFF        (1L << 12)
751
 
#define ALTER_STORAGE           (1L << 13)
752
 
#define ALTER_ROW_FORMAT        (1L << 14)
753
 
#define ALTER_CONVERT           (1L << 15)
754
 
#define ALTER_FORCE             (1L << 16)
755
 
#define ALTER_RECREATE          (1L << 17)
756
 
#define ALTER_TABLE_REORG        (1L << 24)
757
 
#define ALTER_FOREIGN_KEY         (1L << 31)
 
643
enum enum_alter_info_flags
 
644
{
 
645
  ALTER_ADD_COLUMN= 0,
 
646
  ALTER_DROP_COLUMN,
 
647
  ALTER_CHANGE_COLUMN,
 
648
  ALTER_COLUMN_STORAGE,
 
649
  ALTER_COLUMN_FORMAT,
 
650
  ALTER_COLUMN_ORDER,
 
651
  ALTER_ADD_INDEX,
 
652
  ALTER_DROP_INDEX,
 
653
  ALTER_RENAME,
 
654
  ALTER_ORDER,
 
655
  ALTER_OPTIONS,
 
656
  ALTER_COLUMN_DEFAULT,
 
657
  ALTER_KEYS_ONOFF,
 
658
  ALTER_STORAGE,
 
659
  ALTER_ROW_FORMAT,
 
660
  ALTER_CONVERT,
 
661
  ALTER_FORCE,
 
662
  ALTER_RECREATE,
 
663
  ALTER_TABLE_REORG,
 
664
  ALTER_FOREIGN_KEY
 
665
};
758
666
 
759
667
/**
760
668
  @brief Parsing data for CREATE or ALTER Table.
766
674
class Alter_info
767
675
{
768
676
public:
769
 
  List<Alter_drop>              drop_list;
770
 
  List<Alter_column>            alter_list;
771
 
  List<Key>                     key_list;
772
 
  List<Create_field>            create_list;
773
 
  uint32_t                          flags;
774
 
  enum enum_enable_or_disable   keys_onoff;
775
 
  enum tablespace_op_type       tablespace_op;
776
 
  uint32_t                          no_parts;
777
 
  enum ha_build_method          build_method;
778
 
  Create_field                 *datetime_field;
779
 
  bool                          error_if_not_empty;
780
 
 
 
677
  List<Alter_drop> drop_list;
 
678
  List<Alter_column> alter_list;
 
679
  List<Key> key_list;
 
680
  List<CreateField> create_list;
 
681
  std::bitset<32> flags;
 
682
  enum enum_enable_or_disable keys_onoff;
 
683
  enum tablespace_op_type tablespace_op;
 
684
  uint32_t no_parts;
 
685
  enum ha_build_method build_method;
 
686
  CreateField *datetime_field;
 
687
  bool error_if_not_empty;
781
688
 
782
689
  Alter_info() :
783
 
    flags(0),
 
690
    flags(),
784
691
    keys_onoff(LEAVE_AS_IS),
785
692
    tablespace_op(NO_TABLESPACE_OP),
786
693
    no_parts(0),
795
702
    alter_list.empty();
796
703
    key_list.empty();
797
704
    create_list.empty();
798
 
    flags= 0;
 
705
    flags.reset();
799
706
    keys_onoff= LEAVE_AS_IS;
800
707
    tablespace_op= NO_TABLESPACE_OP;
801
708
    no_parts= 0;
809
716
  Alter_info(const Alter_info &rhs);            // not implemented
810
717
};
811
718
 
812
 
enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
813
 
                      XA_SUSPEND, XA_FOR_MIGRATE};
 
719
enum xa_option_words
 
720
{
 
721
  XA_NONE
 
722
, XA_JOIN
 
723
, XA_RESUME
 
724
, XA_ONE_PHASE
 
725
, XA_SUSPEND
 
726
, XA_FOR_MIGRATE
 
727
};
814
728
 
815
729
extern const LEX_STRING null_lex_str;
816
730
 
817
 
 
818
731
/*
819
732
  Class representing list of all tables used by statement.
820
733
  It also contains information about stored functions used by statement
825
738
  Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
826
739
  methods to save and restore this information.
827
740
*/
828
 
 
829
741
class Query_tables_list
830
742
{
831
743
public:
840
752
    0 - indicates that this query does not need prelocking.
841
753
  */
842
754
  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
755
 
862
756
  /*
863
757
    These constructor and destructor serve for creation/destruction
868
762
 
869
763
  /* Initializes (or resets) Query_tables_list object for "real" use. */
870
764
  void reset_query_tables_list(bool init);
871
 
  void destroy_query_tables_list();
872
 
  void set_query_tables_list(Query_tables_list *state)
873
 
  {
874
 
    *this= *state;
875
 
  }
876
765
 
877
766
  /*
878
767
    Direct addition to the list of query tables.
898
787
      query_tables_own_last= 0;
899
788
    }
900
789
  }
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
 
};
944
 
 
945
 
 
946
 
/*
947
 
  st_parsing_options contains the flags for constructions that are
948
 
  allowed in the current statement.
949
 
*/
950
 
 
951
 
struct st_parsing_options
952
 
{
953
 
  bool allows_variable;
954
 
  bool allows_select_into;
955
 
  bool allows_select_procedure;
956
 
  bool allows_derived;
957
 
 
958
 
  st_parsing_options() { reset(); }
959
 
  void reset();
960
 
};
961
 
 
 
790
};
962
791
 
963
792
/**
964
793
  The state of the lexical parser, when parsing comments.
983
812
  DISCARD_COMMENT
984
813
};
985
814
 
986
 
 
987
 
/**
988
 
  @brief This class represents the character input stream consumed during
989
 
  lexical analysis.
990
 
 
991
 
  In addition to consuming the input stream, this class performs some
992
 
  comment pre processing, by filtering out out of bound special text
993
 
  from the query input stream.
994
 
  Two buffers, with pointers inside each buffers, are maintained in
995
 
  parallel. The 'raw' buffer is the original query text, which may
996
 
  contain out-of-bound comments. The 'cpp' (for comments pre processor)
997
 
  is the pre-processed buffer that contains only the query text that
998
 
  should be seen once out-of-bound data is removed.
999
 
*/
1000
 
 
1001
 
class Lex_input_stream
1002
 
{
1003
 
public:
1004
 
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
1005
 
  ~Lex_input_stream();
1006
 
 
1007
 
  /**
1008
 
    Set the echo mode.
1009
 
 
1010
 
    When echo is true, characters parsed from the raw input stream are
1011
 
    preserved. When false, characters parsed are silently ignored.
1012
 
    @param echo the echo mode.
1013
 
  */
1014
 
  void set_echo(bool echo)
1015
 
  {
1016
 
    m_echo= echo;
1017
 
  }
1018
 
 
1019
 
  /**
1020
 
    Skip binary from the input stream.
1021
 
    @param n number of bytes to accept.
1022
 
  */
1023
 
  void skip_binary(int n)
1024
 
  {
1025
 
    if (m_echo)
1026
 
    {
1027
 
      memcpy(m_cpp_ptr, m_ptr, n);
1028
 
      m_cpp_ptr += n;
1029
 
    }
1030
 
    m_ptr += n;
1031
 
  }
1032
 
 
1033
 
  /**
1034
 
    Get a character, and advance in the stream.
1035
 
    @return the next character to parse.
1036
 
  */
1037
 
  char yyGet()
1038
 
  {
1039
 
    char c= *m_ptr++;
1040
 
    if (m_echo)
1041
 
      *m_cpp_ptr++ = c;
1042
 
    return c;
1043
 
  }
1044
 
 
1045
 
  /**
1046
 
    Get the last character accepted.
1047
 
    @return the last character accepted.
1048
 
  */
1049
 
  char yyGetLast()
1050
 
  {
1051
 
    return m_ptr[-1];
1052
 
  }
1053
 
 
1054
 
  /**
1055
 
    Look at the next character to parse, but do not accept it.
1056
 
  */
1057
 
  char yyPeek()
1058
 
  {
1059
 
    return m_ptr[0];
1060
 
  }
1061
 
 
1062
 
  /**
1063
 
    Look ahead at some character to parse.
1064
 
    @param n offset of the character to look up
1065
 
  */
1066
 
  char yyPeekn(int n)
1067
 
  {
1068
 
    return m_ptr[n];
1069
 
  }
1070
 
 
1071
 
  /**
1072
 
    Cancel the effect of the last yyGet() or yySkip().
1073
 
    Note that the echo mode should not change between calls to yyGet / yySkip
1074
 
    and yyUnget. The caller is responsible for ensuring that.
1075
 
  */
1076
 
  void yyUnget()
1077
 
  {
1078
 
    m_ptr--;
1079
 
    if (m_echo)
1080
 
      m_cpp_ptr--;
1081
 
  }
1082
 
 
1083
 
  /**
1084
 
    Accept a character, by advancing the input stream.
1085
 
  */
1086
 
  void yySkip()
1087
 
  {
1088
 
    if (m_echo)
1089
 
      *m_cpp_ptr++ = *m_ptr++;
1090
 
    else
1091
 
      m_ptr++;
1092
 
  }
1093
 
 
1094
 
  /**
1095
 
    Accept multiple characters at once.
1096
 
    @param n the number of characters to accept.
1097
 
  */
1098
 
  void yySkipn(int n)
1099
 
  {
1100
 
    if (m_echo)
1101
 
    {
1102
 
      memcpy(m_cpp_ptr, m_ptr, n);
1103
 
      m_cpp_ptr += n;
1104
 
    }
1105
 
    m_ptr += n;
1106
 
  }
1107
 
 
1108
 
  /**
1109
 
    End of file indicator for the query text to parse.
1110
 
    @return true if there are no more characters to parse
1111
 
  */
1112
 
  bool eof()
1113
 
  {
1114
 
    return (m_ptr >= m_end_of_query);
1115
 
  }
1116
 
 
1117
 
  /**
1118
 
    End of file indicator for the query text to parse.
1119
 
    @param n number of characters expected
1120
 
    @return true if there are less than n characters to parse
1121
 
  */
1122
 
  bool eof(int n)
1123
 
  {
1124
 
    return ((m_ptr + n) >= m_end_of_query);
1125
 
  }
1126
 
 
1127
 
  /** Get the raw query buffer. */
1128
 
  const char *get_buf()
1129
 
  {
1130
 
    return m_buf;
1131
 
  }
1132
 
 
1133
 
  /** Get the pre-processed query buffer. */
1134
 
  const char *get_cpp_buf()
1135
 
  {
1136
 
    return m_cpp_buf;
1137
 
  }
1138
 
 
1139
 
  /** Get the end of the raw query buffer. */
1140
 
  const char *get_end_of_query()
1141
 
  {
1142
 
    return m_end_of_query;
1143
 
  }
1144
 
 
1145
 
  /** Mark the stream position as the start of a new token. */
1146
 
  void start_token()
1147
 
  {
1148
 
    m_tok_start_prev= m_tok_start;
1149
 
    m_tok_start= m_ptr;
1150
 
    m_tok_end= m_ptr;
1151
 
 
1152
 
    m_cpp_tok_start_prev= m_cpp_tok_start;
1153
 
    m_cpp_tok_start= m_cpp_ptr;
1154
 
    m_cpp_tok_end= m_cpp_ptr;
1155
 
  }
1156
 
 
1157
 
  /**
1158
 
    Adjust the starting position of the current token.
1159
 
    This is used to compensate for starting whitespace.
1160
 
  */
1161
 
  void restart_token()
1162
 
  {
1163
 
    m_tok_start= m_ptr;
1164
 
    m_cpp_tok_start= m_cpp_ptr;
1165
 
  }
1166
 
 
1167
 
  /** Get the token start position, in the raw buffer. */
1168
 
  const char *get_tok_start()
1169
 
  {
1170
 
    return m_tok_start;
1171
 
  }
1172
 
 
1173
 
  /** Get the token start position, in the pre-processed buffer. */
1174
 
  const char *get_cpp_tok_start()
1175
 
  {
1176
 
    return m_cpp_tok_start;
1177
 
  }
1178
 
 
1179
 
  /** Get the token end position, in the raw buffer. */
1180
 
  const char *get_tok_end()
1181
 
  {
1182
 
    return m_tok_end;
1183
 
  }
1184
 
 
1185
 
  /** Get the token end position, in the pre-processed buffer. */
1186
 
  const char *get_cpp_tok_end()
1187
 
  {
1188
 
    return m_cpp_tok_end;
1189
 
  }
1190
 
 
1191
 
  /** Get the previous token start position, in the raw buffer. */
1192
 
  const char *get_tok_start_prev()
1193
 
  {
1194
 
    return m_tok_start_prev;
1195
 
  }
1196
 
 
1197
 
  /** Get the current stream pointer, in the raw buffer. */
1198
 
  const char *get_ptr()
1199
 
  {
1200
 
    return m_ptr;
1201
 
  }
1202
 
 
1203
 
  /** Get the current stream pointer, in the pre-processed buffer. */
1204
 
  const char *get_cpp_ptr()
1205
 
  {
1206
 
    return m_cpp_ptr;
1207
 
  }
1208
 
 
1209
 
  /** Get the length of the current token, in the raw buffer. */
1210
 
  uint32_t yyLength()
1211
 
  {
1212
 
    /*
1213
 
      The assumption is that the lexical analyser is always 1 character ahead,
1214
 
      which the -1 account for.
1215
 
    */
1216
 
    assert(m_ptr > m_tok_start);
1217
 
    return (uint32_t) ((m_ptr - m_tok_start) - 1);
1218
 
  }
1219
 
 
1220
 
  /** Get the utf8-body string. */
1221
 
  const char *get_body_utf8_str()
1222
 
  {
1223
 
    return m_body_utf8;
1224
 
  }
1225
 
 
1226
 
  /** Get the utf8-body length. */
1227
 
  uint32_t get_body_utf8_length()
1228
 
  {
1229
 
    return m_body_utf8_ptr - m_body_utf8;
1230
 
  }
1231
 
 
1232
 
  void body_utf8_start(THD *thd, const char *begin_ptr);
1233
 
  void body_utf8_append(const char *ptr);
1234
 
  void body_utf8_append(const char *ptr, const char *end_ptr);
1235
 
  void body_utf8_append_literal(THD *thd,
1236
 
                                const LEX_STRING *txt,
1237
 
                                const CHARSET_INFO * const txt_cs,
1238
 
                                const char *end_ptr);
1239
 
 
1240
 
  /** Current thread. */
1241
 
  THD *m_thd;
1242
 
 
1243
 
  /** Current line number. */
1244
 
  uint32_t yylineno;
1245
 
 
1246
 
  /** Length of the last token parsed. */
1247
 
  uint32_t yytoklen;
1248
 
 
1249
 
  /** Interface with bison, value of the last token parsed. */
1250
 
  LEX_YYSTYPE yylval;
1251
 
 
1252
 
  /** LALR(2) resolution, look ahead token.*/
1253
 
  int lookahead_token;
1254
 
 
1255
 
  /** LALR(2) resolution, value of the look ahead token.*/
1256
 
  LEX_YYSTYPE lookahead_yylval;
1257
 
 
1258
 
private:
1259
 
  /** Pointer to the current position in the raw input stream. */
1260
 
  const char *m_ptr;
1261
 
 
1262
 
  /** Starting position of the last token parsed, in the raw buffer. */
1263
 
  const char *m_tok_start;
1264
 
 
1265
 
  /** Ending position of the previous token parsed, in the raw buffer. */
1266
 
  const char *m_tok_end;
1267
 
 
1268
 
  /** End of the query text in the input stream, in the raw buffer. */
1269
 
  const char *m_end_of_query;
1270
 
 
1271
 
  /** Starting position of the previous token parsed, in the raw buffer. */
1272
 
  const char *m_tok_start_prev;
1273
 
 
1274
 
  /** Begining of the query text in the input stream, in the raw buffer. */
1275
 
  const char *m_buf;
1276
 
 
1277
 
  /** Length of the raw buffer. */
1278
 
  uint32_t m_buf_length;
1279
 
 
1280
 
  /** Echo the parsed stream to the pre-processed buffer. */
1281
 
  bool m_echo;
1282
 
 
1283
 
  /** Pre-processed buffer. */
1284
 
  char *m_cpp_buf;
1285
 
 
1286
 
  /** Pointer to the current position in the pre-processed input stream. */
1287
 
  char *m_cpp_ptr;
1288
 
 
1289
 
  /**
1290
 
    Starting position of the last token parsed,
1291
 
    in the pre-processed buffer.
1292
 
  */
1293
 
  const char *m_cpp_tok_start;
1294
 
 
1295
 
  /**
1296
 
    Starting position of the previous token parsed,
1297
 
    in the pre-procedded buffer.
1298
 
  */
1299
 
  const char *m_cpp_tok_start_prev;
1300
 
 
1301
 
  /**
1302
 
    Ending position of the previous token parsed,
1303
 
    in the pre-processed buffer.
1304
 
  */
1305
 
  const char *m_cpp_tok_end;
1306
 
 
1307
 
  /** UTF8-body buffer created during parsing. */
1308
 
  char *m_body_utf8;
1309
 
 
1310
 
  /** Pointer to the current position in the UTF8-body buffer. */
1311
 
  char *m_body_utf8_ptr;
1312
 
 
1313
 
  /**
1314
 
    Position in the pre-processed buffer. The query from m_cpp_buf to
1315
 
    m_cpp_utf_processed_ptr is converted to UTF8-body.
1316
 
  */
1317
 
  const char *m_cpp_utf8_processed_ptr;
1318
 
 
1319
 
public:
1320
 
 
1321
 
  /** Current state of the lexical analyser. */
1322
 
  enum my_lex_states next_state;
1323
 
 
1324
 
  /**
1325
 
    Position of ';' in the stream, to delimit multiple queries.
1326
 
    This delimiter is in the raw buffer.
1327
 
  */
1328
 
  const char *found_semicolon;
1329
 
 
1330
 
  /** Token character bitmaps, to detect 7bit strings. */
1331
 
  unsigned char tok_bitmap;
1332
 
 
1333
 
  /** SQL_MODE = IGNORE_SPACE. */
1334
 
  bool ignore_space;
1335
 
 
1336
 
  /** State of the lexical analyser for comments. */
1337
 
  enum_comment_state in_comment;
1338
 
 
1339
 
  /**
1340
 
    Starting position of the TEXT_STRING or IDENT in the pre-processed
1341
 
    buffer.
1342
 
 
1343
 
    NOTE: this member must be used within MYSQLlex() function only.
1344
 
  */
1345
 
  const char *m_cpp_text_start;
1346
 
 
1347
 
  /**
1348
 
    Ending position of the TEXT_STRING or IDENT in the pre-processed
1349
 
    buffer.
1350
 
 
1351
 
    NOTE: this member must be used within MYSQLlex() function only.
1352
 
    */
1353
 
  const char *m_cpp_text_end;
1354
 
 
1355
 
  /**
1356
 
    Character set specified by the character-set-introducer.
1357
 
 
1358
 
    NOTE: this member must be used within MYSQLlex() function only.
1359
 
  */
1360
 
  const CHARSET_INFO *m_underscore_cs;
1361
 
};
1362
 
 
1363
 
 
1364
 
/* The state of the lex parsing. This is saved in the THD struct */
1365
 
 
1366
 
typedef struct st_lex : public Query_tables_list
1367
 
{
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;
1374
 
 
1375
 
  char *length,*dec,*change;
 
815
#include "drizzled/lex_input_stream.h"
 
816
 
 
817
/* The state of the lex parsing. This is saved in the Session struct */
 
818
class LEX : public Query_tables_list
 
819
{
 
820
public:
 
821
  Select_Lex_Unit unit;                         /* most upper unit */
 
822
  Select_Lex select_lex;                        /* first Select_Lex */
 
823
  /* current Select_Lex in parsing */
 
824
  Select_Lex *current_select;
 
825
  /* list of all Select_Lex */
 
826
  Select_Lex *all_selects_list;
 
827
 
 
828
  /* This is the "scale" for DECIMAL (S,P) notation */ 
 
829
  char *length;
 
830
  /* This is the decimal precision in DECIMAL(S,P) notation */
 
831
  char *dec;
 
832
  /* The text in a CHANGE COLUMN clause in ALTER TABLE */
 
833
  char *change;
 
834
  
 
835
  /**
 
836
   * This is used kind of like the "ident" member variable below, as 
 
837
   * a place to store certain names of identifiers.  Unfortunately, it
 
838
   * is used differently depending on the Command (SELECT on a derived
 
839
   * table vs CREATE)
 
840
   */
1376
841
  LEX_STRING name;
1377
 
  char *help_arg;
1378
 
  char* to_log;                                 /* For PURGE MASTER LOGS TO */
1379
 
  char* x509_subject,*x509_issuer,*ssl_cipher;
 
842
  /* The string literal used in a LIKE expression */
1380
843
  String *wild;
1381
 
  sql_exchange *exchange;
 
844
  file_exchange *exchange;
1382
845
  select_result *result;
1383
 
  Item *default_value, *on_update_value;
1384
 
  LEX_STRING comment, ident;
1385
 
  XID *xid;
 
846
 
 
847
  /* An item representing the DEFAULT clause in CREATE/ALTER TABLE */
 
848
  Item *default_value;
 
849
  /* An item representing the ON UPDATE clause in CREATE/ALTER TABLE */
 
850
  Item *on_update_value;
 
851
  /* Not really sure what exactly goes in here... Comment text at beginning of statement? */
 
852
  LEX_STRING comment;
 
853
 
 
854
  /**
 
855
   * This is current used to store the name of a named key cache
 
856
   * or a named savepoint.  It should probably be refactored out into
 
857
   * the eventual Command class built for the Keycache and Savepoint
 
858
   * commands.
 
859
   */ 
 
860
  LEX_STRING ident;
 
861
 
1386
862
  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];
1392
 
 
 
863
  /* The owning Session of this LEX */
 
864
  Session *session;
1393
865
  const CHARSET_INFO *charset;
1394
866
  bool text_string_is_7bit;
1395
867
  /* store original leaf_tables for INSERT SELECT and PS/SP */
1398
870
  List<Key_part_spec> col_list;
1399
871
  List<Key_part_spec> ref_list;
1400
872
  List<String>        interval_list;
1401
 
  List<LEX_COLUMN>    columns;
 
873
  List<Lex_Column>    columns;
1402
874
  List<Item>          *insert_list,field_list,value_list,update_list;
1403
875
  List<List_item>     many_values;
1404
876
  List<set_var_base>  var_list;
1417
889
    required a local context, the parser pops the top-most context.
1418
890
  */
1419
891
  List<Name_resolution_context> context_stack;
1420
 
  List<LEX_STRING>     db_list;
1421
892
 
1422
 
  SQL_LIST            proc_list, auxiliary_table_list, save_list;
1423
 
  Create_field        *last_field;
 
893
  SQL_LIST auxiliary_table_list;
 
894
  SQL_LIST save_list;
 
895
  CreateField *last_field;
1424
896
  Item_sum *in_sum_func;
1425
 
  udf_func udf;
1426
 
  HA_CHECK_OPT   check_opt;                     // check/repair options
 
897
  drizzled::plugin::Function *udf;
 
898
  HA_CHECK_OPT check_opt;                       // check/repair options
1427
899
  HA_CREATE_INFO create_info;
 
900
  drizzled::message::Table *create_table_proto;
 
901
  StorageEngine *show_engine;
1428
902
  KEY_CREATE_INFO key_create_info;
1429
 
  LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
1430
 
  LEX_SERVER_OPTIONS server_options;
1431
903
  uint32_t type;
1432
904
  /*
1433
905
    This variable is used in post-parse stage to declare that sum-functions,
1440
912
  */
1441
913
  nesting_map allow_sum_func;
1442
914
  enum_sql_command sql_command;
 
915
  drizzled::statement::Statement *statement;
1443
916
  /*
1444
917
    Usually `expr` rule of yacc is quite reused but some commands better
1445
918
    not support subqueries which comes standard with this rule, like
1455
928
  union {
1456
929
    enum ha_rkey_function ha_rkey_mode;
1457
930
    enum xa_option_words xa_opt;
1458
 
    bool lock_transactional;            /* For LOCK Table ... IN ... MODE */
1459
931
  };
1460
932
  enum enum_var_type option_type;
1461
933
 
1462
 
  uint32_t profile_query_id;
1463
 
  uint32_t profile_options;
1464
934
  enum column_format_type column_format;
1465
 
  uint32_t which_columns;
1466
935
  enum Foreign_key::fk_match_opt fk_match_option;
1467
936
  enum Foreign_key::fk_option fk_update_opt;
1468
937
  enum Foreign_key::fk_option fk_delete_opt;
1469
 
  uint32_t slave_thd_opt, start_transaction_opt;
 
938
  /* Options used in START TRANSACTION statement */
 
939
  uint32_t start_transaction_opt;
1470
940
  int nest_level;
1471
 
  /*
1472
 
    In LEX representing update which were transformed to multi-update
1473
 
    stores total number of tables. For LEX representing multi-delete
1474
 
    holds number of tables from which we will delete records.
1475
 
  */
1476
 
  uint32_t table_count;
1477
941
  uint8_t describe;
1478
942
  /*
1479
943
    A flag that indicates what kinds of derived tables are present in the
1480
944
    query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
1481
945
  */
1482
946
  uint8_t derived_tables;
1483
 
  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
1484
 
  bool autocommit;
 
947
 
 
948
  /* True if "IF EXISTS" used in DROP statement */
 
949
  bool drop_if_exists;
 
950
  /* True if "TEMPORARY" used in DROP/CREATE statement */
 
951
  bool drop_temporary;
 
952
  bool one_shot_set;
 
953
 
 
954
  /* Only true when FULL symbol is found (e.g. SHOW FULL PROCESSLIST) */
1485
955
  bool verbose;
1486
 
 
1487
 
  bool tx_chain, tx_release;
1488
 
  bool subqueries, ignore;
1489
 
  st_parsing_options parsing_options;
 
956
  
 
957
  /* Was the CHAIN option using in COMMIT/ROLLBACK? */
 
958
  bool tx_chain;
 
959
  /* Was the RELEASE option used in COMMIT/ROLLBACK? */
 
960
  bool tx_release;
 
961
  /* Was the IGNORE symbol found in statement */
 
962
  bool ignore;
1490
963
  Alter_info alter_info;
1491
964
 
1492
965
  /*
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
966
    Pointers to part of LOAD DATA statement that should be rewritten
1500
967
    during replication ("LOCAL 'filename' REPLACE INTO" part).
1501
968
  */
1502
969
  const char *fname_start;
1503
970
  const char *fname_end;
1504
 
  
 
971
 
1505
972
  /**
1506
 
    During name resolution search only in the table list given by 
 
973
    During name resolution search only in the table list given by
1507
974
    Name_resolution_context::first_name_resolution_table and
1508
975
    Name_resolution_context::last_name_resolution_table
1509
 
    (see Item_field::fix_fields()). 
 
976
    (see Item_field::fix_fields()).
1510
977
  */
1511
978
  bool use_only_table_context;
1512
 
  
 
979
 
 
980
  /* Was the ESCAPE keyword used? */
1513
981
  bool escape_used;
1514
982
  bool is_lex_started; /* If lex_start() did run. For debugging. */
1515
983
 
1516
 
  st_lex();
 
984
  LEX();
1517
985
 
1518
 
  virtual ~st_lex()
 
986
  /* Note that init and de-init mostly happen in lex_start and lex_end
 
987
     and not here. This is because LEX isn't delete/new for each new
 
988
     statement in a session. It's re-used by doing lex_end, lex_start
 
989
     in sql_lex.cc
 
990
  */
 
991
  virtual ~LEX()
1519
992
  {
1520
 
    destroy_query_tables_list();
1521
 
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1522
 
    delete_dynamic(&plugins);
1523
993
  }
1524
994
 
1525
995
  TableList *unlink_first_table(bool *link_to_local);
1526
996
  void link_first_table_back(TableList *first, bool link_to_local);
1527
997
  void first_lists_tables_same();
1528
998
 
1529
 
  bool can_be_merged();
1530
 
  bool can_use_merged();
1531
 
  bool can_not_use_merged();
1532
999
  bool only_view_structure();
1533
1000
  bool need_correct_ident();
1534
1001
 
1551
1018
    return context_stack.head();
1552
1019
  }
1553
1020
  /*
1554
 
    Restore the LEX and THD in case of a parse error.
 
1021
    Restore the LEX and Session in case of a parse error.
1555
1022
  */
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();
 
1023
  static void cleanup_lex_after_parse_error(Session *session);
1562
1024
 
1563
1025
  /**
1564
1026
    @brief check if the statement is a single-level join
1565
1027
    @return result of the check
1566
 
      @retval true  The statement doesn't contain subqueries, unions and 
 
1028
      @retval true  The statement doesn't contain subqueries, unions and
1567
1029
                    stored procedure calls.
1568
1030
      @retval false There are subqueries, UNIONs or stored procedure calls.
1569
1031
  */
1570
 
  bool is_single_level_stmt() 
1571
 
  { 
1572
 
    /* 
 
1032
  bool is_single_level_stmt()
 
1033
  {
 
1034
    /*
1573
1035
      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 
 
1036
      on its top. So select_lex (as the first added) will be at the tail
1575
1037
      of the list.
1576
 
    */ 
1577
 
    if (&select_lex == all_selects_list && !sroutines.records)
 
1038
    */
 
1039
    if (&select_lex == all_selects_list)
1578
1040
    {
1579
1041
      assert(!all_selects_list->next_select_in_list());
1580
1042
      return true;
1581
1043
    }
1582
1044
    return false;
1583
1045
  }
1584
 
} LEX;
1585
 
 
1586
 
struct st_lex_local: public st_lex
1587
 
{
1588
 
  static void *operator new(size_t size) throw()
1589
 
  {
1590
 
    return sql_alloc(size);
1591
 
  }
1592
 
  static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
1593
 
  {
1594
 
    return (void*) alloc_root(mem_root, (uint32_t) size);
1595
 
  }
1596
 
  static void operator delete(void *ptr __attribute__((unused)),
1597
 
                              size_t size __attribute__((unused)))
1598
 
  { TRASH(ptr, size); }
1599
 
  static void operator delete(void *ptr __attribute__((unused)),
1600
 
                              MEM_ROOT *mem_root __attribute__((unused)))
1601
 
  { /* Never called */ }
1602
1046
};
1603
1047
 
1604
 
extern void lex_init(void);
1605
 
extern void lex_free(void);
1606
 
extern void lex_start(THD *thd);
 
1048
extern void lex_start(Session *session);
1607
1049
extern void lex_end(LEX *lex);
1608
 
 
1609
1050
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
1610
 
 
1611
1051
extern bool is_lex_native_function(const LEX_STRING *name);
1612
1052
 
1613
1053
/**
1615
1055
*/
1616
1056
 
1617
1057
#endif /* DRIZZLE_SERVER */
1618
 
#endif /* DRIZZLE_SERVER_SQL_LEX_H */
 
1058
#endif /* DRIZZLED_SQL_LEX_H */