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