~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_lex.h

Merged in changes. 
Edited a the comment test case so deal with our version bump.

Show diffs side-by-side

added added

removed removed

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