~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_lex.cc

Moved my_handler to MyISAM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
 
#define DRIZZLE_LEX 1
20
 
#include "drizzled/server_includes.h"
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
 
 
 
19
#define MYSQL_LEX 1
 
20
#include "mysql_priv.h"
 
21
#include "item_create.h"
 
22
#include <m_ctype.h>
 
23
#include <hash.h>
 
24
 
 
25
static int lex_one_token(void *arg, void *yythd);
34
26
 
35
27
/*
36
28
  We are using pointer to this variable for distinguishing between assignment
44
36
*/
45
37
const LEX_STRING null_lex_str= {NULL, 0};
46
38
 
 
39
/* Longest standard keyword name */
 
40
 
 
41
#define TOCK_NAME_LENGTH 24
 
42
 
 
43
/*
 
44
  The following data is based on the latin1 character set, and is only
 
45
  used when comparing keywords
 
46
*/
 
47
 
 
48
static uchar to_upper_lex[]=
 
49
{
 
50
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
 
51
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 
52
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 
53
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
 
54
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
55
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
 
56
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
57
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
 
58
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
59
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
 
60
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
 
61
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
 
62
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
63
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
 
64
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
65
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
 
66
};
 
67
 
 
68
/* 
 
69
  Names of the index hints (for error messages). Keep in sync with 
 
70
  index_hint_type 
 
71
*/
 
72
 
 
73
const char * index_hint_type_name[] =
 
74
{
 
75
  "IGNORE INDEX", 
 
76
  "USE INDEX", 
 
77
  "FORCE INDEX"
 
78
};
 
79
 
 
80
inline int lex_casecmp(const char *s, const char *t, uint len)
 
81
{
 
82
  while (len-- != 0 &&
 
83
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
 
84
  return (int) len+1;
 
85
}
 
86
 
 
87
#include <lex_hash.h>
 
88
 
 
89
 
 
90
void lex_init(void)
 
91
{
 
92
  uint i;
 
93
  DBUG_ENTER("lex_init");
 
94
  for (i=0 ; i < array_elements(symbols) ; i++)
 
95
    symbols[i].length=(uchar) strlen(symbols[i].name);
 
96
  for (i=0 ; i < array_elements(sql_functions) ; i++)
 
97
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
 
98
 
 
99
  DBUG_VOID_RETURN;
 
100
}
 
101
 
 
102
 
 
103
void lex_free(void)
 
104
{                                       // Call this when daemon ends
 
105
  DBUG_ENTER("lex_free");
 
106
  DBUG_VOID_RETURN;
 
107
}
 
108
 
 
109
 
47
110
void
48
111
st_parsing_options::reset()
49
112
{
50
 
  allows_select_procedure= true;
 
113
  allows_variable= TRUE;
 
114
  allows_select_into= TRUE;
 
115
  allows_select_procedure= TRUE;
 
116
  allows_derived= TRUE;
51
117
}
52
118
 
53
 
Lex_input_stream::Lex_input_stream(Session *session,
 
119
Lex_input_stream::Lex_input_stream(THD *thd,
54
120
                                   const char* buffer,
55
121
                                   unsigned int length)
56
 
: m_session(session),
 
122
: m_thd(thd),
57
123
  yylineno(1),
58
124
  yytoklen(0),
59
125
  yylval(NULL),
66
132
  m_tok_start_prev(NULL),
67
133
  m_buf(buffer),
68
134
  m_buf_length(length),
69
 
  m_echo(true),
 
135
  m_echo(TRUE),
70
136
  m_cpp_tok_start(NULL),
71
137
  m_cpp_tok_start_prev(NULL),
72
138
  m_cpp_tok_end(NULL),
75
141
  next_state(MY_LEX_START),
76
142
  found_semicolon(NULL),
77
143
  ignore_space(1),
 
144
  stmt_prepare_mode(FALSE),
78
145
  in_comment(NO_COMMENT),
79
146
  m_underscore_cs(NULL)
80
147
{
81
 
  m_cpp_buf= (char*) session->alloc(length + 1);
 
148
  m_cpp_buf= (char*) thd->alloc(length + 1);
82
149
  m_cpp_ptr= m_cpp_buf;
83
150
}
84
151
 
92
159
     statement;
93
160
  2) Determine the beginning of the body.
94
161
 
95
 
  @param session        Thread context.
 
162
  @param thd        Thread context.
96
163
  @param begin_ptr  Pointer to the start of the body in the pre-processed
97
164
                    buffer.
98
165
*/
99
166
 
100
 
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
 
167
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
101
168
{
102
 
  assert(begin_ptr);
103
 
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
 
169
  DBUG_ASSERT(begin_ptr);
 
170
  DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
104
171
 
105
 
  uint32_t body_utf8_length=
106
 
    (m_buf_length / default_charset_info->mbminlen) *
 
172
  uint body_utf8_length=
 
173
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
107
174
    my_charset_utf8_bin.mbmaxlen;
108
175
 
109
 
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
 
176
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
110
177
  m_body_utf8_ptr= m_body_utf8;
111
178
  *m_body_utf8_ptr= 0;
112
179
 
137
204
void Lex_input_stream::body_utf8_append(const char *ptr,
138
205
                                        const char *end_ptr)
139
206
{
140
 
  assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
141
 
  assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
 
207
  DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
 
208
  DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
142
209
 
143
210
  if (!m_body_utf8)
144
211
    return;
172
239
  The operation converts the specified text literal to the utf8 and appends
173
240
  the result to the utf8-body.
174
241
 
175
 
  @param session      Thread context.
 
242
  @param thd      Thread context.
176
243
  @param txt      Text literal.
177
244
  @param txt_cs   Character set of the text literal.
178
245
  @param end_ptr  Pointer in the pre-processed buffer, to which
180
247
                  operation.
181
248
*/
182
249
 
183
 
void Lex_input_stream::body_utf8_append_literal(Session *session,
 
250
void Lex_input_stream::body_utf8_append_literal(THD *thd,
184
251
                                                const LEX_STRING *txt,
185
 
                                                const CHARSET_INFO * const txt_cs,
 
252
                                                CHARSET_INFO *txt_cs,
186
253
                                                const char *end_ptr)
187
254
{
188
255
  if (!m_cpp_utf8_processed_ptr)
192
259
 
193
260
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
194
261
  {
195
 
    session->convert_string(&utf_txt,
 
262
    thd->convert_string(&utf_txt,
196
263
                        &my_charset_utf8_general_ci,
197
264
                        txt->str, txt->length,
198
265
                        txt_cs);
219
286
  (We already do too much here)
220
287
*/
221
288
 
222
 
void lex_start(Session *session)
 
289
void lex_start(THD *thd)
223
290
{
224
 
  LEX *lex= session->lex;
 
291
  LEX *lex= thd->lex;
 
292
  DBUG_ENTER("lex_start");
225
293
 
226
 
  lex->session= lex->unit.session= session;
 
294
  lex->thd= lex->unit.thd= thd;
227
295
 
228
296
  lex->context_stack.empty();
229
297
  lex->unit.init_query();
234
302
  lex->value_list.empty();
235
303
  lex->update_list.empty();
236
304
  lex->param_list.empty();
 
305
  lex->view_list.empty();
237
306
  lex->auxiliary_table_list.empty();
238
307
  lex->unit.next= lex->unit.master=
239
308
    lex->unit.link_next= lex->unit.return_to= 0;
243
312
  lex->select_lex.master= &lex->unit;
244
313
  lex->select_lex.prev= &lex->unit.slave;
245
314
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
246
 
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
 
315
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
247
316
  lex->select_lex.options= 0;
248
317
  lex->select_lex.init_order();
249
318
  lex->select_lex.group_list.empty();
250
319
  lex->describe= 0;
251
 
  lex->subqueries= false;
 
320
  lex->subqueries= FALSE;
252
321
  lex->derived_tables= 0;
253
322
  lex->lock_option= TL_READ;
254
323
  lex->leaf_tables_insert= 0;
255
324
  lex->parsing_options.reset();
 
325
  lex->empty_field_list_on_rset= 0;
256
326
  lex->select_lex.select_number= 1;
257
327
  lex->length=0;
258
328
  lex->select_lex.in_sum_expr=0;
263
333
  lex->sql_command= SQLCOM_END;
264
334
  lex->duplicates= DUP_ERROR;
265
335
  lex->ignore= 0;
266
 
  lex->escape_used= false;
 
336
  lex->proc_list.first= 0;
 
337
  lex->escape_used= FALSE;
267
338
  lex->query_tables= 0;
268
 
  lex->reset_query_tables_list(false);
269
 
  lex->expr_allows_subselect= true;
270
 
  lex->use_only_table_context= false;
271
 
  lex->parse_vcol_expr= false;
 
339
  lex->reset_query_tables_list(FALSE);
 
340
  lex->expr_allows_subselect= TRUE;
 
341
  lex->use_only_table_context= FALSE;
272
342
 
273
343
  lex->name.str= 0;
274
344
  lex->name.length= 0;
275
345
  lex->nest_level=0 ;
276
346
  lex->allow_sum_func= 0;
277
347
  lex->in_sum_func= NULL;
 
348
  /*
 
349
    ok, there must be a better solution for this, long-term
 
350
    I tried "bzero" in the sql_yacc.yy code, but that for
 
351
    some reason made the values zero, even if they were set
 
352
  */
 
353
  lex->server_options.server_name= 0;
 
354
  lex->server_options.server_name_length= 0;
 
355
  lex->server_options.host= 0;
 
356
  lex->server_options.db= 0;
 
357
  lex->server_options.username= 0;
 
358
  lex->server_options.password= 0;
 
359
  lex->server_options.scheme= 0;
 
360
  lex->server_options.socket= 0;
 
361
  lex->server_options.owner= 0;
 
362
  lex->server_options.port= -1;
278
363
 
279
 
  lex->is_lex_started= true;
 
364
  lex->is_lex_started= TRUE;
 
365
  DBUG_VOID_RETURN;
280
366
}
281
367
 
282
368
void lex_end(LEX *lex)
283
369
{
 
370
  DBUG_ENTER("lex_end");
 
371
  DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
284
372
  if (lex->yacc_yyss)
285
373
  {
286
 
    free(lex->yacc_yyss);
287
 
    free(lex->yacc_yyvs);
 
374
    my_free(lex->yacc_yyss, MYF(0));
 
375
    my_free(lex->yacc_yyvs, MYF(0));
288
376
    lex->yacc_yyss= 0;
289
377
    lex->yacc_yyvs= 0;
290
378
  }
291
379
 
292
 
  delete lex->result;
293
 
  lex->result= 0;
 
380
  /* release used plugins */
 
381
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
 
382
                     lex->plugins.elements);
 
383
  reset_dynamic(&lex->plugins);
 
384
 
 
385
  DBUG_VOID_RETURN;
294
386
}
295
387
 
296
388
 
297
 
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
 
389
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
298
390
{
299
 
  /* Plenty of memory for the largest lex symbol we have */
300
 
  char tok_upper[64];
301
391
  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;
306
392
 
307
 
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
 
393
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
308
394
  if (symbol)
309
395
  {
310
396
    lip->yylval->symbol.symbol=symbol;
313
399
 
314
400
    return symbol->tok;
315
401
  }
316
 
 
317
402
  return 0;
318
403
}
319
404
 
 
405
/*
 
406
  Check if name is a keyword
 
407
 
 
408
  SYNOPSIS
 
409
    is_keyword()
 
410
    name      checked name (must not be empty)
 
411
    len       length of checked name
 
412
 
 
413
  RETURN VALUES
 
414
    0         name is a keyword
 
415
    1         name isn't a keyword
 
416
*/
 
417
 
 
418
bool is_keyword(const char *name, uint len)
 
419
{
 
420
  DBUG_ASSERT(len != 0);
 
421
  return get_hash_symbol(name,len,0)!=0;
 
422
}
320
423
 
321
424
bool is_lex_native_function(const LEX_STRING *name)
322
425
{
323
 
  assert(name != NULL);
324
 
  return (lookup_symbol(name->str, name->length, 1) != 0);
 
426
  DBUG_ASSERT(name != NULL);
 
427
  return (get_hash_symbol(name->str, name->length, 1) != 0);
325
428
}
326
429
 
327
430
/* make a copy of token before ptr and set yytoklen */
328
431
 
329
 
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
 
432
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
330
433
{
331
434
  LEX_STRING tmp;
332
435
  lip->yyUnget();                       // ptr points now after last token char
333
436
  tmp.length=lip->yytoklen=length;
334
 
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
 
437
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
335
438
 
336
439
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
337
440
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
339
442
  return tmp;
340
443
}
341
444
 
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
 
445
/* 
 
446
 todo: 
 
447
   There are no dangerous charsets in mysql for function 
 
448
   get_quoted_token yet. But it should be fixed in the 
346
449
   future to operate multichar strings (like ucs2)
347
450
*/
348
451
 
349
452
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
350
 
                                   uint32_t skip,
351
 
                                   uint32_t length, char quote)
 
453
                                   uint skip,
 
454
                                   uint length, char quote)
352
455
{
353
456
  LEX_STRING tmp;
354
457
  const char *from, *end;
355
458
  char *to;
356
459
  lip->yyUnget();                       // ptr points now after last token char
357
460
  tmp.length= lip->yytoklen=length;
358
 
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
 
461
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
359
462
  from= lip->get_tok_start() + skip;
360
463
  to= tmp.str;
361
464
  end= to+length;
383
486
 
384
487
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
385
488
{
386
 
  register unsigned char c,sep;
387
 
  uint32_t found_escape=0;
388
 
  const CHARSET_INFO * const cs= lip->m_session->charset();
 
489
  register uchar c,sep;
 
490
  uint found_escape=0;
 
491
  CHARSET_INFO *cs= lip->m_thd->charset();
389
492
 
390
493
  lip->tok_bitmap= 0;
391
494
  sep= lip->yyGetLast();                        // String should end with this
431
534
      /* Extract the text from the token */
432
535
      str += pre_skip;
433
536
      end -= post_skip;
434
 
      assert(end >= str);
 
537
      DBUG_ASSERT(end >= str);
435
538
 
436
 
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
 
539
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
437
540
        return (char*) "";              // Sql_alloc has set error flag
438
541
 
439
542
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
441
544
 
442
545
      if (!found_escape)
443
546
      {
444
 
        lip->yytoklen=(uint32_t) (end-str);
 
547
        lip->yytoklen=(uint) (end-str);
445
548
        memcpy(start,str,lip->yytoklen);
446
549
        start[lip->yytoklen]=0;
447
550
      }
461
564
              continue;
462
565
          }
463
566
#endif
464
 
          if (*str == '\\' && str+1 != end)
 
567
          if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
568
              *str == '\\' && str+1 != end)
465
569
          {
466
570
            switch(*++str) {
467
571
            case 'n':
497
601
            *to++ = *str;
498
602
        }
499
603
        *to=0;
500
 
        lip->yytoklen=(uint32_t) (to-start);
 
604
        lip->yytoklen=(uint) (to-start);
501
605
      }
502
606
      return start;
503
607
    }
507
611
 
508
612
 
509
613
/*
510
 
** Calc type of integer; long integer, int64_t integer or real.
 
614
** Calc type of integer; long integer, longlong integer or real.
511
615
** Returns smallest type that match the string.
512
 
** When using uint64_t values the result is converted to a real
 
616
** When using unsigned long long values the result is converted to a real
513
617
** because else they will be unexpected sign changes because all calculation
514
 
** is done with int64_t or double.
 
618
** is done with longlong or double.
515
619
*/
516
620
 
517
621
static const char *long_str="2147483647";
518
 
static const uint32_t long_len=10;
 
622
static const uint long_len=10;
519
623
static const char *signed_long_str="-2147483648";
520
 
static const char *int64_t_str="9223372036854775807";
521
 
static const uint32_t int64_t_len=19;
522
 
static const char *signed_int64_t_str="-9223372036854775808";
523
 
static const uint32_t signed_int64_t_len=19;
524
 
static const char *unsigned_int64_t_str="18446744073709551615";
525
 
static const uint32_t unsigned_int64_t_len=20;
 
624
static const char *longlong_str="9223372036854775807";
 
625
static const uint longlong_len=19;
 
626
static const char *signed_longlong_str="-9223372036854775808";
 
627
static const uint signed_longlong_len=19;
 
628
static const char *unsigned_longlong_str="18446744073709551615";
 
629
static const uint unsigned_longlong_len=20;
526
630
 
527
 
static inline uint32_t int_token(const char *str,uint32_t length)
 
631
static inline uint int_token(const char *str,uint length)
528
632
{
529
633
  if (length < long_len)                        // quick normal case
530
634
    return NUM;
546
650
  if (length < long_len)
547
651
    return NUM;
548
652
 
549
 
  uint32_t smaller,bigger;
 
653
  uint smaller,bigger;
550
654
  const char *cmp;
551
655
  if (neg)
552
656
  {
556
660
      smaller=NUM;                              // If <= signed_long_str
557
661
      bigger=LONG_NUM;                          // If >= signed_long_str
558
662
    }
559
 
    else if (length < signed_int64_t_len)
 
663
    else if (length < signed_longlong_len)
560
664
      return LONG_NUM;
561
 
    else if (length > signed_int64_t_len)
 
665
    else if (length > signed_longlong_len)
562
666
      return DECIMAL_NUM;
563
667
    else
564
668
    {
565
 
      cmp=signed_int64_t_str+1;
566
 
      smaller=LONG_NUM;                         // If <= signed_int64_t_str
 
669
      cmp=signed_longlong_str+1;
 
670
      smaller=LONG_NUM;                         // If <= signed_longlong_str
567
671
      bigger=DECIMAL_NUM;
568
672
    }
569
673
  }
575
679
      smaller=NUM;
576
680
      bigger=LONG_NUM;
577
681
    }
578
 
    else if (length < int64_t_len)
 
682
    else if (length < longlong_len)
579
683
      return LONG_NUM;
580
 
    else if (length > int64_t_len)
 
684
    else if (length > longlong_len)
581
685
    {
582
 
      if (length > unsigned_int64_t_len)
 
686
      if (length > unsigned_longlong_len)
583
687
        return DECIMAL_NUM;
584
 
      cmp=unsigned_int64_t_str;
 
688
      cmp=unsigned_longlong_str;
585
689
      smaller=ULONGLONG_NUM;
586
690
      bigger=DECIMAL_NUM;
587
691
    }
588
692
    else
589
693
    {
590
 
      cmp=int64_t_str;
 
694
      cmp=longlong_str;
591
695
      smaller=LONG_NUM;
592
696
      bigger= ULONGLONG_NUM;
593
697
    }
594
698
  }
595
699
  while (*cmp && *cmp++ == *str++) ;
596
 
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
 
700
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
597
701
}
598
702
 
599
703
 
600
704
/*
601
 
  DRIZZLElex remember the following states from the following DRIZZLElex()
 
705
  MYSQLlex remember the following states from the following MYSQLlex()
602
706
 
603
707
  - MY_LEX_EOQ                  Found end of query
604
708
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
605
709
                                (which can't be followed by a signed number)
606
710
*/
607
711
 
608
 
int DRIZZLElex(void *arg, void *yysession)
 
712
int MYSQLlex(void *arg, void *yythd)
609
713
{
610
 
  Session *session= (Session *)yysession;
611
 
  Lex_input_stream *lip= session->m_lip;
 
714
  THD *thd= (THD *)yythd;
 
715
  Lex_input_stream *lip= thd->m_lip;
612
716
  YYSTYPE *yylval=(YYSTYPE*) arg;
613
717
  int token;
614
718
 
625
729
    return token;
626
730
  }
627
731
 
628
 
  token= lex_one_token(arg, yysession);
 
732
  token= lex_one_token(arg, yythd);
629
733
 
630
734
  switch(token) {
631
735
  case WITH:
632
736
    /*
633
 
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
 
737
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
634
738
      which makes the grammar LALR(2).
635
739
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
636
740
      to transform the grammar into a LALR(1) grammar,
637
741
      which sql_yacc.yy can process.
638
742
    */
639
 
    token= lex_one_token(arg, yysession);
640
 
    if (token == ROLLUP_SYM)
641
 
    {
 
743
    token= lex_one_token(arg, yythd);
 
744
    switch(token) {
 
745
    case CUBE_SYM:
 
746
      return WITH_CUBE_SYM;
 
747
    case ROLLUP_SYM:
642
748
      return WITH_ROLLUP_SYM;
643
 
    }
644
 
    else
645
 
    {
 
749
    default:
646
750
      /*
647
751
        Save the token following 'WITH'
648
752
      */
651
755
      lip->lookahead_token= token;
652
756
      return WITH;
653
757
    }
 
758
    break;
654
759
  default:
655
760
    break;
656
761
  }
658
763
  return token;
659
764
}
660
765
 
661
 
int lex_one_token(void *arg, void *yysession)
 
766
int lex_one_token(void *arg, void *yythd)
662
767
{
663
768
  register unsigned char c= 0; /* Just set to shutup GCC */
664
769
  bool comment_closed;
665
770
  int   tokval, result_state;
666
771
  unsigned int length;
667
772
  enum my_lex_states state;
668
 
  Session *session= (Session *)yysession;
669
 
  Lex_input_stream *lip= session->m_lip;
670
 
  LEX *lex= session->lex;
 
773
  THD *thd= (THD *)yythd;
 
774
  Lex_input_stream *lip= thd->m_lip;
 
775
  LEX *lex= thd->lex;
671
776
  YYSTYPE *yylval=(YYSTYPE*) arg;
672
 
  const CHARSET_INFO * const cs= session->charset();
673
 
  unsigned char *state_map= cs->state_map;
674
 
  unsigned char *ident_map= cs->ident_map;
 
777
  CHARSET_INFO *cs= thd->charset();
 
778
  uchar *state_map= cs->state_map;
 
779
  uchar *ident_map= cs->ident_map;
675
780
 
676
781
  lip->yylval=yylval;                   // The global state
677
782
 
730
835
        */
731
836
        lip->restart_token();
732
837
      }
 
838
      else
 
839
      {
 
840
        /*
 
841
          Check for a placeholder: it should not precede a possible identifier
 
842
          because of binlogging: when a placeholder is replaced with
 
843
          its value in a query for the binlog, the query must stay
 
844
          grammatically correct.
 
845
        */
 
846
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[(uint8_t)(lip->yyPeek())])
 
847
        return(PARAM_MARKER);
 
848
      }
733
849
 
734
850
      return((int) c);
735
851
 
 
852
    case MY_LEX_IDENT_OR_NCHAR:
 
853
      if (lip->yyPeek() != '\'')
 
854
      {
 
855
        state= MY_LEX_IDENT;
 
856
        break;
 
857
      }
 
858
      /* Found N'string' */
 
859
      lip->yySkip();                         // Skip '
 
860
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
 
861
      {
 
862
        state= MY_LEX_CHAR;             // Read char by char
 
863
        break;
 
864
      }
 
865
      yylval->lex_str.length= lip->yytoklen;
 
866
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
867
      return(NCHAR_STRING);
 
868
 
736
869
    case MY_LEX_IDENT_OR_HEX:
737
870
      if (lip->yyPeek() == '\'')
738
871
      {                                 // Found x'hex-number'
806
939
      }
807
940
      yylval->lex_str=get_token(lip, 0, length);
808
941
 
 
942
      /*
 
943
         Note: "SELECT _bla AS 'alias'"
 
944
         _bla should be considered as a IDENT if charset haven't been found.
 
945
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
 
946
         producing an error.
 
947
      */
 
948
 
 
949
      if (yylval->lex_str.str[0] == '_')
 
950
      {
 
951
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
952
                                                MY_CS_PRIMARY, MYF(0));
 
953
        if (cs)
 
954
        {
 
955
          yylval->charset= cs;
 
956
          lip->m_underscore_cs= cs;
 
957
 
 
958
          lip->body_utf8_append(lip->m_cpp_text_start,
 
959
                                lip->get_cpp_tok_start() + length);
 
960
          return(UNDERSCORE_CHARSET);
 
961
        }
 
962
      }
 
963
 
809
964
      lip->body_utf8_append(lip->m_cpp_text_start);
810
965
 
811
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
966
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
812
967
                                    lip->m_cpp_text_end);
813
968
 
814
969
      return(result_state);                     // IDENT or IDENT_QUOTED
911
1066
 
912
1067
      lip->body_utf8_append(lip->m_cpp_text_start);
913
1068
 
914
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1069
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
915
1070
                                    lip->m_cpp_text_end);
916
1071
 
917
1072
      return(result_state);
918
1073
 
919
1074
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
920
1075
    {
921
 
      uint32_t double_quotes= 0;
 
1076
      uint double_quotes= 0;
922
1077
      char quote_char= c;                       // Used char
923
1078
      while ((c=lip->yyGet()))
924
1079
      {
952
1107
 
953
1108
      lip->body_utf8_append(lip->m_cpp_text_start);
954
1109
 
955
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1110
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
956
1111
                                    lip->m_cpp_text_end);
957
1112
 
958
1113
      return(IDENT_QUOTED);
1066
1221
 
1067
1222
      lip->body_utf8_append(lip->m_cpp_text_start);
1068
1223
 
1069
 
      lip->body_utf8_append_literal(session, &yylval->lex_str,
 
1224
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
1070
1225
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1071
1226
        lip->m_cpp_text_end);
1072
1227
 
1095
1250
      {
1096
1251
        lip->in_comment= DISCARD_COMMENT;
1097
1252
        /* Accept '/' '*' '!', but do not keep this marker. */
1098
 
        lip->set_echo(false);
 
1253
        lip->set_echo(FALSE);
1099
1254
        lip->yySkip();
1100
1255
        lip->yySkip();
1101
1256
        lip->yySkip();
1102
1257
 
1103
1258
        /*
1104
1259
          The special comment format is very strict:
1105
 
          '/' '*' '!', followed by digits ended by a non-digit.
1106
 
          There must be at least 5 digits for it to count
 
1260
          '/' '*' '!', followed by exactly
 
1261
          1 digit (major), 2 digits (minor), then 2 digits (dot).
 
1262
          32302 -> 3.23.02
 
1263
          50032 -> 5.0.32
 
1264
          50114 -> 5.1.14
1107
1265
        */
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);
 
1266
        char version_str[6];
 
1267
        version_str[0]= lip->yyPeekn(0);
 
1268
        version_str[1]= lip->yyPeekn(1);
 
1269
        version_str[2]= lip->yyPeekn(2);
 
1270
        version_str[3]= lip->yyPeekn(3);
 
1271
        version_str[4]= lip->yyPeekn(4);
 
1272
        version_str[5]= 0;
 
1273
        if (  my_isdigit(cs, version_str[0])
 
1274
           && my_isdigit(cs, version_str[1])
 
1275
           && my_isdigit(cs, version_str[2])
 
1276
           && my_isdigit(cs, version_str[3])
 
1277
           && my_isdigit(cs, version_str[4])
 
1278
           )
 
1279
        {
 
1280
          ulong version;
 
1281
          version=strtol(version_str, NULL, 10);
1124
1282
 
1125
1283
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1126
 
          lip->yySkipn(pos-1);
 
1284
          lip->yySkipn(5);
1127
1285
 
1128
 
          if (version <= DRIZZLE_VERSION_ID)
 
1286
          if (version <= MYSQL_VERSION_ID)
1129
1287
          {
1130
1288
            /* Expand the content of the special comment as real code */
1131
 
            lip->set_echo(true);
 
1289
            lip->set_echo(TRUE);
1132
1290
            state=MY_LEX_START;
1133
1291
            break;
1134
1292
          }
1136
1294
        else
1137
1295
        {
1138
1296
          state=MY_LEX_START;
1139
 
          lip->set_echo(true);
 
1297
          lip->set_echo(TRUE);
1140
1298
          break;
1141
1299
        }
1142
1300
      }
1154
1312
        Note: There is no such thing as nesting comments,
1155
1313
        the first '*' '/' sequence seen will mark the end.
1156
1314
      */
1157
 
      comment_closed= false;
 
1315
      comment_closed= FALSE;
1158
1316
      while (! lip->eof())
1159
1317
      {
1160
1318
        c= lip->yyGet();
1163
1321
          if (lip->yyPeek() == '/')
1164
1322
          {
1165
1323
            lip->yySkip();
1166
 
            comment_closed= true;
 
1324
            comment_closed= TRUE;
1167
1325
            state = MY_LEX_START;
1168
1326
            break;
1169
1327
          }
1176
1334
        return (ABORT_SYM);
1177
1335
      state = MY_LEX_START;             // Try again
1178
1336
      lip->in_comment= NO_COMMENT;
1179
 
      lip->set_echo(true);
 
1337
      lip->set_echo(TRUE);
1180
1338
      break;
1181
1339
    case MY_LEX_END_LONG_COMMENT:
1182
1340
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1187
1345
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1188
1346
        lip->yySkipn(2);
1189
1347
        /* And start recording the tokens again */
1190
 
        lip->set_echo(true);
 
1348
        lip->set_echo(TRUE);
1191
1349
        lip->in_comment=NO_COMMENT;
1192
1350
        state=MY_LEX_START;
1193
1351
      }
1205
1363
    case MY_LEX_SEMICOLON:                      // optional line terminator
1206
1364
      if (lip->yyPeek())
1207
1365
      {
1208
 
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1366
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && 
 
1367
            !lip->stmt_prepare_mode)
1209
1368
        {
1210
1369
          lip->found_semicolon= lip->get_ptr();
1211
 
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1370
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
1212
1371
          lip->next_state= MY_LEX_END;
1213
 
          lip->set_echo(true);
 
1372
          lip->set_echo(TRUE);
1214
1373
          return (END_OF_INPUT);
1215
1374
        }
1216
1375
        state= MY_LEX_CHAR;             // Return ';'
1222
1381
      if (lip->eof())
1223
1382
      {
1224
1383
        lip->yyUnget();                 // Reject the last '\0'
1225
 
        lip->set_echo(false);
 
1384
        lip->set_echo(FALSE);
1226
1385
        lip->yySkip();
1227
 
        lip->set_echo(true);
 
1386
        lip->set_echo(TRUE);
1228
1387
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
1229
1388
        if (lip->in_comment != NO_COMMENT)
1230
1389
          return (ABORT_SYM);
1236
1395
    case MY_LEX_END:
1237
1396
      lip->next_state=MY_LEX_END;
1238
1397
      return(0);                        // We found end of input last time
1239
 
 
 
1398
      
1240
1399
      /* Actually real shouldn't start with . but allow them anyhow */
1241
1400
    case MY_LEX_REAL_OR_POINT:
1242
1401
      if (my_isdigit(cs,lip->yyPeek()))
1303
1462
 
1304
1463
      lip->body_utf8_append(lip->m_cpp_text_start);
1305
1464
 
1306
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1465
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1307
1466
                                    lip->m_cpp_text_end);
1308
1467
 
1309
1468
      return(result_state);
1321
1480
  statements and stored procedures and is compensated by always
1322
1481
  supplying a copy of Alter_info to these functions.
1323
1482
 
1324
 
  @return You need to use check the error in Session for out
 
1483
  @return You need to use check the error in THD for out
1325
1484
  of memory condition after calling this function.
1326
1485
*/
1327
1486
 
1354
1513
}
1355
1514
 
1356
1515
 
1357
 
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
 
1516
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
1358
1517
{
1359
1518
  /*
1360
1519
    TODO:
1380
1539
 
1381
1540
 
1382
1541
/*
1383
 
  Select_Lex structures initialisations
 
1542
  st_select_lex structures initialisations
1384
1543
*/
1385
1544
 
1386
 
void Select_Lex_Node::init_query()
 
1545
void st_select_lex_node::init_query()
1387
1546
{
1388
1547
  options= 0;
1389
1548
  linkage= UNSPECIFIED_TYPE;
1391
1550
  uncacheable= 0;
1392
1551
}
1393
1552
 
1394
 
void Select_Lex_Node::init_select()
 
1553
void st_select_lex_node::init_select()
1395
1554
{
1396
1555
}
1397
1556
 
1398
 
void Select_Lex_Unit::init_query()
 
1557
void st_select_lex_unit::init_query()
1399
1558
{
1400
 
  Select_Lex_Node::init_query();
 
1559
  st_select_lex_node::init_query();
1401
1560
  linkage= GLOBAL_OPTIONS_TYPE;
1402
1561
  global_parameters= first_select();
1403
1562
  select_limit_cnt= HA_POS_ERROR;
1414
1573
  found_rows_for_union= 0;
1415
1574
}
1416
1575
 
1417
 
void Select_Lex::init_query()
 
1576
void st_select_lex::init_query()
1418
1577
{
1419
 
  Select_Lex_Node::init_query();
 
1578
  st_select_lex_node::init_query();
1420
1579
  table_list.empty();
1421
1580
  top_join_list.empty();
1422
1581
  join_list= &top_join_list;
1423
1582
  embedding= leaf_tables= 0;
1424
1583
  item_list.empty();
1425
1584
  join= 0;
1426
 
  having= where= 0;
 
1585
  having= prep_having= where= prep_where= 0;
1427
1586
  olap= UNSPECIFIED_OLAP_TYPE;
1428
1587
  having_fix_field= 0;
1429
1588
  context.select_lex= this;
1440
1599
  parent_lex->push_context(&context);
1441
1600
  cond_count= between_count= with_wild= 0;
1442
1601
  max_equal_elems= 0;
 
1602
  conds_processed_with_permanent_arena= 0;
1443
1603
  ref_pointer_array= 0;
1444
1604
  select_n_where_fields= 0;
1445
1605
  select_n_having_items= 0;
1446
1606
  subquery_in_having= explicit_limit= 0;
1447
1607
  is_item_list_lookup= 0;
 
1608
  first_execution= 1;
 
1609
  first_cond_optimization= 1;
1448
1610
  parsing_place= NO_MATTER;
1449
 
  exclude_from_table_unique_test= false;
 
1611
  exclude_from_table_unique_test= no_wrap_view_item= FALSE;
1450
1612
  nest_level= 0;
1451
1613
  link_next= 0;
1452
1614
}
1453
1615
 
1454
 
void Select_Lex::init_select()
 
1616
void st_select_lex::init_select()
1455
1617
{
1456
 
  Select_Lex_Node::init_select();
 
1618
  st_select_lex_node::init_select();
1457
1619
  sj_nests.empty();
1458
1620
  group_list.empty();
1459
1621
  type= db= 0;
1469
1631
  linkage= UNSPECIFIED_TYPE;
1470
1632
  order_list.elements= 0;
1471
1633
  order_list.first= 0;
1472
 
  order_list.next= (unsigned char**) &order_list.first;
 
1634
  order_list.next= (uchar**) &order_list.first;
1473
1635
  /* Set limit and offset to default values */
1474
1636
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1475
1637
  offset_limit= 0;      /* denotes the default offset = 0 */
1483
1645
}
1484
1646
 
1485
1647
/*
1486
 
  Select_Lex structures linking
 
1648
  st_select_lex structures linking
1487
1649
*/
1488
1650
 
1489
1651
/* include on level down */
1490
 
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
 
1652
void st_select_lex_node::include_down(st_select_lex_node *upper)
1491
1653
{
1492
1654
  if ((next= upper->slave))
1493
1655
    next->prev= &next;
1501
1663
  include on level down (but do not link)
1502
1664
 
1503
1665
  SYNOPSYS
1504
 
    Select_Lex_Node::include_standalone()
 
1666
    st_select_lex_node::include_standalone()
1505
1667
    upper - reference on node underr which this node should be included
1506
1668
    ref - references on reference on this node
1507
1669
*/
1508
 
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1509
 
                                            Select_Lex_Node **ref)
 
1670
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
 
1671
                                            st_select_lex_node **ref)
1510
1672
{
1511
1673
  next= 0;
1512
1674
  prev= ref;
1515
1677
}
1516
1678
 
1517
1679
/* include neighbour (on same level) */
1518
 
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
 
1680
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1519
1681
{
1520
1682
  if ((next= before->next))
1521
1683
    next->prev= &next;
1525
1687
  slave= 0;
1526
1688
}
1527
1689
 
1528
 
/* including in global Select_Lex list */
1529
 
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
 
1690
/* including in global SELECT_LEX list */
 
1691
void st_select_lex_node::include_global(st_select_lex_node **plink)
1530
1692
{
1531
1693
  if ((link_next= *plink))
1532
1694
    link_next->link_prev= &link_next;
1535
1697
}
1536
1698
 
1537
1699
//excluding from global list (internal function)
1538
 
void Select_Lex_Node::fast_exclude()
 
1700
void st_select_lex_node::fast_exclude()
1539
1701
{
1540
1702
  if (link_prev)
1541
1703
  {
1545
1707
  // Remove slave structure
1546
1708
  for (; slave; slave= slave->next)
1547
1709
    slave->fast_exclude();
1548
 
 
 
1710
  
1549
1711
}
1550
1712
 
1551
1713
/*
1552
1714
  excluding select_lex structure (except first (first select can't be
1553
1715
  deleted, because it is most upper select))
1554
1716
*/
1555
 
void Select_Lex_Node::exclude()
 
1717
void st_select_lex_node::exclude()
1556
1718
{
1557
1719
  //exclude from global list
1558
1720
  fast_exclude();
1559
1721
  //exclude from other structures
1560
1722
  if ((*prev= next))
1561
1723
    next->prev= prev;
1562
 
  /*
1563
 
     We do not need following statements, because prev pointer of first
 
1724
  /* 
 
1725
     We do not need following statements, because prev pointer of first 
1564
1726
     list element point to master->slave
1565
1727
     if (master->slave == this)
1566
1728
       master->slave= next;
1572
1734
  Exclude level of current unit from tree of SELECTs
1573
1735
 
1574
1736
  SYNOPSYS
1575
 
    Select_Lex_Unit::exclude_level()
 
1737
    st_select_lex_unit::exclude_level()
1576
1738
 
1577
1739
  NOTE: units which belong to current will be brought up on level of
1578
 
  currernt unit
 
1740
  currernt unit 
1579
1741
*/
1580
 
void Select_Lex_Unit::exclude_level()
 
1742
void st_select_lex_unit::exclude_level()
1581
1743
{
1582
 
  Select_Lex_Unit *units= 0, **units_last= &units;
1583
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1744
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
 
1745
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1584
1746
  {
1585
1747
    // unlink current level from global SELECTs list
1586
1748
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1587
1749
      sl->link_next->link_prev= sl->link_prev;
1588
1750
 
1589
1751
    // bring up underlay levels
1590
 
    Select_Lex_Unit **last= 0;
1591
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1752
    SELECT_LEX_UNIT **last= 0;
 
1753
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1592
1754
    {
1593
1755
      u->master= master;
1594
 
      last= (Select_Lex_Unit**)&(u->next);
 
1756
      last= (SELECT_LEX_UNIT**)&(u->next);
1595
1757
    }
1596
1758
    if (last)
1597
1759
    {
1603
1765
  {
1604
1766
    // include brought up levels in place of current
1605
1767
    (*prev)= units;
1606
 
    (*units_last)= (Select_Lex_Unit*)next;
 
1768
    (*units_last)= (SELECT_LEX_UNIT*)next;
1607
1769
    if (next)
1608
 
      next->prev= (Select_Lex_Node**)units_last;
 
1770
      next->prev= (SELECT_LEX_NODE**)units_last;
1609
1771
    units->prev= prev;
1610
1772
  }
1611
1773
  else
1622
1784
  Exclude subtree of current unit from tree of SELECTs
1623
1785
 
1624
1786
  SYNOPSYS
1625
 
    Select_Lex_Unit::exclude_tree()
 
1787
    st_select_lex_unit::exclude_tree()
1626
1788
*/
1627
 
void Select_Lex_Unit::exclude_tree()
 
1789
void st_select_lex_unit::exclude_tree()
1628
1790
{
1629
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1791
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1630
1792
  {
1631
1793
    // unlink current level from global SELECTs list
1632
1794
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1633
1795
      sl->link_next->link_prev= sl->link_prev;
1634
1796
 
1635
1797
    // unlink underlay levels
1636
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1798
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1637
1799
    {
1638
1800
      u->exclude_level();
1639
1801
    }
1646
1808
 
1647
1809
 
1648
1810
/*
1649
 
  Select_Lex_Node::mark_as_dependent mark all Select_Lex struct from
 
1811
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
1650
1812
  this to 'last' as dependent
1651
1813
 
1652
1814
  SYNOPSIS
1653
 
    last - pointer to last Select_Lex struct, before wich all
1654
 
           Select_Lex have to be marked as dependent
 
1815
    last - pointer to last st_select_lex struct, before wich all 
 
1816
           st_select_lex have to be marked as dependent
1655
1817
 
1656
1818
  NOTE
1657
 
    'last' should be reachable from this Select_Lex_Node
 
1819
    'last' should be reachable from this st_select_lex_node
1658
1820
*/
1659
1821
 
1660
 
void Select_Lex::mark_as_dependent(Select_Lex *last)
 
1822
void st_select_lex::mark_as_dependent(st_select_lex *last)
1661
1823
{
1662
1824
  /*
1663
1825
    Mark all selects from resolved to 1 before select where was
1664
1826
    found table as depended (of select where was found table)
1665
1827
  */
1666
 
  for (Select_Lex *s= this;
 
1828
  for (SELECT_LEX *s= this;
1667
1829
       s && s != last;
1668
1830
       s= s->outer_select())
1669
1831
  {
1672
1834
      // Select is dependent of outer select
1673
1835
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1674
1836
                       UNCACHEABLE_DEPENDENT;
1675
 
      Select_Lex_Unit *munit= s->master_unit();
 
1837
      SELECT_LEX_UNIT *munit= s->master_unit();
1676
1838
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1677
1839
                       UNCACHEABLE_DEPENDENT;
1678
 
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1840
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1679
1841
      {
1680
1842
        if (sl != s &&
1681
1843
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1682
1844
          sl->uncacheable|= UNCACHEABLE_UNITED;
1683
1845
      }
1684
1846
    }
1685
 
    s->is_correlated= true;
 
1847
    s->is_correlated= TRUE;
1686
1848
    Item_subselect *subquery_predicate= s->master_unit()->item;
1687
1849
    if (subquery_predicate)
1688
 
      subquery_predicate->is_correlated= true;
 
1850
      subquery_predicate->is_correlated= TRUE;
1689
1851
  }
1690
1852
}
1691
1853
 
1692
 
bool Select_Lex_Node::set_braces(bool)
1693
 
{ return 1; }
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 *)
 
1854
bool st_select_lex_node::set_braces(bool value)      { return 1; }
 
1855
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
 
1856
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1857
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
 
1858
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
 
1859
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
 
1860
                                                  LEX_STRING *alias,
 
1861
                                                  ulong table_join_options,
 
1862
                                                  thr_lock_type flags,
 
1863
                                                  List<Index_hint> *hints,
 
1864
                                                  LEX_STRING *option)
1700
1865
{
1701
1866
  return 0;
1702
1867
}
1703
 
uint32_t Select_Lex_Node::get_table_join_options()
 
1868
ulong st_select_lex_node::get_table_join_options()
1704
1869
{
1705
1870
  return 0;
1706
1871
}
1708
1873
/*
1709
1874
  prohibit using LIMIT clause
1710
1875
*/
1711
 
bool Select_Lex::test_limit()
 
1876
bool st_select_lex::test_limit()
1712
1877
{
1713
1878
  if (select_limit != 0)
1714
1879
  {
1720
1885
}
1721
1886
 
1722
1887
 
1723
 
Select_Lex_Unit* Select_Lex_Unit::master_unit()
 
1888
st_select_lex_unit* st_select_lex_unit::master_unit()
1724
1889
{
1725
1890
    return this;
1726
1891
}
1727
1892
 
1728
1893
 
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)
1742
 
{
1743
 
  return(item_list.push_back(item));
1744
 
}
1745
 
 
1746
 
 
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
st_select_lex* st_select_lex_unit::outer_select()
 
1895
{
 
1896
  return (st_select_lex*) master;
 
1897
}
 
1898
 
 
1899
 
 
1900
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1901
{
 
1902
  return add_to_list(thd, order_list, item, asc);
 
1903
}
 
1904
 
 
1905
 
 
1906
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
 
1907
{
 
1908
  DBUG_ENTER("st_select_lex::add_item_to_list");
 
1909
  DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
 
1910
  DBUG_RETURN(item_list.push_back(item));
 
1911
}
 
1912
 
 
1913
 
 
1914
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1915
{
 
1916
  return add_to_list(thd, group_list, item, asc);
 
1917
}
 
1918
 
 
1919
 
 
1920
st_select_lex_unit* st_select_lex::master_unit()
 
1921
{
 
1922
  return (st_select_lex_unit*) master;
 
1923
}
 
1924
 
 
1925
 
 
1926
st_select_lex* st_select_lex::outer_select()
 
1927
{
 
1928
  return (st_select_lex*) master->get_master();
 
1929
}
 
1930
 
 
1931
 
 
1932
bool st_select_lex::set_braces(bool value)
1766
1933
{
1767
1934
  braces= value;
1768
 
  return 0;
 
1935
  return 0; 
1769
1936
}
1770
1937
 
1771
1938
 
1772
 
bool Select_Lex::inc_in_sum_expr()
 
1939
bool st_select_lex::inc_in_sum_expr()
1773
1940
{
1774
1941
  in_sum_expr++;
1775
1942
  return 0;
1776
1943
}
1777
1944
 
1778
1945
 
1779
 
uint32_t Select_Lex::get_in_sum_expr()
 
1946
uint st_select_lex::get_in_sum_expr()
1780
1947
{
1781
1948
  return in_sum_expr;
1782
1949
}
1783
1950
 
1784
1951
 
1785
 
TableList* Select_Lex::get_table_list()
 
1952
TABLE_LIST* st_select_lex::get_table_list()
1786
1953
{
1787
 
  return (TableList*) table_list.first;
 
1954
  return (TABLE_LIST*) table_list.first;
1788
1955
}
1789
1956
 
1790
 
List<Item>* Select_Lex::get_item_list()
 
1957
List<Item>* st_select_lex::get_item_list()
1791
1958
{
1792
1959
  return &item_list;
1793
1960
}
1794
1961
 
1795
 
uint32_t Select_Lex::get_table_join_options()
 
1962
ulong st_select_lex::get_table_join_options()
1796
1963
{
1797
1964
  return table_join_options;
1798
1965
}
1799
1966
 
1800
1967
 
1801
 
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
 
1968
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1802
1969
{
1803
1970
  if (ref_pointer_array)
1804
1971
    return 0;
1805
1972
 
 
1973
  /*
 
1974
    We have to create array in prepared statement memory if it is
 
1975
    prepared statement
 
1976
  */
 
1977
  Query_arena *arena= thd->stmt_arena;
1806
1978
  return (ref_pointer_array=
1807
 
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
 
1979
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1808
1980
                                                 item_list.elements +
1809
1981
                                                 select_n_having_items +
1810
1982
                                                 select_n_where_fields +
1812
1984
}
1813
1985
 
1814
1986
 
1815
 
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
 
1987
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1816
1988
{
1817
1989
  bool union_all= !union_distinct;
1818
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1990
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1819
1991
  {
1820
1992
    if (sl != first_select())
1821
1993
    {
1823
1995
      if (union_all)
1824
1996
        str->append(STRING_WITH_LEN("all "));
1825
1997
      else if (union_distinct == sl)
1826
 
        union_all= true;
 
1998
        union_all= TRUE;
1827
1999
    }
1828
2000
    if (sl->braces)
1829
2001
      str->append('(');
1830
 
    sl->print(session, str, query_type);
 
2002
    sl->print(thd, str, query_type);
1831
2003
    if (sl->braces)
1832
2004
      str->append(')');
1833
2005
  }
1838
2010
      str->append(STRING_WITH_LEN(" order by "));
1839
2011
      fake_select_lex->print_order(
1840
2012
        str,
1841
 
        (order_st *) fake_select_lex->order_list.first,
 
2013
        (ORDER *) fake_select_lex->order_list.first,
1842
2014
        query_type);
1843
2015
    }
1844
 
    fake_select_lex->print_limit(session, str, query_type);
 
2016
    fake_select_lex->print_limit(thd, str, query_type);
1845
2017
  }
1846
2018
}
1847
2019
 
1848
2020
 
1849
 
void Select_Lex::print_order(String *str,
1850
 
                                order_st *order,
 
2021
void st_select_lex::print_order(String *str,
 
2022
                                ORDER *order,
1851
2023
                                enum_query_type query_type)
1852
2024
{
1853
2025
  for (; order; order= order->next)
1855
2027
    if (order->counter_used)
1856
2028
    {
1857
2029
      char buffer[20];
1858
 
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
 
2030
      uint length= my_snprintf(buffer, 20, "%d", order->counter);
1859
2031
      str->append(buffer, length);
1860
2032
    }
1861
2033
    else
1866
2038
      str->append(',');
1867
2039
  }
1868
2040
}
1869
 
 
1870
 
 
1871
 
void Select_Lex::print_limit(Session *, String *str,
 
2041
 
 
2042
 
 
2043
void st_select_lex::print_limit(THD *thd,
 
2044
                                String *str,
1872
2045
                                enum_query_type query_type)
1873
2046
{
1874
 
  Select_Lex_Unit *unit= master_unit();
 
2047
  SELECT_LEX_UNIT *unit= master_unit();
1875
2048
  Item_subselect *item= unit->item;
1876
2049
 
1877
2050
  if (item && unit->global_parameters == this)
1881
2054
        subs_type == Item_subselect::IN_SUBS ||
1882
2055
        subs_type == Item_subselect::ALL_SUBS)
1883
2056
    {
1884
 
      assert(!item->fixed ||
 
2057
      DBUG_ASSERT(!item->fixed ||
1885
2058
                  /*
1886
2059
                    If not using materialization both:
1887
2060
                    select_limit == 1, and there should be no offset_limit.
1889
2062
                  (((subs_type == Item_subselect::IN_SUBS) &&
1890
2063
                    ((Item_in_subselect*)item)->exec_method ==
1891
2064
                    Item_in_subselect::MATERIALIZATION) ?
1892
 
                   true :
1893
 
                   (select_limit->val_int() == 1L) &&
 
2065
                   TRUE :
 
2066
                   (select_limit->val_int() == LL(1)) &&
1894
2067
                   offset_limit == 0));
1895
2068
      return;
1896
2069
    }
1908
2081
}
1909
2082
 
1910
2083
/**
1911
 
  @brief Restore the LEX and Session in case of a parse error.
 
2084
  @brief Restore the LEX and THD in case of a parse error.
1912
2085
 
1913
2086
  This is a clean up call that is invoked by the Bison generated
1914
 
  parser before returning an error from DRIZZLEparse. If your
 
2087
  parser before returning an error from MYSQLparse. If your
1915
2088
  semantic actions manipulate with the global thread state (which
1916
2089
  is a very bad practice and should not normally be employed) and
1917
2090
  need a clean-up in case of error, and you can not use %destructor
1919
2092
  to implement the clean up.
1920
2093
*/
1921
2094
 
1922
 
void LEX::cleanup_lex_after_parse_error(Session *)
 
2095
void st_lex::cleanup_lex_after_parse_error(THD *thd)
1923
2096
{
1924
2097
}
1925
2098
 
1928
2101
 
1929
2102
  SYNOPSIS
1930
2103
    reset_query_tables_list()
1931
 
      init  true  - we should perform full initialization of object with
 
2104
      init  TRUE  - we should perform full initialization of object with
1932
2105
                    allocating needed memory
1933
 
            false - object is already initialized so we should only reset
 
2106
            FALSE - object is already initialized so we should only reset
1934
2107
                    its state so it can be used for parsing/processing
1935
2108
                    of new statement
1936
2109
 
1945
2118
{
1946
2119
  if (!init && query_tables)
1947
2120
  {
1948
 
    TableList *table= query_tables;
 
2121
    TABLE_LIST *table= query_tables;
1949
2122
    for (;;)
1950
2123
    {
1951
2124
      if (query_tables_last == &table->next_global ||
1956
2129
  query_tables= 0;
1957
2130
  query_tables_last= &query_tables;
1958
2131
  query_tables_own_last= 0;
 
2132
  if (init)
 
2133
  {
 
2134
    /*
 
2135
      We delay real initialization of hash (and therefore related
 
2136
      memory allocation) until first insertion into this hash.
 
2137
    */
 
2138
    hash_clear(&sroutines);
 
2139
  }
 
2140
  else if (sroutines.records)
 
2141
  {
 
2142
    /* Non-zero sroutines.records means that hash was initialized. */
 
2143
    my_hash_reset(&sroutines);
 
2144
  }
 
2145
  sroutines_list.empty();
 
2146
  sroutines_list_own_last= sroutines_list.next;
 
2147
  sroutines_list_own_elements= 0;
 
2148
  binlog_stmt_flags= 0;
1959
2149
}
1960
2150
 
1961
2151
 
1968
2158
 
1969
2159
void Query_tables_list::destroy_query_tables_list()
1970
2160
{
 
2161
  hash_free(&sroutines);
1971
2162
}
1972
2163
 
1973
2164
 
1975
2166
  Initialize LEX object.
1976
2167
 
1977
2168
  SYNOPSIS
1978
 
    LEX::LEX()
 
2169
    st_lex::st_lex()
1979
2170
 
1980
2171
  NOTE
1981
2172
    LEX object initialized with this constructor can be used as part of
1982
 
    Session object for which one can safely call open_tables(), lock_tables()
 
2173
    THD object for which one can safely call open_tables(), lock_tables()
1983
2174
    and close_thread_tables() functions. But it is not yet ready for
1984
2175
    statement parsing. On should use lex_start() function to prepare LEX
1985
2176
    for this.
1986
2177
*/
1987
2178
 
1988
 
LEX::LEX()
 
2179
st_lex::st_lex()
1989
2180
  :result(0), yacc_yyss(0), yacc_yyvs(0),
1990
2181
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1991
2182
{
1992
2183
 
1993
 
  reset_query_tables_list(true);
 
2184
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
 
2185
                         plugins_static_buffer,
 
2186
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
 
2187
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
 
2188
  reset_query_tables_list(TRUE);
 
2189
}
 
2190
 
 
2191
 
 
2192
/*
 
2193
  Check whether the merging algorithm can be used on this VIEW
 
2194
 
 
2195
  SYNOPSIS
 
2196
    st_lex::can_be_merged()
 
2197
 
 
2198
  DESCRIPTION
 
2199
    We can apply merge algorithm if it is single SELECT view  with
 
2200
    subqueries only in WHERE clause (we do not count SELECTs of underlying
 
2201
    views, and second level subqueries) and we have not grpouping, ordering,
 
2202
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
 
2203
    several underlying tables.
 
2204
 
 
2205
  RETURN
 
2206
    FALSE - only temporary table algorithm can be used
 
2207
    TRUE  - merge algorithm can be used
 
2208
*/
 
2209
 
 
2210
bool st_lex::can_be_merged()
 
2211
{
 
2212
  // TODO: do not forget implement case when select_lex.table_list.elements==0
 
2213
 
 
2214
  /* find non VIEW subqueries/unions */
 
2215
  bool selects_allow_merge= select_lex.next_select() == 0;
 
2216
  if (selects_allow_merge)
 
2217
  {
 
2218
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
 
2219
         tmp_unit;
 
2220
         tmp_unit= tmp_unit->next_unit())
 
2221
    {
 
2222
      if (tmp_unit->first_select()->parent_lex == this &&
 
2223
          (tmp_unit->item == 0 ||
 
2224
           (tmp_unit->item->place() != IN_WHERE &&
 
2225
            tmp_unit->item->place() != IN_ON)))
 
2226
      {
 
2227
        selects_allow_merge= 0;
 
2228
        break;
 
2229
      }
 
2230
    }
 
2231
  }
 
2232
 
 
2233
  return (selects_allow_merge &&
 
2234
          select_lex.group_list.elements == 0 &&
 
2235
          select_lex.having == 0 &&
 
2236
          select_lex.with_sum_func == 0 &&
 
2237
          select_lex.table_list.elements >= 1 &&
 
2238
          !(select_lex.options & SELECT_DISTINCT) &&
 
2239
          select_lex.select_limit == 0);
 
2240
}
 
2241
 
 
2242
 
 
2243
/*
 
2244
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
 
2245
 
 
2246
  SYNOPSIS
 
2247
    st_lex::can_use_merged()
 
2248
 
 
2249
  DESCRIPTION
 
2250
    Only listed here commands can use merge algorithm in top level
 
2251
    SELECT_LEX (for subqueries will be used merge algorithm if
 
2252
    st_lex::can_not_use_merged() is not TRUE).
 
2253
 
 
2254
  RETURN
 
2255
    FALSE - command can't use merged VIEWs
 
2256
    TRUE  - VIEWs with MERGE algorithms can be used
 
2257
*/
 
2258
 
 
2259
bool st_lex::can_use_merged()
 
2260
{
 
2261
  switch (sql_command)
 
2262
  {
 
2263
  case SQLCOM_SELECT:
 
2264
  case SQLCOM_CREATE_TABLE:
 
2265
  case SQLCOM_UPDATE:
 
2266
  case SQLCOM_UPDATE_MULTI:
 
2267
  case SQLCOM_DELETE:
 
2268
  case SQLCOM_DELETE_MULTI:
 
2269
  case SQLCOM_INSERT:
 
2270
  case SQLCOM_INSERT_SELECT:
 
2271
  case SQLCOM_REPLACE:
 
2272
  case SQLCOM_REPLACE_SELECT:
 
2273
  case SQLCOM_LOAD:
 
2274
    return TRUE;
 
2275
  default:
 
2276
    return FALSE;
 
2277
  }
 
2278
}
 
2279
 
 
2280
/*
 
2281
  Check if command can't use merged views in any part of command
 
2282
 
 
2283
  SYNOPSIS
 
2284
    st_lex::can_not_use_merged()
 
2285
 
 
2286
  DESCRIPTION
 
2287
    Temporary table algorithm will be used on all SELECT levels for queries
 
2288
    listed here (see also st_lex::can_use_merged()).
 
2289
 
 
2290
  RETURN
 
2291
    FALSE - command can't use merged VIEWs
 
2292
    TRUE  - VIEWs with MERGE algorithms can be used
 
2293
*/
 
2294
 
 
2295
bool st_lex::can_not_use_merged()
 
2296
{
 
2297
  switch (sql_command)
 
2298
  {
 
2299
  /*
 
2300
    SQLCOM_SHOW_FIELDS is necessary to make 
 
2301
    information schema tables working correctly with views.
 
2302
    see get_schema_tables_result function
 
2303
  */
 
2304
  case SQLCOM_SHOW_FIELDS:
 
2305
    return TRUE;
 
2306
  default:
 
2307
    return FALSE;
 
2308
  }
1994
2309
}
1995
2310
 
1996
2311
/*
2000
2315
    only_view_structure()
2001
2316
 
2002
2317
  RETURN
2003
 
    true yes, we need only structure
2004
 
    false no, we need data
 
2318
    TRUE yes, we need only structure
 
2319
    FALSE no, we need data
2005
2320
*/
2006
2321
 
2007
 
bool LEX::only_view_structure()
 
2322
bool st_lex::only_view_structure()
2008
2323
{
2009
2324
  switch (sql_command) {
2010
2325
  case SQLCOM_SHOW_CREATE:
2011
2326
  case SQLCOM_SHOW_TABLES:
2012
2327
  case SQLCOM_SHOW_FIELDS:
2013
 
    return true;
 
2328
    return TRUE;
2014
2329
  default:
2015
 
    return false;
 
2330
    return FALSE;
2016
2331
  }
2017
2332
}
2018
2333
 
2024
2339
    need_correct_ident()
2025
2340
 
2026
2341
  RETURN
2027
 
    true yes, we need only structure
2028
 
    false no, we need data
 
2342
    TRUE yes, we need only structure
 
2343
    FALSE no, we need data
2029
2344
*/
2030
2345
 
2031
2346
 
2032
 
bool LEX::need_correct_ident()
 
2347
bool st_lex::need_correct_ident()
2033
2348
{
2034
2349
  switch(sql_command)
2035
2350
  {
2036
2351
  case SQLCOM_SHOW_CREATE:
2037
2352
  case SQLCOM_SHOW_TABLES:
2038
 
    return true;
 
2353
    return TRUE;
2039
2354
  default:
2040
 
    return false;
 
2355
    return FALSE;
2041
2356
  }
2042
2357
}
2043
2358
 
 
2359
/*
 
2360
  Get effective type of CHECK OPTION for given view
 
2361
 
 
2362
  SYNOPSIS
 
2363
    get_effective_with_check()
 
2364
    view    given view
 
2365
 
 
2366
  NOTE
 
2367
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
 
2368
    so we do not.
 
2369
 
 
2370
  RETURN
 
2371
    VIEW_CHECK_NONE      no need CHECK OPTION
 
2372
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
 
2373
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
 
2374
*/
 
2375
 
 
2376
uint8 st_lex::get_effective_with_check(TABLE_LIST *view)
 
2377
{
 
2378
  return 0;
 
2379
}
 
2380
 
2044
2381
 
2045
2382
/**
2046
2383
  This method should be called only during parsing.
2048
2385
  and will initialize the destination with the default
2049
2386
  database of the stored routine, rather than the default
2050
2387
  database of the connection it is parsed in.
2051
 
  E.g. if one has no current database selected, or current database
 
2388
  E.g. if one has no current database selected, or current database 
2052
2389
  set to 'bar' and then issues:
2053
2390
 
2054
2391
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2057
2394
 
2058
2395
  This method is needed to support this rule.
2059
2396
 
2060
 
  @return true in case of error (parsing should be aborted, false in
 
2397
  @return TRUE in case of error (parsing should be aborted, FALSE in
2061
2398
  case of success
2062
2399
*/
2063
2400
 
2064
2401
bool
2065
 
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
 
2402
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
2066
2403
{
2067
 
  return session->copy_db_to(p_db, p_db_length);
 
2404
  return thd->copy_db_to(p_db, p_db_length);
2068
2405
}
2069
2406
 
2070
2407
/*
2071
2408
  initialize limit counters
2072
2409
 
2073
2410
  SYNOPSIS
2074
 
    Select_Lex_Unit::set_limit()
2075
 
    values      - Select_Lex with initial values for counters
 
2411
    st_select_lex_unit::set_limit()
 
2412
    values      - SELECT_LEX with initial values for counters
2076
2413
*/
2077
2414
 
2078
 
void Select_Lex_Unit::set_limit(Select_Lex *sl)
 
2415
void st_select_lex_unit::set_limit(st_select_lex *sl)
2079
2416
{
2080
2417
  ha_rows select_limit_val;
2081
 
  uint64_t val;
 
2418
  ulonglong val;
2082
2419
 
2083
2420
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2084
2421
  select_limit_val= (ha_rows)val;
2085
 
  /*
2086
 
    Check for overflow : ha_rows can be smaller then uint64_t if
 
2422
#ifndef BIG_TABLES
 
2423
  /* 
 
2424
    Check for overflow : ha_rows can be smaller then ulonglong if
2087
2425
    BIG_TABLES is off.
2088
2426
    */
2089
 
  if (val != (uint64_t)select_limit_val)
 
2427
  if (val != (ulonglong)select_limit_val)
2090
2428
    select_limit_val= HA_POS_ERROR;
 
2429
#endif
2091
2430
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2092
 
                                                 0UL);
 
2431
                                                 ULL(0));
2093
2432
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2094
2433
  if (select_limit_cnt < select_limit_val)
2095
2434
    select_limit_cnt= HA_POS_ERROR;             // no limit
2114
2453
      In this case link_to_local is set.
2115
2454
 
2116
2455
*/
2117
 
TableList *LEX::unlink_first_table(bool *link_to_local)
 
2456
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
2118
2457
{
2119
 
  TableList *first;
 
2458
  TABLE_LIST *first;
2120
2459
  if ((first= query_tables))
2121
2460
  {
2122
2461
    /*
2133
2472
    */
2134
2473
    if ((*link_to_local= test(select_lex.table_list.first)))
2135
2474
    {
2136
 
      select_lex.context.table_list=
 
2475
      select_lex.context.table_list= 
2137
2476
        select_lex.context.first_name_resolution_table= first->next_local;
2138
 
      select_lex.table_list.first= (unsigned char*) (first->next_local);
 
2477
      select_lex.table_list.first= (uchar*) (first->next_local);
2139
2478
      select_lex.table_list.elements--; //safety
2140
2479
      first->next_local= 0;
2141
2480
      /*
2154
2493
  table list
2155
2494
 
2156
2495
  SYNOPSYS
2157
 
     LEX::first_lists_tables_same()
 
2496
     st_lex::first_lists_tables_same()
2158
2497
 
2159
2498
  NOTES
2160
2499
    In many cases (for example, usual INSERT/DELETE/...) the first table of
2161
 
    main Select_Lex have special meaning => check that it is the first table
 
2500
    main SELECT_LEX have special meaning => check that it is the first table
2162
2501
    in global list and re-link to be first in the global list if it is
2163
2502
    necessary.  We need such re-linking only for queries with sub-queries in
2164
2503
    the select list, as only in this case tables of sub-queries will go to
2165
2504
    the global list first.
2166
2505
*/
2167
2506
 
2168
 
void LEX::first_lists_tables_same()
 
2507
void st_lex::first_lists_tables_same()
2169
2508
{
2170
 
  TableList *first_table= (TableList*) select_lex.table_list.first;
 
2509
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
2171
2510
  if (query_tables != first_table && first_table != 0)
2172
2511
  {
2173
 
    TableList *next;
 
2512
    TABLE_LIST *next;
2174
2513
    if (query_tables_last == &first_table->next_global)
2175
2514
      query_tables_last= first_table->prev_global;
2176
2515
 
2201
2540
    global list
2202
2541
*/
2203
2542
 
2204
 
void LEX::link_first_table_back(TableList *first,
 
2543
void st_lex::link_first_table_back(TABLE_LIST *first,
2205
2544
                                   bool link_to_local)
2206
2545
{
2207
2546
  if (first)
2214
2553
 
2215
2554
    if (link_to_local)
2216
2555
    {
2217
 
      first->next_local= (TableList*) select_lex.table_list.first;
 
2556
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
2218
2557
      select_lex.context.table_list= first;
2219
 
      select_lex.table_list.first= (unsigned char*) first;
 
2558
      select_lex.table_list.first= (uchar*) first;
2220
2559
      select_lex.table_list.elements++; //safety
2221
2560
    }
2222
2561
  }
2228
2567
  cleanup lex for case when we open table by table for processing
2229
2568
 
2230
2569
  SYNOPSIS
2231
 
    LEX::cleanup_after_one_table_open()
 
2570
    st_lex::cleanup_after_one_table_open()
2232
2571
 
2233
2572
  NOTE
2234
2573
    This method is mostly responsible for cleaning up of selects lists and
2235
2574
    derived tables state. To rollback changes in Query_tables_list one has
2236
 
    to call Query_tables_list::reset_query_tables_list(false).
 
2575
    to call Query_tables_list::reset_query_tables_list(FALSE).
2237
2576
*/
2238
2577
 
2239
 
void LEX::cleanup_after_one_table_open()
 
2578
void st_lex::cleanup_after_one_table_open()
2240
2579
{
2241
2580
  /*
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
 
2581
    thd->lex->derived_tables & additional units may be set if we open
 
2582
    a view. It is necessary to clear thd->lex->derived_tables flag
2244
2583
    to prevent processing of derived tables during next open_and_lock_tables
2245
2584
    if next table is a real table and cleanup & remove underlying units
2246
 
    NOTE: all units will be connected to session->lex->select_lex, because we
 
2585
    NOTE: all units will be connected to thd->lex->select_lex, because we
2247
2586
    have not UNION on most upper level.
2248
2587
    */
2249
2588
  if (all_selects_list != &select_lex)
2250
2589
  {
2251
2590
    derived_tables= 0;
2252
2591
    /* cleunup underlying units (units of VIEW) */
2253
 
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
 
2592
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
2254
2593
         un;
2255
2594
         un= un->next_unit())
2256
2595
      un->cleanup();
2263
2602
 
2264
2603
 
2265
2604
/*
 
2605
  Save current state of Query_tables_list for this LEX, and prepare it
 
2606
  for processing of new statemnt.
 
2607
 
 
2608
  SYNOPSIS
 
2609
    reset_n_backup_query_tables_list()
 
2610
      backup  Pointer to Query_tables_list instance to be used for backup
 
2611
*/
 
2612
 
 
2613
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup)
 
2614
{
 
2615
}
 
2616
 
 
2617
 
 
2618
/*
 
2619
  Restore state of Query_tables_list for this LEX from backup.
 
2620
 
 
2621
  SYNOPSIS
 
2622
    restore_backup_query_tables_list()
 
2623
      backup  Pointer to Query_tables_list instance used for backup
 
2624
*/
 
2625
 
 
2626
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup)
 
2627
{
 
2628
}
 
2629
 
 
2630
 
 
2631
/*
 
2632
  Checks for usage of routines and/or tables in a parsed statement
 
2633
 
 
2634
  SYNOPSIS
 
2635
    st_lex:table_or_sp_used()
 
2636
 
 
2637
  RETURN
 
2638
    FALSE  No routines and tables used
 
2639
    TRUE   Either or both routines and tables are used.
 
2640
*/
 
2641
 
 
2642
bool st_lex::table_or_sp_used()
 
2643
{
 
2644
  DBUG_ENTER("table_or_sp_used");
 
2645
 
 
2646
  if (sroutines.records || query_tables)
 
2647
    DBUG_RETURN(TRUE);
 
2648
 
 
2649
  DBUG_RETURN(FALSE);
 
2650
}
 
2651
 
 
2652
 
 
2653
/*
2266
2654
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2267
2655
 
2268
2656
  SYNOPSIS
2269
2657
    fix_prepare_info_in_table_list()
2270
 
      session  Thread handle
 
2658
      thd  Thread handle
2271
2659
      tbl  List of tables to process
2272
2660
 
2273
2661
  DESCRIPTION
2277
2665
 
2278
2666
*/
2279
2667
 
2280
 
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
 
2668
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
2281
2669
{
2282
2670
  for (; tbl; tbl= tbl->next_local)
2283
2671
  {
2284
2672
    if (tbl->on_expr)
2285
2673
    {
2286
2674
      tbl->prep_on_expr= tbl->on_expr;
2287
 
      tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
2288
 
    }
2289
 
    fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
2290
 
  }
2291
 
}
2292
 
 
2293
 
 
2294
 
/*
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
 
2675
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2676
    }
 
2677
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2678
  }
 
2679
}
 
2680
 
 
2681
 
 
2682
/*
 
2683
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
 
2684
 
 
2685
  SYNOPSIS
 
2686
    st_select_lex::fix_prepare_information
 
2687
      thd          thread handler
 
2688
      conds        in/out pointer to WHERE condition to be met at execution
 
2689
      having_conds in/out pointer to HAVING condition to be met at execution
 
2690
  
 
2691
  DESCRIPTION
 
2692
    The passed WHERE and HAVING are to be saved for the future executions.
 
2693
    This function saves it, and returns a copy which can be thrashed during
 
2694
    this execution of the statement. By saving/thrashing here we mean only
 
2695
    AND/OR trees.
 
2696
    The function also calls fix_prepare_info_in_table_list that saves all
 
2697
    ON expressions.    
 
2698
*/
 
2699
 
 
2700
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
 
2701
                                            Item **having_conds)
 
2702
{
 
2703
  if (thd->stmt_arena->is_conventional() == false && first_execution)
 
2704
  {
 
2705
    first_execution= 0;
 
2706
    if (*conds)
 
2707
    {
 
2708
      prep_where= *conds;
 
2709
      *conds= where= prep_where->copy_andor_structure(thd);
 
2710
    }
 
2711
    if (*having_conds)
 
2712
    {
 
2713
      prep_having= *having_conds;
 
2714
      *having_conds= having= prep_having->copy_andor_structure(thd);
 
2715
    }
 
2716
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
 
2717
  }
 
2718
}
 
2719
 
 
2720
 
 
2721
/*
 
2722
  There are st_select_lex::add_table_to_list &
 
2723
  st_select_lex::set_lock_for_tables are in sql_parse.cc
 
2724
 
 
2725
  st_select_lex::print is in sql_select.cc
 
2726
 
 
2727
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
 
2728
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
 
2729
  st_select_lex_unit::change_result
2303
2730
  are in sql_union.cc
2304
2731
*/
2305
2732
 
2313
2740
 
2314
2741
  DESCRIPTION
2315
2742
    Used in filling up the tagged hints list.
2316
 
    This list is filled by first setting the kind of the hint as a
 
2743
    This list is filled by first setting the kind of the hint as a 
2317
2744
    context variable and then adding hints of the current kind.
2318
2745
    Then the context variable index_hint_type can be reset to the
2319
2746
    next hint type.
2320
2747
*/
2321
 
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg,
 
2748
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
2322
2749
                                        index_clause_map clause)
2323
 
{
 
2750
2324
2751
  current_index_hint_type= type_arg;
2325
2752
  current_index_hint_clause= clause;
2326
2753
}
2331
2758
 
2332
2759
  SYNOPSIS
2333
2760
    alloc_index_hints()
2334
 
      session         current thread.
 
2761
      thd         current thread.
2335
2762
*/
2336
2763
 
2337
 
void Select_Lex::alloc_index_hints (Session *session)
2338
 
{
2339
 
  index_hints= new (session->mem_root) List<Index_hint>();
 
2764
void st_select_lex::alloc_index_hints (THD *thd)
 
2765
 
2766
  index_hints= new (thd->mem_root) List<Index_hint>(); 
2340
2767
}
2341
2768
 
2342
2769
 
2343
2770
 
2344
2771
/*
2345
 
  adds an element to the array storing index usage hints
 
2772
  adds an element to the array storing index usage hints 
2346
2773
  (ADD/FORCE/IGNORE INDEX).
2347
2774
 
2348
2775
  SYNOPSIS
2349
2776
    add_index_hint()
2350
 
      session         current thread.
 
2777
      thd         current thread.
2351
2778
      str         name of the index.
2352
2779
      length      number of characters in str.
2353
2780
 
2354
2781
  RETURN VALUE
2355
2782
    0 on success, non-zero otherwise
2356
2783
*/
2357
 
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
 
2784
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2358
2785
{
2359
 
  return index_hints->push_front (new (session->mem_root)
 
2786
  return index_hints->push_front (new (thd->mem_root) 
2360
2787
                                 Index_hint(current_index_hint_type,
2361
2788
                                            current_index_hint_clause,
2362
2789
                                            str, length));