~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Monty Taylor
  • Date: 2008-10-23 00:05:28 UTC
  • Revision ID: monty@inaugust.com-20081023000528-grdvrd8c4058nutm
Moved my_handler to myisam, which is where it actually belongs.

Show diffs side-by-side

added added

removed removed

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