~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/sql_lex.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
/* A lexical scanner on a temporary buffer with a yacc interface */
 
18
 
 
19
#define MYSQL_LEX 1
 
20
#include "mysql_priv.h"
 
21
#include "item_create.h"
 
22
#include <m_ctype.h>
 
23
#include <hash.h>
 
24
#include "sp.h"
 
25
#include "sp_head.h"
 
26
 
 
27
/*
 
28
  We are using pointer to this variable for distinguishing between assignment
 
29
  to NEW row field (when parsing trigger definition) and structured variable.
 
30
*/
 
31
 
 
32
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
 
33
 
 
34
/* Longest standard keyword name */
 
35
#define TOCK_NAME_LENGTH 24
 
36
 
 
37
/*
 
38
  The following data is based on the latin1 character set, and is only
 
39
  used when comparing keywords
 
40
*/
 
41
 
 
42
static uchar to_upper_lex[]=
 
43
{
 
44
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
 
45
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 
46
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 
47
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
 
48
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
49
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
 
50
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
51
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
 
52
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
53
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
 
54
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
 
55
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
 
56
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
57
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
 
58
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
59
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
 
60
};
 
61
 
 
62
/* 
 
63
  Names of the index hints (for error messages). Keep in sync with 
 
64
  index_hint_type 
 
65
*/
 
66
 
 
67
const char * index_hint_type_name[] =
 
68
{
 
69
  "IGNORE INDEX", 
 
70
  "USE INDEX", 
 
71
  "FORCE INDEX"
 
72
};
 
73
 
 
74
inline int lex_casecmp(const char *s, const char *t, uint len)
 
75
{
 
76
  while (len-- != 0 &&
 
77
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
 
78
  return (int) len+1;
 
79
}
 
80
 
 
81
#include <lex_hash.h>
 
82
 
 
83
 
 
84
void lex_init(void)
 
85
{
 
86
  uint i;
 
87
  DBUG_ENTER("lex_init");
 
88
  for (i=0 ; i < array_elements(symbols) ; i++)
 
89
    symbols[i].length=(uchar) strlen(symbols[i].name);
 
90
  for (i=0 ; i < array_elements(sql_functions) ; i++)
 
91
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
 
92
 
 
93
  DBUG_VOID_RETURN;
 
94
}
 
95
 
 
96
 
 
97
void lex_free(void)
 
98
{                                       // Call this when daemon ends
 
99
  DBUG_ENTER("lex_free");
 
100
  DBUG_VOID_RETURN;
 
101
}
 
102
 
 
103
 
 
104
void
 
105
st_parsing_options::reset()
 
106
{
 
107
  allows_variable= TRUE;
 
108
  allows_select_into= TRUE;
 
109
  allows_select_procedure= TRUE;
 
110
  allows_derived= TRUE;
 
111
}
 
112
 
 
113
Lex_input_stream::Lex_input_stream(THD *thd,
 
114
                                   const char* buffer,
 
115
                                   unsigned int length)
 
116
: m_thd(thd),
 
117
  yylineno(1),
 
118
  yytoklen(0),
 
119
  yylval(NULL),
 
120
  m_ptr(buffer),
 
121
  m_tok_start(NULL),
 
122
  m_tok_end(NULL),
 
123
  m_end_of_query(buffer + length),
 
124
  m_tok_start_prev(NULL),
 
125
  m_buf(buffer),
 
126
  m_buf_length(length),
 
127
  m_echo(TRUE),
 
128
  m_cpp_tok_start(NULL),
 
129
  m_cpp_tok_start_prev(NULL),
 
130
  m_cpp_tok_end(NULL),
 
131
  m_body_utf8(NULL),
 
132
  m_cpp_utf8_processed_ptr(NULL),
 
133
  next_state(MY_LEX_START),
 
134
  found_semicolon(NULL),
 
135
  ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)),
 
136
  stmt_prepare_mode(FALSE),
 
137
  in_comment(NO_COMMENT),
 
138
  m_underscore_cs(NULL)
 
139
{
 
140
  m_cpp_buf= (char*) thd->alloc(length + 1);
 
141
  m_cpp_ptr= m_cpp_buf;
 
142
}
 
143
 
 
144
Lex_input_stream::~Lex_input_stream()
 
145
{}
 
146
 
 
147
/**
 
148
  The operation is called from the parser in order to
 
149
  1) designate the intention to have utf8 body;
 
150
  1) Indicate to the lexer that we will need a utf8 representation of this
 
151
     statement;
 
152
  2) Determine the beginning of the body.
 
153
 
 
154
  @param thd        Thread context.
 
155
  @param begin_ptr  Pointer to the start of the body in the pre-processed
 
156
                    buffer.
 
157
*/
 
158
 
 
159
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
 
160
{
 
161
  DBUG_ASSERT(begin_ptr);
 
162
  DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
 
163
 
 
164
  uint body_utf8_length=
 
165
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
 
166
    my_charset_utf8_bin.mbmaxlen;
 
167
 
 
168
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
 
169
  m_body_utf8_ptr= m_body_utf8;
 
170
  *m_body_utf8_ptr= 0;
 
171
 
 
172
  m_cpp_utf8_processed_ptr= begin_ptr;
 
173
}
 
174
 
 
175
/**
 
176
  @brief The operation appends unprocessed part of pre-processed buffer till
 
177
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.
 
178
 
 
179
  The idea is that some tokens in the pre-processed buffer (like character
 
180
  set introducers) should be skipped.
 
181
 
 
182
  Example:
 
183
    CPP buffer: SELECT 'str1', _latin1 'str2';
 
184
    m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
 
185
    In order to skip "_latin1", the following call should be made:
 
186
      body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)
 
187
 
 
188
  @param ptr      Pointer in the pre-processed buffer, which specifies the
 
189
                  end of the chunk, which should be appended to the utf8
 
190
                  body.
 
191
  @param end_ptr  Pointer in the pre-processed buffer, to which
 
192
                  m_cpp_utf8_processed_ptr will be set in the end of the
 
193
                  operation.
 
194
*/
 
195
 
 
196
void Lex_input_stream::body_utf8_append(const char *ptr,
 
197
                                        const char *end_ptr)
 
198
{
 
199
  DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
 
200
  DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
 
201
 
 
202
  if (!m_body_utf8)
 
203
    return;
 
204
 
 
205
  if (m_cpp_utf8_processed_ptr >= ptr)
 
206
    return;
 
207
 
 
208
  int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
 
209
 
 
210
  memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
 
211
  m_body_utf8_ptr += bytes_to_copy;
 
212
  *m_body_utf8_ptr= 0;
 
213
 
 
214
  m_cpp_utf8_processed_ptr= end_ptr;
 
215
}
 
216
 
 
217
/**
 
218
  The operation appends unprocessed part of the pre-processed buffer till
 
219
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.
 
220
 
 
221
  @param ptr  Pointer in the pre-processed buffer, which specifies the end
 
222
              of the chunk, which should be appended to the utf8 body.
 
223
*/
 
224
 
 
225
void Lex_input_stream::body_utf8_append(const char *ptr)
 
226
{
 
227
  body_utf8_append(ptr, ptr);
 
228
}
 
229
 
 
230
/**
 
231
  The operation converts the specified text literal to the utf8 and appends
 
232
  the result to the utf8-body.
 
233
 
 
234
  @param thd      Thread context.
 
235
  @param txt      Text literal.
 
236
  @param txt_cs   Character set of the text literal.
 
237
  @param end_ptr  Pointer in the pre-processed buffer, to which
 
238
                  m_cpp_utf8_processed_ptr will be set in the end of the
 
239
                  operation.
 
240
*/
 
241
 
 
242
void Lex_input_stream::body_utf8_append_literal(THD *thd,
 
243
                                                const LEX_STRING *txt,
 
244
                                                CHARSET_INFO *txt_cs,
 
245
                                                const char *end_ptr)
 
246
{
 
247
  if (!m_cpp_utf8_processed_ptr)
 
248
    return;
 
249
 
 
250
  LEX_STRING utf_txt;
 
251
 
 
252
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
 
253
  {
 
254
    thd->convert_string(&utf_txt,
 
255
                        &my_charset_utf8_general_ci,
 
256
                        txt->str, (uint) txt->length,
 
257
                        txt_cs);
 
258
  }
 
259
  else
 
260
  {
 
261
    utf_txt.str= txt->str;
 
262
    utf_txt.length= txt->length;
 
263
  }
 
264
 
 
265
  /* NOTE: utf_txt.length is in bytes, not in symbols. */
 
266
 
 
267
  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
 
268
  m_body_utf8_ptr += utf_txt.length;
 
269
  *m_body_utf8_ptr= 0;
 
270
 
 
271
  m_cpp_utf8_processed_ptr= end_ptr;
 
272
}
 
273
 
 
274
 
 
275
/*
 
276
  This is called before every query that is to be parsed.
 
277
  Because of this, it's critical to not do too much things here.
 
278
  (We already do too much here)
 
279
*/
 
280
 
 
281
void lex_start(THD *thd)
 
282
{
 
283
  LEX *lex= thd->lex;
 
284
  DBUG_ENTER("lex_start");
 
285
 
 
286
  lex->thd= lex->unit.thd= thd;
 
287
 
 
288
  lex->context_stack.empty();
 
289
  lex->unit.init_query();
 
290
  lex->unit.init_select();
 
291
  /* 'parent_lex' is used in init_query() so it must be before it. */
 
292
  lex->select_lex.parent_lex= lex;
 
293
  lex->select_lex.init_query();
 
294
  lex->value_list.empty();
 
295
  lex->update_list.empty();
 
296
  lex->set_var_list.empty();
 
297
  lex->param_list.empty();
 
298
  lex->view_list.empty();
 
299
  lex->prepared_stmt_params.empty();
 
300
  lex->auxiliary_table_list.empty();
 
301
  lex->unit.next= lex->unit.master=
 
302
    lex->unit.link_next= lex->unit.return_to= 0;
 
303
  lex->unit.prev= lex->unit.link_prev= 0;
 
304
  lex->unit.slave= lex->unit.global_parameters= lex->current_select=
 
305
    lex->all_selects_list= &lex->select_lex;
 
306
  lex->select_lex.master= &lex->unit;
 
307
  lex->select_lex.prev= &lex->unit.slave;
 
308
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
 
309
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
 
310
  lex->select_lex.options= 0;
 
311
  lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
 
312
  lex->select_lex.init_order();
 
313
  lex->select_lex.group_list.empty();
 
314
  lex->describe= 0;
 
315
  lex->subqueries= FALSE;
 
316
  lex->view_prepare_mode= FALSE;
 
317
  lex->derived_tables= 0;
 
318
  lex->lock_option= TL_READ;
 
319
  lex->safe_to_cache_query= 1;
 
320
  lex->leaf_tables_insert= 0;
 
321
  lex->parsing_options.reset();
 
322
  lex->empty_field_list_on_rset= 0;
 
323
  lex->select_lex.select_number= 1;
 
324
  lex->length=0;
 
325
  lex->part_info= 0;
 
326
  lex->select_lex.in_sum_expr=0;
 
327
  lex->select_lex.ftfunc_list_alloc.empty();
 
328
  lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
 
329
  lex->select_lex.group_list.empty();
 
330
  lex->select_lex.order_list.empty();
 
331
  lex->sql_command= SQLCOM_END;
 
332
  lex->duplicates= DUP_ERROR;
 
333
  lex->ignore= 0;
 
334
  lex->spname= NULL;
 
335
  lex->sphead= NULL;
 
336
  lex->spcont= NULL;
 
337
  lex->proc_list.first= 0;
 
338
  lex->escape_used= FALSE;
 
339
  lex->query_tables= 0;
 
340
  lex->reset_query_tables_list(FALSE);
 
341
  lex->expr_allows_subselect= TRUE;
 
342
  lex->use_only_table_context= FALSE;
 
343
 
 
344
  lex->name.str= 0;
 
345
  lex->name.length= 0;
 
346
  lex->event_parse_data= NULL;
 
347
  lex->profile_options= PROFILE_NONE;
 
348
  lex->nest_level=0 ;
 
349
  lex->allow_sum_func= 0;
 
350
  lex->in_sum_func= NULL;
 
351
  lex->protect_against_global_read_lock= FALSE;
 
352
  /*
 
353
    ok, there must be a better solution for this, long-term
 
354
    I tried "bzero" in the sql_yacc.yy code, but that for
 
355
    some reason made the values zero, even if they were set
 
356
  */
 
357
  lex->server_options.server_name= 0;
 
358
  lex->server_options.server_name_length= 0;
 
359
  lex->server_options.host= 0;
 
360
  lex->server_options.db= 0;
 
361
  lex->server_options.username= 0;
 
362
  lex->server_options.password= 0;
 
363
  lex->server_options.scheme= 0;
 
364
  lex->server_options.socket= 0;
 
365
  lex->server_options.owner= 0;
 
366
  lex->server_options.port= -1;
 
367
 
 
368
  lex->is_lex_started= TRUE;
 
369
  DBUG_VOID_RETURN;
 
370
}
 
371
 
 
372
void lex_end(LEX *lex)
 
373
{
 
374
  DBUG_ENTER("lex_end");
 
375
  DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
 
376
 
 
377
  /* release used plugins */
 
378
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
 
379
                     lex->plugins.elements);
 
380
  reset_dynamic(&lex->plugins);
 
381
 
 
382
  DBUG_VOID_RETURN;
 
383
}
 
384
 
 
385
Yacc_state::~Yacc_state()
 
386
{
 
387
  if (yacc_yyss)
 
388
  {
 
389
    my_free(yacc_yyss, MYF(0));
 
390
    my_free(yacc_yyvs, MYF(0));
 
391
  }
 
392
}
 
393
 
 
394
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
 
395
{
 
396
  const char *tok= lip->get_tok_start();
 
397
 
 
398
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
 
399
  if (symbol)
 
400
  {
 
401
    lip->yylval->symbol.symbol=symbol;
 
402
    lip->yylval->symbol.str= (char*) tok;
 
403
    lip->yylval->symbol.length=len;
 
404
 
 
405
    if ((symbol->tok == NOT_SYM) &&
 
406
        (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE))
 
407
      return NOT2_SYM;
 
408
    if ((symbol->tok == OR_OR_SYM) &&
 
409
        !(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT))
 
410
      return OR2_SYM;
 
411
 
 
412
    return symbol->tok;
 
413
  }
 
414
  return 0;
 
415
}
 
416
 
 
417
/*
 
418
  Check if name is a keyword
 
419
 
 
420
  SYNOPSIS
 
421
    is_keyword()
 
422
    name      checked name (must not be empty)
 
423
    len       length of checked name
 
424
 
 
425
  RETURN VALUES
 
426
    0         name is a keyword
 
427
    1         name isn't a keyword
 
428
*/
 
429
 
 
430
bool is_keyword(const char *name, uint len)
 
431
{
 
432
  DBUG_ASSERT(len != 0);
 
433
  return get_hash_symbol(name,len,0)!=0;
 
434
}
 
435
 
 
436
/**
 
437
  Check if name is a sql function
 
438
 
 
439
    @param name      checked name
 
440
 
 
441
    @return is this a native function or not
 
442
    @retval 0         name is a function
 
443
    @retval 1         name isn't a function
 
444
*/
 
445
 
 
446
bool is_lex_native_function(const LEX_STRING *name)
 
447
{
 
448
  DBUG_ASSERT(name != NULL);
 
449
  return (get_hash_symbol(name->str, (uint) name->length, 1) != 0);
 
450
}
 
451
 
 
452
/* make a copy of token before ptr and set yytoklen */
 
453
 
 
454
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
 
455
{
 
456
  LEX_STRING tmp;
 
457
  lip->yyUnget();                       // ptr points now after last token char
 
458
  tmp.length=lip->yytoklen=length;
 
459
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
 
460
 
 
461
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
 
462
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
 
463
 
 
464
  return tmp;
 
465
}
 
466
 
 
467
/* 
 
468
 todo: 
 
469
   There are no dangerous charsets in mysql for function 
 
470
   get_quoted_token yet. But it should be fixed in the 
 
471
   future to operate multichar strings (like ucs2)
 
472
*/
 
473
 
 
474
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
 
475
                                   uint skip,
 
476
                                   uint length, char quote)
 
477
{
 
478
  LEX_STRING tmp;
 
479
  const char *from, *end;
 
480
  char *to;
 
481
  lip->yyUnget();                       // ptr points now after last token char
 
482
  tmp.length= lip->yytoklen=length;
 
483
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
 
484
  from= lip->get_tok_start() + skip;
 
485
  to= tmp.str;
 
486
  end= to+length;
 
487
 
 
488
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
 
489
  lip->m_cpp_text_end= lip->m_cpp_text_start + length;
 
490
 
 
491
  for ( ; to != end; )
 
492
  {
 
493
    if ((*to++= *from++) == quote)
 
494
    {
 
495
      from++;                                   // Skip double quotes
 
496
      lip->m_cpp_text_start++;
 
497
    }
 
498
  }
 
499
  *to= 0;                                       // End null for safety
 
500
  return tmp;
 
501
}
 
502
 
 
503
 
 
504
/*
 
505
  Return an unescaped text literal without quotes
 
506
  Fix sometimes to do only one scan of the string
 
507
*/
 
508
 
 
509
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
 
510
{
 
511
  reg1 uchar c,sep;
 
512
  uint found_escape=0;
 
513
  CHARSET_INFO *cs= lip->m_thd->charset();
 
514
 
 
515
  lip->tok_bitmap= 0;
 
516
  sep= lip->yyGetLast();                        // String should end with this
 
517
  while (! lip->eof())
 
518
  {
 
519
    c= lip->yyGet();
 
520
    lip->tok_bitmap|= c;
 
521
#ifdef USE_MB
 
522
    {
 
523
      int l;
 
524
      if (use_mb(cs) &&
 
525
          (l = my_ismbchar(cs,
 
526
                           lip->get_ptr() -1,
 
527
                           lip->get_end_of_query()))) {
 
528
        lip->skip_binary(l-1);
 
529
        continue;
 
530
      }
 
531
    }
 
532
#endif
 
533
    if (c == '\\' &&
 
534
        !(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES))
 
535
    {                                   // Escaped character
 
536
      found_escape=1;
 
537
      if (lip->eof())
 
538
        return 0;
 
539
      lip->yySkip();
 
540
    }
 
541
    else if (c == sep)
 
542
    {
 
543
      if (c == lip->yyGet())            // Check if two separators in a row
 
544
      {
 
545
        found_escape=1;                 // duplicate. Remember for delete
 
546
        continue;
 
547
      }
 
548
      else
 
549
        lip->yyUnget();
 
550
 
 
551
      /* Found end. Unescape and return string */
 
552
      const char *str, *end;
 
553
      char *start;
 
554
 
 
555
      str= lip->get_tok_start();
 
556
      end= lip->get_ptr();
 
557
      /* Extract the text from the token */
 
558
      str += pre_skip;
 
559
      end -= post_skip;
 
560
      DBUG_ASSERT(end >= str);
 
561
 
 
562
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
563
        return (char*) "";              // Sql_alloc has set error flag
 
564
 
 
565
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
 
566
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
 
567
 
 
568
      if (!found_escape)
 
569
      {
 
570
        lip->yytoklen=(uint) (end-str);
 
571
        memcpy(start,str,lip->yytoklen);
 
572
        start[lip->yytoklen]=0;
 
573
      }
 
574
      else
 
575
      {
 
576
        char *to;
 
577
 
 
578
        for (to=start ; str != end ; str++)
 
579
        {
 
580
#ifdef USE_MB
 
581
          int l;
 
582
          if (use_mb(cs) &&
 
583
              (l = my_ismbchar(cs, str, end))) {
 
584
              while (l--)
 
585
                  *to++ = *str++;
 
586
              str--;
 
587
              continue;
 
588
          }
 
589
#endif
 
590
          if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
591
              *str == '\\' && str+1 != end)
 
592
          {
 
593
            switch(*++str) {
 
594
            case 'n':
 
595
              *to++='\n';
 
596
              break;
 
597
            case 't':
 
598
              *to++= '\t';
 
599
              break;
 
600
            case 'r':
 
601
              *to++ = '\r';
 
602
              break;
 
603
            case 'b':
 
604
              *to++ = '\b';
 
605
              break;
 
606
            case '0':
 
607
              *to++= 0;                 // Ascii null
 
608
              break;
 
609
            case 'Z':                   // ^Z must be escaped on Win32
 
610
              *to++='\032';
 
611
              break;
 
612
            case '_':
 
613
            case '%':
 
614
              *to++= '\\';              // remember prefix for wildcard
 
615
              /* Fall through */
 
616
            default:
 
617
              *to++= *str;
 
618
              break;
 
619
            }
 
620
          }
 
621
          else if (*str == sep)
 
622
            *to++= *str++;              // Two ' or "
 
623
          else
 
624
            *to++ = *str;
 
625
        }
 
626
        *to=0;
 
627
        lip->yytoklen=(uint) (to-start);
 
628
      }
 
629
      return start;
 
630
    }
 
631
  }
 
632
  return 0;                                     // unexpected end of query
 
633
}
 
634
 
 
635
 
 
636
/*
 
637
** Calc type of integer; long integer, longlong integer or real.
 
638
** Returns smallest type that match the string.
 
639
** When using unsigned long long values the result is converted to a real
 
640
** because else they will be unexpected sign changes because all calculation
 
641
** is done with longlong or double.
 
642
*/
 
643
 
 
644
static const char *long_str="2147483647";
 
645
static const uint long_len=10;
 
646
static const char *signed_long_str="-2147483648";
 
647
static const char *longlong_str="9223372036854775807";
 
648
static const uint longlong_len=19;
 
649
static const char *signed_longlong_str="-9223372036854775808";
 
650
static const uint signed_longlong_len=19;
 
651
static const char *unsigned_longlong_str="18446744073709551615";
 
652
static const uint unsigned_longlong_len=20;
 
653
 
 
654
static inline uint int_token(const char *str,uint length)
 
655
{
 
656
  if (length < long_len)                        // quick normal case
 
657
    return NUM;
 
658
  bool neg=0;
 
659
 
 
660
  if (*str == '+')                              // Remove sign and pre-zeros
 
661
  {
 
662
    str++; length--;
 
663
  }
 
664
  else if (*str == '-')
 
665
  {
 
666
    str++; length--;
 
667
    neg=1;
 
668
  }
 
669
  while (*str == '0' && length)
 
670
  {
 
671
    str++; length --;
 
672
  }
 
673
  if (length < long_len)
 
674
    return NUM;
 
675
 
 
676
  uint smaller,bigger;
 
677
  const char *cmp;
 
678
  if (neg)
 
679
  {
 
680
    if (length == long_len)
 
681
    {
 
682
      cmp= signed_long_str+1;
 
683
      smaller=NUM;                              // If <= signed_long_str
 
684
      bigger=LONG_NUM;                          // If >= signed_long_str
 
685
    }
 
686
    else if (length < signed_longlong_len)
 
687
      return LONG_NUM;
 
688
    else if (length > signed_longlong_len)
 
689
      return DECIMAL_NUM;
 
690
    else
 
691
    {
 
692
      cmp=signed_longlong_str+1;
 
693
      smaller=LONG_NUM;                         // If <= signed_longlong_str
 
694
      bigger=DECIMAL_NUM;
 
695
    }
 
696
  }
 
697
  else
 
698
  {
 
699
    if (length == long_len)
 
700
    {
 
701
      cmp= long_str;
 
702
      smaller=NUM;
 
703
      bigger=LONG_NUM;
 
704
    }
 
705
    else if (length < longlong_len)
 
706
      return LONG_NUM;
 
707
    else if (length > longlong_len)
 
708
    {
 
709
      if (length > unsigned_longlong_len)
 
710
        return DECIMAL_NUM;
 
711
      cmp=unsigned_longlong_str;
 
712
      smaller=ULONGLONG_NUM;
 
713
      bigger=DECIMAL_NUM;
 
714
    }
 
715
    else
 
716
    {
 
717
      cmp=longlong_str;
 
718
      smaller=LONG_NUM;
 
719
      bigger= ULONGLONG_NUM;
 
720
    }
 
721
  }
 
722
  while (*cmp && *cmp++ == *str++) ;
 
723
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
 
724
}
 
725
 
 
726
 
 
727
/**
 
728
  Given a stream that is advanced to the first contained character in 
 
729
  an open comment, consume the comment.  Optionally, if we are allowed, 
 
730
  recurse so that we understand comments within this current comment.
 
731
 
 
732
  At this level, we do not support version-condition comments.  We might 
 
733
  have been called with having just passed one in the stream, though.  In 
 
734
  that case, we probably want to tolerate mundane comments inside.  Thus,
 
735
  the case for recursion.
 
736
 
 
737
  @retval  Whether EOF reached before comment is closed.
 
738
*/
 
739
bool consume_comment(Lex_input_stream *lip, int remaining_recursions_permitted)
 
740
{
 
741
  reg1 uchar c;
 
742
  while (! lip->eof())
 
743
  {
 
744
    c= lip->yyGet();
 
745
 
 
746
    if (remaining_recursions_permitted > 0)
 
747
    {
 
748
      if ((c == '/') && (lip->yyPeek() == '*'))
 
749
      {
 
750
        lip->yySkip(); /* Eat asterisk */
 
751
        consume_comment(lip, remaining_recursions_permitted-1);
 
752
        continue;
 
753
      }
 
754
    }
 
755
 
 
756
    if (c == '*')
 
757
    {
 
758
      if (lip->yyPeek() == '/')
 
759
      {
 
760
        lip->yySkip(); /* Eat slash */
 
761
        return FALSE;
 
762
      }
 
763
    }
 
764
 
 
765
    if (c == '\n')
 
766
      lip->yylineno++;
 
767
  }
 
768
 
 
769
  return TRUE;
 
770
}
 
771
 
 
772
 
 
773
/*
 
774
  MYSQLlex remember the following states from the following MYSQLlex()
 
775
 
 
776
  - MY_LEX_EOQ                  Found end of query
 
777
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
 
778
                                (which can't be followed by a signed number)
 
779
*/
 
780
 
 
781
int MYSQLlex(void *arg, void *yythd)
 
782
{
 
783
  reg1  uchar c= 0;
 
784
  bool comment_closed;
 
785
  int   tokval, result_state;
 
786
  uint length;
 
787
  enum my_lex_states state;
 
788
  THD *thd= (THD *)yythd;
 
789
  Lex_input_stream *lip= & thd->m_parser_state->m_lip;
 
790
  LEX *lex= thd->lex;
 
791
  YYSTYPE *yylval=(YYSTYPE*) arg;
 
792
  CHARSET_INFO *cs= thd->charset();
 
793
  uchar *state_map= cs->state_map;
 
794
  uchar *ident_map= cs->ident_map;
 
795
 
 
796
  lip->yylval=yylval;                   // The global state
 
797
 
 
798
  lip->start_token();
 
799
  state=lip->next_state;
 
800
  lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
 
801
  for (;;)
 
802
  {
 
803
    switch (state) {
 
804
    case MY_LEX_OPERATOR_OR_IDENT:      // Next is operator or keyword
 
805
    case MY_LEX_START:                  // Start of token
 
806
      // Skip starting whitespace
 
807
      while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
 
808
      {
 
809
        if (c == '\n')
 
810
          lip->yylineno++;
 
811
 
 
812
        lip->yySkip();
 
813
      }
 
814
 
 
815
      /* Start of real token */
 
816
      lip->restart_token();
 
817
      c= lip->yyGet();
 
818
      state= (enum my_lex_states) state_map[c];
 
819
      break;
 
820
    case MY_LEX_ESCAPE:
 
821
      if (lip->yyGet() == 'N')
 
822
      {                                 // Allow \N as shortcut for NULL
 
823
        yylval->lex_str.str=(char*) "\\N";
 
824
        yylval->lex_str.length=2;
 
825
        return NULL_SYM;
 
826
      }
 
827
    case MY_LEX_CHAR:                   // Unknown or single char token
 
828
    case MY_LEX_SKIP:                   // This should not happen
 
829
      if (c == '-' && lip->yyPeek() == '-' &&
 
830
          (my_isspace(cs,lip->yyPeekn(1)) ||
 
831
           my_iscntrl(cs,lip->yyPeekn(1))))
 
832
      {
 
833
        state=MY_LEX_COMMENT;
 
834
        break;
 
835
      }
 
836
 
 
837
      if (c != ')')
 
838
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
839
 
 
840
      if (c == ',')
 
841
      {
 
842
        /*
 
843
          Warning:
 
844
          This is a work around, to make the "remember_name" rule in
 
845
          sql/sql_yacc.yy work properly.
 
846
          The problem is that, when parsing "select expr1, expr2",
 
847
          the code generated by bison executes the *pre* action
 
848
          remember_name (see select_item) *before* actually parsing the
 
849
          first token of expr2.
 
850
        */
 
851
        lip->restart_token();
 
852
      }
 
853
      else
 
854
      {
 
855
        /*
 
856
          Check for a placeholder: it should not precede a possible identifier
 
857
          because of binlogging: when a placeholder is replaced with
 
858
          its value in a query for the binlog, the query must stay
 
859
          grammatically correct.
 
860
        */
 
861
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[lip->yyPeek()])
 
862
        return(PARAM_MARKER);
 
863
      }
 
864
 
 
865
      return((int) c);
 
866
 
 
867
    case MY_LEX_IDENT_OR_NCHAR:
 
868
      if (lip->yyPeek() != '\'')
 
869
      {
 
870
        state= MY_LEX_IDENT;
 
871
        break;
 
872
      }
 
873
      /* Found N'string' */
 
874
      lip->yySkip();                         // Skip '
 
875
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
 
876
      {
 
877
        state= MY_LEX_CHAR;             // Read char by char
 
878
        break;
 
879
      }
 
880
      yylval->lex_str.length= lip->yytoklen;
 
881
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
882
      return(NCHAR_STRING);
 
883
 
 
884
    case MY_LEX_IDENT_OR_HEX:
 
885
      if (lip->yyPeek() == '\'')
 
886
      {                                 // Found x'hex-number'
 
887
        state= MY_LEX_HEX_NUMBER;
 
888
        break;
 
889
      }
 
890
    case MY_LEX_IDENT_OR_BIN:
 
891
      if (lip->yyPeek() == '\'')
 
892
      {                                 // Found b'bin-number'
 
893
        state= MY_LEX_BIN_NUMBER;
 
894
        break;
 
895
      }
 
896
    case MY_LEX_IDENT:
 
897
      const char *start;
 
898
#if defined(USE_MB) && defined(USE_MB_IDENT)
 
899
      if (use_mb(cs))
 
900
      {
 
901
        result_state= IDENT_QUOTED;
 
902
        if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
 
903
        {
 
904
          int l = my_ismbchar(cs,
 
905
                              lip->get_ptr() -1,
 
906
                              lip->get_end_of_query());
 
907
          if (l == 0) {
 
908
            state = MY_LEX_CHAR;
 
909
            continue;
 
910
          }
 
911
          lip->skip_binary(l - 1);
 
912
        }
 
913
        while (ident_map[c=lip->yyGet()])
 
914
        {
 
915
          if (my_mbcharlen(cs, c) > 1)
 
916
          {
 
917
            int l;
 
918
            if ((l = my_ismbchar(cs,
 
919
                                 lip->get_ptr() -1,
 
920
                                 lip->get_end_of_query())) == 0)
 
921
              break;
 
922
            lip->skip_binary(l-1);
 
923
          }
 
924
        }
 
925
      }
 
926
      else
 
927
#endif
 
928
      {
 
929
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) ;
 
930
        /* If there were non-ASCII characters, mark that we must convert */
 
931
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
 
932
      }
 
933
      length= lip->yyLength();
 
934
      start= lip->get_ptr();
 
935
      if (lip->ignore_space)
 
936
      {
 
937
        /*
 
938
          If we find a space then this can't be an identifier. We notice this
 
939
          below by checking start != lex->ptr.
 
940
        */
 
941
        for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) ;
 
942
      }
 
943
      if (start == lip->get_ptr() && c == '.' && ident_map[lip->yyPeek()])
 
944
        lip->next_state=MY_LEX_IDENT_SEP;
 
945
      else
 
946
      {                                 // '(' must follow directly if function
 
947
        lip->yyUnget();
 
948
        if ((tokval = find_keyword(lip, length, c == '(')))
 
949
        {
 
950
          lip->next_state= MY_LEX_START;        // Allow signed numbers
 
951
          return(tokval);               // Was keyword
 
952
        }
 
953
        lip->yySkip();                  // next state does a unget
 
954
      }
 
955
      yylval->lex_str=get_token(lip, 0, length);
 
956
 
 
957
      /*
 
958
         Note: "SELECT _bla AS 'alias'"
 
959
         _bla should be considered as a IDENT if charset haven't been found.
 
960
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
 
961
         producing an error.
 
962
      */
 
963
 
 
964
      if (yylval->lex_str.str[0] == '_')
 
965
      {
 
966
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
967
                                                MY_CS_PRIMARY, MYF(0));
 
968
        if (cs)
 
969
        {
 
970
          yylval->charset= cs;
 
971
          lip->m_underscore_cs= cs;
 
972
 
 
973
          lip->body_utf8_append(lip->m_cpp_text_start,
 
974
                                lip->get_cpp_tok_start() + length);
 
975
          return(UNDERSCORE_CHARSET);
 
976
        }
 
977
      }
 
978
 
 
979
      lip->body_utf8_append(lip->m_cpp_text_start);
 
980
 
 
981
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
982
                                    lip->m_cpp_text_end);
 
983
 
 
984
      return(result_state);                     // IDENT or IDENT_QUOTED
 
985
 
 
986
    case MY_LEX_IDENT_SEP:              // Found ident and now '.'
 
987
      yylval->lex_str.str= (char*) lip->get_ptr();
 
988
      yylval->lex_str.length= 1;
 
989
      c= lip->yyGet();                  // should be '.'
 
990
      lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
 
991
      if (!ident_map[lip->yyPeek()])            // Probably ` or "
 
992
        lip->next_state= MY_LEX_START;
 
993
      return((int) c);
 
994
 
 
995
    case MY_LEX_NUMBER_IDENT:           // number or ident which num-start
 
996
      if (lip->yyGetLast() == '0')
 
997
      {
 
998
        c= lip->yyGet();
 
999
        if (c == 'x')
 
1000
        {
 
1001
          while (my_isxdigit(cs,(c = lip->yyGet()))) ;
 
1002
          if ((lip->yyLength() >= 3) && !ident_map[c])
 
1003
          {
 
1004
            /* skip '0x' */
 
1005
            yylval->lex_str=get_token(lip, 2, lip->yyLength()-2);
 
1006
            return (HEX_NUM);
 
1007
          }
 
1008
          lip->yyUnget();
 
1009
          state= MY_LEX_IDENT_START;
 
1010
          break;
 
1011
        }
 
1012
        else if (c == 'b')
 
1013
        {
 
1014
          while ((c= lip->yyGet()) == '0' || c == '1') ;
 
1015
          if ((lip->yyLength() >= 3) && !ident_map[c])
 
1016
          {
 
1017
            /* Skip '0b' */
 
1018
            yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
 
1019
            return (BIN_NUM);
 
1020
          }
 
1021
          lip->yyUnget();
 
1022
          state= MY_LEX_IDENT_START;
 
1023
          break;
 
1024
        }
 
1025
        lip->yyUnget();
 
1026
      }
 
1027
 
 
1028
      while (my_isdigit(cs, (c = lip->yyGet()))) ;
 
1029
      if (!ident_map[c])
 
1030
      {                                 // Can't be identifier
 
1031
        state=MY_LEX_INT_OR_REAL;
 
1032
        break;
 
1033
      }
 
1034
      if (c == 'e' || c == 'E')
 
1035
      {
 
1036
        // The following test is written this way to allow numbers of type 1e1
 
1037
        if (my_isdigit(cs,lip->yyPeek()) ||
 
1038
            (c=(lip->yyGet())) == '+' || c == '-')
 
1039
        {                               // Allow 1E+10
 
1040
          if (my_isdigit(cs,lip->yyPeek()))     // Number must have digit after sign
 
1041
          {
 
1042
            lip->yySkip();
 
1043
            while (my_isdigit(cs,lip->yyGet())) ;
 
1044
            yylval->lex_str=get_token(lip, 0, lip->yyLength());
 
1045
            return(FLOAT_NUM);
 
1046
          }
 
1047
        }
 
1048
        lip->yyUnget();
 
1049
      }
 
1050
      // fall through
 
1051
    case MY_LEX_IDENT_START:                    // We come here after '.'
 
1052
      result_state= IDENT;
 
1053
#if defined(USE_MB) && defined(USE_MB_IDENT)
 
1054
      if (use_mb(cs))
 
1055
      {
 
1056
        result_state= IDENT_QUOTED;
 
1057
        while (ident_map[c=lip->yyGet()])
 
1058
        {
 
1059
          if (my_mbcharlen(cs, c) > 1)
 
1060
          {
 
1061
            int l;
 
1062
            if ((l = my_ismbchar(cs,
 
1063
                                 lip->get_ptr() -1,
 
1064
                                 lip->get_end_of_query())) == 0)
 
1065
              break;
 
1066
            lip->skip_binary(l-1);
 
1067
          }
 
1068
        }
 
1069
      }
 
1070
      else
 
1071
#endif
 
1072
      {
 
1073
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) ;
 
1074
        /* If there were non-ASCII characters, mark that we must convert */
 
1075
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
 
1076
      }
 
1077
      if (c == '.' && ident_map[lip->yyPeek()])
 
1078
        lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
 
1079
 
 
1080
      yylval->lex_str= get_token(lip, 0, lip->yyLength());
 
1081
 
 
1082
      lip->body_utf8_append(lip->m_cpp_text_start);
 
1083
 
 
1084
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1085
                                    lip->m_cpp_text_end);
 
1086
 
 
1087
      return(result_state);
 
1088
 
 
1089
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
 
1090
    {
 
1091
      uint double_quotes= 0;
 
1092
      char quote_char= c;                       // Used char
 
1093
      while ((c=lip->yyGet()))
 
1094
      {
 
1095
        int var_length;
 
1096
        if ((var_length= my_mbcharlen(cs, c)) == 1)
 
1097
        {
 
1098
          if (c == quote_char)
 
1099
          {
 
1100
            if (lip->yyPeek() != quote_char)
 
1101
              break;
 
1102
            c=lip->yyGet();
 
1103
            double_quotes++;
 
1104
            continue;
 
1105
          }
 
1106
        }
 
1107
#ifdef USE_MB
 
1108
        else if (use_mb(cs))
 
1109
        {
 
1110
          if ((var_length= my_ismbchar(cs, lip->get_ptr() - 1,
 
1111
                                       lip->get_end_of_query())))
 
1112
            lip->skip_binary(var_length-1);
 
1113
        }
 
1114
#endif
 
1115
      }
 
1116
      if (double_quotes)
 
1117
        yylval->lex_str=get_quoted_token(lip, 1,
 
1118
                                         lip->yyLength() - double_quotes -1,
 
1119
                                         quote_char);
 
1120
      else
 
1121
        yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
 
1122
      if (c == quote_char)
 
1123
        lip->yySkip();                  // Skip end `
 
1124
      lip->next_state= MY_LEX_START;
 
1125
 
 
1126
      lip->body_utf8_append(lip->m_cpp_text_start);
 
1127
 
 
1128
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1129
                                    lip->m_cpp_text_end);
 
1130
 
 
1131
      return(IDENT_QUOTED);
 
1132
    }
 
1133
    case MY_LEX_INT_OR_REAL:            // Complete int or incomplete real
 
1134
      if (c != '.')
 
1135
      {                                 // Found complete integer number.
 
1136
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
 
1137
        return int_token(yylval->lex_str.str, (uint) yylval->lex_str.length);
 
1138
      }
 
1139
      // fall through
 
1140
    case MY_LEX_REAL:                   // Incomplete real number
 
1141
      while (my_isdigit(cs,c = lip->yyGet())) ;
 
1142
 
 
1143
      if (c == 'e' || c == 'E')
 
1144
      {
 
1145
        c = lip->yyGet();
 
1146
        if (c == '-' || c == '+')
 
1147
          c = lip->yyGet();                     // Skip sign
 
1148
        if (!my_isdigit(cs,c))
 
1149
        {                               // No digit after sign
 
1150
          state= MY_LEX_CHAR;
 
1151
          break;
 
1152
        }
 
1153
        while (my_isdigit(cs,lip->yyGet())) ;
 
1154
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
 
1155
        return(FLOAT_NUM);
 
1156
      }
 
1157
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
 
1158
      return(DECIMAL_NUM);
 
1159
 
 
1160
    case MY_LEX_HEX_NUMBER:             // Found x'hexstring'
 
1161
      lip->yySkip();                    // Accept opening '
 
1162
      while (my_isxdigit(cs, (c= lip->yyGet()))) ;
 
1163
      if (c != '\'')
 
1164
        return(ABORT_SYM);              // Illegal hex constant
 
1165
      lip->yySkip();                    // Accept closing '
 
1166
      length= lip->yyLength();          // Length of hexnum+3
 
1167
      if ((length % 2) == 0)
 
1168
        return(ABORT_SYM);              // odd number of hex digits
 
1169
      yylval->lex_str=get_token(lip,
 
1170
                                2,          // skip x'
 
1171
                                length-3);  // don't count x' and last '
 
1172
      return (HEX_NUM);
 
1173
 
 
1174
    case MY_LEX_BIN_NUMBER:           // Found b'bin-string'
 
1175
      lip->yySkip();                  // Accept opening '
 
1176
      while ((c= lip->yyGet()) == '0' || c == '1') ;
 
1177
      if (c != '\'')
 
1178
        return(ABORT_SYM);            // Illegal hex constant
 
1179
      lip->yySkip();                  // Accept closing '
 
1180
      length= lip->yyLength();        // Length of bin-num + 3
 
1181
      yylval->lex_str= get_token(lip,
 
1182
                                 2,         // skip b'
 
1183
                                 length-3); // don't count b' and last '
 
1184
      return (BIN_NUM);
 
1185
 
 
1186
    case MY_LEX_CMP_OP:                 // Incomplete comparison operator
 
1187
      if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
 
1188
          state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
 
1189
        lip->yySkip();
 
1190
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
 
1191
      {
 
1192
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
1193
        return(tokval);
 
1194
      }
 
1195
      state = MY_LEX_CHAR;              // Something fishy found
 
1196
      break;
 
1197
 
 
1198
    case MY_LEX_LONG_CMP_OP:            // Incomplete comparison operator
 
1199
      if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
 
1200
          state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
 
1201
      {
 
1202
        lip->yySkip();
 
1203
        if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP)
 
1204
          lip->yySkip();
 
1205
      }
 
1206
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
 
1207
      {
 
1208
        lip->next_state= MY_LEX_START;  // Found long op
 
1209
        return(tokval);
 
1210
      }
 
1211
      state = MY_LEX_CHAR;              // Something fishy found
 
1212
      break;
 
1213
 
 
1214
    case MY_LEX_BOOL:
 
1215
      if (c != lip->yyPeek())
 
1216
      {
 
1217
        state=MY_LEX_CHAR;
 
1218
        break;
 
1219
      }
 
1220
      lip->yySkip();
 
1221
      tokval = find_keyword(lip,2,0);   // Is a bool operator
 
1222
      lip->next_state= MY_LEX_START;    // Allow signed numbers
 
1223
      return(tokval);
 
1224
 
 
1225
    case MY_LEX_STRING_OR_DELIMITER:
 
1226
      if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
 
1227
      {
 
1228
        state= MY_LEX_USER_VARIABLE_DELIMITER;
 
1229
        break;
 
1230
      }
 
1231
      /* " used for strings */
 
1232
    case MY_LEX_STRING:                 // Incomplete text string
 
1233
      if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
 
1234
      {
 
1235
        state= MY_LEX_CHAR;             // Read char by char
 
1236
        break;
 
1237
      }
 
1238
      yylval->lex_str.length=lip->yytoklen;
 
1239
 
 
1240
      lip->body_utf8_append(lip->m_cpp_text_start);
 
1241
 
 
1242
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1243
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
 
1244
        lip->m_cpp_text_end);
 
1245
 
 
1246
      lip->m_underscore_cs= NULL;
 
1247
 
 
1248
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
1249
      return(TEXT_STRING);
 
1250
 
 
1251
    case MY_LEX_COMMENT:                        //  Comment
 
1252
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
 
1253
      while ((c = lip->yyGet()) != '\n' && c) ;
 
1254
      lip->yyUnget();                   // Safety against eof
 
1255
      state = MY_LEX_START;             // Try again
 
1256
      break;
 
1257
    case MY_LEX_LONG_COMMENT:           /* Long C comment? */
 
1258
      if (lip->yyPeek() != '*')
 
1259
      {
 
1260
        state=MY_LEX_CHAR;              // Probable division
 
1261
        break;
 
1262
      }
 
1263
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
 
1264
      /* Reject '/' '*', since we might need to turn off the echo */
 
1265
      lip->yyUnget();
 
1266
 
 
1267
      lip->save_in_comment_state();
 
1268
 
 
1269
      if (lip->yyPeekn(2) == '!')
 
1270
      {
 
1271
        lip->in_comment= DISCARD_COMMENT;
 
1272
        /* Accept '/' '*' '!', but do not keep this marker. */
 
1273
        lip->set_echo(FALSE);
 
1274
        lip->yySkip();
 
1275
        lip->yySkip();
 
1276
        lip->yySkip();
 
1277
 
 
1278
        /*
 
1279
          The special comment format is very strict:
 
1280
          '/' '*' '!', followed by exactly
 
1281
          1 digit (major), 2 digits (minor), then 2 digits (dot).
 
1282
          32302 -> 3.23.02
 
1283
          50032 -> 5.0.32
 
1284
          50114 -> 5.1.14
 
1285
        */
 
1286
        char version_str[6];
 
1287
        version_str[0]= lip->yyPeekn(0);
 
1288
        version_str[1]= lip->yyPeekn(1);
 
1289
        version_str[2]= lip->yyPeekn(2);
 
1290
        version_str[3]= lip->yyPeekn(3);
 
1291
        version_str[4]= lip->yyPeekn(4);
 
1292
        version_str[5]= 0;
 
1293
        if (  my_isdigit(cs, version_str[0])
 
1294
           && my_isdigit(cs, version_str[1])
 
1295
           && my_isdigit(cs, version_str[2])
 
1296
           && my_isdigit(cs, version_str[3])
 
1297
           && my_isdigit(cs, version_str[4])
 
1298
           )
 
1299
        {
 
1300
          ulong version;
 
1301
          version=strtol(version_str, NULL, 10);
 
1302
 
 
1303
          /* Accept 'M' 'm' 'm' 'd' 'd' */
 
1304
          lip->yySkipn(5);
 
1305
 
 
1306
          if (version <= MYSQL_VERSION_ID)
 
1307
          {
 
1308
            /* Expand the content of the special comment as real code */
 
1309
            lip->set_echo(TRUE);
 
1310
            state=MY_LEX_START;
 
1311
            break;  /* Do not treat contents as a comment.  */
 
1312
          }
 
1313
          else
 
1314
          {
 
1315
            comment_closed= ! consume_comment(lip, 1);
 
1316
            /* version allowed to have one level of comment inside. */
 
1317
          }
 
1318
        }
 
1319
        else
 
1320
        {
 
1321
          /* Not a version comment. */
 
1322
          state=MY_LEX_START;
 
1323
          lip->set_echo(TRUE);
 
1324
          break;
 
1325
        }
 
1326
      }
 
1327
      else
 
1328
      {
 
1329
        lip->in_comment= PRESERVE_COMMENT;
 
1330
        lip->yySkip();                  // Accept /
 
1331
        lip->yySkip();                  // Accept *
 
1332
        comment_closed= ! consume_comment(lip, 0);
 
1333
        /* regular comments can have zero comments inside. */
 
1334
      }
 
1335
      /*
 
1336
        Discard:
 
1337
        - regular '/' '*' comments,
 
1338
        - special comments '/' '*' '!' for a future version,
 
1339
        by scanning until we find a closing '*' '/' marker.
 
1340
 
 
1341
        Nesting regular comments isn't allowed.  The first 
 
1342
        '*' '/' returns the parser to the previous state.
 
1343
 
 
1344
        /#!VERSI oned containing /# regular #/ is allowed #/
 
1345
 
 
1346
                Inside one versioned comment, another versioned comment
 
1347
                is treated as a regular discardable comment.  It gets
 
1348
                no special parsing.
 
1349
      */
 
1350
 
 
1351
      /* Unbalanced comments with a missing '*' '/' are a syntax error */
 
1352
      if (! comment_closed)
 
1353
        return (ABORT_SYM);
 
1354
      state = MY_LEX_START;             // Try again
 
1355
      lip->restore_in_comment_state();
 
1356
      break;
 
1357
    case MY_LEX_END_LONG_COMMENT:
 
1358
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
 
1359
      {
 
1360
        /* Reject '*' '/' */
 
1361
        lip->yyUnget();
 
1362
        /* Accept '*' '/', with the proper echo */
 
1363
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
 
1364
        lip->yySkipn(2);
 
1365
        /* And start recording the tokens again */
 
1366
        lip->set_echo(TRUE);
 
1367
        lip->in_comment=NO_COMMENT;
 
1368
        state=MY_LEX_START;
 
1369
      }
 
1370
      else
 
1371
        state=MY_LEX_CHAR;              // Return '*'
 
1372
      break;
 
1373
    case MY_LEX_SET_VAR:                // Check if ':='
 
1374
      if (lip->yyPeek() != '=')
 
1375
      {
 
1376
        state=MY_LEX_CHAR;              // Return ':'
 
1377
        break;
 
1378
      }
 
1379
      lip->yySkip();
 
1380
      return (SET_VAR);
 
1381
    case MY_LEX_SEMICOLON:                      // optional line terminator
 
1382
      state= MY_LEX_CHAR;               // Return ';'
 
1383
      break;
 
1384
    case MY_LEX_EOL:
 
1385
      if (lip->eof())
 
1386
      {
 
1387
        lip->yyUnget();                 // Reject the last '\0'
 
1388
        lip->set_echo(FALSE);
 
1389
        lip->yySkip();
 
1390
        lip->set_echo(TRUE);
 
1391
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
 
1392
        if (lip->in_comment != NO_COMMENT)
 
1393
          return (ABORT_SYM);
 
1394
        lip->next_state=MY_LEX_END;     // Mark for next loop
 
1395
        return(END_OF_INPUT);
 
1396
      }
 
1397
      state=MY_LEX_CHAR;
 
1398
      break;
 
1399
    case MY_LEX_END:
 
1400
      lip->next_state=MY_LEX_END;
 
1401
      return(0);                        // We found end of input last time
 
1402
 
 
1403
      /* Actually real shouldn't start with . but allow them anyhow */
 
1404
    case MY_LEX_REAL_OR_POINT:
 
1405
      if (my_isdigit(cs,lip->yyPeek()))
 
1406
        state = MY_LEX_REAL;            // Real
 
1407
      else
 
1408
      {
 
1409
        state= MY_LEX_IDENT_SEP;        // return '.'
 
1410
        lip->yyUnget();                 // Put back '.'
 
1411
      }
 
1412
      break;
 
1413
    case MY_LEX_USER_END:               // end '@' of user@hostname
 
1414
      switch (state_map[lip->yyPeek()]) {
 
1415
      case MY_LEX_STRING:
 
1416
      case MY_LEX_USER_VARIABLE_DELIMITER:
 
1417
      case MY_LEX_STRING_OR_DELIMITER:
 
1418
        break;
 
1419
      case MY_LEX_USER_END:
 
1420
        lip->next_state=MY_LEX_SYSTEM_VAR;
 
1421
        break;
 
1422
      default:
 
1423
        lip->next_state=MY_LEX_HOSTNAME;
 
1424
        break;
 
1425
      }
 
1426
      yylval->lex_str.str=(char*) lip->get_ptr();
 
1427
      yylval->lex_str.length=1;
 
1428
      return((int) '@');
 
1429
    case MY_LEX_HOSTNAME:               // end '@' of user@hostname
 
1430
      for (c=lip->yyGet() ;
 
1431
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
 
1432
           c= lip->yyGet()) ;
 
1433
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
 
1434
      return(LEX_HOSTNAME);
 
1435
    case MY_LEX_SYSTEM_VAR:
 
1436
      yylval->lex_str.str=(char*) lip->get_ptr();
 
1437
      yylval->lex_str.length=1;
 
1438
      lip->yySkip();                                    // Skip '@'
 
1439
      lip->next_state= (state_map[lip->yyPeek()] ==
 
1440
                        MY_LEX_USER_VARIABLE_DELIMITER ?
 
1441
                        MY_LEX_OPERATOR_OR_IDENT :
 
1442
                        MY_LEX_IDENT_OR_KEYWORD);
 
1443
      return((int) '@');
 
1444
    case MY_LEX_IDENT_OR_KEYWORD:
 
1445
      /*
 
1446
        We come here when we have found two '@' in a row.
 
1447
        We should now be able to handle:
 
1448
        [(global | local | session) .]variable_name
 
1449
      */
 
1450
 
 
1451
      for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) ;
 
1452
      /* If there were non-ASCII characters, mark that we must convert */
 
1453
      result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
 
1454
 
 
1455
      if (c == '.')
 
1456
        lip->next_state=MY_LEX_IDENT_SEP;
 
1457
      length= lip->yyLength();
 
1458
      if (length == 0)
 
1459
        return(ABORT_SYM);              // Names must be nonempty.
 
1460
      if ((tokval= find_keyword(lip, length,0)))
 
1461
      {
 
1462
        lip->yyUnget();                         // Put back 'c'
 
1463
        return(tokval);                         // Was keyword
 
1464
      }
 
1465
      yylval->lex_str=get_token(lip, 0, length);
 
1466
 
 
1467
      lip->body_utf8_append(lip->m_cpp_text_start);
 
1468
 
 
1469
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1470
                                    lip->m_cpp_text_end);
 
1471
 
 
1472
      return(result_state);
 
1473
    }
 
1474
  }
 
1475
}
 
1476
 
 
1477
 
 
1478
/**
 
1479
  Construct a copy of this object to be used for mysql_alter_table
 
1480
  and mysql_create_table.
 
1481
 
 
1482
  Historically, these two functions modify their Alter_info
 
1483
  arguments. This behaviour breaks re-execution of prepared
 
1484
  statements and stored procedures and is compensated by always
 
1485
  supplying a copy of Alter_info to these functions.
 
1486
 
 
1487
  @return You need to use check the error in THD for out
 
1488
  of memory condition after calling this function.
 
1489
*/
 
1490
 
 
1491
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
 
1492
  :drop_list(rhs.drop_list, mem_root),
 
1493
  alter_list(rhs.alter_list, mem_root),
 
1494
  key_list(rhs.key_list, mem_root),
 
1495
  create_list(rhs.create_list, mem_root),
 
1496
  flags(rhs.flags),
 
1497
  keys_onoff(rhs.keys_onoff),
 
1498
  tablespace_op(rhs.tablespace_op),
 
1499
  partition_names(rhs.partition_names, mem_root),
 
1500
  no_parts(rhs.no_parts),
 
1501
  change_level(rhs.change_level),
 
1502
  datetime_field(rhs.datetime_field),
 
1503
  error_if_not_empty(rhs.error_if_not_empty)
 
1504
{
 
1505
  /*
 
1506
    Make deep copies of used objects.
 
1507
    This is not a fully deep copy - clone() implementations
 
1508
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
 
1509
    do not copy string constants. At the same length the only
 
1510
    reason we make a copy currently is that ALTER/CREATE TABLE
 
1511
    code changes input Alter_info definitions, but string
 
1512
    constants never change.
 
1513
  */
 
1514
  list_copy_and_replace_each_value(drop_list, mem_root);
 
1515
  list_copy_and_replace_each_value(alter_list, mem_root);
 
1516
  list_copy_and_replace_each_value(key_list, mem_root);
 
1517
  list_copy_and_replace_each_value(create_list, mem_root);
 
1518
  /* partition_names are not deeply copied currently */
 
1519
}
 
1520
 
 
1521
 
 
1522
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
 
1523
{
 
1524
  /*
 
1525
    TODO:
 
1526
    This code assumes that there are no multi-bytes characters
 
1527
    that can be considered white-space.
 
1528
  */
 
1529
 
 
1530
  while ((str->length > 0) && (my_isspace(cs, str->str[0])))
 
1531
  {
 
1532
    str->length --;
 
1533
    str->str ++;
 
1534
  }
 
1535
 
 
1536
  /*
 
1537
    FIXME:
 
1538
    Also, parsing backward is not safe with multi bytes characters
 
1539
  */
 
1540
  while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
 
1541
  {
 
1542
    str->length --;
 
1543
  }
 
1544
}
 
1545
 
 
1546
 
 
1547
/*
 
1548
  st_select_lex structures initialisations
 
1549
*/
 
1550
 
 
1551
void st_select_lex_node::init_query()
 
1552
{
 
1553
  options= 0;
 
1554
  sql_cache= SQL_CACHE_UNSPECIFIED;
 
1555
  linkage= UNSPECIFIED_TYPE;
 
1556
  no_error= no_table_names_allowed= 0;
 
1557
  uncacheable= 0;
 
1558
}
 
1559
 
 
1560
void st_select_lex_node::init_select()
 
1561
{
 
1562
}
 
1563
 
 
1564
void st_select_lex_unit::init_query()
 
1565
{
 
1566
  st_select_lex_node::init_query();
 
1567
  linkage= GLOBAL_OPTIONS_TYPE;
 
1568
  global_parameters= first_select();
 
1569
  select_limit_cnt= HA_POS_ERROR;
 
1570
  offset_limit_cnt= 0;
 
1571
  union_distinct= 0;
 
1572
  prepared= optimized= executed= 0;
 
1573
  item= 0;
 
1574
  union_result= 0;
 
1575
  table= 0;
 
1576
  fake_select_lex= 0;
 
1577
  cleaned= 0;
 
1578
  item_list.empty();
 
1579
  describe= 0;
 
1580
  found_rows_for_union= 0;
 
1581
}
 
1582
 
 
1583
void st_select_lex::init_query()
 
1584
{
 
1585
  st_select_lex_node::init_query();
 
1586
  table_list.empty();
 
1587
  top_join_list.empty();
 
1588
  join_list= &top_join_list;
 
1589
  embedding= leaf_tables= 0;
 
1590
  item_list.empty();
 
1591
  join= 0;
 
1592
  having= prep_having= where= prep_where= 0;
 
1593
  olap= UNSPECIFIED_OLAP_TYPE;
 
1594
  having_fix_field= 0;
 
1595
  group_fix_field= 0;
 
1596
  context.select_lex= this;
 
1597
  context.init();
 
1598
  /*
 
1599
    Add the name resolution context of the current (sub)query to the
 
1600
    stack of contexts for the whole query.
 
1601
    TODO:
 
1602
    push_context may return an error if there is no memory for a new
 
1603
    element in the stack, however this method has no return value,
 
1604
    thus push_context should be moved to a place where query
 
1605
    initialization is checked for failure.
 
1606
  */
 
1607
  parent_lex->push_context(&context);
 
1608
  cond_count= between_count= with_wild= 0;
 
1609
  max_equal_elems= 0;
 
1610
  conds_processed_with_permanent_arena= 0;
 
1611
  ref_pointer_array= 0;
 
1612
  select_n_where_fields= 0;
 
1613
  select_n_having_items= 0;
 
1614
  subquery_in_having= explicit_limit= 0;
 
1615
  is_item_list_lookup= 0;
 
1616
  first_execution= 1;
 
1617
  first_natural_join_processing= 1;
 
1618
  first_cond_optimization= 1;
 
1619
  parsing_place= NO_MATTER;
 
1620
  exclude_from_table_unique_test= no_wrap_view_item= FALSE;
 
1621
  nest_level= 0;
 
1622
  link_next= 0;
 
1623
  lock_option= TL_READ_DEFAULT;
 
1624
}
 
1625
 
 
1626
void st_select_lex::init_select()
 
1627
{
 
1628
  st_select_lex_node::init_select();
 
1629
  group_list.empty();
 
1630
  type= db= 0;
 
1631
  having= 0;
 
1632
  table_join_options= 0;
 
1633
  in_sum_expr= with_wild= 0;
 
1634
  options= 0;
 
1635
  sql_cache= SQL_CACHE_UNSPECIFIED;
 
1636
  braces= 0;
 
1637
  interval_list.empty();
 
1638
  ftfunc_list_alloc.empty();
 
1639
  inner_sum_func_list= 0;
 
1640
  ftfunc_list= &ftfunc_list_alloc;
 
1641
  linkage= UNSPECIFIED_TYPE;
 
1642
  order_list.elements= 0;
 
1643
  order_list.first= 0;
 
1644
  order_list.next= (uchar**) &order_list.first;
 
1645
  /* Set limit and offset to default values */
 
1646
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
 
1647
  offset_limit= 0;      /* denotes the default offset = 0 */
 
1648
  with_sum_func= 0;
 
1649
  is_correlated= 0;
 
1650
  cur_pos_in_select_list= UNDEF_POS;
 
1651
  non_agg_fields.empty();
 
1652
  cond_value= having_value= Item::COND_UNDEF;
 
1653
  inner_refs_list.empty();
 
1654
  full_group_by_flag= 0;
 
1655
}
 
1656
 
 
1657
/*
 
1658
  st_select_lex structures linking
 
1659
*/
 
1660
 
 
1661
/* include on level down */
 
1662
void st_select_lex_node::include_down(st_select_lex_node *upper)
 
1663
{
 
1664
  if ((next= upper->slave))
 
1665
    next->prev= &next;
 
1666
  prev= &upper->slave;
 
1667
  upper->slave= this;
 
1668
  master= upper;
 
1669
  slave= 0;
 
1670
}
 
1671
 
 
1672
/*
 
1673
  include on level down (but do not link)
 
1674
 
 
1675
  SYNOPSYS
 
1676
    st_select_lex_node::include_standalone()
 
1677
    upper - reference on node underr which this node should be included
 
1678
    ref - references on reference on this node
 
1679
*/
 
1680
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
 
1681
                                            st_select_lex_node **ref)
 
1682
{
 
1683
  next= 0;
 
1684
  prev= ref;
 
1685
  master= upper;
 
1686
  slave= 0;
 
1687
}
 
1688
 
 
1689
/* include neighbour (on same level) */
 
1690
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
 
1691
{
 
1692
  if ((next= before->next))
 
1693
    next->prev= &next;
 
1694
  prev= &before->next;
 
1695
  before->next= this;
 
1696
  master= before->master;
 
1697
  slave= 0;
 
1698
}
 
1699
 
 
1700
/* including in global SELECT_LEX list */
 
1701
void st_select_lex_node::include_global(st_select_lex_node **plink)
 
1702
{
 
1703
  if ((link_next= *plink))
 
1704
    link_next->link_prev= &link_next;
 
1705
  link_prev= plink;
 
1706
  *plink= this;
 
1707
}
 
1708
 
 
1709
//excluding from global list (internal function)
 
1710
void st_select_lex_node::fast_exclude()
 
1711
{
 
1712
  if (link_prev)
 
1713
  {
 
1714
    if ((*link_prev= link_next))
 
1715
      link_next->link_prev= link_prev;
 
1716
  }
 
1717
  // Remove slave structure
 
1718
  for (; slave; slave= slave->next)
 
1719
    slave->fast_exclude();
 
1720
  
 
1721
}
 
1722
 
 
1723
/*
 
1724
  excluding select_lex structure (except first (first select can't be
 
1725
  deleted, because it is most upper select))
 
1726
*/
 
1727
void st_select_lex_node::exclude()
 
1728
{
 
1729
  //exclude from global list
 
1730
  fast_exclude();
 
1731
  //exclude from other structures
 
1732
  if ((*prev= next))
 
1733
    next->prev= prev;
 
1734
  /* 
 
1735
     We do not need following statements, because prev pointer of first 
 
1736
     list element point to master->slave
 
1737
     if (master->slave == this)
 
1738
       master->slave= next;
 
1739
  */
 
1740
}
 
1741
 
 
1742
 
 
1743
/*
 
1744
  Exclude level of current unit from tree of SELECTs
 
1745
 
 
1746
  SYNOPSYS
 
1747
    st_select_lex_unit::exclude_level()
 
1748
 
 
1749
  NOTE: units which belong to current will be brought up on level of
 
1750
  currernt unit 
 
1751
*/
 
1752
void st_select_lex_unit::exclude_level()
 
1753
{
 
1754
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
 
1755
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1756
  {
 
1757
    // unlink current level from global SELECTs list
 
1758
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
 
1759
      sl->link_next->link_prev= sl->link_prev;
 
1760
 
 
1761
    // bring up underlay levels
 
1762
    SELECT_LEX_UNIT **last= 0;
 
1763
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1764
    {
 
1765
      u->master= master;
 
1766
      last= (SELECT_LEX_UNIT**)&(u->next);
 
1767
    }
 
1768
    if (last)
 
1769
    {
 
1770
      (*units_last)= sl->first_inner_unit();
 
1771
      units_last= last;
 
1772
    }
 
1773
  }
 
1774
  if (units)
 
1775
  {
 
1776
    // include brought up levels in place of current
 
1777
    (*prev)= units;
 
1778
    (*units_last)= (SELECT_LEX_UNIT*)next;
 
1779
    if (next)
 
1780
      next->prev= (SELECT_LEX_NODE**)units_last;
 
1781
    units->prev= prev;
 
1782
  }
 
1783
  else
 
1784
  {
 
1785
    // exclude currect unit from list of nodes
 
1786
    (*prev)= next;
 
1787
    if (next)
 
1788
      next->prev= prev;
 
1789
  }
 
1790
}
 
1791
 
 
1792
 
 
1793
/*
 
1794
  Exclude subtree of current unit from tree of SELECTs
 
1795
 
 
1796
  SYNOPSYS
 
1797
    st_select_lex_unit::exclude_tree()
 
1798
*/
 
1799
void st_select_lex_unit::exclude_tree()
 
1800
{
 
1801
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1802
  {
 
1803
    // unlink current level from global SELECTs list
 
1804
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
 
1805
      sl->link_next->link_prev= sl->link_prev;
 
1806
 
 
1807
    // unlink underlay levels
 
1808
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1809
    {
 
1810
      u->exclude_level();
 
1811
    }
 
1812
  }
 
1813
  // exclude currect unit from list of nodes
 
1814
  (*prev)= next;
 
1815
  if (next)
 
1816
    next->prev= prev;
 
1817
}
 
1818
 
 
1819
 
 
1820
/*
 
1821
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1822
  this to 'last' as dependent
 
1823
 
 
1824
  SYNOPSIS
 
1825
    last - pointer to last st_select_lex struct, before wich all 
 
1826
           st_select_lex have to be marked as dependent
 
1827
 
 
1828
  NOTE
 
1829
    'last' should be reachable from this st_select_lex_node
 
1830
*/
 
1831
 
 
1832
void st_select_lex::mark_as_dependent(st_select_lex *last)
 
1833
{
 
1834
  /*
 
1835
    Mark all selects from resolved to 1 before select where was
 
1836
    found table as depended (of select where was found table)
 
1837
  */
 
1838
  for (SELECT_LEX *s= this;
 
1839
       s && s != last;
 
1840
       s= s->outer_select())
 
1841
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
 
1842
    {
 
1843
      // Select is dependent of outer select
 
1844
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1845
                       UNCACHEABLE_DEPENDENT;
 
1846
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1847
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1848
                       UNCACHEABLE_DEPENDENT;
 
1849
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1850
      {
 
1851
        if (sl != s &&
 
1852
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1853
          sl->uncacheable|= UNCACHEABLE_UNITED;
 
1854
      }
 
1855
    }
 
1856
  is_correlated= TRUE;
 
1857
  this->master_unit()->item->is_correlated= TRUE;
 
1858
}
 
1859
 
 
1860
bool st_select_lex_node::set_braces(bool value)      { return 1; }
 
1861
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
 
1862
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1863
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
 
1864
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
 
1865
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
 
1866
                                                  LEX_STRING *alias,
 
1867
                                                  ulong table_join_options,
 
1868
                                                  thr_lock_type flags,
 
1869
                                                  List<Index_hint> *hints,
 
1870
                                                  LEX_STRING *option)
 
1871
{
 
1872
  return 0;
 
1873
}
 
1874
ulong st_select_lex_node::get_table_join_options()
 
1875
{
 
1876
  return 0;
 
1877
}
 
1878
 
 
1879
/*
 
1880
  prohibit using LIMIT clause
 
1881
*/
 
1882
bool st_select_lex::test_limit()
 
1883
{
 
1884
  if (select_limit != 0)
 
1885
  {
 
1886
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
 
1887
             "LIMIT & IN/ALL/ANY/SOME subquery");
 
1888
    return(1);
 
1889
  }
 
1890
  return(0);
 
1891
}
 
1892
 
 
1893
 
 
1894
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1895
{
 
1896
    return this;
 
1897
}
 
1898
 
 
1899
 
 
1900
st_select_lex* st_select_lex_unit::outer_select()
 
1901
{
 
1902
  return (st_select_lex*) master;
 
1903
}
 
1904
 
 
1905
 
 
1906
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1907
{
 
1908
  return add_to_list(thd, order_list, item, asc);
 
1909
}
 
1910
 
 
1911
 
 
1912
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
 
1913
{
 
1914
  DBUG_ENTER("st_select_lex::add_item_to_list");
 
1915
  DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
 
1916
  DBUG_RETURN(item_list.push_back(item));
 
1917
}
 
1918
 
 
1919
 
 
1920
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1921
{
 
1922
  return add_to_list(thd, group_list, item, asc);
 
1923
}
 
1924
 
 
1925
 
 
1926
bool st_select_lex::add_ftfunc_to_list(Item_func_match *func)
 
1927
{
 
1928
  return !func || ftfunc_list->push_back(func); // end of memory?
 
1929
}
 
1930
 
 
1931
 
 
1932
st_select_lex_unit* st_select_lex::master_unit()
 
1933
{
 
1934
  return (st_select_lex_unit*) master;
 
1935
}
 
1936
 
 
1937
 
 
1938
st_select_lex* st_select_lex::outer_select()
 
1939
{
 
1940
  return (st_select_lex*) master->get_master();
 
1941
}
 
1942
 
 
1943
 
 
1944
bool st_select_lex::set_braces(bool value)
 
1945
{
 
1946
  braces= value;
 
1947
  return 0; 
 
1948
}
 
1949
 
 
1950
 
 
1951
bool st_select_lex::inc_in_sum_expr()
 
1952
{
 
1953
  in_sum_expr++;
 
1954
  return 0;
 
1955
}
 
1956
 
 
1957
 
 
1958
uint st_select_lex::get_in_sum_expr()
 
1959
{
 
1960
  return in_sum_expr;
 
1961
}
 
1962
 
 
1963
 
 
1964
TABLE_LIST* st_select_lex::get_table_list()
 
1965
{
 
1966
  return (TABLE_LIST*) table_list.first;
 
1967
}
 
1968
 
 
1969
List<Item>* st_select_lex::get_item_list()
 
1970
{
 
1971
  return &item_list;
 
1972
}
 
1973
 
 
1974
ulong st_select_lex::get_table_join_options()
 
1975
{
 
1976
  return table_join_options;
 
1977
}
 
1978
 
 
1979
 
 
1980
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
 
1981
{
 
1982
  if (ref_pointer_array)
 
1983
    return 0;
 
1984
 
 
1985
  /*
 
1986
    We have to create array in prepared statement memory if it is
 
1987
    prepared statement
 
1988
  */
 
1989
  Query_arena *arena= thd->stmt_arena;
 
1990
  return (ref_pointer_array=
 
1991
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
 
1992
                                                 item_list.elements +
 
1993
                                                 select_n_having_items +
 
1994
                                                 select_n_where_fields +
 
1995
                                                 order_group_num)*5)) == 0;
 
1996
}
 
1997
 
 
1998
 
 
1999
void st_select_lex_unit::print(String *str, enum_query_type query_type)
 
2000
{
 
2001
  bool union_all= !union_distinct;
 
2002
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
2003
  {
 
2004
    if (sl != first_select())
 
2005
    {
 
2006
      str->append(STRING_WITH_LEN(" union "));
 
2007
      if (union_all)
 
2008
        str->append(STRING_WITH_LEN("all "));
 
2009
      else if (union_distinct == sl)
 
2010
        union_all= TRUE;
 
2011
    }
 
2012
    if (sl->braces)
 
2013
      str->append('(');
 
2014
    sl->print(thd, str, query_type);
 
2015
    if (sl->braces)
 
2016
      str->append(')');
 
2017
  }
 
2018
  if (fake_select_lex == global_parameters)
 
2019
  {
 
2020
    if (fake_select_lex->order_list.elements)
 
2021
    {
 
2022
      str->append(STRING_WITH_LEN(" order by "));
 
2023
      fake_select_lex->print_order(
 
2024
        str,
 
2025
        (ORDER *) fake_select_lex->order_list.first,
 
2026
        query_type);
 
2027
    }
 
2028
    fake_select_lex->print_limit(thd, str, query_type);
 
2029
  }
 
2030
}
 
2031
 
 
2032
 
 
2033
void st_select_lex::print_order(String *str,
 
2034
                                ORDER *order,
 
2035
                                enum_query_type query_type)
 
2036
{
 
2037
  for (; order; order= order->next)
 
2038
  {
 
2039
    if (order->counter_used)
 
2040
    {
 
2041
      char buffer[20];
 
2042
      size_t length= my_snprintf(buffer, 20, "%d", order->counter);
 
2043
      str->append(buffer, (uint) length);
 
2044
    }
 
2045
    else
 
2046
      (*order->item)->print(str, query_type);
 
2047
    if (!order->asc)
 
2048
      str->append(STRING_WITH_LEN(" desc"));
 
2049
    if (order->next)
 
2050
      str->append(',');
 
2051
  }
 
2052
}
 
2053
 
 
2054
 
 
2055
void st_select_lex::print_limit(THD *thd,
 
2056
                                String *str,
 
2057
                                enum_query_type query_type)
 
2058
{
 
2059
  SELECT_LEX_UNIT *unit= master_unit();
 
2060
  Item_subselect *item= unit->item;
 
2061
  if (item && unit->global_parameters == this &&
 
2062
      (item->substype() == Item_subselect::EXISTS_SUBS ||
 
2063
       item->substype() == Item_subselect::IN_SUBS ||
 
2064
       item->substype() == Item_subselect::ALL_SUBS))
 
2065
  {
 
2066
    DBUG_ASSERT(!item->fixed ||
 
2067
                (select_limit->val_int() == LL(1) && offset_limit == 0));
 
2068
    return;
 
2069
  }
 
2070
 
 
2071
  if (explicit_limit)
 
2072
  {
 
2073
    str->append(STRING_WITH_LEN(" limit "));
 
2074
    if (offset_limit)
 
2075
    {
 
2076
      offset_limit->print(str, query_type);
 
2077
      str->append(',');
 
2078
    }
 
2079
    select_limit->print(str, query_type);
 
2080
  }
 
2081
}
 
2082
 
 
2083
/**
 
2084
  @brief Restore the LEX and THD in case of a parse error.
 
2085
 
 
2086
  This is a clean up call that is invoked by the Bison generated
 
2087
  parser before returning an error from MYSQLparse. If your
 
2088
  semantic actions manipulate with the global thread state (which
 
2089
  is a very bad practice and should not normally be employed) and
 
2090
  need a clean-up in case of error, and you can not use %destructor
 
2091
  rule in the grammar file itself, this function should be used
 
2092
  to implement the clean up.
 
2093
*/
 
2094
 
 
2095
void st_lex::cleanup_lex_after_parse_error(THD *thd)
 
2096
{
 
2097
  /*
 
2098
    Delete sphead for the side effect of restoring of the original
 
2099
    LEX state, thd->lex, thd->mem_root and thd->free_list if they
 
2100
    were replaced when parsing stored procedure statements.  We
 
2101
    will never use sphead object after a parse error, so it's okay
 
2102
    to delete it only for the sake of the side effect.
 
2103
    TODO: make this functionality explicit in sp_head class.
 
2104
    Sic: we must nullify the member of the main lex, not the
 
2105
    current one that will be thrown away
 
2106
  */
 
2107
  if (thd->lex->sphead)
 
2108
  {
 
2109
    delete thd->lex->sphead;
 
2110
    thd->lex->sphead= NULL;
 
2111
  }
 
2112
}
 
2113
 
 
2114
/*
 
2115
  Initialize (or reset) Query_tables_list object.
 
2116
 
 
2117
  SYNOPSIS
 
2118
    reset_query_tables_list()
 
2119
      init  TRUE  - we should perform full initialization of object with
 
2120
                    allocating needed memory
 
2121
            FALSE - object is already initialized so we should only reset
 
2122
                    its state so it can be used for parsing/processing
 
2123
                    of new statement
 
2124
 
 
2125
  DESCRIPTION
 
2126
    This method initializes Query_tables_list so it can be used as part
 
2127
    of LEX object for parsing/processing of statement. One can also use
 
2128
    this method to reset state of already initialized Query_tables_list
 
2129
    so it can be used for processing of new statement.
 
2130
*/
 
2131
 
 
2132
void Query_tables_list::reset_query_tables_list(bool init)
 
2133
{
 
2134
  if (!init && query_tables)
 
2135
  {
 
2136
    TABLE_LIST *table= query_tables;
 
2137
    for (;;)
 
2138
    {
 
2139
      delete table->view;
 
2140
      if (query_tables_last == &table->next_global ||
 
2141
          !(table= table->next_global))
 
2142
        break;
 
2143
    }
 
2144
  }
 
2145
  query_tables= 0;
 
2146
  query_tables_last= &query_tables;
 
2147
  query_tables_own_last= 0;
 
2148
  if (init)
 
2149
  {
 
2150
    /*
 
2151
      We delay real initialization of hash (and therefore related
 
2152
      memory allocation) until first insertion into this hash.
 
2153
    */
 
2154
    hash_clear(&sroutines);
 
2155
  }
 
2156
  else if (sroutines.records)
 
2157
  {
 
2158
    /* Non-zero sroutines.records means that hash was initialized. */
 
2159
    my_hash_reset(&sroutines);
 
2160
  }
 
2161
  sroutines_list.empty();
 
2162
  sroutines_list_own_last= sroutines_list.next;
 
2163
  sroutines_list_own_elements= 0;
 
2164
  binlog_stmt_flags= 0;
 
2165
}
 
2166
 
 
2167
 
 
2168
/*
 
2169
  Destroy Query_tables_list object with freeing all resources used by it.
 
2170
 
 
2171
  SYNOPSIS
 
2172
    destroy_query_tables_list()
 
2173
*/
 
2174
 
 
2175
void Query_tables_list::destroy_query_tables_list()
 
2176
{
 
2177
  hash_free(&sroutines);
 
2178
}
 
2179
 
 
2180
 
 
2181
/*
 
2182
  Initialize LEX object.
 
2183
 
 
2184
  SYNOPSIS
 
2185
    st_lex::st_lex()
 
2186
 
 
2187
  NOTE
 
2188
    LEX object initialized with this constructor can be used as part of
 
2189
    THD object for which one can safely call open_tables(), lock_tables()
 
2190
    and close_thread_tables() functions. But it is not yet ready for
 
2191
    statement parsing. On should use lex_start() function to prepare LEX
 
2192
    for this.
 
2193
*/
 
2194
 
 
2195
st_lex::st_lex()
 
2196
  :result(0),
 
2197
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
 
2198
{
 
2199
 
 
2200
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
 
2201
                         plugins_static_buffer,
 
2202
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
 
2203
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
 
2204
  reset_query_tables_list(TRUE);
 
2205
}
 
2206
 
 
2207
 
 
2208
/*
 
2209
  Check whether the merging algorithm can be used on this VIEW
 
2210
 
 
2211
  SYNOPSIS
 
2212
    st_lex::can_be_merged()
 
2213
 
 
2214
  DESCRIPTION
 
2215
    We can apply merge algorithm if it is single SELECT view  with
 
2216
    subqueries only in WHERE clause (we do not count SELECTs of underlying
 
2217
    views, and second level subqueries) and we have not grpouping, ordering,
 
2218
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
 
2219
    several underlying tables.
 
2220
 
 
2221
  RETURN
 
2222
    FALSE - only temporary table algorithm can be used
 
2223
    TRUE  - merge algorithm can be used
 
2224
*/
 
2225
 
 
2226
bool st_lex::can_be_merged()
 
2227
{
 
2228
  // TODO: do not forget implement case when select_lex.table_list.elements==0
 
2229
 
 
2230
  /* find non VIEW subqueries/unions */
 
2231
  bool selects_allow_merge= select_lex.next_select() == 0;
 
2232
  if (selects_allow_merge)
 
2233
  {
 
2234
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
 
2235
         tmp_unit;
 
2236
         tmp_unit= tmp_unit->next_unit())
 
2237
    {
 
2238
      if (tmp_unit->first_select()->parent_lex == this &&
 
2239
          (tmp_unit->item == 0 ||
 
2240
           (tmp_unit->item->place() != IN_WHERE &&
 
2241
            tmp_unit->item->place() != IN_ON)))
 
2242
      {
 
2243
        selects_allow_merge= 0;
 
2244
        break;
 
2245
      }
 
2246
    }
 
2247
  }
 
2248
 
 
2249
  return (selects_allow_merge &&
 
2250
          select_lex.group_list.elements == 0 &&
 
2251
          select_lex.having == 0 &&
 
2252
          select_lex.with_sum_func == 0 &&
 
2253
          select_lex.table_list.elements >= 1 &&
 
2254
          !(select_lex.options & SELECT_DISTINCT) &&
 
2255
          select_lex.select_limit == 0);
 
2256
}
 
2257
 
 
2258
 
 
2259
/*
 
2260
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
 
2261
 
 
2262
  SYNOPSIS
 
2263
    st_lex::can_use_merged()
 
2264
 
 
2265
  DESCRIPTION
 
2266
    Only listed here commands can use merge algorithm in top level
 
2267
    SELECT_LEX (for subqueries will be used merge algorithm if
 
2268
    st_lex::can_not_use_merged() is not TRUE).
 
2269
 
 
2270
  RETURN
 
2271
    FALSE - command can't use merged VIEWs
 
2272
    TRUE  - VIEWs with MERGE algorithms can be used
 
2273
*/
 
2274
 
 
2275
bool st_lex::can_use_merged()
 
2276
{
 
2277
  switch (sql_command)
 
2278
  {
 
2279
  case SQLCOM_SELECT:
 
2280
  case SQLCOM_CREATE_TABLE:
 
2281
  case SQLCOM_UPDATE:
 
2282
  case SQLCOM_UPDATE_MULTI:
 
2283
  case SQLCOM_DELETE:
 
2284
  case SQLCOM_DELETE_MULTI:
 
2285
  case SQLCOM_INSERT:
 
2286
  case SQLCOM_INSERT_SELECT:
 
2287
  case SQLCOM_REPLACE:
 
2288
  case SQLCOM_REPLACE_SELECT:
 
2289
  case SQLCOM_LOAD:
 
2290
    return TRUE;
 
2291
  default:
 
2292
    return FALSE;
 
2293
  }
 
2294
}
 
2295
 
 
2296
/*
 
2297
  Check if command can't use merged views in any part of command
 
2298
 
 
2299
  SYNOPSIS
 
2300
    st_lex::can_not_use_merged()
 
2301
 
 
2302
  DESCRIPTION
 
2303
    Temporary table algorithm will be used on all SELECT levels for queries
 
2304
    listed here (see also st_lex::can_use_merged()).
 
2305
 
 
2306
  RETURN
 
2307
    FALSE - command can't use merged VIEWs
 
2308
    TRUE  - VIEWs with MERGE algorithms can be used
 
2309
*/
 
2310
 
 
2311
bool st_lex::can_not_use_merged()
 
2312
{
 
2313
  switch (sql_command)
 
2314
  {
 
2315
  case SQLCOM_CREATE_VIEW:
 
2316
  case SQLCOM_SHOW_CREATE:
 
2317
  /*
 
2318
    SQLCOM_SHOW_FIELDS is necessary to make 
 
2319
    information schema tables working correctly with views.
 
2320
    see get_schema_tables_result function
 
2321
  */
 
2322
  case SQLCOM_SHOW_FIELDS:
 
2323
    return TRUE;
 
2324
  default:
 
2325
    return FALSE;
 
2326
  }
 
2327
}
 
2328
 
 
2329
/*
 
2330
  Detect that we need only table structure of derived table/view
 
2331
 
 
2332
  SYNOPSIS
 
2333
    only_view_structure()
 
2334
 
 
2335
  RETURN
 
2336
    TRUE yes, we need only structure
 
2337
    FALSE no, we need data
 
2338
*/
 
2339
 
 
2340
bool st_lex::only_view_structure()
 
2341
{
 
2342
  switch (sql_command) {
 
2343
  case SQLCOM_SHOW_CREATE:
 
2344
  case SQLCOM_SHOW_TABLES:
 
2345
  case SQLCOM_SHOW_FIELDS:
 
2346
  case SQLCOM_REVOKE_ALL:
 
2347
  case SQLCOM_REVOKE:
 
2348
  case SQLCOM_GRANT:
 
2349
  case SQLCOM_CREATE_VIEW:
 
2350
    return TRUE;
 
2351
  default:
 
2352
    return FALSE;
 
2353
  }
 
2354
}
 
2355
 
 
2356
 
 
2357
/*
 
2358
  Should Items_ident be printed correctly
 
2359
 
 
2360
  SYNOPSIS
 
2361
    need_correct_ident()
 
2362
 
 
2363
  RETURN
 
2364
    TRUE yes, we need only structure
 
2365
    FALSE no, we need data
 
2366
*/
 
2367
 
 
2368
 
 
2369
bool st_lex::need_correct_ident()
 
2370
{
 
2371
  switch(sql_command)
 
2372
  {
 
2373
  case SQLCOM_SHOW_CREATE:
 
2374
  case SQLCOM_SHOW_TABLES:
 
2375
  case SQLCOM_CREATE_VIEW:
 
2376
    return TRUE;
 
2377
  default:
 
2378
    return FALSE;
 
2379
  }
 
2380
}
 
2381
 
 
2382
/*
 
2383
  Get effective type of CHECK OPTION for given view
 
2384
 
 
2385
  SYNOPSIS
 
2386
    get_effective_with_check()
 
2387
    view    given view
 
2388
 
 
2389
  NOTE
 
2390
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
 
2391
    so we do not.
 
2392
 
 
2393
  RETURN
 
2394
    VIEW_CHECK_NONE      no need CHECK OPTION
 
2395
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
 
2396
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
 
2397
*/
 
2398
 
 
2399
uint8 st_lex::get_effective_with_check(TABLE_LIST *view)
 
2400
{
 
2401
  if (view->select_lex->master_unit() == &unit &&
 
2402
      which_check_option_applicable())
 
2403
    return (uint8)view->with_check;
 
2404
  return VIEW_CHECK_NONE;
 
2405
}
 
2406
 
 
2407
 
 
2408
/**
 
2409
  This method should be called only during parsing.
 
2410
  It is aware of compound statements (stored routine bodies)
 
2411
  and will initialize the destination with the default
 
2412
  database of the stored routine, rather than the default
 
2413
  database of the connection it is parsed in.
 
2414
  E.g. if one has no current database selected, or current database 
 
2415
  set to 'bar' and then issues:
 
2416
 
 
2417
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
 
2418
 
 
2419
  t1 is meant to refer to foo.t1, not to bar.t1.
 
2420
 
 
2421
  This method is needed to support this rule.
 
2422
 
 
2423
  @return TRUE in case of error (parsing should be aborted, FALSE in
 
2424
  case of success
 
2425
*/
 
2426
 
 
2427
bool
 
2428
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
 
2429
{
 
2430
  if (sphead)
 
2431
  {
 
2432
    DBUG_ASSERT(sphead->m_db.str && sphead->m_db.length);
 
2433
    /*
 
2434
      It is safe to assign the string by-pointer, both sphead and
 
2435
      its statements reside in the same memory root.
 
2436
    */
 
2437
    *p_db= sphead->m_db.str;
 
2438
    if (p_db_length)
 
2439
      *p_db_length= sphead->m_db.length;
 
2440
    return FALSE;
 
2441
  }
 
2442
  return thd->copy_db_to(p_db, p_db_length);
 
2443
}
 
2444
 
 
2445
/*
 
2446
  initialize limit counters
 
2447
 
 
2448
  SYNOPSIS
 
2449
    st_select_lex_unit::set_limit()
 
2450
    values      - SELECT_LEX with initial values for counters
 
2451
*/
 
2452
 
 
2453
void st_select_lex_unit::set_limit(st_select_lex *sl)
 
2454
{
 
2455
  ha_rows select_limit_val;
 
2456
  ulonglong val;
 
2457
 
 
2458
  DBUG_ASSERT(! thd->stmt_arena->is_stmt_prepare());
 
2459
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
 
2460
  select_limit_val= (ha_rows)val;
 
2461
#ifndef BIG_TABLES
 
2462
  /*
 
2463
    Check for overflow : ha_rows can be smaller then ulonglong if
 
2464
    BIG_TABLES is off.
 
2465
    */
 
2466
  if (val != (ulonglong)select_limit_val)
 
2467
    select_limit_val= HA_POS_ERROR;
 
2468
#endif
 
2469
  val= sl->offset_limit ? sl->offset_limit->val_uint() : ULL(0);
 
2470
  offset_limit_cnt= (ha_rows)val;
 
2471
#ifndef BIG_TABLES
 
2472
  /* Check for truncation. */
 
2473
  if (val != (ulonglong)offset_limit_cnt)
 
2474
    offset_limit_cnt= HA_POS_ERROR;
 
2475
#endif
 
2476
  select_limit_cnt= select_limit_val + offset_limit_cnt;
 
2477
  if (select_limit_cnt < select_limit_val)
 
2478
    select_limit_cnt= HA_POS_ERROR;             // no limit
 
2479
}
 
2480
 
 
2481
 
 
2482
/**
 
2483
  @brief Set the initial purpose of this TABLE_LIST object in the list of used
 
2484
    tables.
 
2485
 
 
2486
  We need to track this information on table-by-table basis, since when this
 
2487
  table becomes an element of the pre-locked list, it's impossible to identify
 
2488
  which SQL sub-statement it has been originally used in.
 
2489
 
 
2490
  E.g.:
 
2491
 
 
2492
  User request:                 SELECT * FROM t1 WHERE f1();
 
2493
  FUNCTION f1():                DELETE FROM t2; RETURN 1;
 
2494
  BEFORE DELETE trigger on t2:  INSERT INTO t3 VALUES (old.a);
 
2495
 
 
2496
  For this user request, the pre-locked list will contain t1, t2, t3
 
2497
  table elements, each needed for different DML.
 
2498
 
 
2499
  The trigger event map is updated to reflect INSERT, UPDATE, DELETE,
 
2500
  REPLACE, LOAD DATA, CREATE TABLE .. SELECT, CREATE TABLE ..
 
2501
  REPLACE SELECT statements, and additionally ON DUPLICATE KEY UPDATE
 
2502
  clause.
 
2503
*/
 
2504
 
 
2505
void st_lex::set_trg_event_type_for_tables()
 
2506
{
 
2507
  uint8 new_trg_event_map= 0;
 
2508
 
 
2509
  /*
 
2510
    Some auxiliary operations
 
2511
    (e.g. GRANT processing) create TABLE_LIST instances outside
 
2512
    the parser. Additionally, some commands (e.g. OPTIMIZE) change
 
2513
    the lock type for a table only after parsing is done. Luckily,
 
2514
    these do not fire triggers and do not need to pre-load them.
 
2515
    For these TABLE_LISTs set_trg_event_type is never called, and
 
2516
    trg_event_map is always empty. That means that the pre-locking
 
2517
    algorithm will ignore triggers defined on these tables, if
 
2518
    any, and the execution will either fail with an assert in
 
2519
    sql_trigger.cc or with an error that a used table was not
 
2520
    pre-locked, in case of a production build.
 
2521
 
 
2522
    TODO: this usage pattern creates unnecessary module dependencies
 
2523
    and should be rewritten to go through the parser.
 
2524
    Table list instances created outside the parser in most cases
 
2525
    refer to mysql.* system tables. It is not allowed to have
 
2526
    a trigger on a system table, but keeping track of
 
2527
    initialization provides extra safety in case this limitation
 
2528
    is circumvented.
 
2529
  */
 
2530
 
 
2531
  switch (sql_command) {
 
2532
  case SQLCOM_LOCK_TABLES:
 
2533
  /*
 
2534
    On a LOCK TABLE, all triggers must be pre-loaded for this TABLE_LIST
 
2535
    when opening an associated TABLE.
 
2536
  */
 
2537
    new_trg_event_map= static_cast<uint8>
 
2538
                        (1 << static_cast<int>(TRG_EVENT_INSERT)) |
 
2539
                      static_cast<uint8>
 
2540
                        (1 << static_cast<int>(TRG_EVENT_UPDATE)) |
 
2541
                      static_cast<uint8>
 
2542
                        (1 << static_cast<int>(TRG_EVENT_DELETE));
 
2543
    break;
 
2544
  /*
 
2545
    Basic INSERT. If there is an additional ON DUPLIATE KEY UPDATE
 
2546
    clause, it will be handled later in this method.
 
2547
  */
 
2548
  case SQLCOM_INSERT:                           /* fall through */
 
2549
  case SQLCOM_INSERT_SELECT:
 
2550
  /*
 
2551
    LOAD DATA ... INFILE is expected to fire BEFORE/AFTER INSERT
 
2552
    triggers.
 
2553
    If the statement also has REPLACE clause, it will be
 
2554
    handled later in this method.
 
2555
  */
 
2556
  case SQLCOM_LOAD:                             /* fall through */
 
2557
  /*
 
2558
    REPLACE is semantically equivalent to INSERT. In case
 
2559
    of a primary or unique key conflict, it deletes the old
 
2560
    record and inserts a new one. So we also may need to
 
2561
    fire ON DELETE triggers. This functionality is handled
 
2562
    later in this method.
 
2563
  */
 
2564
  case SQLCOM_REPLACE:                          /* fall through */
 
2565
  case SQLCOM_REPLACE_SELECT:
 
2566
  /*
 
2567
    CREATE TABLE ... SELECT defaults to INSERT if the table or
 
2568
    view already exists. REPLACE option of CREATE TABLE ...
 
2569
    REPLACE SELECT is handled later in this method.
 
2570
  */
 
2571
  case SQLCOM_CREATE_TABLE:
 
2572
    new_trg_event_map|= static_cast<uint8>
 
2573
                          (1 << static_cast<int>(TRG_EVENT_INSERT));
 
2574
    break;
 
2575
  /* Basic update and multi-update */
 
2576
  case SQLCOM_UPDATE:                           /* fall through */
 
2577
  case SQLCOM_UPDATE_MULTI:
 
2578
    new_trg_event_map|= static_cast<uint8>
 
2579
                          (1 << static_cast<int>(TRG_EVENT_UPDATE));
 
2580
    break;
 
2581
  /* Basic delete and multi-delete */
 
2582
  case SQLCOM_DELETE:                           /* fall through */
 
2583
  case SQLCOM_DELETE_MULTI:
 
2584
    new_trg_event_map|= static_cast<uint8>
 
2585
                          (1 << static_cast<int>(TRG_EVENT_DELETE));
 
2586
    break;
 
2587
  default:
 
2588
    break;
 
2589
  }
 
2590
 
 
2591
  switch (duplicates) {
 
2592
  case DUP_UPDATE:
 
2593
    new_trg_event_map|= static_cast<uint8>
 
2594
                          (1 << static_cast<int>(TRG_EVENT_UPDATE));
 
2595
    break;
 
2596
  case DUP_REPLACE:
 
2597
    new_trg_event_map|= static_cast<uint8>
 
2598
                          (1 << static_cast<int>(TRG_EVENT_DELETE));
 
2599
    break;
 
2600
  case DUP_ERROR:
 
2601
  default:
 
2602
    break;
 
2603
  }
 
2604
 
 
2605
 
 
2606
  /*
 
2607
    Do not iterate over sub-selects, only the tables in the outermost
 
2608
    SELECT_LEX can be modified, if any.
 
2609
  */
 
2610
  TABLE_LIST *tables= select_lex.get_table_list();
 
2611
 
 
2612
  while (tables)
 
2613
  {
 
2614
    /*
 
2615
      This is a fast check to filter out statements that do
 
2616
      not change data, or tables  on the right side, in case of
 
2617
      INSERT .. SELECT, CREATE TABLE .. SELECT and so on.
 
2618
      Here we also filter out OPTIMIZE statement and non-updateable
 
2619
      views, for which lock_type is TL_UNLOCK or TL_READ after
 
2620
      parsing.
 
2621
    */
 
2622
    if (static_cast<int>(tables->lock_type) >=
 
2623
        static_cast<int>(TL_WRITE_ALLOW_WRITE))
 
2624
      tables->trg_event_map= new_trg_event_map;
 
2625
    tables= tables->next_local;
 
2626
  }
 
2627
}
 
2628
 
 
2629
 
 
2630
/*
 
2631
  Unlink the first table from the global table list and the first table from
 
2632
  outer select (lex->select_lex) local list
 
2633
 
 
2634
  SYNOPSIS
 
2635
    unlink_first_table()
 
2636
    link_to_local       Set to 1 if caller should link this table to local list
 
2637
 
 
2638
  NOTES
 
2639
    We assume that first tables in both lists is the same table or the local
 
2640
    list is empty.
 
2641
 
 
2642
  RETURN
 
2643
    0   If 'query_tables' == 0
 
2644
    unlinked table
 
2645
      In this case link_to_local is set.
 
2646
 
 
2647
*/
 
2648
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
 
2649
{
 
2650
  TABLE_LIST *first;
 
2651
  if ((first= query_tables))
 
2652
  {
 
2653
    /*
 
2654
      Exclude from global table list
 
2655
    */
 
2656
    if ((query_tables= query_tables->next_global))
 
2657
      query_tables->prev_global= &query_tables;
 
2658
    else
 
2659
      query_tables_last= &query_tables;
 
2660
    first->next_global= 0;
 
2661
 
 
2662
    /*
 
2663
      and from local list if it is not empty
 
2664
    */
 
2665
    if ((*link_to_local= test(select_lex.table_list.first)))
 
2666
    {
 
2667
      select_lex.context.table_list= 
 
2668
        select_lex.context.first_name_resolution_table= first->next_local;
 
2669
      select_lex.table_list.first= (uchar*) (first->next_local);
 
2670
      select_lex.table_list.elements--; //safety
 
2671
      first->next_local= 0;
 
2672
      /*
 
2673
        Ensure that the global list has the same first table as the local
 
2674
        list.
 
2675
      */
 
2676
      first_lists_tables_same();
 
2677
    }
 
2678
  }
 
2679
  return first;
 
2680
}
 
2681
 
 
2682
 
 
2683
/*
 
2684
  Bring first local table of first most outer select to first place in global
 
2685
  table list
 
2686
 
 
2687
  SYNOPSYS
 
2688
     st_lex::first_lists_tables_same()
 
2689
 
 
2690
  NOTES
 
2691
    In many cases (for example, usual INSERT/DELETE/...) the first table of
 
2692
    main SELECT_LEX have special meaning => check that it is the first table
 
2693
    in global list and re-link to be first in the global list if it is
 
2694
    necessary.  We need such re-linking only for queries with sub-queries in
 
2695
    the select list, as only in this case tables of sub-queries will go to
 
2696
    the global list first.
 
2697
*/
 
2698
 
 
2699
void st_lex::first_lists_tables_same()
 
2700
{
 
2701
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
 
2702
  if (query_tables != first_table && first_table != 0)
 
2703
  {
 
2704
    TABLE_LIST *next;
 
2705
    if (query_tables_last == &first_table->next_global)
 
2706
      query_tables_last= first_table->prev_global;
 
2707
 
 
2708
    if ((next= *first_table->prev_global= first_table->next_global))
 
2709
      next->prev_global= first_table->prev_global;
 
2710
    /* include in new place */
 
2711
    first_table->next_global= query_tables;
 
2712
    /*
 
2713
       We are sure that query_tables is not 0, because first_table was not
 
2714
       first table in the global list => we can use
 
2715
       query_tables->prev_global without check of query_tables
 
2716
    */
 
2717
    query_tables->prev_global= &first_table->next_global;
 
2718
    first_table->prev_global= &query_tables;
 
2719
    query_tables= first_table;
 
2720
  }
 
2721
}
 
2722
 
 
2723
 
 
2724
/*
 
2725
  Link table back that was unlinked with unlink_first_table()
 
2726
 
 
2727
  SYNOPSIS
 
2728
    link_first_table_back()
 
2729
    link_to_local       do we need link this table to local
 
2730
 
 
2731
  RETURN
 
2732
    global list
 
2733
*/
 
2734
 
 
2735
void st_lex::link_first_table_back(TABLE_LIST *first,
 
2736
                                   bool link_to_local)
 
2737
{
 
2738
  if (first)
 
2739
  {
 
2740
    if ((first->next_global= query_tables))
 
2741
      query_tables->prev_global= &first->next_global;
 
2742
    else
 
2743
      query_tables_last= &first->next_global;
 
2744
    query_tables= first;
 
2745
 
 
2746
    if (link_to_local)
 
2747
    {
 
2748
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
 
2749
      select_lex.context.table_list= first;
 
2750
      select_lex.table_list.first= (uchar*) first;
 
2751
      select_lex.table_list.elements++; //safety
 
2752
    }
 
2753
  }
 
2754
}
 
2755
 
 
2756
 
 
2757
 
 
2758
/*
 
2759
  cleanup lex for case when we open table by table for processing
 
2760
 
 
2761
  SYNOPSIS
 
2762
    st_lex::cleanup_after_one_table_open()
 
2763
 
 
2764
  NOTE
 
2765
    This method is mostly responsible for cleaning up of selects lists and
 
2766
    derived tables state. To rollback changes in Query_tables_list one has
 
2767
    to call Query_tables_list::reset_query_tables_list(FALSE).
 
2768
*/
 
2769
 
 
2770
void st_lex::cleanup_after_one_table_open()
 
2771
{
 
2772
  /*
 
2773
    thd->lex->derived_tables & additional units may be set if we open
 
2774
    a view. It is necessary to clear thd->lex->derived_tables flag
 
2775
    to prevent processing of derived tables during next open_and_lock_tables
 
2776
    if next table is a real table and cleanup & remove underlying units
 
2777
    NOTE: all units will be connected to thd->lex->select_lex, because we
 
2778
    have not UNION on most upper level.
 
2779
    */
 
2780
  if (all_selects_list != &select_lex)
 
2781
  {
 
2782
    derived_tables= 0;
 
2783
    /* cleunup underlying units (units of VIEW) */
 
2784
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
 
2785
         un;
 
2786
         un= un->next_unit())
 
2787
      un->cleanup();
 
2788
    /* reduce all selects list to default state */
 
2789
    all_selects_list= &select_lex;
 
2790
    /* remove underlying units (units of VIEW) subtree */
 
2791
    select_lex.cut_subtree();
 
2792
  }
 
2793
}
 
2794
 
 
2795
 
 
2796
/*
 
2797
  Save current state of Query_tables_list for this LEX, and prepare it
 
2798
  for processing of new statemnt.
 
2799
 
 
2800
  SYNOPSIS
 
2801
    reset_n_backup_query_tables_list()
 
2802
      backup  Pointer to Query_tables_list instance to be used for backup
 
2803
*/
 
2804
 
 
2805
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup)
 
2806
{
 
2807
  backup->set_query_tables_list(this);
 
2808
  /*
 
2809
    We have to perform full initialization here since otherwise we
 
2810
    will damage backed up state.
 
2811
  */
 
2812
  this->reset_query_tables_list(TRUE);
 
2813
}
 
2814
 
 
2815
 
 
2816
/*
 
2817
  Restore state of Query_tables_list for this LEX from backup.
 
2818
 
 
2819
  SYNOPSIS
 
2820
    restore_backup_query_tables_list()
 
2821
      backup  Pointer to Query_tables_list instance used for backup
 
2822
*/
 
2823
 
 
2824
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup)
 
2825
{
 
2826
  this->destroy_query_tables_list();
 
2827
  this->set_query_tables_list(backup);
 
2828
}
 
2829
 
 
2830
 
 
2831
/*
 
2832
  Checks for usage of routines and/or tables in a parsed statement
 
2833
 
 
2834
  SYNOPSIS
 
2835
    st_lex:table_or_sp_used()
 
2836
 
 
2837
  RETURN
 
2838
    FALSE  No routines and tables used
 
2839
    TRUE   Either or both routines and tables are used.
 
2840
*/
 
2841
 
 
2842
bool st_lex::table_or_sp_used()
 
2843
{
 
2844
  DBUG_ENTER("table_or_sp_used");
 
2845
 
 
2846
  if (sroutines.records || query_tables)
 
2847
    DBUG_RETURN(TRUE);
 
2848
 
 
2849
  DBUG_RETURN(FALSE);
 
2850
}
 
2851
 
 
2852
 
 
2853
/*
 
2854
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
 
2855
 
 
2856
  SYNOPSIS
 
2857
    fix_prepare_info_in_table_list()
 
2858
      thd  Thread handle
 
2859
      tbl  List of tables to process
 
2860
 
 
2861
  DESCRIPTION
 
2862
    Perform end-end-of prepare fixup for list of tables, if any of the tables
 
2863
    is a merge-algorithm VIEW, recursively fix up its underlying tables as
 
2864
    well.
 
2865
 
 
2866
*/
 
2867
 
 
2868
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
 
2869
{
 
2870
  for (; tbl; tbl= tbl->next_local)
 
2871
  {
 
2872
    if (tbl->on_expr)
 
2873
    {
 
2874
      tbl->prep_on_expr= tbl->on_expr;
 
2875
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2876
    }
 
2877
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2878
  }
 
2879
}
 
2880
 
 
2881
 
 
2882
/*
 
2883
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
 
2884
 
 
2885
  SYNOPSIS
 
2886
    st_select_lex::fix_prepare_information
 
2887
      thd          thread handler
 
2888
      conds        in/out pointer to WHERE condition to be met at execution
 
2889
      having_conds in/out pointer to HAVING condition to be met at execution
 
2890
  
 
2891
  DESCRIPTION
 
2892
    The passed WHERE and HAVING are to be saved for the future executions.
 
2893
    This function saves it, and returns a copy which can be thrashed during
 
2894
    this execution of the statement. By saving/thrashing here we mean only
 
2895
    AND/OR trees.
 
2896
    The function also calls fix_prepare_info_in_table_list that saves all
 
2897
    ON expressions.    
 
2898
*/
 
2899
 
 
2900
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
 
2901
                                            Item **having_conds)
 
2902
{
 
2903
  if (!thd->stmt_arena->is_conventional() && first_execution)
 
2904
  {
 
2905
    first_execution= 0;
 
2906
    if (*conds)
 
2907
    {
 
2908
      prep_where= *conds;
 
2909
      *conds= where= prep_where->copy_andor_structure(thd);
 
2910
    }
 
2911
    if (*having_conds)
 
2912
    {
 
2913
      prep_having= *having_conds;
 
2914
      *having_conds= having= prep_having->copy_andor_structure(thd);
 
2915
    }
 
2916
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
 
2917
  }
 
2918
}
 
2919
 
 
2920
 
 
2921
/*
 
2922
  There are st_select_lex::add_table_to_list &
 
2923
  st_select_lex::set_lock_for_tables are in sql_parse.cc
 
2924
 
 
2925
  st_select_lex::print is in sql_select.cc
 
2926
 
 
2927
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
 
2928
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
 
2929
  st_select_lex_unit::change_result
 
2930
  are in sql_union.cc
 
2931
*/
 
2932
 
 
2933
/*
 
2934
  Sets the kind of hints to be added by the calls to add_index_hint().
 
2935
 
 
2936
  SYNOPSIS
 
2937
    set_index_hint_type()
 
2938
      type_arg     The kind of hints to be added from now on.
 
2939
      clause       The clause to use for hints to be added from now on.
 
2940
 
 
2941
  DESCRIPTION
 
2942
    Used in filling up the tagged hints list.
 
2943
    This list is filled by first setting the kind of the hint as a 
 
2944
    context variable and then adding hints of the current kind.
 
2945
    Then the context variable index_hint_type can be reset to the
 
2946
    next hint type.
 
2947
*/
 
2948
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2949
                                        index_clause_map clause)
 
2950
 
2951
  current_index_hint_type= type_arg;
 
2952
  current_index_hint_clause= clause;
 
2953
}
 
2954
 
 
2955
 
 
2956
/*
 
2957
  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
 
2958
 
 
2959
  SYNOPSIS
 
2960
    alloc_index_hints()
 
2961
      thd         current thread.
 
2962
*/
 
2963
 
 
2964
void st_select_lex::alloc_index_hints (THD *thd)
 
2965
 
2966
  index_hints= new (thd->mem_root) List<Index_hint>(); 
 
2967
}
 
2968
 
 
2969
 
 
2970
 
 
2971
/*
 
2972
  adds an element to the array storing index usage hints 
 
2973
  (ADD/FORCE/IGNORE INDEX).
 
2974
 
 
2975
  SYNOPSIS
 
2976
    add_index_hint()
 
2977
      thd         current thread.
 
2978
      str         name of the index.
 
2979
      length      number of characters in str.
 
2980
 
 
2981
  RETURN VALUE
 
2982
    0 on success, non-zero otherwise
 
2983
*/
 
2984
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
 
2985
{
 
2986
  return index_hints->push_front (new (thd->mem_root) 
 
2987
                                 Index_hint(current_index_hint_type,
 
2988
                                            current_index_hint_clause,
 
2989
                                            str, length));
 
2990
}
 
2991
 
 
2992
/**
 
2993
  A routine used by the parser to decide whether we are specifying a full
 
2994
  partitioning or if only partitions to add or to split.
 
2995
 
 
2996
  @note  This needs to be outside of WITH_PARTITION_STORAGE_ENGINE since it
 
2997
  is used from the sql parser that doesn't have any ifdef's
 
2998
 
 
2999
  @retval  TRUE    Yes, it is part of a management partition command
 
3000
  @retval  FALSE          No, not a management partition command
 
3001
*/
 
3002
 
 
3003
bool st_lex::is_partition_management() const
 
3004
{
 
3005
  return (sql_command == SQLCOM_ALTER_TABLE &&
 
3006
          (alter_info.flags == ALTER_ADD_PARTITION ||
 
3007
           alter_info.flags == ALTER_REORGANIZE_PARTITION));
 
3008
}
 
3009