~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory

Show diffs side-by-side

added added

removed removed

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