~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_lex.cc

  • 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:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
 
#include <config.h>
20
 
 
21
 
#define DRIZZLE_LEX 1
22
 
 
23
 
#include <drizzled/sql_reserved_words.h>
24
 
 
25
 
#include <drizzled/configmake.h>
26
 
#include <drizzled/item/num.h>
27
 
#include <drizzled/error.h>
28
 
#include <drizzled/session.h>
29
 
#include <drizzled/sql_base.h>
30
 
#include <drizzled/lookup_symbol.h>
31
 
#include <drizzled/index_hint.h>
32
 
#include <drizzled/select_result.h>
33
 
 
34
 
#include <cstdio>
35
 
#include <ctype.h>
36
 
 
37
 
union ParserType;
38
 
 
39
 
using namespace std;
40
 
 
41
 
/* Stay outside of the namespace because otherwise bison goes nuts */
42
 
int base_sql_lex(ParserType *arg, drizzled::Session *yysession);
43
 
 
44
 
namespace drizzled
45
 
{
46
 
 
47
 
static int lex_one_token(ParserType *arg, drizzled::Session *yysession);
48
 
 
49
 
/**
50
 
  save order by and tables in own lists.
 
19
#define MYSQL_LEX 1
 
20
#include "mysql_priv.h"
 
21
#include "item_create.h"
 
22
#include <m_ctype.h>
 
23
#include <hash.h>
 
24
 
 
25
static int lex_one_token(void *arg, void *yythd);
 
26
 
 
27
/*
 
28
  We are using pointer to this variable for distinguishing between assignment
 
29
  to NEW row field (when parsing trigger definition) and structured variable.
51
30
*/
52
 
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
53
 
{
54
 
  Order *order;
55
 
 
56
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
57
 
    return true;
58
 
 
59
 
  order->item_ptr= item;
60
 
  order->item= &order->item_ptr;
61
 
  order->asc = asc;
62
 
  order->free_me=0;
63
 
  order->used=0;
64
 
  order->counter_used= 0;
65
 
  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
66
 
 
67
 
  return false;
68
 
}
 
31
 
 
32
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
69
33
 
70
34
/**
71
35
  LEX_STRING constant for null-string to be used in parser and other places.
72
36
*/
73
37
const LEX_STRING null_lex_str= {NULL, 0};
74
38
 
75
 
Lex_input_stream::Lex_input_stream(Session *session,
 
39
/* Longest standard keyword name */
 
40
 
 
41
#define TOCK_NAME_LENGTH 24
 
42
 
 
43
/*
 
44
  The following data is based on the latin1 character set, and is only
 
45
  used when comparing keywords
 
46
*/
 
47
 
 
48
static uchar to_upper_lex[]=
 
49
{
 
50
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
 
51
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 
52
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 
53
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
 
54
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
55
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
 
56
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
57
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
 
58
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
59
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
 
60
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
 
61
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
 
62
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
63
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
 
64
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
65
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
 
66
};
 
67
 
 
68
/* 
 
69
  Names of the index hints (for error messages). Keep in sync with 
 
70
  index_hint_type 
 
71
*/
 
72
 
 
73
const char * index_hint_type_name[] =
 
74
{
 
75
  "IGNORE INDEX", 
 
76
  "USE INDEX", 
 
77
  "FORCE INDEX"
 
78
};
 
79
 
 
80
inline int lex_casecmp(const char *s, const char *t, uint len)
 
81
{
 
82
  while (len-- != 0 &&
 
83
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
 
84
  return (int) len+1;
 
85
}
 
86
 
 
87
#include <lex_hash.h>
 
88
 
 
89
 
 
90
void lex_init(void)
 
91
{
 
92
  uint i;
 
93
  for (i=0 ; i < array_elements(symbols) ; i++)
 
94
    symbols[i].length=(uchar) strlen(symbols[i].name);
 
95
  for (i=0 ; i < array_elements(sql_functions) ; i++)
 
96
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
 
97
 
 
98
  return;
 
99
}
 
100
 
 
101
 
 
102
void lex_free(void)
 
103
{                                       // Call this when daemon ends
 
104
  return;
 
105
}
 
106
 
 
107
 
 
108
void
 
109
st_parsing_options::reset()
 
110
{
 
111
  allows_variable= true;
 
112
  allows_select_into= true;
 
113
  allows_select_procedure= true;
 
114
  allows_derived= true;
 
115
}
 
116
 
 
117
Lex_input_stream::Lex_input_stream(THD *thd,
76
118
                                   const char* buffer,
77
 
                                   unsigned int length) :
78
 
  m_session(session),
 
119
                                   unsigned int length)
 
120
: m_thd(thd),
79
121
  yylineno(1),
80
122
  yytoklen(0),
81
123
  yylval(NULL),
95
137
  m_body_utf8(NULL),
96
138
  m_cpp_utf8_processed_ptr(NULL),
97
139
  next_state(MY_LEX_START),
 
140
  found_semicolon(NULL),
98
141
  ignore_space(1),
99
 
  in_comment(NO_COMMENT)
 
142
  stmt_prepare_mode(false),
 
143
  in_comment(NO_COMMENT),
 
144
  m_underscore_cs(NULL)
100
145
{
101
 
  m_cpp_buf= (char*) session->getMemRoot()->allocate(length + 1);
 
146
  m_cpp_buf= (char*) thd->alloc(length + 1);
102
147
  m_cpp_ptr= m_cpp_buf;
103
148
}
104
149
 
112
157
     statement;
113
158
  2) Determine the beginning of the body.
114
159
 
115
 
  @param session        Thread context.
 
160
  @param thd        Thread context.
116
161
  @param begin_ptr  Pointer to the start of the body in the pre-processed
117
162
                    buffer.
118
163
*/
119
 
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
 
164
 
 
165
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
120
166
{
121
167
  assert(begin_ptr);
122
168
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
123
169
 
124
 
  uint32_t body_utf8_length=
125
 
    (m_buf_length / default_charset_info->mbminlen) *
 
170
  uint body_utf8_length=
 
171
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
126
172
    my_charset_utf8_bin.mbmaxlen;
127
173
 
128
 
  m_body_utf8= (char *) session->getMemRoot()->allocate(body_utf8_length + 1);
 
174
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
129
175
  m_body_utf8_ptr= m_body_utf8;
130
176
  *m_body_utf8_ptr= 0;
131
177
 
152
198
                  m_cpp_utf8_processed_ptr will be set in the end of the
153
199
                  operation.
154
200
*/
 
201
 
155
202
void Lex_input_stream::body_utf8_append(const char *ptr,
156
203
                                        const char *end_ptr)
157
204
{
180
227
  @param ptr  Pointer in the pre-processed buffer, which specifies the end
181
228
              of the chunk, which should be appended to the utf8 body.
182
229
*/
 
230
 
183
231
void Lex_input_stream::body_utf8_append(const char *ptr)
184
232
{
185
233
  body_utf8_append(ptr, ptr);
189
237
  The operation converts the specified text literal to the utf8 and appends
190
238
  the result to the utf8-body.
191
239
 
192
 
  @param session      Thread context.
 
240
  @param thd      Thread context.
193
241
  @param txt      Text literal.
194
242
  @param txt_cs   Character set of the text literal.
195
243
  @param end_ptr  Pointer in the pre-processed buffer, to which
196
244
                  m_cpp_utf8_processed_ptr will be set in the end of the
197
245
                  operation.
198
246
*/
199
 
void Lex_input_stream::body_utf8_append_literal(const LEX_STRING *txt,
 
247
 
 
248
void Lex_input_stream::body_utf8_append_literal(THD *thd,
 
249
                                                const LEX_STRING *txt,
 
250
                                                CHARSET_INFO *txt_cs,
200
251
                                                const char *end_ptr)
201
252
{
202
253
  if (!m_cpp_utf8_processed_ptr)
203
254
    return;
204
255
 
 
256
  LEX_STRING utf_txt;
 
257
 
 
258
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
 
259
  {
 
260
    thd->convert_string(&utf_txt,
 
261
                        &my_charset_utf8_general_ci,
 
262
                        txt->str, txt->length,
 
263
                        txt_cs);
 
264
  }
 
265
  else
 
266
  {
 
267
    utf_txt.str= txt->str;
 
268
    utf_txt.length= txt->length;
 
269
  }
 
270
 
205
271
  /* NOTE: utf_txt.length is in bytes, not in symbols. */
206
272
 
207
 
  memcpy(m_body_utf8_ptr, txt->str, txt->length);
208
 
  m_body_utf8_ptr += txt->length;
 
273
  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
 
274
  m_body_utf8_ptr += utf_txt.length;
209
275
  *m_body_utf8_ptr= 0;
210
276
 
211
277
  m_cpp_utf8_processed_ptr= end_ptr;
212
278
}
213
279
 
 
280
 
214
281
/*
215
282
  This is called before every query that is to be parsed.
216
283
  Because of this, it's critical to not do too much things here.
217
284
  (We already do too much here)
218
285
*/
219
 
void LEX::start(Session *arg)
220
 
{
221
 
  lex_start(arg);
222
 
}
223
 
 
224
 
void lex_start(Session *session)
225
 
{
226
 
  LEX *lex= session->getLex();
227
 
 
228
 
  lex->session= lex->unit.session= session;
229
 
 
230
 
  lex->context_stack.clear();
 
286
 
 
287
void lex_start(THD *thd)
 
288
{
 
289
  LEX *lex= thd->lex;
 
290
 
 
291
  lex->thd= lex->unit.thd= thd;
 
292
 
 
293
  lex->context_stack.empty();
231
294
  lex->unit.init_query();
232
295
  lex->unit.init_select();
233
296
  /* 'parent_lex' is used in init_query() so it must be before it. */
234
297
  lex->select_lex.parent_lex= lex;
235
298
  lex->select_lex.init_query();
236
 
  lex->value_list.clear();
237
 
  lex->update_list.clear();
238
 
  lex->auxiliary_table_list.clear();
 
299
  lex->value_list.empty();
 
300
  lex->update_list.empty();
 
301
  lex->param_list.empty();
 
302
  lex->view_list.empty();
 
303
  lex->auxiliary_table_list.empty();
239
304
  lex->unit.next= lex->unit.master=
240
305
    lex->unit.link_next= lex->unit.return_to= 0;
241
306
  lex->unit.prev= lex->unit.link_prev= 0;
244
309
  lex->select_lex.master= &lex->unit;
245
310
  lex->select_lex.prev= &lex->unit.slave;
246
311
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
247
 
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
 
312
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
248
313
  lex->select_lex.options= 0;
249
314
  lex->select_lex.init_order();
250
 
  lex->select_lex.group_list.clear();
 
315
  lex->select_lex.group_list.empty();
251
316
  lex->describe= 0;
 
317
  lex->subqueries= false;
252
318
  lex->derived_tables= 0;
253
319
  lex->lock_option= TL_READ;
254
320
  lex->leaf_tables_insert= 0;
255
 
  lex->var_list.clear();
 
321
  lex->parsing_options.reset();
 
322
  lex->empty_field_list_on_rset= 0;
256
323
  lex->select_lex.select_number= 1;
257
324
  lex->length=0;
258
325
  lex->select_lex.in_sum_expr=0;
259
 
  lex->select_lex.group_list.clear();
260
 
  lex->select_lex.order_list.clear();
 
326
  lex->select_lex.ftfunc_list_alloc.empty();
 
327
  lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
 
328
  lex->select_lex.group_list.empty();
 
329
  lex->select_lex.order_list.empty();
261
330
  lex->sql_command= SQLCOM_END;
262
331
  lex->duplicates= DUP_ERROR;
263
332
  lex->ignore= 0;
 
333
  lex->proc_list.first= 0;
264
334
  lex->escape_used= false;
265
335
  lex->query_tables= 0;
266
336
  lex->reset_query_tables_list(false);
272
342
  lex->nest_level=0 ;
273
343
  lex->allow_sum_func= 0;
274
344
  lex->in_sum_func= NULL;
275
 
  lex->type= 0;
 
345
  /*
 
346
    ok, there must be a better solution for this, long-term
 
347
    I tried "bzero" in the sql_yacc.yy code, but that for
 
348
    some reason made the values zero, even if they were set
 
349
  */
 
350
  lex->server_options.server_name= 0;
 
351
  lex->server_options.server_name_length= 0;
 
352
  lex->server_options.host= 0;
 
353
  lex->server_options.db= 0;
 
354
  lex->server_options.username= 0;
 
355
  lex->server_options.password= 0;
 
356
  lex->server_options.scheme= 0;
 
357
  lex->server_options.socket= 0;
 
358
  lex->server_options.owner= 0;
 
359
  lex->server_options.port= -1;
276
360
 
277
361
  lex->is_lex_started= true;
278
 
  lex->statement= NULL;
279
 
  
280
 
  lex->is_cross= false;
281
 
  lex->reset();
 
362
  return;
282
363
}
283
364
 
284
 
void LEX::end()
 
365
void lex_end(LEX *lex)
285
366
{
286
 
  if (yacc_yyss)
 
367
  if (lex->yacc_yyss)
287
368
  {
288
 
    free(yacc_yyss);
289
 
    free(yacc_yyvs);
290
 
    yacc_yyss= 0;
291
 
    yacc_yyvs= 0;
 
369
    my_free(lex->yacc_yyss, MYF(0));
 
370
    my_free(lex->yacc_yyvs, MYF(0));
 
371
    lex->yacc_yyss= 0;
 
372
    lex->yacc_yyvs= 0;
292
373
  }
293
374
 
294
 
  safe_delete(result);
295
 
  safe_delete(_create_table);
296
 
  _create_table= NULL;
297
 
  _create_field= NULL;
298
 
 
299
 
  result= 0;
300
 
  setCacheable(true);
301
 
 
302
 
  safe_delete(statement);
 
375
  /* release used plugins */
 
376
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
 
377
                     lex->plugins.elements);
 
378
  reset_dynamic(&lex->plugins);
 
379
 
 
380
  return;
303
381
}
304
382
 
305
 
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
 
383
 
 
384
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
306
385
{
307
 
  /* Plenty of memory for the largest lex symbol we have */
308
 
  char tok_upper[64];
309
386
  const char *tok= lip->get_tok_start();
310
 
  uint32_t tok_pos= 0;
311
 
  for (;tok_pos<len && tok_pos<63;tok_pos++)
312
 
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
313
 
  tok_upper[tok_pos]=0;
314
387
 
315
 
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
 
388
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
316
389
  if (symbol)
317
390
  {
318
391
    lip->yylval->symbol.symbol=symbol;
321
394
 
322
395
    return symbol->tok;
323
396
  }
324
 
 
325
397
  return 0;
326
398
}
327
399
 
 
400
/*
 
401
  Check if name is a keyword
 
402
 
 
403
  SYNOPSIS
 
404
    is_keyword()
 
405
    name      checked name (must not be empty)
 
406
    len       length of checked name
 
407
 
 
408
  RETURN VALUES
 
409
    0         name is a keyword
 
410
    1         name isn't a keyword
 
411
*/
 
412
 
 
413
bool is_keyword(const char *name, uint len)
 
414
{
 
415
  assert(len != 0);
 
416
  return get_hash_symbol(name,len,0)!=0;
 
417
}
 
418
 
328
419
bool is_lex_native_function(const LEX_STRING *name)
329
420
{
330
421
  assert(name != NULL);
331
 
  return (lookup_symbol(name->str, name->length, 1) != 0);
 
422
  return (get_hash_symbol(name->str, name->length, 1) != 0);
332
423
}
333
424
 
334
425
/* make a copy of token before ptr and set yytoklen */
335
 
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
 
426
 
 
427
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
336
428
{
337
429
  LEX_STRING tmp;
338
430
  lip->yyUnget();                       // ptr points now after last token char
339
431
  tmp.length=lip->yytoklen=length;
340
 
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
 
432
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
341
433
 
342
434
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
343
435
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
345
437
  return tmp;
346
438
}
347
439
 
348
 
/*
349
 
 todo:
350
 
   There are no dangerous charsets in mysql for function
351
 
   get_quoted_token yet. But it should be fixed in the
 
440
/* 
 
441
 todo: 
 
442
   There are no dangerous charsets in mysql for function 
 
443
   get_quoted_token yet. But it should be fixed in the 
352
444
   future to operate multichar strings (like ucs2)
353
445
*/
 
446
 
354
447
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
355
 
                                   uint32_t skip,
356
 
                                   uint32_t length, char quote)
 
448
                                   uint skip,
 
449
                                   uint length, char quote)
357
450
{
358
451
  LEX_STRING tmp;
359
452
  const char *from, *end;
360
453
  char *to;
361
454
  lip->yyUnget();                       // ptr points now after last token char
362
455
  tmp.length= lip->yytoklen=length;
363
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
456
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
364
457
  from= lip->get_tok_start() + skip;
365
458
  to= tmp.str;
366
459
  end= to+length;
385
478
  Return an unescaped text literal without quotes
386
479
  Fix sometimes to do only one scan of the string
387
480
*/
 
481
 
388
482
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
389
483
{
390
 
  unsigned char c,sep;
391
 
  bool found_escape= false;
392
 
  const CHARSET_INFO * const cs= lip->m_session->charset();
 
484
  register uchar c,sep;
 
485
  uint found_escape=0;
 
486
  CHARSET_INFO *cs= lip->m_thd->charset();
393
487
 
394
488
  lip->tok_bitmap= 0;
395
489
  sep= lip->yyGetLast();                        // String should end with this
397
491
  {
398
492
    c= lip->yyGet();
399
493
    lip->tok_bitmap|= c;
 
494
#ifdef USE_MB
400
495
    {
401
 
      if (use_mb(cs))
402
 
      {
403
 
        int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
404
 
        if (l != 0) 
405
 
        {
406
 
          lip->skip_binary(l-1);
407
 
          continue;
408
 
        }
 
496
      int l;
 
497
      if (use_mb(cs) &&
 
498
          (l = my_ismbchar(cs,
 
499
                           lip->get_ptr() -1,
 
500
                           lip->get_end_of_query()))) {
 
501
        lip->skip_binary(l-1);
 
502
        continue;
409
503
      }
410
504
    }
 
505
#endif
411
506
    if (c == '\\')
412
507
    {                                   // Escaped character
413
 
      found_escape= true;
 
508
      found_escape=1;
414
509
      if (lip->eof())
415
 
        return 0;
 
510
        return 0;
416
511
      lip->yySkip();
417
512
    }
418
513
    else if (c == sep)
419
514
    {
420
515
      if (c == lip->yyGet())            // Check if two separators in a row
421
516
      {
422
 
        found_escape= true;                 // duplicate. Remember for delete
423
 
        continue;
 
517
        found_escape=1;                 // duplicate. Remember for delete
 
518
        continue;
424
519
      }
425
520
      else
426
521
        lip->yyUnget();
432
527
      str= lip->get_tok_start();
433
528
      end= lip->get_ptr();
434
529
      /* Extract the text from the token */
435
 
      str+= pre_skip;
436
 
      end-= post_skip;
 
530
      str += pre_skip;
 
531
      end -= post_skip;
437
532
      assert(end >= str);
438
533
 
439
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
440
 
        return (char*) "";              // memory::SqlAlloc has set error flag
 
534
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
535
        return (char*) "";              // Sql_alloc has set error flag
441
536
 
442
537
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
443
538
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
444
539
 
445
 
      if (! found_escape)
 
540
      if (!found_escape)
446
541
      {
447
 
        lip->yytoklen= (uint32_t) (end-str);
448
 
        memcpy(start, str, lip->yytoklen);
449
 
        start[lip->yytoklen]= 0;
 
542
        lip->yytoklen=(uint) (end-str);
 
543
        memcpy(start,str,lip->yytoklen);
 
544
        start[lip->yytoklen]=0;
450
545
      }
451
546
      else
452
547
      {
453
548
        char *to;
454
549
 
455
 
        for (to= start; str != end; str++)
456
 
        {
457
 
          if (use_mb(cs))
458
 
          {
459
 
            int l= my_ismbchar(cs, str, end);
460
 
            if (l != 0)
461
 
            {
462
 
              while (l--)
463
 
                *to++= *str++;
464
 
              str--;
465
 
              continue;
466
 
            }
467
 
          }
468
 
          if (*str == '\\' && (str + 1) != end)
469
 
          {
470
 
            switch (*++str) {
471
 
            case 'n':
472
 
              *to++= '\n';
473
 
              break;
474
 
            case 't':
475
 
              *to++= '\t';
476
 
              break;
477
 
            case 'r':
478
 
              *to++= '\r';
479
 
              break;
480
 
            case 'b':
481
 
              *to++= '\b';
482
 
              break;
483
 
            case '0':
484
 
              *to++= 0;                 // Ascii null
485
 
              break;
486
 
            case 'Z':                   // ^Z must be escaped on Win32
487
 
              *to++= '\032';
488
 
              break;
489
 
            case '_':
490
 
            case '%':
491
 
              *to++= '\\';              // remember prefix for wildcard
492
 
              /* Fall through */
493
 
            default:
 
550
        for (to=start ; str != end ; str++)
 
551
        {
 
552
#ifdef USE_MB
 
553
          int l;
 
554
          if (use_mb(cs) &&
 
555
              (l = my_ismbchar(cs, str, end))) {
 
556
              while (l--)
 
557
                  *to++ = *str++;
 
558
              str--;
 
559
              continue;
 
560
          }
 
561
#endif
 
562
          if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
563
              *str == '\\' && str+1 != end)
 
564
          {
 
565
            switch(*++str) {
 
566
            case 'n':
 
567
              *to++='\n';
 
568
              break;
 
569
            case 't':
 
570
              *to++= '\t';
 
571
              break;
 
572
            case 'r':
 
573
              *to++ = '\r';
 
574
              break;
 
575
            case 'b':
 
576
              *to++ = '\b';
 
577
              break;
 
578
            case '0':
 
579
              *to++= 0;                 // Ascii null
 
580
              break;
 
581
            case 'Z':                   // ^Z must be escaped on Win32
 
582
              *to++='\032';
 
583
              break;
 
584
            case '_':
 
585
            case '%':
 
586
              *to++= '\\';              // remember prefix for wildcard
 
587
              /* Fall through */
 
588
            default:
494
589
              *to++= *str;
495
 
              break;
496
 
            }
497
 
          }
498
 
          else if (*str == sep)
499
 
            *to++= *str++;              // Two ' or "
500
 
          else
501
 
            *to++ = *str;
502
 
        }
503
 
        *to= 0;
504
 
        lip->yytoklen= (uint32_t) (to - start);
 
590
              break;
 
591
            }
 
592
          }
 
593
          else if (*str == sep)
 
594
            *to++= *str++;              // Two ' or "
 
595
          else
 
596
            *to++ = *str;
 
597
        }
 
598
        *to=0;
 
599
        lip->yytoklen=(uint) (to-start);
505
600
      }
506
601
      return start;
507
602
    }
513
608
/*
514
609
** Calc type of integer; long integer, int64_t integer or real.
515
610
** Returns smallest type that match the string.
516
 
** When using uint64_t values the result is converted to a real
 
611
** When using unsigned long long values the result is converted to a real
517
612
** because else they will be unexpected sign changes because all calculation
518
613
** is done with int64_t or double.
519
614
*/
520
615
 
521
 
static const char *long_str= "2147483647";
522
 
static const uint32_t long_len= 10;
523
 
static const char *signed_long_str= "-2147483648";
524
 
static const char *int64_t_str= "9223372036854775807";
525
 
static const uint32_t int64_t_len= 19;
526
 
static const char *signed_int64_t_str= "-9223372036854775808";
527
 
static const uint32_t signed_int64_t_len= 19;
528
 
static const char *unsigned_int64_t_str= "18446744073709551615";
529
 
static const uint32_t unsigned_int64_t_len= 20;
 
616
static const char *long_str="2147483647";
 
617
static const uint long_len=10;
 
618
static const char *signed_long_str="-2147483648";
 
619
static const char *int64_t_str="9223372036854775807";
 
620
static const uint int64_t_len=19;
 
621
static const char *signed_int64_t_str="-9223372036854775808";
 
622
static const uint signed_int64_t_len=19;
 
623
static const char *unsigned_int64_t_str="18446744073709551615";
 
624
static const uint unsigned_int64_t_len=20;
530
625
 
531
 
static inline uint32_t int_token(const char *str,uint32_t length)
 
626
static inline uint int_token(const char *str,uint length)
532
627
{
533
628
  if (length < long_len)                        // quick normal case
534
629
    return NUM;
550
645
  if (length < long_len)
551
646
    return NUM;
552
647
 
553
 
  uint32_t smaller,bigger;
 
648
  uint smaller,bigger;
554
649
  const char *cmp;
555
650
  if (neg)
556
651
  {
597
692
    }
598
693
  }
599
694
  while (*cmp && *cmp++ == *str++) ;
600
 
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
 
695
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
601
696
}
602
697
 
603
 
} /* namespace drizzled */
 
698
 
604
699
/*
605
 
  base_sql_lex remember the following states from the following sql_baselex()
 
700
  MYSQLlex remember the following states from the following MYSQLlex()
606
701
 
607
702
  - MY_LEX_EOQ                  Found end of query
608
703
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
609
704
                                (which can't be followed by a signed number)
610
705
*/
611
 
int base_sql_lex(union ParserType *yylval, drizzled::Session *session)
 
706
 
 
707
int MYSQLlex(void *arg, void *yythd)
612
708
{
613
 
  drizzled::Lex_input_stream *lip= session->m_lip;
 
709
  THD *thd= (THD *)yythd;
 
710
  Lex_input_stream *lip= thd->m_lip;
 
711
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
712
  int token;
615
713
 
616
714
  if (lip->lookahead_token != END_OF_INPUT)
626
724
    return token;
627
725
  }
628
726
 
629
 
  token= drizzled::lex_one_token(yylval, session);
 
727
  token= lex_one_token(arg, yythd);
630
728
 
631
729
  switch(token) {
632
730
  case WITH:
633
731
    /*
634
 
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
 
732
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
635
733
      which makes the grammar LALR(2).
636
734
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
637
735
      to transform the grammar into a LALR(1) grammar,
638
736
      which sql_yacc.yy can process.
639
737
    */
640
 
    token= drizzled::lex_one_token(yylval, session);
641
 
    if (token == ROLLUP_SYM)
642
 
    {
 
738
    token= lex_one_token(arg, yythd);
 
739
    switch(token) {
 
740
    case CUBE_SYM:
 
741
      return WITH_CUBE_SYM;
 
742
    case ROLLUP_SYM:
643
743
      return WITH_ROLLUP_SYM;
644
 
    }
645
 
    else
646
 
    {
 
744
    default:
647
745
      /*
648
746
        Save the token following 'WITH'
649
747
      */
652
750
      lip->lookahead_token= token;
653
751
      return WITH;
654
752
    }
 
753
    break;
655
754
  default:
656
755
    break;
657
756
  }
659
758
  return token;
660
759
}
661
760
 
662
 
namespace drizzled
663
 
{
664
 
 
665
 
int lex_one_token(ParserType *yylval, drizzled::Session *session)
666
 
{
667
 
  unsigned char c= 0; /* Just set to shutup GCC */
 
761
int lex_one_token(void *arg, void *yythd)
 
762
{
 
763
  register unsigned char c= 0; /* Just set to shutup GCC */
668
764
  bool comment_closed;
669
765
  int   tokval, result_state;
670
766
  unsigned int length;
671
767
  enum my_lex_states state;
672
 
  Lex_input_stream *lip= session->m_lip;
673
 
  LEX *lex= session->getLex();
674
 
  const CHARSET_INFO * const cs= session->charset();
675
 
  unsigned char *state_map= cs->state_map;
676
 
  unsigned char *ident_map= cs->ident_map;
 
768
  THD *thd= (THD *)yythd;
 
769
  Lex_input_stream *lip= thd->m_lip;
 
770
  LEX *lex= thd->lex;
 
771
  YYSTYPE *yylval=(YYSTYPE*) arg;
 
772
  CHARSET_INFO *cs= thd->charset();
 
773
  uchar *state_map= cs->state_map;
 
774
  uchar *ident_map= cs->ident_map;
677
775
 
678
776
  lip->yylval=yylval;                   // The global state
679
777
 
688
786
      // Skip starting whitespace
689
787
      while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
690
788
      {
691
 
        if (c == '\n')
692
 
          lip->yylineno++;
 
789
        if (c == '\n')
 
790
          lip->yylineno++;
693
791
 
694
792
        lip->yySkip();
695
793
      }
702
800
    case MY_LEX_ESCAPE:
703
801
      if (lip->yyGet() == 'N')
704
802
      {                                 // Allow \N as shortcut for NULL
705
 
        yylval->lex_str.str=(char*) "\\N";
706
 
        yylval->lex_str.length=2;
707
 
        return NULL_SYM;
 
803
        yylval->lex_str.str=(char*) "\\N";
 
804
        yylval->lex_str.length=2;
 
805
        return NULL_SYM;
708
806
      }
709
807
    case MY_LEX_CHAR:                   // Unknown or single char token
710
808
    case MY_LEX_SKIP:                   // This should not happen
717
815
      }
718
816
 
719
817
      if (c != ')')
720
 
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
818
        lip->next_state= MY_LEX_START;  // Allow signed numbers
721
819
 
722
820
      if (c == ',')
723
821
      {
732
830
        */
733
831
        lip->restart_token();
734
832
      }
 
833
      else
 
834
      {
 
835
        /*
 
836
          Check for a placeholder: it should not precede a possible identifier
 
837
          because of binlogging: when a placeholder is replaced with
 
838
          its value in a query for the binlog, the query must stay
 
839
          grammatically correct.
 
840
        */
 
841
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[(uint8_t)(lip->yyPeek())])
 
842
        return(PARAM_MARKER);
 
843
      }
735
844
 
736
845
      return((int) c);
737
846
 
 
847
    case MY_LEX_IDENT_OR_NCHAR:
 
848
      if (lip->yyPeek() != '\'')
 
849
      {
 
850
        state= MY_LEX_IDENT;
 
851
        break;
 
852
      }
 
853
      /* Found N'string' */
 
854
      lip->yySkip();                         // Skip '
 
855
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
 
856
      {
 
857
        state= MY_LEX_CHAR;             // Read char by char
 
858
        break;
 
859
      }
 
860
      yylval->lex_str.length= lip->yytoklen;
 
861
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
862
      return(NCHAR_STRING);
 
863
 
738
864
    case MY_LEX_IDENT_OR_HEX:
739
865
      if (lip->yyPeek() == '\'')
740
866
      {                                 // Found x'hex-number'
741
 
        state= MY_LEX_HEX_NUMBER;
742
 
        break;
 
867
        state= MY_LEX_HEX_NUMBER;
 
868
        break;
743
869
      }
744
870
    case MY_LEX_IDENT_OR_BIN:
745
871
      if (lip->yyPeek() == '\'')
749
875
      }
750
876
    case MY_LEX_IDENT:
751
877
      const char *start;
 
878
#if defined(USE_MB) && defined(USE_MB_IDENT)
752
879
      if (use_mb(cs))
753
880
      {
754
 
        result_state= IDENT_QUOTED;
 
881
        result_state= IDENT_QUOTED;
755
882
        if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
756
883
        {
757
884
          int l = my_ismbchar(cs,
767
894
        {
768
895
          if (my_mbcharlen(cs, c) > 1)
769
896
          {
770
 
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
771
 
            if (l == 0)
 
897
            int l;
 
898
            if ((l = my_ismbchar(cs,
 
899
                                 lip->get_ptr() -1,
 
900
                                 lip->get_end_of_query())) == 0)
772
901
              break;
773
902
            lip->skip_binary(l-1);
774
903
          }
775
904
        }
776
905
      }
777
906
      else
 
907
#endif
778
908
      {
779
909
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
780
910
        /* If there were non-ASCII characters, mark that we must convert */
791
921
        for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
792
922
      }
793
923
      if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
794
 
              lip->next_state=MY_LEX_IDENT_SEP;
 
924
        lip->next_state=MY_LEX_IDENT_SEP;
795
925
      else
796
926
      {                                 // '(' must follow directly if function
797
927
        lip->yyUnget();
798
 
        if ((tokval = find_keyword(lip, length, c == '(')))
799
 
        {
800
 
          lip->next_state= MY_LEX_START;        // Allow signed numbers
801
 
          return(tokval);               // Was keyword
802
 
        }
 
928
        if ((tokval = find_keyword(lip, length, c == '(')))
 
929
        {
 
930
          lip->next_state= MY_LEX_START;        // Allow signed numbers
 
931
          return(tokval);               // Was keyword
 
932
        }
803
933
        lip->yySkip();                  // next state does a unget
804
934
      }
805
935
      yylval->lex_str=get_token(lip, 0, length);
806
936
 
 
937
      /*
 
938
         Note: "SELECT _bla AS 'alias'"
 
939
         _bla should be considered as a IDENT if charset haven't been found.
 
940
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
 
941
         producing an error.
 
942
      */
 
943
 
 
944
      if (yylval->lex_str.str[0] == '_')
 
945
      {
 
946
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
947
                                                MY_CS_PRIMARY, MYF(0));
 
948
        if (cs)
 
949
        {
 
950
          yylval->charset= cs;
 
951
          lip->m_underscore_cs= cs;
 
952
 
 
953
          lip->body_utf8_append(lip->m_cpp_text_start,
 
954
                                lip->get_cpp_tok_start() + length);
 
955
          return(UNDERSCORE_CHARSET);
 
956
        }
 
957
      }
 
958
 
807
959
      lip->body_utf8_append(lip->m_cpp_text_start);
808
960
 
809
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
961
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
962
                                    lip->m_cpp_text_end);
810
963
 
811
964
      return(result_state);                     // IDENT or IDENT_QUOTED
812
965
 
816
969
      c= lip->yyGet();                  // should be '.'
817
970
      lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
818
971
      if (!ident_map[(uint8_t)lip->yyPeek()])            // Probably ` or "
819
 
        lip->next_state= MY_LEX_START;
 
972
        lip->next_state= MY_LEX_START;
820
973
      return((int) c);
821
974
 
822
975
    case MY_LEX_NUMBER_IDENT:           // number or ident which num-start
855
1008
      while (my_isdigit(cs, (c = lip->yyGet()))) ;
856
1009
      if (!ident_map[c])
857
1010
      {                                 // Can't be identifier
858
 
        state=MY_LEX_INT_OR_REAL;
859
 
        break;
 
1011
        state=MY_LEX_INT_OR_REAL;
 
1012
        break;
860
1013
      }
861
1014
      if (c == 'e' || c == 'E')
862
1015
      {
863
 
        // The following test is written this way to allow numbers of type 1e1
 
1016
        // The following test is written this way to allow numbers of type 1e1
864
1017
        if (my_isdigit(cs,lip->yyPeek()) ||
865
1018
            (c=(lip->yyGet())) == '+' || c == '-')
866
 
        {                               // Allow 1E+10
 
1019
        {                               // Allow 1E+10
867
1020
          if (my_isdigit(cs,lip->yyPeek()))     // Number must have digit after sign
868
 
          {
 
1021
          {
869
1022
            lip->yySkip();
870
1023
            while (my_isdigit(cs,lip->yyGet())) ;
871
1024
            yylval->lex_str=get_token(lip, 0, lip->yyLength());
872
 
            return(FLOAT_NUM);
873
 
          }
874
 
        }
 
1025
            return(FLOAT_NUM);
 
1026
          }
 
1027
        }
875
1028
        lip->yyUnget();
876
1029
      }
877
1030
      // fall through
878
1031
    case MY_LEX_IDENT_START:                    // We come here after '.'
879
1032
      result_state= IDENT;
 
1033
#if defined(USE_MB) && defined(USE_MB_IDENT)
880
1034
      if (use_mb(cs))
881
1035
      {
882
 
        result_state= IDENT_QUOTED;
 
1036
        result_state= IDENT_QUOTED;
883
1037
        while (ident_map[c=lip->yyGet()])
884
1038
        {
885
1039
          if (my_mbcharlen(cs, c) > 1)
886
1040
          {
887
 
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
888
 
            if (l == 0)
 
1041
            int l;
 
1042
            if ((l = my_ismbchar(cs,
 
1043
                                 lip->get_ptr() -1,
 
1044
                                 lip->get_end_of_query())) == 0)
889
1045
              break;
890
1046
            lip->skip_binary(l-1);
891
1047
          }
892
1048
        }
893
1049
      }
894
1050
      else
 
1051
#endif
895
1052
      {
896
1053
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
897
1054
        /* If there were non-ASCII characters, mark that we must convert */
898
1055
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
899
1056
      }
900
1057
      if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
901
 
        lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
 
1058
        lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
902
1059
 
903
1060
      yylval->lex_str= get_token(lip, 0, lip->yyLength());
904
1061
 
905
1062
      lip->body_utf8_append(lip->m_cpp_text_start);
906
1063
 
907
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1064
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1065
                                    lip->m_cpp_text_end);
908
1066
 
909
1067
      return(result_state);
910
1068
 
911
1069
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
912
1070
    {
913
 
      uint32_t double_quotes= 0;
 
1071
      uint double_quotes= 0;
914
1072
      char quote_char= c;                       // Used char
915
1073
      while ((c=lip->yyGet()))
916
1074
      {
917
 
        int var_length;
918
 
        if ((var_length= my_mbcharlen(cs, c)) == 1)
919
 
        {
920
 
          if (c == quote_char)
921
 
          {
922
 
                  if (lip->yyPeek() != quote_char)
923
 
              break;
924
 
                  c=lip->yyGet();
925
 
            double_quotes++;
926
 
            continue;
927
 
          }
928
 
        }
929
 
        else if (var_length < 1)
930
 
          break;                                // Error
 
1075
        int var_length;
 
1076
        if ((var_length= my_mbcharlen(cs, c)) == 1)
 
1077
        {
 
1078
          if (c == quote_char)
 
1079
          {
 
1080
            if (lip->yyPeek() != quote_char)
 
1081
              break;
 
1082
            c=lip->yyGet();
 
1083
            double_quotes++;
 
1084
            continue;
 
1085
          }
 
1086
        }
 
1087
#ifdef USE_MB
 
1088
        else if (var_length < 1)
 
1089
          break;                                // Error
931
1090
        lip->skip_binary(var_length-1);
 
1091
#endif
932
1092
      }
933
1093
      if (double_quotes)
934
 
              yylval->lex_str=get_quoted_token(lip, 1, lip->yyLength() - double_quotes -1, quote_char);
 
1094
        yylval->lex_str=get_quoted_token(lip, 1,
 
1095
                                         lip->yyLength() - double_quotes -1,
 
1096
                                         quote_char);
935
1097
      else
936
1098
        yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
937
1099
      if (c == quote_char)
938
1100
        lip->yySkip();                  // Skip end `
939
1101
      lip->next_state= MY_LEX_START;
 
1102
 
940
1103
      lip->body_utf8_append(lip->m_cpp_text_start);
941
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1104
 
 
1105
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1106
                                    lip->m_cpp_text_end);
 
1107
 
942
1108
      return(IDENT_QUOTED);
943
1109
    }
944
1110
    case MY_LEX_INT_OR_REAL:            // Complete int or incomplete real
945
1111
      if (c != '.')
946
1112
      {                                 // Found complete integer number.
947
1113
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
948
 
        return int_token(yylval->lex_str.str,yylval->lex_str.length);
 
1114
        return int_token(yylval->lex_str.str,yylval->lex_str.length);
949
1115
      }
950
1116
      // fall through
951
1117
    case MY_LEX_REAL:                   // Incomplete real number
954
1120
      if (c == 'e' || c == 'E')
955
1121
      {
956
1122
        c = lip->yyGet();
957
 
        if (c == '-' || c == '+')
958
 
                c = lip->yyGet();                     // Skip sign
959
 
        if (!my_isdigit(cs,c))
960
 
        {                               // No digit after sign
961
 
          state= MY_LEX_CHAR;
962
 
          break;
963
 
        }
 
1123
        if (c == '-' || c == '+')
 
1124
          c = lip->yyGet();                     // Skip sign
 
1125
        if (!my_isdigit(cs,c))
 
1126
        {                               // No digit after sign
 
1127
          state= MY_LEX_CHAR;
 
1128
          break;
 
1129
        }
964
1130
        while (my_isdigit(cs,lip->yyGet())) ;
965
1131
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
966
 
        return(FLOAT_NUM);
 
1132
        return(FLOAT_NUM);
967
1133
      }
968
1134
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
969
1135
      return(DECIMAL_NUM);
1000
1166
        lip->yySkip();
1001
1167
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1002
1168
      {
1003
 
        lip->next_state= MY_LEX_START;  // Allow signed numbers
1004
 
        return(tokval);
 
1169
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
1170
        return(tokval);
1005
1171
      }
1006
1172
      state = MY_LEX_CHAR;              // Something fishy found
1007
1173
      break;
1016
1182
      }
1017
1183
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1018
1184
      {
1019
 
        lip->next_state= MY_LEX_START;  // Found long op
1020
 
        return(tokval);
 
1185
        lip->next_state= MY_LEX_START;  // Found long op
 
1186
        return(tokval);
1021
1187
      }
1022
1188
      state = MY_LEX_CHAR;              // Something fishy found
1023
1189
      break;
1025
1191
    case MY_LEX_BOOL:
1026
1192
      if (c != lip->yyPeek())
1027
1193
      {
1028
 
        state=MY_LEX_CHAR;
1029
 
        break;
 
1194
        state=MY_LEX_CHAR;
 
1195
        break;
1030
1196
      }
1031
1197
      lip->yySkip();
1032
1198
      tokval = find_keyword(lip,2,0);   // Is a bool operator
1043
1209
    case MY_LEX_STRING:                 // Incomplete text string
1044
1210
      if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
1045
1211
      {
1046
 
        state= MY_LEX_CHAR;             // Read char by char
1047
 
        break;
 
1212
        state= MY_LEX_CHAR;             // Read char by char
 
1213
        break;
1048
1214
      }
1049
1215
      yylval->lex_str.length=lip->yytoklen;
1050
1216
 
1051
1217
      lip->body_utf8_append(lip->m_cpp_text_start);
1052
1218
 
1053
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1219
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1220
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
 
1221
        lip->m_cpp_text_end);
 
1222
 
 
1223
      lip->m_underscore_cs= NULL;
1054
1224
 
1055
1225
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1056
1226
      return(TEXT_STRING);
1064
1234
    case MY_LEX_LONG_COMMENT:           /* Long C comment? */
1065
1235
      if (lip->yyPeek() != '*')
1066
1236
      {
1067
 
        state=MY_LEX_CHAR;              // Probable division
1068
 
        break;
 
1237
        state=MY_LEX_CHAR;              // Probable division
 
1238
        break;
1069
1239
      }
1070
1240
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1071
1241
      /* Reject '/' '*', since we might need to turn off the echo */
1082
1252
 
1083
1253
        /*
1084
1254
          The special comment format is very strict:
1085
 
          '/' '*' '!', followed by digits ended by a non-digit.
1086
 
          There must be at least 5 digits for it to count
 
1255
          '/' '*' '!', followed by exactly
 
1256
          1 digit (major), 2 digits (minor), then 2 digits (dot).
 
1257
          32302 -> 3.23.02
 
1258
          50032 -> 5.0.32
 
1259
          50114 -> 5.1.14
1087
1260
        */
1088
 
        const int MAX_VERSION_SIZE= 16;
1089
 
        char version_str[MAX_VERSION_SIZE];
1090
 
 
1091
 
        int pos= 0;
1092
 
        do
1093
 
        {
1094
 
          version_str[pos]= lip->yyPeekn(pos);
1095
 
          pos++;
1096
 
        } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1097
 
        version_str[pos]= 0;
1098
 
 
1099
 
        /* To keep some semblance of compatibility, we impose a 5 digit floor */
1100
 
        if (pos > 4)
1101
 
        {
1102
 
          uint64_t version;
1103
 
          version=strtoll(version_str, NULL, 10);
 
1261
        char version_str[6];
 
1262
        version_str[0]= lip->yyPeekn(0);
 
1263
        version_str[1]= lip->yyPeekn(1);
 
1264
        version_str[2]= lip->yyPeekn(2);
 
1265
        version_str[3]= lip->yyPeekn(3);
 
1266
        version_str[4]= lip->yyPeekn(4);
 
1267
        version_str[5]= 0;
 
1268
        if (  my_isdigit(cs, version_str[0])
 
1269
           && my_isdigit(cs, version_str[1])
 
1270
           && my_isdigit(cs, version_str[2])
 
1271
           && my_isdigit(cs, version_str[3])
 
1272
           && my_isdigit(cs, version_str[4])
 
1273
           )
 
1274
        {
 
1275
          ulong version;
 
1276
          version=strtol(version_str, NULL, 10);
1104
1277
 
1105
1278
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1106
 
          lip->yySkipn(pos-1);
 
1279
          lip->yySkipn(5);
1107
1280
 
1108
 
          if (version <= DRIZZLE_VERSION_ID)
 
1281
          if (version <= MYSQL_VERSION_ID)
1109
1282
          {
1110
1283
            /* Expand the content of the special comment as real code */
1111
1284
            lip->set_echo(true);
1172
1345
        state=MY_LEX_START;
1173
1346
      }
1174
1347
      else
1175
 
        state=MY_LEX_CHAR;              // Return '*'
 
1348
        state=MY_LEX_CHAR;              // Return '*'
1176
1349
      break;
1177
1350
    case MY_LEX_SET_VAR:                // Check if ':='
1178
1351
      if (lip->yyPeek() != '=')
1179
1352
      {
1180
 
        state=MY_LEX_CHAR;              // Return ':'
1181
 
        break;
 
1353
        state=MY_LEX_CHAR;              // Return ':'
 
1354
        break;
1182
1355
      }
1183
1356
      lip->yySkip();
1184
1357
      return (SET_VAR);
1185
1358
    case MY_LEX_SEMICOLON:                      // optional line terminator
1186
1359
      if (lip->yyPeek())
1187
1360
      {
 
1361
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && 
 
1362
            !lip->stmt_prepare_mode)
 
1363
        {
 
1364
          lip->found_semicolon= lip->get_ptr();
 
1365
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1366
          lip->next_state= MY_LEX_END;
 
1367
          lip->set_echo(true);
 
1368
          return (END_OF_INPUT);
 
1369
        }
1188
1370
        state= MY_LEX_CHAR;             // Return ';'
1189
 
        break;
 
1371
        break;
1190
1372
      }
1191
1373
      lip->next_state=MY_LEX_END;       // Mark for next loop
1192
1374
      return(END_OF_INPUT);
1207
1389
      break;
1208
1390
    case MY_LEX_END:
1209
1391
      lip->next_state=MY_LEX_END;
1210
 
      return false;                     // We found end of input last time
1211
 
 
 
1392
      return(0);                        // We found end of input last time
 
1393
      
1212
1394
      /* Actually real shouldn't start with . but allow them anyhow */
1213
1395
    case MY_LEX_REAL_OR_POINT:
1214
1396
      if (my_isdigit(cs,lip->yyPeek()))
1215
 
        state= MY_LEX_REAL;             // Real
 
1397
        state = MY_LEX_REAL;            // Real
1216
1398
      else
1217
1399
      {
1218
 
        state= MY_LEX_IDENT_SEP;        // return '.'
 
1400
        state= MY_LEX_IDENT_SEP;        // return '.'
1219
1401
        lip->yyUnget();                 // Put back '.'
1220
1402
      }
1221
1403
      break;
1224
1406
      case MY_LEX_STRING:
1225
1407
      case MY_LEX_USER_VARIABLE_DELIMITER:
1226
1408
      case MY_LEX_STRING_OR_DELIMITER:
1227
 
        break;
 
1409
        break;
1228
1410
      case MY_LEX_USER_END:
1229
 
        lip->next_state=MY_LEX_SYSTEM_VAR;
1230
 
        break;
 
1411
        lip->next_state=MY_LEX_SYSTEM_VAR;
 
1412
        break;
1231
1413
      default:
1232
 
        lip->next_state=MY_LEX_HOSTNAME;
1233
 
        break;
 
1414
        lip->next_state=MY_LEX_HOSTNAME;
 
1415
        break;
1234
1416
      }
1235
1417
      yylval->lex_str.str=(char*) lip->get_ptr();
1236
1418
      yylval->lex_str.length=1;
1237
1419
      return((int) '@');
1238
1420
    case MY_LEX_HOSTNAME:               // end '@' of user@hostname
1239
1421
      for (c=lip->yyGet() ;
1240
 
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
 
1422
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
1241
1423
           c= lip->yyGet()) ;
1242
1424
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
1243
1425
      return(LEX_HOSTNAME);
1252
1434
      return((int) '@');
1253
1435
    case MY_LEX_IDENT_OR_KEYWORD:
1254
1436
      /*
1255
 
        We come here when we have found two '@' in a row.
1256
 
        We should now be able to handle:
1257
 
        [(global | local | session) .]variable_name
 
1437
        We come here when we have found two '@' in a row.
 
1438
        We should now be able to handle:
 
1439
        [(global | local | session) .]variable_name
1258
1440
      */
1259
1441
 
1260
1442
      for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1262
1444
      result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1263
1445
 
1264
1446
      if (c == '.')
1265
 
        lip->next_state=MY_LEX_IDENT_SEP;
 
1447
        lip->next_state=MY_LEX_IDENT_SEP;
1266
1448
      length= lip->yyLength();
1267
1449
      if (length == 0)
1268
1450
        return(ABORT_SYM);              // Names must be nonempty.
1269
1451
      if ((tokval= find_keyword(lip, length,0)))
1270
1452
      {
1271
1453
        lip->yyUnget();                         // Put back 'c'
1272
 
        return(tokval);                         // Was keyword
 
1454
        return(tokval);                         // Was keyword
1273
1455
      }
1274
1456
      yylval->lex_str=get_token(lip, 0, length);
1275
1457
 
1276
1458
      lip->body_utf8_append(lip->m_cpp_text_start);
1277
1459
 
1278
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1460
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1461
                                    lip->m_cpp_text_end);
1279
1462
 
1280
1463
      return(result_state);
1281
1464
    }
1282
1465
  }
1283
1466
}
1284
1467
 
1285
 
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
 
1468
 
 
1469
/**
 
1470
  Construct a copy of this object to be used for mysql_alter_table
 
1471
  and mysql_create_table.
 
1472
 
 
1473
  Historically, these two functions modify their Alter_info
 
1474
  arguments. This behaviour breaks re-execution of prepared
 
1475
  statements and stored procedures and is compensated by always
 
1476
  supplying a copy of Alter_info to these functions.
 
1477
 
 
1478
  @return You need to use check the error in THD for out
 
1479
  of memory condition after calling this function.
 
1480
*/
 
1481
 
 
1482
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
 
1483
  :drop_list(rhs.drop_list, mem_root),
 
1484
  alter_list(rhs.alter_list, mem_root),
 
1485
  key_list(rhs.key_list, mem_root),
 
1486
  create_list(rhs.create_list, mem_root),
 
1487
  flags(rhs.flags),
 
1488
  keys_onoff(rhs.keys_onoff),
 
1489
  tablespace_op(rhs.tablespace_op),
 
1490
  no_parts(rhs.no_parts),
 
1491
  build_method(rhs.build_method),
 
1492
  datetime_field(rhs.datetime_field),
 
1493
  error_if_not_empty(rhs.error_if_not_empty)
 
1494
{
 
1495
  /*
 
1496
    Make deep copies of used objects.
 
1497
    This is not a fully deep copy - clone() implementations
 
1498
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
 
1499
    do not copy string constants. At the same length the only
 
1500
    reason we make a copy currently is that ALTER/CREATE TABLE
 
1501
    code changes input Alter_info definitions, but string
 
1502
    constants never change.
 
1503
  */
 
1504
  list_copy_and_replace_each_value(drop_list, mem_root);
 
1505
  list_copy_and_replace_each_value(alter_list, mem_root);
 
1506
  list_copy_and_replace_each_value(key_list, mem_root);
 
1507
  list_copy_and_replace_each_value(create_list, mem_root);
 
1508
}
 
1509
 
 
1510
 
 
1511
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
1286
1512
{
1287
1513
  /*
1288
1514
    TODO:
1289
1515
    This code assumes that there are no multi-bytes characters
1290
1516
    that can be considered white-space.
1291
1517
  */
 
1518
 
1292
1519
  while ((str->length > 0) && (my_isspace(cs, str->str[0])))
1293
1520
  {
1294
 
    str->length--;
1295
 
    str->str++;
 
1521
    str->length --;
 
1522
    str->str ++;
1296
1523
  }
1297
1524
 
1298
1525
  /*
1301
1528
  */
1302
1529
  while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
1303
1530
  {
1304
 
    str->length--;
 
1531
    str->length --;
1305
1532
  }
1306
1533
}
1307
1534
 
 
1535
 
1308
1536
/*
1309
 
  Select_Lex structures initialisations
 
1537
  st_select_lex structures initialisations
1310
1538
*/
1311
 
void Select_Lex_Node::init_query()
 
1539
 
 
1540
void st_select_lex_node::init_query()
1312
1541
{
1313
1542
  options= 0;
1314
1543
  linkage= UNSPECIFIED_TYPE;
1315
1544
  no_error= no_table_names_allowed= 0;
1316
 
  uncacheable.reset();
1317
 
}
1318
 
 
1319
 
void Select_Lex_Node::init_select()
1320
 
{
1321
 
}
1322
 
 
1323
 
void Select_Lex_Unit::init_query()
1324
 
{
1325
 
  Select_Lex_Node::init_query();
 
1545
  uncacheable= 0;
 
1546
}
 
1547
 
 
1548
void st_select_lex_node::init_select()
 
1549
{
 
1550
}
 
1551
 
 
1552
void st_select_lex_unit::init_query()
 
1553
{
 
1554
  st_select_lex_node::init_query();
1326
1555
  linkage= GLOBAL_OPTIONS_TYPE;
1327
1556
  global_parameters= first_select();
1328
1557
  select_limit_cnt= HA_POS_ERROR;
1334
1563
  table= 0;
1335
1564
  fake_select_lex= 0;
1336
1565
  cleaned= 0;
1337
 
  item_list.clear();
 
1566
  item_list.empty();
1338
1567
  describe= 0;
1339
1568
  found_rows_for_union= 0;
1340
1569
}
1341
1570
 
1342
 
void Select_Lex::init_query()
 
1571
void st_select_lex::init_query()
1343
1572
{
1344
 
  Select_Lex_Node::init_query();
1345
 
  table_list.clear();
1346
 
  top_join_list.clear();
 
1573
  st_select_lex_node::init_query();
 
1574
  table_list.empty();
 
1575
  top_join_list.empty();
1347
1576
  join_list= &top_join_list;
1348
1577
  embedding= leaf_tables= 0;
1349
 
  item_list.clear();
 
1578
  item_list.empty();
1350
1579
  join= 0;
1351
 
  having= where= 0;
 
1580
  having= prep_having= where= prep_where= 0;
1352
1581
  olap= UNSPECIFIED_OLAP_TYPE;
1353
1582
  having_fix_field= 0;
1354
1583
  context.select_lex= this;
1365
1594
  parent_lex->push_context(&context);
1366
1595
  cond_count= between_count= with_wild= 0;
1367
1596
  max_equal_elems= 0;
 
1597
  conds_processed_with_permanent_arena= 0;
1368
1598
  ref_pointer_array= 0;
1369
1599
  select_n_where_fields= 0;
1370
1600
  select_n_having_items= 0;
1371
1601
  subquery_in_having= explicit_limit= 0;
1372
1602
  is_item_list_lookup= 0;
 
1603
  first_execution= 1;
 
1604
  first_cond_optimization= 1;
1373
1605
  parsing_place= NO_MATTER;
1374
 
  exclude_from_table_unique_test= false;
 
1606
  exclude_from_table_unique_test= no_wrap_view_item= false;
1375
1607
  nest_level= 0;
1376
1608
  link_next= 0;
1377
1609
}
1378
1610
 
1379
 
void Select_Lex::init_select()
 
1611
void st_select_lex::init_select()
1380
1612
{
1381
 
  sj_nests.clear();
1382
 
  group_list.clear();
1383
 
  db= 0;
 
1613
  st_select_lex_node::init_select();
 
1614
  sj_nests.empty();
 
1615
  group_list.empty();
 
1616
  type= db= 0;
1384
1617
  having= 0;
 
1618
  table_join_options= 0;
1385
1619
  in_sum_expr= with_wild= 0;
1386
1620
  options= 0;
1387
1621
  braces= 0;
1388
 
  interval_list.clear();
 
1622
  interval_list.empty();
 
1623
  ftfunc_list_alloc.empty();
1389
1624
  inner_sum_func_list= 0;
 
1625
  ftfunc_list= &ftfunc_list_alloc;
1390
1626
  linkage= UNSPECIFIED_TYPE;
1391
1627
  order_list.elements= 0;
1392
1628
  order_list.first= 0;
1393
 
  order_list.next= (unsigned char**) &order_list.first;
 
1629
  order_list.next= (uchar**) &order_list.first;
1394
1630
  /* Set limit and offset to default values */
1395
1631
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1396
1632
  offset_limit= 0;      /* denotes the default offset = 0 */
1397
1633
  with_sum_func= 0;
1398
 
  is_cross= false;
1399
1634
  is_correlated= 0;
1400
1635
  cur_pos_in_select_list= UNDEF_POS;
1401
 
  non_agg_fields.clear();
 
1636
  non_agg_fields.empty();
1402
1637
  cond_value= having_value= Item::COND_UNDEF;
1403
 
  inner_refs_list.clear();
1404
 
  full_group_by_flag.reset();
 
1638
  inner_refs_list.empty();
 
1639
  full_group_by_flag= 0;
1405
1640
}
1406
1641
 
1407
1642
/*
1408
 
  Select_Lex structures linking
 
1643
  st_select_lex structures linking
1409
1644
*/
1410
1645
 
1411
1646
/* include on level down */
1412
 
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
 
1647
void st_select_lex_node::include_down(st_select_lex_node *upper)
1413
1648
{
1414
1649
  if ((next= upper->slave))
1415
1650
    next->prev= &next;
1423
1658
  include on level down (but do not link)
1424
1659
 
1425
1660
  SYNOPSYS
1426
 
    Select_Lex_Node::include_standalone()
 
1661
    st_select_lex_node::include_standalone()
1427
1662
    upper - reference on node underr which this node should be included
1428
1663
    ref - references on reference on this node
1429
1664
*/
1430
 
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1431
 
                                            Select_Lex_Node **ref)
 
1665
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
 
1666
                                            st_select_lex_node **ref)
1432
1667
{
1433
1668
  next= 0;
1434
1669
  prev= ref;
1437
1672
}
1438
1673
 
1439
1674
/* include neighbour (on same level) */
1440
 
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
 
1675
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1441
1676
{
1442
1677
  if ((next= before->next))
1443
1678
    next->prev= &next;
1447
1682
  slave= 0;
1448
1683
}
1449
1684
 
1450
 
/* including in global Select_Lex list */
1451
 
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
 
1685
/* including in global SELECT_LEX list */
 
1686
void st_select_lex_node::include_global(st_select_lex_node **plink)
1452
1687
{
1453
1688
  if ((link_next= *plink))
1454
1689
    link_next->link_prev= &link_next;
1457
1692
}
1458
1693
 
1459
1694
//excluding from global list (internal function)
1460
 
void Select_Lex_Node::fast_exclude()
 
1695
void st_select_lex_node::fast_exclude()
1461
1696
{
1462
1697
  if (link_prev)
1463
1698
  {
1467
1702
  // Remove slave structure
1468
1703
  for (; slave; slave= slave->next)
1469
1704
    slave->fast_exclude();
1470
 
 
 
1705
  
1471
1706
}
1472
1707
 
1473
1708
/*
1474
1709
  excluding select_lex structure (except first (first select can't be
1475
1710
  deleted, because it is most upper select))
1476
1711
*/
1477
 
void Select_Lex_Node::exclude()
 
1712
void st_select_lex_node::exclude()
1478
1713
{
1479
1714
  //exclude from global list
1480
1715
  fast_exclude();
1481
1716
  //exclude from other structures
1482
1717
  if ((*prev= next))
1483
1718
    next->prev= prev;
1484
 
  /*
1485
 
     We do not need following statements, because prev pointer of first
 
1719
  /* 
 
1720
     We do not need following statements, because prev pointer of first 
1486
1721
     list element point to master->slave
1487
1722
     if (master->slave == this)
1488
1723
       master->slave= next;
1494
1729
  Exclude level of current unit from tree of SELECTs
1495
1730
 
1496
1731
  SYNOPSYS
1497
 
    Select_Lex_Unit::exclude_level()
 
1732
    st_select_lex_unit::exclude_level()
1498
1733
 
1499
1734
  NOTE: units which belong to current will be brought up on level of
1500
 
  currernt unit
 
1735
  currernt unit 
1501
1736
*/
1502
 
void Select_Lex_Unit::exclude_level()
 
1737
void st_select_lex_unit::exclude_level()
1503
1738
{
1504
 
  Select_Lex_Unit *units= 0, **units_last= &units;
1505
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1739
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
 
1740
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1506
1741
  {
1507
1742
    // unlink current level from global SELECTs list
1508
1743
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1509
1744
      sl->link_next->link_prev= sl->link_prev;
1510
1745
 
1511
1746
    // bring up underlay levels
1512
 
    Select_Lex_Unit **last= 0;
1513
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1747
    SELECT_LEX_UNIT **last= 0;
 
1748
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1514
1749
    {
1515
1750
      u->master= master;
1516
 
      last= (Select_Lex_Unit**)&(u->next);
 
1751
      last= (SELECT_LEX_UNIT**)&(u->next);
1517
1752
    }
1518
1753
    if (last)
1519
1754
    {
1525
1760
  {
1526
1761
    // include brought up levels in place of current
1527
1762
    (*prev)= units;
1528
 
    (*units_last)= (Select_Lex_Unit*)next;
 
1763
    (*units_last)= (SELECT_LEX_UNIT*)next;
1529
1764
    if (next)
1530
 
      next->prev= (Select_Lex_Node**)units_last;
 
1765
      next->prev= (SELECT_LEX_NODE**)units_last;
1531
1766
    units->prev= prev;
1532
1767
  }
1533
1768
  else
1539
1774
  }
1540
1775
}
1541
1776
 
 
1777
 
1542
1778
/*
1543
1779
  Exclude subtree of current unit from tree of SELECTs
 
1780
 
 
1781
  SYNOPSYS
 
1782
    st_select_lex_unit::exclude_tree()
1544
1783
*/
1545
 
void Select_Lex_Unit::exclude_tree()
 
1784
void st_select_lex_unit::exclude_tree()
1546
1785
{
1547
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1786
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1548
1787
  {
1549
1788
    // unlink current level from global SELECTs list
1550
1789
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1551
1790
      sl->link_next->link_prev= sl->link_prev;
1552
1791
 
1553
1792
    // unlink underlay levels
1554
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1793
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1555
1794
    {
1556
1795
      u->exclude_level();
1557
1796
    }
1562
1801
    next->prev= prev;
1563
1802
}
1564
1803
 
1565
 
/**
1566
 
 * Mark all Select_Lex struct from this to 'last' as dependent
1567
 
 *
1568
 
 * @param Pointer to last Select_Lex struct, before wich all
1569
 
 *        Select_Lex have to be marked as dependent
1570
 
 * @note 'last' should be reachable from this Select_Lex_Node
1571
 
 */
1572
 
void Select_Lex::mark_as_dependent(Select_Lex *last)
 
1804
 
 
1805
/*
 
1806
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1807
  this to 'last' as dependent
 
1808
 
 
1809
  SYNOPSIS
 
1810
    last - pointer to last st_select_lex struct, before wich all 
 
1811
           st_select_lex have to be marked as dependent
 
1812
 
 
1813
  NOTE
 
1814
    'last' should be reachable from this st_select_lex_node
 
1815
*/
 
1816
 
 
1817
void st_select_lex::mark_as_dependent(st_select_lex *last)
1573
1818
{
1574
1819
  /*
1575
1820
    Mark all selects from resolved to 1 before select where was
1576
1821
    found table as depended (of select where was found table)
1577
1822
  */
1578
 
  for (Select_Lex *s= this;
 
1823
  for (SELECT_LEX *s= this;
1579
1824
       s && s != last;
1580
1825
       s= s->outer_select())
1581
1826
  {
1582
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1827
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1583
1828
    {
1584
1829
      // Select is dependent of outer select
1585
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1586
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
1587
 
      Select_Lex_Unit *munit= s->master_unit();
1588
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1589
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1590
 
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1830
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1831
                       UNCACHEABLE_DEPENDENT;
 
1832
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1833
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1834
                       UNCACHEABLE_DEPENDENT;
 
1835
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1591
1836
      {
1592
1837
        if (sl != s &&
1593
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1594
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1838
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1839
          sl->uncacheable|= UNCACHEABLE_UNITED;
1595
1840
      }
1596
1841
    }
1597
1842
    s->is_correlated= true;
1601
1846
  }
1602
1847
}
1603
1848
 
1604
 
bool Select_Lex_Node::set_braces(bool)
1605
 
{ return true; }
1606
 
 
1607
 
bool Select_Lex_Node::inc_in_sum_expr()
1608
 
{ return true; }
1609
 
 
1610
 
uint32_t Select_Lex_Node::get_in_sum_expr() 
1611
 
{ return 0; }
1612
 
 
1613
 
TableList* Select_Lex_Node::get_table_list()
1614
 
{ return NULL; }
1615
 
 
1616
 
List<Item>* Select_Lex_Node::get_item_list()
1617
 
{ return NULL; }
1618
 
 
1619
 
TableList *Select_Lex_Node::add_table_to_list(Session *, 
1620
 
                                              Table_ident *, 
1621
 
                                              LEX_STRING *, 
1622
 
                                              const bitset<NUM_OF_TABLE_OPTIONS>&,
1623
 
                                              thr_lock_type, 
1624
 
                                              List<Index_hint> *, 
1625
 
                                              LEX_STRING *)
1626
 
{
1627
 
  return 0;
1628
 
}
1629
 
 
 
1849
bool st_select_lex_node::set_braces(bool value __attribute__((__unused__)))
 
1850
{ return 1; }
 
1851
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
 
1852
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1853
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
 
1854
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
 
1855
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd __attribute__((__unused__)),
 
1856
                                                   Table_ident *table __attribute__((__unused__)),
 
1857
                                                  LEX_STRING *alias __attribute__((__unused__)),
 
1858
                                                  ulong table_join_options __attribute__((__unused__)),
 
1859
                                                  thr_lock_type flags __attribute__((__unused__)),
 
1860
                                                  List<Index_hint> *hints __attribute__((__unused__)),
 
1861
                                                  LEX_STRING *option __attribute__((__unused__)))
 
1862
{
 
1863
  return 0;
 
1864
}
 
1865
ulong st_select_lex_node::get_table_join_options()
 
1866
{
 
1867
  return 0;
 
1868
}
1630
1869
 
1631
1870
/*
1632
1871
  prohibit using LIMIT clause
1633
1872
*/
1634
 
bool Select_Lex::test_limit()
 
1873
bool st_select_lex::test_limit()
1635
1874
{
1636
1875
  if (select_limit != 0)
1637
1876
  {
1638
1877
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1639
1878
             "LIMIT & IN/ALL/ANY/SOME subquery");
1640
 
    return true;
 
1879
    return(1);
1641
1880
  }
1642
 
  return false;
1643
 
}
1644
 
 
1645
 
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1646
 
{
1647
 
  return this;
1648
 
}
1649
 
 
1650
 
Select_Lex* Select_Lex_Unit::outer_select()
1651
 
{
1652
 
  return (Select_Lex*) master;
1653
 
}
1654
 
 
1655
 
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1656
 
{
1657
 
  return add_to_list(session, order_list, item, asc);
1658
 
}
1659
 
 
1660
 
bool Select_Lex::add_item_to_list(Session *, Item *item)
 
1881
  return(0);
 
1882
}
 
1883
 
 
1884
 
 
1885
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1886
{
 
1887
    return this;
 
1888
}
 
1889
 
 
1890
 
 
1891
st_select_lex* st_select_lex_unit::outer_select()
 
1892
{
 
1893
  return (st_select_lex*) master;
 
1894
}
 
1895
 
 
1896
 
 
1897
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1898
{
 
1899
  return add_to_list(thd, order_list, item, asc);
 
1900
}
 
1901
 
 
1902
 
 
1903
bool st_select_lex::add_item_to_list(THD *thd __attribute__((__unused__)),
 
1904
                                     Item *item)
1661
1905
{
1662
1906
  return(item_list.push_back(item));
1663
1907
}
1664
1908
 
1665
 
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1666
 
{
1667
 
  return add_to_list(session, group_list, item, asc);
1668
 
}
1669
 
 
1670
 
Select_Lex_Unit* Select_Lex::master_unit()
1671
 
{
1672
 
  return (Select_Lex_Unit*) master;
1673
 
}
1674
 
 
1675
 
Select_Lex* Select_Lex::outer_select()
1676
 
{
1677
 
  return (Select_Lex*) master->get_master();
1678
 
}
1679
 
 
1680
 
bool Select_Lex::set_braces(bool value)
 
1909
 
 
1910
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1911
{
 
1912
  return add_to_list(thd, group_list, item, asc);
 
1913
}
 
1914
 
 
1915
 
 
1916
st_select_lex_unit* st_select_lex::master_unit()
 
1917
{
 
1918
  return (st_select_lex_unit*) master;
 
1919
}
 
1920
 
 
1921
 
 
1922
st_select_lex* st_select_lex::outer_select()
 
1923
{
 
1924
  return (st_select_lex*) master->get_master();
 
1925
}
 
1926
 
 
1927
 
 
1928
bool st_select_lex::set_braces(bool value)
1681
1929
{
1682
1930
  braces= value;
1683
 
  return false;
 
1931
  return 0; 
1684
1932
}
1685
1933
 
1686
 
bool Select_Lex::inc_in_sum_expr()
 
1934
 
 
1935
bool st_select_lex::inc_in_sum_expr()
1687
1936
{
1688
1937
  in_sum_expr++;
1689
 
  return false;
 
1938
  return 0;
1690
1939
}
1691
1940
 
1692
 
uint32_t Select_Lex::get_in_sum_expr()
 
1941
 
 
1942
uint st_select_lex::get_in_sum_expr()
1693
1943
{
1694
1944
  return in_sum_expr;
1695
1945
}
1696
1946
 
1697
 
TableList* Select_Lex::get_table_list()
 
1947
 
 
1948
TABLE_LIST* st_select_lex::get_table_list()
1698
1949
{
1699
 
  return (TableList*) table_list.first;
 
1950
  return (TABLE_LIST*) table_list.first;
1700
1951
}
1701
1952
 
1702
 
List<Item>* Select_Lex::get_item_list()
 
1953
List<Item>* st_select_lex::get_item_list()
1703
1954
{
1704
1955
  return &item_list;
1705
1956
}
1706
1957
 
1707
 
 
1708
 
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
 
1958
ulong st_select_lex::get_table_join_options()
 
1959
{
 
1960
  return table_join_options;
 
1961
}
 
1962
 
 
1963
 
 
1964
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1709
1965
{
1710
1966
  if (ref_pointer_array)
1711
 
    return false;
 
1967
    return 0;
1712
1968
 
 
1969
  /*
 
1970
    We have to create array in prepared statement memory if it is
 
1971
    prepared statement
 
1972
  */
 
1973
  Query_arena *arena= thd->stmt_arena;
1713
1974
  return (ref_pointer_array=
1714
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1975
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1715
1976
                                                 item_list.elements +
1716
1977
                                                 select_n_having_items +
1717
1978
                                                 select_n_where_fields +
1718
1979
                                                 order_group_num)*5)) == 0;
1719
1980
}
1720
1981
 
1721
 
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
 
1982
 
 
1983
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1722
1984
{
1723
1985
  bool union_all= !union_distinct;
1724
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1986
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1725
1987
  {
1726
1988
    if (sl != first_select())
1727
1989
    {
1733
1995
    }
1734
1996
    if (sl->braces)
1735
1997
      str->append('(');
1736
 
    sl->print(session, str, query_type);
 
1998
    sl->print(thd, str, query_type);
1737
1999
    if (sl->braces)
1738
2000
      str->append(')');
1739
2001
  }
1744
2006
      str->append(STRING_WITH_LEN(" order by "));
1745
2007
      fake_select_lex->print_order(
1746
2008
        str,
1747
 
        (Order *) fake_select_lex->order_list.first,
 
2009
        (ORDER *) fake_select_lex->order_list.first,
1748
2010
        query_type);
1749
2011
    }
1750
 
    fake_select_lex->print_limit(session, str, query_type);
 
2012
    fake_select_lex->print_limit(thd, str, query_type);
1751
2013
  }
1752
2014
}
1753
2015
 
1754
 
void Select_Lex::print_order(String *str,
1755
 
                                Order *order,
 
2016
 
 
2017
void st_select_lex::print_order(String *str,
 
2018
                                ORDER *order,
1756
2019
                                enum_query_type query_type)
1757
2020
{
1758
2021
  for (; order; order= order->next)
1760
2023
    if (order->counter_used)
1761
2024
    {
1762
2025
      char buffer[20];
1763
 
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
 
2026
      uint length= snprintf(buffer, 20, "%d", order->counter);
1764
2027
      str->append(buffer, length);
1765
2028
    }
1766
2029
    else
1771
2034
      str->append(',');
1772
2035
  }
1773
2036
}
 
2037
 
1774
2038
 
1775
 
void Select_Lex::print_limit(Session *, String *str,
 
2039
void st_select_lex::print_limit(THD *thd __attribute__((__unused__)),
 
2040
                                String *str,
1776
2041
                                enum_query_type query_type)
1777
2042
{
1778
 
  Select_Lex_Unit *unit= master_unit();
 
2043
  SELECT_LEX_UNIT *unit= master_unit();
1779
2044
  Item_subselect *item= unit->item;
1780
2045
 
1781
2046
  if (item && unit->global_parameters == this)
1794
2059
                    ((Item_in_subselect*)item)->exec_method ==
1795
2060
                    Item_in_subselect::MATERIALIZATION) ?
1796
2061
                   true :
1797
 
                   (select_limit->val_int() == 1L) &&
 
2062
                   (select_limit->val_int() == 1LL) &&
1798
2063
                   offset_limit == 0));
1799
2064
      return;
1800
2065
    }
1811
2076
  }
1812
2077
}
1813
2078
 
1814
 
LEX::~LEX()
 
2079
/**
 
2080
  @brief Restore the LEX and THD in case of a parse error.
 
2081
 
 
2082
  This is a clean up call that is invoked by the Bison generated
 
2083
  parser before returning an error from MYSQLparse. If your
 
2084
  semantic actions manipulate with the global thread state (which
 
2085
  is a very bad practice and should not normally be employed) and
 
2086
  need a clean-up in case of error, and you can not use %destructor
 
2087
  rule in the grammar file itself, this function should be used
 
2088
  to implement the clean up.
 
2089
*/
 
2090
 
 
2091
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((__unused__)))
1815
2092
{
1816
 
  delete _create_table;
1817
2093
}
1818
2094
 
1819
2095
/*
1833
2109
    this method to reset state of already initialized Query_tables_list
1834
2110
    so it can be used for processing of new statement.
1835
2111
*/
 
2112
 
1836
2113
void Query_tables_list::reset_query_tables_list(bool init)
1837
2114
{
1838
2115
  if (!init && query_tables)
1839
2116
  {
1840
 
    TableList *table= query_tables;
 
2117
    TABLE_LIST *table= query_tables;
1841
2118
    for (;;)
1842
2119
    {
1843
2120
      if (query_tables_last == &table->next_global ||
1848
2125
  query_tables= 0;
1849
2126
  query_tables_last= &query_tables;
1850
2127
  query_tables_own_last= 0;
1851
 
}
 
2128
  if (init)
 
2129
  {
 
2130
    /*
 
2131
      We delay real initialization of hash (and therefore related
 
2132
      memory allocation) until first insertion into this hash.
 
2133
    */
 
2134
    hash_clear(&sroutines);
 
2135
  }
 
2136
  else if (sroutines.records)
 
2137
  {
 
2138
    /* Non-zero sroutines.records means that hash was initialized. */
 
2139
    my_hash_reset(&sroutines);
 
2140
  }
 
2141
  sroutines_list.empty();
 
2142
  sroutines_list_own_last= sroutines_list.next;
 
2143
  sroutines_list_own_elements= 0;
 
2144
  binlog_stmt_flags= 0;
 
2145
}
 
2146
 
 
2147
 
 
2148
/*
 
2149
  Destroy Query_tables_list object with freeing all resources used by it.
 
2150
 
 
2151
  SYNOPSIS
 
2152
    destroy_query_tables_list()
 
2153
*/
 
2154
 
 
2155
void Query_tables_list::destroy_query_tables_list()
 
2156
{
 
2157
  hash_free(&sroutines);
 
2158
}
 
2159
 
1852
2160
 
1853
2161
/*
1854
2162
  Initialize LEX object.
1855
2163
 
1856
2164
  SYNOPSIS
1857
 
    LEX::LEX()
 
2165
    st_lex::st_lex()
1858
2166
 
1859
2167
  NOTE
1860
2168
    LEX object initialized with this constructor can be used as part of
1861
 
    Session object for which one can safely call open_tables(), lock_tables()
 
2169
    THD object for which one can safely call open_tables(), lock_tables()
1862
2170
    and close_thread_tables() functions. But it is not yet ready for
1863
2171
    statement parsing. On should use lex_start() function to prepare LEX
1864
2172
    for this.
1865
2173
*/
1866
 
LEX::LEX() :
1867
 
    result(0), 
1868
 
    yacc_yyss(0), 
1869
 
    yacc_yyvs(0),
1870
 
    session(NULL),
1871
 
    charset(NULL),
1872
 
    var_list(),
1873
 
    sql_command(SQLCOM_END), 
1874
 
    statement(NULL),
1875
 
    option_type(OPT_DEFAULT), 
1876
 
    is_lex_started(0),
1877
 
    cacheable(true),
1878
 
    sum_expr_used(false),
1879
 
    _create_table(NULL),
1880
 
    _create_field(NULL),
1881
 
    _exists(false)
 
2174
 
 
2175
st_lex::st_lex()
 
2176
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
2177
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1882
2178
{
 
2179
 
 
2180
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
 
2181
                         plugins_static_buffer,
 
2182
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
 
2183
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
1883
2184
  reset_query_tables_list(true);
1884
2185
}
1885
2186
 
 
2187
 
 
2188
/*
 
2189
  Check whether the merging algorithm can be used on this VIEW
 
2190
 
 
2191
  SYNOPSIS
 
2192
    st_lex::can_be_merged()
 
2193
 
 
2194
  DESCRIPTION
 
2195
    We can apply merge algorithm if it is single SELECT view  with
 
2196
    subqueries only in WHERE clause (we do not count SELECTs of underlying
 
2197
    views, and second level subqueries) and we have not grpouping, ordering,
 
2198
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
 
2199
    several underlying tables.
 
2200
 
 
2201
  RETURN
 
2202
    false - only temporary table algorithm can be used
 
2203
    true  - merge algorithm can be used
 
2204
*/
 
2205
 
 
2206
bool st_lex::can_be_merged()
 
2207
{
 
2208
  // TODO: do not forget implement case when select_lex.table_list.elements==0
 
2209
 
 
2210
  /* find non VIEW subqueries/unions */
 
2211
  bool selects_allow_merge= select_lex.next_select() == 0;
 
2212
  if (selects_allow_merge)
 
2213
  {
 
2214
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
 
2215
         tmp_unit;
 
2216
         tmp_unit= tmp_unit->next_unit())
 
2217
    {
 
2218
      if (tmp_unit->first_select()->parent_lex == this &&
 
2219
          (tmp_unit->item == 0 ||
 
2220
           (tmp_unit->item->place() != IN_WHERE &&
 
2221
            tmp_unit->item->place() != IN_ON)))
 
2222
      {
 
2223
        selects_allow_merge= 0;
 
2224
        break;
 
2225
      }
 
2226
    }
 
2227
  }
 
2228
 
 
2229
  return (selects_allow_merge &&
 
2230
          select_lex.group_list.elements == 0 &&
 
2231
          select_lex.having == 0 &&
 
2232
          select_lex.with_sum_func == 0 &&
 
2233
          select_lex.table_list.elements >= 1 &&
 
2234
          !(select_lex.options & SELECT_DISTINCT) &&
 
2235
          select_lex.select_limit == 0);
 
2236
}
 
2237
 
 
2238
 
 
2239
/*
 
2240
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
 
2241
 
 
2242
  SYNOPSIS
 
2243
    st_lex::can_use_merged()
 
2244
 
 
2245
  DESCRIPTION
 
2246
    Only listed here commands can use merge algorithm in top level
 
2247
    SELECT_LEX (for subqueries will be used merge algorithm if
 
2248
    st_lex::can_not_use_merged() is not true).
 
2249
 
 
2250
  RETURN
 
2251
    false - command can't use merged VIEWs
 
2252
    true  - VIEWs with MERGE algorithms can be used
 
2253
*/
 
2254
 
 
2255
bool st_lex::can_use_merged()
 
2256
{
 
2257
  switch (sql_command)
 
2258
  {
 
2259
  case SQLCOM_SELECT:
 
2260
  case SQLCOM_CREATE_TABLE:
 
2261
  case SQLCOM_UPDATE:
 
2262
  case SQLCOM_UPDATE_MULTI:
 
2263
  case SQLCOM_DELETE:
 
2264
  case SQLCOM_DELETE_MULTI:
 
2265
  case SQLCOM_INSERT:
 
2266
  case SQLCOM_INSERT_SELECT:
 
2267
  case SQLCOM_REPLACE:
 
2268
  case SQLCOM_REPLACE_SELECT:
 
2269
  case SQLCOM_LOAD:
 
2270
    return true;
 
2271
  default:
 
2272
    return false;
 
2273
  }
 
2274
}
 
2275
 
 
2276
/*
 
2277
  Check if command can't use merged views in any part of command
 
2278
 
 
2279
  SYNOPSIS
 
2280
    st_lex::can_not_use_merged()
 
2281
 
 
2282
  DESCRIPTION
 
2283
    Temporary table algorithm will be used on all SELECT levels for queries
 
2284
    listed here (see also st_lex::can_use_merged()).
 
2285
 
 
2286
  RETURN
 
2287
    false - command can't use merged VIEWs
 
2288
    true  - VIEWs with MERGE algorithms can be used
 
2289
*/
 
2290
 
 
2291
bool st_lex::can_not_use_merged()
 
2292
{
 
2293
  switch (sql_command)
 
2294
  {
 
2295
  /*
 
2296
    SQLCOM_SHOW_FIELDS is necessary to make 
 
2297
    information schema tables working correctly with views.
 
2298
    see get_schema_tables_result function
 
2299
  */
 
2300
  case SQLCOM_SHOW_FIELDS:
 
2301
    return true;
 
2302
  default:
 
2303
    return false;
 
2304
  }
 
2305
}
 
2306
 
 
2307
/*
 
2308
  Detect that we need only table structure of derived table/view
 
2309
 
 
2310
  SYNOPSIS
 
2311
    only_view_structure()
 
2312
 
 
2313
  RETURN
 
2314
    true yes, we need only structure
 
2315
    false no, we need data
 
2316
*/
 
2317
 
 
2318
bool st_lex::only_view_structure()
 
2319
{
 
2320
  switch (sql_command) {
 
2321
  case SQLCOM_SHOW_CREATE:
 
2322
  case SQLCOM_SHOW_TABLES:
 
2323
  case SQLCOM_SHOW_FIELDS:
 
2324
    return true;
 
2325
  default:
 
2326
    return false;
 
2327
  }
 
2328
}
 
2329
 
 
2330
 
 
2331
/*
 
2332
  Should Items_ident be printed correctly
 
2333
 
 
2334
  SYNOPSIS
 
2335
    need_correct_ident()
 
2336
 
 
2337
  RETURN
 
2338
    true yes, we need only structure
 
2339
    false no, we need data
 
2340
*/
 
2341
 
 
2342
 
 
2343
bool st_lex::need_correct_ident()
 
2344
{
 
2345
  switch(sql_command)
 
2346
  {
 
2347
  case SQLCOM_SHOW_CREATE:
 
2348
  case SQLCOM_SHOW_TABLES:
 
2349
    return true;
 
2350
  default:
 
2351
    return false;
 
2352
  }
 
2353
}
 
2354
 
 
2355
/*
 
2356
  Get effective type of CHECK OPTION for given view
 
2357
 
 
2358
  SYNOPSIS
 
2359
    get_effective_with_check()
 
2360
    view    given view
 
2361
 
 
2362
  NOTE
 
2363
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
 
2364
    so we do not.
 
2365
 
 
2366
  RETURN
 
2367
    VIEW_CHECK_NONE      no need CHECK OPTION
 
2368
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
 
2369
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
 
2370
*/
 
2371
 
 
2372
uint8 st_lex::get_effective_with_check(TABLE_LIST *view __attribute__((__unused__)))
 
2373
{
 
2374
  return 0;
 
2375
}
 
2376
 
 
2377
 
1886
2378
/**
1887
2379
  This method should be called only during parsing.
1888
2380
  It is aware of compound statements (stored routine bodies)
1889
2381
  and will initialize the destination with the default
1890
2382
  database of the stored routine, rather than the default
1891
2383
  database of the connection it is parsed in.
1892
 
  E.g. if one has no current database selected, or current database
 
2384
  E.g. if one has no current database selected, or current database 
1893
2385
  set to 'bar' and then issues:
1894
2386
 
1895
2387
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
1901
2393
  @return true in case of error (parsing should be aborted, false in
1902
2394
  case of success
1903
2395
*/
1904
 
bool LEX::copy_db_to(char **p_db, size_t *p_db_length) const
 
2396
 
 
2397
bool
 
2398
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
1905
2399
{
1906
 
  return session->copy_db_to(p_db, p_db_length);
 
2400
  return thd->copy_db_to(p_db, p_db_length);
1907
2401
}
1908
2402
 
1909
2403
/*
1910
2404
  initialize limit counters
1911
2405
 
1912
2406
  SYNOPSIS
1913
 
    Select_Lex_Unit::set_limit()
1914
 
    values      - Select_Lex with initial values for counters
 
2407
    st_select_lex_unit::set_limit()
 
2408
    values      - SELECT_LEX with initial values for counters
1915
2409
*/
1916
 
void Select_Lex_Unit::set_limit(Select_Lex *sl)
 
2410
 
 
2411
void st_select_lex_unit::set_limit(st_select_lex *sl)
1917
2412
{
1918
2413
  ha_rows select_limit_val;
1919
2414
  uint64_t val;
1920
2415
 
1921
2416
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
1922
2417
  select_limit_val= (ha_rows)val;
1923
 
  /*
 
2418
#ifndef BIG_TABLES
 
2419
  /* 
1924
2420
    Check for overflow : ha_rows can be smaller then uint64_t if
1925
2421
    BIG_TABLES is off.
1926
2422
    */
1927
2423
  if (val != (uint64_t)select_limit_val)
1928
2424
    select_limit_val= HA_POS_ERROR;
 
2425
#endif
1929
2426
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
1930
 
                                                 0UL);
 
2427
                                                 0ULL);
1931
2428
  select_limit_cnt= select_limit_val + offset_limit_cnt;
1932
2429
  if (select_limit_cnt < select_limit_val)
1933
2430
    select_limit_cnt= HA_POS_ERROR;             // no limit
1934
2431
}
1935
2432
 
 
2433
 
1936
2434
/*
1937
2435
  Unlink the first table from the global table list and the first table from
1938
2436
  outer select (lex->select_lex) local list
1951
2449
      In this case link_to_local is set.
1952
2450
 
1953
2451
*/
1954
 
TableList *LEX::unlink_first_table(bool *link_to_local)
 
2452
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
1955
2453
{
1956
 
  TableList *first;
 
2454
  TABLE_LIST *first;
1957
2455
  if ((first= query_tables))
1958
2456
  {
1959
2457
    /*
1970
2468
    */
1971
2469
    if ((*link_to_local= test(select_lex.table_list.first)))
1972
2470
    {
1973
 
      select_lex.context.table_list=
 
2471
      select_lex.context.table_list= 
1974
2472
        select_lex.context.first_name_resolution_table= first->next_local;
1975
 
      select_lex.table_list.first= (unsigned char*) (first->next_local);
 
2473
      select_lex.table_list.first= (uchar*) (first->next_local);
1976
2474
      select_lex.table_list.elements--; //safety
1977
2475
      first->next_local= 0;
1978
2476
      /*
1985
2483
  return first;
1986
2484
}
1987
2485
 
 
2486
 
1988
2487
/*
1989
2488
  Bring first local table of first most outer select to first place in global
1990
2489
  table list
1991
2490
 
1992
2491
  SYNOPSYS
1993
 
     LEX::first_lists_tables_same()
 
2492
     st_lex::first_lists_tables_same()
1994
2493
 
1995
2494
  NOTES
1996
2495
    In many cases (for example, usual INSERT/DELETE/...) the first table of
1997
 
    main Select_Lex have special meaning => check that it is the first table
 
2496
    main SELECT_LEX have special meaning => check that it is the first table
1998
2497
    in global list and re-link to be first in the global list if it is
1999
2498
    necessary.  We need such re-linking only for queries with sub-queries in
2000
2499
    the select list, as only in this case tables of sub-queries will go to
2001
2500
    the global list first.
2002
2501
*/
2003
 
void LEX::first_lists_tables_same()
 
2502
 
 
2503
void st_lex::first_lists_tables_same()
2004
2504
{
2005
 
  TableList *first_table= (TableList*) select_lex.table_list.first;
 
2505
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
2006
2506
  if (query_tables != first_table && first_table != 0)
2007
2507
  {
2008
 
    TableList *next;
 
2508
    TABLE_LIST *next;
2009
2509
    if (query_tables_last == &first_table->next_global)
2010
2510
      query_tables_last= first_table->prev_global;
2011
2511
 
2024
2524
  }
2025
2525
}
2026
2526
 
 
2527
 
2027
2528
/*
2028
2529
  Link table back that was unlinked with unlink_first_table()
2029
2530
 
2034
2535
  RETURN
2035
2536
    global list
2036
2537
*/
2037
 
void LEX::link_first_table_back(TableList *first, bool link_to_local)
 
2538
 
 
2539
void st_lex::link_first_table_back(TABLE_LIST *first,
 
2540
                                   bool link_to_local)
2038
2541
{
2039
2542
  if (first)
2040
2543
  {
2046
2549
 
2047
2550
    if (link_to_local)
2048
2551
    {
2049
 
      first->next_local= (TableList*) select_lex.table_list.first;
 
2552
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
2050
2553
      select_lex.context.table_list= first;
2051
 
      select_lex.table_list.first= (unsigned char*) first;
 
2554
      select_lex.table_list.first= (uchar*) first;
2052
2555
      select_lex.table_list.elements++; //safety
2053
2556
    }
2054
2557
  }
2055
2558
}
2056
2559
 
 
2560
 
 
2561
 
2057
2562
/*
2058
2563
  cleanup lex for case when we open table by table for processing
2059
2564
 
2060
2565
  SYNOPSIS
2061
 
    LEX::cleanup_after_one_table_open()
 
2566
    st_lex::cleanup_after_one_table_open()
2062
2567
 
2063
2568
  NOTE
2064
2569
    This method is mostly responsible for cleaning up of selects lists and
2065
2570
    derived tables state. To rollback changes in Query_tables_list one has
2066
2571
    to call Query_tables_list::reset_query_tables_list(false).
2067
2572
*/
2068
 
void LEX::cleanup_after_one_table_open()
 
2573
 
 
2574
void st_lex::cleanup_after_one_table_open()
2069
2575
{
2070
2576
  /*
2071
 
    session->getLex()->derived_tables & additional units may be set if we open
2072
 
    a view. It is necessary to clear session->getLex()->derived_tables flag
2073
 
    to prevent processing of derived tables during next openTablesLock
 
2577
    thd->lex->derived_tables & additional units may be set if we open
 
2578
    a view. It is necessary to clear thd->lex->derived_tables flag
 
2579
    to prevent processing of derived tables during next open_and_lock_tables
2074
2580
    if next table is a real table and cleanup & remove underlying units
2075
 
    NOTE: all units will be connected to session->getLex()->select_lex, because we
 
2581
    NOTE: all units will be connected to thd->lex->select_lex, because we
2076
2582
    have not UNION on most upper level.
2077
2583
    */
2078
2584
  if (all_selects_list != &select_lex)
2079
2585
  {
2080
2586
    derived_tables= 0;
2081
2587
    /* cleunup underlying units (units of VIEW) */
2082
 
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
 
2588
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
2083
2589
         un;
2084
2590
         un= un->next_unit())
2085
2591
      un->cleanup();
2090
2596
  }
2091
2597
}
2092
2598
 
2093
 
/*
2094
 
  There are Select_Lex::add_table_to_list &
2095
 
  Select_Lex::set_lock_for_tables are in sql_parse.cc
2096
 
 
2097
 
  Select_Lex::print is in sql_select.cc
2098
 
 
2099
 
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2100
 
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2101
 
  Select_Lex_Unit::change_result
 
2599
 
 
2600
/*
 
2601
  Save current state of Query_tables_list for this LEX, and prepare it
 
2602
  for processing of new statemnt.
 
2603
 
 
2604
  SYNOPSIS
 
2605
    reset_n_backup_query_tables_list()
 
2606
      backup  Pointer to Query_tables_list instance to be used for backup
 
2607
*/
 
2608
 
 
2609
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((__unused__)))
 
2610
{
 
2611
}
 
2612
 
 
2613
 
 
2614
/*
 
2615
  Restore state of Query_tables_list for this LEX from backup.
 
2616
 
 
2617
  SYNOPSIS
 
2618
    restore_backup_query_tables_list()
 
2619
      backup  Pointer to Query_tables_list instance used for backup
 
2620
*/
 
2621
 
 
2622
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((__unused__)))
 
2623
{
 
2624
}
 
2625
 
 
2626
 
 
2627
/*
 
2628
  Checks for usage of routines and/or tables in a parsed statement
 
2629
 
 
2630
  SYNOPSIS
 
2631
    st_lex:table_or_sp_used()
 
2632
 
 
2633
  RETURN
 
2634
    false  No routines and tables used
 
2635
    true   Either or both routines and tables are used.
 
2636
*/
 
2637
 
 
2638
bool st_lex::table_or_sp_used()
 
2639
{
 
2640
  if (sroutines.records || query_tables)
 
2641
    return(true);
 
2642
 
 
2643
  return(false);
 
2644
}
 
2645
 
 
2646
 
 
2647
/*
 
2648
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
 
2649
 
 
2650
  SYNOPSIS
 
2651
    fix_prepare_info_in_table_list()
 
2652
      thd  Thread handle
 
2653
      tbl  List of tables to process
 
2654
 
 
2655
  DESCRIPTION
 
2656
    Perform end-end-of prepare fixup for list of tables, if any of the tables
 
2657
    is a merge-algorithm VIEW, recursively fix up its underlying tables as
 
2658
    well.
 
2659
 
 
2660
*/
 
2661
 
 
2662
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
 
2663
{
 
2664
  for (; tbl; tbl= tbl->next_local)
 
2665
  {
 
2666
    if (tbl->on_expr)
 
2667
    {
 
2668
      tbl->prep_on_expr= tbl->on_expr;
 
2669
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2670
    }
 
2671
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2672
  }
 
2673
}
 
2674
 
 
2675
 
 
2676
/*
 
2677
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
 
2678
 
 
2679
  SYNOPSIS
 
2680
    st_select_lex::fix_prepare_information
 
2681
      thd          thread handler
 
2682
      conds        in/out pointer to WHERE condition to be met at execution
 
2683
      having_conds in/out pointer to HAVING condition to be met at execution
 
2684
  
 
2685
  DESCRIPTION
 
2686
    The passed WHERE and HAVING are to be saved for the future executions.
 
2687
    This function saves it, and returns a copy which can be thrashed during
 
2688
    this execution of the statement. By saving/thrashing here we mean only
 
2689
    AND/OR trees.
 
2690
    The function also calls fix_prepare_info_in_table_list that saves all
 
2691
    ON expressions.    
 
2692
*/
 
2693
 
 
2694
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
 
2695
                                            Item **having_conds)
 
2696
{
 
2697
  if (thd->stmt_arena->is_conventional() == false && first_execution)
 
2698
  {
 
2699
    first_execution= 0;
 
2700
    if (*conds)
 
2701
    {
 
2702
      prep_where= *conds;
 
2703
      *conds= where= prep_where->copy_andor_structure(thd);
 
2704
    }
 
2705
    if (*having_conds)
 
2706
    {
 
2707
      prep_having= *having_conds;
 
2708
      *having_conds= having= prep_having->copy_andor_structure(thd);
 
2709
    }
 
2710
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
 
2711
  }
 
2712
}
 
2713
 
 
2714
 
 
2715
/*
 
2716
  There are st_select_lex::add_table_to_list &
 
2717
  st_select_lex::set_lock_for_tables are in sql_parse.cc
 
2718
 
 
2719
  st_select_lex::print is in sql_select.cc
 
2720
 
 
2721
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
 
2722
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
 
2723
  st_select_lex_unit::change_result
2102
2724
  are in sql_union.cc
2103
2725
*/
2104
2726
 
2112
2734
 
2113
2735
  DESCRIPTION
2114
2736
    Used in filling up the tagged hints list.
2115
 
    This list is filled by first setting the kind of the hint as a
 
2737
    This list is filled by first setting the kind of the hint as a 
2116
2738
    context variable and then adding hints of the current kind.
2117
2739
    Then the context variable index_hint_type can be reset to the
2118
2740
    next hint type.
2119
2741
*/
2120
 
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg, index_clause_map clause)
2121
 
{
 
2742
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2743
                                        index_clause_map clause)
 
2744
2122
2745
  current_index_hint_type= type_arg;
2123
2746
  current_index_hint_clause= clause;
2124
2747
}
2125
2748
 
 
2749
 
2126
2750
/*
2127
2751
  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
2128
2752
 
2129
2753
  SYNOPSIS
2130
2754
    alloc_index_hints()
2131
 
      session         current thread.
 
2755
      thd         current thread.
2132
2756
*/
2133
 
void Select_Lex::alloc_index_hints (Session *session)
2134
 
{
2135
 
  index_hints= new (session->mem_root) List<Index_hint>();
 
2757
 
 
2758
void st_select_lex::alloc_index_hints (THD *thd)
 
2759
 
2760
  index_hints= new (thd->mem_root) List<Index_hint>(); 
2136
2761
}
2137
2762
 
 
2763
 
 
2764
 
2138
2765
/*
2139
 
  adds an element to the array storing index usage hints
 
2766
  adds an element to the array storing index usage hints 
2140
2767
  (ADD/FORCE/IGNORE INDEX).
2141
2768
 
2142
2769
  SYNOPSIS
2143
2770
    add_index_hint()
2144
 
      session         current thread.
 
2771
      thd         current thread.
2145
2772
      str         name of the index.
2146
2773
      length      number of characters in str.
2147
2774
 
2148
2775
  RETURN VALUE
2149
2776
    0 on success, non-zero otherwise
2150
2777
*/
2151
 
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
 
2778
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2152
2779
{
2153
 
  return index_hints->push_front (new (session->mem_root)
 
2780
  return index_hints->push_front (new (thd->mem_root) 
2154
2781
                                 Index_hint(current_index_hint_type,
2155
2782
                                            current_index_hint_clause,
2156
2783
                                            str, length));
2157
2784
}
2158
 
 
2159
 
bool check_for_sql_keyword(drizzled::lex_string_t const& string)
2160
 
{
2161
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2162
 
      return true;
2163
 
 
2164
 
  return false;
2165
 
}
2166
 
 
2167
 
bool check_for_sql_keyword(drizzled::st_lex_symbol const& string)
2168
 
{
2169
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2170
 
      return true;
2171
 
 
2172
 
  return false;
2173
 
}
2174
 
 
2175
 
 
2176
 
} /* namespace drizzled */