~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

Moved the last of the libdrizzleclient calls into Protocol.

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;
325
263
  lex->sql_command= SQLCOM_END;
326
264
  lex->duplicates= DUP_ERROR;
327
265
  lex->ignore= 0;
328
 
  lex->proc_list.first= 0;
329
266
  lex->escape_used= false;
330
267
  lex->query_tables= 0;
331
268
  lex->reset_query_tables_list(false);
332
269
  lex->expr_allows_subselect= true;
333
270
  lex->use_only_table_context= false;
 
271
  lex->parse_vcol_expr= false;
334
272
 
335
273
  lex->name.str= 0;
336
274
  lex->name.length= 0;
337
275
  lex->nest_level=0 ;
338
276
  lex->allow_sum_func= 0;
339
277
  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
278
 
355
279
  lex->is_lex_started= true;
356
 
  return;
357
280
}
358
281
 
359
282
void lex_end(LEX *lex)
366
289
    lex->yacc_yyvs= 0;
367
290
  }
368
291
 
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
292
  delete lex->result;
375
293
  lex->result= 0;
376
 
 
377
 
  return;
378
294
}
379
295
 
380
296
 
381
297
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
382
298
{
 
299
  /* Plenty of memory for the largest lex symbol we have */
 
300
  char tok_upper[64];
383
301
  const char *tok= lip->get_tok_start();
 
302
  uint32_t tok_pos= 0;
 
303
  for (;tok_pos<len && tok_pos<63;tok_pos++)
 
304
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
 
305
  tok_upper[tok_pos]=0;
384
306
 
385
 
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
 
307
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
386
308
  if (symbol)
387
309
  {
388
310
    lip->yylval->symbol.symbol=symbol;
391
313
 
392
314
    return symbol->tok;
393
315
  }
 
316
 
394
317
  return 0;
395
318
}
396
319
 
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
320
 
416
321
bool is_lex_native_function(const LEX_STRING *name)
417
322
{
418
323
  assert(name != NULL);
419
 
  return (get_hash_symbol(name->str, name->length, 1) != 0);
 
324
  return (lookup_symbol(name->str, name->length, 1) != 0);
420
325
}
421
326
 
422
327
/* make a copy of token before ptr and set yytoklen */
426
331
  LEX_STRING tmp;
427
332
  lip->yyUnget();                       // ptr points now after last token char
428
333
  tmp.length=lip->yytoklen=length;
429
 
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
 
334
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
430
335
 
431
336
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
432
337
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
434
339
  return tmp;
435
340
}
436
341
 
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 
 
342
/*
 
343
 todo:
 
344
   There are no dangerous charsets in mysql for function
 
345
   get_quoted_token yet. But it should be fixed in the
441
346
   future to operate multichar strings (like ucs2)
442
347
*/
443
348
 
450
355
  char *to;
451
356
  lip->yyUnget();                       // ptr points now after last token char
452
357
  tmp.length= lip->yytoklen=length;
453
 
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
 
358
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
454
359
  from= lip->get_tok_start() + skip;
455
360
  to= tmp.str;
456
361
  end= to+length;
480
385
{
481
386
  register unsigned char c,sep;
482
387
  uint32_t found_escape=0;
483
 
  const CHARSET_INFO * const cs= lip->m_thd->charset();
 
388
  const CHARSET_INFO * const cs= lip->m_session->charset();
484
389
 
485
390
  lip->tok_bitmap= 0;
486
391
  sep= lip->yyGetLast();                        // String should end with this
528
433
      end -= post_skip;
529
434
      assert(end >= str);
530
435
 
531
 
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
436
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
532
437
        return (char*) "";              // Sql_alloc has set error flag
533
438
 
534
439
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
536
441
 
537
442
      if (!found_escape)
538
443
      {
539
 
        lip->yytoklen=(uint) (end-str);
 
444
        lip->yytoklen=(uint32_t) (end-str);
540
445
        memcpy(start,str,lip->yytoklen);
541
446
        start[lip->yytoklen]=0;
542
447
      }
592
497
            *to++ = *str;
593
498
        }
594
499
        *to=0;
595
 
        lip->yytoklen=(uint) (to-start);
 
500
        lip->yytoklen=(uint32_t) (to-start);
596
501
      }
597
502
      return start;
598
503
    }
604
509
/*
605
510
** Calc type of integer; long integer, int64_t integer or real.
606
511
** Returns smallest type that match the string.
607
 
** When using unsigned long long values the result is converted to a real
 
512
** When using uint64_t values the result is converted to a real
608
513
** because else they will be unexpected sign changes because all calculation
609
514
** is done with int64_t or double.
610
515
*/
693
598
 
694
599
 
695
600
/*
696
 
  MYSQLlex remember the following states from the following MYSQLlex()
 
601
  DRIZZLElex remember the following states from the following DRIZZLElex()
697
602
 
698
603
  - MY_LEX_EOQ                  Found end of query
699
604
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
700
605
                                (which can't be followed by a signed number)
701
606
*/
702
607
 
703
 
int MYSQLlex(void *arg, void *yythd)
 
608
int DRIZZLElex(void *arg, void *yysession)
704
609
{
705
 
  THD *thd= (THD *)yythd;
706
 
  Lex_input_stream *lip= thd->m_lip;
 
610
  Session *session= (Session *)yysession;
 
611
  Lex_input_stream *lip= session->m_lip;
707
612
  YYSTYPE *yylval=(YYSTYPE*) arg;
708
613
  int token;
709
614
 
720
625
    return token;
721
626
  }
722
627
 
723
 
  token= lex_one_token(arg, yythd);
 
628
  token= lex_one_token(arg, yysession);
724
629
 
725
630
  switch(token) {
726
631
  case WITH:
731
636
      to transform the grammar into a LALR(1) grammar,
732
637
      which sql_yacc.yy can process.
733
638
    */
734
 
    token= lex_one_token(arg, yythd);
 
639
    token= lex_one_token(arg, yysession);
735
640
    if (token == ROLLUP_SYM)
736
641
    {
737
642
      return WITH_ROLLUP_SYM;
746
651
      lip->lookahead_token= token;
747
652
      return WITH;
748
653
    }
749
 
    break;
750
654
  default:
751
655
    break;
752
656
  }
754
658
  return token;
755
659
}
756
660
 
757
 
int lex_one_token(void *arg, void *yythd)
 
661
int lex_one_token(void *arg, void *yysession)
758
662
{
759
663
  register unsigned char c= 0; /* Just set to shutup GCC */
760
664
  bool comment_closed;
761
665
  int   tokval, result_state;
762
666
  unsigned int length;
763
667
  enum my_lex_states state;
764
 
  THD *thd= (THD *)yythd;
765
 
  Lex_input_stream *lip= thd->m_lip;
766
 
  LEX *lex= thd->lex;
 
668
  Session *session= (Session *)yysession;
 
669
  Lex_input_stream *lip= session->m_lip;
 
670
  LEX *lex= session->lex;
767
671
  YYSTYPE *yylval=(YYSTYPE*) arg;
768
 
  const CHARSET_INFO * const cs= thd->charset();
 
672
  const CHARSET_INFO * const cs= session->charset();
769
673
  unsigned char *state_map= cs->state_map;
770
674
  unsigned char *ident_map= cs->ident_map;
771
675
 
902
806
      }
903
807
      yylval->lex_str=get_token(lip, 0, length);
904
808
 
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
809
      lip->body_utf8_append(lip->m_cpp_text_start);
928
810
 
929
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
811
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
930
812
                                    lip->m_cpp_text_end);
931
813
 
932
814
      return(result_state);                     // IDENT or IDENT_QUOTED
1029
911
 
1030
912
      lip->body_utf8_append(lip->m_cpp_text_start);
1031
913
 
1032
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
914
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1033
915
                                    lip->m_cpp_text_end);
1034
916
 
1035
917
      return(result_state);
1070
952
 
1071
953
      lip->body_utf8_append(lip->m_cpp_text_start);
1072
954
 
1073
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
955
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1074
956
                                    lip->m_cpp_text_end);
1075
957
 
1076
958
      return(IDENT_QUOTED);
1184
1066
 
1185
1067
      lip->body_utf8_append(lip->m_cpp_text_start);
1186
1068
 
1187
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1069
      lip->body_utf8_append_literal(session, &yylval->lex_str,
1188
1070
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1189
1071
        lip->m_cpp_text_end);
1190
1072
 
1220
1102
 
1221
1103
        /*
1222
1104
          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
 
1105
          '/' '*' '!', followed by digits ended by a non-digit.
 
1106
          There must be at least 5 digits for it to count
1228
1107
        */
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);
 
1108
        const int MAX_VERSION_SIZE= 16;
 
1109
        char version_str[MAX_VERSION_SIZE];
 
1110
 
 
1111
        int pos= 0;
 
1112
        do
 
1113
        {
 
1114
          version_str[pos]= lip->yyPeekn(pos);
 
1115
          pos++;
 
1116
        } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
 
1117
        version_str[pos]= 0;
 
1118
 
 
1119
        /* To keep some semblance of compatibility, we impose a 5 digit floor */
 
1120
        if (pos > 4)
 
1121
        {
 
1122
          uint64_t version;
 
1123
          version=strtoll(version_str, NULL, 10);
1245
1124
 
1246
1125
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1247
 
          lip->yySkipn(5);
 
1126
          lip->yySkipn(pos-1);
1248
1127
 
1249
1128
          if (version <= DRIZZLE_VERSION_ID)
1250
1129
          {
1326
1205
    case MY_LEX_SEMICOLON:                      // optional line terminator
1327
1206
      if (lip->yyPeek())
1328
1207
      {
1329
 
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1208
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
1330
1209
        {
1331
1210
          lip->found_semicolon= lip->get_ptr();
1332
 
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1211
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
1333
1212
          lip->next_state= MY_LEX_END;
1334
1213
          lip->set_echo(true);
1335
1214
          return (END_OF_INPUT);
1357
1236
    case MY_LEX_END:
1358
1237
      lip->next_state=MY_LEX_END;
1359
1238
      return(0);                        // We found end of input last time
1360
 
      
 
1239
 
1361
1240
      /* Actually real shouldn't start with . but allow them anyhow */
1362
1241
    case MY_LEX_REAL_OR_POINT:
1363
1242
      if (my_isdigit(cs,lip->yyPeek()))
1424
1303
 
1425
1304
      lip->body_utf8_append(lip->m_cpp_text_start);
1426
1305
 
1427
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1306
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1428
1307
                                    lip->m_cpp_text_end);
1429
1308
 
1430
1309
      return(result_state);
1442
1321
  statements and stored procedures and is compensated by always
1443
1322
  supplying a copy of Alter_info to these functions.
1444
1323
 
1445
 
  @return You need to use check the error in THD for out
 
1324
  @return You need to use check the error in Session for out
1446
1325
  of memory condition after calling this function.
1447
1326
*/
1448
1327
 
1501
1380
 
1502
1381
 
1503
1382
/*
1504
 
  st_select_lex structures initialisations
 
1383
  Select_Lex structures initialisations
1505
1384
*/
1506
1385
 
1507
 
void st_select_lex_node::init_query()
 
1386
void Select_Lex_Node::init_query()
1508
1387
{
1509
1388
  options= 0;
1510
1389
  linkage= UNSPECIFIED_TYPE;
1512
1391
  uncacheable= 0;
1513
1392
}
1514
1393
 
1515
 
void st_select_lex_node::init_select()
 
1394
void Select_Lex_Node::init_select()
1516
1395
{
1517
1396
}
1518
1397
 
1519
 
void st_select_lex_unit::init_query()
 
1398
void Select_Lex_Unit::init_query()
1520
1399
{
1521
 
  st_select_lex_node::init_query();
 
1400
  Select_Lex_Node::init_query();
1522
1401
  linkage= GLOBAL_OPTIONS_TYPE;
1523
1402
  global_parameters= first_select();
1524
1403
  select_limit_cnt= HA_POS_ERROR;
1535
1414
  found_rows_for_union= 0;
1536
1415
}
1537
1416
 
1538
 
void st_select_lex::init_query()
 
1417
void Select_Lex::init_query()
1539
1418
{
1540
 
  st_select_lex_node::init_query();
 
1419
  Select_Lex_Node::init_query();
1541
1420
  table_list.empty();
1542
1421
  top_join_list.empty();
1543
1422
  join_list= &top_join_list;
1561
1440
  parent_lex->push_context(&context);
1562
1441
  cond_count= between_count= with_wild= 0;
1563
1442
  max_equal_elems= 0;
1564
 
  conds_processed_with_permanent_arena= 0;
1565
1443
  ref_pointer_array= 0;
1566
1444
  select_n_where_fields= 0;
1567
1445
  select_n_having_items= 0;
1573
1451
  link_next= 0;
1574
1452
}
1575
1453
 
1576
 
void st_select_lex::init_select()
 
1454
void Select_Lex::init_select()
1577
1455
{
1578
 
  st_select_lex_node::init_select();
 
1456
  Select_Lex_Node::init_select();
1579
1457
  sj_nests.empty();
1580
1458
  group_list.empty();
1581
1459
  type= db= 0;
1605
1483
}
1606
1484
 
1607
1485
/*
1608
 
  st_select_lex structures linking
 
1486
  Select_Lex structures linking
1609
1487
*/
1610
1488
 
1611
1489
/* include on level down */
1612
 
void st_select_lex_node::include_down(st_select_lex_node *upper)
 
1490
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1613
1491
{
1614
1492
  if ((next= upper->slave))
1615
1493
    next->prev= &next;
1623
1501
  include on level down (but do not link)
1624
1502
 
1625
1503
  SYNOPSYS
1626
 
    st_select_lex_node::include_standalone()
 
1504
    Select_Lex_Node::include_standalone()
1627
1505
    upper - reference on node underr which this node should be included
1628
1506
    ref - references on reference on this node
1629
1507
*/
1630
 
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
1631
 
                                            st_select_lex_node **ref)
 
1508
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
 
1509
                                            Select_Lex_Node **ref)
1632
1510
{
1633
1511
  next= 0;
1634
1512
  prev= ref;
1637
1515
}
1638
1516
 
1639
1517
/* include neighbour (on same level) */
1640
 
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
 
1518
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1641
1519
{
1642
1520
  if ((next= before->next))
1643
1521
    next->prev= &next;
1647
1525
  slave= 0;
1648
1526
}
1649
1527
 
1650
 
/* including in global SELECT_LEX list */
1651
 
void st_select_lex_node::include_global(st_select_lex_node **plink)
 
1528
/* including in global Select_Lex list */
 
1529
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1652
1530
{
1653
1531
  if ((link_next= *plink))
1654
1532
    link_next->link_prev= &link_next;
1657
1535
}
1658
1536
 
1659
1537
//excluding from global list (internal function)
1660
 
void st_select_lex_node::fast_exclude()
 
1538
void Select_Lex_Node::fast_exclude()
1661
1539
{
1662
1540
  if (link_prev)
1663
1541
  {
1667
1545
  // Remove slave structure
1668
1546
  for (; slave; slave= slave->next)
1669
1547
    slave->fast_exclude();
1670
 
  
 
1548
 
1671
1549
}
1672
1550
 
1673
1551
/*
1674
1552
  excluding select_lex structure (except first (first select can't be
1675
1553
  deleted, because it is most upper select))
1676
1554
*/
1677
 
void st_select_lex_node::exclude()
 
1555
void Select_Lex_Node::exclude()
1678
1556
{
1679
1557
  //exclude from global list
1680
1558
  fast_exclude();
1681
1559
  //exclude from other structures
1682
1560
  if ((*prev= next))
1683
1561
    next->prev= prev;
1684
 
  /* 
1685
 
     We do not need following statements, because prev pointer of first 
 
1562
  /*
 
1563
     We do not need following statements, because prev pointer of first
1686
1564
     list element point to master->slave
1687
1565
     if (master->slave == this)
1688
1566
       master->slave= next;
1694
1572
  Exclude level of current unit from tree of SELECTs
1695
1573
 
1696
1574
  SYNOPSYS
1697
 
    st_select_lex_unit::exclude_level()
 
1575
    Select_Lex_Unit::exclude_level()
1698
1576
 
1699
1577
  NOTE: units which belong to current will be brought up on level of
1700
 
  currernt unit 
 
1578
  currernt unit
1701
1579
*/
1702
 
void st_select_lex_unit::exclude_level()
 
1580
void Select_Lex_Unit::exclude_level()
1703
1581
{
1704
 
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
1705
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1582
  Select_Lex_Unit *units= 0, **units_last= &units;
 
1583
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1706
1584
  {
1707
1585
    // unlink current level from global SELECTs list
1708
1586
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1709
1587
      sl->link_next->link_prev= sl->link_prev;
1710
1588
 
1711
1589
    // 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())
 
1590
    Select_Lex_Unit **last= 0;
 
1591
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1714
1592
    {
1715
1593
      u->master= master;
1716
 
      last= (SELECT_LEX_UNIT**)&(u->next);
 
1594
      last= (Select_Lex_Unit**)&(u->next);
1717
1595
    }
1718
1596
    if (last)
1719
1597
    {
1725
1603
  {
1726
1604
    // include brought up levels in place of current
1727
1605
    (*prev)= units;
1728
 
    (*units_last)= (SELECT_LEX_UNIT*)next;
 
1606
    (*units_last)= (Select_Lex_Unit*)next;
1729
1607
    if (next)
1730
 
      next->prev= (SELECT_LEX_NODE**)units_last;
 
1608
      next->prev= (Select_Lex_Node**)units_last;
1731
1609
    units->prev= prev;
1732
1610
  }
1733
1611
  else
1744
1622
  Exclude subtree of current unit from tree of SELECTs
1745
1623
 
1746
1624
  SYNOPSYS
1747
 
    st_select_lex_unit::exclude_tree()
 
1625
    Select_Lex_Unit::exclude_tree()
1748
1626
*/
1749
 
void st_select_lex_unit::exclude_tree()
 
1627
void Select_Lex_Unit::exclude_tree()
1750
1628
{
1751
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1629
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1752
1630
  {
1753
1631
    // unlink current level from global SELECTs list
1754
1632
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1755
1633
      sl->link_next->link_prev= sl->link_prev;
1756
1634
 
1757
1635
    // unlink underlay levels
1758
 
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1636
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1759
1637
    {
1760
1638
      u->exclude_level();
1761
1639
    }
1768
1646
 
1769
1647
 
1770
1648
/*
1771
 
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1649
  Select_Lex_Node::mark_as_dependent mark all Select_Lex struct from
1772
1650
  this to 'last' as dependent
1773
1651
 
1774
1652
  SYNOPSIS
1775
 
    last - pointer to last st_select_lex struct, before wich all 
1776
 
           st_select_lex have to be marked as dependent
 
1653
    last - pointer to last Select_Lex struct, before wich all
 
1654
           Select_Lex have to be marked as dependent
1777
1655
 
1778
1656
  NOTE
1779
 
    'last' should be reachable from this st_select_lex_node
 
1657
    'last' should be reachable from this Select_Lex_Node
1780
1658
*/
1781
1659
 
1782
 
void st_select_lex::mark_as_dependent(st_select_lex *last)
 
1660
void Select_Lex::mark_as_dependent(Select_Lex *last)
1783
1661
{
1784
1662
  /*
1785
1663
    Mark all selects from resolved to 1 before select where was
1786
1664
    found table as depended (of select where was found table)
1787
1665
  */
1788
 
  for (SELECT_LEX *s= this;
 
1666
  for (Select_Lex *s= this;
1789
1667
       s && s != last;
1790
1668
       s= s->outer_select())
1791
1669
  {
1794
1672
      // Select is dependent of outer select
1795
1673
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1796
1674
                       UNCACHEABLE_DEPENDENT;
1797
 
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1675
      Select_Lex_Unit *munit= s->master_unit();
1798
1676
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1799
1677
                       UNCACHEABLE_DEPENDENT;
1800
 
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1678
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1801
1679
      {
1802
1680
        if (sl != s &&
1803
1681
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1811
1689
  }
1812
1690
}
1813
1691
 
1814
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
 
1692
bool Select_Lex_Node::set_braces(bool)
1815
1693
{ 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)))
 
1694
bool Select_Lex_Node::inc_in_sum_expr()           { return 1; }
 
1695
uint32_t Select_Lex_Node::get_in_sum_expr()           { return 0; }
 
1696
TableList* Select_Lex_Node::get_table_list()     { return 0; }
 
1697
List<Item>* Select_Lex_Node::get_item_list()      { return 0; }
 
1698
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1699
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1827
1700
{
1828
1701
  return 0;
1829
1702
}
1830
 
uint32_t st_select_lex_node::get_table_join_options()
 
1703
uint32_t Select_Lex_Node::get_table_join_options()
1831
1704
{
1832
1705
  return 0;
1833
1706
}
1835
1708
/*
1836
1709
  prohibit using LIMIT clause
1837
1710
*/
1838
 
bool st_select_lex::test_limit()
 
1711
bool Select_Lex::test_limit()
1839
1712
{
1840
1713
  if (select_limit != 0)
1841
1714
  {
1847
1720
}
1848
1721
 
1849
1722
 
1850
 
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1723
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1851
1724
{
1852
1725
    return this;
1853
1726
}
1854
1727
 
1855
1728
 
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)
 
1729
Select_Lex* Select_Lex_Unit::outer_select()
 
1730
{
 
1731
  return (Select_Lex*) master;
 
1732
}
 
1733
 
 
1734
 
 
1735
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
 
1736
{
 
1737
  return add_to_list(session, order_list, item, asc);
 
1738
}
 
1739
 
 
1740
 
 
1741
bool Select_Lex::add_item_to_list(Session *, Item *item)
1870
1742
{
1871
1743
  return(item_list.push_back(item));
1872
1744
}
1873
1745
 
1874
1746
 
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)
 
1747
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
 
1748
{
 
1749
  return add_to_list(session, group_list, item, asc);
 
1750
}
 
1751
 
 
1752
 
 
1753
Select_Lex_Unit* Select_Lex::master_unit()
 
1754
{
 
1755
  return (Select_Lex_Unit*) master;
 
1756
}
 
1757
 
 
1758
 
 
1759
Select_Lex* Select_Lex::outer_select()
 
1760
{
 
1761
  return (Select_Lex*) master->get_master();
 
1762
}
 
1763
 
 
1764
 
 
1765
bool Select_Lex::set_braces(bool value)
1894
1766
{
1895
1767
  braces= value;
1896
 
  return 0; 
 
1768
  return 0;
1897
1769
}
1898
1770
 
1899
1771
 
1900
 
bool st_select_lex::inc_in_sum_expr()
 
1772
bool Select_Lex::inc_in_sum_expr()
1901
1773
{
1902
1774
  in_sum_expr++;
1903
1775
  return 0;
1904
1776
}
1905
1777
 
1906
1778
 
1907
 
uint32_t st_select_lex::get_in_sum_expr()
 
1779
uint32_t Select_Lex::get_in_sum_expr()
1908
1780
{
1909
1781
  return in_sum_expr;
1910
1782
}
1911
1783
 
1912
1784
 
1913
 
TableList* st_select_lex::get_table_list()
 
1785
TableList* Select_Lex::get_table_list()
1914
1786
{
1915
1787
  return (TableList*) table_list.first;
1916
1788
}
1917
1789
 
1918
 
List<Item>* st_select_lex::get_item_list()
 
1790
List<Item>* Select_Lex::get_item_list()
1919
1791
{
1920
1792
  return &item_list;
1921
1793
}
1922
1794
 
1923
 
uint32_t st_select_lex::get_table_join_options()
 
1795
uint32_t Select_Lex::get_table_join_options()
1924
1796
{
1925
1797
  return table_join_options;
1926
1798
}
1927
1799
 
1928
1800
 
1929
 
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
 
1801
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1930
1802
{
1931
1803
  if (ref_pointer_array)
1932
1804
    return 0;
1933
1805
 
1934
1806
  return (ref_pointer_array=
1935
 
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
 
1807
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1936
1808
                                                 item_list.elements +
1937
1809
                                                 select_n_having_items +
1938
1810
                                                 select_n_where_fields +
1940
1812
}
1941
1813
 
1942
1814
 
1943
 
void st_select_lex_unit::print(String *str, enum_query_type query_type)
 
1815
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
1944
1816
{
1945
1817
  bool union_all= !union_distinct;
1946
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1818
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1947
1819
  {
1948
1820
    if (sl != first_select())
1949
1821
    {
1955
1827
    }
1956
1828
    if (sl->braces)
1957
1829
      str->append('(');
1958
 
    sl->print(thd, str, query_type);
 
1830
    sl->print(session, str, query_type);
1959
1831
    if (sl->braces)
1960
1832
      str->append(')');
1961
1833
  }
1969
1841
        (order_st *) fake_select_lex->order_list.first,
1970
1842
        query_type);
1971
1843
    }
1972
 
    fake_select_lex->print_limit(thd, str, query_type);
 
1844
    fake_select_lex->print_limit(session, str, query_type);
1973
1845
  }
1974
1846
}
1975
1847
 
1976
1848
 
1977
 
void st_select_lex::print_order(String *str,
 
1849
void Select_Lex::print_order(String *str,
1978
1850
                                order_st *order,
1979
1851
                                enum_query_type query_type)
1980
1852
{
1994
1866
      str->append(',');
1995
1867
  }
1996
1868
}
1997
 
 
1998
 
 
1999
 
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
2000
 
                                String *str,
 
1869
 
 
1870
 
 
1871
void Select_Lex::print_limit(Session *, String *str,
2001
1872
                                enum_query_type query_type)
2002
1873
{
2003
 
  SELECT_LEX_UNIT *unit= master_unit();
 
1874
  Select_Lex_Unit *unit= master_unit();
2004
1875
  Item_subselect *item= unit->item;
2005
1876
 
2006
1877
  if (item && unit->global_parameters == this)
2037
1908
}
2038
1909
 
2039
1910
/**
2040
 
  @brief Restore the LEX and THD in case of a parse error.
 
1911
  @brief Restore the LEX and Session in case of a parse error.
2041
1912
 
2042
1913
  This is a clean up call that is invoked by the Bison generated
2043
 
  parser before returning an error from MYSQLparse. If your
 
1914
  parser before returning an error from DRIZZLEparse. If your
2044
1915
  semantic actions manipulate with the global thread state (which
2045
1916
  is a very bad practice and should not normally be employed) and
2046
1917
  need a clean-up in case of error, and you can not use %destructor
2048
1919
  to implement the clean up.
2049
1920
*/
2050
1921
 
2051
 
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
 
1922
void LEX::cleanup_lex_after_parse_error(Session *)
2052
1923
{
2053
1924
}
2054
1925
 
2085
1956
  query_tables= 0;
2086
1957
  query_tables_last= &query_tables;
2087
1958
  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
1959
}
2106
1960
 
2107
1961
 
2114
1968
 
2115
1969
void Query_tables_list::destroy_query_tables_list()
2116
1970
{
2117
 
  hash_free(&sroutines);
2118
1971
}
2119
1972
 
2120
1973
 
2122
1975
  Initialize LEX object.
2123
1976
 
2124
1977
  SYNOPSIS
2125
 
    st_lex::st_lex()
 
1978
    LEX::LEX()
2126
1979
 
2127
1980
  NOTE
2128
1981
    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()
 
1982
    Session object for which one can safely call open_tables(), lock_tables()
2130
1983
    and close_thread_tables() functions. But it is not yet ready for
2131
1984
    statement parsing. On should use lex_start() function to prepare LEX
2132
1985
    for this.
2133
1986
*/
2134
1987
 
2135
 
st_lex::st_lex()
 
1988
LEX::LEX()
2136
1989
  :result(0), yacc_yyss(0), yacc_yyvs(0),
2137
1990
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2138
1991
{
2139
1992
 
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
1993
  reset_query_tables_list(true);
2145
1994
}
2146
1995
 
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
1996
/*
2268
1997
  Detect that we need only table structure of derived table/view
2269
1998
 
2275
2004
    false no, we need data
2276
2005
*/
2277
2006
 
2278
 
bool st_lex::only_view_structure()
 
2007
bool LEX::only_view_structure()
2279
2008
{
2280
2009
  switch (sql_command) {
2281
2010
  case SQLCOM_SHOW_CREATE:
2300
2029
*/
2301
2030
 
2302
2031
 
2303
 
bool st_lex::need_correct_ident()
 
2032
bool LEX::need_correct_ident()
2304
2033
{
2305
2034
  switch(sql_command)
2306
2035
  {
2319
2048
  and will initialize the destination with the default
2320
2049
  database of the stored routine, rather than the default
2321
2050
  database of the connection it is parsed in.
2322
 
  E.g. if one has no current database selected, or current database 
 
2051
  E.g. if one has no current database selected, or current database
2323
2052
  set to 'bar' and then issues:
2324
2053
 
2325
2054
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2333
2062
*/
2334
2063
 
2335
2064
bool
2336
 
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
 
2065
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2337
2066
{
2338
 
  return thd->copy_db_to(p_db, p_db_length);
 
2067
  return session->copy_db_to(p_db, p_db_length);
2339
2068
}
2340
2069
 
2341
2070
/*
2342
2071
  initialize limit counters
2343
2072
 
2344
2073
  SYNOPSIS
2345
 
    st_select_lex_unit::set_limit()
2346
 
    values      - SELECT_LEX with initial values for counters
 
2074
    Select_Lex_Unit::set_limit()
 
2075
    values      - Select_Lex with initial values for counters
2347
2076
*/
2348
2077
 
2349
 
void st_select_lex_unit::set_limit(st_select_lex *sl)
 
2078
void Select_Lex_Unit::set_limit(Select_Lex *sl)
2350
2079
{
2351
2080
  ha_rows select_limit_val;
2352
2081
  uint64_t val;
2353
2082
 
2354
2083
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2355
2084
  select_limit_val= (ha_rows)val;
2356
 
#ifndef BIG_TABLES
2357
 
  /* 
 
2085
  /*
2358
2086
    Check for overflow : ha_rows can be smaller then uint64_t if
2359
2087
    BIG_TABLES is off.
2360
2088
    */
2361
2089
  if (val != (uint64_t)select_limit_val)
2362
2090
    select_limit_val= HA_POS_ERROR;
2363
 
#endif
2364
2091
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2365
2092
                                                 0UL);
2366
2093
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2387
2114
      In this case link_to_local is set.
2388
2115
 
2389
2116
*/
2390
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2117
TableList *LEX::unlink_first_table(bool *link_to_local)
2391
2118
{
2392
2119
  TableList *first;
2393
2120
  if ((first= query_tables))
2406
2133
    */
2407
2134
    if ((*link_to_local= test(select_lex.table_list.first)))
2408
2135
    {
2409
 
      select_lex.context.table_list= 
 
2136
      select_lex.context.table_list=
2410
2137
        select_lex.context.first_name_resolution_table= first->next_local;
2411
2138
      select_lex.table_list.first= (unsigned char*) (first->next_local);
2412
2139
      select_lex.table_list.elements--; //safety
2427
2154
  table list
2428
2155
 
2429
2156
  SYNOPSYS
2430
 
     st_lex::first_lists_tables_same()
 
2157
     LEX::first_lists_tables_same()
2431
2158
 
2432
2159
  NOTES
2433
2160
    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
 
2161
    main Select_Lex have special meaning => check that it is the first table
2435
2162
    in global list and re-link to be first in the global list if it is
2436
2163
    necessary.  We need such re-linking only for queries with sub-queries in
2437
2164
    the select list, as only in this case tables of sub-queries will go to
2438
2165
    the global list first.
2439
2166
*/
2440
2167
 
2441
 
void st_lex::first_lists_tables_same()
 
2168
void LEX::first_lists_tables_same()
2442
2169
{
2443
2170
  TableList *first_table= (TableList*) select_lex.table_list.first;
2444
2171
  if (query_tables != first_table && first_table != 0)
2474
2201
    global list
2475
2202
*/
2476
2203
 
2477
 
void st_lex::link_first_table_back(TableList *first,
 
2204
void LEX::link_first_table_back(TableList *first,
2478
2205
                                   bool link_to_local)
2479
2206
{
2480
2207
  if (first)
2501
2228
  cleanup lex for case when we open table by table for processing
2502
2229
 
2503
2230
  SYNOPSIS
2504
 
    st_lex::cleanup_after_one_table_open()
 
2231
    LEX::cleanup_after_one_table_open()
2505
2232
 
2506
2233
  NOTE
2507
2234
    This method is mostly responsible for cleaning up of selects lists and
2509
2236
    to call Query_tables_list::reset_query_tables_list(false).
2510
2237
*/
2511
2238
 
2512
 
void st_lex::cleanup_after_one_table_open()
 
2239
void LEX::cleanup_after_one_table_open()
2513
2240
{
2514
2241
  /*
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
 
2242
    session->lex->derived_tables & additional units may be set if we open
 
2243
    a view. It is necessary to clear session->lex->derived_tables flag
2517
2244
    to prevent processing of derived tables during next open_and_lock_tables
2518
2245
    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
 
2246
    NOTE: all units will be connected to session->lex->select_lex, because we
2520
2247
    have not UNION on most upper level.
2521
2248
    */
2522
2249
  if (all_selects_list != &select_lex)
2523
2250
  {
2524
2251
    derived_tables= 0;
2525
2252
    /* cleunup underlying units (units of VIEW) */
2526
 
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
 
2253
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
2527
2254
         un;
2528
2255
         un= un->next_unit())
2529
2256
      un->cleanup();
2536
2263
 
2537
2264
 
2538
2265
/*
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
2266
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2587
2267
 
2588
2268
  SYNOPSIS
2589
2269
    fix_prepare_info_in_table_list()
2590
 
      thd  Thread handle
 
2270
      session  Thread handle
2591
2271
      tbl  List of tables to process
2592
2272
 
2593
2273
  DESCRIPTION
2597
2277
 
2598
2278
*/
2599
2279
 
2600
 
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
 
2280
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
2601
2281
{
2602
2282
  for (; tbl; tbl= tbl->next_local)
2603
2283
  {
2604
2284
    if (tbl->on_expr)
2605
2285
    {
2606
2286
      tbl->prep_on_expr= tbl->on_expr;
2607
 
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2287
      tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
2608
2288
    }
2609
 
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2289
    fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
2610
2290
  }
2611
2291
}
2612
2292
 
2613
2293
 
2614
2294
/*
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
 
2295
  There are Select_Lex::add_table_to_list &
 
2296
  Select_Lex::set_lock_for_tables are in sql_parse.cc
 
2297
 
 
2298
  Select_Lex::print is in sql_select.cc
 
2299
 
 
2300
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
 
2301
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
 
2302
  Select_Lex_Unit::change_result
2623
2303
  are in sql_union.cc
2624
2304
*/
2625
2305
 
2633
2313
 
2634
2314
  DESCRIPTION
2635
2315
    Used in filling up the tagged hints list.
2636
 
    This list is filled by first setting the kind of the hint as a 
 
2316
    This list is filled by first setting the kind of the hint as a
2637
2317
    context variable and then adding hints of the current kind.
2638
2318
    Then the context variable index_hint_type can be reset to the
2639
2319
    next hint type.
2640
2320
*/
2641
 
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2321
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg,
2642
2322
                                        index_clause_map clause)
2643
 
 
2323
{
2644
2324
  current_index_hint_type= type_arg;
2645
2325
  current_index_hint_clause= clause;
2646
2326
}
2651
2331
 
2652
2332
  SYNOPSIS
2653
2333
    alloc_index_hints()
2654
 
      thd         current thread.
 
2334
      session         current thread.
2655
2335
*/
2656
2336
 
2657
 
void st_select_lex::alloc_index_hints (THD *thd)
2658
 
2659
 
  index_hints= new (thd->mem_root) List<Index_hint>(); 
 
2337
void Select_Lex::alloc_index_hints (Session *session)
 
2338
{
 
2339
  index_hints= new (session->mem_root) List<Index_hint>();
2660
2340
}
2661
2341
 
2662
2342
 
2663
2343
 
2664
2344
/*
2665
 
  adds an element to the array storing index usage hints 
 
2345
  adds an element to the array storing index usage hints
2666
2346
  (ADD/FORCE/IGNORE INDEX).
2667
2347
 
2668
2348
  SYNOPSIS
2669
2349
    add_index_hint()
2670
 
      thd         current thread.
 
2350
      session         current thread.
2671
2351
      str         name of the index.
2672
2352
      length      number of characters in str.
2673
2353
 
2674
2354
  RETURN VALUE
2675
2355
    0 on success, non-zero otherwise
2676
2356
*/
2677
 
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
 
2357
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
2678
2358
{
2679
 
  return index_hints->push_front (new (thd->mem_root) 
 
2359
  return index_hints->push_front (new (session->mem_root)
2680
2360
                                 Index_hint(current_index_hint_type,
2681
2361
                                            current_index_hint_clause,
2682
2362
                                            str, length));