~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Monty Taylor
  • Date: 2008-08-16 21:06:22 UTC
  • Revision ID: monty@inaugust.com-20080816210622-zpnn13unyinqzn72
Updated po files.

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