~drizzle-trunk/drizzle/development

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