~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

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