sql_lex.cc 83.4 KB
Newer Older
1
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13 14 15 16 17 18
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* A lexical scanner on a temporary buffer with a yacc interface */

19
#define MYSQL_LEX 1
unknown's avatar
unknown committed
20 21 22 23
#include "mysql_priv.h"
#include "item_create.h"
#include <m_ctype.h>
#include <hash.h>
24 25
#include "sp.h"
#include "sp_head.h"
unknown's avatar
unknown committed
26

27 28
static int lex_one_token(void *arg, void *yythd);

29 30 31 32
/*
  We are using pointer to this variable for distinguishing between assignment
  to NEW row field (when parsing trigger definition) and structured variable.
*/
33 34

sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
35

36 37 38 39
/**
  LEX_STRING constant for null-string to be used in parser and other places.
*/
const LEX_STRING null_lex_str= {NULL, 0};
40
const LEX_STRING empty_lex_str= { (char*) "", 0 };
41 42 43 44 45 46 47 48 49 50
/**
  @note The order of the elements of this array must correspond to
  the order of elements in enum_binlog_stmt_unsafe.
*/
const int
Query_tables_list::binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT] =
{
  ER_BINLOG_UNSAFE_LIMIT,
  ER_BINLOG_UNSAFE_INSERT_DELAYED,
  ER_BINLOG_UNSAFE_SYSTEM_TABLE,
51
  ER_BINLOG_UNSAFE_AUTOINC_COLUMNS,
52 53
  ER_BINLOG_UNSAFE_UDF,
  ER_BINLOG_UNSAFE_SYSTEM_VARIABLE,
54
  ER_BINLOG_UNSAFE_SYSTEM_FUNCTION,
55
  ER_BINLOG_UNSAFE_NONTRANS_AFTER_TRANS,
56 57
  ER_BINLOG_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
  ER_BINLOG_UNSAFE_MIXED_STATEMENT,
58 59
};

60

61
/* Longest standard keyword name */
62

unknown's avatar
unknown committed
63 64
#define TOCK_NAME_LENGTH 24

65 66
/*
  The following data is based on the latin1 character set, and is only
unknown's avatar
unknown committed
67 68 69
  used when comparing keywords
*/

unknown's avatar
unknown committed
70 71
static uchar to_upper_lex[]=
{
unknown's avatar
unknown committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
};

90 91 92 93 94 95 96 97 98 99 100
/* 
  Names of the index hints (for error messages). Keep in sync with 
  index_hint_type 
*/

const char * index_hint_type_name[] =
{
  "IGNORE INDEX", 
  "USE INDEX", 
  "FORCE INDEX"
};
101

unknown's avatar
unknown committed
102 103 104 105 106 107 108
inline int lex_casecmp(const char *s, const char *t, uint len)
{
  while (len-- != 0 &&
	 to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
  return (int) len+1;
}

unknown's avatar
unknown committed
109
#include <lex_hash.h>
unknown's avatar
unknown committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131


void lex_init(void)
{
  uint i;
  DBUG_ENTER("lex_init");
  for (i=0 ; i < array_elements(symbols) ; i++)
    symbols[i].length=(uchar) strlen(symbols[i].name);
  for (i=0 ; i < array_elements(sql_functions) ; i++)
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);

  DBUG_VOID_RETURN;
}


void lex_free(void)
{					// Call this when daemon ends
  DBUG_ENTER("lex_free");
  DBUG_VOID_RETURN;
}


132 133 134 135 136 137 138 139 140
void
st_parsing_options::reset()
{
  allows_variable= TRUE;
  allows_select_into= TRUE;
  allows_select_procedure= TRUE;
  allows_derived= TRUE;
}

141 142 143 144 145 146 147
Lex_input_stream::Lex_input_stream(THD *thd,
                                   const char* buffer,
                                   unsigned int length)
: m_thd(thd),
  yylineno(1),
  yytoklen(0),
  yylval(NULL),
148 149
  lookahead_token(-1),
  lookahead_yylval(NULL),
150 151 152 153 154 155
  m_ptr(buffer),
  m_tok_start(NULL),
  m_tok_end(NULL),
  m_end_of_query(buffer + length),
  m_tok_start_prev(NULL),
  m_buf(buffer),
unknown's avatar
unknown committed
156
  m_buf_length(length),
unknown's avatar
unknown committed
157
  m_echo(TRUE),
158 159 160
  m_cpp_tok_start(NULL),
  m_cpp_tok_start_prev(NULL),
  m_cpp_tok_end(NULL),
unknown's avatar
unknown committed
161 162
  m_body_utf8(NULL),
  m_cpp_utf8_processed_ptr(NULL),
163 164
  next_state(MY_LEX_START),
  found_semicolon(NULL),
165
  ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)),
166
  stmt_prepare_mode(FALSE),
167
  multi_statements(TRUE),
unknown's avatar
unknown committed
168 169
  in_comment(NO_COMMENT),
  m_underscore_cs(NULL)
170
{
171 172
  m_cpp_buf= (char*) thd->alloc(length + 1);
  m_cpp_ptr= m_cpp_buf;
173 174 175 176 177
}

Lex_input_stream::~Lex_input_stream()
{}

unknown's avatar
unknown committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/**
  The operation is called from the parser in order to
  1) designate the intention to have utf8 body;
  1) Indicate to the lexer that we will need a utf8 representation of this
     statement;
  2) Determine the beginning of the body.

  @param thd        Thread context.
  @param begin_ptr  Pointer to the start of the body in the pre-processed
                    buffer.
*/

void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
{
  DBUG_ASSERT(begin_ptr);
  DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);

  uint body_utf8_length=
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
    my_charset_utf8_bin.mbmaxlen;

  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
  m_body_utf8_ptr= m_body_utf8;
  *m_body_utf8_ptr= 0;

  m_cpp_utf8_processed_ptr= begin_ptr;
}

/**
unknown's avatar
unknown committed
207
  @brief The operation appends unprocessed part of pre-processed buffer till
unknown's avatar
unknown committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.

  The idea is that some tokens in the pre-processed buffer (like character
  set introducers) should be skipped.

  Example:
    CPP buffer: SELECT 'str1', _latin1 'str2';
    m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
    In order to skip "_latin1", the following call should be made:
      body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)

  @param ptr      Pointer in the pre-processed buffer, which specifies the
                  end of the chunk, which should be appended to the utf8
                  body.
  @param end_ptr  Pointer in the pre-processed buffer, to which
                  m_cpp_utf8_processed_ptr will be set in the end of the
                  operation.
*/

void Lex_input_stream::body_utf8_append(const char *ptr,
                                        const char *end_ptr)
{
  DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
  DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);

  if (!m_body_utf8)
    return;

  if (m_cpp_utf8_processed_ptr >= ptr)
    return;

  int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;

  memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
  m_body_utf8_ptr += bytes_to_copy;
  *m_body_utf8_ptr= 0;

  m_cpp_utf8_processed_ptr= end_ptr;
}

/**
  The operation appends unprocessed part of the pre-processed buffer till
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.

  @param ptr  Pointer in the pre-processed buffer, which specifies the end
              of the chunk, which should be appended to the utf8 body.
*/

void Lex_input_stream::body_utf8_append(const char *ptr)
{
  body_utf8_append(ptr, ptr);
}

/**
  The operation converts the specified text literal to the utf8 and appends
  the result to the utf8-body.

  @param thd      Thread context.
  @param txt      Text literal.
  @param txt_cs   Character set of the text literal.
  @param end_ptr  Pointer in the pre-processed buffer, to which
                  m_cpp_utf8_processed_ptr will be set in the end of the
                  operation.
*/

void Lex_input_stream::body_utf8_append_literal(THD *thd,
                                                const LEX_STRING *txt,
                                                CHARSET_INFO *txt_cs,
                                                const char *end_ptr)
{
  if (!m_cpp_utf8_processed_ptr)
    return;

  LEX_STRING utf_txt;

unknown's avatar
unknown committed
283
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
unknown's avatar
unknown committed
284 285 286
  {
    thd->convert_string(&utf_txt,
                        &my_charset_utf8_general_ci,
287
                        txt->str, (uint) txt->length,
unknown's avatar
unknown committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
                        txt_cs);
  }
  else
  {
    utf_txt.str= txt->str;
    utf_txt.length= txt->length;
  }

  /* NOTE: utf_txt.length is in bytes, not in symbols. */

  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
  m_body_utf8_ptr += utf_txt.length;
  *m_body_utf8_ptr= 0;

  m_cpp_utf8_processed_ptr= end_ptr;
}

305

306 307 308 309 310 311
/*
  This is called before every query that is to be parsed.
  Because of this, it's critical to not do too much things here.
  (We already do too much here)
*/

312
void lex_start(THD *thd)
unknown's avatar
unknown committed
313
{
unknown's avatar
unknown committed
314
  LEX *lex= thd->lex;
unknown's avatar
unknown committed
315 316 317 318
  DBUG_ENTER("lex_start");

  lex->thd= lex->unit.thd= thd;

unknown's avatar
unknown committed
319
  lex->context_stack.empty();
320 321
  lex->unit.init_query();
  lex->unit.init_select();
unknown's avatar
unknown committed
322 323
  /* 'parent_lex' is used in init_query() so it must be before it. */
  lex->select_lex.parent_lex= lex;
324 325
  lex->select_lex.init_query();
  lex->value_list.empty();
unknown's avatar
unknown committed
326
  lex->update_list.empty();
327
  lex->set_var_list.empty();
328
  lex->param_list.empty();
unknown's avatar
unknown committed
329
  lex->view_list.empty();
330
  lex->prepared_stmt_params.empty();
331
  lex->auxiliary_table_list.empty();
332 333 334 335 336 337 338 339 340 341
  lex->unit.next= lex->unit.master=
    lex->unit.link_next= lex->unit.return_to= 0;
  lex->unit.prev= lex->unit.link_prev= 0;
  lex->unit.slave= lex->unit.global_parameters= lex->current_select=
    lex->all_selects_list= &lex->select_lex;
  lex->select_lex.master= &lex->unit;
  lex->select_lex.prev= &lex->unit.slave;
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
  lex->select_lex.options= 0;
342
  lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
unknown's avatar
unknown committed
343 344
  lex->select_lex.init_order();
  lex->select_lex.group_list.empty();
345
  lex->describe= 0;
unknown's avatar
unknown committed
346
  lex->subqueries= FALSE;
unknown's avatar
unknown committed
347
  lex->view_prepare_mode= FALSE;
unknown's avatar
unknown committed
348
  lex->derived_tables= 0;
349 350
  lex->lock_option= TL_READ;
  lex->safe_to_cache_query= 1;
351
  lex->leaf_tables_insert= 0;
352
  lex->parsing_options.reset();
unknown's avatar
unknown committed
353
  lex->empty_field_list_on_rset= 0;
354
  lex->select_lex.select_number= 1;
unknown's avatar
unknown committed
355
  lex->length=0;
356
  lex->part_info= 0;
unknown's avatar
unknown committed
357
  lex->select_lex.in_sum_expr=0;
unknown's avatar
unknown committed
358
  lex->select_lex.ftfunc_list_alloc.empty();
359
  lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
unknown's avatar
unknown committed
360 361
  lex->select_lex.group_list.empty();
  lex->select_lex.order_list.empty();
362
  lex->sql_command= SQLCOM_END;
363
  lex->duplicates= DUP_ERROR;
364
  lex->ignore= 0;
unknown's avatar
unknown committed
365
  lex->spname= NULL;
366 367
  lex->sphead= NULL;
  lex->spcont= NULL;
unknown's avatar
unknown committed
368
  lex->proc_list.first= 0;
unknown's avatar
unknown committed
369
  lex->escape_used= FALSE;
unknown's avatar
unknown committed
370
  lex->query_tables= 0;
371
  lex->reset_query_tables_list(FALSE);
372
  lex->expr_allows_subselect= TRUE;
373
  lex->use_only_table_context= FALSE;
374

375 376
  lex->name.str= 0;
  lex->name.length= 0;
unknown's avatar
unknown committed
377
  lex->event_parse_data= NULL;
unknown's avatar
unknown committed
378
  lex->profile_options= PROFILE_NONE;
unknown's avatar
unknown committed
379 380 381
  lex->nest_level=0 ;
  lex->allow_sum_func= 0;
  lex->in_sum_func= NULL;
382
  lex->protect_against_global_read_lock= FALSE;
unknown's avatar
unknown committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  /*
    ok, there must be a better solution for this, long-term
    I tried "bzero" in the sql_yacc.yy code, but that for
    some reason made the values zero, even if they were set
  */
  lex->server_options.server_name= 0;
  lex->server_options.server_name_length= 0;
  lex->server_options.host= 0;
  lex->server_options.db= 0;
  lex->server_options.username= 0;
  lex->server_options.password= 0;
  lex->server_options.scheme= 0;
  lex->server_options.socket= 0;
  lex->server_options.owner= 0;
  lex->server_options.port= -1;
unknown's avatar
unknown committed
398

399
  lex->is_lex_started= TRUE;
unknown's avatar
unknown committed
400
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
401 402 403 404
}

void lex_end(LEX *lex)
{
unknown's avatar
unknown committed
405
  DBUG_ENTER("lex_end");
unknown's avatar
unknown committed
406
  DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
unknown's avatar
unknown committed
407 408 409 410 411 412

  /* release used plugins */
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
                     lex->plugins.elements);
  reset_dynamic(&lex->plugins);

unknown's avatar
unknown committed
413
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
414 415
}

416 417 418 419
Yacc_state::~Yacc_state()
{
  if (yacc_yyss)
  {
420 421
    my_free(yacc_yyss, MYF(0));
    my_free(yacc_yyvs, MYF(0));
422
  }
unknown's avatar
unknown committed
423 424
}

425
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
unknown's avatar
unknown committed
426
{
427
  const char *tok= lip->get_tok_start();
unknown's avatar
unknown committed
428

429
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
unknown's avatar
unknown committed
430 431
  if (symbol)
  {
432 433 434 435
    lip->yylval->symbol.symbol=symbol;
    lip->yylval->symbol.str= (char*) tok;
    lip->yylval->symbol.length=len;

436
    if ((symbol->tok == NOT_SYM) &&
437
        (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE))
438 439
      return NOT2_SYM;
    if ((symbol->tok == OR_OR_SYM) &&
440
	!(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT))
441
      return OR2_SYM;
442

unknown's avatar
unknown committed
443 444 445 446 447
    return symbol->tok;
  }
  return 0;
}

448 449 450 451 452
/*
  Check if name is a keyword

  SYNOPSIS
    is_keyword()
453
    name      checked name (must not be empty)
454 455 456 457 458 459 460 461 462
    len       length of checked name

  RETURN VALUES
    0         name is a keyword
    1         name isn't a keyword
*/

bool is_keyword(const char *name, uint len)
{
463
  DBUG_ASSERT(len != 0);
464 465
  return get_hash_symbol(name,len,0)!=0;
}
unknown's avatar
unknown committed
466

467 468 469 470 471
/**
  Check if name is a sql function

    @param name      checked name

472
    @return is this a native function or not
473 474 475 476
    @retval 0         name is a function
    @retval 1         name isn't a function
*/

477 478 479
bool is_lex_native_function(const LEX_STRING *name)
{
  DBUG_ASSERT(name != NULL);
480
  return (get_hash_symbol(name->str, (uint) name->length, 1) != 0);
481 482
}

unknown's avatar
unknown committed
483 484
/* make a copy of token before ptr and set yytoklen */

485
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
unknown's avatar
unknown committed
486 487
{
  LEX_STRING tmp;
488
  lip->yyUnget();                       // ptr points now after last token char
489
  tmp.length=lip->yytoklen=length;
490
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
unknown's avatar
unknown committed
491 492 493 494

  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;

unknown's avatar
unknown committed
495 496 497
  return tmp;
}

498 499 500 501 502 503 504
/* 
 todo: 
   There are no dangerous charsets in mysql for function 
   get_quoted_token yet. But it should be fixed in the 
   future to operate multichar strings (like ucs2)
*/

505
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
506
                                   uint skip,
507
                                   uint length, char quote)
508 509
{
  LEX_STRING tmp;
510 511
  const char *from, *end;
  char *to;
512
  lip->yyUnget();                       // ptr points now after last token char
513 514
  tmp.length= lip->yytoklen=length;
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
515
  from= lip->get_tok_start() + skip;
516
  to= tmp.str;
517
  end= to+length;
unknown's avatar
unknown committed
518 519 520 521

  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
  lip->m_cpp_text_end= lip->m_cpp_text_start + length;

522
  for ( ; to != end; )
523
  {
524
    if ((*to++= *from++) == quote)
unknown's avatar
unknown committed
525
    {
526
      from++;					// Skip double quotes
unknown's avatar
unknown committed
527 528
      lip->m_cpp_text_start++;
    }
529 530 531 532 533 534 535 536 537 538
  }
  *to= 0;					// End null for safety
  return tmp;
}


/*
  Return an unescaped text literal without quotes
  Fix sometimes to do only one scan of the string
*/
unknown's avatar
unknown committed
539

540
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
unknown's avatar
unknown committed
541 542 543
{
  reg1 uchar c,sep;
  uint found_escape=0;
544
  CHARSET_INFO *cs= lip->m_thd->charset();
unknown's avatar
unknown committed
545

546
  lip->tok_bitmap= 0;
547 548
  sep= lip->yyGetLast();                        // String should end with this
  while (! lip->eof())
unknown's avatar
unknown committed
549
  {
550
    c= lip->yyGet();
551
    lip->tok_bitmap|= c;
unknown's avatar
unknown committed
552
#ifdef USE_MB
553 554 555
    {
      int l;
      if (use_mb(cs) &&
556 557 558 559 560
          (l = my_ismbchar(cs,
                           lip->get_ptr() -1,
                           lip->get_end_of_query()))) {
        lip->skip_binary(l-1);
        continue;
561
      }
unknown's avatar
unknown committed
562 563
    }
#endif
564
    if (c == '\\' &&
565
        !(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES))
unknown's avatar
unknown committed
566 567
    {					// Escaped character
      found_escape=1;
568
      if (lip->eof())
unknown's avatar
unknown committed
569
	return 0;
570
      lip->yySkip();
unknown's avatar
unknown committed
571 572 573
    }
    else if (c == sep)
    {
574
      if (c == lip->yyGet())            // Check if two separators in a row
unknown's avatar
unknown committed
575
      {
576
        found_escape=1;                 // duplicate. Remember for delete
unknown's avatar
unknown committed
577 578 579
	continue;
      }
      else
580
        lip->yyUnget();
unknown's avatar
unknown committed
581 582

      /* Found end. Unescape and return string */
583 584
      const char *str, *end;
      char *start;
unknown's avatar
unknown committed
585

586 587 588 589 590 591 592
      str= lip->get_tok_start();
      end= lip->get_ptr();
      /* Extract the text from the token */
      str += pre_skip;
      end -= post_skip;
      DBUG_ASSERT(end >= str);

593
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
unknown's avatar
unknown committed
594
	return (char*) "";		// Sql_alloc has set error flag
unknown's avatar
unknown committed
595 596 597 598

      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;

unknown's avatar
unknown committed
599 600
      if (!found_escape)
      {
601 602 603
	lip->yytoklen=(uint) (end-str);
	memcpy(start,str,lip->yytoklen);
	start[lip->yytoklen]=0;
unknown's avatar
unknown committed
604 605 606
      }
      else
      {
607
        char *to;
608

unknown's avatar
unknown committed
609 610 611 612
	for (to=start ; str != end ; str++)
	{
#ifdef USE_MB
	  int l;
unknown's avatar
unknown committed
613
	  if (use_mb(cs) &&
614
              (l = my_ismbchar(cs, str, end))) {
unknown's avatar
unknown committed
615 616 617 618 619 620
	      while (l--)
		  *to++ = *str++;
	      str--;
	      continue;
	  }
#endif
621
	  if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
622
              *str == '\\' && str+1 != end)
unknown's avatar
unknown committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	  {
	    switch(*++str) {
	    case 'n':
	      *to++='\n';
	      break;
	    case 't':
	      *to++= '\t';
	      break;
	    case 'r':
	      *to++ = '\r';
	      break;
	    case 'b':
	      *to++ = '\b';
	      break;
	    case '0':
	      *to++= 0;			// Ascii null
	      break;
	    case 'Z':			// ^Z must be escaped on Win32
	      *to++='\032';
	      break;
	    case '_':
	    case '%':
	      *to++= '\\';		// remember prefix for wildcard
	      /* Fall through */
	    default:
648
              *to++= *str;
unknown's avatar
unknown committed
649 650 651
	      break;
	    }
	  }
652 653
	  else if (*str == sep)
	    *to++= *str++;		// Two ' or "
unknown's avatar
unknown committed
654 655 656 657
	  else
	    *to++ = *str;
	}
	*to=0;
658
	lip->yytoklen=(uint) (to-start);
unknown's avatar
unknown committed
659
      }
660
      return start;
unknown's avatar
unknown committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    }
  }
  return 0;					// unexpected end of query
}


/*
** Calc type of integer; long integer, longlong integer or real.
** Returns smallest type that match the string.
** When using unsigned long long values the result is converted to a real
** because else they will be unexpected sign changes because all calculation
** is done with longlong or double.
*/

static const char *long_str="2147483647";
static const uint long_len=10;
static const char *signed_long_str="-2147483648";
static const char *longlong_str="9223372036854775807";
static const uint longlong_len=19;
static const char *signed_longlong_str="-9223372036854775808";
static const uint signed_longlong_len=19;
unknown's avatar
unknown committed
682 683
static const char *unsigned_longlong_str="18446744073709551615";
static const uint unsigned_longlong_len=20;
unknown's avatar
unknown committed
684

unknown's avatar
unknown committed
685
static inline uint int_token(const char *str,uint length)
unknown's avatar
unknown committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
  if (length < long_len)			// quick normal case
    return NUM;
  bool neg=0;

  if (*str == '+')				// Remove sign and pre-zeros
  {
    str++; length--;
  }
  else if (*str == '-')
  {
    str++; length--;
    neg=1;
  }
  while (*str == '0' && length)
  {
    str++; length --;
  }
  if (length < long_len)
    return NUM;

  uint smaller,bigger;
  const char *cmp;
  if (neg)
  {
    if (length == long_len)
    {
      cmp= signed_long_str+1;
      smaller=NUM;				// If <= signed_long_str
      bigger=LONG_NUM;				// If >= signed_long_str
    }
    else if (length < signed_longlong_len)
      return LONG_NUM;
    else if (length > signed_longlong_len)
unknown's avatar
unknown committed
720
      return DECIMAL_NUM;
unknown's avatar
unknown committed
721 722 723 724
    else
    {
      cmp=signed_longlong_str+1;
      smaller=LONG_NUM;				// If <= signed_longlong_str
unknown's avatar
unknown committed
725
      bigger=DECIMAL_NUM;
unknown's avatar
unknown committed
726 727 728 729 730 731 732 733 734 735 736 737 738
    }
  }
  else
  {
    if (length == long_len)
    {
      cmp= long_str;
      smaller=NUM;
      bigger=LONG_NUM;
    }
    else if (length < longlong_len)
      return LONG_NUM;
    else if (length > longlong_len)
unknown's avatar
unknown committed
739 740
    {
      if (length > unsigned_longlong_len)
unknown's avatar
unknown committed
741
        return DECIMAL_NUM;
unknown's avatar
unknown committed
742 743
      cmp=unsigned_longlong_str;
      smaller=ULONGLONG_NUM;
unknown's avatar
unknown committed
744
      bigger=DECIMAL_NUM;
unknown's avatar
unknown committed
745
    }
unknown's avatar
unknown committed
746 747 748 749
    else
    {
      cmp=longlong_str;
      smaller=LONG_NUM;
750
      bigger= ULONGLONG_NUM;
unknown's avatar
unknown committed
751 752 753 754 755 756
    }
  }
  while (*cmp && *cmp++ == *str++) ;
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
}

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

/**
  Given a stream that is advanced to the first contained character in 
  an open comment, consume the comment.  Optionally, if we are allowed, 
  recurse so that we understand comments within this current comment.

  At this level, we do not support version-condition comments.  We might 
  have been called with having just passed one in the stream, though.  In 
  that case, we probably want to tolerate mundane comments inside.  Thus,
  the case for recursion.

  @retval  Whether EOF reached before comment is closed.
*/
bool consume_comment(Lex_input_stream *lip, int remaining_recursions_permitted)
{
  reg1 uchar c;
  while (! lip->eof())
  {
    c= lip->yyGet();

    if (remaining_recursions_permitted > 0)
    {
      if ((c == '/') && (lip->yyPeek() == '*'))
      {
        lip->yySkip(); /* Eat asterisk */
        consume_comment(lip, remaining_recursions_permitted-1);
        continue;
      }
    }

    if (c == '*')
    {
      if (lip->yyPeek() == '/')
      {
        lip->yySkip(); /* Eat slash */
        return FALSE;
      }
    }

    if (c == '\n')
      lip->yylineno++;
  }

  return TRUE;
}


804
/*
805
  MYSQLlex remember the following states from the following MYSQLlex()
806 807 808 809 810

  - MY_LEX_EOQ			Found end of query
  - MY_LEX_OPERATOR_OR_IDENT	Last state was an ident, text or number
				(which can't be followed by a signed number)
*/
unknown's avatar
unknown committed
811

812
int MYSQLlex(void *arg, void *yythd)
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
{
  THD *thd= (THD *)yythd;
  Lex_input_stream *lip= & thd->m_parser_state->m_lip;
  YYSTYPE *yylval=(YYSTYPE*) arg;
  int token;

  if (lip->lookahead_token >= 0)
  {
    /*
      The next token was already parsed in advance,
      return it.
    */
    token= lip->lookahead_token;
    lip->lookahead_token= -1;
    *yylval= *(lip->lookahead_yylval);
    lip->lookahead_yylval= NULL;
    return token;
  }

  token= lex_one_token(arg, yythd);

  switch(token) {
  case WITH:
    /*
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
      which makes the grammar LALR(2).
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
      to transform the grammar into a LALR(1) grammar,
      which sql_yacc.yy can process.
    */
    token= lex_one_token(arg, yythd);
    switch(token) {
    case CUBE_SYM:
      return WITH_CUBE_SYM;
    case ROLLUP_SYM:
      return WITH_ROLLUP_SYM;
    default:
      /*
        Save the token following 'WITH'
      */
      lip->lookahead_yylval= lip->yylval;
      lip->yylval= NULL;
      lip->lookahead_token= token;
      return WITH;
    }
    break;
  default:
    break;
  }

  return token;
}

int lex_one_token(void *arg, void *yythd)
unknown's avatar
unknown committed
867
{
Staale Smedseng's avatar
Staale Smedseng committed
868
  reg1	uchar c= 0;
869
  bool comment_closed;
870
  int	tokval, result_state;
unknown's avatar
unknown committed
871
  uint length;
unknown's avatar
unknown committed
872
  enum my_lex_states state;
873
  THD *thd= (THD *)yythd;
874
  Lex_input_stream *lip= & thd->m_parser_state->m_lip;
875
  LEX *lex= thd->lex;
unknown's avatar
unknown committed
876
  YYSTYPE *yylval=(YYSTYPE*) arg;
877
  CHARSET_INFO *cs= thd->charset();
878 879
  uchar *state_map= cs->state_map;
  uchar *ident_map= cs->ident_map;
unknown's avatar
unknown committed
880

881
  lip->yylval=yylval;			// The global state
882

883
  lip->start_token();
884 885
  state=lip->next_state;
  lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
unknown's avatar
unknown committed
886 887
  for (;;)
  {
888
    switch (state) {
889 890
    case MY_LEX_OPERATOR_OR_IDENT:	// Next is operator or keyword
    case MY_LEX_START:			// Start of token
891 892
      // Skip starting whitespace
      while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
unknown's avatar
unknown committed
893 894
      {
	if (c == '\n')
895
	  lip->yylineno++;
896 897

        lip->yySkip();
unknown's avatar
unknown committed
898
      }
899 900 901 902

      /* Start of real token */
      lip->restart_token();
      c= lip->yyGet();
903
      state= (enum my_lex_states) state_map[c];
unknown's avatar
unknown committed
904
      break;
905
    case MY_LEX_ESCAPE:
906
      if (lip->yyGet() == 'N')
unknown's avatar
unknown committed
907 908 909 910 911
      {					// Allow \N as shortcut for NULL
	yylval->lex_str.str=(char*) "\\N";
	yylval->lex_str.length=2;
	return NULL_SYM;
      }
912 913
    case MY_LEX_CHAR:			// Unknown or single char token
    case MY_LEX_SKIP:			// This should not happen
914 915 916
      if (c == '-' && lip->yyPeek() == '-' &&
          (my_isspace(cs,lip->yyPeekn(1)) ||
           my_iscntrl(cs,lip->yyPeekn(1))))
917 918 919 920
      {
        state=MY_LEX_COMMENT;
        break;
      }
921

unknown's avatar
unknown committed
922
      if (c != ')')
923
	lip->next_state= MY_LEX_START;	// Allow signed numbers
924

unknown's avatar
unknown committed
925
      if (c == ',')
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
      {
        /*
          Warning:
          This is a work around, to make the "remember_name" rule in
          sql/sql_yacc.yy work properly.
          The problem is that, when parsing "select expr1, expr2",
          the code generated by bison executes the *pre* action
          remember_name (see select_item) *before* actually parsing the
          first token of expr2.
        */
        lip->restart_token();
      }
      else
      {
        /*
          Check for a placeholder: it should not precede a possible identifier
          because of binlogging: when a placeholder is replaced with
          its value in a query for the binlog, the query must stay
          grammatically correct.
        */
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[lip->yyPeek()])
947
        return(PARAM_MARKER);
948 949
      }

unknown's avatar
unknown committed
950 951
      return((int) c);

unknown's avatar
unknown committed
952
    case MY_LEX_IDENT_OR_NCHAR:
953 954
      if (lip->yyPeek() != '\'')
      {
unknown's avatar
unknown committed
955 956 957
	state= MY_LEX_IDENT;
	break;
      }
958
      /* Found N'string' */
959 960
      lip->yySkip();                         // Skip '
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
unknown's avatar
unknown committed
961
      {
962 963
	state= MY_LEX_CHAR;             // Read char by char
	break;
unknown's avatar
unknown committed
964
      }
965
      yylval->lex_str.length= lip->yytoklen;
966
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
967
      return(NCHAR_STRING);
unknown's avatar
unknown committed
968

969
    case MY_LEX_IDENT_OR_HEX:
970
      if (lip->yyPeek() == '\'')
971
      {					// Found x'hex-number'
972
	state= MY_LEX_HEX_NUMBER;
973 974
	break;
      }
unknown's avatar
unknown committed
975
    case MY_LEX_IDENT_OR_BIN:
976
      if (lip->yyPeek() == '\'')
unknown's avatar
unknown committed
977 978 979 980
      {                                 // Found b'bin-number'
        state= MY_LEX_BIN_NUMBER;
        break;
      }
981
    case MY_LEX_IDENT:
982
      const char *start;
unknown's avatar
unknown committed
983
#if defined(USE_MB) && defined(USE_MB_IDENT)
984
      if (use_mb(cs))
unknown's avatar
unknown committed
985
      {
986
	result_state= IDENT_QUOTED;
987
        if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
unknown's avatar
unknown committed
988
        {
989 990 991
          int l = my_ismbchar(cs,
                              lip->get_ptr() -1,
                              lip->get_end_of_query());
unknown's avatar
unknown committed
992
          if (l == 0) {
993
            state = MY_LEX_CHAR;
unknown's avatar
unknown committed
994 995
            continue;
          }
996
          lip->skip_binary(l - 1);
unknown's avatar
unknown committed
997
        }
998
        while (ident_map[c=lip->yyGet()])
unknown's avatar
unknown committed
999
        {
1000
          if (my_mbcharlen(cs, c) > 1)
unknown's avatar
unknown committed
1001 1002
          {
            int l;
1003 1004 1005
            if ((l = my_ismbchar(cs,
                                 lip->get_ptr() -1,
                                 lip->get_end_of_query())) == 0)
unknown's avatar
unknown committed
1006
              break;
1007
            lip->skip_binary(l-1);
unknown's avatar
unknown committed
1008 1009 1010 1011 1012
          }
        }
      }
      else
#endif
1013
      {
Staale Smedseng's avatar
Staale Smedseng committed
1014
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) ;
unknown's avatar
unknown committed
1015 1016
        /* If there were non-ASCII characters, mark that we must convert */
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1017
      }
1018 1019
      length= lip->yyLength();
      start= lip->get_ptr();
1020
      if (lip->ignore_space)
unknown's avatar
unknown committed
1021
      {
unknown's avatar
unknown committed
1022 1023 1024 1025
        /*
          If we find a space then this can't be an identifier. We notice this
          below by checking start != lex->ptr.
        */
Staale Smedseng's avatar
Staale Smedseng committed
1026
        for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) ;
unknown's avatar
unknown committed
1027
      }
1028
      if (start == lip->get_ptr() && c == '.' && ident_map[lip->yyPeek()])
1029
	lip->next_state=MY_LEX_IDENT_SEP;
unknown's avatar
unknown committed
1030 1031
      else
      {					// '(' must follow directly if function
1032 1033
        lip->yyUnget();
	if ((tokval = find_keyword(lip, length, c == '(')))
unknown's avatar
unknown committed
1034
	{
1035
	  lip->next_state= MY_LEX_START;	// Allow signed numbers
unknown's avatar
unknown committed
1036 1037
	  return(tokval);		// Was keyword
	}
1038
        lip->yySkip();                  // next state does a unget
unknown's avatar
unknown committed
1039
      }
1040
      yylval->lex_str=get_token(lip, 0, length);
1041

unknown's avatar
unknown committed
1042
      /*
1043 1044
         Note: "SELECT _bla AS 'alias'"
         _bla should be considered as a IDENT if charset haven't been found.
unknown's avatar
unknown committed
1045
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
1046 1047 1048
         producing an error.
      */

unknown's avatar
unknown committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
      if (yylval->lex_str.str[0] == '_')
      {
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
                                                MY_CS_PRIMARY, MYF(0));
        if (cs)
        {
          yylval->charset= cs;
          lip->m_underscore_cs= cs;

          lip->body_utf8_append(lip->m_cpp_text_start,
                                lip->get_cpp_tok_start() + length);
          return(UNDERSCORE_CHARSET);
        }
      }

      lip->body_utf8_append(lip->m_cpp_text_start);

      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
                                    lip->m_cpp_text_end);

1069
      return(result_state);			// IDENT or IDENT_QUOTED
unknown's avatar
unknown committed
1070

1071
    case MY_LEX_IDENT_SEP:		// Found ident and now '.'
1072 1073 1074
      yylval->lex_str.str= (char*) lip->get_ptr();
      yylval->lex_str.length= 1;
      c= lip->yyGet();                  // should be '.'
1075
      lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
1076
      if (!ident_map[lip->yyPeek()])            // Probably ` or "
1077
	lip->next_state= MY_LEX_START;
unknown's avatar
unknown committed
1078 1079
      return((int) c);

1080
    case MY_LEX_NUMBER_IDENT:		// number or ident which num-start
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
      if (lip->yyGetLast() == '0')
      {
        c= lip->yyGet();
        if (c == 'x')
        {
          while (my_isxdigit(cs,(c = lip->yyGet()))) ;
          if ((lip->yyLength() >= 3) && !ident_map[c])
          {
            /* skip '0x' */
            yylval->lex_str=get_token(lip, 2, lip->yyLength()-2);
            return (HEX_NUM);
          }
          lip->yyUnget();
          state= MY_LEX_IDENT_START;
          break;
        }
        else if (c == 'b')
        {
Staale Smedseng's avatar
Staale Smedseng committed
1099
          while ((c= lip->yyGet()) == '0' || c == '1') ;
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
          if ((lip->yyLength() >= 3) && !ident_map[c])
          {
            /* Skip '0b' */
            yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
            return (BIN_NUM);
          }
          lip->yyUnget();
          state= MY_LEX_IDENT_START;
          break;
        }
        lip->yyUnget();
      }

      while (my_isdigit(cs, (c = lip->yyGet()))) ;
unknown's avatar
unknown committed
1114
      if (!ident_map[c])
unknown's avatar
unknown committed
1115
      {					// Can't be identifier
1116
	state=MY_LEX_INT_OR_REAL;
unknown's avatar
unknown committed
1117 1118 1119 1120
	break;
      }
      if (c == 'e' || c == 'E')
      {
unknown's avatar
unknown committed
1121
	// The following test is written this way to allow numbers of type 1e1
1122 1123
        if (my_isdigit(cs,lip->yyPeek()) ||
            (c=(lip->yyGet())) == '+' || c == '-')
unknown's avatar
unknown committed
1124
	{				// Allow 1E+10
1125
          if (my_isdigit(cs,lip->yyPeek()))     // Number must have digit after sign
unknown's avatar
unknown committed
1126
	  {
1127 1128 1129
            lip->yySkip();
            while (my_isdigit(cs,lip->yyGet())) ;
            yylval->lex_str=get_token(lip, 0, lip->yyLength());
unknown's avatar
unknown committed
1130 1131 1132
	    return(FLOAT_NUM);
	  }
	}
1133
        lip->yyUnget();
unknown's avatar
unknown committed
1134
      }
unknown's avatar
unknown committed
1135
      // fall through
1136
    case MY_LEX_IDENT_START:			// We come here after '.'
1137
      result_state= IDENT;
unknown's avatar
unknown committed
1138
#if defined(USE_MB) && defined(USE_MB_IDENT)
1139
      if (use_mb(cs))
unknown's avatar
unknown committed
1140
      {
1141
	result_state= IDENT_QUOTED;
1142
        while (ident_map[c=lip->yyGet()])
unknown's avatar
unknown committed
1143
        {
1144
          if (my_mbcharlen(cs, c) > 1)
unknown's avatar
unknown committed
1145 1146
          {
            int l;
1147 1148 1149
            if ((l = my_ismbchar(cs,
                                 lip->get_ptr() -1,
                                 lip->get_end_of_query())) == 0)
unknown's avatar
unknown committed
1150
              break;
1151
            lip->skip_binary(l-1);
unknown's avatar
unknown committed
1152 1153 1154 1155 1156
          }
        }
      }
      else
#endif
unknown's avatar
unknown committed
1157
      {
Staale Smedseng's avatar
Staale Smedseng committed
1158
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) ;
unknown's avatar
unknown committed
1159 1160 1161
        /* If there were non-ASCII characters, mark that we must convert */
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
      }
1162
      if (c == '.' && ident_map[lip->yyPeek()])
1163
	lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
unknown's avatar
unknown committed
1164

1165
      yylval->lex_str= get_token(lip, 0, lip->yyLength());
unknown's avatar
unknown committed
1166 1167 1168 1169 1170

      lip->body_utf8_append(lip->m_cpp_text_start);

      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
                                    lip->m_cpp_text_end);
unknown's avatar
unknown committed
1171

1172
      return(result_state);
unknown's avatar
unknown committed
1173

1174
    case MY_LEX_USER_VARIABLE_DELIMITER:	// Found quote char
1175
    {
1176 1177
      uint double_quotes= 0;
      char quote_char= c;                       // Used char
1178
      while ((c=lip->yyGet()))
unknown's avatar
unknown committed
1179
      {
1180 1181
	int var_length;
	if ((var_length= my_mbcharlen(cs, c)) == 1)
1182 1183 1184
	{
	  if (c == quote_char)
	  {
1185
            if (lip->yyPeek() != quote_char)
1186
	      break;
1187
            c=lip->yyGet();
1188 1189 1190
	    double_quotes++;
	    continue;
	  }
1191 1192
	}
#ifdef USE_MB
Davi Arnaut's avatar
Davi Arnaut committed
1193
        else if (use_mb(cs))
1194
        {
Davi Arnaut's avatar
Davi Arnaut committed
1195 1196 1197
          if ((var_length= my_ismbchar(cs, lip->get_ptr() - 1,
                                       lip->get_end_of_query())))
            lip->skip_binary(var_length-1);
1198
        }
1199
#endif
unknown's avatar
unknown committed
1200
      }
1201
      if (double_quotes)
1202
	yylval->lex_str=get_quoted_token(lip, 1,
1203
                                         lip->yyLength() - double_quotes -1,
1204 1205
					 quote_char);
      else
1206
        yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
1207
      if (c == quote_char)
1208
        lip->yySkip();                  // Skip end `
1209
      lip->next_state= MY_LEX_START;
unknown's avatar
unknown committed
1210 1211 1212 1213 1214 1215

      lip->body_utf8_append(lip->m_cpp_text_start);

      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
                                    lip->m_cpp_text_end);

1216
      return(IDENT_QUOTED);
1217
    }
1218
    case MY_LEX_INT_OR_REAL:		// Complete int or incomplete real
unknown's avatar
unknown committed
1219 1220
      if (c != '.')
      {					// Found complete integer number.
1221
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
1222
	return int_token(yylval->lex_str.str, (uint) yylval->lex_str.length);
unknown's avatar
unknown committed
1223 1224
      }
      // fall through
1225
    case MY_LEX_REAL:			// Incomplete real number
1226
      while (my_isdigit(cs,c = lip->yyGet())) ;
unknown's avatar
unknown committed
1227 1228 1229

      if (c == 'e' || c == 'E')
      {
1230
        c = lip->yyGet();
unknown's avatar
unknown committed
1231
	if (c == '-' || c == '+')
1232
          c = lip->yyGet();                     // Skip sign
1233
	if (!my_isdigit(cs,c))
unknown's avatar
unknown committed
1234
	{				// No digit after sign
1235
	  state= MY_LEX_CHAR;
unknown's avatar
unknown committed
1236 1237
	  break;
	}
1238 1239
        while (my_isdigit(cs,lip->yyGet())) ;
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
unknown's avatar
unknown committed
1240 1241
	return(FLOAT_NUM);
      }
1242
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
unknown's avatar
unknown committed
1243
      return(DECIMAL_NUM);
unknown's avatar
unknown committed
1244

1245
    case MY_LEX_HEX_NUMBER:		// Found x'hexstring'
1246 1247 1248 1249 1250 1251 1252 1253
      lip->yySkip();                    // Accept opening '
      while (my_isxdigit(cs, (c= lip->yyGet()))) ;
      if (c != '\'')
        return(ABORT_SYM);              // Illegal hex constant
      lip->yySkip();                    // Accept closing '
      length= lip->yyLength();          // Length of hexnum+3
      if ((length % 2) == 0)
        return(ABORT_SYM);              // odd number of hex digits
1254 1255 1256
      yylval->lex_str=get_token(lip,
                                2,          // skip x'
                                length-3);  // don't count x' and last '
1257 1258
      return (HEX_NUM);

unknown's avatar
unknown committed
1259
    case MY_LEX_BIN_NUMBER:           // Found b'bin-string'
1260
      lip->yySkip();                  // Accept opening '
Staale Smedseng's avatar
Staale Smedseng committed
1261
      while ((c= lip->yyGet()) == '0' || c == '1') ;
unknown's avatar
unknown committed
1262
      if (c != '\'')
1263 1264 1265
        return(ABORT_SYM);            // Illegal hex constant
      lip->yySkip();                  // Accept closing '
      length= lip->yyLength();        // Length of bin-num + 3
1266 1267 1268 1269
      yylval->lex_str= get_token(lip,
                                 2,         // skip b'
                                 length-3); // don't count b' and last '
      return (BIN_NUM);
unknown's avatar
unknown committed
1270

1271
    case MY_LEX_CMP_OP:			// Incomplete comparison operator
1272 1273 1274 1275
      if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
          state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
        lip->yySkip();
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
unknown's avatar
unknown committed
1276
      {
1277
	lip->next_state= MY_LEX_START;	// Allow signed numbers
unknown's avatar
unknown committed
1278 1279
	return(tokval);
      }
1280
      state = MY_LEX_CHAR;		// Something fishy found
unknown's avatar
unknown committed
1281 1282
      break;

1283
    case MY_LEX_LONG_CMP_OP:		// Incomplete comparison operator
1284 1285
      if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
          state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
unknown's avatar
unknown committed
1286
      {
1287 1288 1289
        lip->yySkip();
        if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP)
          lip->yySkip();
unknown's avatar
unknown committed
1290
      }
1291
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
unknown's avatar
unknown committed
1292
      {
1293
	lip->next_state= MY_LEX_START;	// Found long op
unknown's avatar
unknown committed
1294 1295
	return(tokval);
      }
1296
      state = MY_LEX_CHAR;		// Something fishy found
unknown's avatar
unknown committed
1297 1298
      break;

1299
    case MY_LEX_BOOL:
1300
      if (c != lip->yyPeek())
unknown's avatar
unknown committed
1301
      {
1302
	state=MY_LEX_CHAR;
unknown's avatar
unknown committed
1303 1304
	break;
      }
1305
      lip->yySkip();
1306 1307
      tokval = find_keyword(lip,2,0);	// Is a bool operator
      lip->next_state= MY_LEX_START;	// Allow signed numbers
unknown's avatar
unknown committed
1308 1309
      return(tokval);

1310
    case MY_LEX_STRING_OR_DELIMITER:
1311
      if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
1312
      {
1313
	state= MY_LEX_USER_VARIABLE_DELIMITER;
1314 1315 1316
	break;
      }
      /* " used for strings */
1317
    case MY_LEX_STRING:			// Incomplete text string
1318
      if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
unknown's avatar
unknown committed
1319
      {
1320
	state= MY_LEX_CHAR;		// Read char by char
unknown's avatar
unknown committed
1321 1322
	break;
      }
1323
      yylval->lex_str.length=lip->yytoklen;
unknown's avatar
unknown committed
1324 1325 1326 1327 1328 1329 1330 1331 1332

      lip->body_utf8_append(lip->m_cpp_text_start);

      lip->body_utf8_append_literal(thd, &yylval->lex_str,
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
        lip->m_cpp_text_end);

      lip->m_underscore_cs= NULL;

1333
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
unknown's avatar
unknown committed
1334 1335
      return(TEXT_STRING);

1336
    case MY_LEX_COMMENT:			//  Comment
unknown's avatar
unknown committed
1337
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1338 1339
      while ((c = lip->yyGet()) != '\n' && c) ;
      lip->yyUnget();                   // Safety against eof
1340
      state = MY_LEX_START;		// Try again
unknown's avatar
unknown committed
1341
      break;
1342
    case MY_LEX_LONG_COMMENT:		/* Long C comment? */
1343
      if (lip->yyPeek() != '*')
unknown's avatar
unknown committed
1344
      {
1345
	state=MY_LEX_CHAR;		// Probable division
unknown's avatar
unknown committed
1346 1347
	break;
      }
unknown's avatar
unknown committed
1348
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1349 1350 1351
      /* Reject '/' '*', since we might need to turn off the echo */
      lip->yyUnget();

1352 1353
      lip->save_in_comment_state();

1354
      if (lip->yyPeekn(2) == '!')
unknown's avatar
unknown committed
1355
      {
1356 1357
        lip->in_comment= DISCARD_COMMENT;
        /* Accept '/' '*' '!', but do not keep this marker. */
unknown's avatar
unknown committed
1358
        lip->set_echo(FALSE);
1359 1360 1361 1362 1363 1364 1365
        lip->yySkip();
        lip->yySkip();
        lip->yySkip();

        /*
          The special comment format is very strict:
          '/' '*' '!', followed by exactly
1366 1367 1368 1369
          1 digit (major), 2 digits (minor), then 2 digits (dot).
          32302 -> 3.23.02
          50032 -> 5.0.32
          50114 -> 5.1.14
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        */
        char version_str[6];
        version_str[0]= lip->yyPeekn(0);
        version_str[1]= lip->yyPeekn(1);
        version_str[2]= lip->yyPeekn(2);
        version_str[3]= lip->yyPeekn(3);
        version_str[4]= lip->yyPeekn(4);
        version_str[5]= 0;
        if (  my_isdigit(cs, version_str[0])
           && my_isdigit(cs, version_str[1])
           && my_isdigit(cs, version_str[2])
           && my_isdigit(cs, version_str[3])
           && my_isdigit(cs, version_str[4])
           )
        {
          ulong version;
          version=strtol(version_str, NULL, 10);

1388
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1389 1390 1391 1392 1393
          lip->yySkipn(5);

          if (version <= MYSQL_VERSION_ID)
          {
            /* Expand the content of the special comment as real code */
unknown's avatar
unknown committed
1394
            lip->set_echo(TRUE);
1395
            state=MY_LEX_START;
1396 1397 1398 1399 1400 1401
            break;  /* Do not treat contents as a comment.  */
          }
          else
          {
            comment_closed= ! consume_comment(lip, 1);
            /* version allowed to have one level of comment inside. */
1402 1403 1404 1405
          }
        }
        else
        {
1406
          /* Not a version comment. */
1407
          state=MY_LEX_START;
unknown's avatar
unknown committed
1408
          lip->set_echo(TRUE);
1409 1410
          break;
        }
unknown's avatar
unknown committed
1411
      }
1412
      else
unknown's avatar
unknown committed
1413
      {
1414 1415 1416
        lip->in_comment= PRESERVE_COMMENT;
        lip->yySkip();                  // Accept /
        lip->yySkip();                  // Accept *
1417 1418
        comment_closed= ! consume_comment(lip, 0);
        /* regular comments can have zero comments inside. */
unknown's avatar
unknown committed
1419
      }
1420 1421 1422 1423 1424
      /*
        Discard:
        - regular '/' '*' comments,
        - special comments '/' '*' '!' for a future version,
        by scanning until we find a closing '*' '/' marker.
1425 1426 1427 1428 1429 1430 1431 1432 1433

        Nesting regular comments isn't allowed.  The first 
        '*' '/' returns the parser to the previous state.

        /#!VERSI oned containing /# regular #/ is allowed #/

		Inside one versioned comment, another versioned comment
		is treated as a regular discardable comment.  It gets
		no special parsing.
1434
      */
1435

1436 1437 1438
      /* Unbalanced comments with a missing '*' '/' are a syntax error */
      if (! comment_closed)
        return (ABORT_SYM);
1439
      state = MY_LEX_START;             // Try again
1440
      lip->restore_in_comment_state();
unknown's avatar
unknown committed
1441
      break;
1442
    case MY_LEX_END_LONG_COMMENT:
1443
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
unknown's avatar
unknown committed
1444
      {
1445 1446 1447 1448 1449 1450
        /* Reject '*' '/' */
        lip->yyUnget();
        /* Accept '*' '/', with the proper echo */
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
        lip->yySkipn(2);
        /* And start recording the tokens again */
unknown's avatar
unknown committed
1451
        lip->set_echo(TRUE);
1452 1453
        lip->in_comment=NO_COMMENT;
        state=MY_LEX_START;
unknown's avatar
unknown committed
1454 1455
      }
      else
1456
	state=MY_LEX_CHAR;		// Return '*'
unknown's avatar
unknown committed
1457
      break;
1458
    case MY_LEX_SET_VAR:		// Check if ':='
1459
      if (lip->yyPeek() != '=')
unknown's avatar
unknown committed
1460
      {
1461
	state=MY_LEX_CHAR;		// Return ':'
unknown's avatar
unknown committed
1462 1463
	break;
      }
1464
      lip->yySkip();
unknown's avatar
unknown committed
1465
      return (SET_VAR);
1466
    case MY_LEX_SEMICOLON:			// optional line terminator
1467 1468
      state= MY_LEX_CHAR;               // Return ';'
      break;
1469
    case MY_LEX_EOL:
1470
      if (lip->eof())
unknown's avatar
unknown committed
1471
      {
1472
        lip->yyUnget();                 // Reject the last '\0'
unknown's avatar
unknown committed
1473
        lip->set_echo(FALSE);
1474
        lip->yySkip();
unknown's avatar
unknown committed
1475
        lip->set_echo(TRUE);
1476 1477 1478
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
        if (lip->in_comment != NO_COMMENT)
          return (ABORT_SYM);
1479 1480
        lip->next_state=MY_LEX_END;     // Mark for next loop
        return(END_OF_INPUT);
unknown's avatar
unknown committed
1481 1482 1483
      }
      state=MY_LEX_CHAR;
      break;
1484
    case MY_LEX_END:
1485
      lip->next_state=MY_LEX_END;
unknown's avatar
unknown committed
1486
      return(0);			// We found end of input last time
1487

1488
      /* Actually real shouldn't start with . but allow them anyhow */
1489
    case MY_LEX_REAL_OR_POINT:
1490
      if (my_isdigit(cs,lip->yyPeek()))
1491
	state = MY_LEX_REAL;		// Real
unknown's avatar
unknown committed
1492 1493
      else
      {
1494
	state= MY_LEX_IDENT_SEP;	// return '.'
1495
        lip->yyUnget();                 // Put back '.'
unknown's avatar
unknown committed
1496 1497
      }
      break;
1498
    case MY_LEX_USER_END:		// end '@' of user@hostname
1499
      switch (state_map[lip->yyPeek()]) {
1500 1501 1502
      case MY_LEX_STRING:
      case MY_LEX_USER_VARIABLE_DELIMITER:
      case MY_LEX_STRING_OR_DELIMITER:
unknown's avatar
unknown committed
1503
	break;
1504
      case MY_LEX_USER_END:
1505
	lip->next_state=MY_LEX_SYSTEM_VAR;
unknown's avatar
unknown committed
1506 1507
	break;
      default:
1508
	lip->next_state=MY_LEX_HOSTNAME;
unknown's avatar
unknown committed
1509 1510
	break;
      }
1511
      yylval->lex_str.str=(char*) lip->get_ptr();
unknown's avatar
unknown committed
1512 1513
      yylval->lex_str.length=1;
      return((int) '@');
1514
    case MY_LEX_HOSTNAME:		// end '@' of user@hostname
1515
      for (c=lip->yyGet() ;
1516
	   my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
1517 1518
           c= lip->yyGet()) ;
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
unknown's avatar
unknown committed
1519
      return(LEX_HOSTNAME);
1520
    case MY_LEX_SYSTEM_VAR:
1521
      yylval->lex_str.str=(char*) lip->get_ptr();
unknown's avatar
unknown committed
1522
      yylval->lex_str.length=1;
1523 1524
      lip->yySkip();                                    // Skip '@'
      lip->next_state= (state_map[lip->yyPeek()] ==
1525 1526 1527
			MY_LEX_USER_VARIABLE_DELIMITER ?
			MY_LEX_OPERATOR_OR_IDENT :
			MY_LEX_IDENT_OR_KEYWORD);
unknown's avatar
unknown committed
1528
      return((int) '@');
1529
    case MY_LEX_IDENT_OR_KEYWORD:
unknown's avatar
unknown committed
1530 1531 1532 1533 1534
      /*
	We come here when we have found two '@' in a row.
	We should now be able to handle:
	[(global | local | session) .]variable_name
      */
1535

Staale Smedseng's avatar
Staale Smedseng committed
1536
      for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) ;
unknown's avatar
unknown committed
1537 1538
      /* If there were non-ASCII characters, mark that we must convert */
      result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1539

unknown's avatar
unknown committed
1540
      if (c == '.')
1541
	lip->next_state=MY_LEX_IDENT_SEP;
1542 1543
      length= lip->yyLength();
      if (length == 0)
unknown's avatar
unknown committed
1544
        return(ABORT_SYM);              // Names must be nonempty.
1545
      if ((tokval= find_keyword(lip, length,0)))
unknown's avatar
unknown committed
1546
      {
1547
        lip->yyUnget();                         // Put back 'c'
unknown's avatar
unknown committed
1548 1549
	return(tokval);				// Was keyword
      }
1550
      yylval->lex_str=get_token(lip, 0, length);
unknown's avatar
unknown committed
1551 1552 1553 1554 1555 1556

      lip->body_utf8_append(lip->m_cpp_text_start);

      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
                                    lip->m_cpp_text_end);

1557
      return(result_state);
unknown's avatar
unknown committed
1558 1559 1560
    }
  }
}
unknown's avatar
unknown committed
1561

1562

unknown's avatar
unknown committed
1563 1564 1565
/**
  Construct a copy of this object to be used for mysql_alter_table
  and mysql_create_table.
1566

unknown's avatar
unknown committed
1567 1568 1569 1570
  Historically, these two functions modify their Alter_info
  arguments. This behaviour breaks re-execution of prepared
  statements and stored procedures and is compensated by always
  supplying a copy of Alter_info to these functions.
1571

unknown's avatar
unknown committed
1572 1573
  @return You need to use check the error in THD for out
  of memory condition after calling this function.
1574 1575
*/

1576 1577 1578 1579 1580 1581 1582 1583 1584
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
  :drop_list(rhs.drop_list, mem_root),
  alter_list(rhs.alter_list, mem_root),
  key_list(rhs.key_list, mem_root),
  create_list(rhs.create_list, mem_root),
  flags(rhs.flags),
  keys_onoff(rhs.keys_onoff),
  tablespace_op(rhs.tablespace_op),
  partition_names(rhs.partition_names, mem_root),
1585
  num_parts(rhs.num_parts),
unknown's avatar
unknown committed
1586 1587 1588
  change_level(rhs.change_level),
  datetime_field(rhs.datetime_field),
  error_if_not_empty(rhs.error_if_not_empty)
1589
{
1590 1591 1592
  /*
    Make deep copies of used objects.
    This is not a fully deep copy - clone() implementations
unknown's avatar
unknown committed
1593
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
    do not copy string constants. At the same length the only
    reason we make a copy currently is that ALTER/CREATE TABLE
    code changes input Alter_info definitions, but string
    constants never change.
  */
  list_copy_and_replace_each_value(drop_list, mem_root);
  list_copy_and_replace_each_value(alter_list, mem_root);
  list_copy_and_replace_each_value(key_list, mem_root);
  list_copy_and_replace_each_value(create_list, mem_root);
  /* partition_names are not deeply copied currently */
}


1607 1608 1609 1610 1611 1612 1613
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
{
  /*
    TODO:
    This code assumes that there are no multi-bytes characters
    that can be considered white-space.
  */
1614

1615 1616 1617 1618 1619
  while ((str->length > 0) && (my_isspace(cs, str->str[0])))
  {
    str->length --;
    str->str ++;
  }
1620

1621 1622 1623 1624 1625 1626 1627 1628
  /*
    FIXME:
    Also, parsing backward is not safe with multi bytes characters
  */
  while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
  {
    str->length --;
  }
1629 1630
}

1631

unknown's avatar
unknown committed
1632 1633 1634 1635 1636 1637
/*
  st_select_lex structures initialisations
*/

void st_select_lex_node::init_query()
{
1638
  options= 0;
1639
  sql_cache= SQL_CACHE_UNSPECIFIED;
1640
  linkage= UNSPECIFIED_TYPE;
1641 1642
  no_error= no_table_names_allowed= 0;
  uncacheable= 0;
unknown's avatar
unknown committed
1643 1644 1645 1646 1647 1648 1649 1650 1651
}

void st_select_lex_node::init_select()
{
}

void st_select_lex_unit::init_query()
{
  st_select_lex_node::init_query();
1652
  linkage= GLOBAL_OPTIONS_TYPE;
unknown's avatar
unknown committed
1653
  global_parameters= first_select();
1654 1655
  select_limit_cnt= HA_POS_ERROR;
  offset_limit_cnt= 0;
1656
  union_distinct= 0;
unknown's avatar
unknown committed
1657
  prepared= optimized= executed= 0;
unknown's avatar
unknown committed
1658
  item= 0;
1659 1660
  union_result= 0;
  table= 0;
unknown's avatar
unknown committed
1661
  fake_select_lex= 0;
1662
  cleaned= 0;
1663
  item_list.empty();
1664
  describe= 0;
1665
  found_rows_for_union= 0;
unknown's avatar
unknown committed
1666 1667 1668 1669 1670
}

void st_select_lex::init_query()
{
  st_select_lex_node::init_query();
1671
  table_list.empty();
1672 1673
  top_join_list.empty();
  join_list= &top_join_list;
1674
  embedding= leaf_tables= 0;
unknown's avatar
unknown committed
1675
  item_list.empty();
unknown's avatar
unknown committed
1676
  join= 0;
1677
  having= prep_having= where= prep_where= 0;
1678
  olap= UNSPECIFIED_OLAP_TYPE;
1679
  having_fix_field= 0;
1680
  group_fix_field= 0;
1681 1682
  context.select_lex= this;
  context.init();
unknown's avatar
unknown committed
1683 1684 1685
  /*
    Add the name resolution context of the current (sub)query to the
    stack of contexts for the whole query.
1686 1687 1688 1689 1690
    TODO:
    push_context may return an error if there is no memory for a new
    element in the stack, however this method has no return value,
    thus push_context should be moved to a place where query
    initialization is checked for failure.
unknown's avatar
unknown committed
1691 1692
  */
  parent_lex->push_context(&context);
1693
  cond_count= between_count= with_wild= 0;
unknown's avatar
unknown committed
1694
  max_equal_elems= 0;
unknown's avatar
unknown committed
1695
  ref_pointer_array= 0;
unknown's avatar
unknown committed
1696
  select_n_where_fields= 0;
unknown's avatar
unknown committed
1697
  select_n_having_items= 0;
1698
  subquery_in_having= explicit_limit= 0;
1699
  is_item_list_lookup= 0;
1700
  first_execution= 1;
1701
  first_natural_join_processing= 1;
unknown's avatar
unknown committed
1702
  first_cond_optimization= 1;
1703
  parsing_place= NO_MATTER;
1704
  exclude_from_table_unique_test= no_wrap_view_item= FALSE;
unknown's avatar
unknown committed
1705
  nest_level= 0;
1706
  link_next= 0;
1707
  lock_option= TL_READ_DEFAULT;
unknown's avatar
unknown committed
1708 1709 1710 1711 1712
}

void st_select_lex::init_select()
{
  st_select_lex_node::init_select();
1713
  group_list.empty();
unknown's avatar
unknown committed
1714
  type= db= 0;
1715 1716 1717
  having= 0;
  table_join_options= 0;
  in_sum_expr= with_wild= 0;
unknown's avatar
unknown committed
1718
  options= 0;
1719
  sql_cache= SQL_CACHE_UNSPECIFIED;
1720
  braces= 0;
unknown's avatar
unknown committed
1721
  interval_list.empty();
unknown's avatar
unknown committed
1722
  ftfunc_list_alloc.empty();
unknown's avatar
unknown committed
1723
  inner_sum_func_list= 0;
unknown's avatar
unknown committed
1724
  ftfunc_list= &ftfunc_list_alloc;
unknown's avatar
unknown committed
1725
  linkage= UNSPECIFIED_TYPE;
unknown's avatar
unknown committed
1726 1727
  order_list.elements= 0;
  order_list.first= 0;
1728
  order_list.next= (uchar**) &order_list.first;
1729 1730 1731
  /* Set limit and offset to default values */
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
  offset_limit= 0;      /* denotes the default offset = 0 */
unknown's avatar
unknown committed
1732
  with_sum_func= 0;
1733
  is_correlated= 0;
1734 1735
  cur_pos_in_select_list= UNDEF_POS;
  non_agg_fields.empty();
1736
  cond_value= having_value= Item::COND_UNDEF;
1737
  inner_refs_list.empty();
1738
  full_group_by_flag= 0;
unknown's avatar
unknown committed
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
}

/*
  st_select_lex structures linking
*/

/* include on level down */
void st_select_lex_node::include_down(st_select_lex_node *upper)
{
  if ((next= upper->slave))
    next->prev= &next;
  prev= &upper->slave;
  upper->slave= this;
  master= upper;
unknown's avatar
unknown committed
1753
  slave= 0;
unknown's avatar
unknown committed
1754 1755
}

unknown's avatar
unknown committed
1756 1757
/*
  include on level down (but do not link)
unknown's avatar
unknown committed
1758

unknown's avatar
unknown committed
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
  SYNOPSYS
    st_select_lex_node::include_standalone()
    upper - reference on node underr which this node should be included
    ref - references on reference on this node
*/
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
					    st_select_lex_node **ref)
{
  next= 0;
  prev= ref;
  master= upper;
  slave= 0;
}

unknown's avatar
unknown committed
1773 1774 1775 1776 1777 1778 1779 1780
/* include neighbour (on same level) */
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
{
  if ((next= before->next))
    next->prev= &next;
  prev= &before->next;
  before->next= this;
  master= before->master;
unknown's avatar
unknown committed
1781
  slave= 0;
unknown's avatar
unknown committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
}

/* including in global SELECT_LEX list */
void st_select_lex_node::include_global(st_select_lex_node **plink)
{
  if ((link_next= *plink))
    link_next->link_prev= &link_next;
  link_prev= plink;
  *plink= this;
}

//excluding from global list (internal function)
void st_select_lex_node::fast_exclude()
{
unknown's avatar
unknown committed
1796
  if (link_prev)
unknown's avatar
unknown committed
1797 1798 1799 1800
  {
    if ((*link_prev= link_next))
      link_next->link_prev= link_prev;
  }
1801 1802 1803 1804
  // Remove slave structure
  for (; slave; slave= slave->next)
    slave->fast_exclude();
  
unknown's avatar
unknown committed
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
}

/*
  excluding select_lex structure (except first (first select can't be
  deleted, because it is most upper select))
*/
void st_select_lex_node::exclude()
{
  //exclude from global list
  fast_exclude();
  //exclude from other structures
  if ((*prev= next))
    next->prev= prev;
  /* 
     We do not need following statements, because prev pointer of first 
     list element point to master->slave
     if (master->slave == this)
       master->slave= next;
  */
}
1825

1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

/*
  Exclude level of current unit from tree of SELECTs

  SYNOPSYS
    st_select_lex_unit::exclude_level()

  NOTE: units which belong to current will be brought up on level of
  currernt unit 
*/
unknown's avatar
unknown committed
1836 1837 1838
void st_select_lex_unit::exclude_level()
{
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
unknown's avatar
unknown committed
1839
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
unknown's avatar
unknown committed
1840
  {
unknown's avatar
unknown committed
1841
    // unlink current level from global SELECTs list
unknown's avatar
unknown committed
1842 1843
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
      sl->link_next->link_prev= sl->link_prev;
unknown's avatar
unknown committed
1844 1845

    // bring up underlay levels
unknown's avatar
unknown committed
1846 1847
    SELECT_LEX_UNIT **last= 0;
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
unknown's avatar
unknown committed
1848 1849
    {
      u->master= master;
unknown's avatar
unknown committed
1850
      last= (SELECT_LEX_UNIT**)&(u->next);
unknown's avatar
unknown committed
1851
    }
unknown's avatar
unknown committed
1852 1853 1854 1855 1856 1857 1858 1859
    if (last)
    {
      (*units_last)= sl->first_inner_unit();
      units_last= last;
    }
  }
  if (units)
  {
unknown's avatar
unknown committed
1860
    // include brought up levels in place of current
unknown's avatar
unknown committed
1861 1862
    (*prev)= units;
    (*units_last)= (SELECT_LEX_UNIT*)next;
unknown's avatar
unknown committed
1863 1864 1865
    if (next)
      next->prev= (SELECT_LEX_NODE**)units_last;
    units->prev= prev;
unknown's avatar
unknown committed
1866 1867
  }
  else
unknown's avatar
unknown committed
1868 1869
  {
    // exclude currect unit from list of nodes
unknown's avatar
unknown committed
1870
    (*prev)= next;
unknown's avatar
unknown committed
1871 1872 1873
    if (next)
      next->prev= prev;
  }
unknown's avatar
unknown committed
1874 1875
}

1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886

/*
  Exclude subtree of current unit from tree of SELECTs

  SYNOPSYS
    st_select_lex_unit::exclude_tree()
*/
void st_select_lex_unit::exclude_tree()
{
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
  {
unknown's avatar
unknown committed
1887
    // unlink current level from global SELECTs list
1888 1889 1890
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
      sl->link_next->link_prev= sl->link_prev;

unknown's avatar
unknown committed
1891
    // unlink underlay levels
1892 1893 1894 1895 1896
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
    {
      u->exclude_level();
    }
  }
unknown's avatar
unknown committed
1897
  // exclude currect unit from list of nodes
1898
  (*prev)= next;
unknown's avatar
unknown committed
1899 1900
  if (next)
    next->prev= prev;
1901 1902 1903
}


unknown's avatar
unknown committed
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
/*
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
  this to 'last' as dependent

  SYNOPSIS
    last - pointer to last st_select_lex struct, before wich all 
           st_select_lex have to be marked as dependent

  NOTE
    'last' should be reachable from this st_select_lex_node
*/

unknown's avatar
unknown committed
1916
void st_select_lex::mark_as_dependent(st_select_lex *last)
unknown's avatar
unknown committed
1917 1918 1919 1920 1921
{
  /*
    Mark all selects from resolved to 1 before select where was
    found table as depended (of select where was found table)
  */
unknown's avatar
unknown committed
1922
  for (SELECT_LEX *s= this;
1923
       s && s != last;
unknown's avatar
unknown committed
1924
       s= s->outer_select())
1925
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
unknown's avatar
unknown committed
1926 1927
    {
      // Select is dependent of outer select
1928 1929
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
                       UNCACHEABLE_DEPENDENT;
1930
      SELECT_LEX_UNIT *munit= s->master_unit();
1931 1932 1933 1934 1935 1936 1937 1938
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
                       UNCACHEABLE_DEPENDENT;
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
      {
        if (sl != s &&
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
          sl->uncacheable|= UNCACHEABLE_UNITED;
      }
unknown's avatar
unknown committed
1939
    }
1940 1941
  is_correlated= TRUE;
  this->master_unit()->item->is_correlated= TRUE;
unknown's avatar
unknown committed
1942 1943
}

1944 1945 1946 1947 1948
bool st_select_lex_node::set_braces(bool value)      { return 1; }
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
1949
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
1950
						  LEX_STRING *alias,
unknown's avatar
unknown committed
1951
						  ulong table_join_options,
1952
						  thr_lock_type flags,
unknown's avatar
unknown committed
1953
						  List<Index_hint> *hints,
unknown's avatar
unknown committed
1954
                                                  LEX_STRING *option)
1955 1956 1957
{
  return 0;
}
unknown's avatar
unknown committed
1958 1959 1960 1961 1962 1963 1964 1965
ulong st_select_lex_node::get_table_join_options()
{
  return 0;
}

/*
  prohibit using LIMIT clause
*/
unknown's avatar
unknown committed
1966
bool st_select_lex::test_limit()
unknown's avatar
unknown committed
1967
{
1968
  if (select_limit != 0)
unknown's avatar
unknown committed
1969 1970
  {
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
unknown's avatar
unknown committed
1971
             "LIMIT & IN/ALL/ANY/SOME subquery");
unknown's avatar
unknown committed
1972 1973 1974 1975
    return(1);
  }
  return(0);
}
1976

1977

1978 1979 1980 1981 1982
st_select_lex_unit* st_select_lex_unit::master_unit()
{
    return this;
}

1983

1984 1985 1986 1987 1988
st_select_lex* st_select_lex_unit::outer_select()
{
  return (st_select_lex*) master;
}

1989

unknown's avatar
unknown committed
1990
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
unknown's avatar
unknown committed
1991
{
unknown's avatar
unknown committed
1992
  return add_to_list(thd, order_list, item, asc);
unknown's avatar
unknown committed
1993
}
1994

1995

unknown's avatar
unknown committed
1996
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
1997
{
1998
  DBUG_ENTER("st_select_lex::add_item_to_list");
unknown's avatar
unknown committed
1999
  DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
2000
  DBUG_RETURN(item_list.push_back(item));
2001 2002
}

2003

unknown's avatar
unknown committed
2004
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
2005
{
unknown's avatar
unknown committed
2006
  return add_to_list(thd, group_list, item, asc);
2007 2008
}

2009

2010 2011 2012 2013 2014
bool st_select_lex::add_ftfunc_to_list(Item_func_match *func)
{
  return !func || ftfunc_list->push_back(func); // end of memory?
}

2015

2016 2017 2018 2019 2020
st_select_lex_unit* st_select_lex::master_unit()
{
  return (st_select_lex_unit*) master;
}

2021

2022 2023 2024 2025 2026
st_select_lex* st_select_lex::outer_select()
{
  return (st_select_lex*) master->get_master();
}

2027

2028 2029 2030 2031 2032 2033
bool st_select_lex::set_braces(bool value)
{
  braces= value;
  return 0; 
}

2034

2035 2036 2037 2038 2039 2040
bool st_select_lex::inc_in_sum_expr()
{
  in_sum_expr++;
  return 0;
}

2041

2042 2043 2044 2045 2046
uint st_select_lex::get_in_sum_expr()
{
  return in_sum_expr;
}

2047

2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
TABLE_LIST* st_select_lex::get_table_list()
{
  return (TABLE_LIST*) table_list.first;
}

List<Item>* st_select_lex::get_item_list()
{
  return &item_list;
}

unknown's avatar
unknown committed
2058 2059 2060 2061 2062
ulong st_select_lex::get_table_join_options()
{
  return table_join_options;
}

2063

unknown's avatar
unknown committed
2064 2065 2066 2067
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
{
  if (ref_pointer_array)
    return 0;
2068 2069 2070 2071 2072

  /*
    We have to create array in prepared statement memory if it is
    prepared statement
  */
unknown's avatar
unknown committed
2073
  Query_arena *arena= thd->stmt_arena;
unknown's avatar
unknown committed
2074
  return (ref_pointer_array=
2075 2076 2077
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
                                                 item_list.elements +
                                                 select_n_having_items +
unknown's avatar
unknown committed
2078
                                                 select_n_where_fields +
2079
                                                 order_group_num)*5)) == 0;
unknown's avatar
unknown committed
2080 2081
}

2082

2083
void st_select_lex_unit::print(String *str, enum_query_type query_type)
unknown's avatar
unknown committed
2084
{
2085
  bool union_all= !union_distinct;
unknown's avatar
unknown committed
2086 2087 2088 2089
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
  {
    if (sl != first_select())
    {
2090
      str->append(STRING_WITH_LEN(" union "));
2091
      if (union_all)
2092
	str->append(STRING_WITH_LEN("all "));
2093
      else if (union_distinct == sl)
2094
        union_all= TRUE;
unknown's avatar
unknown committed
2095 2096 2097
    }
    if (sl->braces)
      str->append('(');
2098
    sl->print(thd, str, query_type);
unknown's avatar
unknown committed
2099 2100 2101 2102 2103 2104 2105
    if (sl->braces)
      str->append(')');
  }
  if (fake_select_lex == global_parameters)
  {
    if (fake_select_lex->order_list.elements)
    {
2106
      str->append(STRING_WITH_LEN(" order by "));
2107 2108 2109 2110
      fake_select_lex->print_order(
        str,
        (ORDER *) fake_select_lex->order_list.first,
        query_type);
unknown's avatar
unknown committed
2111
    }
2112
    fake_select_lex->print_limit(thd, str, query_type);
unknown's avatar
unknown committed
2113 2114 2115 2116
  }
}


2117 2118 2119
void st_select_lex::print_order(String *str,
                                ORDER *order,
                                enum_query_type query_type)
unknown's avatar
unknown committed
2120 2121 2122
{
  for (; order; order= order->next)
  {
2123 2124 2125
    if (order->counter_used)
    {
      char buffer[20];
2126 2127
      size_t length= my_snprintf(buffer, 20, "%d", order->counter);
      str->append(buffer, (uint) length);
2128 2129
    }
    else
2130
      (*order->item)->print(str, query_type);
unknown's avatar
unknown committed
2131
    if (!order->asc)
2132
      str->append(STRING_WITH_LEN(" desc"));
unknown's avatar
unknown committed
2133 2134 2135 2136 2137
    if (order->next)
      str->append(',');
  }
}
 
2138

2139 2140 2141
void st_select_lex::print_limit(THD *thd,
                                String *str,
                                enum_query_type query_type)
unknown's avatar
unknown committed
2142
{
2143 2144 2145
  SELECT_LEX_UNIT *unit= master_unit();
  Item_subselect *item= unit->item;
  if (item && unit->global_parameters == this &&
unknown's avatar
VIEW  
unknown committed
2146 2147 2148 2149
      (item->substype() == Item_subselect::EXISTS_SUBS ||
       item->substype() == Item_subselect::IN_SUBS ||
       item->substype() == Item_subselect::ALL_SUBS))
  {
2150
    DBUG_ASSERT(!item->fixed ||
2151
                (select_limit->val_int() == LL(1) && offset_limit == 0));
unknown's avatar
VIEW  
unknown committed
2152 2153 2154
    return;
  }

2155
  if (explicit_limit)
unknown's avatar
unknown committed
2156
  {
2157
    str->append(STRING_WITH_LEN(" limit "));
unknown's avatar
unknown committed
2158 2159
    if (offset_limit)
    {
2160
      offset_limit->print(str, query_type);
unknown's avatar
unknown committed
2161 2162
      str->append(',');
    }
2163
    select_limit->print(str, query_type);
unknown's avatar
unknown committed
2164 2165 2166
  }
}

2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
/**
  @brief Restore the LEX and THD in case of a parse error.

  This is a clean up call that is invoked by the Bison generated
  parser before returning an error from MYSQLparse. If your
  semantic actions manipulate with the global thread state (which
  is a very bad practice and should not normally be employed) and
  need a clean-up in case of error, and you can not use %destructor
  rule in the grammar file itself, this function should be used
  to implement the clean up.
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2179
void LEX::cleanup_lex_after_parse_error(THD *thd)
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
{
  /*
    Delete sphead for the side effect of restoring of the original
    LEX state, thd->lex, thd->mem_root and thd->free_list if they
    were replaced when parsing stored procedure statements.  We
    will never use sphead object after a parse error, so it's okay
    to delete it only for the sake of the side effect.
    TODO: make this functionality explicit in sp_head class.
    Sic: we must nullify the member of the main lex, not the
    current one that will be thrown away
  */
2191
  if (thd->lex->sphead)
2192 2193 2194 2195 2196
  {
    delete thd->lex->sphead;
    thd->lex->sphead= NULL;
  }
}
unknown's avatar
VIEW  
unknown committed
2197

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
/*
  Initialize (or reset) Query_tables_list object.

  SYNOPSIS
    reset_query_tables_list()
      init  TRUE  - we should perform full initialization of object with
                    allocating needed memory
            FALSE - object is already initialized so we should only reset
                    its state so it can be used for parsing/processing
                    of new statement

  DESCRIPTION
    This method initializes Query_tables_list so it can be used as part
    of LEX object for parsing/processing of statement. One can also use
    this method to reset state of already initialized Query_tables_list
    so it can be used for processing of new statement.
*/

void Query_tables_list::reset_query_tables_list(bool init)
{
unknown's avatar
unknown committed
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
  if (!init && query_tables)
  {
    TABLE_LIST *table= query_tables;
    for (;;)
    {
      delete table->view;
      if (query_tables_last == &table->next_global ||
          !(table= table->next_global))
        break;
    }
  }
2229 2230 2231 2232
  query_tables= 0;
  query_tables_last= &query_tables;
  query_tables_own_last= 0;
  if (init)
2233 2234 2235 2236 2237
  {
    /*
      We delay real initialization of hash (and therefore related
      memory allocation) until first insertion into this hash.
    */
Konstantin Osipov's avatar
Konstantin Osipov committed
2238
    my_hash_clear(&sroutines);
2239
  }
2240
  else if (sroutines.records)
2241 2242
  {
    /* Non-zero sroutines.records means that hash was initialized. */
2243
    my_hash_reset(&sroutines);
2244
  }
2245 2246 2247
  sroutines_list.empty();
  sroutines_list_own_last= sroutines_list.next;
  sroutines_list_own_elements= 0;
2248
  binlog_stmt_flags= 0;
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
}


/*
  Destroy Query_tables_list object with freeing all resources used by it.

  SYNOPSIS
    destroy_query_tables_list()
*/

void Query_tables_list::destroy_query_tables_list()
{
Konstantin Osipov's avatar
Konstantin Osipov committed
2261
  my_hash_free(&sroutines);
2262 2263 2264
}


2265 2266 2267 2268
/*
  Initialize LEX object.

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2269
    LEX::LEX()
2270 2271 2272 2273 2274 2275 2276 2277 2278

  NOTE
    LEX object initialized with this constructor can be used as part of
    THD object for which one can safely call open_tables(), lock_tables()
    and close_thread_tables() functions. But it is not yet ready for
    statement parsing. On should use lex_start() function to prepare LEX
    for this.
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2279
LEX::LEX()
2280
  :result(0),
2281
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2282
{
unknown's avatar
unknown committed
2283

unknown's avatar
unknown committed
2284 2285 2286 2287
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
                         plugins_static_buffer,
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2288
  reset_query_tables_list(TRUE);
2289 2290 2291
}


unknown's avatar
VIEW  
unknown committed
2292
/*
2293
  Check whether the merging algorithm can be used on this VIEW
unknown's avatar
VIEW  
unknown committed
2294 2295

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2296
    LEX::can_be_merged()
unknown's avatar
VIEW  
unknown committed
2297

2298
  DESCRIPTION
2299 2300 2301
    We can apply merge algorithm if it is single SELECT view  with
    subqueries only in WHERE clause (we do not count SELECTs of underlying
    views, and second level subqueries) and we have not grpouping, ordering,
2302 2303 2304
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
    several underlying tables.

unknown's avatar
VIEW  
unknown committed
2305 2306 2307 2308 2309
  RETURN
    FALSE - only temporary table algorithm can be used
    TRUE  - merge algorithm can be used
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2310
bool LEX::can_be_merged()
unknown's avatar
VIEW  
unknown committed
2311 2312 2313 2314
{
  // TODO: do not forget implement case when select_lex.table_list.elements==0

  /* find non VIEW subqueries/unions */
2315 2316 2317
  bool selects_allow_merge= select_lex.next_select() == 0;
  if (selects_allow_merge)
  {
2318 2319 2320
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
         tmp_unit;
         tmp_unit= tmp_unit->next_unit())
2321
    {
2322 2323 2324 2325
      if (tmp_unit->first_select()->parent_lex == this &&
          (tmp_unit->item == 0 ||
           (tmp_unit->item->place() != IN_WHERE &&
            tmp_unit->item->place() != IN_ON)))
2326 2327 2328 2329 2330 2331 2332 2333
      {
        selects_allow_merge= 0;
        break;
      }
    }
  }

  return (selects_allow_merge &&
unknown's avatar
VIEW  
unknown committed
2334 2335
	  select_lex.group_list.elements == 0 &&
	  select_lex.having == 0 &&
2336
          select_lex.with_sum_func == 0 &&
2337
	  select_lex.table_list.elements >= 1 &&
unknown's avatar
VIEW  
unknown committed
2338
	  !(select_lex.options & SELECT_DISTINCT) &&
2339
          select_lex.select_limit == 0);
unknown's avatar
VIEW  
unknown committed
2340 2341
}

2342

unknown's avatar
VIEW  
unknown committed
2343
/*
2344
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
unknown's avatar
VIEW  
unknown committed
2345 2346

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2347
    LEX::can_use_merged()
unknown's avatar
VIEW  
unknown committed
2348

2349 2350 2351
  DESCRIPTION
    Only listed here commands can use merge algorithm in top level
    SELECT_LEX (for subqueries will be used merge algorithm if
Konstantin Osipov's avatar
Konstantin Osipov committed
2352
    LEX::can_not_use_merged() is not TRUE).
2353

unknown's avatar
VIEW  
unknown committed
2354 2355 2356 2357 2358
  RETURN
    FALSE - command can't use merged VIEWs
    TRUE  - VIEWs with MERGE algorithms can be used
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2359
bool LEX::can_use_merged()
unknown's avatar
VIEW  
unknown committed
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
{
  switch (sql_command)
  {
  case SQLCOM_SELECT:
  case SQLCOM_CREATE_TABLE:
  case SQLCOM_UPDATE:
  case SQLCOM_UPDATE_MULTI:
  case SQLCOM_DELETE:
  case SQLCOM_DELETE_MULTI:
  case SQLCOM_INSERT:
  case SQLCOM_INSERT_SELECT:
  case SQLCOM_REPLACE:
  case SQLCOM_REPLACE_SELECT:
2373
  case SQLCOM_LOAD:
unknown's avatar
VIEW  
unknown committed
2374 2375 2376 2377 2378 2379
    return TRUE;
  default:
    return FALSE;
  }
}

2380
/*
2381
  Check if command can't use merged views in any part of command
2382 2383

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2384
    LEX::can_not_use_merged()
2385

2386 2387
  DESCRIPTION
    Temporary table algorithm will be used on all SELECT levels for queries
Konstantin Osipov's avatar
Konstantin Osipov committed
2388
    listed here (see also LEX::can_use_merged()).
2389

2390 2391 2392 2393 2394
  RETURN
    FALSE - command can't use merged VIEWs
    TRUE  - VIEWs with MERGE algorithms can be used
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2395
bool LEX::can_not_use_merged()
2396 2397 2398 2399 2400
{
  switch (sql_command)
  {
  case SQLCOM_CREATE_VIEW:
  case SQLCOM_SHOW_CREATE:
2401 2402 2403 2404 2405 2406
  /*
    SQLCOM_SHOW_FIELDS is necessary to make 
    information schema tables working correctly with views.
    see get_schema_tables_result function
  */
  case SQLCOM_SHOW_FIELDS:
2407 2408 2409 2410 2411 2412
    return TRUE;
  default:
    return FALSE;
  }
}

unknown's avatar
VIEW  
unknown committed
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
/*
  Detect that we need only table structure of derived table/view

  SYNOPSIS
    only_view_structure()

  RETURN
    TRUE yes, we need only structure
    FALSE no, we need data
*/
unknown's avatar
unknown committed
2423

Konstantin Osipov's avatar
Konstantin Osipov committed
2424
bool LEX::only_view_structure()
unknown's avatar
VIEW  
unknown committed
2425
{
2426
  switch (sql_command) {
unknown's avatar
VIEW  
unknown committed
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
  case SQLCOM_SHOW_CREATE:
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_SHOW_FIELDS:
  case SQLCOM_REVOKE_ALL:
  case SQLCOM_REVOKE:
  case SQLCOM_GRANT:
  case SQLCOM_CREATE_VIEW:
    return TRUE;
  default:
    return FALSE;
  }
}


unknown's avatar
unknown committed
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
/*
  Should Items_ident be printed correctly

  SYNOPSIS
    need_correct_ident()

  RETURN
    TRUE yes, we need only structure
    FALSE no, we need data
*/


Konstantin Osipov's avatar
Konstantin Osipov committed
2453
bool LEX::need_correct_ident()
unknown's avatar
unknown committed
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
{
  switch(sql_command)
  {
  case SQLCOM_SHOW_CREATE:
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_CREATE_VIEW:
    return TRUE;
  default:
    return FALSE;
  }
}

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
/*
  Get effective type of CHECK OPTION for given view

  SYNOPSIS
    get_effective_with_check()
    view    given view

  NOTE
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
    so we do not.

  RETURN
    VIEW_CHECK_NONE      no need CHECK OPTION
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2483
uint8 LEX::get_effective_with_check(TABLE_LIST *view)
2484 2485 2486 2487 2488 2489 2490
{
  if (view->select_lex->master_unit() == &unit &&
      which_check_option_applicable())
    return (uint8)view->with_check;
  return VIEW_CHECK_NONE;
}

unknown's avatar
unknown committed
2491

2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
/**
  This method should be called only during parsing.
  It is aware of compound statements (stored routine bodies)
  and will initialize the destination with the default
  database of the stored routine, rather than the default
  database of the connection it is parsed in.
  E.g. if one has no current database selected, or current database 
  set to 'bar' and then issues:

  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//

  t1 is meant to refer to foo.t1, not to bar.t1.

  This method is needed to support this rule.

  @return TRUE in case of error (parsing should be aborted, FALSE in
  case of success
*/

bool
Konstantin Osipov's avatar
Konstantin Osipov committed
2512
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
{
  if (sphead)
  {
    DBUG_ASSERT(sphead->m_db.str && sphead->m_db.length);
    /*
      It is safe to assign the string by-pointer, both sphead and
      its statements reside in the same memory root.
    */
    *p_db= sphead->m_db.str;
    if (p_db_length)
      *p_db_length= sphead->m_db.length;
    return FALSE;
  }
  return thd->copy_db_to(p_db, p_db_length);
}

unknown's avatar
unknown committed
2529 2530 2531 2532 2533 2534 2535
/*
  initialize limit counters

  SYNOPSIS
    st_select_lex_unit::set_limit()
    values	- SELECT_LEX with initial values for counters
*/
unknown's avatar
VIEW  
unknown committed
2536

unknown's avatar
unknown committed
2537
void st_select_lex_unit::set_limit(st_select_lex *sl)
2538
{
2539
  ha_rows select_limit_val;
2540
  ulonglong val;
2541

unknown's avatar
unknown committed
2542
  DBUG_ASSERT(! thd->stmt_arena->is_stmt_prepare());
2543 2544 2545
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
  select_limit_val= (ha_rows)val;
#ifndef BIG_TABLES
2546
  /*
2547 2548 2549 2550 2551 2552
    Check for overflow : ha_rows can be smaller then ulonglong if
    BIG_TABLES is off.
    */
  if (val != (ulonglong)select_limit_val)
    select_limit_val= HA_POS_ERROR;
#endif
2553 2554 2555 2556 2557 2558 2559
  val= sl->offset_limit ? sl->offset_limit->val_uint() : ULL(0);
  offset_limit_cnt= (ha_rows)val;
#ifndef BIG_TABLES
  /* Check for truncation. */
  if (val != (ulonglong)offset_limit_cnt)
    offset_limit_cnt= HA_POS_ERROR;
#endif
2560 2561
  select_limit_cnt= select_limit_val + offset_limit_cnt;
  if (select_limit_cnt < select_limit_val)
2562 2563 2564
    select_limit_cnt= HA_POS_ERROR;		// no limit
}

unknown's avatar
unknown committed
2565

2566
/**
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
  @brief Set the initial purpose of this TABLE_LIST object in the list of used
    tables.

  We need to track this information on table-by-table basis, since when this
  table becomes an element of the pre-locked list, it's impossible to identify
  which SQL sub-statement it has been originally used in.

  E.g.:

  User request:                 SELECT * FROM t1 WHERE f1();
  FUNCTION f1():                DELETE FROM t2; RETURN 1;
  BEFORE DELETE trigger on t2:  INSERT INTO t3 VALUES (old.a);

  For this user request, the pre-locked list will contain t1, t2, t3
  table elements, each needed for different DML.

  The trigger event map is updated to reflect INSERT, UPDATE, DELETE,
  REPLACE, LOAD DATA, CREATE TABLE .. SELECT, CREATE TABLE ..
  REPLACE SELECT statements, and additionally ON DUPLICATE KEY UPDATE
  clause.
2587 2588
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2589
void LEX::set_trg_event_type_for_tables()
2590
{
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
  uint8 new_trg_event_map= 0;

  /*
    Some auxiliary operations
    (e.g. GRANT processing) create TABLE_LIST instances outside
    the parser. Additionally, some commands (e.g. OPTIMIZE) change
    the lock type for a table only after parsing is done. Luckily,
    these do not fire triggers and do not need to pre-load them.
    For these TABLE_LISTs set_trg_event_type is never called, and
    trg_event_map is always empty. That means that the pre-locking
    algorithm will ignore triggers defined on these tables, if
    any, and the execution will either fail with an assert in
    sql_trigger.cc or with an error that a used table was not
    pre-locked, in case of a production build.

    TODO: this usage pattern creates unnecessary module dependencies
    and should be rewritten to go through the parser.
    Table list instances created outside the parser in most cases
    refer to mysql.* system tables. It is not allowed to have
    a trigger on a system table, but keeping track of
    initialization provides extra safety in case this limitation
    is circumvented.
  */

  switch (sql_command) {
  case SQLCOM_LOCK_TABLES:
  /*
    On a LOCK TABLE, all triggers must be pre-loaded for this TABLE_LIST
    when opening an associated TABLE.
  */
    new_trg_event_map= static_cast<uint8>
                        (1 << static_cast<int>(TRG_EVENT_INSERT)) |
                      static_cast<uint8>
                        (1 << static_cast<int>(TRG_EVENT_UPDATE)) |
                      static_cast<uint8>
                        (1 << static_cast<int>(TRG_EVENT_DELETE));
    break;
  /*
    Basic INSERT. If there is an additional ON DUPLIATE KEY UPDATE
    clause, it will be handled later in this method.
  */
  case SQLCOM_INSERT:                           /* fall through */
  case SQLCOM_INSERT_SELECT:
  /*
    LOAD DATA ... INFILE is expected to fire BEFORE/AFTER INSERT
    triggers.
    If the statement also has REPLACE clause, it will be
    handled later in this method.
  */
  case SQLCOM_LOAD:                             /* fall through */
  /*
    REPLACE is semantically equivalent to INSERT. In case
    of a primary or unique key conflict, it deletes the old
    record and inserts a new one. So we also may need to
    fire ON DELETE triggers. This functionality is handled
    later in this method.
  */
  case SQLCOM_REPLACE:                          /* fall through */
  case SQLCOM_REPLACE_SELECT:
  /*
    CREATE TABLE ... SELECT defaults to INSERT if the table or
    view already exists. REPLACE option of CREATE TABLE ...
    REPLACE SELECT is handled later in this method.
  */
  case SQLCOM_CREATE_TABLE:
    new_trg_event_map|= static_cast<uint8>
                          (1 << static_cast<int>(TRG_EVENT_INSERT));
    break;
  /* Basic update and multi-update */
  case SQLCOM_UPDATE:                           /* fall through */
  case SQLCOM_UPDATE_MULTI:
    new_trg_event_map|= static_cast<uint8>
                          (1 << static_cast<int>(TRG_EVENT_UPDATE));
    break;
  /* Basic delete and multi-delete */
  case SQLCOM_DELETE:                           /* fall through */
  case SQLCOM_DELETE_MULTI:
    new_trg_event_map|= static_cast<uint8>
                          (1 << static_cast<int>(TRG_EVENT_DELETE));
    break;
  default:
    break;
  }

  switch (duplicates) {
  case DUP_UPDATE:
    new_trg_event_map|= static_cast<uint8>
                          (1 << static_cast<int>(TRG_EVENT_UPDATE));
    break;
  case DUP_REPLACE:
    new_trg_event_map|= static_cast<uint8>
                          (1 << static_cast<int>(TRG_EVENT_DELETE));
    break;
  case DUP_ERROR:
  default:
    break;
  }


2690 2691 2692 2693 2694 2695 2696 2697
  /*
    Do not iterate over sub-selects, only the tables in the outermost
    SELECT_LEX can be modified, if any.
  */
  TABLE_LIST *tables= select_lex.get_table_list();

  while (tables)
  {
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
    /*
      This is a fast check to filter out statements that do
      not change data, or tables  on the right side, in case of
      INSERT .. SELECT, CREATE TABLE .. SELECT and so on.
      Here we also filter out OPTIMIZE statement and non-updateable
      views, for which lock_type is TL_UNLOCK or TL_READ after
      parsing.
    */
    if (static_cast<int>(tables->lock_type) >=
        static_cast<int>(TL_WRITE_ALLOW_WRITE))
      tables->trg_event_map= new_trg_event_map;
2709 2710 2711 2712 2713
    tables= tables->next_local;
  }
}


2714
/*
2715 2716
  Unlink the first table from the global table list and the first table from
  outer select (lex->select_lex) local list
2717 2718 2719

  SYNOPSIS
    unlink_first_table()
2720
    link_to_local	Set to 1 if caller should link this table to local list
unknown's avatar
unknown committed
2721

unknown's avatar
VIEW  
unknown committed
2722
  NOTES
2723 2724
    We assume that first tables in both lists is the same table or the local
    list is empty.
2725 2726

  RETURN
2727
    0	If 'query_tables' == 0
unknown's avatar
VIEW  
unknown committed
2728
    unlinked table
2729
      In this case link_to_local is set.
unknown's avatar
unknown committed
2730

2731
*/
Konstantin Osipov's avatar
Konstantin Osipov committed
2732
TABLE_LIST *LEX::unlink_first_table(bool *link_to_local)
2733
{
unknown's avatar
VIEW  
unknown committed
2734 2735 2736 2737 2738 2739 2740 2741
  TABLE_LIST *first;
  if ((first= query_tables))
  {
    /*
      Exclude from global table list
    */
    if ((query_tables= query_tables->next_global))
      query_tables->prev_global= &query_tables;
2742 2743
    else
      query_tables_last= &query_tables;
unknown's avatar
VIEW  
unknown committed
2744 2745 2746 2747 2748 2749 2750
    first->next_global= 0;

    /*
      and from local list if it is not empty
    */
    if ((*link_to_local= test(select_lex.table_list.first)))
    {
2751 2752
      select_lex.context.table_list= 
        select_lex.context.first_name_resolution_table= first->next_local;
2753
      select_lex.table_list.first= (uchar*) (first->next_local);
unknown's avatar
VIEW  
unknown committed
2754 2755 2756
      select_lex.table_list.elements--;	//safety
      first->next_local= 0;
      /*
2757 2758
        Ensure that the global list has the same first table as the local
        list.
unknown's avatar
VIEW  
unknown committed
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
      */
      first_lists_tables_same();
    }
  }
  return first;
}


/*
  Bring first local table of first most outer select to first place in global
  table list

  SYNOPSYS
Konstantin Osipov's avatar
Konstantin Osipov committed
2772
     LEX::first_lists_tables_same()
unknown's avatar
VIEW  
unknown committed
2773 2774

  NOTES
2775 2776 2777 2778 2779 2780
    In many cases (for example, usual INSERT/DELETE/...) the first table of
    main SELECT_LEX have special meaning => check that it is the first table
    in global list and re-link to be first in the global list if it is
    necessary.  We need such re-linking only for queries with sub-queries in
    the select list, as only in this case tables of sub-queries will go to
    the global list first.
unknown's avatar
VIEW  
unknown committed
2781 2782
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2783
void LEX::first_lists_tables_same()
unknown's avatar
VIEW  
unknown committed
2784 2785 2786 2787
{
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
  if (query_tables != first_table && first_table != 0)
  {
2788
    TABLE_LIST *next;
unknown's avatar
VIEW  
unknown committed
2789 2790
    if (query_tables_last == &first_table->next_global)
      query_tables_last= first_table->prev_global;
2791

2792
    if ((next= *first_table->prev_global= first_table->next_global))
unknown's avatar
VIEW  
unknown committed
2793 2794 2795 2796
      next->prev_global= first_table->prev_global;
    /* include in new place */
    first_table->next_global= query_tables;
    /*
2797 2798 2799
       We are sure that query_tables is not 0, because first_table was not
       first table in the global list => we can use
       query_tables->prev_global without check of query_tables
unknown's avatar
VIEW  
unknown committed
2800 2801 2802 2803 2804
    */
    query_tables->prev_global= &first_table->next_global;
    first_table->prev_global= &query_tables;
    query_tables= first_table;
  }
2805 2806
}

unknown's avatar
unknown committed
2807

2808
/*
unknown's avatar
unknown committed
2809
  Link table back that was unlinked with unlink_first_table()
2810 2811 2812

  SYNOPSIS
    link_first_table_back()
unknown's avatar
VIEW  
unknown committed
2813
    link_to_local	do we need link this table to local
2814 2815 2816 2817

  RETURN
    global list
*/
unknown's avatar
VIEW  
unknown committed
2818

Konstantin Osipov's avatar
Konstantin Osipov committed
2819
void LEX::link_first_table_back(TABLE_LIST *first,
unknown's avatar
VIEW  
unknown committed
2820
				   bool link_to_local)
2821
{
unknown's avatar
VIEW  
unknown committed
2822
  if (first)
2823
  {
unknown's avatar
VIEW  
unknown committed
2824 2825
    if ((first->next_global= query_tables))
      query_tables->prev_global= &first->next_global;
2826 2827
    else
      query_tables_last= &first->next_global;
unknown's avatar
VIEW  
unknown committed
2828 2829 2830 2831 2832
    query_tables= first;

    if (link_to_local)
    {
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
unknown's avatar
unknown committed
2833
      select_lex.context.table_list= first;
2834
      select_lex.table_list.first= (uchar*) first;
unknown's avatar
VIEW  
unknown committed
2835 2836 2837 2838 2839 2840
      select_lex.table_list.elements++;	//safety
    }
  }
}


2841 2842 2843 2844 2845

/*
  cleanup lex for case when we open table by table for processing

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2846
    LEX::cleanup_after_one_table_open()
2847 2848 2849 2850 2851

  NOTE
    This method is mostly responsible for cleaning up of selects lists and
    derived tables state. To rollback changes in Query_tables_list one has
    to call Query_tables_list::reset_query_tables_list(FALSE).
2852 2853
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2854
void LEX::cleanup_after_one_table_open()
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
{
  /*
    thd->lex->derived_tables & additional units may be set if we open
    a view. It is necessary to clear thd->lex->derived_tables flag
    to prevent processing of derived tables during next open_and_lock_tables
    if next table is a real table and cleanup & remove underlying units
    NOTE: all units will be connected to thd->lex->select_lex, because we
    have not UNION on most upper level.
    */
  if (all_selects_list != &select_lex)
  {
    derived_tables= 0;
    /* cleunup underlying units (units of VIEW) */
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
         un;
         un= un->next_unit())
      un->cleanup();
    /* reduce all selects list to default state */
    all_selects_list= &select_lex;
    /* remove underlying units (units of VIEW) subtree */
    select_lex.cut_subtree();
  }
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
}


/*
  Save current state of Query_tables_list for this LEX, and prepare it
  for processing of new statemnt.

  SYNOPSIS
    reset_n_backup_query_tables_list()
      backup  Pointer to Query_tables_list instance to be used for backup
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2889
void LEX::reset_n_backup_query_tables_list(Query_tables_list *backup)
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
{
  backup->set_query_tables_list(this);
  /*
    We have to perform full initialization here since otherwise we
    will damage backed up state.
  */
  this->reset_query_tables_list(TRUE);
}


/*
  Restore state of Query_tables_list for this LEX from backup.

  SYNOPSIS
    restore_backup_query_tables_list()
      backup  Pointer to Query_tables_list instance used for backup
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2908
void LEX::restore_backup_query_tables_list(Query_tables_list *backup)
2909 2910 2911
{
  this->destroy_query_tables_list();
  this->set_query_tables_list(backup);
2912 2913 2914
}


unknown's avatar
unknown committed
2915 2916 2917 2918
/*
  Checks for usage of routines and/or tables in a parsed statement

  SYNOPSIS
Konstantin Osipov's avatar
Konstantin Osipov committed
2919
    LEX:table_or_sp_used()
unknown's avatar
unknown committed
2920 2921 2922 2923 2924 2925

  RETURN
    FALSE  No routines and tables used
    TRUE   Either or both routines and tables are used.
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
2926
bool LEX::table_or_sp_used()
unknown's avatar
unknown committed
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
{
  DBUG_ENTER("table_or_sp_used");

  if (sroutines.records || query_tables)
    DBUG_RETURN(TRUE);

  DBUG_RETURN(FALSE);
}


2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
/*
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables

  SYNOPSIS
    fix_prepare_info_in_table_list()
      thd  Thread handle
      tbl  List of tables to process

  DESCRIPTION
    Perform end-end-of prepare fixup for list of tables, if any of the tables
    is a merge-algorithm VIEW, recursively fix up its underlying tables as
    well.

*/

static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
{
  for (; tbl; tbl= tbl->next_local)
  {
    if (tbl->on_expr)
    {
      tbl->prep_on_expr= tbl->on_expr;
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
    }
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
  }
}


unknown's avatar
VIEW  
unknown committed
2966
/*
2967
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
unknown's avatar
VIEW  
unknown committed
2968 2969 2970

  SYNOPSIS
    st_select_lex::fix_prepare_information
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
      thd          thread handler
      conds        in/out pointer to WHERE condition to be met at execution
      having_conds in/out pointer to HAVING condition to be met at execution
  
  DESCRIPTION
    The passed WHERE and HAVING are to be saved for the future executions.
    This function saves it, and returns a copy which can be thrashed during
    this execution of the statement. By saving/thrashing here we mean only
    AND/OR trees.
    The function also calls fix_prepare_info_in_table_list that saves all
    ON expressions.    
unknown's avatar
VIEW  
unknown committed
2982 2983
*/

2984 2985
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
                                            Item **having_conds)
unknown's avatar
VIEW  
unknown committed
2986
{
unknown's avatar
unknown committed
2987
  if (!thd->stmt_arena->is_conventional() && first_execution)
unknown's avatar
VIEW  
unknown committed
2988 2989
  {
    first_execution= 0;
2990 2991 2992 2993 2994
    if (*conds)
    {
      prep_where= *conds;
      *conds= where= prep_where->copy_andor_structure(thd);
    }
2995 2996 2997 2998 2999
    if (*having_conds)
    {
      prep_having= *having_conds;
      *having_conds= having= prep_having->copy_andor_structure(thd);
    }
3000
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
3001 3002 3003
  }
}

unknown's avatar
unknown committed
3004

unknown's avatar
unknown committed
3005
/*
unknown's avatar
VIEW  
unknown committed
3006
  There are st_select_lex::add_table_to_list &
unknown's avatar
unknown committed
3007
  st_select_lex::set_lock_for_tables are in sql_parse.cc
unknown's avatar
unknown committed
3008

unknown's avatar
VIEW  
unknown committed
3009
  st_select_lex::print is in sql_select.cc
unknown's avatar
unknown committed
3010 3011

  st_select_lex_unit::prepare, st_select_lex_unit::exec,
3012 3013
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
  st_select_lex_unit::change_result
unknown's avatar
unknown committed
3014
  are in sql_union.cc
unknown's avatar
unknown committed
3015
*/
3016

3017 3018 3019 3020 3021
/*
  Sets the kind of hints to be added by the calls to add_index_hint().

  SYNOPSIS
    set_index_hint_type()
3022 3023
      type_arg     The kind of hints to be added from now on.
      clause       The clause to use for hints to be added from now on.
3024 3025 3026 3027 3028 3029 3030 3031

  DESCRIPTION
    Used in filling up the tagged hints list.
    This list is filled by first setting the kind of the hint as a 
    context variable and then adding hints of the current kind.
    Then the context variable index_hint_type can be reset to the
    next hint type.
*/
3032
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
3033 3034
                                        index_clause_map clause)
{ 
3035
  current_index_hint_type= type_arg;
3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
  current_index_hint_clause= clause;
}


/*
  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).

  SYNOPSIS
    alloc_index_hints()
      thd         current thread.
*/

void st_select_lex::alloc_index_hints (THD *thd)
{ 
unknown's avatar
unknown committed
3050
  index_hints= new (thd->mem_root) List<Index_hint>(); 
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
}



/*
  adds an element to the array storing index usage hints 
  (ADD/FORCE/IGNORE INDEX).

  SYNOPSIS
    add_index_hint()
      thd         current thread.
      str         name of the index.
      length      number of characters in str.

  RETURN VALUE
    0 on success, non-zero otherwise
*/
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
{
  return index_hints->push_front (new (thd->mem_root) 
unknown's avatar
unknown committed
3071
                                 Index_hint(current_index_hint_type,
3072 3073 3074
                                            current_index_hint_clause,
                                            str, length));
}
3075 3076 3077 3078 3079 3080

/**
  A routine used by the parser to decide whether we are specifying a full
  partitioning or if only partitions to add or to split.

  @note  This needs to be outside of WITH_PARTITION_STORAGE_ENGINE since it
unknown's avatar
unknown committed
3081
  is used from the sql parser that doesn't have any ifdef's
3082 3083 3084 3085 3086

  @retval  TRUE    Yes, it is part of a management partition command
  @retval  FALSE          No, not a management partition command
*/

Konstantin Osipov's avatar
Konstantin Osipov committed
3087
bool LEX::is_partition_management() const
3088 3089 3090 3091 3092 3093
{
  return (sql_command == SQLCOM_ALTER_TABLE &&
          (alter_info.flags == ALTER_ADD_PARTITION ||
           alter_info.flags == ALTER_REORGANIZE_PARTITION));
}