~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2009-05-15 17:06:35 UTC
  • mto: This revision was merged to the branch mainline in revision 1023.
  • Revision ID: brian@gaz-20090515170635-croy1u63a3gqdn9n
Dead convert functions for character sets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
19
#define DRIZZLE_LEX 1
20
 
#include <drizzled/server_includes.h>
21
 
 
22
 
static int lex_one_token(void *arg, void *yythd);
 
20
#include "drizzled/server_includes.h"
 
21
#include "drizzled/item/num.h"
 
22
#include "drizzled/error.h"
 
23
#include "drizzled/session.h"
 
24
#include "drizzled/sql_base.h"
 
25
#include "drizzled/lookup_symbol.h"
 
26
#include "drizzled/index_hint.h"
 
27
 
 
28
#include <ctype.h>
 
29
 
 
30
using namespace std;
 
31
 
 
32
static int lex_one_token(void *arg, void *yysession);
 
33
 
23
34
 
24
35
/*
25
36
  We are using pointer to this variable for distinguishing between assignment
33
44
*/
34
45
const LEX_STRING null_lex_str= {NULL, 0};
35
46
 
36
 
/* Longest standard keyword name */
37
 
 
38
 
#define TOCK_NAME_LENGTH 24
39
 
 
40
 
/*
41
 
  The following data is based on the latin1 character set, and is only
42
 
  used when comparing keywords
43
 
*/
44
 
 
45
 
static unsigned char to_upper_lex[]=
46
 
{
47
 
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
48
 
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
49
 
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
50
 
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
51
 
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
52
 
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
53
 
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
54
 
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
55
 
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
56
 
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
57
 
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
58
 
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
59
 
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
60
 
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
61
 
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
62
 
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
63
 
};
64
 
 
65
 
/* 
66
 
  Names of the index hints (for error messages). Keep in sync with 
67
 
  index_hint_type 
68
 
*/
69
 
 
70
 
const char * index_hint_type_name[] =
71
 
{
72
 
  "IGNORE INDEX", 
73
 
  "USE INDEX", 
74
 
  "FORCE INDEX"
75
 
};
76
 
 
77
 
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
78
 
{
79
 
  while (len-- != 0 &&
80
 
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
81
 
  return (int) len+1;
82
 
}
83
 
 
84
 
#include <lex_hash.h>
85
 
 
86
 
 
87
 
void lex_init(void)
88
 
{
89
 
  uint32_t i;
90
 
  for (i=0 ; i < array_elements(symbols) ; i++)
91
 
    symbols[i].length=(unsigned char) strlen(symbols[i].name);
92
 
  for (i=0 ; i < array_elements(sql_functions) ; i++)
93
 
    sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
94
 
 
95
 
  return;
96
 
}
97
 
 
98
 
 
99
 
void lex_free(void)
100
 
{                                       // Call this when daemon ends
101
 
  return;
102
 
}
103
 
 
104
 
 
105
47
void
106
48
st_parsing_options::reset()
107
49
{
108
 
  allows_variable= true;
109
 
  allows_select_into= true;
110
50
  allows_select_procedure= true;
111
 
  allows_derived= true;
112
51
}
113
52
 
114
 
Lex_input_stream::Lex_input_stream(THD *thd,
 
53
Lex_input_stream::Lex_input_stream(Session *session,
115
54
                                   const char* buffer,
116
55
                                   unsigned int length)
117
 
: m_thd(thd),
 
56
: m_session(session),
118
57
  yylineno(1),
119
58
  yytoklen(0),
120
59
  yylval(NULL),
139
78
  in_comment(NO_COMMENT),
140
79
  m_underscore_cs(NULL)
141
80
{
142
 
  m_cpp_buf= (char*) thd->alloc(length + 1);
 
81
  m_cpp_buf= (char*) session->alloc(length + 1);
143
82
  m_cpp_ptr= m_cpp_buf;
144
83
}
145
84
 
153
92
     statement;
154
93
  2) Determine the beginning of the body.
155
94
 
156
 
  @param thd        Thread context.
 
95
  @param session        Thread context.
157
96
  @param begin_ptr  Pointer to the start of the body in the pre-processed
158
97
                    buffer.
159
98
*/
160
99
 
161
 
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
 
100
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
162
101
{
163
102
  assert(begin_ptr);
164
103
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
165
104
 
166
105
  uint32_t body_utf8_length=
167
 
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
 
106
    (m_buf_length / default_charset_info->mbminlen) *
168
107
    my_charset_utf8_bin.mbmaxlen;
169
108
 
170
 
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
 
109
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
171
110
  m_body_utf8_ptr= m_body_utf8;
172
111
  *m_body_utf8_ptr= 0;
173
112
 
233
172
  The operation converts the specified text literal to the utf8 and appends
234
173
  the result to the utf8-body.
235
174
 
236
 
  @param thd      Thread context.
 
175
  @param session      Thread context.
237
176
  @param txt      Text literal.
238
177
  @param txt_cs   Character set of the text literal.
239
178
  @param end_ptr  Pointer in the pre-processed buffer, to which
241
180
                  operation.
242
181
*/
243
182
 
244
 
void Lex_input_stream::body_utf8_append_literal(THD *thd,
 
183
void Lex_input_stream::body_utf8_append_literal(Session *session,
245
184
                                                const LEX_STRING *txt,
246
185
                                                const CHARSET_INFO * const txt_cs,
247
186
                                                const char *end_ptr)
253
192
 
254
193
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
255
194
  {
256
 
    thd->convert_string(&utf_txt,
 
195
    session->convert_string(&utf_txt,
257
196
                        &my_charset_utf8_general_ci,
258
197
                        txt->str, txt->length,
259
198
                        txt_cs);
280
219
  (We already do too much here)
281
220
*/
282
221
 
283
 
void lex_start(THD *thd)
 
222
void lex_start(Session *session)
284
223
{
285
 
  LEX *lex= thd->lex;
 
224
  LEX *lex= session->lex;
286
225
 
287
 
  lex->thd= lex->unit.thd= thd;
 
226
  lex->session= lex->unit.session= session;
288
227
 
289
228
  lex->context_stack.empty();
290
229
  lex->unit.init_query();
304
243
  lex->select_lex.master= &lex->unit;
305
244
  lex->select_lex.prev= &lex->unit.slave;
306
245
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
307
 
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
 
246
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
308
247
  lex->select_lex.options= 0;
309
248
  lex->select_lex.init_order();
310
249
  lex->select_lex.group_list.empty();
314
253
  lex->lock_option= TL_READ;
315
254
  lex->leaf_tables_insert= 0;
316
255
  lex->parsing_options.reset();
317
 
  lex->empty_field_list_on_rset= 0;
318
256
  lex->select_lex.select_number= 1;
319
257
  lex->length=0;
320
258
  lex->select_lex.in_sum_expr=0;
321
 
  lex->select_lex.ftfunc_list_alloc.empty();
322
 
  lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
323
259
  lex->select_lex.group_list.empty();
324
260
  lex->select_lex.order_list.empty();
325
261
  lex->sql_command= SQLCOM_END;
326
262
  lex->duplicates= DUP_ERROR;
327
263
  lex->ignore= 0;
328
 
  lex->proc_list.first= 0;
329
264
  lex->escape_used= false;
330
265
  lex->query_tables= 0;
331
266
  lex->reset_query_tables_list(false);
337
272
  lex->nest_level=0 ;
338
273
  lex->allow_sum_func= 0;
339
274
  lex->in_sum_func= NULL;
340
 
  /*
341
 
    ok, there must be a better solution for this, long-term
342
 
    I tried "memset" in the sql_yacc.yy code, but that for
343
 
    some reason made the values zero, even if they were set
344
 
  */
345
 
  lex->server_options.server_name= 0;
346
 
  lex->server_options.server_name_length= 0;
347
 
  lex->server_options.host= 0;
348
 
  lex->server_options.db= 0;
349
 
  lex->server_options.username= 0;
350
 
  lex->server_options.password= 0;
351
 
  lex->server_options.scheme= 0;
352
 
  lex->server_options.owner= 0;
353
 
  lex->server_options.port= -1;
354
275
 
355
276
  lex->is_lex_started= true;
356
 
  return;
357
277
}
358
278
 
359
279
void lex_end(LEX *lex)
366
286
    lex->yacc_yyvs= 0;
367
287
  }
368
288
 
369
 
  /* release used plugins */
370
 
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
371
 
                     lex->plugins.elements);
372
 
  reset_dynamic(&lex->plugins);
373
 
 
374
289
  delete lex->result;
375
290
  lex->result= 0;
376
 
 
377
 
  return;
378
291
}
379
292
 
380
293
 
381
294
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
382
295
{
 
296
  /* Plenty of memory for the largest lex symbol we have */
 
297
  char tok_upper[64];
383
298
  const char *tok= lip->get_tok_start();
 
299
  uint32_t tok_pos= 0;
 
300
  for (;tok_pos<len && tok_pos<63;tok_pos++)
 
301
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
 
302
  tok_upper[tok_pos]=0;
384
303
 
385
 
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
 
304
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
386
305
  if (symbol)
387
306
  {
388
307
    lip->yylval->symbol.symbol=symbol;
391
310
 
392
311
    return symbol->tok;
393
312
  }
 
313
 
394
314
  return 0;
395
315
}
396
316
 
397
 
/*
398
 
  Check if name is a keyword
399
 
 
400
 
  SYNOPSIS
401
 
    is_keyword()
402
 
    name      checked name (must not be empty)
403
 
    len       length of checked name
404
 
 
405
 
  RETURN VALUES
406
 
    0         name is a keyword
407
 
    1         name isn't a keyword
408
 
*/
409
 
 
410
 
bool is_keyword(const char *name, uint32_t len)
411
 
{
412
 
  assert(len != 0);
413
 
  return get_hash_symbol(name,len,0)!=0;
414
 
}
415
317
 
416
318
bool is_lex_native_function(const LEX_STRING *name)
417
319
{
418
320
  assert(name != NULL);
419
 
  return (get_hash_symbol(name->str, name->length, 1) != 0);
 
321
  return (lookup_symbol(name->str, name->length, 1) != 0);
420
322
}
421
323
 
422
324
/* make a copy of token before ptr and set yytoklen */
426
328
  LEX_STRING tmp;
427
329
  lip->yyUnget();                       // ptr points now after last token char
428
330
  tmp.length=lip->yytoklen=length;
429
 
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
 
331
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
430
332
 
431
333
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
432
334
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
434
336
  return tmp;
435
337
}
436
338
 
437
 
/* 
438
 
 todo: 
439
 
   There are no dangerous charsets in mysql for function 
440
 
   get_quoted_token yet. But it should be fixed in the 
 
339
/*
 
340
 todo:
 
341
   There are no dangerous charsets in mysql for function
 
342
   get_quoted_token yet. But it should be fixed in the
441
343
   future to operate multichar strings (like ucs2)
442
344
*/
443
345
 
450
352
  char *to;
451
353
  lip->yyUnget();                       // ptr points now after last token char
452
354
  tmp.length= lip->yytoklen=length;
453
 
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
 
355
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
454
356
  from= lip->get_tok_start() + skip;
455
357
  to= tmp.str;
456
358
  end= to+length;
480
382
{
481
383
  register unsigned char c,sep;
482
384
  uint32_t found_escape=0;
483
 
  const CHARSET_INFO * const cs= lip->m_thd->charset();
 
385
  const CHARSET_INFO * const cs= lip->m_session->charset();
484
386
 
485
387
  lip->tok_bitmap= 0;
486
388
  sep= lip->yyGetLast();                        // String should end with this
528
430
      end -= post_skip;
529
431
      assert(end >= str);
530
432
 
531
 
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
433
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
532
434
        return (char*) "";              // Sql_alloc has set error flag
533
435
 
534
436
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
536
438
 
537
439
      if (!found_escape)
538
440
      {
539
 
        lip->yytoklen=(uint) (end-str);
 
441
        lip->yytoklen=(uint32_t) (end-str);
540
442
        memcpy(start,str,lip->yytoklen);
541
443
        start[lip->yytoklen]=0;
542
444
      }
592
494
            *to++ = *str;
593
495
        }
594
496
        *to=0;
595
 
        lip->yytoklen=(uint) (to-start);
 
497
        lip->yytoklen=(uint32_t) (to-start);
596
498
      }
597
499
      return start;
598
500
    }
604
506
/*
605
507
** Calc type of integer; long integer, int64_t integer or real.
606
508
** Returns smallest type that match the string.
607
 
** When using unsigned long long values the result is converted to a real
 
509
** When using uint64_t values the result is converted to a real
608
510
** because else they will be unexpected sign changes because all calculation
609
511
** is done with int64_t or double.
610
512
*/
693
595
 
694
596
 
695
597
/*
696
 
  MYSQLlex remember the following states from the following MYSQLlex()
 
598
  DRIZZLElex remember the following states from the following DRIZZLElex()
697
599
 
698
600
  - MY_LEX_EOQ                  Found end of query
699
601
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
700
602
                                (which can't be followed by a signed number)
701
603
*/
702
604
 
703
 
int MYSQLlex(void *arg, void *yythd)
 
605
int DRIZZLElex(void *arg, void *yysession)
704
606
{
705
 
  THD *thd= (THD *)yythd;
706
 
  Lex_input_stream *lip= thd->m_lip;
 
607
  Session *session= (Session *)yysession;
 
608
  Lex_input_stream *lip= session->m_lip;
707
609
  YYSTYPE *yylval=(YYSTYPE*) arg;
708
610
  int token;
709
611
 
720
622
    return token;
721
623
  }
722
624
 
723
 
  token= lex_one_token(arg, yythd);
 
625
  token= lex_one_token(arg, yysession);
724
626
 
725
627
  switch(token) {
726
628
  case WITH:
731
633
      to transform the grammar into a LALR(1) grammar,
732
634
      which sql_yacc.yy can process.
733
635
    */
734
 
    token= lex_one_token(arg, yythd);
 
636
    token= lex_one_token(arg, yysession);
735
637
    if (token == ROLLUP_SYM)
736
638
    {
737
639
      return WITH_ROLLUP_SYM;
746
648
      lip->lookahead_token= token;
747
649
      return WITH;
748
650
    }
749
 
    break;
750
651
  default:
751
652
    break;
752
653
  }
754
655
  return token;
755
656
}
756
657
 
757
 
int lex_one_token(void *arg, void *yythd)
 
658
int lex_one_token(void *arg, void *yysession)
758
659
{
759
660
  register unsigned char c= 0; /* Just set to shutup GCC */
760
661
  bool comment_closed;
761
662
  int   tokval, result_state;
762
663
  unsigned int length;
763
664
  enum my_lex_states state;
764
 
  THD *thd= (THD *)yythd;
765
 
  Lex_input_stream *lip= thd->m_lip;
766
 
  LEX *lex= thd->lex;
 
665
  Session *session= (Session *)yysession;
 
666
  Lex_input_stream *lip= session->m_lip;
 
667
  LEX *lex= session->lex;
767
668
  YYSTYPE *yylval=(YYSTYPE*) arg;
768
 
  const CHARSET_INFO * const cs= thd->charset();
 
669
  const CHARSET_INFO * const cs= session->charset();
769
670
  unsigned char *state_map= cs->state_map;
770
671
  unsigned char *ident_map= cs->ident_map;
771
672
 
902
803
      }
903
804
      yylval->lex_str=get_token(lip, 0, length);
904
805
 
905
 
      /*
906
 
         Note: "SELECT _bla AS 'alias'"
907
 
         _bla should be considered as a IDENT if charset haven't been found.
908
 
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
909
 
         producing an error.
910
 
      */
911
 
 
912
 
      if (yylval->lex_str.str[0] == '_')
913
 
      {
914
 
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
915
 
                                                             MY_CS_PRIMARY, MYF(0));
916
 
        if (cs)
917
 
        {
918
 
          yylval->charset= cs;
919
 
          lip->m_underscore_cs= cs;
920
 
 
921
 
          lip->body_utf8_append(lip->m_cpp_text_start,
922
 
                                lip->get_cpp_tok_start() + length);
923
 
          return(UNDERSCORE_CHARSET);
924
 
        }
925
 
      }
926
 
 
927
806
      lip->body_utf8_append(lip->m_cpp_text_start);
928
807
 
929
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
808
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
930
809
                                    lip->m_cpp_text_end);
931
810
 
932
811
      return(result_state);                     // IDENT or IDENT_QUOTED
1029
908
 
1030
909
      lip->body_utf8_append(lip->m_cpp_text_start);
1031
910
 
1032
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
911
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1033
912
                                    lip->m_cpp_text_end);
1034
913
 
1035
914
      return(result_state);
1070
949
 
1071
950
      lip->body_utf8_append(lip->m_cpp_text_start);
1072
951
 
1073
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
952
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1074
953
                                    lip->m_cpp_text_end);
1075
954
 
1076
955
      return(IDENT_QUOTED);
1184
1063
 
1185
1064
      lip->body_utf8_append(lip->m_cpp_text_start);
1186
1065
 
1187
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1066
      lip->body_utf8_append_literal(session, &yylval->lex_str,
1188
1067
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1189
1068
        lip->m_cpp_text_end);
1190
1069
 
1220
1099
 
1221
1100
        /*
1222
1101
          The special comment format is very strict:
1223
 
          '/' '*' '!', followed by exactly
1224
 
          1 digit (major), 2 digits (minor), then 2 digits (dot).
1225
 
          32302 -> 3.23.02
1226
 
          50032 -> 5.0.32
1227
 
          50114 -> 5.1.14
 
1102
          '/' '*' '!', followed by digits ended by a non-digit.
 
1103
          There must be at least 5 digits for it to count
1228
1104
        */
1229
 
        char version_str[6];
1230
 
        version_str[0]= lip->yyPeekn(0);
1231
 
        version_str[1]= lip->yyPeekn(1);
1232
 
        version_str[2]= lip->yyPeekn(2);
1233
 
        version_str[3]= lip->yyPeekn(3);
1234
 
        version_str[4]= lip->yyPeekn(4);
1235
 
        version_str[5]= 0;
1236
 
        if (  my_isdigit(cs, version_str[0])
1237
 
           && my_isdigit(cs, version_str[1])
1238
 
           && my_isdigit(cs, version_str[2])
1239
 
           && my_isdigit(cs, version_str[3])
1240
 
           && my_isdigit(cs, version_str[4])
1241
 
           )
1242
 
        {
1243
 
          ulong version;
1244
 
          version=strtol(version_str, NULL, 10);
 
1105
        const int MAX_VERSION_SIZE= 16;
 
1106
        char version_str[MAX_VERSION_SIZE];
 
1107
 
 
1108
        int pos= 0;
 
1109
        do
 
1110
        {
 
1111
          version_str[pos]= lip->yyPeekn(pos);
 
1112
          pos++;
 
1113
        } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
 
1114
        version_str[pos]= 0;
 
1115
 
 
1116
        /* To keep some semblance of compatibility, we impose a 5 digit floor */
 
1117
        if (pos > 4)
 
1118
        {
 
1119
          uint64_t version;
 
1120
          version=strtoll(version_str, NULL, 10);
1245
1121
 
1246
1122
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1247
 
          lip->yySkipn(5);
 
1123
          lip->yySkipn(pos-1);
1248
1124
 
1249
1125
          if (version <= DRIZZLE_VERSION_ID)
1250
1126
          {
1326
1202
    case MY_LEX_SEMICOLON:                      // optional line terminator
1327
1203
      if (lip->yyPeek())
1328
1204
      {
1329
 
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1205
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
1330
1206
        {
1331
1207
          lip->found_semicolon= lip->get_ptr();
1332
 
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1208
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
1333
1209
          lip->next_state= MY_LEX_END;
1334
1210
          lip->set_echo(true);
1335
1211
          return (END_OF_INPUT);
1356
1232
      break;
1357
1233
    case MY_LEX_END:
1358
1234
      lip->next_state=MY_LEX_END;
1359
 
      return(0);                        // We found end of input last time
1360
 
      
 
1235
      return false;                     // We found end of input last time
 
1236
 
1361
1237
      /* Actually real shouldn't start with . but allow them anyhow */
1362
1238
    case MY_LEX_REAL_OR_POINT:
1363
1239
      if (my_isdigit(cs,lip->yyPeek()))
1424
1300
 
1425
1301
      lip->body_utf8_append(lip->m_cpp_text_start);
1426
1302
 
1427
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1303
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1428
1304
                                    lip->m_cpp_text_end);
1429
1305
 
1430
1306
      return(result_state);
1442
1318
  statements and stored procedures and is compensated by always
1443
1319
  supplying a copy of Alter_info to these functions.
1444
1320
 
1445
 
  @return You need to use check the error in THD for out
 
1321
  @return You need to use check the error in Session for out
1446
1322
  of memory condition after calling this function.
1447
1323
*/
1448
1324
 
1501
1377
 
1502
1378
 
1503
1379
/*
1504
 
  st_select_lex structures initialisations
 
1380
  Select_Lex structures initialisations
1505
1381
*/
1506
1382
 
1507
 
void st_select_lex_node::init_query()
 
1383
void Select_Lex_Node::init_query()
1508
1384
{
1509
1385
  options= 0;
1510
1386
  linkage= UNSPECIFIED_TYPE;
1512
1388
  uncacheable= 0;
1513
1389
}
1514
1390
 
1515
 
void st_select_lex_node::init_select()
 
1391
void Select_Lex_Node::init_select()
1516
1392
{
1517
1393
}
1518
1394
 
1519
 
void st_select_lex_unit::init_query()
 
1395
void Select_Lex_Unit::init_query()
1520
1396
{
1521
 
  st_select_lex_node::init_query();
 
1397
  Select_Lex_Node::init_query();
1522
1398
  linkage= GLOBAL_OPTIONS_TYPE;
1523
1399
  global_parameters= first_select();
1524
1400
  select_limit_cnt= HA_POS_ERROR;
1535
1411
  found_rows_for_union= 0;
1536
1412
}
1537
1413
 
1538
 
void st_select_lex::init_query()
 
1414
void Select_Lex::init_query()
1539
1415
{
1540
 
  st_select_lex_node::init_query();
 
1416
  Select_Lex_Node::init_query();
1541
1417
  table_list.empty();
1542
1418
  top_join_list.empty();
1543
1419
  join_list= &top_join_list;
1561
1437
  parent_lex->push_context(&context);
1562
1438
  cond_count= between_count= with_wild= 0;
1563
1439
  max_equal_elems= 0;
1564
 
  conds_processed_with_permanent_arena= 0;
1565
1440
  ref_pointer_array= 0;
1566
1441
  select_n_where_fields= 0;
1567
1442
  select_n_having_items= 0;
1573
1448
  link_next= 0;
1574
1449
}
1575
1450
 
1576
 
void st_select_lex::init_select()
 
1451
void Select_Lex::init_select()
1577
1452
{
1578
 
  st_select_lex_node::init_select();
 
1453
  Select_Lex_Node::init_select();
1579
1454
  sj_nests.empty();
1580
1455
  group_list.empty();
1581
1456
  type= db= 0;
1585
1460
  options= 0;
1586
1461
  braces= 0;
1587
1462
  interval_list.empty();
1588
 
  ftfunc_list_alloc.empty();
1589
1463
  inner_sum_func_list= 0;
1590
 
  ftfunc_list= &ftfunc_list_alloc;
1591
1464
  linkage= UNSPECIFIED_TYPE;
1592
1465
  order_list.elements= 0;
1593
1466
  order_list.first= 0;
1605
1478
}
1606
1479
 
1607
1480
/*
1608
 
  st_select_lex structures linking
 
1481
  Select_Lex structures linking
1609
1482
*/
1610
1483
 
1611
1484
/* include on level down */
1612
 
void st_select_lex_node::include_down(st_select_lex_node *upper)
 
1485
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1613
1486
{
1614
1487
  if ((next= upper->slave))
1615
1488
    next->prev= &next;
1623
1496
  include on level down (but do not link)
1624
1497
 
1625
1498
  SYNOPSYS
1626
 
    st_select_lex_node::include_standalone()
 
1499
    Select_Lex_Node::include_standalone()
1627
1500
    upper - reference on node underr which this node should be included
1628
1501
    ref - references on reference on this node
1629
1502
*/
1630
 
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
1631
 
                                            st_select_lex_node **ref)
 
1503
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
 
1504
                                            Select_Lex_Node **ref)
1632
1505
{
1633
1506
  next= 0;
1634
1507
  prev= ref;
1637
1510
}
1638
1511
 
1639
1512
/* include neighbour (on same level) */
1640
 
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
 
1513
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1641
1514
{
1642
1515
  if ((next= before->next))
1643
1516
    next->prev= &next;
1647
1520
  slave= 0;
1648
1521
}
1649
1522
 
1650
 
/* including in global SELECT_LEX list */
1651
 
void st_select_lex_node::include_global(st_select_lex_node **plink)
 
1523
/* including in global Select_Lex list */
 
1524
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1652
1525
{
1653
1526
  if ((link_next= *plink))
1654
1527
    link_next->link_prev= &link_next;
1657
1530
}
1658
1531
 
1659
1532
//excluding from global list (internal function)
1660
 
void st_select_lex_node::fast_exclude()
 
1533
void Select_Lex_Node::fast_exclude()
1661
1534
{
1662
1535
  if (link_prev)
1663
1536
  {
1667
1540
  // Remove slave structure
1668
1541
  for (; slave; slave= slave->next)
1669
1542
    slave->fast_exclude();
1670
 
  
 
1543
 
1671
1544
}
1672
1545
 
1673
1546
/*
1674
1547
  excluding select_lex structure (except first (first select can't be
1675
1548
  deleted, because it is most upper select))
1676
1549
*/
1677
 
void st_select_lex_node::exclude()
 
1550
void Select_Lex_Node::exclude()
1678
1551
{
1679
1552
  //exclude from global list
1680
1553
  fast_exclude();
1681
1554
  //exclude from other structures
1682
1555
  if ((*prev= next))
1683
1556
    next->prev= prev;
1684
 
  /* 
1685
 
     We do not need following statements, because prev pointer of first 
 
1557
  /*
 
1558
     We do not need following statements, because prev pointer of first
1686
1559
     list element point to master->slave
1687
1560
     if (master->slave == this)
1688
1561
       master->slave= next;
1694
1567
  Exclude level of current unit from tree of SELECTs
1695
1568
 
1696
1569
  SYNOPSYS
1697
 
    st_select_lex_unit::exclude_level()
 
1570
    Select_Lex_Unit::exclude_level()
1698
1571
 
1699
1572
  NOTE: units which belong to current will be brought up on level of
1700
 
  currernt unit 
 
1573
  currernt unit
1701
1574
*/
1702
 
void st_select_lex_unit::exclude_level()
 
1575
void Select_Lex_Unit::exclude_level()
1703
1576
{
1704
 
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
1705
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1577
  Select_Lex_Unit *units= 0, **units_last= &units;
 
1578
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1706
1579
  {
1707
1580
    // unlink current level from global SELECTs list
1708
1581
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1709
1582
      sl->link_next->link_prev= sl->link_prev;
1710
1583
 
1711
1584
    // bring up underlay levels
1712
 
    SELECT_LEX_UNIT **last= 0;
1713
 
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1585
    Select_Lex_Unit **last= 0;
 
1586
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1714
1587
    {
1715
1588
      u->master= master;
1716
 
      last= (SELECT_LEX_UNIT**)&(u->next);
 
1589
      last= (Select_Lex_Unit**)&(u->next);
1717
1590
    }
1718
1591
    if (last)
1719
1592
    {
1725
1598
  {
1726
1599
    // include brought up levels in place of current
1727
1600
    (*prev)= units;
1728
 
    (*units_last)= (SELECT_LEX_UNIT*)next;
 
1601
    (*units_last)= (Select_Lex_Unit*)next;
1729
1602
    if (next)
1730
 
      next->prev= (SELECT_LEX_NODE**)units_last;
 
1603
      next->prev= (Select_Lex_Node**)units_last;
1731
1604
    units->prev= prev;
1732
1605
  }
1733
1606
  else
1744
1617
  Exclude subtree of current unit from tree of SELECTs
1745
1618
 
1746
1619
  SYNOPSYS
1747
 
    st_select_lex_unit::exclude_tree()
 
1620
    Select_Lex_Unit::exclude_tree()
1748
1621
*/
1749
 
void st_select_lex_unit::exclude_tree()
 
1622
void Select_Lex_Unit::exclude_tree()
1750
1623
{
1751
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1624
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1752
1625
  {
1753
1626
    // unlink current level from global SELECTs list
1754
1627
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1755
1628
      sl->link_next->link_prev= sl->link_prev;
1756
1629
 
1757
1630
    // unlink underlay levels
1758
 
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1631
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1759
1632
    {
1760
1633
      u->exclude_level();
1761
1634
    }
1768
1641
 
1769
1642
 
1770
1643
/*
1771
 
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1644
  Select_Lex_Node::mark_as_dependent mark all Select_Lex struct from
1772
1645
  this to 'last' as dependent
1773
1646
 
1774
1647
  SYNOPSIS
1775
 
    last - pointer to last st_select_lex struct, before wich all 
1776
 
           st_select_lex have to be marked as dependent
 
1648
    last - pointer to last Select_Lex struct, before wich all
 
1649
           Select_Lex have to be marked as dependent
1777
1650
 
1778
1651
  NOTE
1779
 
    'last' should be reachable from this st_select_lex_node
 
1652
    'last' should be reachable from this Select_Lex_Node
1780
1653
*/
1781
1654
 
1782
 
void st_select_lex::mark_as_dependent(st_select_lex *last)
 
1655
void Select_Lex::mark_as_dependent(Select_Lex *last)
1783
1656
{
1784
1657
  /*
1785
1658
    Mark all selects from resolved to 1 before select where was
1786
1659
    found table as depended (of select where was found table)
1787
1660
  */
1788
 
  for (SELECT_LEX *s= this;
 
1661
  for (Select_Lex *s= this;
1789
1662
       s && s != last;
1790
1663
       s= s->outer_select())
1791
1664
  {
1794
1667
      // Select is dependent of outer select
1795
1668
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1796
1669
                       UNCACHEABLE_DEPENDENT;
1797
 
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1670
      Select_Lex_Unit *munit= s->master_unit();
1798
1671
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1799
1672
                       UNCACHEABLE_DEPENDENT;
1800
 
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1673
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1801
1674
      {
1802
1675
        if (sl != s &&
1803
1676
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1811
1684
  }
1812
1685
}
1813
1686
 
1814
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
1815
 
{ return 1; }
1816
 
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
1817
 
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
1818
 
TableList* st_select_lex_node::get_table_list()     { return 0; }
1819
 
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)))
 
1687
bool Select_Lex_Node::set_braces(bool)
 
1688
{ return true; }
 
1689
 
 
1690
bool Select_Lex_Node::inc_in_sum_expr()
 
1691
{ return true; }
 
1692
 
 
1693
uint32_t Select_Lex_Node::get_in_sum_expr() 
 
1694
{ return 0; }
 
1695
 
 
1696
TableList* Select_Lex_Node::get_table_list()
 
1697
{ return NULL; }
 
1698
 
 
1699
List<Item>* Select_Lex_Node::get_item_list()
 
1700
{ return NULL; }
 
1701
 
 
1702
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1703
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1827
1704
{
1828
1705
  return 0;
1829
1706
}
1830
 
uint32_t st_select_lex_node::get_table_join_options()
 
1707
 
 
1708
uint32_t Select_Lex_Node::get_table_join_options()
1831
1709
{
1832
1710
  return 0;
1833
1711
}
1835
1713
/*
1836
1714
  prohibit using LIMIT clause
1837
1715
*/
1838
 
bool st_select_lex::test_limit()
 
1716
bool Select_Lex::test_limit()
1839
1717
{
1840
1718
  if (select_limit != 0)
1841
1719
  {
1842
1720
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1843
1721
             "LIMIT & IN/ALL/ANY/SOME subquery");
1844
 
    return(1);
 
1722
    return true;
1845
1723
  }
1846
 
  return(0);
 
1724
  return false;
1847
1725
}
1848
1726
 
1849
1727
 
1850
 
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1728
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1851
1729
{
1852
1730
    return this;
1853
1731
}
1854
1732
 
1855
1733
 
1856
 
st_select_lex* st_select_lex_unit::outer_select()
1857
 
{
1858
 
  return (st_select_lex*) master;
1859
 
}
1860
 
 
1861
 
 
1862
 
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
1863
 
{
1864
 
  return add_to_list(thd, order_list, item, asc);
1865
 
}
1866
 
 
1867
 
 
1868
 
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
1869
 
                                     Item *item)
 
1734
Select_Lex* Select_Lex_Unit::outer_select()
 
1735
{
 
1736
  return (Select_Lex*) master;
 
1737
}
 
1738
 
 
1739
 
 
1740
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
 
1741
{
 
1742
  return add_to_list(session, order_list, item, asc);
 
1743
}
 
1744
 
 
1745
 
 
1746
bool Select_Lex::add_item_to_list(Session *, Item *item)
1870
1747
{
1871
1748
  return(item_list.push_back(item));
1872
1749
}
1873
1750
 
1874
1751
 
1875
 
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
1876
 
{
1877
 
  return add_to_list(thd, group_list, item, asc);
1878
 
}
1879
 
 
1880
 
 
1881
 
st_select_lex_unit* st_select_lex::master_unit()
1882
 
{
1883
 
  return (st_select_lex_unit*) master;
1884
 
}
1885
 
 
1886
 
 
1887
 
st_select_lex* st_select_lex::outer_select()
1888
 
{
1889
 
  return (st_select_lex*) master->get_master();
1890
 
}
1891
 
 
1892
 
 
1893
 
bool st_select_lex::set_braces(bool value)
 
1752
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
 
1753
{
 
1754
  return add_to_list(session, group_list, item, asc);
 
1755
}
 
1756
 
 
1757
 
 
1758
Select_Lex_Unit* Select_Lex::master_unit()
 
1759
{
 
1760
  return (Select_Lex_Unit*) master;
 
1761
}
 
1762
 
 
1763
 
 
1764
Select_Lex* Select_Lex::outer_select()
 
1765
{
 
1766
  return (Select_Lex*) master->get_master();
 
1767
}
 
1768
 
 
1769
 
 
1770
bool Select_Lex::set_braces(bool value)
1894
1771
{
1895
1772
  braces= value;
1896
 
  return 0; 
 
1773
 
 
1774
  return false;
1897
1775
}
1898
1776
 
1899
1777
 
1900
 
bool st_select_lex::inc_in_sum_expr()
 
1778
bool Select_Lex::inc_in_sum_expr()
1901
1779
{
1902
1780
  in_sum_expr++;
1903
 
  return 0;
 
1781
 
 
1782
  return false;
1904
1783
}
1905
1784
 
1906
1785
 
1907
 
uint32_t st_select_lex::get_in_sum_expr()
 
1786
uint32_t Select_Lex::get_in_sum_expr()
1908
1787
{
1909
1788
  return in_sum_expr;
1910
1789
}
1911
1790
 
1912
1791
 
1913
 
TableList* st_select_lex::get_table_list()
 
1792
TableList* Select_Lex::get_table_list()
1914
1793
{
1915
1794
  return (TableList*) table_list.first;
1916
1795
}
1917
1796
 
1918
 
List<Item>* st_select_lex::get_item_list()
 
1797
List<Item>* Select_Lex::get_item_list()
1919
1798
{
1920
1799
  return &item_list;
1921
1800
}
1922
1801
 
1923
 
uint32_t st_select_lex::get_table_join_options()
 
1802
uint32_t Select_Lex::get_table_join_options()
1924
1803
{
1925
1804
  return table_join_options;
1926
1805
}
1927
1806
 
1928
1807
 
1929
 
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
 
1808
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1930
1809
{
1931
1810
  if (ref_pointer_array)
1932
 
    return 0;
 
1811
    return false;
1933
1812
 
1934
1813
  return (ref_pointer_array=
1935
 
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
 
1814
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1936
1815
                                                 item_list.elements +
1937
1816
                                                 select_n_having_items +
1938
1817
                                                 select_n_where_fields +
1940
1819
}
1941
1820
 
1942
1821
 
1943
 
void st_select_lex_unit::print(String *str, enum_query_type query_type)
 
1822
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
1944
1823
{
1945
1824
  bool union_all= !union_distinct;
1946
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1825
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1947
1826
  {
1948
1827
    if (sl != first_select())
1949
1828
    {
1955
1834
    }
1956
1835
    if (sl->braces)
1957
1836
      str->append('(');
1958
 
    sl->print(thd, str, query_type);
 
1837
    sl->print(session, str, query_type);
1959
1838
    if (sl->braces)
1960
1839
      str->append(')');
1961
1840
  }
1969
1848
        (order_st *) fake_select_lex->order_list.first,
1970
1849
        query_type);
1971
1850
    }
1972
 
    fake_select_lex->print_limit(thd, str, query_type);
 
1851
    fake_select_lex->print_limit(session, str, query_type);
1973
1852
  }
1974
1853
}
1975
1854
 
1976
1855
 
1977
 
void st_select_lex::print_order(String *str,
 
1856
void Select_Lex::print_order(String *str,
1978
1857
                                order_st *order,
1979
1858
                                enum_query_type query_type)
1980
1859
{
1994
1873
      str->append(',');
1995
1874
  }
1996
1875
}
1997
 
 
1998
 
 
1999
 
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
2000
 
                                String *str,
 
1876
 
 
1877
 
 
1878
void Select_Lex::print_limit(Session *, String *str,
2001
1879
                                enum_query_type query_type)
2002
1880
{
2003
 
  SELECT_LEX_UNIT *unit= master_unit();
 
1881
  Select_Lex_Unit *unit= master_unit();
2004
1882
  Item_subselect *item= unit->item;
2005
1883
 
2006
1884
  if (item && unit->global_parameters == this)
2037
1915
}
2038
1916
 
2039
1917
/**
2040
 
  @brief Restore the LEX and THD in case of a parse error.
 
1918
  @brief Restore the LEX and Session in case of a parse error.
2041
1919
 
2042
1920
  This is a clean up call that is invoked by the Bison generated
2043
 
  parser before returning an error from MYSQLparse. If your
 
1921
  parser before returning an error from DRIZZLEparse. If your
2044
1922
  semantic actions manipulate with the global thread state (which
2045
1923
  is a very bad practice and should not normally be employed) and
2046
1924
  need a clean-up in case of error, and you can not use %destructor
2048
1926
  to implement the clean up.
2049
1927
*/
2050
1928
 
2051
 
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
 
1929
void LEX::cleanup_lex_after_parse_error(Session *)
2052
1930
{
2053
1931
}
2054
1932
 
2085
1963
  query_tables= 0;
2086
1964
  query_tables_last= &query_tables;
2087
1965
  query_tables_own_last= 0;
2088
 
  if (init)
2089
 
  {
2090
 
    /*
2091
 
      We delay real initialization of hash (and therefore related
2092
 
      memory allocation) until first insertion into this hash.
2093
 
    */
2094
 
    hash_clear(&sroutines);
2095
 
  }
2096
 
  else if (sroutines.records)
2097
 
  {
2098
 
    /* Non-zero sroutines.records means that hash was initialized. */
2099
 
    my_hash_reset(&sroutines);
2100
 
  }
2101
 
  sroutines_list.empty();
2102
 
  sroutines_list_own_last= sroutines_list.next;
2103
 
  sroutines_list_own_elements= 0;
2104
 
  binlog_stmt_flags= 0;
2105
 
}
2106
 
 
2107
 
 
2108
 
/*
2109
 
  Destroy Query_tables_list object with freeing all resources used by it.
2110
 
 
2111
 
  SYNOPSIS
2112
 
    destroy_query_tables_list()
2113
 
*/
2114
 
 
2115
 
void Query_tables_list::destroy_query_tables_list()
2116
 
{
2117
 
  hash_free(&sroutines);
2118
1966
}
2119
1967
 
2120
1968
 
2122
1970
  Initialize LEX object.
2123
1971
 
2124
1972
  SYNOPSIS
2125
 
    st_lex::st_lex()
 
1973
    LEX::LEX()
2126
1974
 
2127
1975
  NOTE
2128
1976
    LEX object initialized with this constructor can be used as part of
2129
 
    THD object for which one can safely call open_tables(), lock_tables()
 
1977
    Session object for which one can safely call open_tables(), lock_tables()
2130
1978
    and close_thread_tables() functions. But it is not yet ready for
2131
1979
    statement parsing. On should use lex_start() function to prepare LEX
2132
1980
    for this.
2133
1981
*/
2134
1982
 
2135
 
st_lex::st_lex()
 
1983
LEX::LEX()
2136
1984
  :result(0), yacc_yyss(0), yacc_yyvs(0),
2137
1985
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2138
1986
{
2139
 
 
2140
 
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2141
 
                         plugins_static_buffer,
2142
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
2143
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2144
1987
  reset_query_tables_list(true);
2145
1988
}
2146
1989
 
2147
 
 
2148
 
/*
2149
 
  Check whether the merging algorithm can be used on this VIEW
2150
 
 
2151
 
  SYNOPSIS
2152
 
    st_lex::can_be_merged()
2153
 
 
2154
 
  DESCRIPTION
2155
 
    We can apply merge algorithm if it is single SELECT view  with
2156
 
    subqueries only in WHERE clause (we do not count SELECTs of underlying
2157
 
    views, and second level subqueries) and we have not grpouping, ordering,
2158
 
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2159
 
    several underlying tables.
2160
 
 
2161
 
  RETURN
2162
 
    false - only temporary table algorithm can be used
2163
 
    true  - merge algorithm can be used
2164
 
*/
2165
 
 
2166
 
bool st_lex::can_be_merged()
2167
 
{
2168
 
  // TODO: do not forget implement case when select_lex.table_list.elements==0
2169
 
 
2170
 
  /* find non VIEW subqueries/unions */
2171
 
  bool selects_allow_merge= select_lex.next_select() == 0;
2172
 
  if (selects_allow_merge)
2173
 
  {
2174
 
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2175
 
         tmp_unit;
2176
 
         tmp_unit= tmp_unit->next_unit())
2177
 
    {
2178
 
      if (tmp_unit->first_select()->parent_lex == this &&
2179
 
          (tmp_unit->item == 0 ||
2180
 
           (tmp_unit->item->place() != IN_WHERE &&
2181
 
            tmp_unit->item->place() != IN_ON)))
2182
 
      {
2183
 
        selects_allow_merge= 0;
2184
 
        break;
2185
 
      }
2186
 
    }
2187
 
  }
2188
 
 
2189
 
  return (selects_allow_merge &&
2190
 
          select_lex.group_list.elements == 0 &&
2191
 
          select_lex.having == 0 &&
2192
 
          select_lex.with_sum_func == 0 &&
2193
 
          select_lex.table_list.elements >= 1 &&
2194
 
          !(select_lex.options & SELECT_DISTINCT) &&
2195
 
          select_lex.select_limit == 0);
2196
 
}
2197
 
 
2198
 
 
2199
 
/*
2200
 
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
2201
 
 
2202
 
  SYNOPSIS
2203
 
    st_lex::can_use_merged()
2204
 
 
2205
 
  DESCRIPTION
2206
 
    Only listed here commands can use merge algorithm in top level
2207
 
    SELECT_LEX (for subqueries will be used merge algorithm if
2208
 
    st_lex::can_not_use_merged() is not true).
2209
 
 
2210
 
  RETURN
2211
 
    false - command can't use merged VIEWs
2212
 
    true  - VIEWs with MERGE algorithms can be used
2213
 
*/
2214
 
 
2215
 
bool st_lex::can_use_merged()
2216
 
{
2217
 
  switch (sql_command)
2218
 
  {
2219
 
  case SQLCOM_SELECT:
2220
 
  case SQLCOM_CREATE_TABLE:
2221
 
  case SQLCOM_UPDATE:
2222
 
  case SQLCOM_UPDATE_MULTI:
2223
 
  case SQLCOM_DELETE:
2224
 
  case SQLCOM_DELETE_MULTI:
2225
 
  case SQLCOM_INSERT:
2226
 
  case SQLCOM_INSERT_SELECT:
2227
 
  case SQLCOM_REPLACE:
2228
 
  case SQLCOM_REPLACE_SELECT:
2229
 
  case SQLCOM_LOAD:
2230
 
    return true;
2231
 
  default:
2232
 
    return false;
2233
 
  }
2234
 
}
2235
 
 
2236
 
/*
2237
 
  Check if command can't use merged views in any part of command
2238
 
 
2239
 
  SYNOPSIS
2240
 
    st_lex::can_not_use_merged()
2241
 
 
2242
 
  DESCRIPTION
2243
 
    Temporary table algorithm will be used on all SELECT levels for queries
2244
 
    listed here (see also st_lex::can_use_merged()).
2245
 
 
2246
 
  RETURN
2247
 
    false - command can't use merged VIEWs
2248
 
    true  - VIEWs with MERGE algorithms can be used
2249
 
*/
2250
 
 
2251
 
bool st_lex::can_not_use_merged()
2252
 
{
2253
 
  switch (sql_command)
2254
 
  {
2255
 
  /*
2256
 
    SQLCOM_SHOW_FIELDS is necessary to make 
2257
 
    information schema tables working correctly with views.
2258
 
    see get_schema_tables_result function
2259
 
  */
2260
 
  case SQLCOM_SHOW_FIELDS:
2261
 
    return true;
2262
 
  default:
2263
 
    return false;
2264
 
  }
2265
 
}
2266
 
 
2267
1990
/*
2268
1991
  Detect that we need only table structure of derived table/view
2269
1992
 
2275
1998
    false no, we need data
2276
1999
*/
2277
2000
 
2278
 
bool st_lex::only_view_structure()
 
2001
bool LEX::only_view_structure()
2279
2002
{
2280
2003
  switch (sql_command) {
2281
2004
  case SQLCOM_SHOW_CREATE:
2300
2023
*/
2301
2024
 
2302
2025
 
2303
 
bool st_lex::need_correct_ident()
 
2026
bool LEX::need_correct_ident()
2304
2027
{
2305
2028
  switch(sql_command)
2306
2029
  {
2319
2042
  and will initialize the destination with the default
2320
2043
  database of the stored routine, rather than the default
2321
2044
  database of the connection it is parsed in.
2322
 
  E.g. if one has no current database selected, or current database 
 
2045
  E.g. if one has no current database selected, or current database
2323
2046
  set to 'bar' and then issues:
2324
2047
 
2325
2048
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2333
2056
*/
2334
2057
 
2335
2058
bool
2336
 
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
 
2059
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2337
2060
{
2338
 
  return thd->copy_db_to(p_db, p_db_length);
 
2061
  return session->copy_db_to(p_db, p_db_length);
2339
2062
}
2340
2063
 
2341
2064
/*
2342
2065
  initialize limit counters
2343
2066
 
2344
2067
  SYNOPSIS
2345
 
    st_select_lex_unit::set_limit()
2346
 
    values      - SELECT_LEX with initial values for counters
 
2068
    Select_Lex_Unit::set_limit()
 
2069
    values      - Select_Lex with initial values for counters
2347
2070
*/
2348
2071
 
2349
 
void st_select_lex_unit::set_limit(st_select_lex *sl)
 
2072
void Select_Lex_Unit::set_limit(Select_Lex *sl)
2350
2073
{
2351
2074
  ha_rows select_limit_val;
2352
2075
  uint64_t val;
2353
2076
 
2354
2077
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2355
2078
  select_limit_val= (ha_rows)val;
2356
 
#ifndef BIG_TABLES
2357
 
  /* 
 
2079
  /*
2358
2080
    Check for overflow : ha_rows can be smaller then uint64_t if
2359
2081
    BIG_TABLES is off.
2360
2082
    */
2361
2083
  if (val != (uint64_t)select_limit_val)
2362
2084
    select_limit_val= HA_POS_ERROR;
2363
 
#endif
2364
2085
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2365
2086
                                                 0UL);
2366
2087
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2387
2108
      In this case link_to_local is set.
2388
2109
 
2389
2110
*/
2390
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2111
TableList *LEX::unlink_first_table(bool *link_to_local)
2391
2112
{
2392
2113
  TableList *first;
2393
2114
  if ((first= query_tables))
2406
2127
    */
2407
2128
    if ((*link_to_local= test(select_lex.table_list.first)))
2408
2129
    {
2409
 
      select_lex.context.table_list= 
 
2130
      select_lex.context.table_list=
2410
2131
        select_lex.context.first_name_resolution_table= first->next_local;
2411
2132
      select_lex.table_list.first= (unsigned char*) (first->next_local);
2412
2133
      select_lex.table_list.elements--; //safety
2427
2148
  table list
2428
2149
 
2429
2150
  SYNOPSYS
2430
 
     st_lex::first_lists_tables_same()
 
2151
     LEX::first_lists_tables_same()
2431
2152
 
2432
2153
  NOTES
2433
2154
    In many cases (for example, usual INSERT/DELETE/...) the first table of
2434
 
    main SELECT_LEX have special meaning => check that it is the first table
 
2155
    main Select_Lex have special meaning => check that it is the first table
2435
2156
    in global list and re-link to be first in the global list if it is
2436
2157
    necessary.  We need such re-linking only for queries with sub-queries in
2437
2158
    the select list, as only in this case tables of sub-queries will go to
2438
2159
    the global list first.
2439
2160
*/
2440
2161
 
2441
 
void st_lex::first_lists_tables_same()
 
2162
void LEX::first_lists_tables_same()
2442
2163
{
2443
2164
  TableList *first_table= (TableList*) select_lex.table_list.first;
2444
2165
  if (query_tables != first_table && first_table != 0)
2474
2195
    global list
2475
2196
*/
2476
2197
 
2477
 
void st_lex::link_first_table_back(TableList *first,
 
2198
void LEX::link_first_table_back(TableList *first,
2478
2199
                                   bool link_to_local)
2479
2200
{
2480
2201
  if (first)
2501
2222
  cleanup lex for case when we open table by table for processing
2502
2223
 
2503
2224
  SYNOPSIS
2504
 
    st_lex::cleanup_after_one_table_open()
 
2225
    LEX::cleanup_after_one_table_open()
2505
2226
 
2506
2227
  NOTE
2507
2228
    This method is mostly responsible for cleaning up of selects lists and
2509
2230
    to call Query_tables_list::reset_query_tables_list(false).
2510
2231
*/
2511
2232
 
2512
 
void st_lex::cleanup_after_one_table_open()
 
2233
void LEX::cleanup_after_one_table_open()
2513
2234
{
2514
2235
  /*
2515
 
    thd->lex->derived_tables & additional units may be set if we open
2516
 
    a view. It is necessary to clear thd->lex->derived_tables flag
 
2236
    session->lex->derived_tables & additional units may be set if we open
 
2237
    a view. It is necessary to clear session->lex->derived_tables flag
2517
2238
    to prevent processing of derived tables during next open_and_lock_tables
2518
2239
    if next table is a real table and cleanup & remove underlying units
2519
 
    NOTE: all units will be connected to thd->lex->select_lex, because we
 
2240
    NOTE: all units will be connected to session->lex->select_lex, because we
2520
2241
    have not UNION on most upper level.
2521
2242
    */
2522
2243
  if (all_selects_list != &select_lex)
2523
2244
  {
2524
2245
    derived_tables= 0;
2525
2246
    /* cleunup underlying units (units of VIEW) */
2526
 
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
 
2247
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
2527
2248
         un;
2528
2249
         un= un->next_unit())
2529
2250
      un->cleanup();
2536
2257
 
2537
2258
 
2538
2259
/*
2539
 
  Save current state of Query_tables_list for this LEX, and prepare it
2540
 
  for processing of new statemnt.
2541
 
 
2542
 
  SYNOPSIS
2543
 
    reset_n_backup_query_tables_list()
2544
 
      backup  Pointer to Query_tables_list instance to be used for backup
2545
 
*/
2546
 
 
2547
 
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2548
 
{
2549
 
}
2550
 
 
2551
 
 
2552
 
/*
2553
 
  Restore state of Query_tables_list for this LEX from backup.
2554
 
 
2555
 
  SYNOPSIS
2556
 
    restore_backup_query_tables_list()
2557
 
      backup  Pointer to Query_tables_list instance used for backup
2558
 
*/
2559
 
 
2560
 
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2561
 
{
2562
 
}
2563
 
 
2564
 
 
2565
 
/*
2566
 
  Checks for usage of routines and/or tables in a parsed statement
2567
 
 
2568
 
  SYNOPSIS
2569
 
    st_lex:table_or_sp_used()
2570
 
 
2571
 
  RETURN
2572
 
    false  No routines and tables used
2573
 
    true   Either or both routines and tables are used.
2574
 
*/
2575
 
 
2576
 
bool st_lex::table_or_sp_used()
2577
 
{
2578
 
  if (sroutines.records || query_tables)
2579
 
    return(true);
2580
 
 
2581
 
  return(false);
2582
 
}
2583
 
 
2584
 
 
2585
 
/*
2586
 
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2587
 
 
2588
 
  SYNOPSIS
2589
 
    fix_prepare_info_in_table_list()
2590
 
      thd  Thread handle
2591
 
      tbl  List of tables to process
2592
 
 
2593
 
  DESCRIPTION
2594
 
    Perform end-end-of prepare fixup for list of tables, if any of the tables
2595
 
    is a merge-algorithm VIEW, recursively fix up its underlying tables as
2596
 
    well.
2597
 
 
2598
 
*/
2599
 
 
2600
 
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
2601
 
{
2602
 
  for (; tbl; tbl= tbl->next_local)
2603
 
  {
2604
 
    if (tbl->on_expr)
2605
 
    {
2606
 
      tbl->prep_on_expr= tbl->on_expr;
2607
 
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
2608
 
    }
2609
 
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
2610
 
  }
2611
 
}
2612
 
 
2613
 
 
2614
 
/*
2615
 
  There are st_select_lex::add_table_to_list &
2616
 
  st_select_lex::set_lock_for_tables are in sql_parse.cc
2617
 
 
2618
 
  st_select_lex::print is in sql_select.cc
2619
 
 
2620
 
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
2621
 
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2622
 
  st_select_lex_unit::change_result
 
2260
  There are Select_Lex::add_table_to_list &
 
2261
  Select_Lex::set_lock_for_tables are in sql_parse.cc
 
2262
 
 
2263
  Select_Lex::print is in sql_select.cc
 
2264
 
 
2265
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
 
2266
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
 
2267
  Select_Lex_Unit::change_result
2623
2268
  are in sql_union.cc
2624
2269
*/
2625
2270
 
2633
2278
 
2634
2279
  DESCRIPTION
2635
2280
    Used in filling up the tagged hints list.
2636
 
    This list is filled by first setting the kind of the hint as a 
 
2281
    This list is filled by first setting the kind of the hint as a
2637
2282
    context variable and then adding hints of the current kind.
2638
2283
    Then the context variable index_hint_type can be reset to the
2639
2284
    next hint type.
2640
2285
*/
2641
 
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2286
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg,
2642
2287
                                        index_clause_map clause)
2643
 
 
2288
{
2644
2289
  current_index_hint_type= type_arg;
2645
2290
  current_index_hint_clause= clause;
2646
2291
}
2651
2296
 
2652
2297
  SYNOPSIS
2653
2298
    alloc_index_hints()
2654
 
      thd         current thread.
 
2299
      session         current thread.
2655
2300
*/
2656
2301
 
2657
 
void st_select_lex::alloc_index_hints (THD *thd)
2658
 
2659
 
  index_hints= new (thd->mem_root) List<Index_hint>(); 
 
2302
void Select_Lex::alloc_index_hints (Session *session)
 
2303
{
 
2304
  index_hints= new (session->mem_root) List<Index_hint>();
2660
2305
}
2661
2306
 
2662
2307
 
2663
2308
 
2664
2309
/*
2665
 
  adds an element to the array storing index usage hints 
 
2310
  adds an element to the array storing index usage hints
2666
2311
  (ADD/FORCE/IGNORE INDEX).
2667
2312
 
2668
2313
  SYNOPSIS
2669
2314
    add_index_hint()
2670
 
      thd         current thread.
 
2315
      session         current thread.
2671
2316
      str         name of the index.
2672
2317
      length      number of characters in str.
2673
2318
 
2674
2319
  RETURN VALUE
2675
2320
    0 on success, non-zero otherwise
2676
2321
*/
2677
 
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
 
2322
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
2678
2323
{
2679
 
  return index_hints->push_front (new (thd->mem_root) 
 
2324
  return index_hints->push_front (new (session->mem_root)
2680
2325
                                 Index_hint(current_index_hint_type,
2681
2326
                                            current_index_hint_clause,
2682
2327
                                            str, length));