~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_lex.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

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
 
#define DRIZZLE_LEX 1
20
 
#include <drizzled/server_includes.h>
 
19
#define MYSQL_LEX 1
 
20
#include "mysql_priv.h"
 
21
#include "item_create.h"
 
22
#include <m_ctype.h>
 
23
#include <hash.h>
21
24
 
22
25
static int lex_one_token(void *arg, void *yythd);
23
26
 
42
45
  used when comparing keywords
43
46
*/
44
47
 
45
 
static unsigned char to_upper_lex[]=
 
48
static uchar to_upper_lex[]=
46
49
{
47
50
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
48
51
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
74
77
  "FORCE INDEX"
75
78
};
76
79
 
77
 
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
 
80
inline int lex_casecmp(const char *s, const char *t, uint len)
78
81
{
79
82
  while (len-- != 0 &&
80
 
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
 
83
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
81
84
  return (int) len+1;
82
85
}
83
86
 
86
89
 
87
90
void lex_init(void)
88
91
{
89
 
  uint32_t i;
 
92
  uint i;
 
93
  DBUG_ENTER("lex_init");
90
94
  for (i=0 ; i < array_elements(symbols) ; i++)
91
 
    symbols[i].length=(unsigned char) strlen(symbols[i].name);
 
95
    symbols[i].length=(uchar) strlen(symbols[i].name);
92
96
  for (i=0 ; i < array_elements(sql_functions) ; i++)
93
 
    sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
 
97
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
94
98
 
95
 
  return;
 
99
  DBUG_VOID_RETURN;
96
100
}
97
101
 
98
102
 
99
103
void lex_free(void)
100
104
{                                       // Call this when daemon ends
101
 
  return;
 
105
  DBUG_ENTER("lex_free");
 
106
  DBUG_VOID_RETURN;
102
107
}
103
108
 
104
109
 
105
110
void
106
111
st_parsing_options::reset()
107
112
{
108
 
  allows_variable= true;
109
 
  allows_select_into= true;
110
 
  allows_select_procedure= true;
111
 
  allows_derived= true;
 
113
  allows_variable= TRUE;
 
114
  allows_select_into= TRUE;
 
115
  allows_select_procedure= TRUE;
 
116
  allows_derived= TRUE;
112
117
}
113
118
 
114
119
Lex_input_stream::Lex_input_stream(THD *thd,
127
132
  m_tok_start_prev(NULL),
128
133
  m_buf(buffer),
129
134
  m_buf_length(length),
130
 
  m_echo(true),
 
135
  m_echo(TRUE),
131
136
  m_cpp_tok_start(NULL),
132
137
  m_cpp_tok_start_prev(NULL),
133
138
  m_cpp_tok_end(NULL),
136
141
  next_state(MY_LEX_START),
137
142
  found_semicolon(NULL),
138
143
  ignore_space(1),
 
144
  stmt_prepare_mode(FALSE),
139
145
  in_comment(NO_COMMENT),
140
146
  m_underscore_cs(NULL)
141
147
{
160
166
 
161
167
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
162
168
{
163
 
  assert(begin_ptr);
164
 
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
 
169
  DBUG_ASSERT(begin_ptr);
 
170
  DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
165
171
 
166
 
  uint32_t body_utf8_length=
 
172
  uint body_utf8_length=
167
173
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
168
174
    my_charset_utf8_bin.mbmaxlen;
169
175
 
198
204
void Lex_input_stream::body_utf8_append(const char *ptr,
199
205
                                        const char *end_ptr)
200
206
{
201
 
  assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
202
 
  assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
 
207
  DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
 
208
  DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
203
209
 
204
210
  if (!m_body_utf8)
205
211
    return;
243
249
 
244
250
void Lex_input_stream::body_utf8_append_literal(THD *thd,
245
251
                                                const LEX_STRING *txt,
246
 
                                                const CHARSET_INFO * const txt_cs,
 
252
                                                CHARSET_INFO *txt_cs,
247
253
                                                const char *end_ptr)
248
254
{
249
255
  if (!m_cpp_utf8_processed_ptr)
283
289
void lex_start(THD *thd)
284
290
{
285
291
  LEX *lex= thd->lex;
 
292
  DBUG_ENTER("lex_start");
286
293
 
287
294
  lex->thd= lex->unit.thd= thd;
288
295
 
295
302
  lex->value_list.empty();
296
303
  lex->update_list.empty();
297
304
  lex->param_list.empty();
 
305
  lex->view_list.empty();
298
306
  lex->auxiliary_table_list.empty();
299
307
  lex->unit.next= lex->unit.master=
300
308
    lex->unit.link_next= lex->unit.return_to= 0;
309
317
  lex->select_lex.init_order();
310
318
  lex->select_lex.group_list.empty();
311
319
  lex->describe= 0;
312
 
  lex->subqueries= false;
 
320
  lex->subqueries= FALSE;
313
321
  lex->derived_tables= 0;
314
322
  lex->lock_option= TL_READ;
315
323
  lex->leaf_tables_insert= 0;
326
334
  lex->duplicates= DUP_ERROR;
327
335
  lex->ignore= 0;
328
336
  lex->proc_list.first= 0;
329
 
  lex->escape_used= false;
 
337
  lex->escape_used= FALSE;
330
338
  lex->query_tables= 0;
331
 
  lex->reset_query_tables_list(false);
332
 
  lex->expr_allows_subselect= true;
333
 
  lex->use_only_table_context= false;
 
339
  lex->reset_query_tables_list(FALSE);
 
340
  lex->expr_allows_subselect= TRUE;
 
341
  lex->use_only_table_context= FALSE;
334
342
 
335
343
  lex->name.str= 0;
336
344
  lex->name.length= 0;
339
347
  lex->in_sum_func= NULL;
340
348
  /*
341
349
    ok, there must be a better solution for this, long-term
342
 
    I tried "memset" in the sql_yacc.yy code, but that for
 
350
    I tried "bzero" in the sql_yacc.yy code, but that for
343
351
    some reason made the values zero, even if they were set
344
352
  */
345
353
  lex->server_options.server_name= 0;
349
357
  lex->server_options.username= 0;
350
358
  lex->server_options.password= 0;
351
359
  lex->server_options.scheme= 0;
 
360
  lex->server_options.socket= 0;
352
361
  lex->server_options.owner= 0;
353
362
  lex->server_options.port= -1;
354
363
 
355
 
  lex->is_lex_started= true;
356
 
  return;
 
364
  lex->is_lex_started= TRUE;
 
365
  DBUG_VOID_RETURN;
357
366
}
358
367
 
359
368
void lex_end(LEX *lex)
360
369
{
 
370
  DBUG_ENTER("lex_end");
 
371
  DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
361
372
  if (lex->yacc_yyss)
362
373
  {
363
 
    free(lex->yacc_yyss);
364
 
    free(lex->yacc_yyvs);
 
374
    my_free(lex->yacc_yyss, MYF(0));
 
375
    my_free(lex->yacc_yyvs, MYF(0));
365
376
    lex->yacc_yyss= 0;
366
377
    lex->yacc_yyvs= 0;
367
378
  }
371
382
                     lex->plugins.elements);
372
383
  reset_dynamic(&lex->plugins);
373
384
 
374
 
  delete lex->result;
375
 
  lex->result= 0;
376
 
 
377
 
  return;
 
385
  DBUG_VOID_RETURN;
378
386
}
379
387
 
380
388
 
381
 
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
 
389
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
382
390
{
383
391
  const char *tok= lip->get_tok_start();
384
392
 
407
415
    1         name isn't a keyword
408
416
*/
409
417
 
410
 
bool is_keyword(const char *name, uint32_t len)
 
418
bool is_keyword(const char *name, uint len)
411
419
{
412
 
  assert(len != 0);
 
420
  DBUG_ASSERT(len != 0);
413
421
  return get_hash_symbol(name,len,0)!=0;
414
422
}
415
423
 
416
424
bool is_lex_native_function(const LEX_STRING *name)
417
425
{
418
 
  assert(name != NULL);
 
426
  DBUG_ASSERT(name != NULL);
419
427
  return (get_hash_symbol(name->str, name->length, 1) != 0);
420
428
}
421
429
 
422
430
/* make a copy of token before ptr and set yytoklen */
423
431
 
424
 
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
 
432
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
425
433
{
426
434
  LEX_STRING tmp;
427
435
  lip->yyUnget();                       // ptr points now after last token char
442
450
*/
443
451
 
444
452
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
445
 
                                   uint32_t skip,
446
 
                                   uint32_t length, char quote)
 
453
                                   uint skip,
 
454
                                   uint length, char quote)
447
455
{
448
456
  LEX_STRING tmp;
449
457
  const char *from, *end;
478
486
 
479
487
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
480
488
{
481
 
  register unsigned char c,sep;
482
 
  uint32_t found_escape=0;
483
 
  const CHARSET_INFO * const cs= lip->m_thd->charset();
 
489
  register uchar c,sep;
 
490
  uint found_escape=0;
 
491
  CHARSET_INFO *cs= lip->m_thd->charset();
484
492
 
485
493
  lip->tok_bitmap= 0;
486
494
  sep= lip->yyGetLast();                        // String should end with this
526
534
      /* Extract the text from the token */
527
535
      str += pre_skip;
528
536
      end -= post_skip;
529
 
      assert(end >= str);
 
537
      DBUG_ASSERT(end >= str);
530
538
 
531
539
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
532
540
        return (char*) "";              // Sql_alloc has set error flag
556
564
              continue;
557
565
          }
558
566
#endif
559
 
          if (*str == '\\' && str+1 != end)
 
567
          if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
568
              *str == '\\' && str+1 != end)
560
569
          {
561
570
            switch(*++str) {
562
571
            case 'n':
602
611
 
603
612
 
604
613
/*
605
 
** Calc type of integer; long integer, int64_t integer or real.
 
614
** Calc type of integer; long integer, longlong integer or real.
606
615
** Returns smallest type that match the string.
607
616
** When using unsigned long long values the result is converted to a real
608
617
** because else they will be unexpected sign changes because all calculation
609
 
** is done with int64_t or double.
 
618
** is done with longlong or double.
610
619
*/
611
620
 
612
621
static const char *long_str="2147483647";
613
 
static const uint32_t long_len=10;
 
622
static const uint long_len=10;
614
623
static const char *signed_long_str="-2147483648";
615
 
static const char *int64_t_str="9223372036854775807";
616
 
static const uint32_t int64_t_len=19;
617
 
static const char *signed_int64_t_str="-9223372036854775808";
618
 
static const uint32_t signed_int64_t_len=19;
619
 
static const char *unsigned_int64_t_str="18446744073709551615";
620
 
static const uint32_t unsigned_int64_t_len=20;
 
624
static const char *longlong_str="9223372036854775807";
 
625
static const uint longlong_len=19;
 
626
static const char *signed_longlong_str="-9223372036854775808";
 
627
static const uint signed_longlong_len=19;
 
628
static const char *unsigned_longlong_str="18446744073709551615";
 
629
static const uint unsigned_longlong_len=20;
621
630
 
622
 
static inline uint32_t int_token(const char *str,uint32_t length)
 
631
static inline uint int_token(const char *str,uint length)
623
632
{
624
633
  if (length < long_len)                        // quick normal case
625
634
    return NUM;
641
650
  if (length < long_len)
642
651
    return NUM;
643
652
 
644
 
  uint32_t smaller,bigger;
 
653
  uint smaller,bigger;
645
654
  const char *cmp;
646
655
  if (neg)
647
656
  {
651
660
      smaller=NUM;                              // If <= signed_long_str
652
661
      bigger=LONG_NUM;                          // If >= signed_long_str
653
662
    }
654
 
    else if (length < signed_int64_t_len)
 
663
    else if (length < signed_longlong_len)
655
664
      return LONG_NUM;
656
 
    else if (length > signed_int64_t_len)
 
665
    else if (length > signed_longlong_len)
657
666
      return DECIMAL_NUM;
658
667
    else
659
668
    {
660
 
      cmp=signed_int64_t_str+1;
661
 
      smaller=LONG_NUM;                         // If <= signed_int64_t_str
 
669
      cmp=signed_longlong_str+1;
 
670
      smaller=LONG_NUM;                         // If <= signed_longlong_str
662
671
      bigger=DECIMAL_NUM;
663
672
    }
664
673
  }
670
679
      smaller=NUM;
671
680
      bigger=LONG_NUM;
672
681
    }
673
 
    else if (length < int64_t_len)
 
682
    else if (length < longlong_len)
674
683
      return LONG_NUM;
675
 
    else if (length > int64_t_len)
 
684
    else if (length > longlong_len)
676
685
    {
677
 
      if (length > unsigned_int64_t_len)
 
686
      if (length > unsigned_longlong_len)
678
687
        return DECIMAL_NUM;
679
 
      cmp=unsigned_int64_t_str;
 
688
      cmp=unsigned_longlong_str;
680
689
      smaller=ULONGLONG_NUM;
681
690
      bigger=DECIMAL_NUM;
682
691
    }
683
692
    else
684
693
    {
685
 
      cmp=int64_t_str;
 
694
      cmp=longlong_str;
686
695
      smaller=LONG_NUM;
687
696
      bigger= ULONGLONG_NUM;
688
697
    }
689
698
  }
690
699
  while (*cmp && *cmp++ == *str++) ;
691
 
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
 
700
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
692
701
}
693
702
 
694
703
 
725
734
  switch(token) {
726
735
  case WITH:
727
736
    /*
728
 
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
 
737
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
729
738
      which makes the grammar LALR(2).
730
739
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
731
740
      to transform the grammar into a LALR(1) grammar,
732
741
      which sql_yacc.yy can process.
733
742
    */
734
743
    token= lex_one_token(arg, yythd);
735
 
    if (token == ROLLUP_SYM)
736
 
    {
 
744
    switch(token) {
 
745
    case CUBE_SYM:
 
746
      return WITH_CUBE_SYM;
 
747
    case ROLLUP_SYM:
737
748
      return WITH_ROLLUP_SYM;
738
 
    }
739
 
    else
740
 
    {
 
749
    default:
741
750
      /*
742
751
        Save the token following 'WITH'
743
752
      */
765
774
  Lex_input_stream *lip= thd->m_lip;
766
775
  LEX *lex= thd->lex;
767
776
  YYSTYPE *yylval=(YYSTYPE*) arg;
768
 
  const CHARSET_INFO * const cs= thd->charset();
769
 
  unsigned char *state_map= cs->state_map;
770
 
  unsigned char *ident_map= cs->ident_map;
 
777
  CHARSET_INFO *cs= thd->charset();
 
778
  uchar *state_map= cs->state_map;
 
779
  uchar *ident_map= cs->ident_map;
771
780
 
772
781
  lip->yylval=yylval;                   // The global state
773
782
 
826
835
        */
827
836
        lip->restart_token();
828
837
      }
 
838
      else
 
839
      {
 
840
        /*
 
841
          Check for a placeholder: it should not precede a possible identifier
 
842
          because of binlogging: when a placeholder is replaced with
 
843
          its value in a query for the binlog, the query must stay
 
844
          grammatically correct.
 
845
        */
 
846
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[(uint8_t)(lip->yyPeek())])
 
847
        return(PARAM_MARKER);
 
848
      }
829
849
 
830
850
      return((int) c);
831
851
 
 
852
    case MY_LEX_IDENT_OR_NCHAR:
 
853
      if (lip->yyPeek() != '\'')
 
854
      {
 
855
        state= MY_LEX_IDENT;
 
856
        break;
 
857
      }
 
858
      /* Found N'string' */
 
859
      lip->yySkip();                         // Skip '
 
860
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
 
861
      {
 
862
        state= MY_LEX_CHAR;             // Read char by char
 
863
        break;
 
864
      }
 
865
      yylval->lex_str.length= lip->yytoklen;
 
866
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
867
      return(NCHAR_STRING);
 
868
 
832
869
    case MY_LEX_IDENT_OR_HEX:
833
870
      if (lip->yyPeek() == '\'')
834
871
      {                                 // Found x'hex-number'
911
948
 
912
949
      if (yylval->lex_str.str[0] == '_')
913
950
      {
914
 
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
915
 
                                                             MY_CS_PRIMARY, MYF(0));
 
951
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
952
                                                MY_CS_PRIMARY, MYF(0));
916
953
        if (cs)
917
954
        {
918
955
          yylval->charset= cs;
1036
1073
 
1037
1074
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
1038
1075
    {
1039
 
      uint32_t double_quotes= 0;
 
1076
      uint double_quotes= 0;
1040
1077
      char quote_char= c;                       // Used char
1041
1078
      while ((c=lip->yyGet()))
1042
1079
      {
1213
1250
      {
1214
1251
        lip->in_comment= DISCARD_COMMENT;
1215
1252
        /* Accept '/' '*' '!', but do not keep this marker. */
1216
 
        lip->set_echo(false);
 
1253
        lip->set_echo(FALSE);
1217
1254
        lip->yySkip();
1218
1255
        lip->yySkip();
1219
1256
        lip->yySkip();
1246
1283
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1247
1284
          lip->yySkipn(5);
1248
1285
 
1249
 
          if (version <= DRIZZLE_VERSION_ID)
 
1286
          if (version <= MYSQL_VERSION_ID)
1250
1287
          {
1251
1288
            /* Expand the content of the special comment as real code */
1252
 
            lip->set_echo(true);
 
1289
            lip->set_echo(TRUE);
1253
1290
            state=MY_LEX_START;
1254
1291
            break;
1255
1292
          }
1257
1294
        else
1258
1295
        {
1259
1296
          state=MY_LEX_START;
1260
 
          lip->set_echo(true);
 
1297
          lip->set_echo(TRUE);
1261
1298
          break;
1262
1299
        }
1263
1300
      }
1275
1312
        Note: There is no such thing as nesting comments,
1276
1313
        the first '*' '/' sequence seen will mark the end.
1277
1314
      */
1278
 
      comment_closed= false;
 
1315
      comment_closed= FALSE;
1279
1316
      while (! lip->eof())
1280
1317
      {
1281
1318
        c= lip->yyGet();
1284
1321
          if (lip->yyPeek() == '/')
1285
1322
          {
1286
1323
            lip->yySkip();
1287
 
            comment_closed= true;
 
1324
            comment_closed= TRUE;
1288
1325
            state = MY_LEX_START;
1289
1326
            break;
1290
1327
          }
1297
1334
        return (ABORT_SYM);
1298
1335
      state = MY_LEX_START;             // Try again
1299
1336
      lip->in_comment= NO_COMMENT;
1300
 
      lip->set_echo(true);
 
1337
      lip->set_echo(TRUE);
1301
1338
      break;
1302
1339
    case MY_LEX_END_LONG_COMMENT:
1303
1340
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1308
1345
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1309
1346
        lip->yySkipn(2);
1310
1347
        /* And start recording the tokens again */
1311
 
        lip->set_echo(true);
 
1348
        lip->set_echo(TRUE);
1312
1349
        lip->in_comment=NO_COMMENT;
1313
1350
        state=MY_LEX_START;
1314
1351
      }
1326
1363
    case MY_LEX_SEMICOLON:                      // optional line terminator
1327
1364
      if (lip->yyPeek())
1328
1365
      {
1329
 
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1366
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && 
 
1367
            !lip->stmt_prepare_mode)
1330
1368
        {
1331
1369
          lip->found_semicolon= lip->get_ptr();
1332
1370
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
1333
1371
          lip->next_state= MY_LEX_END;
1334
 
          lip->set_echo(true);
 
1372
          lip->set_echo(TRUE);
1335
1373
          return (END_OF_INPUT);
1336
1374
        }
1337
1375
        state= MY_LEX_CHAR;             // Return ';'
1343
1381
      if (lip->eof())
1344
1382
      {
1345
1383
        lip->yyUnget();                 // Reject the last '\0'
1346
 
        lip->set_echo(false);
 
1384
        lip->set_echo(FALSE);
1347
1385
        lip->yySkip();
1348
 
        lip->set_echo(true);
 
1386
        lip->set_echo(TRUE);
1349
1387
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
1350
1388
        if (lip->in_comment != NO_COMMENT)
1351
1389
          return (ABORT_SYM);
1475
1513
}
1476
1514
 
1477
1515
 
1478
 
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
 
1516
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
1479
1517
{
1480
1518
  /*
1481
1519
    TODO:
1544
1582
  embedding= leaf_tables= 0;
1545
1583
  item_list.empty();
1546
1584
  join= 0;
1547
 
  having= where= 0;
 
1585
  having= prep_having= where= prep_where= 0;
1548
1586
  olap= UNSPECIFIED_OLAP_TYPE;
1549
1587
  having_fix_field= 0;
1550
1588
  context.select_lex= this;
1567
1605
  select_n_having_items= 0;
1568
1606
  subquery_in_having= explicit_limit= 0;
1569
1607
  is_item_list_lookup= 0;
 
1608
  first_execution= 1;
 
1609
  first_cond_optimization= 1;
1570
1610
  parsing_place= NO_MATTER;
1571
 
  exclude_from_table_unique_test= false;
 
1611
  exclude_from_table_unique_test= no_wrap_view_item= FALSE;
1572
1612
  nest_level= 0;
1573
1613
  link_next= 0;
1574
1614
}
1591
1631
  linkage= UNSPECIFIED_TYPE;
1592
1632
  order_list.elements= 0;
1593
1633
  order_list.first= 0;
1594
 
  order_list.next= (unsigned char**) &order_list.first;
 
1634
  order_list.next= (uchar**) &order_list.first;
1595
1635
  /* Set limit and offset to default values */
1596
1636
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1597
1637
  offset_limit= 0;      /* denotes the default offset = 0 */
1804
1844
          sl->uncacheable|= UNCACHEABLE_UNITED;
1805
1845
      }
1806
1846
    }
1807
 
    s->is_correlated= true;
 
1847
    s->is_correlated= TRUE;
1808
1848
    Item_subselect *subquery_predicate= s->master_unit()->item;
1809
1849
    if (subquery_predicate)
1810
 
      subquery_predicate->is_correlated= true;
 
1850
      subquery_predicate->is_correlated= TRUE;
1811
1851
  }
1812
1852
}
1813
1853
 
1814
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
1815
 
{ return 1; }
 
1854
bool st_select_lex_node::set_braces(bool value)      { return 1; }
1816
1855
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
1817
 
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
1818
 
TableList* st_select_lex_node::get_table_list()     { return 0; }
 
1856
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1857
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
1819
1858
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
1820
 
TableList *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)))
 
1859
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
 
1860
                                                  LEX_STRING *alias,
 
1861
                                                  ulong table_join_options,
 
1862
                                                  thr_lock_type flags,
 
1863
                                                  List<Index_hint> *hints,
 
1864
                                                  LEX_STRING *option)
1827
1865
{
1828
1866
  return 0;
1829
1867
}
1830
 
uint32_t st_select_lex_node::get_table_join_options()
 
1868
ulong st_select_lex_node::get_table_join_options()
1831
1869
{
1832
1870
  return 0;
1833
1871
}
1865
1903
}
1866
1904
 
1867
1905
 
1868
 
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
1869
 
                                     Item *item)
 
1906
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
1870
1907
{
1871
 
  return(item_list.push_back(item));
 
1908
  DBUG_ENTER("st_select_lex::add_item_to_list");
 
1909
  DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
 
1910
  DBUG_RETURN(item_list.push_back(item));
1872
1911
}
1873
1912
 
1874
1913
 
1904
1943
}
1905
1944
 
1906
1945
 
1907
 
uint32_t st_select_lex::get_in_sum_expr()
 
1946
uint st_select_lex::get_in_sum_expr()
1908
1947
{
1909
1948
  return in_sum_expr;
1910
1949
}
1911
1950
 
1912
1951
 
1913
 
TableList* st_select_lex::get_table_list()
 
1952
TABLE_LIST* st_select_lex::get_table_list()
1914
1953
{
1915
 
  return (TableList*) table_list.first;
 
1954
  return (TABLE_LIST*) table_list.first;
1916
1955
}
1917
1956
 
1918
1957
List<Item>* st_select_lex::get_item_list()
1920
1959
  return &item_list;
1921
1960
}
1922
1961
 
1923
 
uint32_t st_select_lex::get_table_join_options()
 
1962
ulong st_select_lex::get_table_join_options()
1924
1963
{
1925
1964
  return table_join_options;
1926
1965
}
1927
1966
 
1928
1967
 
1929
 
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
 
1968
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1930
1969
{
1931
1970
  if (ref_pointer_array)
1932
1971
    return 0;
1933
1972
 
 
1973
  /*
 
1974
    We have to create array in prepared statement memory if it is
 
1975
    prepared statement
 
1976
  */
 
1977
  Query_arena *arena= thd->stmt_arena;
1934
1978
  return (ref_pointer_array=
1935
 
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
 
1979
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1936
1980
                                                 item_list.elements +
1937
1981
                                                 select_n_having_items +
1938
1982
                                                 select_n_where_fields +
1951
1995
      if (union_all)
1952
1996
        str->append(STRING_WITH_LEN("all "));
1953
1997
      else if (union_distinct == sl)
1954
 
        union_all= true;
 
1998
        union_all= TRUE;
1955
1999
    }
1956
2000
    if (sl->braces)
1957
2001
      str->append('(');
1966
2010
      str->append(STRING_WITH_LEN(" order by "));
1967
2011
      fake_select_lex->print_order(
1968
2012
        str,
1969
 
        (order_st *) fake_select_lex->order_list.first,
 
2013
        (ORDER *) fake_select_lex->order_list.first,
1970
2014
        query_type);
1971
2015
    }
1972
2016
    fake_select_lex->print_limit(thd, str, query_type);
1975
2019
 
1976
2020
 
1977
2021
void st_select_lex::print_order(String *str,
1978
 
                                order_st *order,
 
2022
                                ORDER *order,
1979
2023
                                enum_query_type query_type)
1980
2024
{
1981
2025
  for (; order; order= order->next)
1983
2027
    if (order->counter_used)
1984
2028
    {
1985
2029
      char buffer[20];
1986
 
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
 
2030
      uint length= my_snprintf(buffer, 20, "%d", order->counter);
1987
2031
      str->append(buffer, length);
1988
2032
    }
1989
2033
    else
1996
2040
}
1997
2041
 
1998
2042
 
1999
 
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
 
2043
void st_select_lex::print_limit(THD *thd,
2000
2044
                                String *str,
2001
2045
                                enum_query_type query_type)
2002
2046
{
2010
2054
        subs_type == Item_subselect::IN_SUBS ||
2011
2055
        subs_type == Item_subselect::ALL_SUBS)
2012
2056
    {
2013
 
      assert(!item->fixed ||
 
2057
      DBUG_ASSERT(!item->fixed ||
2014
2058
                  /*
2015
2059
                    If not using materialization both:
2016
2060
                    select_limit == 1, and there should be no offset_limit.
2018
2062
                  (((subs_type == Item_subselect::IN_SUBS) &&
2019
2063
                    ((Item_in_subselect*)item)->exec_method ==
2020
2064
                    Item_in_subselect::MATERIALIZATION) ?
2021
 
                   true :
2022
 
                   (select_limit->val_int() == 1L) &&
 
2065
                   TRUE :
 
2066
                   (select_limit->val_int() == LL(1)) &&
2023
2067
                   offset_limit == 0));
2024
2068
      return;
2025
2069
    }
2048
2092
  to implement the clean up.
2049
2093
*/
2050
2094
 
2051
 
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
 
2095
void st_lex::cleanup_lex_after_parse_error(THD *thd)
2052
2096
{
2053
2097
}
2054
2098
 
2057
2101
 
2058
2102
  SYNOPSIS
2059
2103
    reset_query_tables_list()
2060
 
      init  true  - we should perform full initialization of object with
 
2104
      init  TRUE  - we should perform full initialization of object with
2061
2105
                    allocating needed memory
2062
 
            false - object is already initialized so we should only reset
 
2106
            FALSE - object is already initialized so we should only reset
2063
2107
                    its state so it can be used for parsing/processing
2064
2108
                    of new statement
2065
2109
 
2074
2118
{
2075
2119
  if (!init && query_tables)
2076
2120
  {
2077
 
    TableList *table= query_tables;
 
2121
    TABLE_LIST *table= query_tables;
2078
2122
    for (;;)
2079
2123
    {
2080
2124
      if (query_tables_last == &table->next_global ||
2141
2185
                         plugins_static_buffer,
2142
2186
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
2143
2187
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2144
 
  reset_query_tables_list(true);
 
2188
  reset_query_tables_list(TRUE);
2145
2189
}
2146
2190
 
2147
2191
 
2159
2203
    several underlying tables.
2160
2204
 
2161
2205
  RETURN
2162
 
    false - only temporary table algorithm can be used
2163
 
    true  - merge algorithm can be used
 
2206
    FALSE - only temporary table algorithm can be used
 
2207
    TRUE  - merge algorithm can be used
2164
2208
*/
2165
2209
 
2166
2210
bool st_lex::can_be_merged()
2205
2249
  DESCRIPTION
2206
2250
    Only listed here commands can use merge algorithm in top level
2207
2251
    SELECT_LEX (for subqueries will be used merge algorithm if
2208
 
    st_lex::can_not_use_merged() is not true).
 
2252
    st_lex::can_not_use_merged() is not TRUE).
2209
2253
 
2210
2254
  RETURN
2211
 
    false - command can't use merged VIEWs
2212
 
    true  - VIEWs with MERGE algorithms can be used
 
2255
    FALSE - command can't use merged VIEWs
 
2256
    TRUE  - VIEWs with MERGE algorithms can be used
2213
2257
*/
2214
2258
 
2215
2259
bool st_lex::can_use_merged()
2227
2271
  case SQLCOM_REPLACE:
2228
2272
  case SQLCOM_REPLACE_SELECT:
2229
2273
  case SQLCOM_LOAD:
2230
 
    return true;
 
2274
    return TRUE;
2231
2275
  default:
2232
 
    return false;
 
2276
    return FALSE;
2233
2277
  }
2234
2278
}
2235
2279
 
2244
2288
    listed here (see also st_lex::can_use_merged()).
2245
2289
 
2246
2290
  RETURN
2247
 
    false - command can't use merged VIEWs
2248
 
    true  - VIEWs with MERGE algorithms can be used
 
2291
    FALSE - command can't use merged VIEWs
 
2292
    TRUE  - VIEWs with MERGE algorithms can be used
2249
2293
*/
2250
2294
 
2251
2295
bool st_lex::can_not_use_merged()
2258
2302
    see get_schema_tables_result function
2259
2303
  */
2260
2304
  case SQLCOM_SHOW_FIELDS:
2261
 
    return true;
 
2305
    return TRUE;
2262
2306
  default:
2263
 
    return false;
 
2307
    return FALSE;
2264
2308
  }
2265
2309
}
2266
2310
 
2271
2315
    only_view_structure()
2272
2316
 
2273
2317
  RETURN
2274
 
    true yes, we need only structure
2275
 
    false no, we need data
 
2318
    TRUE yes, we need only structure
 
2319
    FALSE no, we need data
2276
2320
*/
2277
2321
 
2278
2322
bool st_lex::only_view_structure()
2281
2325
  case SQLCOM_SHOW_CREATE:
2282
2326
  case SQLCOM_SHOW_TABLES:
2283
2327
  case SQLCOM_SHOW_FIELDS:
2284
 
    return true;
 
2328
    return TRUE;
2285
2329
  default:
2286
 
    return false;
 
2330
    return FALSE;
2287
2331
  }
2288
2332
}
2289
2333
 
2295
2339
    need_correct_ident()
2296
2340
 
2297
2341
  RETURN
2298
 
    true yes, we need only structure
2299
 
    false no, we need data
 
2342
    TRUE yes, we need only structure
 
2343
    FALSE no, we need data
2300
2344
*/
2301
2345
 
2302
2346
 
2306
2350
  {
2307
2351
  case SQLCOM_SHOW_CREATE:
2308
2352
  case SQLCOM_SHOW_TABLES:
2309
 
    return true;
 
2353
    return TRUE;
2310
2354
  default:
2311
 
    return false;
 
2355
    return FALSE;
2312
2356
  }
2313
2357
}
2314
2358
 
 
2359
/*
 
2360
  Get effective type of CHECK OPTION for given view
 
2361
 
 
2362
  SYNOPSIS
 
2363
    get_effective_with_check()
 
2364
    view    given view
 
2365
 
 
2366
  NOTE
 
2367
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
 
2368
    so we do not.
 
2369
 
 
2370
  RETURN
 
2371
    VIEW_CHECK_NONE      no need CHECK OPTION
 
2372
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
 
2373
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
 
2374
*/
 
2375
 
 
2376
uint8 st_lex::get_effective_with_check(TABLE_LIST *view)
 
2377
{
 
2378
  return 0;
 
2379
}
 
2380
 
2315
2381
 
2316
2382
/**
2317
2383
  This method should be called only during parsing.
2328
2394
 
2329
2395
  This method is needed to support this rule.
2330
2396
 
2331
 
  @return true in case of error (parsing should be aborted, false in
 
2397
  @return TRUE in case of error (parsing should be aborted, FALSE in
2332
2398
  case of success
2333
2399
*/
2334
2400
 
2349
2415
void st_select_lex_unit::set_limit(st_select_lex *sl)
2350
2416
{
2351
2417
  ha_rows select_limit_val;
2352
 
  uint64_t val;
 
2418
  ulonglong val;
2353
2419
 
2354
2420
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2355
2421
  select_limit_val= (ha_rows)val;
2356
2422
#ifndef BIG_TABLES
2357
2423
  /* 
2358
 
    Check for overflow : ha_rows can be smaller then uint64_t if
 
2424
    Check for overflow : ha_rows can be smaller then ulonglong if
2359
2425
    BIG_TABLES is off.
2360
2426
    */
2361
 
  if (val != (uint64_t)select_limit_val)
 
2427
  if (val != (ulonglong)select_limit_val)
2362
2428
    select_limit_val= HA_POS_ERROR;
2363
2429
#endif
2364
2430
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2365
 
                                                 0UL);
 
2431
                                                 ULL(0));
2366
2432
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2367
2433
  if (select_limit_cnt < select_limit_val)
2368
2434
    select_limit_cnt= HA_POS_ERROR;             // no limit
2387
2453
      In this case link_to_local is set.
2388
2454
 
2389
2455
*/
2390
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2456
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
2391
2457
{
2392
 
  TableList *first;
 
2458
  TABLE_LIST *first;
2393
2459
  if ((first= query_tables))
2394
2460
  {
2395
2461
    /*
2408
2474
    {
2409
2475
      select_lex.context.table_list= 
2410
2476
        select_lex.context.first_name_resolution_table= first->next_local;
2411
 
      select_lex.table_list.first= (unsigned char*) (first->next_local);
 
2477
      select_lex.table_list.first= (uchar*) (first->next_local);
2412
2478
      select_lex.table_list.elements--; //safety
2413
2479
      first->next_local= 0;
2414
2480
      /*
2440
2506
 
2441
2507
void st_lex::first_lists_tables_same()
2442
2508
{
2443
 
  TableList *first_table= (TableList*) select_lex.table_list.first;
 
2509
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
2444
2510
  if (query_tables != first_table && first_table != 0)
2445
2511
  {
2446
 
    TableList *next;
 
2512
    TABLE_LIST *next;
2447
2513
    if (query_tables_last == &first_table->next_global)
2448
2514
      query_tables_last= first_table->prev_global;
2449
2515
 
2474
2540
    global list
2475
2541
*/
2476
2542
 
2477
 
void st_lex::link_first_table_back(TableList *first,
 
2543
void st_lex::link_first_table_back(TABLE_LIST *first,
2478
2544
                                   bool link_to_local)
2479
2545
{
2480
2546
  if (first)
2487
2553
 
2488
2554
    if (link_to_local)
2489
2555
    {
2490
 
      first->next_local= (TableList*) select_lex.table_list.first;
 
2556
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
2491
2557
      select_lex.context.table_list= first;
2492
 
      select_lex.table_list.first= (unsigned char*) first;
 
2558
      select_lex.table_list.first= (uchar*) first;
2493
2559
      select_lex.table_list.elements++; //safety
2494
2560
    }
2495
2561
  }
2506
2572
  NOTE
2507
2573
    This method is mostly responsible for cleaning up of selects lists and
2508
2574
    derived tables state. To rollback changes in Query_tables_list one has
2509
 
    to call Query_tables_list::reset_query_tables_list(false).
 
2575
    to call Query_tables_list::reset_query_tables_list(FALSE).
2510
2576
*/
2511
2577
 
2512
2578
void st_lex::cleanup_after_one_table_open()
2544
2610
      backup  Pointer to Query_tables_list instance to be used for backup
2545
2611
*/
2546
2612
 
2547
 
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2613
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup)
2548
2614
{
2549
2615
}
2550
2616
 
2557
2623
      backup  Pointer to Query_tables_list instance used for backup
2558
2624
*/
2559
2625
 
2560
 
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2626
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup)
2561
2627
{
2562
2628
}
2563
2629
 
2569
2635
    st_lex:table_or_sp_used()
2570
2636
 
2571
2637
  RETURN
2572
 
    false  No routines and tables used
2573
 
    true   Either or both routines and tables are used.
 
2638
    FALSE  No routines and tables used
 
2639
    TRUE   Either or both routines and tables are used.
2574
2640
*/
2575
2641
 
2576
2642
bool st_lex::table_or_sp_used()
2577
2643
{
 
2644
  DBUG_ENTER("table_or_sp_used");
 
2645
 
2578
2646
  if (sroutines.records || query_tables)
2579
 
    return(true);
 
2647
    DBUG_RETURN(TRUE);
2580
2648
 
2581
 
  return(false);
 
2649
  DBUG_RETURN(FALSE);
2582
2650
}
2583
2651
 
2584
2652
 
2597
2665
 
2598
2666
*/
2599
2667
 
2600
 
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
 
2668
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
2601
2669
{
2602
2670
  for (; tbl; tbl= tbl->next_local)
2603
2671
  {
2612
2680
 
2613
2681
 
2614
2682
/*
 
2683
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
 
2684
 
 
2685
  SYNOPSIS
 
2686
    st_select_lex::fix_prepare_information
 
2687
      thd          thread handler
 
2688
      conds        in/out pointer to WHERE condition to be met at execution
 
2689
      having_conds in/out pointer to HAVING condition to be met at execution
 
2690
  
 
2691
  DESCRIPTION
 
2692
    The passed WHERE and HAVING are to be saved for the future executions.
 
2693
    This function saves it, and returns a copy which can be thrashed during
 
2694
    this execution of the statement. By saving/thrashing here we mean only
 
2695
    AND/OR trees.
 
2696
    The function also calls fix_prepare_info_in_table_list that saves all
 
2697
    ON expressions.    
 
2698
*/
 
2699
 
 
2700
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
 
2701
                                            Item **having_conds)
 
2702
{
 
2703
  if (thd->stmt_arena->is_conventional() == false && first_execution)
 
2704
  {
 
2705
    first_execution= 0;
 
2706
    if (*conds)
 
2707
    {
 
2708
      prep_where= *conds;
 
2709
      *conds= where= prep_where->copy_andor_structure(thd);
 
2710
    }
 
2711
    if (*having_conds)
 
2712
    {
 
2713
      prep_having= *having_conds;
 
2714
      *having_conds= having= prep_having->copy_andor_structure(thd);
 
2715
    }
 
2716
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
 
2717
  }
 
2718
}
 
2719
 
 
2720
 
 
2721
/*
2615
2722
  There are st_select_lex::add_table_to_list &
2616
2723
  st_select_lex::set_lock_for_tables are in sql_parse.cc
2617
2724
 
2674
2781
  RETURN VALUE
2675
2782
    0 on success, non-zero otherwise
2676
2783
*/
2677
 
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
 
2784
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2678
2785
{
2679
2786
  return index_hints->push_front (new (thd->mem_root) 
2680
2787
                                 Index_hint(current_index_hint_type,