~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_lex.h

  • Committer: Jay Pipes
  • Date: 2008-07-17 17:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717175400-xm2aazihjra8mdzq
Removal of DBUG from libdrizzle/ - Round 2

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