~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
243.1.8 by Jay Pipes
More cleanup and revert broken base.h
16
#ifndef DRIZZLE_SERVER_SQL_LEX_H
17
#define DRIZZLE_SERVER_SQL_LEX_H
18
1 by brian
clean slate
19
/**
20
  @defgroup Semantic_Analysis Semantic Analysis
21
*/
22
23
/* YACC and LEX Definitions */
24
25
/* These may not be declared yet */
26
class Table_ident;
27
class sql_exchange;
28
class LEX_COLUMN;
29
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
30
#ifdef DRIZZLE_SERVER
1 by brian
clean slate
31
/*
32
  The following hack is needed because mysql_yacc.cc does not define
33
  YYSTYPE before including this file
34
*/
35
36
#include "set_var.h"
37
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
38
#ifdef DRIZZLE_YACC
1 by brian
clean slate
39
#define LEX_YYSTYPE void *
40
#else
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
41
#if defined(DRIZZLE_LEX)
1 by brian
clean slate
42
#include "lex_symbol.h"
43
#include "sql_yacc.h"
44
#define LEX_YYSTYPE YYSTYPE *
45
#else
46
#define LEX_YYSTYPE void *
47
#endif
48
#endif
49
#endif
50
51
/*
52
  When a command is added here, be sure it's also added in mysqld.cc
53
  in "struct show_var_st status_vars[]= {" ...
54
55
  If the command returns a result set or is not allowed in stored
56
  functions or triggers, please also make sure that
57
  sp_get_flags_for_command (sp_head.cc) returns proper flags for the
58
  added SQLCOM_.
59
*/
60
61
enum enum_sql_command {
62
  SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
63
  SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
64
  SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
65
  SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
66
  SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
67
  SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
68
  SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
69
  SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
70
  SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
71
  SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
72
  SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
73
  SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
74
  SQLCOM_OPTIMIZE, SQLCOM_CHECK,
59 by Brian Aker
Removed preload from parser.
75
  SQLCOM_ASSIGN_TO_KEYCACHE,
1 by brian
clean slate
76
  SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
77
  SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
78
  SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
79
  SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
80
  SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
81
  SQLCOM_RENAME_TABLE,  
82
  SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
83
  SQLCOM_SHOW_OPEN_TABLES,
84
  SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
12 by Brian Aker
More dead code removal.
85
  SQLCOM_SHOW_BINLOG_EVENTS,
1 by brian
clean slate
86
  SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
87
  SQLCOM_CHECKSUM,
12.1.2 by Brian Aker
Removing dead state calls.
88
  SQLCOM_BINLOG_BASE64_EVENT,
1 by brian
clean slate
89
  SQLCOM_SHOW_PLUGINS,
90
  /*
91
    When a command is added here, be sure it's also added in mysqld.cc
92
    in "struct show_var_st status_vars[]= {" ...
93
  */
94
  /* This should be the last !!! */
95
  SQLCOM_END
96
};
97
98
// describe/explain types
99
#define DESCRIBE_NORMAL		1
100
#define DESCRIBE_EXTENDED	2
101
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
102
#ifdef DRIZZLE_SERVER
1 by brian
clean slate
103
104
#define DERIVED_NONE	0
105
#define DERIVED_SUBQUERY	1
106
107
typedef List<Item> List_item;
108
109
/* SERVERS CACHE CHANGES */
110
typedef struct st_lex_server_options
111
{
202 by Brian Aker
Cleanup sql_lex to modern types.
112
  int32_t port;
113
  uint32_t server_name_length;
185 by Brian Aker
More dead code baby... it has become a little like drilling for oil. Alaska
114
  char *server_name, *host, *db, *username, *password, *scheme, *owner;
1 by brian
clean slate
115
} LEX_SERVER_OPTIONS;
116
117
typedef struct st_lex_master_info
118
{
119
  char *host, *user, *password, *log_file_name;
202 by Brian Aker
Cleanup sql_lex to modern types.
120
  uint32_t port, connect_retry;
1 by brian
clean slate
121
  float heartbeat_period;
151 by Brian Aker
Ulonglong to uint64_t
122
  uint64_t pos;
202 by Brian Aker
Cleanup sql_lex to modern types.
123
  uint32_t server_id;
1 by brian
clean slate
124
  /*
125
    Enum is used for making it possible to detect if the user
126
    changed variable or if it should be left at old value
127
   */
128
  enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE}
129
    ssl, ssl_verify_server_cert, heartbeat_opt;
130
  char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
131
  char *relay_log_name;
202 by Brian Aker
Cleanup sql_lex to modern types.
132
  uint32_t relay_log_pos;
1 by brian
clean slate
133
} LEX_MASTER_INFO;
134
135
136
enum sub_select_type
137
{
138
  UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
139
  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
140
};
141
142
enum olap_type 
143
{
144
  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
145
};
146
147
enum tablespace_op_type
148
{
149
  NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
150
};
151
152
/* 
153
  String names used to print a statement with index hints.
154
  Keep in sync with index_hint_type.
155
*/
156
extern const char * index_hint_type_name[];
157
typedef uchar index_clause_map;
158
159
/*
160
  Bits in index_clause_map : one for each possible FOR clause in
161
  USE/FORCE/IGNORE INDEX index hint specification
162
*/
163
#define INDEX_HINT_MASK_JOIN  (1)
164
#define INDEX_HINT_MASK_GROUP (1 << 1)
165
#define INDEX_HINT_MASK_ORDER (1 << 2)
166
167
#define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
168
                             INDEX_HINT_MASK_ORDER)
169
170
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint  */
171
class Index_hint : public Sql_alloc
172
{
173
public:
174
  /* The type of the hint : USE/FORCE/IGNORE */
175
  enum index_hint_type type;
176
  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
177
  index_clause_map clause;
178
  /* 
179
    The index name. Empty (str=NULL) name represents an empty list 
180
    USE INDEX () clause 
181
  */ 
182
  LEX_STRING key_name;
183
184
  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
202 by Brian Aker
Cleanup sql_lex to modern types.
185
              char *str, uint32_t length) :
1 by brian
clean slate
186
    type(type_arg), clause(clause_arg)
187
  {
188
    key_name.str= str;
189
    key_name.length= length;
190
  }
191
192
  void print(THD *thd, String *str);
193
}; 
194
195
/* 
196
  The state of the lex parsing for selects 
197
   
198
   master and slaves are pointers to select_lex.
199
   master is pointer to upper level node.
200
   slave is pointer to lower level node
201
   select_lex is a SELECT without union
202
   unit is container of either
203
     - One SELECT
204
     - UNION of selects
205
   select_lex and unit are both inherited form select_lex_node
206
   neighbors are two select_lex or units on the same level
207
208
   All select describing structures linked with following pointers:
209
   - list of neighbors (next/prev) (prev of first element point to slave
210
     pointer of upper structure)
211
     - For select this is a list of UNION's (or one element list)
212
     - For units this is a list of sub queries for the upper level select
213
214
   - pointer to master (master), which is
215
     If this is a unit
216
       - pointer to outer select_lex
217
     If this is a select_lex
218
       - pointer to outer unit structure for select
219
220
   - pointer to slave (slave), which is either:
221
     If this is a unit:
222
       - first SELECT that belong to this unit
223
     If this is a select_lex
224
       - first unit that belong to this SELECT (subquries or derived tables)
225
226
   - list of all select_lex (link_next/link_prev)
227
     This is to be used for things like derived tables creation, where we
228
     go through this list and create the derived tables.
229
230
   If unit contain several selects (UNION now, INTERSECT etc later)
231
   then it have special select_lex called fake_select_lex. It used for
232
   storing global parameters (like ORDER BY, LIMIT) and executing union.
233
   Subqueries used in global ORDER BY clause will be attached to this
234
   fake_select_lex, which will allow them correctly resolve fields of
235
   'upper' UNION and outer selects.
236
237
   For example for following query:
238
239
   select *
240
     from table1
241
     where table1.field IN (select * from table1_1_1 union
242
                            select * from table1_1_2)
243
     union
244
   select *
245
     from table2
246
     where table2.field=(select (select f1 from table2_1_1_1_1
247
                                   where table2_1_1_1_1.f2=table2_1_1.f3)
248
                           from table2_1_1
249
                           where table2_1_1.f1=table2.f2)
250
     union
251
   select * from table3;
252
253
   we will have following structure:
254
255
   select1: (select * from table1 ...)
256
   select2: (select * from table2 ...)
257
   select3: (select * from table3)
258
   select1.1.1: (select * from table1_1_1)
259
   ...
260
261
     main unit
262
     fake0
263
     select1 select2 select3
264
     |^^     |^
265
    s|||     ||master
266
    l|||     |+---------------------------------+
267
    a|||     +---------------------------------+|
268
    v|||master                         slave   ||
269
    e||+-------------------------+             ||
270
     V|            neighbor      |             V|
271
     unit1.1<+==================>unit1.2       unit2.1
272
     fake1.1
273
     select1.1.1 select 1.1.2    select1.2.1   select2.1.1
274
                                               |^
275
                                               ||
276
                                               V|
277
                                               unit2.1.1.1
278
                                               select2.1.1.1.1
279
280
281
   relation in main unit will be following:
282
   (bigger picture for:
283
      main unit
284
      fake0
285
      select1 select2 select3
286
   in the above picture)
287
288
         main unit
289
         |^^^^|fake_select_lex
290
         |||||+--------------------------------------------+
291
         ||||+--------------------------------------------+|
292
         |||+------------------------------+              ||
293
         ||+--------------+                |              ||
294
    slave||master         |                |              ||
295
         V|      neighbor |       neighbor |        master|V
296
         select1<========>select2<========>select3        fake0
297
298
    list of all select_lex will be following (as it will be constructed by
299
    parser):
300
301
    select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
302
                                                                          |
303
    +---------------------------------------------------------------------+
304
    |
305
    +->select1.1.1->select1.1.2
306
307
*/
308
309
/* 
310
    Base class for st_select_lex (SELECT_LEX) & 
311
    st_select_lex_unit (SELECT_LEX_UNIT)
312
*/
313
struct st_lex;
314
class st_select_lex;
315
class st_select_lex_unit;
316
class st_select_lex_node {
317
protected:
318
  st_select_lex_node *next, **prev,   /* neighbor list */
319
    *master, *slave,                  /* vertical links */
320
    *link_next, **link_prev;          /* list of whole SELECT_LEX */
321
public:
322
151 by Brian Aker
Ulonglong to uint64_t
323
  uint64_t options;
1 by brian
clean slate
324
325
  /*
326
    result of this query can't be cached, bit field, can be :
327
      UNCACHEABLE_DEPENDENT
328
      UNCACHEABLE_RAND
329
      UNCACHEABLE_SIDEEFFECT
330
      UNCACHEABLE_EXPLAIN
331
      UNCACHEABLE_PREPARE
332
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
333
  uint8_t uncacheable;
1 by brian
clean slate
334
  enum sub_select_type linkage;
335
  bool no_table_names_allowed; /* used for global order by */
336
  bool no_error; /* suppress error message (convert it to warnings) */
337
338
  static void *operator new(size_t size)
339
  {
340
    return sql_alloc(size);
341
  }
342
  static void *operator new(size_t size, MEM_ROOT *mem_root)
202 by Brian Aker
Cleanup sql_lex to modern types.
343
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
344
  static void operator delete(void *ptr __attribute__((unused)),
345
                              size_t size __attribute__((unused)))
77.1.7 by Monty Taylor
Heap builds clean.
346
  { TRASH(ptr, size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
347
  static void operator delete(void *ptr __attribute__((unused)),
348
                              MEM_ROOT *mem_root __attribute__((unused)))
77.1.7 by Monty Taylor
Heap builds clean.
349
  {}
1 by brian
clean slate
350
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
351
  virtual ~st_select_lex_node() {}
352
  inline st_select_lex_node* get_master() { return master; }
353
  virtual void init_query();
354
  virtual void init_select();
355
  void include_down(st_select_lex_node *upper);
356
  void include_neighbour(st_select_lex_node *before);
357
  void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
358
  void include_global(st_select_lex_node **plink);
359
  void exclude();
360
361
  virtual st_select_lex_unit* master_unit()= 0;
362
  virtual st_select_lex* outer_select()= 0;
363
  virtual st_select_lex* return_after_parsing()= 0;
364
365
  virtual bool set_braces(bool value);
366
  virtual bool inc_in_sum_expr();
202 by Brian Aker
Cleanup sql_lex to modern types.
367
  virtual uint32_t get_in_sum_expr();
327.2.4 by Brian Aker
Refactoring table.h
368
  virtual TableList* get_table_list();
1 by brian
clean slate
369
  virtual List<Item>* get_item_list();
202 by Brian Aker
Cleanup sql_lex to modern types.
370
  virtual uint32_t get_table_join_options();
327.2.4 by Brian Aker
Refactoring table.h
371
  virtual TableList *add_table_to_list(THD *thd, Table_ident *table,
77.1.7 by Monty Taylor
Heap builds clean.
372
                                        LEX_STRING *alias,
202 by Brian Aker
Cleanup sql_lex to modern types.
373
                                        uint32_t table_options,
77.1.7 by Monty Taylor
Heap builds clean.
374
                                        thr_lock_type flags= TL_UNLOCK,
375
                                        List<Index_hint> *hints= 0,
1 by brian
clean slate
376
                                        LEX_STRING *option= 0);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
377
  virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
77.1.7 by Monty Taylor
Heap builds clean.
378
  {}
1 by brian
clean slate
379
380
  friend class st_select_lex_unit;
381
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
382
private:
383
  void fast_exclude();
384
};
385
typedef class st_select_lex_node SELECT_LEX_NODE;
386
387
/* 
388
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
389
   SELECT_LEXs
390
*/
391
class THD;
392
class select_result;
393
class JOIN;
394
class select_union;
395
class st_select_lex_unit: public st_select_lex_node {
396
protected:
327.2.4 by Brian Aker
Refactoring table.h
397
  TableList result_table_list;
1 by brian
clean slate
398
  select_union *union_result;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
399
  Table *table; /* temporary table using for appending UNION results */
1 by brian
clean slate
400
401
  select_result *result;
151 by Brian Aker
Ulonglong to uint64_t
402
  uint64_t found_rows_for_union;
1 by brian
clean slate
403
  bool saved_error;
404
405
public:
406
  bool  prepared, // prepare phase already performed for UNION (unit)
407
    optimized, // optimize phase already performed for UNION (unit)
408
    executed, // already executed
409
    cleaned;
410
411
  // list of fields which points to temporary table for union
412
  List<Item> item_list;
413
  /*
414
    list of types of items inside union (used for union & derived tables)
415
    
416
    Item_type_holders from which this list consist may have pointers to Field,
417
    pointers is valid only after preparing SELECTS of this unit and before
418
    any SELECT of this unit execution
419
420
    TODO:
421
    Possibly this member should be protected, and its direct use replaced
422
    by get_unit_column_types(). Check the places where it is used.
423
  */
424
  List<Item> types;
425
  /*
426
    Pointer to 'last' select or pointer to unit where stored
427
    global parameters for union
428
  */
429
  st_select_lex *global_parameters;
430
  //node on wich we should return current_select pointer after parsing subquery
431
  st_select_lex *return_to;
432
  /* LIMIT clause runtime counters */
433
  ha_rows select_limit_cnt, offset_limit_cnt;
434
  /* not NULL if unit used in subselect, point to subselect item */
435
  Item_subselect *item;
436
  /* thread handler */
437
  THD *thd;
438
  /*
439
    SELECT_LEX for hidden SELECT in onion which process global
440
    ORDER BY and LIMIT
441
  */
442
  st_select_lex *fake_select_lex;
443
444
  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
445
  bool describe; /* union exec() called for EXPLAIN */
446
447
  void init_query();
448
  st_select_lex_unit* master_unit();
449
  st_select_lex* outer_select();
450
  st_select_lex* first_select()
451
  {
452
    return my_reinterpret_cast(st_select_lex*)(slave);
453
  }
454
  st_select_lex_unit* next_unit()
455
  {
456
    return my_reinterpret_cast(st_select_lex_unit*)(next);
457
  }
458
  st_select_lex* return_after_parsing() { return return_to; }
459
  void exclude_level();
460
  void exclude_tree();
461
462
  /* UNION methods */
202 by Brian Aker
Cleanup sql_lex to modern types.
463
  bool prepare(THD *thd, select_result *result, uint32_t additional_options);
1 by brian
clean slate
464
  bool exec();
465
  bool cleanup();
466
  inline void unclean() { cleaned= 0; }
467
  void reinit_exec_mechanism();
468
469
  void print(String *str, enum_query_type query_type);
470
471
  bool add_fake_select_lex(THD *thd);
472
  void init_prepare_fake_select_lex(THD *thd);
473
  bool change_result(select_result_interceptor *result,
474
                     select_result_interceptor *old_result);
475
  void set_limit(st_select_lex *values);
476
  void set_thd(THD *thd_arg) { thd= thd_arg; }
477
  inline bool is_union (); 
478
479
  friend void lex_start(THD *thd);
480
  friend int subselect_union_engine::exec();
481
482
  List<Item> *get_unit_column_types();
483
};
484
485
typedef class st_select_lex_unit SELECT_LEX_UNIT;
486
487
/*
488
  SELECT_LEX - store information of parsed SELECT statment
489
*/
490
class st_select_lex: public st_select_lex_node
491
{
492
public:
493
  Name_resolution_context context;
494
  char *db;
495
  Item *where, *having;                         /* WHERE & HAVING clauses */
496
  /* Saved values of the WHERE and HAVING clauses*/
497
  Item::cond_result cond_value, having_value;
498
  /* point on lex in which it was created, used in view subquery detection */
499
  st_lex *parent_lex;
500
  enum olap_type olap;
327.2.4 by Brian Aker
Refactoring table.h
501
  /* FROM clause - points to the beginning of the TableList::next_local list. */
1 by brian
clean slate
502
  SQL_LIST	      table_list;
503
  SQL_LIST	      group_list; /* GROUP BY clause. */
504
  List<Item>          item_list;  /* list of fields & expressions */
505
  List<String>        interval_list;
506
  bool	              is_item_list_lookup;
507
  /* 
508
    Despite their names, the following are used in unions. This should 
509
    be rewritten. -Brian
510
  */
511
  List<Item_real_func> *ftfunc_list;
512
  List<Item_real_func> ftfunc_list_alloc;
513
  JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
327.2.4 by Brian Aker
Refactoring table.h
514
  List<TableList> top_join_list; /* join list of the top level          */
515
  List<TableList> *join_list;    /* list for the currently parsed join  */
516
  TableList *embedding;          /* table embedding to the above list   */
517
  List<TableList> sj_nests;
1 by brian
clean slate
518
  /*
519
    Beginning of the list of leaves in a FROM clause, where the leaves
520
    inlcude all base tables including view tables. The tables are connected
327.2.4 by Brian Aker
Refactoring table.h
521
    by TableList::next_leaf, so leaf_tables points to the left-most leaf.
1 by brian
clean slate
522
  */
327.2.4 by Brian Aker
Refactoring table.h
523
  TableList *leaf_tables;
1 by brian
clean slate
524
  const char *type;               /* type of select for EXPLAIN          */
525
526
  SQL_LIST order_list;                /* ORDER clause */
527
  SQL_LIST *gorder_list;
528
  Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
529
  // Arrays of pointers to top elements of all_fields list
530
  Item **ref_pointer_array;
531
532
  /*
533
    number of items in select_list and HAVING clause used to get number
534
    bigger then can be number of entries that will be added to all item
535
    list during split_sum_func
536
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
537
  uint32_t select_n_having_items;
538
  uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
539
  uint32_t between_count; /* number of between predicates in where/having/on      */
540
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */   
1 by brian
clean slate
541
  /*
542
    Number of fields used in select list or where clause of current select
543
    and all inner subselects.
544
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
545
  uint32_t select_n_where_fields;
1 by brian
clean slate
546
  enum_parsing_place parsing_place; /* where we are parsing expression */
547
  bool with_sum_func;   /* sum function indicator */
548
  /* 
549
    PS or SP cond natural joins was alredy processed with permanent
550
    arena and all additional items which we need alredy stored in it
551
  */
552
  bool conds_processed_with_permanent_arena;
553
202 by Brian Aker
Cleanup sql_lex to modern types.
554
  uint32_t table_join_options;
555
  uint32_t in_sum_expr;
556
  uint32_t select_number; /* number of select (used for EXPLAIN) */
1 by brian
clean slate
557
  int nest_level;     /* nesting level of select */
558
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */ 
202 by Brian Aker
Cleanup sql_lex to modern types.
559
  uint32_t with_wild; /* item list contain '*' */
1 by brian
clean slate
560
  bool  braces;   	/* SELECT ... UNION (SELECT ... ) <- this braces */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
561
  /* true when having fix field called in processing of this SELECT */
1 by brian
clean slate
562
  bool having_fix_field;
563
  /* List of references to fields referenced from inner selects */
564
  List<Item_outer_ref> inner_refs_list;
565
  /* Number of Item_sum-derived objects in this SELECT */
202 by Brian Aker
Cleanup sql_lex to modern types.
566
  uint32_t n_sum_items;
1 by brian
clean slate
567
  /* Number of Item_sum-derived objects in children and descendant SELECTs */
202 by Brian Aker
Cleanup sql_lex to modern types.
568
  uint32_t n_child_sum_items;
1 by brian
clean slate
569
570
  /* explicit LIMIT clause was used */
571
  bool explicit_limit;
572
  /*
573
    there are subquery in HAVING clause => we can't close tables before
574
    query processing end even if we use temporary table
575
  */
576
  bool subquery_in_having;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
577
  /* true <=> this SELECT is correlated w.r.t. some ancestor select */
1 by brian
clean slate
578
  bool is_correlated;
579
  /*
580
    This variable is required to ensure proper work of subqueries and
581
    stored procedures. Generally, one should use the states of
582
    Query_arena to determine if it's a statement prepare or first
583
    execution of a stored procedure. However, in case when there was an
584
    error during the first execution of a stored procedure, the SP body
585
    is not expelled from the SP cache. Therefore, a deeply nested
586
    subquery might be left unoptimized. So we need this per-subquery
587
    variable to inidicate the optimization/execution state of every
588
    subquery. Prepared statements work OK in that regard, as in
589
    case of an error during prepare the PS is not created.
590
  */
591
  bool first_execution;
592
  bool first_cond_optimization;
593
  /* exclude this select from check of unique_table() */
594
  bool exclude_from_table_unique_test;
595
  /* List of fields that aren't under an aggregate function */
596
  List<Item_field> non_agg_fields;
597
  /* index in the select list of the expression currently being fixed */
598
  int cur_pos_in_select_list;
599
600
  List<udf_func>     udf_list;                  /* udf function calls stack */
601
  /* 
602
    This is a copy of the original JOIN USING list that comes from
603
    the parser. The parser :
327.2.4 by Brian Aker
Refactoring table.h
604
      1. Sets the natural_join of the second TableList in the join
1 by brian
clean slate
605
         and the st_select_lex::prev_join_using.
327.2.4 by Brian Aker
Refactoring table.h
606
      2. Makes a parent TableList and sets its is_natural_join/
1 by brian
clean slate
607
       join_using_fields members.
327.2.4 by Brian Aker
Refactoring table.h
608
      3. Uses the wrapper TableList as a table in the upper level.
1 by brian
clean slate
609
    We cannot assign directly to join_using_fields in the parser because
327.2.4 by Brian Aker
Refactoring table.h
610
    at stage (1.) the parent TableList is not constructed yet and
1 by brian
clean slate
611
    the assignment will override the JOIN USING fields of the lower level
612
    joins on the right.
613
  */
614
  List<String> *prev_join_using;
615
  /*
616
    Bitmap used in the ONLY_FULL_GROUP_BY_MODE to prevent mixture of aggregate
617
    functions and non aggregated fields when GROUP BY list is absent.
618
    Bits:
619
      0 - non aggregated fields are used in this select,
620
          defined as NON_AGG_FIELD_USED.
621
      1 - aggregate functions are used in this select,
622
          defined as SUM_FUNC_USED.
623
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
624
  uint8_t full_group_by_flag;
1 by brian
clean slate
625
  void init_query();
626
  void init_select();
627
  st_select_lex_unit* master_unit();
628
  st_select_lex_unit* first_inner_unit() 
629
  { 
630
    return (st_select_lex_unit*) slave; 
631
  }
632
  st_select_lex* outer_select();
633
  st_select_lex* next_select() { return (st_select_lex*) next; }
634
  st_select_lex* next_select_in_list() 
635
  {
636
    return (st_select_lex*) link_next;
637
  }
638
  st_select_lex_node** next_select_in_list_addr()
639
  {
640
    return &link_next;
641
  }
642
  st_select_lex* return_after_parsing()
643
  {
644
    return master_unit()->return_after_parsing();
645
  }
646
647
  void mark_as_dependent(st_select_lex *last);
648
649
  bool set_braces(bool value);
650
  bool inc_in_sum_expr();
202 by Brian Aker
Cleanup sql_lex to modern types.
651
  uint32_t get_in_sum_expr();
1 by brian
clean slate
652
653
  bool add_item_to_list(THD *thd, Item *item);
654
  bool add_group_to_list(THD *thd, Item *item, bool asc);
655
  bool add_order_to_list(THD *thd, Item *item, bool asc);
327.2.4 by Brian Aker
Refactoring table.h
656
  TableList* add_table_to_list(THD *thd, Table_ident *table,
1 by brian
clean slate
657
				LEX_STRING *alias,
202 by Brian Aker
Cleanup sql_lex to modern types.
658
				uint32_t table_options,
1 by brian
clean slate
659
				thr_lock_type flags= TL_UNLOCK,
660
				List<Index_hint> *hints= 0,
661
                                LEX_STRING *option= 0);
327.2.4 by Brian Aker
Refactoring table.h
662
  TableList* get_table_list();
1 by brian
clean slate
663
  bool init_nested_join(THD *thd);
327.2.4 by Brian Aker
Refactoring table.h
664
  TableList *end_nested_join(THD *thd);
665
  TableList *nest_last_join(THD *thd);
666
  void add_joined_table(TableList *table);
667
  TableList *convert_right_join();
1 by brian
clean slate
668
  List<Item>* get_item_list();
202 by Brian Aker
Cleanup sql_lex to modern types.
669
  uint32_t get_table_join_options();
1 by brian
clean slate
670
  void set_lock_for_tables(thr_lock_type lock_type);
671
  inline void init_order()
672
  {
673
    order_list.elements= 0;
674
    order_list.first= 0;
675
    order_list.next= (uchar**) &order_list.first;
676
  }
677
  /*
678
    This method created for reiniting LEX in mysql_admin_table() and can be
679
    used only if you are going remove all SELECT_LEX & units except belonger
680
    to LEX (LEX::unit & LEX::select, for other purposes there are
681
    SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
682
  */
683
  void cut_subtree() { slave= 0; }
684
  bool test_limit();
685
686
  friend void lex_start(THD *thd);
687
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
688
  void make_empty_select()
689
  {
690
    init_query();
691
    init_select();
692
  }
202 by Brian Aker
Cleanup sql_lex to modern types.
693
  bool setup_ref_array(THD *thd, uint32_t order_group_num);
1 by brian
clean slate
694
  void print(THD *thd, String *str, enum_query_type query_type);
695
  static void print_order(String *str,
327.2.3 by Brian Aker
Refactoring of class Table
696
                          order_st *order,
1 by brian
clean slate
697
                          enum_query_type query_type);
698
  void print_limit(THD *thd, String *str, enum_query_type query_type);
699
  void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
700
  /*
701
    Destroy the used execution plan (JOIN) of this subtree (this
702
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
703
  */
704
  bool cleanup();
705
  /*
706
    Recursively cleanup the join of this select lex and of all nested
707
    select lexes.
708
  */
709
  void cleanup_all_joins(bool full);
710
711
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
712
713
  /* 
714
   Add a index hint to the tagged list of hints. The type and clause of the
715
   hint will be the current ones (set by set_index_hint()) 
716
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
717
  bool add_index_hint (THD *thd, char *str, uint32_t length);
1 by brian
clean slate
718
719
  /* make a list to hold index hints */
720
  void alloc_index_hints (THD *thd);
721
  /* read and clear the index hints */
722
  List<Index_hint>* pop_index_hints(void) 
723
  {
724
    List<Index_hint> *hints= index_hints;
725
    index_hints= NULL;
726
    return hints;
727
  }
728
729
  void clear_index_hints(void) { index_hints= NULL; }
730
731
private:  
732
  /* current index hint kind. used in filling up index_hints */
733
  enum index_hint_type current_index_hint_type;
734
  index_clause_map current_index_hint_clause;
735
  /* a list of USE/FORCE/IGNORE INDEX */
736
  List<Index_hint> *index_hints;
737
};
738
typedef class st_select_lex SELECT_LEX;
739
740
inline bool st_select_lex_unit::is_union ()
741
{ 
742
  return first_select()->next_select() && 
743
    first_select()->next_select()->linkage == UNION_TYPE;
744
}
745
746
#define ALTER_ADD_COLUMN	(1L << 0)
747
#define ALTER_DROP_COLUMN	(1L << 1)
748
#define ALTER_CHANGE_COLUMN	(1L << 2)
749
#define ALTER_COLUMN_STORAGE	(1L << 3)
750
#define ALTER_COLUMN_FORMAT	(1L << 4)
751
#define ALTER_COLUMN_ORDER      (1L << 5)
752
#define ALTER_ADD_INDEX		(1L << 6)
753
#define ALTER_DROP_INDEX	(1L << 7)
754
#define ALTER_RENAME		(1L << 8)
755
#define ALTER_ORDER		(1L << 9)
756
#define ALTER_OPTIONS		(1L << 10)
757
#define ALTER_COLUMN_DEFAULT    (1L << 11)
758
#define ALTER_KEYS_ONOFF        (1L << 12)
759
#define ALTER_STORAGE	        (1L << 13)
760
#define ALTER_ROW_FORMAT        (1L << 14)
761
#define ALTER_CONVERT           (1L << 15)
762
#define ALTER_FORCE		(1L << 16)
763
#define ALTER_RECREATE          (1L << 17)
764
#define ALTER_TABLE_REORG        (1L << 24)
765
#define ALTER_FOREIGN_KEY         (1L << 31)
766
767
/**
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
768
  @brief Parsing data for CREATE or ALTER Table.
1 by brian
clean slate
769
770
  This structure contains a list of columns or indexes to be created,
771
  altered or dropped.
772
*/
773
774
class Alter_info
775
{
776
public:
777
  List<Alter_drop>              drop_list;
778
  List<Alter_column>            alter_list;
779
  List<Key>                     key_list;
780
  List<Create_field>            create_list;
202 by Brian Aker
Cleanup sql_lex to modern types.
781
  uint32_t                          flags;
1 by brian
clean slate
782
  enum enum_enable_or_disable   keys_onoff;
783
  enum tablespace_op_type       tablespace_op;
202 by Brian Aker
Cleanup sql_lex to modern types.
784
  uint32_t                          no_parts;
1 by brian
clean slate
785
  enum ha_build_method          build_method;
786
  Create_field                 *datetime_field;
787
  bool                          error_if_not_empty;
788
789
790
  Alter_info() :
791
    flags(0),
792
    keys_onoff(LEAVE_AS_IS),
793
    tablespace_op(NO_TABLESPACE_OP),
794
    no_parts(0),
795
    build_method(HA_BUILD_DEFAULT),
796
    datetime_field(NULL),
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
797
    error_if_not_empty(false)
1 by brian
clean slate
798
  {}
799
800
  void reset()
801
  {
802
    drop_list.empty();
803
    alter_list.empty();
804
    key_list.empty();
805
    create_list.empty();
806
    flags= 0;
807
    keys_onoff= LEAVE_AS_IS;
808
    tablespace_op= NO_TABLESPACE_OP;
809
    no_parts= 0;
810
    build_method= HA_BUILD_DEFAULT;
811
    datetime_field= 0;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
812
    error_if_not_empty= false;
1 by brian
clean slate
813
  }
814
  Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
815
private:
816
  Alter_info &operator=(const Alter_info &rhs); // not implemented
817
  Alter_info(const Alter_info &rhs);            // not implemented
818
};
819
820
enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
821
                      XA_SUSPEND, XA_FOR_MIGRATE};
822
823
extern const LEX_STRING null_lex_str;
824
825
826
/*
827
  Class representing list of all tables used by statement.
828
  It also contains information about stored functions used by statement
829
  since during its execution we may have to add all tables used by its
830
  stored functions/triggers to this list in order to pre-open and lock
831
  them.
832
833
  Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
834
  methods to save and restore this information.
835
*/
836
837
class Query_tables_list
838
{
839
public:
840
  /* Global list of all tables used by this statement */
327.2.4 by Brian Aker
Refactoring table.h
841
  TableList *query_tables;
1 by brian
clean slate
842
  /* Pointer to next_global member of last element in the previous list. */
327.2.4 by Brian Aker
Refactoring table.h
843
  TableList **query_tables_last;
1 by brian
clean slate
844
  /*
845
    If non-0 then indicates that query requires prelocking and points to
846
    next_global member of last own element in query table list (i.e. last
847
    table which was not added to it as part of preparation to prelocking).
848
    0 - indicates that this query does not need prelocking.
849
  */
327.2.4 by Brian Aker
Refactoring table.h
850
  TableList **query_tables_own_last;
1 by brian
clean slate
851
  /*
852
    Set of stored routines called by statement.
853
    (Note that we use lazy-initialization for this hash).
854
  */
855
  enum { START_SROUTINES_HASH_SIZE= 16 };
856
  HASH sroutines;
857
  /*
858
    List linking elements of 'sroutines' set. Allows you to add new elements
859
    to this set as you iterate through the list of existing elements.
860
    'sroutines_list_own_last' is pointer to ::next member of last element of
861
    this list which represents routine which is explicitly used by query.
862
    'sroutines_list_own_elements' number of explicitly used routines.
863
    We use these two members for restoring of 'sroutines_list' to the state
864
    in which it was right after query parsing.
865
  */
866
  SQL_LIST sroutines_list;
867
  uchar    **sroutines_list_own_last;
202 by Brian Aker
Cleanup sql_lex to modern types.
868
  uint32_t     sroutines_list_own_elements;
1 by brian
clean slate
869
870
  /*
871
    These constructor and destructor serve for creation/destruction
872
    of Query_tables_list instances which are used as backup storage.
873
  */
874
  Query_tables_list() {}
875
  ~Query_tables_list() {}
876
877
  /* Initializes (or resets) Query_tables_list object for "real" use. */
878
  void reset_query_tables_list(bool init);
879
  void destroy_query_tables_list();
880
  void set_query_tables_list(Query_tables_list *state)
881
  {
882
    *this= *state;
883
  }
884
885
  /*
886
    Direct addition to the list of query tables.
887
    If you are using this function, you must ensure that the table
888
    object, in particular table->db member, is initialized.
889
  */
327.2.4 by Brian Aker
Refactoring table.h
890
  void add_to_query_tables(TableList *table)
1 by brian
clean slate
891
  {
892
    *(table->prev_global= query_tables_last)= table;
893
    query_tables_last= &table->next_global;
894
  }
895
  /* Return pointer to first not-own table in query-tables or 0 */
327.2.4 by Brian Aker
Refactoring table.h
896
  TableList* first_not_own_table()
1 by brian
clean slate
897
  {
898
    return ( query_tables_own_last ? *query_tables_own_last : 0);
899
  }
900
  void chop_off_not_own_tables()
901
  {
902
    if (query_tables_own_last)
903
    {
904
      *query_tables_own_last= 0;
905
      query_tables_last= query_tables_own_last;
906
      query_tables_own_last= 0;
907
    }
908
  }
909
910
  /**
911
     Has the parser/scanner detected that this statement is unsafe?
912
   */
913
  inline bool is_stmt_unsafe() const {
914
    return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
915
  }
916
917
  /**
918
     Flag the current (top-level) statement as unsafe.
919
920
     The flag will be reset after the statement has finished.
921
922
   */
923
  inline void set_stmt_unsafe() {
924
    binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
925
  }
926
927
  inline void clear_stmt_unsafe() {
928
    binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
929
  }
930
931
  /**
932
    true if the parsed tree contains references to stored procedures
933
    or functions, false otherwise
934
  */
935
  bool uses_stored_routines() const
936
  { return sroutines_list.elements != 0; }
937
938
private:
939
  enum enum_binlog_stmt_flag {
940
    BINLOG_STMT_FLAG_UNSAFE,
941
    BINLOG_STMT_FLAG_COUNT
942
  };
943
944
  /*
945
    Tells if the parsing stage detected properties of the statement,
946
    for example: that some items require row-based binlogging to give
947
    a reliable binlog/replication, or if we will use stored functions
948
    or triggers which themselves need require row-based binlogging.
949
  */
205 by Brian Aker
uint32 -> uin32_t
950
  uint32_t binlog_stmt_flags;
1 by brian
clean slate
951
};
952
953
954
/*
955
  st_parsing_options contains the flags for constructions that are
956
  allowed in the current statement.
957
*/
958
959
struct st_parsing_options
960
{
961
  bool allows_variable;
962
  bool allows_select_into;
963
  bool allows_select_procedure;
964
  bool allows_derived;
965
966
  st_parsing_options() { reset(); }
967
  void reset();
968
};
969
970
971
/**
972
  The state of the lexical parser, when parsing comments.
973
*/
974
enum enum_comment_state
975
{
976
  /**
977
    Not parsing comments.
978
  */
979
  NO_COMMENT,
980
  /**
981
    Parsing comments that need to be preserved.
982
    Typically, these are user comments '/' '*' ... '*' '/'.
983
  */
984
  PRESERVE_COMMENT,
985
  /**
986
    Parsing comments that need to be discarded.
987
    Typically, these are special comments '/' '*' '!' ... '*' '/',
988
    or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
989
    markers should not be expanded.
990
  */
991
  DISCARD_COMMENT
992
};
993
994
995
/**
996
  @brief This class represents the character input stream consumed during
997
  lexical analysis.
998
999
  In addition to consuming the input stream, this class performs some
1000
  comment pre processing, by filtering out out of bound special text
1001
  from the query input stream.
1002
  Two buffers, with pointers inside each buffers, are maintained in
1003
  parallel. The 'raw' buffer is the original query text, which may
1004
  contain out-of-bound comments. The 'cpp' (for comments pre processor)
1005
  is the pre-processed buffer that contains only the query text that
1006
  should be seen once out-of-bound data is removed.
1007
*/
1008
1009
class Lex_input_stream
1010
{
1011
public:
1012
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
1013
  ~Lex_input_stream();
1014
1015
  /**
1016
    Set the echo mode.
1017
1018
    When echo is true, characters parsed from the raw input stream are
1019
    preserved. When false, characters parsed are silently ignored.
1020
    @param echo the echo mode.
1021
  */
1022
  void set_echo(bool echo)
1023
  {
1024
    m_echo= echo;
1025
  }
1026
1027
  /**
1028
    Skip binary from the input stream.
1029
    @param n number of bytes to accept.
1030
  */
1031
  void skip_binary(int n)
1032
  {
1033
    if (m_echo)
1034
    {
1035
      memcpy(m_cpp_ptr, m_ptr, n);
1036
      m_cpp_ptr += n;
1037
    }
1038
    m_ptr += n;
1039
  }
1040
1041
  /**
1042
    Get a character, and advance in the stream.
1043
    @return the next character to parse.
1044
  */
1045
  char yyGet()
1046
  {
1047
    char c= *m_ptr++;
1048
    if (m_echo)
1049
      *m_cpp_ptr++ = c;
1050
    return c;
1051
  }
1052
1053
  /**
1054
    Get the last character accepted.
1055
    @return the last character accepted.
1056
  */
1057
  char yyGetLast()
1058
  {
1059
    return m_ptr[-1];
1060
  }
1061
1062
  /**
1063
    Look at the next character to parse, but do not accept it.
1064
  */
1065
  char yyPeek()
1066
  {
1067
    return m_ptr[0];
1068
  }
1069
1070
  /**
1071
    Look ahead at some character to parse.
1072
    @param n offset of the character to look up
1073
  */
1074
  char yyPeekn(int n)
1075
  {
1076
    return m_ptr[n];
1077
  }
1078
1079
  /**
1080
    Cancel the effect of the last yyGet() or yySkip().
1081
    Note that the echo mode should not change between calls to yyGet / yySkip
1082
    and yyUnget. The caller is responsible for ensuring that.
1083
  */
1084
  void yyUnget()
1085
  {
1086
    m_ptr--;
1087
    if (m_echo)
1088
      m_cpp_ptr--;
1089
  }
1090
1091
  /**
1092
    Accept a character, by advancing the input stream.
1093
  */
1094
  void yySkip()
1095
  {
1096
    if (m_echo)
1097
      *m_cpp_ptr++ = *m_ptr++;
1098
    else
1099
      m_ptr++;
1100
  }
1101
1102
  /**
1103
    Accept multiple characters at once.
1104
    @param n the number of characters to accept.
1105
  */
1106
  void yySkipn(int n)
1107
  {
1108
    if (m_echo)
1109
    {
1110
      memcpy(m_cpp_ptr, m_ptr, n);
1111
      m_cpp_ptr += n;
1112
    }
1113
    m_ptr += n;
1114
  }
1115
1116
  /**
1117
    End of file indicator for the query text to parse.
1118
    @return true if there are no more characters to parse
1119
  */
1120
  bool eof()
1121
  {
1122
    return (m_ptr >= m_end_of_query);
1123
  }
1124
1125
  /**
1126
    End of file indicator for the query text to parse.
1127
    @param n number of characters expected
1128
    @return true if there are less than n characters to parse
1129
  */
1130
  bool eof(int n)
1131
  {
1132
    return ((m_ptr + n) >= m_end_of_query);
1133
  }
1134
1135
  /** Get the raw query buffer. */
1136
  const char *get_buf()
1137
  {
1138
    return m_buf;
1139
  }
1140
1141
  /** Get the pre-processed query buffer. */
1142
  const char *get_cpp_buf()
1143
  {
1144
    return m_cpp_buf;
1145
  }
1146
1147
  /** Get the end of the raw query buffer. */
1148
  const char *get_end_of_query()
1149
  {
1150
    return m_end_of_query;
1151
  }
1152
1153
  /** Mark the stream position as the start of a new token. */
1154
  void start_token()
1155
  {
1156
    m_tok_start_prev= m_tok_start;
1157
    m_tok_start= m_ptr;
1158
    m_tok_end= m_ptr;
1159
1160
    m_cpp_tok_start_prev= m_cpp_tok_start;
1161
    m_cpp_tok_start= m_cpp_ptr;
1162
    m_cpp_tok_end= m_cpp_ptr;
1163
  }
1164
1165
  /**
1166
    Adjust the starting position of the current token.
1167
    This is used to compensate for starting whitespace.
1168
  */
1169
  void restart_token()
1170
  {
1171
    m_tok_start= m_ptr;
1172
    m_cpp_tok_start= m_cpp_ptr;
1173
  }
1174
1175
  /** Get the token start position, in the raw buffer. */
1176
  const char *get_tok_start()
1177
  {
1178
    return m_tok_start;
1179
  }
1180
1181
  /** Get the token start position, in the pre-processed buffer. */
1182
  const char *get_cpp_tok_start()
1183
  {
1184
    return m_cpp_tok_start;
1185
  }
1186
1187
  /** Get the token end position, in the raw buffer. */
1188
  const char *get_tok_end()
1189
  {
1190
    return m_tok_end;
1191
  }
1192
1193
  /** Get the token end position, in the pre-processed buffer. */
1194
  const char *get_cpp_tok_end()
1195
  {
1196
    return m_cpp_tok_end;
1197
  }
1198
1199
  /** Get the previous token start position, in the raw buffer. */
1200
  const char *get_tok_start_prev()
1201
  {
1202
    return m_tok_start_prev;
1203
  }
1204
1205
  /** Get the current stream pointer, in the raw buffer. */
1206
  const char *get_ptr()
1207
  {
1208
    return m_ptr;
1209
  }
1210
1211
  /** Get the current stream pointer, in the pre-processed buffer. */
1212
  const char *get_cpp_ptr()
1213
  {
1214
    return m_cpp_ptr;
1215
  }
1216
1217
  /** Get the length of the current token, in the raw buffer. */
202 by Brian Aker
Cleanup sql_lex to modern types.
1218
  uint32_t yyLength()
1 by brian
clean slate
1219
  {
1220
    /*
1221
      The assumption is that the lexical analyser is always 1 character ahead,
1222
      which the -1 account for.
1223
    */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1224
    assert(m_ptr > m_tok_start);
202 by Brian Aker
Cleanup sql_lex to modern types.
1225
    return (uint32_t) ((m_ptr - m_tok_start) - 1);
1 by brian
clean slate
1226
  }
1227
1228
  /** Get the utf8-body string. */
1229
  const char *get_body_utf8_str()
1230
  {
1231
    return m_body_utf8;
1232
  }
1233
1234
  /** Get the utf8-body length. */
202 by Brian Aker
Cleanup sql_lex to modern types.
1235
  uint32_t get_body_utf8_length()
1 by brian
clean slate
1236
  {
1237
    return m_body_utf8_ptr - m_body_utf8;
1238
  }
1239
1240
  void body_utf8_start(THD *thd, const char *begin_ptr);
1241
  void body_utf8_append(const char *ptr);
1242
  void body_utf8_append(const char *ptr, const char *end_ptr);
1243
  void body_utf8_append_literal(THD *thd,
1244
                                const LEX_STRING *txt,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1245
                                const CHARSET_INFO * const txt_cs,
1 by brian
clean slate
1246
                                const char *end_ptr);
1247
1248
  /** Current thread. */
1249
  THD *m_thd;
1250
1251
  /** Current line number. */
202 by Brian Aker
Cleanup sql_lex to modern types.
1252
  uint32_t yylineno;
1 by brian
clean slate
1253
1254
  /** Length of the last token parsed. */
202 by Brian Aker
Cleanup sql_lex to modern types.
1255
  uint32_t yytoklen;
1 by brian
clean slate
1256
1257
  /** Interface with bison, value of the last token parsed. */
1258
  LEX_YYSTYPE yylval;
1259
1260
  /** LALR(2) resolution, look ahead token.*/
1261
  int lookahead_token;
1262
1263
  /** LALR(2) resolution, value of the look ahead token.*/
1264
  LEX_YYSTYPE lookahead_yylval;
1265
1266
private:
1267
  /** Pointer to the current position in the raw input stream. */
1268
  const char *m_ptr;
1269
1270
  /** Starting position of the last token parsed, in the raw buffer. */
1271
  const char *m_tok_start;
1272
1273
  /** Ending position of the previous token parsed, in the raw buffer. */
1274
  const char *m_tok_end;
1275
1276
  /** End of the query text in the input stream, in the raw buffer. */
1277
  const char *m_end_of_query;
1278
1279
  /** Starting position of the previous token parsed, in the raw buffer. */
1280
  const char *m_tok_start_prev;
1281
1282
  /** Begining of the query text in the input stream, in the raw buffer. */
1283
  const char *m_buf;
1284
1285
  /** Length of the raw buffer. */
202 by Brian Aker
Cleanup sql_lex to modern types.
1286
  uint32_t m_buf_length;
1 by brian
clean slate
1287
1288
  /** Echo the parsed stream to the pre-processed buffer. */
1289
  bool m_echo;
1290
1291
  /** Pre-processed buffer. */
1292
  char *m_cpp_buf;
1293
1294
  /** Pointer to the current position in the pre-processed input stream. */
1295
  char *m_cpp_ptr;
1296
1297
  /**
1298
    Starting position of the last token parsed,
1299
    in the pre-processed buffer.
1300
  */
1301
  const char *m_cpp_tok_start;
1302
1303
  /**
1304
    Starting position of the previous token parsed,
1305
    in the pre-procedded buffer.
1306
  */
1307
  const char *m_cpp_tok_start_prev;
1308
1309
  /**
1310
    Ending position of the previous token parsed,
1311
    in the pre-processed buffer.
1312
  */
1313
  const char *m_cpp_tok_end;
1314
1315
  /** UTF8-body buffer created during parsing. */
1316
  char *m_body_utf8;
1317
1318
  /** Pointer to the current position in the UTF8-body buffer. */
1319
  char *m_body_utf8_ptr;
1320
1321
  /**
1322
    Position in the pre-processed buffer. The query from m_cpp_buf to
1323
    m_cpp_utf_processed_ptr is converted to UTF8-body.
1324
  */
1325
  const char *m_cpp_utf8_processed_ptr;
1326
1327
public:
1328
1329
  /** Current state of the lexical analyser. */
1330
  enum my_lex_states next_state;
1331
1332
  /**
1333
    Position of ';' in the stream, to delimit multiple queries.
1334
    This delimiter is in the raw buffer.
1335
  */
1336
  const char *found_semicolon;
1337
1338
  /** Token character bitmaps, to detect 7bit strings. */
1339
  uchar tok_bitmap;
1340
1341
  /** SQL_MODE = IGNORE_SPACE. */
1342
  bool ignore_space;
1343
1344
  /** State of the lexical analyser for comments. */
1345
  enum_comment_state in_comment;
1346
1347
  /**
1348
    Starting 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_start;
1354
1355
  /**
1356
    Ending position of the TEXT_STRING or IDENT in the pre-processed
1357
    buffer.
1358
1359
    NOTE: this member must be used within MYSQLlex() function only.
1360
    */
1361
  const char *m_cpp_text_end;
1362
1363
  /**
1364
    Character set specified by the character-set-introducer.
1365
1366
    NOTE: this member must be used within MYSQLlex() function only.
1367
  */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1368
  const CHARSET_INFO *m_underscore_cs;
1 by brian
clean slate
1369
};
1370
1371
1372
/* The state of the lex parsing. This is saved in the THD struct */
1373
1374
typedef struct st_lex : public Query_tables_list
1375
{
1376
  SELECT_LEX_UNIT unit;                         /* most upper unit */
1377
  SELECT_LEX select_lex;                        /* first SELECT_LEX */
1378
  /* current SELECT_LEX in parsing */
1379
  SELECT_LEX *current_select;
1380
  /* list of all SELECT_LEX */
1381
  SELECT_LEX *all_selects_list;
1382
1383
  char *length,*dec,*change;
1384
  LEX_STRING name;
1385
  char *help_arg;
1386
  char* to_log;                                 /* For PURGE MASTER LOGS TO */
1387
  char* x509_subject,*x509_issuer,*ssl_cipher;
1388
  String *wild;
1389
  sql_exchange *exchange;
1390
  select_result *result;
1391
  Item *default_value, *on_update_value;
1392
  LEX_STRING comment, ident;
1393
  XID *xid;
1394
  uchar* yacc_yyss, *yacc_yyvs;
1395
  THD *thd;
1396
1397
  /* maintain a list of used plugins for this LEX */
1398
  DYNAMIC_ARRAY plugins;
1399
  plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
1400
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1401
  const CHARSET_INFO *charset;
1 by brian
clean slate
1402
  bool text_string_is_7bit;
1403
  /* store original leaf_tables for INSERT SELECT and PS/SP */
327.2.4 by Brian Aker
Refactoring table.h
1404
  TableList *leaf_tables_insert;
1 by brian
clean slate
1405
1406
  List<Key_part_spec> col_list;
1407
  List<Key_part_spec> ref_list;
1408
  List<String>	      interval_list;
1409
  List<LEX_COLUMN>    columns;
1410
  List<Item>	      *insert_list,field_list,value_list,update_list;
1411
  List<List_item>     many_values;
1412
  List<set_var_base>  var_list;
1413
  List<Item_param>    param_list;
1414
  /*
1415
    A stack of name resolution contexts for the query. This stack is used
1416
    at parse time to set local name resolution contexts for various parts
1417
    of a query. For example, in a JOIN ... ON (some_condition) clause the
1418
    Items in 'some_condition' must be resolved only against the operands
1419
    of the the join, and not against the whole clause. Similarly, Items in
1420
    subqueries should be resolved against the subqueries (and outer queries).
1421
    The stack is used in the following way: when the parser detects that
1422
    all Items in some clause need a local context, it creates a new context
1423
    and pushes it on the stack. All newly created Items always store the
1424
    top-most context in the stack. Once the parser leaves the clause that
1425
    required a local context, the parser pops the top-most context.
1426
  */
1427
  List<Name_resolution_context> context_stack;
1428
  List<LEX_STRING>     db_list;
1429
1430
  SQL_LIST	      proc_list, auxiliary_table_list, save_list;
1431
  Create_field	      *last_field;
1432
  Item_sum *in_sum_func;
1433
  udf_func udf;
1434
  HA_CHECK_OPT   check_opt;			// check/repair options
1435
  HA_CREATE_INFO create_info;
1436
  KEY_CREATE_INFO key_create_info;
1437
  LEX_MASTER_INFO mi;				// used by CHANGE MASTER
1438
  LEX_SERVER_OPTIONS server_options;
202 by Brian Aker
Cleanup sql_lex to modern types.
1439
  uint32_t type;
1 by brian
clean slate
1440
  /*
1441
    This variable is used in post-parse stage to declare that sum-functions,
1442
    or functions which have sense only if GROUP BY is present, are allowed.
1443
    For example in a query
1444
    SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
1445
    MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
1446
    in the HAVING clause. Due to possible nesting of select construct
1447
    the variable can contain 0 or 1 for each nest level.
1448
  */
1449
  nesting_map allow_sum_func;
1450
  enum_sql_command sql_command;
1451
  /*
1452
    Usually `expr` rule of yacc is quite reused but some commands better
1453
    not support subqueries which comes standard with this rule, like
1454
    KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
1455
    syntax error back.
1456
  */
1457
  bool expr_allows_subselect;
1458
1459
  thr_lock_type lock_option;
1460
  enum enum_duplicates duplicates;
1461
  enum enum_tx_isolation tx_isolation;
1462
  enum enum_ha_read_modes ha_read_mode;
1463
  union {
1464
    enum ha_rkey_function ha_rkey_mode;
1465
    enum xa_option_words xa_opt;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1466
    bool lock_transactional;            /* For LOCK Table ... IN ... MODE */
1 by brian
clean slate
1467
  };
1468
  enum enum_var_type option_type;
1469
202 by Brian Aker
Cleanup sql_lex to modern types.
1470
  uint32_t profile_query_id;
1471
  uint32_t profile_options;
1 by brian
clean slate
1472
  enum column_format_type column_format;
202 by Brian Aker
Cleanup sql_lex to modern types.
1473
  uint32_t which_columns;
1 by brian
clean slate
1474
  enum Foreign_key::fk_match_opt fk_match_option;
1475
  enum Foreign_key::fk_option fk_update_opt;
1476
  enum Foreign_key::fk_option fk_delete_opt;
202 by Brian Aker
Cleanup sql_lex to modern types.
1477
  uint32_t slave_thd_opt, start_transaction_opt;
1 by brian
clean slate
1478
  int nest_level;
1479
  /*
1480
    In LEX representing update which were transformed to multi-update
1481
    stores total number of tables. For LEX representing multi-delete
1482
    holds number of tables from which we will delete records.
1483
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
1484
  uint32_t table_count;
1485
  uint8_t describe;
1 by brian
clean slate
1486
  /*
1487
    A flag that indicates what kinds of derived tables are present in the
1488
    query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
1489
  */
202 by Brian Aker
Cleanup sql_lex to modern types.
1490
  uint8_t derived_tables;
1 by brian
clean slate
1491
  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
1492
  bool autocommit;
383.1.26 by Brian Aker
Removed option for single command replication and have now left disable of
1493
  bool verbose;
1 by brian
clean slate
1494
1495
  bool tx_chain, tx_release;
1496
  bool subqueries, ignore;
1497
  st_parsing_options parsing_options;
1498
  Alter_info alter_info;
1499
1500
  /*
1501
    field_list was created for view and should be removed before PS/SP
1502
    rexecuton
1503
  */
1504
  bool empty_field_list_on_rset;
1505
1506
  /*
1507
    Pointers to part of LOAD DATA statement that should be rewritten
1508
    during replication ("LOCAL 'filename' REPLACE INTO" part).
1509
  */
1510
  const char *fname_start;
1511
  const char *fname_end;
1512
  
1513
  /**
1514
    During name resolution search only in the table list given by 
1515
    Name_resolution_context::first_name_resolution_table and
1516
    Name_resolution_context::last_name_resolution_table
1517
    (see Item_field::fix_fields()). 
1518
  */
1519
  bool use_only_table_context;
1520
  
1521
  bool escape_used;
1522
  bool is_lex_started; /* If lex_start() did run. For debugging. */
1523
1524
  st_lex();
1525
1526
  virtual ~st_lex()
1527
  {
1528
    destroy_query_tables_list();
1529
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1530
    delete_dynamic(&plugins);
1531
  }
1532
327.2.4 by Brian Aker
Refactoring table.h
1533
  TableList *unlink_first_table(bool *link_to_local);
1534
  void link_first_table_back(TableList *first, bool link_to_local);
1 by brian
clean slate
1535
  void first_lists_tables_same();
1536
1537
  bool can_be_merged();
1538
  bool can_use_merged();
1539
  bool can_not_use_merged();
1540
  bool only_view_structure();
1541
  bool need_correct_ident();
1542
1543
  void cleanup_after_one_table_open();
1544
1545
  bool push_context(Name_resolution_context *context)
1546
  {
1547
    return context_stack.push_front(context);
1548
  }
1549
1550
  void pop_context()
1551
  {
1552
    context_stack.pop();
1553
  }
1554
1555
  bool copy_db_to(char **p_db, size_t *p_db_length) const;
1556
1557
  Name_resolution_context *current_context()
1558
  {
1559
    return context_stack.head();
1560
  }
1561
  /*
1562
    Restore the LEX and THD in case of a parse error.
1563
  */
1564
  static void cleanup_lex_after_parse_error(THD *thd);
1565
1566
  void reset_n_backup_query_tables_list(Query_tables_list *backup);
1567
  void restore_backup_query_tables_list(Query_tables_list *backup);
1568
1569
  bool table_or_sp_used();
1570
1571
  /**
1572
    @brief check if the statement is a single-level join
1573
    @return result of the check
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1574
      @retval true  The statement doesn't contain subqueries, unions and 
1 by brian
clean slate
1575
                    stored procedure calls.
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1576
      @retval false There are subqueries, UNIONs or stored procedure calls.
1 by brian
clean slate
1577
  */
1578
  bool is_single_level_stmt() 
1579
  { 
1580
    /* 
1581
      This check exploits the fact that the last added to all_select_list is
1582
      on its top. So select_lex (as the first added) will be at the tail 
1583
      of the list.
1584
    */ 
1585
    if (&select_lex == all_selects_list && !sroutines.records)
1586
    {
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1587
      assert(!all_selects_list->next_select_in_list());
1588
      return true;
1 by brian
clean slate
1589
    }
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1590
    return false;
1 by brian
clean slate
1591
  }
1592
} LEX;
1593
1594
struct st_lex_local: public st_lex
1595
{
1596
  static void *operator new(size_t size) throw()
1597
  {
1598
    return sql_alloc(size);
1599
  }
1600
  static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
1601
  {
202 by Brian Aker
Cleanup sql_lex to modern types.
1602
    return (void*) alloc_root(mem_root, (uint32_t) size);
1 by brian
clean slate
1603
  }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1604
  static void operator delete(void *ptr __attribute__((unused)),
1605
                              size_t size __attribute__((unused)))
1 by brian
clean slate
1606
  { TRASH(ptr, size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1607
  static void operator delete(void *ptr __attribute__((unused)),
1608
                              MEM_ROOT *mem_root __attribute__((unused)))
1 by brian
clean slate
1609
  { /* Never called */ }
1610
};
1611
1612
extern void lex_init(void);
1613
extern void lex_free(void);
1614
extern void lex_start(THD *thd);
1615
extern void lex_end(LEX *lex);
1616
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1617
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
1 by brian
clean slate
1618
1619
extern bool is_lex_native_function(const LEX_STRING *name);
1620
1621
/**
1622
  @} (End of group Semantic_Analysis)
1623
*/
1624
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1625
#endif /* DRIZZLE_SERVER */
243.1.8 by Jay Pipes
More cleanup and revert broken base.h
1626
#endif /* DRIZZLE_SERVER_SQL_LEX_H */