~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
19
#define DRIZZLE_LEX 1
934.2.1 by Jay Pipes
Split index hints out into their own file, removal from sql_lex.h and sql_select.cc
20
#include "drizzled/server_includes.h"
21
#include "drizzled/item/num.h"
22
#include "drizzled/error.h"
23
#include "drizzled/session.h"
24
#include "drizzled/sql_base.h"
25
#include "drizzled/lookup_symbol.h"
26
#include "drizzled/index_hint.h"
779.4.5 by Monty Taylor
Replaced gen_lex_hash with gperf. Yay for no more building tools to build source!!!
27
908.1.15 by Monty Taylor
Updated comment version indicators to handle drizzle versions.
28
#include <ctype.h>
29
30
using namespace std;
779.4.5 by Monty Taylor
Replaced gen_lex_hash with gperf. Yay for no more building tools to build source!!!
31
520.1.22 by Brian Aker
Second pass of thd cleanup
32
static int lex_one_token(void *arg, void *yysession);
1 by brian
clean slate
33
34
/*
35
  We are using pointer to this variable for distinguishing between assignment
36
  to NEW row field (when parsing trigger definition) and structured variable.
37
*/
38
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
39
40
/**
41
  LEX_STRING constant for null-string to be used in parser and other places.
42
*/
43
const LEX_STRING null_lex_str= {NULL, 0};
44
520.1.22 by Brian Aker
Second pass of thd cleanup
45
Lex_input_stream::Lex_input_stream(Session *session,
1 by brian
clean slate
46
                                   const char* buffer,
47
                                   unsigned int length)
520.1.22 by Brian Aker
Second pass of thd cleanup
48
: m_session(session),
1 by brian
clean slate
49
  yylineno(1),
50
  yytoklen(0),
51
  yylval(NULL),
52
  lookahead_token(END_OF_INPUT),
53
  lookahead_yylval(NULL),
54
  m_ptr(buffer),
55
  m_tok_start(NULL),
56
  m_tok_end(NULL),
57
  m_end_of_query(buffer + length),
58
  m_tok_start_prev(NULL),
59
  m_buf(buffer),
60
  m_buf_length(length),
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
61
  m_echo(true),
1 by brian
clean slate
62
  m_cpp_tok_start(NULL),
63
  m_cpp_tok_start_prev(NULL),
64
  m_cpp_tok_end(NULL),
65
  m_body_utf8(NULL),
66
  m_cpp_utf8_processed_ptr(NULL),
67
  next_state(MY_LEX_START),
68
  found_semicolon(NULL),
69
  ignore_space(1),
70
  in_comment(NO_COMMENT),
71
  m_underscore_cs(NULL)
72
{
520.1.22 by Brian Aker
Second pass of thd cleanup
73
  m_cpp_buf= (char*) session->alloc(length + 1);
1 by brian
clean slate
74
  m_cpp_ptr= m_cpp_buf;
75
}
76
77
Lex_input_stream::~Lex_input_stream()
78
{}
79
80
/**
81
  The operation is called from the parser in order to
82
  1) designate the intention to have utf8 body;
83
  1) Indicate to the lexer that we will need a utf8 representation of this
84
     statement;
85
  2) Determine the beginning of the body.
86
520.1.22 by Brian Aker
Second pass of thd cleanup
87
  @param session        Thread context.
1 by brian
clean slate
88
  @param begin_ptr  Pointer to the start of the body in the pre-processed
89
                    buffer.
90
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
91
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
1 by brian
clean slate
92
{
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
93
  assert(begin_ptr);
94
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
1 by brian
clean slate
95
482 by Brian Aker
Remove uint.
96
  uint32_t body_utf8_length=
748 by Brian Aker
Removal of client side collation.
97
    (m_buf_length / default_charset_info->mbminlen) *
1 by brian
clean slate
98
    my_charset_utf8_bin.mbmaxlen;
99
520.1.22 by Brian Aker
Second pass of thd cleanup
100
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
1 by brian
clean slate
101
  m_body_utf8_ptr= m_body_utf8;
102
  *m_body_utf8_ptr= 0;
103
104
  m_cpp_utf8_processed_ptr= begin_ptr;
105
}
106
107
/**
108
  @brief The operation appends unprocessed part of pre-processed buffer till
109
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.
110
111
  The idea is that some tokens in the pre-processed buffer (like character
112
  set introducers) should be skipped.
113
114
  Example:
115
    CPP buffer: SELECT 'str1', _latin1 'str2';
116
    m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
117
    In order to skip "_latin1", the following call should be made:
118
      body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)
119
120
  @param ptr      Pointer in the pre-processed buffer, which specifies the
121
                  end of the chunk, which should be appended to the utf8
122
                  body.
123
  @param end_ptr  Pointer in the pre-processed buffer, to which
124
                  m_cpp_utf8_processed_ptr will be set in the end of the
125
                  operation.
126
*/
127
void Lex_input_stream::body_utf8_append(const char *ptr,
128
                                        const char *end_ptr)
129
{
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
130
  assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
131
  assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
1 by brian
clean slate
132
133
  if (!m_body_utf8)
134
    return;
135
136
  if (m_cpp_utf8_processed_ptr >= ptr)
137
    return;
138
139
  int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
140
141
  memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
142
  m_body_utf8_ptr += bytes_to_copy;
143
  *m_body_utf8_ptr= 0;
144
145
  m_cpp_utf8_processed_ptr= end_ptr;
146
}
147
148
/**
149
  The operation appends unprocessed part of the pre-processed buffer till
150
  the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.
151
152
  @param ptr  Pointer in the pre-processed buffer, which specifies the end
153
              of the chunk, which should be appended to the utf8 body.
154
*/
155
void Lex_input_stream::body_utf8_append(const char *ptr)
156
{
157
  body_utf8_append(ptr, ptr);
158
}
159
160
/**
161
  The operation converts the specified text literal to the utf8 and appends
162
  the result to the utf8-body.
163
520.1.22 by Brian Aker
Second pass of thd cleanup
164
  @param session      Thread context.
1 by brian
clean slate
165
  @param txt      Text literal.
166
  @param txt_cs   Character set of the text literal.
167
  @param end_ptr  Pointer in the pre-processed buffer, to which
168
                  m_cpp_utf8_processed_ptr will be set in the end of the
169
                  operation.
170
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
171
void Lex_input_stream::body_utf8_append_literal(Session *session,
1 by brian
clean slate
172
                                                const LEX_STRING *txt,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
173
                                                const CHARSET_INFO * const txt_cs,
1 by brian
clean slate
174
                                                const char *end_ptr)
175
{
176
  if (!m_cpp_utf8_processed_ptr)
177
    return;
178
179
  LEX_STRING utf_txt;
180
181
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
182
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
183
    session->convert_string(&utf_txt,
1 by brian
clean slate
184
                        &my_charset_utf8_general_ci,
185
                        txt->str, txt->length,
186
                        txt_cs);
187
  }
188
  else
189
  {
190
    utf_txt.str= txt->str;
191
    utf_txt.length= txt->length;
192
  }
193
194
  /* NOTE: utf_txt.length is in bytes, not in symbols. */
195
196
  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
197
  m_body_utf8_ptr += utf_txt.length;
198
  *m_body_utf8_ptr= 0;
199
200
  m_cpp_utf8_processed_ptr= end_ptr;
201
}
202
203
/*
204
  This is called before every query that is to be parsed.
205
  Because of this, it's critical to not do too much things here.
206
  (We already do too much here)
207
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
208
void lex_start(Session *session)
1 by brian
clean slate
209
{
520.1.22 by Brian Aker
Second pass of thd cleanup
210
  LEX *lex= session->lex;
1 by brian
clean slate
211
520.1.22 by Brian Aker
Second pass of thd cleanup
212
  lex->session= lex->unit.session= session;
1 by brian
clean slate
213
214
  lex->context_stack.empty();
215
  lex->unit.init_query();
216
  lex->unit.init_select();
217
  /* 'parent_lex' is used in init_query() so it must be before it. */
218
  lex->select_lex.parent_lex= lex;
219
  lex->select_lex.init_query();
220
  lex->value_list.empty();
221
  lex->update_list.empty();
222
  lex->param_list.empty();
223
  lex->auxiliary_table_list.empty();
224
  lex->unit.next= lex->unit.master=
225
    lex->unit.link_next= lex->unit.return_to= 0;
226
  lex->unit.prev= lex->unit.link_prev= 0;
227
  lex->unit.slave= lex->unit.global_parameters= lex->current_select=
228
    lex->all_selects_list= &lex->select_lex;
229
  lex->select_lex.master= &lex->unit;
230
  lex->select_lex.prev= &lex->unit.slave;
231
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
847 by Brian Aker
More typdef class removal.
232
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
1 by brian
clean slate
233
  lex->select_lex.options= 0;
234
  lex->select_lex.init_order();
235
  lex->select_lex.group_list.empty();
236
  lex->describe= 0;
237
  lex->derived_tables= 0;
238
  lex->lock_option= TL_READ;
239
  lex->leaf_tables_insert= 0;
240
  lex->select_lex.select_number= 1;
241
  lex->length=0;
242
  lex->select_lex.in_sum_expr=0;
243
  lex->select_lex.group_list.empty();
244
  lex->select_lex.order_list.empty();
245
  lex->sql_command= SQLCOM_END;
246
  lex->duplicates= DUP_ERROR;
247
  lex->ignore= 0;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
248
  lex->escape_used= false;
1 by brian
clean slate
249
  lex->query_tables= 0;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
250
  lex->reset_query_tables_list(false);
251
  lex->expr_allows_subselect= true;
252
  lex->use_only_table_context= false;
1 by brian
clean slate
253
254
  lex->name.str= 0;
255
  lex->name.length= 0;
256
  lex->nest_level=0 ;
257
  lex->allow_sum_func= 0;
258
  lex->in_sum_func= NULL;
259
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
260
  lex->is_lex_started= true;
1 by brian
clean slate
261
}
262
263
void lex_end(LEX *lex)
264
{
265
  if (lex->yacc_yyss)
266
  {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
267
    free(lex->yacc_yyss);
268
    free(lex->yacc_yyvs);
1 by brian
clean slate
269
    lex->yacc_yyss= 0;
270
    lex->yacc_yyvs= 0;
271
  }
272
406 by Brian Aker
Cleanup around Query_arena.
273
  delete lex->result;
274
  lex->result= 0;
1 by brian
clean slate
275
}
276
482 by Brian Aker
Remove uint.
277
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
1 by brian
clean slate
278
{
779.4.5 by Monty Taylor
Replaced gen_lex_hash with gperf. Yay for no more building tools to build source!!!
279
  /* Plenty of memory for the largest lex symbol we have */
280
  char tok_upper[64];
1 by brian
clean slate
281
  const char *tok= lip->get_tok_start();
779.4.5 by Monty Taylor
Replaced gen_lex_hash with gperf. Yay for no more building tools to build source!!!
282
  uint32_t tok_pos= 0;
283
  for (;tok_pos<len && tok_pos<63;tok_pos++)
284
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
285
  tok_upper[tok_pos]=0;
1 by brian
clean slate
286
873.2.23 by Monty Taylor
Fixed a failure during gperf failure.
287
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
1 by brian
clean slate
288
  if (symbol)
289
  {
290
    lip->yylval->symbol.symbol=symbol;
291
    lip->yylval->symbol.str= (char*) tok;
292
    lip->yylval->symbol.length=len;
293
294
    return symbol->tok;
295
  }
686 by Brian Aker
Remove vector from lex for plugins.
296
1 by brian
clean slate
297
  return 0;
298
}
299
300
bool is_lex_native_function(const LEX_STRING *name)
301
{
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
302
  assert(name != NULL);
873.2.23 by Monty Taylor
Fixed a failure during gperf failure.
303
  return (lookup_symbol(name->str, name->length, 1) != 0);
1 by brian
clean slate
304
}
305
306
/* make a copy of token before ptr and set yytoklen */
482 by Brian Aker
Remove uint.
307
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
1 by brian
clean slate
308
{
309
  LEX_STRING tmp;
310
  lip->yyUnget();                       // ptr points now after last token char
311
  tmp.length=lip->yytoklen=length;
520.1.22 by Brian Aker
Second pass of thd cleanup
312
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
1 by brian
clean slate
313
314
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
315
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
316
317
  return tmp;
318
}
319
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
320
/*
321
 todo:
322
   There are no dangerous charsets in mysql for function
323
   get_quoted_token yet. But it should be fixed in the
1 by brian
clean slate
324
   future to operate multichar strings (like ucs2)
325
*/
326
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
482 by Brian Aker
Remove uint.
327
                                   uint32_t skip,
328
                                   uint32_t length, char quote)
1 by brian
clean slate
329
{
330
  LEX_STRING tmp;
331
  const char *from, *end;
332
  char *to;
333
  lip->yyUnget();                       // ptr points now after last token char
334
  tmp.length= lip->yytoklen=length;
520.1.22 by Brian Aker
Second pass of thd cleanup
335
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
1 by brian
clean slate
336
  from= lip->get_tok_start() + skip;
337
  to= tmp.str;
338
  end= to+length;
339
340
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
341
  lip->m_cpp_text_end= lip->m_cpp_text_start + length;
342
343
  for ( ; to != end; )
344
  {
345
    if ((*to++= *from++) == quote)
346
    {
347
      from++;					// Skip double quotes
348
      lip->m_cpp_text_start++;
349
    }
350
  }
351
  *to= 0;					// End null for safety
352
  return tmp;
353
}
354
355
356
/*
357
  Return an unescaped text literal without quotes
358
  Fix sometimes to do only one scan of the string
359
*/
360
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
361
{
481 by Brian Aker
Remove all of uchar.
362
  register unsigned char c,sep;
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
363
  bool found_escape= false;
520.1.22 by Brian Aker
Second pass of thd cleanup
364
  const CHARSET_INFO * const cs= lip->m_session->charset();
1 by brian
clean slate
365
366
  lip->tok_bitmap= 0;
367
  sep= lip->yyGetLast();                        // String should end with this
368
  while (! lip->eof())
369
  {
370
    c= lip->yyGet();
371
    lip->tok_bitmap|= c;
372
#ifdef USE_MB
373
    {
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
374
      if (use_mb(cs))
375
      {
376
        int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
377
        if (l != 0) 
378
        {
379
          lip->skip_binary(l-1);
380
          continue;
381
        }
1 by brian
clean slate
382
      }
383
    }
384
#endif
385
    if (c == '\\')
386
    {					// Escaped character
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
387
      found_escape= true;
1 by brian
clean slate
388
      if (lip->eof())
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
389
        return 0;
1 by brian
clean slate
390
      lip->yySkip();
391
    }
392
    else if (c == sep)
393
    {
394
      if (c == lip->yyGet())            // Check if two separators in a row
395
      {
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
396
        found_escape= true;                 // duplicate. Remember for delete
397
        continue;
1 by brian
clean slate
398
      }
399
      else
400
        lip->yyUnget();
401
402
      /* Found end. Unescape and return string */
403
      const char *str, *end;
404
      char *start;
405
406
      str= lip->get_tok_start();
407
      end= lip->get_ptr();
408
      /* Extract the text from the token */
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
409
      str+= pre_skip;
410
      end-= post_skip;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
411
      assert(end >= str);
1 by brian
clean slate
412
895 by Brian Aker
Completion (?) of uint conversion.
413
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
414
        return (char*) "";		// Sql_alloc has set error flag
1 by brian
clean slate
415
416
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
417
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
418
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
419
      if (! found_escape)
1 by brian
clean slate
420
      {
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
421
        lip->yytoklen= (uint32_t) (end-str);
422
        memcpy(start, str, lip->yytoklen);
423
        start[lip->yytoklen]= 0;
1 by brian
clean slate
424
      }
425
      else
426
      {
427
        char *to;
428
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
429
        for (to= start; str != end; str++)
430
        {
1 by brian
clean slate
431
#ifdef USE_MB
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
432
          if (use_mb(cs))
433
          {
434
            int l= my_ismbchar(cs, str, end);
435
            if (l != 0)
436
            {
437
              while (l--)
438
                *to++= *str++;
439
              str--;
440
              continue;
441
            }
442
          }
1 by brian
clean slate
443
#endif
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
444
          if (*str == '\\' && (str + 1) != end)
445
          {
446
            switch (*++str) {
447
            case 'n':
448
              *to++= '\n';
449
              break;
450
            case 't':
451
              *to++= '\t';
452
              break;
453
            case 'r':
454
              *to++= '\r';
455
              break;
456
            case 'b':
457
              *to++= '\b';
458
              break;
459
            case '0':
460
              *to++= 0;			// Ascii null
461
              break;
462
            case 'Z':			// ^Z must be escaped on Win32
463
              *to++= '\032';
464
              break;
465
            case '_':
466
            case '%':
467
              *to++= '\\';		// remember prefix for wildcard
468
              /* Fall through */
469
            default:
1 by brian
clean slate
470
              *to++= *str;
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
471
              break;
472
            }
473
          }
474
          else if (*str == sep)
475
            *to++= *str++;		// Two ' or "
476
          else
477
            *to++ = *str;
478
        }
479
        *to= 0;
480
        lip->yytoklen= (uint32_t) (to - start);
1 by brian
clean slate
481
      }
482
      return start;
483
    }
484
  }
485
  return 0;					// unexpected end of query
486
}
487
488
489
/*
152 by Brian Aker
longlong replacement
490
** Calc type of integer; long integer, int64_t integer or real.
1 by brian
clean slate
491
** Returns smallest type that match the string.
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
492
** When using uint64_t values the result is converted to a real
1 by brian
clean slate
493
** because else they will be unexpected sign changes because all calculation
152 by Brian Aker
longlong replacement
494
** is done with int64_t or double.
1 by brian
clean slate
495
*/
496
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
497
static const char *long_str= "2147483647";
498
static const uint32_t long_len= 10;
499
static const char *signed_long_str= "-2147483648";
500
static const char *int64_t_str= "9223372036854775807";
501
static const uint32_t int64_t_len= 19;
502
static const char *signed_int64_t_str= "-9223372036854775808";
503
static const uint32_t signed_int64_t_len= 19;
504
static const char *unsigned_int64_t_str= "18446744073709551615";
505
static const uint32_t unsigned_int64_t_len= 20;
1 by brian
clean slate
506
482 by Brian Aker
Remove uint.
507
static inline uint32_t int_token(const char *str,uint32_t length)
1 by brian
clean slate
508
{
509
  if (length < long_len)			// quick normal case
510
    return NUM;
511
  bool neg=0;
512
513
  if (*str == '+')				// Remove sign and pre-zeros
514
  {
515
    str++; length--;
516
  }
517
  else if (*str == '-')
518
  {
519
    str++; length--;
520
    neg=1;
521
  }
522
  while (*str == '0' && length)
523
  {
524
    str++; length --;
525
  }
526
  if (length < long_len)
527
    return NUM;
528
482 by Brian Aker
Remove uint.
529
  uint32_t smaller,bigger;
1 by brian
clean slate
530
  const char *cmp;
531
  if (neg)
532
  {
533
    if (length == long_len)
534
    {
535
      cmp= signed_long_str+1;
536
      smaller=NUM;				// If <= signed_long_str
537
      bigger=LONG_NUM;				// If >= signed_long_str
538
    }
152 by Brian Aker
longlong replacement
539
    else if (length < signed_int64_t_len)
1 by brian
clean slate
540
      return LONG_NUM;
152 by Brian Aker
longlong replacement
541
    else if (length > signed_int64_t_len)
1 by brian
clean slate
542
      return DECIMAL_NUM;
543
    else
544
    {
152 by Brian Aker
longlong replacement
545
      cmp=signed_int64_t_str+1;
546
      smaller=LONG_NUM;				// If <= signed_int64_t_str
1 by brian
clean slate
547
      bigger=DECIMAL_NUM;
548
    }
549
  }
550
  else
551
  {
552
    if (length == long_len)
553
    {
554
      cmp= long_str;
555
      smaller=NUM;
556
      bigger=LONG_NUM;
557
    }
152 by Brian Aker
longlong replacement
558
    else if (length < int64_t_len)
1 by brian
clean slate
559
      return LONG_NUM;
152 by Brian Aker
longlong replacement
560
    else if (length > int64_t_len)
1 by brian
clean slate
561
    {
152 by Brian Aker
longlong replacement
562
      if (length > unsigned_int64_t_len)
1 by brian
clean slate
563
        return DECIMAL_NUM;
152 by Brian Aker
longlong replacement
564
      cmp=unsigned_int64_t_str;
1 by brian
clean slate
565
      smaller=ULONGLONG_NUM;
566
      bigger=DECIMAL_NUM;
567
    }
568
    else
569
    {
152 by Brian Aker
longlong replacement
570
      cmp=int64_t_str;
1 by brian
clean slate
571
      smaller=LONG_NUM;
572
      bigger= ULONGLONG_NUM;
573
    }
574
  }
575
  while (*cmp && *cmp++ == *str++) ;
481 by Brian Aker
Remove all of uchar.
576
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
1 by brian
clean slate
577
}
578
579
/*
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
580
  DRIZZLElex remember the following states from the following DRIZZLElex()
1 by brian
clean slate
581
582
  - MY_LEX_EOQ			Found end of query
583
  - MY_LEX_OPERATOR_OR_IDENT	Last state was an ident, text or number
584
				(which can't be followed by a signed number)
585
*/
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
586
int DRIZZLElex(void *arg, void *yysession)
1 by brian
clean slate
587
{
520.1.22 by Brian Aker
Second pass of thd cleanup
588
  Session *session= (Session *)yysession;
589
  Lex_input_stream *lip= session->m_lip;
1 by brian
clean slate
590
  YYSTYPE *yylval=(YYSTYPE*) arg;
591
  int token;
592
593
  if (lip->lookahead_token != END_OF_INPUT)
594
  {
595
    /*
596
      The next token was already parsed in advance,
597
      return it.
598
    */
599
    token= lip->lookahead_token;
600
    lip->lookahead_token= END_OF_INPUT;
601
    *yylval= *(lip->lookahead_yylval);
602
    lip->lookahead_yylval= NULL;
603
    return token;
604
  }
605
520.1.22 by Brian Aker
Second pass of thd cleanup
606
  token= lex_one_token(arg, yysession);
1 by brian
clean slate
607
608
  switch(token) {
609
  case WITH:
610
    /*
436 by Brian Aker
Removed non-existent CUBE operator.
611
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
1 by brian
clean slate
612
      which makes the grammar LALR(2).
613
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
614
      to transform the grammar into a LALR(1) grammar,
615
      which sql_yacc.yy can process.
616
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
617
    token= lex_one_token(arg, yysession);
436 by Brian Aker
Removed non-existent CUBE operator.
618
    if (token == ROLLUP_SYM)
619
    {
1 by brian
clean slate
620
      return WITH_ROLLUP_SYM;
436 by Brian Aker
Removed non-existent CUBE operator.
621
    }
622
    else
623
    {
1 by brian
clean slate
624
      /*
625
        Save the token following 'WITH'
626
      */
627
      lip->lookahead_yylval= lip->yylval;
628
      lip->yylval= NULL;
629
      lip->lookahead_token= token;
630
      return WITH;
631
    }
632
  default:
633
    break;
634
  }
635
636
  return token;
637
}
638
520.1.22 by Brian Aker
Second pass of thd cleanup
639
int lex_one_token(void *arg, void *yysession)
1 by brian
clean slate
640
{
641
  register unsigned char c= 0; /* Just set to shutup GCC */
642
  bool comment_closed;
643
  int	tokval, result_state;
644
  unsigned int length;
645
  enum my_lex_states state;
520.1.22 by Brian Aker
Second pass of thd cleanup
646
  Session *session= (Session *)yysession;
647
  Lex_input_stream *lip= session->m_lip;
648
  LEX *lex= session->lex;
1 by brian
clean slate
649
  YYSTYPE *yylval=(YYSTYPE*) arg;
520.1.22 by Brian Aker
Second pass of thd cleanup
650
  const CHARSET_INFO * const cs= session->charset();
481 by Brian Aker
Remove all of uchar.
651
  unsigned char *state_map= cs->state_map;
652
  unsigned char *ident_map= cs->ident_map;
1 by brian
clean slate
653
654
  lip->yylval=yylval;			// The global state
655
656
  lip->start_token();
657
  state=lip->next_state;
658
  lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
659
  for (;;)
660
  {
661
    switch (state) {
662
    case MY_LEX_OPERATOR_OR_IDENT:	// Next is operator or keyword
663
    case MY_LEX_START:			// Start of token
664
      // Skip starting whitespace
665
      while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
666
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
667
        if (c == '\n')
668
          lip->yylineno++;
1 by brian
clean slate
669
670
        lip->yySkip();
671
      }
672
673
      /* Start of real token */
674
      lip->restart_token();
675
      c= lip->yyGet();
676
      state= (enum my_lex_states) state_map[c];
677
      break;
678
    case MY_LEX_ESCAPE:
679
      if (lip->yyGet() == 'N')
680
      {					// Allow \N as shortcut for NULL
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
681
        yylval->lex_str.str=(char*) "\\N";
682
        yylval->lex_str.length=2;
683
        return NULL_SYM;
1 by brian
clean slate
684
      }
685
    case MY_LEX_CHAR:			// Unknown or single char token
686
    case MY_LEX_SKIP:			// This should not happen
687
      if (c == '-' && lip->yyPeek() == '-' &&
688
          (my_isspace(cs,lip->yyPeekn(1)) ||
689
           my_iscntrl(cs,lip->yyPeekn(1))))
690
      {
691
        state=MY_LEX_COMMENT;
692
        break;
693
      }
694
695
      if (c != ')')
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
696
        lip->next_state= MY_LEX_START;	// Allow signed numbers
1 by brian
clean slate
697
698
      if (c == ',')
699
      {
700
        /*
701
          Warning:
702
          This is a work around, to make the "remember_name" rule in
703
          sql/sql_yacc.yy work properly.
704
          The problem is that, when parsing "select expr1, expr2",
705
          the code generated by bison executes the *pre* action
706
          remember_name (see select_item) *before* actually parsing the
707
          first token of expr2.
708
        */
709
        lip->restart_token();
710
      }
711
712
      return((int) c);
713
714
    case MY_LEX_IDENT_OR_HEX:
715
      if (lip->yyPeek() == '\'')
716
      {					// Found x'hex-number'
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
717
        state= MY_LEX_HEX_NUMBER;
718
        break;
1 by brian
clean slate
719
      }
720
    case MY_LEX_IDENT_OR_BIN:
721
      if (lip->yyPeek() == '\'')
722
      {                                 // Found b'bin-number'
723
        state= MY_LEX_BIN_NUMBER;
724
        break;
725
      }
726
    case MY_LEX_IDENT:
727
      const char *start;
728
#if defined(USE_MB) && defined(USE_MB_IDENT)
729
      if (use_mb(cs))
730
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
731
        result_state= IDENT_QUOTED;
1 by brian
clean slate
732
        if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
733
        {
734
          int l = my_ismbchar(cs,
735
                              lip->get_ptr() -1,
736
                              lip->get_end_of_query());
737
          if (l == 0) {
738
            state = MY_LEX_CHAR;
739
            continue;
740
          }
741
          lip->skip_binary(l - 1);
742
        }
743
        while (ident_map[c=lip->yyGet()])
744
        {
745
          if (my_mbcharlen(cs, c) > 1)
746
          {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
747
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
748
            if (l == 0)
1 by brian
clean slate
749
              break;
750
            lip->skip_binary(l-1);
751
          }
752
        }
753
      }
754
      else
755
#endif
756
      {
757
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
758
        /* If there were non-ASCII characters, mark that we must convert */
759
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
760
      }
761
      length= lip->yyLength();
762
      start= lip->get_ptr();
763
      if (lip->ignore_space)
764
      {
765
        /*
766
          If we find a space then this can't be an identifier. We notice this
767
          below by checking start != lex->ptr.
768
        */
769
        for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
770
      }
771
      if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
772
	      lip->next_state=MY_LEX_IDENT_SEP;
1 by brian
clean slate
773
      else
774
      {					// '(' must follow directly if function
775
        lip->yyUnget();
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
776
        if ((tokval = find_keyword(lip, length, c == '(')))
777
        {
778
          lip->next_state= MY_LEX_START;	// Allow signed numbers
779
          return(tokval);		// Was keyword
780
        }
1 by brian
clean slate
781
        lip->yySkip();                  // next state does a unget
782
      }
783
      yylval->lex_str=get_token(lip, 0, length);
784
785
      lip->body_utf8_append(lip->m_cpp_text_start);
786
520.1.22 by Brian Aker
Second pass of thd cleanup
787
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1 by brian
clean slate
788
                                    lip->m_cpp_text_end);
789
790
      return(result_state);			// IDENT or IDENT_QUOTED
791
792
    case MY_LEX_IDENT_SEP:		// Found ident and now '.'
793
      yylval->lex_str.str= (char*) lip->get_ptr();
794
      yylval->lex_str.length= 1;
795
      c= lip->yyGet();                  // should be '.'
796
      lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
797
      if (!ident_map[(uint8_t)lip->yyPeek()])            // Probably ` or "
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
798
        lip->next_state= MY_LEX_START;
1 by brian
clean slate
799
      return((int) c);
800
801
    case MY_LEX_NUMBER_IDENT:		// number or ident which num-start
802
      if (lip->yyGetLast() == '0')
803
      {
804
        c= lip->yyGet();
805
        if (c == 'x')
806
        {
807
          while (my_isxdigit(cs,(c = lip->yyGet()))) ;
808
          if ((lip->yyLength() >= 3) && !ident_map[c])
809
          {
810
            /* skip '0x' */
811
            yylval->lex_str=get_token(lip, 2, lip->yyLength()-2);
812
            return (HEX_NUM);
813
          }
814
          lip->yyUnget();
815
          state= MY_LEX_IDENT_START;
816
          break;
817
        }
818
        else if (c == 'b')
819
        {
820
          while ((c= lip->yyGet()) == '0' || c == '1') {};
821
          if ((lip->yyLength() >= 3) && !ident_map[c])
822
          {
823
            /* Skip '0b' */
824
            yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
825
            return (BIN_NUM);
826
          }
827
          lip->yyUnget();
828
          state= MY_LEX_IDENT_START;
829
          break;
830
        }
831
        lip->yyUnget();
832
      }
833
834
      while (my_isdigit(cs, (c = lip->yyGet()))) ;
835
      if (!ident_map[c])
836
      {					// Can't be identifier
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
837
        state=MY_LEX_INT_OR_REAL;
838
        break;
1 by brian
clean slate
839
      }
840
      if (c == 'e' || c == 'E')
841
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
842
        // The following test is written this way to allow numbers of type 1e1
1 by brian
clean slate
843
        if (my_isdigit(cs,lip->yyPeek()) ||
844
            (c=(lip->yyGet())) == '+' || c == '-')
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
845
        {				// Allow 1E+10
1 by brian
clean slate
846
          if (my_isdigit(cs,lip->yyPeek()))     // Number must have digit after sign
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
847
          {
1 by brian
clean slate
848
            lip->yySkip();
849
            while (my_isdigit(cs,lip->yyGet())) ;
850
            yylval->lex_str=get_token(lip, 0, lip->yyLength());
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
851
            return(FLOAT_NUM);
852
          }
853
        }
1 by brian
clean slate
854
        lip->yyUnget();
855
      }
856
      // fall through
857
    case MY_LEX_IDENT_START:			// We come here after '.'
858
      result_state= IDENT;
859
#if defined(USE_MB) && defined(USE_MB_IDENT)
860
      if (use_mb(cs))
861
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
862
        result_state= IDENT_QUOTED;
1 by brian
clean slate
863
        while (ident_map[c=lip->yyGet()])
864
        {
865
          if (my_mbcharlen(cs, c) > 1)
866
          {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
867
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
868
            if (l == 0)
1 by brian
clean slate
869
              break;
870
            lip->skip_binary(l-1);
871
          }
872
        }
873
      }
874
      else
875
#endif
876
      {
877
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
878
        /* If there were non-ASCII characters, mark that we must convert */
879
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
880
      }
881
      if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
882
      	lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
1 by brian
clean slate
883
884
      yylval->lex_str= get_token(lip, 0, lip->yyLength());
885
886
      lip->body_utf8_append(lip->m_cpp_text_start);
887
520.1.22 by Brian Aker
Second pass of thd cleanup
888
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1 by brian
clean slate
889
                                    lip->m_cpp_text_end);
890
891
      return(result_state);
892
893
    case MY_LEX_USER_VARIABLE_DELIMITER:	// Found quote char
894
    {
482 by Brian Aker
Remove uint.
895
      uint32_t double_quotes= 0;
1 by brian
clean slate
896
      char quote_char= c;                       // Used char
897
      while ((c=lip->yyGet()))
898
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
899
        int var_length;
900
        if ((var_length= my_mbcharlen(cs, c)) == 1)
901
        {
902
          if (c == quote_char)
903
          {
904
                  if (lip->yyPeek() != quote_char)
905
              break;
906
                  c=lip->yyGet();
907
            double_quotes++;
908
            continue;
909
          }
910
        }
1 by brian
clean slate
911
#ifdef USE_MB
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
912
        else if (var_length < 1)
913
          break;				// Error
1 by brian
clean slate
914
        lip->skip_binary(var_length-1);
915
#endif
916
      }
917
      if (double_quotes)
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
918
	      yylval->lex_str=get_quoted_token(lip, 1, lip->yyLength() - double_quotes -1, quote_char);
1 by brian
clean slate
919
      else
920
        yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
921
      if (c == quote_char)
922
        lip->yySkip();                  // Skip end `
923
      lip->next_state= MY_LEX_START;
924
      lip->body_utf8_append(lip->m_cpp_text_start);
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
925
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs, lip->m_cpp_text_end);
1 by brian
clean slate
926
      return(IDENT_QUOTED);
927
    }
928
    case MY_LEX_INT_OR_REAL:		// Complete int or incomplete real
929
      if (c != '.')
930
      {					// Found complete integer number.
931
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
932
      	return int_token(yylval->lex_str.str,yylval->lex_str.length);
1 by brian
clean slate
933
      }
934
      // fall through
935
    case MY_LEX_REAL:			// Incomplete real number
936
      while (my_isdigit(cs,c = lip->yyGet())) ;
937
938
      if (c == 'e' || c == 'E')
939
      {
940
        c = lip->yyGet();
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
941
        if (c == '-' || c == '+')
942
                c = lip->yyGet();                     // Skip sign
943
        if (!my_isdigit(cs,c))
944
        {				// No digit after sign
945
          state= MY_LEX_CHAR;
946
          break;
947
        }
1 by brian
clean slate
948
        while (my_isdigit(cs,lip->yyGet())) ;
949
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
950
        return(FLOAT_NUM);
1 by brian
clean slate
951
      }
952
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
953
      return(DECIMAL_NUM);
954
955
    case MY_LEX_HEX_NUMBER:		// Found x'hexstring'
956
      lip->yySkip();                    // Accept opening '
957
      while (my_isxdigit(cs, (c= lip->yyGet()))) ;
958
      if (c != '\'')
959
        return(ABORT_SYM);              // Illegal hex constant
960
      lip->yySkip();                    // Accept closing '
961
      length= lip->yyLength();          // Length of hexnum+3
962
      if ((length % 2) == 0)
963
        return(ABORT_SYM);              // odd number of hex digits
964
      yylval->lex_str=get_token(lip,
965
                                2,          // skip x'
966
                                length-3);  // don't count x' and last '
967
      return (HEX_NUM);
968
969
    case MY_LEX_BIN_NUMBER:           // Found b'bin-string'
970
      lip->yySkip();                  // Accept opening '
971
      while ((c= lip->yyGet()) == '0' || c == '1') {};
972
      if (c != '\'')
973
        return(ABORT_SYM);            // Illegal hex constant
974
      lip->yySkip();                  // Accept closing '
975
      length= lip->yyLength();        // Length of bin-num + 3
976
      yylval->lex_str= get_token(lip,
977
                                 2,         // skip b'
978
                                 length-3); // don't count b' and last '
979
      return (BIN_NUM);
980
981
    case MY_LEX_CMP_OP:			// Incomplete comparison operator
982
      if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
983
          state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
984
        lip->yySkip();
985
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
986
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
987
        lip->next_state= MY_LEX_START;	// Allow signed numbers
988
        return(tokval);
1 by brian
clean slate
989
      }
990
      state = MY_LEX_CHAR;		// Something fishy found
991
      break;
992
993
    case MY_LEX_LONG_CMP_OP:		// Incomplete comparison operator
994
      if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
995
          state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
996
      {
997
        lip->yySkip();
998
        if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP)
999
          lip->yySkip();
1000
      }
1001
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1002
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1003
        lip->next_state= MY_LEX_START;	// Found long op
1004
        return(tokval);
1 by brian
clean slate
1005
      }
1006
      state = MY_LEX_CHAR;		// Something fishy found
1007
      break;
1008
1009
    case MY_LEX_BOOL:
1010
      if (c != lip->yyPeek())
1011
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1012
        state=MY_LEX_CHAR;
1013
        break;
1 by brian
clean slate
1014
      }
1015
      lip->yySkip();
1016
      tokval = find_keyword(lip,2,0);	// Is a bool operator
1017
      lip->next_state= MY_LEX_START;	// Allow signed numbers
1018
      return(tokval);
1019
1020
    case MY_LEX_STRING_OR_DELIMITER:
1021
      if (0)
1022
      {
1023
        state= MY_LEX_USER_VARIABLE_DELIMITER;
1024
        break;
1025
      }
1026
      /* " used for strings */
1027
    case MY_LEX_STRING:			// Incomplete text string
1028
      if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
1029
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1030
        state= MY_LEX_CHAR;		// Read char by char
1031
        break;
1 by brian
clean slate
1032
      }
1033
      yylval->lex_str.length=lip->yytoklen;
1034
1035
      lip->body_utf8_append(lip->m_cpp_text_start);
1036
520.1.22 by Brian Aker
Second pass of thd cleanup
1037
      lip->body_utf8_append_literal(session, &yylval->lex_str,
1 by brian
clean slate
1038
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1039
        lip->m_cpp_text_end);
1040
1041
      lip->m_underscore_cs= NULL;
1042
1043
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1044
      return(TEXT_STRING);
1045
1046
    case MY_LEX_COMMENT:			//  Comment
1047
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1048
      while ((c = lip->yyGet()) != '\n' && c) ;
1049
      lip->yyUnget();                   // Safety against eof
1050
      state = MY_LEX_START;		// Try again
1051
      break;
1052
    case MY_LEX_LONG_COMMENT:		/* Long C comment? */
1053
      if (lip->yyPeek() != '*')
1054
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1055
        state=MY_LEX_CHAR;		// Probable division
1056
        break;
1 by brian
clean slate
1057
      }
1058
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1059
      /* Reject '/' '*', since we might need to turn off the echo */
1060
      lip->yyUnget();
1061
1062
      if (lip->yyPeekn(2) == '!')
1063
      {
1064
        lip->in_comment= DISCARD_COMMENT;
1065
        /* Accept '/' '*' '!', but do not keep this marker. */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1066
        lip->set_echo(false);
1 by brian
clean slate
1067
        lip->yySkip();
1068
        lip->yySkip();
1069
        lip->yySkip();
1070
1071
        /*
1072
          The special comment format is very strict:
908.1.15 by Monty Taylor
Updated comment version indicators to handle drizzle versions.
1073
          '/' '*' '!', followed by digits ended by a non-digit.
1074
          There must be at least 5 digits for it to count
1 by brian
clean slate
1075
        */
908.1.15 by Monty Taylor
Updated comment version indicators to handle drizzle versions.
1076
        const int MAX_VERSION_SIZE= 16;
1077
        char version_str[MAX_VERSION_SIZE];
1078
1079
        int pos= 0;
1080
        do
1081
        {
1082
          version_str[pos]= lip->yyPeekn(pos);
1083
          pos++;
1084
        } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1085
        version_str[pos]= 0;
1086
1087
        /* To keep some semblance of compatibility, we impose a 5 digit floor */
1088
        if (pos > 4)
1089
        {
1090
          uint64_t version;
1091
          version=strtoll(version_str, NULL, 10);
1 by brian
clean slate
1092
1093
          /* Accept 'M' 'm' 'm' 'd' 'd' */
908.1.15 by Monty Taylor
Updated comment version indicators to handle drizzle versions.
1094
          lip->yySkipn(pos-1);
1 by brian
clean slate
1095
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1096
          if (version <= DRIZZLE_VERSION_ID)
1 by brian
clean slate
1097
          {
1098
            /* Expand the content of the special comment as real code */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1099
            lip->set_echo(true);
1 by brian
clean slate
1100
            state=MY_LEX_START;
1101
            break;
1102
          }
1103
        }
1104
        else
1105
        {
1106
          state=MY_LEX_START;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1107
          lip->set_echo(true);
1 by brian
clean slate
1108
          break;
1109
        }
1110
      }
1111
      else
1112
      {
1113
        lip->in_comment= PRESERVE_COMMENT;
1114
        lip->yySkip();                  // Accept /
1115
        lip->yySkip();                  // Accept *
1116
      }
1117
      /*
1118
        Discard:
1119
        - regular '/' '*' comments,
1120
        - special comments '/' '*' '!' for a future version,
1121
        by scanning until we find a closing '*' '/' marker.
1122
        Note: There is no such thing as nesting comments,
1123
        the first '*' '/' sequence seen will mark the end.
1124
      */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1125
      comment_closed= false;
1 by brian
clean slate
1126
      while (! lip->eof())
1127
      {
1128
        c= lip->yyGet();
1129
        if (c == '*')
1130
        {
1131
          if (lip->yyPeek() == '/')
1132
          {
1133
            lip->yySkip();
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1134
            comment_closed= true;
1 by brian
clean slate
1135
            state = MY_LEX_START;
1136
            break;
1137
          }
1138
        }
1139
        else if (c == '\n')
1140
          lip->yylineno++;
1141
      }
1142
      /* Unbalanced comments with a missing '*' '/' are a syntax error */
1143
      if (! comment_closed)
1144
        return (ABORT_SYM);
1145
      state = MY_LEX_START;             // Try again
1146
      lip->in_comment= NO_COMMENT;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1147
      lip->set_echo(true);
1 by brian
clean slate
1148
      break;
1149
    case MY_LEX_END_LONG_COMMENT:
1150
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1151
      {
1152
        /* Reject '*' '/' */
1153
        lip->yyUnget();
1154
        /* Accept '*' '/', with the proper echo */
1155
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1156
        lip->yySkipn(2);
1157
        /* And start recording the tokens again */
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1158
        lip->set_echo(true);
1 by brian
clean slate
1159
        lip->in_comment=NO_COMMENT;
1160
        state=MY_LEX_START;
1161
      }
1162
      else
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1163
        state=MY_LEX_CHAR;		// Return '*'
1 by brian
clean slate
1164
      break;
1165
    case MY_LEX_SET_VAR:		// Check if ':='
1166
      if (lip->yyPeek() != '=')
1167
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1168
        state=MY_LEX_CHAR;		// Return ':'
1169
        break;
1 by brian
clean slate
1170
      }
1171
      lip->yySkip();
1172
      return (SET_VAR);
1173
    case MY_LEX_SEMICOLON:			// optional line terminator
1174
      if (lip->yyPeek())
1175
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1176
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
1 by brian
clean slate
1177
        {
1178
          lip->found_semicolon= lip->get_ptr();
520.1.22 by Brian Aker
Second pass of thd cleanup
1179
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
1 by brian
clean slate
1180
          lip->next_state= MY_LEX_END;
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1181
          lip->set_echo(true);
1 by brian
clean slate
1182
          return (END_OF_INPUT);
1183
        }
1184
        state= MY_LEX_CHAR;		// Return ';'
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1185
        break;
1 by brian
clean slate
1186
      }
1187
      lip->next_state=MY_LEX_END;       // Mark for next loop
1188
      return(END_OF_INPUT);
1189
    case MY_LEX_EOL:
1190
      if (lip->eof())
1191
      {
1192
        lip->yyUnget();                 // Reject the last '\0'
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1193
        lip->set_echo(false);
1 by brian
clean slate
1194
        lip->yySkip();
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1195
        lip->set_echo(true);
1 by brian
clean slate
1196
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
1197
        if (lip->in_comment != NO_COMMENT)
1198
          return (ABORT_SYM);
1199
        lip->next_state=MY_LEX_END;     // Mark for next loop
1200
        return(END_OF_INPUT);
1201
      }
1202
      state=MY_LEX_CHAR;
1203
      break;
1204
    case MY_LEX_END:
1205
      lip->next_state=MY_LEX_END;
1019.1.6 by Brian Aker
A number of random cleanups.
1206
      return false;			// We found end of input last time
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1207
1 by brian
clean slate
1208
      /* Actually real shouldn't start with . but allow them anyhow */
1209
    case MY_LEX_REAL_OR_POINT:
1210
      if (my_isdigit(cs,lip->yyPeek()))
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1211
        state= MY_LEX_REAL;		// Real
1 by brian
clean slate
1212
      else
1213
      {
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1214
      	state= MY_LEX_IDENT_SEP;	// return '.'
1 by brian
clean slate
1215
        lip->yyUnget();                 // Put back '.'
1216
      }
1217
      break;
1218
    case MY_LEX_USER_END:		// end '@' of user@hostname
1219
      switch (state_map[(uint8_t)lip->yyPeek()]) {
1220
      case MY_LEX_STRING:
1221
      case MY_LEX_USER_VARIABLE_DELIMITER:
1222
      case MY_LEX_STRING_OR_DELIMITER:
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1223
      	break;
1 by brian
clean slate
1224
      case MY_LEX_USER_END:
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1225
        lip->next_state=MY_LEX_SYSTEM_VAR;
1226
        break;
1 by brian
clean slate
1227
      default:
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1228
        lip->next_state=MY_LEX_HOSTNAME;
1229
        break;
1 by brian
clean slate
1230
      }
1231
      yylval->lex_str.str=(char*) lip->get_ptr();
1232
      yylval->lex_str.length=1;
1233
      return((int) '@');
1234
    case MY_LEX_HOSTNAME:		// end '@' of user@hostname
1235
      for (c=lip->yyGet() ;
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1236
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
1 by brian
clean slate
1237
           c= lip->yyGet()) ;
1238
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
1239
      return(LEX_HOSTNAME);
1240
    case MY_LEX_SYSTEM_VAR:
1241
      yylval->lex_str.str=(char*) lip->get_ptr();
1242
      yylval->lex_str.length=1;
1243
      lip->yySkip();                                    // Skip '@'
1244
      lip->next_state= (state_map[(uint8_t)lip->yyPeek()] ==
1245
			MY_LEX_USER_VARIABLE_DELIMITER ?
1246
			MY_LEX_OPERATOR_OR_IDENT :
1247
			MY_LEX_IDENT_OR_KEYWORD);
1248
      return((int) '@');
1249
    case MY_LEX_IDENT_OR_KEYWORD:
1250
      /*
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1251
        We come here when we have found two '@' in a row.
1252
        We should now be able to handle:
1253
        [(global | local | session) .]variable_name
1 by brian
clean slate
1254
      */
1255
1256
      for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1257
      /* If there were non-ASCII characters, mark that we must convert */
1258
      result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1259
1260
      if (c == '.')
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1261
        lip->next_state=MY_LEX_IDENT_SEP;
1 by brian
clean slate
1262
      length= lip->yyLength();
1263
      if (length == 0)
1264
        return(ABORT_SYM);              // Names must be nonempty.
1265
      if ((tokval= find_keyword(lip, length,0)))
1266
      {
1267
        lip->yyUnget();                         // Put back 'c'
1014.4.9 by Jay Pipes
Code style cleanup and removal of dead LEX_SERVER_OPTIONS struct
1268
        return(tokval);				// Was keyword
1 by brian
clean slate
1269
      }
1270
      yylval->lex_str=get_token(lip, 0, length);
1271
1272
      lip->body_utf8_append(lip->m_cpp_text_start);
1273
520.1.22 by Brian Aker
Second pass of thd cleanup
1274
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1 by brian
clean slate
1275
                                    lip->m_cpp_text_end);
1276
1277
      return(result_state);
1278
    }
1279
  }
1280
}
1281
1282
/**
1283
  Construct a copy of this object to be used for mysql_alter_table
1284
  and mysql_create_table.
1285
1286
  Historically, these two functions modify their Alter_info
1287
  arguments. This behaviour breaks re-execution of prepared
1288
  statements and stored procedures and is compensated by always
1289
  supplying a copy of Alter_info to these functions.
1290
520.1.21 by Brian Aker
THD -> Session rename
1291
  @return You need to use check the error in Session for out
1 by brian
clean slate
1292
  of memory condition after calling this function.
1293
*/
1294
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
1295
  :drop_list(rhs.drop_list, mem_root),
1296
  alter_list(rhs.alter_list, mem_root),
1297
  key_list(rhs.key_list, mem_root),
1298
  create_list(rhs.create_list, mem_root),
1299
  flags(rhs.flags),
1300
  keys_onoff(rhs.keys_onoff),
1301
  tablespace_op(rhs.tablespace_op),
1302
  no_parts(rhs.no_parts),
1303
  build_method(rhs.build_method),
1304
  datetime_field(rhs.datetime_field),
1305
  error_if_not_empty(rhs.error_if_not_empty)
1306
{
1307
  /*
1308
    Make deep copies of used objects.
1309
    This is not a fully deep copy - clone() implementations
1310
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
1311
    do not copy string constants. At the same length the only
1312
    reason we make a copy currently is that ALTER/CREATE TABLE
1313
    code changes input Alter_info definitions, but string
1314
    constants never change.
1315
  */
1316
  list_copy_and_replace_each_value(drop_list, mem_root);
1317
  list_copy_and_replace_each_value(alter_list, mem_root);
1318
  list_copy_and_replace_each_value(key_list, mem_root);
1319
  list_copy_and_replace_each_value(create_list, mem_root);
1320
}
1321
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1322
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1 by brian
clean slate
1323
{
1324
  /*
1325
    TODO:
1326
    This code assumes that there are no multi-bytes characters
1327
    that can be considered white-space.
1328
  */
1329
  while ((str->length > 0) && (my_isspace(cs, str->str[0])))
1330
  {
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
1331
    str->length--;
1332
    str->str++;
1 by brian
clean slate
1333
  }
1334
1335
  /*
1336
    FIXME:
1337
    Also, parsing backward is not safe with multi bytes characters
1338
  */
1339
  while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
1340
  {
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
1341
    str->length--;
1 by brian
clean slate
1342
  }
1343
}
1344
1345
/*
846 by Brian Aker
Removing on typedeffed class.
1346
  Select_Lex structures initialisations
1 by brian
clean slate
1347
*/
847 by Brian Aker
More typdef class removal.
1348
void Select_Lex_Node::init_query()
1 by brian
clean slate
1349
{
1350
  options= 0;
1351
  linkage= UNSPECIFIED_TYPE;
1352
  no_error= no_table_names_allowed= 0;
1353
  uncacheable= 0;
1354
}
1355
847 by Brian Aker
More typdef class removal.
1356
void Select_Lex_Node::init_select()
1 by brian
clean slate
1357
{
1358
}
1359
848 by Brian Aker
typdef class removal (just... use the name of the class).
1360
void Select_Lex_Unit::init_query()
1 by brian
clean slate
1361
{
847 by Brian Aker
More typdef class removal.
1362
  Select_Lex_Node::init_query();
1 by brian
clean slate
1363
  linkage= GLOBAL_OPTIONS_TYPE;
1364
  global_parameters= first_select();
1365
  select_limit_cnt= HA_POS_ERROR;
1366
  offset_limit_cnt= 0;
1367
  union_distinct= 0;
1368
  prepared= optimized= executed= 0;
1369
  item= 0;
1370
  union_result= 0;
1371
  table= 0;
1372
  fake_select_lex= 0;
1373
  cleaned= 0;
1374
  item_list.empty();
1375
  describe= 0;
1376
  found_rows_for_union= 0;
1377
}
1378
846 by Brian Aker
Removing on typedeffed class.
1379
void Select_Lex::init_query()
1 by brian
clean slate
1380
{
847 by Brian Aker
More typdef class removal.
1381
  Select_Lex_Node::init_query();
1 by brian
clean slate
1382
  table_list.empty();
1383
  top_join_list.empty();
1384
  join_list= &top_join_list;
1385
  embedding= leaf_tables= 0;
1386
  item_list.empty();
1387
  join= 0;
177.1.1 by brian
Removed dead code around prep.
1388
  having= where= 0;
1 by brian
clean slate
1389
  olap= UNSPECIFIED_OLAP_TYPE;
1390
  having_fix_field= 0;
1391
  context.select_lex= this;
1392
  context.init();
1393
  /*
1394
    Add the name resolution context of the current (sub)query to the
1395
    stack of contexts for the whole query.
1396
    TODO:
1397
    push_context may return an error if there is no memory for a new
1398
    element in the stack, however this method has no return value,
1399
    thus push_context should be moved to a place where query
1400
    initialization is checked for failure.
1401
  */
1402
  parent_lex->push_context(&context);
1403
  cond_count= between_count= with_wild= 0;
1404
  max_equal_elems= 0;
1405
  ref_pointer_array= 0;
1406
  select_n_where_fields= 0;
1407
  select_n_having_items= 0;
1408
  subquery_in_having= explicit_limit= 0;
1409
  is_item_list_lookup= 0;
1410
  parsing_place= NO_MATTER;
177.1.2 by brian
Removed dead variable, sorted authors file.
1411
  exclude_from_table_unique_test= false;
1 by brian
clean slate
1412
  nest_level= 0;
1413
  link_next= 0;
1414
}
1415
846 by Brian Aker
Removing on typedeffed class.
1416
void Select_Lex::init_select()
1 by brian
clean slate
1417
{
1418
  sj_nests.empty();
1419
  group_list.empty();
1420
  type= db= 0;
1421
  having= 0;
1422
  table_join_options= 0;
1423
  in_sum_expr= with_wild= 0;
1424
  options= 0;
1425
  braces= 0;
1426
  interval_list.empty();
1427
  inner_sum_func_list= 0;
1428
  linkage= UNSPECIFIED_TYPE;
1429
  order_list.elements= 0;
1430
  order_list.first= 0;
481 by Brian Aker
Remove all of uchar.
1431
  order_list.next= (unsigned char**) &order_list.first;
1 by brian
clean slate
1432
  /* Set limit and offset to default values */
1433
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1434
  offset_limit= 0;      /* denotes the default offset = 0 */
1435
  with_sum_func= 0;
1436
  is_correlated= 0;
1437
  cur_pos_in_select_list= UNDEF_POS;
1438
  non_agg_fields.empty();
1439
  cond_value= having_value= Item::COND_UNDEF;
1440
  inner_refs_list.empty();
1441
  full_group_by_flag= 0;
1442
}
1443
1444
/*
846 by Brian Aker
Removing on typedeffed class.
1445
  Select_Lex structures linking
1 by brian
clean slate
1446
*/
1447
1448
/* include on level down */
847 by Brian Aker
More typdef class removal.
1449
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1 by brian
clean slate
1450
{
1451
  if ((next= upper->slave))
1452
    next->prev= &next;
1453
  prev= &upper->slave;
1454
  upper->slave= this;
1455
  master= upper;
1456
  slave= 0;
1457
}
1458
1459
/*
1460
  include on level down (but do not link)
1461
1462
  SYNOPSYS
847 by Brian Aker
More typdef class removal.
1463
    Select_Lex_Node::include_standalone()
1 by brian
clean slate
1464
    upper - reference on node underr which this node should be included
1465
    ref - references on reference on this node
1466
*/
847 by Brian Aker
More typdef class removal.
1467
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1468
					    Select_Lex_Node **ref)
1 by brian
clean slate
1469
{
1470
  next= 0;
1471
  prev= ref;
1472
  master= upper;
1473
  slave= 0;
1474
}
1475
1476
/* include neighbour (on same level) */
847 by Brian Aker
More typdef class removal.
1477
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1 by brian
clean slate
1478
{
1479
  if ((next= before->next))
1480
    next->prev= &next;
1481
  prev= &before->next;
1482
  before->next= this;
1483
  master= before->master;
1484
  slave= 0;
1485
}
1486
846 by Brian Aker
Removing on typedeffed class.
1487
/* including in global Select_Lex list */
847 by Brian Aker
More typdef class removal.
1488
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1 by brian
clean slate
1489
{
1490
  if ((link_next= *plink))
1491
    link_next->link_prev= &link_next;
1492
  link_prev= plink;
1493
  *plink= this;
1494
}
1495
1496
//excluding from global list (internal function)
847 by Brian Aker
More typdef class removal.
1497
void Select_Lex_Node::fast_exclude()
1 by brian
clean slate
1498
{
1499
  if (link_prev)
1500
  {
1501
    if ((*link_prev= link_next))
1502
      link_next->link_prev= link_prev;
1503
  }
1504
  // Remove slave structure
1505
  for (; slave; slave= slave->next)
1506
    slave->fast_exclude();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1507
1 by brian
clean slate
1508
}
1509
1510
/*
1511
  excluding select_lex structure (except first (first select can't be
1512
  deleted, because it is most upper select))
1513
*/
847 by Brian Aker
More typdef class removal.
1514
void Select_Lex_Node::exclude()
1 by brian
clean slate
1515
{
1516
  //exclude from global list
1517
  fast_exclude();
1518
  //exclude from other structures
1519
  if ((*prev= next))
1520
    next->prev= prev;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1521
  /*
1522
     We do not need following statements, because prev pointer of first
1 by brian
clean slate
1523
     list element point to master->slave
1524
     if (master->slave == this)
1525
       master->slave= next;
1526
  */
1527
}
1528
1529
1530
/*
1531
  Exclude level of current unit from tree of SELECTs
1532
1533
  SYNOPSYS
848 by Brian Aker
typdef class removal (just... use the name of the class).
1534
    Select_Lex_Unit::exclude_level()
1 by brian
clean slate
1535
1536
  NOTE: units which belong to current will be brought up on level of
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1537
  currernt unit
1 by brian
clean slate
1538
*/
848 by Brian Aker
typdef class removal (just... use the name of the class).
1539
void Select_Lex_Unit::exclude_level()
1 by brian
clean slate
1540
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
1541
  Select_Lex_Unit *units= 0, **units_last= &units;
846 by Brian Aker
Removing on typedeffed class.
1542
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1 by brian
clean slate
1543
  {
1544
    // unlink current level from global SELECTs list
1545
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1546
      sl->link_next->link_prev= sl->link_prev;
1547
1548
    // bring up underlay levels
848 by Brian Aker
typdef class removal (just... use the name of the class).
1549
    Select_Lex_Unit **last= 0;
1550
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1 by brian
clean slate
1551
    {
1552
      u->master= master;
848 by Brian Aker
typdef class removal (just... use the name of the class).
1553
      last= (Select_Lex_Unit**)&(u->next);
1 by brian
clean slate
1554
    }
1555
    if (last)
1556
    {
1557
      (*units_last)= sl->first_inner_unit();
1558
      units_last= last;
1559
    }
1560
  }
1561
  if (units)
1562
  {
1563
    // include brought up levels in place of current
1564
    (*prev)= units;
848 by Brian Aker
typdef class removal (just... use the name of the class).
1565
    (*units_last)= (Select_Lex_Unit*)next;
1 by brian
clean slate
1566
    if (next)
847 by Brian Aker
More typdef class removal.
1567
      next->prev= (Select_Lex_Node**)units_last;
1 by brian
clean slate
1568
    units->prev= prev;
1569
  }
1570
  else
1571
  {
1572
    // exclude currect unit from list of nodes
1573
    (*prev)= next;
1574
    if (next)
1575
      next->prev= prev;
1576
  }
1577
}
1578
1579
/*
1580
  Exclude subtree of current unit from tree of SELECTs
1581
*/
848 by Brian Aker
typdef class removal (just... use the name of the class).
1582
void Select_Lex_Unit::exclude_tree()
1 by brian
clean slate
1583
{
846 by Brian Aker
Removing on typedeffed class.
1584
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1 by brian
clean slate
1585
  {
1586
    // unlink current level from global SELECTs list
1587
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1588
      sl->link_next->link_prev= sl->link_prev;
1589
1590
    // unlink underlay levels
848 by Brian Aker
typdef class removal (just... use the name of the class).
1591
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1 by brian
clean slate
1592
    {
1593
      u->exclude_level();
1594
    }
1595
  }
1596
  // exclude currect unit from list of nodes
1597
  (*prev)= next;
1598
  if (next)
1599
    next->prev= prev;
1600
}
1601
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
1602
/**
1603
 * Mark all Select_Lex struct from this to 'last' as dependent
1604
 *
1605
 * @param Pointer to last Select_Lex struct, before wich all
1606
 *        Select_Lex have to be marked as dependent
1607
 * @note 'last' should be reachable from this Select_Lex_Node
1608
 */
846 by Brian Aker
Removing on typedeffed class.
1609
void Select_Lex::mark_as_dependent(Select_Lex *last)
1 by brian
clean slate
1610
{
1611
  /*
1612
    Mark all selects from resolved to 1 before select where was
1613
    found table as depended (of select where was found table)
1614
  */
846 by Brian Aker
Removing on typedeffed class.
1615
  for (Select_Lex *s= this;
1 by brian
clean slate
1616
       s && s != last;
1617
       s= s->outer_select())
1618
  {
1619
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1620
    {
1621
      // Select is dependent of outer select
1622
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1623
                       UNCACHEABLE_DEPENDENT;
848 by Brian Aker
typdef class removal (just... use the name of the class).
1624
      Select_Lex_Unit *munit= s->master_unit();
1 by brian
clean slate
1625
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1626
                       UNCACHEABLE_DEPENDENT;
846 by Brian Aker
Removing on typedeffed class.
1627
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1 by brian
clean slate
1628
      {
1629
        if (sl != s &&
1630
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1631
          sl->uncacheable|= UNCACHEABLE_UNITED;
1632
      }
1633
    }
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1634
    s->is_correlated= true;
1 by brian
clean slate
1635
    Item_subselect *subquery_predicate= s->master_unit()->item;
1636
    if (subquery_predicate)
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1637
      subquery_predicate->is_correlated= true;
1 by brian
clean slate
1638
  }
1639
}
1640
847 by Brian Aker
More typdef class removal.
1641
bool Select_Lex_Node::set_braces(bool)
1019.1.6 by Brian Aker
A number of random cleanups.
1642
{ return true; }
1643
1644
bool Select_Lex_Node::inc_in_sum_expr()
1645
{ return true; }
1646
1647
uint32_t Select_Lex_Node::get_in_sum_expr() 
1648
{ return 0; }
1649
1650
TableList* Select_Lex_Node::get_table_list()
1651
{ return NULL; }
1652
1653
List<Item>* Select_Lex_Node::get_item_list()
1654
{ return NULL; }
1655
847 by Brian Aker
More typdef class removal.
1656
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
654 by Brian Aker
Remove unused (yet more)
1657
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1 by brian
clean slate
1658
{
1659
  return 0;
1660
}
1019.1.6 by Brian Aker
A number of random cleanups.
1661
847 by Brian Aker
More typdef class removal.
1662
uint32_t Select_Lex_Node::get_table_join_options()
1 by brian
clean slate
1663
{
1664
  return 0;
1665
}
1666
1667
/*
1668
  prohibit using LIMIT clause
1669
*/
846 by Brian Aker
Removing on typedeffed class.
1670
bool Select_Lex::test_limit()
1 by brian
clean slate
1671
{
1672
  if (select_limit != 0)
1673
  {
1674
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1675
             "LIMIT & IN/ALL/ANY/SOME subquery");
1019.1.6 by Brian Aker
A number of random cleanups.
1676
    return true;
1 by brian
clean slate
1677
  }
1019.1.6 by Brian Aker
A number of random cleanups.
1678
  return false;
1 by brian
clean slate
1679
}
1680
848 by Brian Aker
typdef class removal (just... use the name of the class).
1681
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1 by brian
clean slate
1682
{
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
1683
  return this;
1 by brian
clean slate
1684
}
1685
848 by Brian Aker
typdef class removal (just... use the name of the class).
1686
Select_Lex* Select_Lex_Unit::outer_select()
1 by brian
clean slate
1687
{
846 by Brian Aker
Removing on typedeffed class.
1688
  return (Select_Lex*) master;
1 by brian
clean slate
1689
}
1690
846 by Brian Aker
Removing on typedeffed class.
1691
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1 by brian
clean slate
1692
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1693
  return add_to_list(session, order_list, item, asc);
1 by brian
clean slate
1694
}
1695
846 by Brian Aker
Removing on typedeffed class.
1696
bool Select_Lex::add_item_to_list(Session *, Item *item)
1 by brian
clean slate
1697
{
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1698
  return(item_list.push_back(item));
1 by brian
clean slate
1699
}
1700
846 by Brian Aker
Removing on typedeffed class.
1701
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1 by brian
clean slate
1702
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1703
  return add_to_list(session, group_list, item, asc);
1 by brian
clean slate
1704
}
1705
848 by Brian Aker
typdef class removal (just... use the name of the class).
1706
Select_Lex_Unit* Select_Lex::master_unit()
846 by Brian Aker
Removing on typedeffed class.
1707
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
1708
  return (Select_Lex_Unit*) master;
846 by Brian Aker
Removing on typedeffed class.
1709
}
1710
1711
Select_Lex* Select_Lex::outer_select()
1712
{
1713
  return (Select_Lex*) master->get_master();
1714
}
1715
1716
bool Select_Lex::set_braces(bool value)
1 by brian
clean slate
1717
{
1718
  braces= value;
1019.1.6 by Brian Aker
A number of random cleanups.
1719
  return false;
1 by brian
clean slate
1720
}
1721
846 by Brian Aker
Removing on typedeffed class.
1722
bool Select_Lex::inc_in_sum_expr()
1 by brian
clean slate
1723
{
1724
  in_sum_expr++;
1019.1.6 by Brian Aker
A number of random cleanups.
1725
  return false;
1 by brian
clean slate
1726
}
1727
846 by Brian Aker
Removing on typedeffed class.
1728
uint32_t Select_Lex::get_in_sum_expr()
1 by brian
clean slate
1729
{
1730
  return in_sum_expr;
1731
}
1732
846 by Brian Aker
Removing on typedeffed class.
1733
TableList* Select_Lex::get_table_list()
1 by brian
clean slate
1734
{
327.2.4 by Brian Aker
Refactoring table.h
1735
  return (TableList*) table_list.first;
1 by brian
clean slate
1736
}
1737
846 by Brian Aker
Removing on typedeffed class.
1738
List<Item>* Select_Lex::get_item_list()
1 by brian
clean slate
1739
{
1740
  return &item_list;
1741
}
1742
846 by Brian Aker
Removing on typedeffed class.
1743
uint32_t Select_Lex::get_table_join_options()
1 by brian
clean slate
1744
{
1745
  return table_join_options;
1746
}
1747
846 by Brian Aker
Removing on typedeffed class.
1748
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1 by brian
clean slate
1749
{
1750
  if (ref_pointer_array)
1019.1.6 by Brian Aker
A number of random cleanups.
1751
    return false;
1 by brian
clean slate
1752
1753
  return (ref_pointer_array=
520.1.22 by Brian Aker
Second pass of thd cleanup
1754
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1 by brian
clean slate
1755
                                                 item_list.elements +
1756
                                                 select_n_having_items +
1757
                                                 select_n_where_fields +
1758
                                                 order_group_num)*5)) == 0;
1759
}
1760
848 by Brian Aker
typdef class removal (just... use the name of the class).
1761
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
1 by brian
clean slate
1762
{
1763
  bool union_all= !union_distinct;
846 by Brian Aker
Removing on typedeffed class.
1764
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1 by brian
clean slate
1765
  {
1766
    if (sl != first_select())
1767
    {
1768
      str->append(STRING_WITH_LEN(" union "));
1769
      if (union_all)
1770
	str->append(STRING_WITH_LEN("all "));
1771
      else if (union_distinct == sl)
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1772
        union_all= true;
1 by brian
clean slate
1773
    }
1774
    if (sl->braces)
1775
      str->append('(');
520.1.22 by Brian Aker
Second pass of thd cleanup
1776
    sl->print(session, str, query_type);
1 by brian
clean slate
1777
    if (sl->braces)
1778
      str->append(')');
1779
  }
1780
  if (fake_select_lex == global_parameters)
1781
  {
1782
    if (fake_select_lex->order_list.elements)
1783
    {
1784
      str->append(STRING_WITH_LEN(" order by "));
1785
      fake_select_lex->print_order(
1786
        str,
327.2.3 by Brian Aker
Refactoring of class Table
1787
        (order_st *) fake_select_lex->order_list.first,
1 by brian
clean slate
1788
        query_type);
1789
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1790
    fake_select_lex->print_limit(session, str, query_type);
1 by brian
clean slate
1791
  }
1792
}
1793
846 by Brian Aker
Removing on typedeffed class.
1794
void Select_Lex::print_order(String *str,
327.2.3 by Brian Aker
Refactoring of class Table
1795
                                order_st *order,
1 by brian
clean slate
1796
                                enum_query_type query_type)
1797
{
1798
  for (; order; order= order->next)
1799
  {
1800
    if (order->counter_used)
1801
    {
1802
      char buffer[20];
482 by Brian Aker
Remove uint.
1803
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
1 by brian
clean slate
1804
      str->append(buffer, length);
1805
    }
1806
    else
1807
      (*order->item)->print(str, query_type);
1808
    if (!order->asc)
1809
      str->append(STRING_WITH_LEN(" desc"));
1810
    if (order->next)
1811
      str->append(',');
1812
  }
1813
}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1814
846 by Brian Aker
Removing on typedeffed class.
1815
void Select_Lex::print_limit(Session *, String *str,
1 by brian
clean slate
1816
                                enum_query_type query_type)
1817
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
1818
  Select_Lex_Unit *unit= master_unit();
1 by brian
clean slate
1819
  Item_subselect *item= unit->item;
1820
1821
  if (item && unit->global_parameters == this)
1822
  {
1823
    Item_subselect::subs_type subs_type= item->substype();
1824
    if (subs_type == Item_subselect::EXISTS_SUBS ||
1825
        subs_type == Item_subselect::IN_SUBS ||
1826
        subs_type == Item_subselect::ALL_SUBS)
1827
    {
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1828
      assert(!item->fixed ||
1 by brian
clean slate
1829
                  /*
1830
                    If not using materialization both:
1831
                    select_limit == 1, and there should be no offset_limit.
1832
                  */
1833
                  (((subs_type == Item_subselect::IN_SUBS) &&
1834
                    ((Item_in_subselect*)item)->exec_method ==
1835
                    Item_in_subselect::MATERIALIZATION) ?
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1836
                   true :
398.1.8 by Monty Taylor
Enabled -Wlong-long.
1837
                   (select_limit->val_int() == 1L) &&
1 by brian
clean slate
1838
                   offset_limit == 0));
1839
      return;
1840
    }
1841
  }
1842
  if (explicit_limit)
1843
  {
1844
    str->append(STRING_WITH_LEN(" limit "));
1845
    if (offset_limit)
1846
    {
1847
      offset_limit->print(str, query_type);
1848
      str->append(',');
1849
    }
1850
    select_limit->print(str, query_type);
1851
  }
1852
}
1853
1854
/**
520.1.21 by Brian Aker
THD -> Session rename
1855
  @brief Restore the LEX and Session in case of a parse error.
1 by brian
clean slate
1856
1857
  This is a clean up call that is invoked by the Bison generated
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
1858
  parser before returning an error from DRIZZLEparse. If your
1 by brian
clean slate
1859
  semantic actions manipulate with the global thread state (which
1860
  is a very bad practice and should not normally be employed) and
1861
  need a clean-up in case of error, and you can not use %destructor
1862
  rule in the grammar file itself, this function should be used
1863
  to implement the clean up.
1864
*/
654 by Brian Aker
Remove unused (yet more)
1865
void LEX::cleanup_lex_after_parse_error(Session *)
1 by brian
clean slate
1866
{
1867
}
1868
1869
/*
1870
  Initialize (or reset) Query_tables_list object.
1871
1872
  SYNOPSIS
1873
    reset_query_tables_list()
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1874
      init  true  - we should perform full initialization of object with
1 by brian
clean slate
1875
                    allocating needed memory
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1876
            false - object is already initialized so we should only reset
1 by brian
clean slate
1877
                    its state so it can be used for parsing/processing
1878
                    of new statement
1879
1880
  DESCRIPTION
1881
    This method initializes Query_tables_list so it can be used as part
1882
    of LEX object for parsing/processing of statement. One can also use
1883
    this method to reset state of already initialized Query_tables_list
1884
    so it can be used for processing of new statement.
1885
*/
1886
void Query_tables_list::reset_query_tables_list(bool init)
1887
{
1888
  if (!init && query_tables)
1889
  {
327.2.4 by Brian Aker
Refactoring table.h
1890
    TableList *table= query_tables;
1 by brian
clean slate
1891
    for (;;)
1892
    {
1893
      if (query_tables_last == &table->next_global ||
1894
          !(table= table->next_global))
1895
        break;
1896
    }
1897
  }
1898
  query_tables= 0;
1899
  query_tables_last= &query_tables;
1900
  query_tables_own_last= 0;
1901
}
1902
1903
/*
1904
  Initialize LEX object.
1905
1906
  SYNOPSIS
575.4.7 by Monty Taylor
More header cleanup.
1907
    LEX::LEX()
1 by brian
clean slate
1908
1909
  NOTE
1910
    LEX object initialized with this constructor can be used as part of
520.1.21 by Brian Aker
THD -> Session rename
1911
    Session object for which one can safely call open_tables(), lock_tables()
1 by brian
clean slate
1912
    and close_thread_tables() functions. But it is not yet ready for
1913
    statement parsing. On should use lex_start() function to prepare LEX
1914
    for this.
1915
*/
575.4.7 by Monty Taylor
More header cleanup.
1916
LEX::LEX()
1 by brian
clean slate
1917
  :result(0), yacc_yyss(0), yacc_yyvs(0),
1918
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1919
{
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1920
  reset_query_tables_list(true);
1 by brian
clean slate
1921
}
1922
1923
/*
1924
  Detect that we need only table structure of derived table/view
1925
1926
  SYNOPSIS
1927
    only_view_structure()
1928
1929
  RETURN
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1930
    true yes, we need only structure
1931
    false no, we need data
1 by brian
clean slate
1932
*/
575.4.7 by Monty Taylor
More header cleanup.
1933
bool LEX::only_view_structure()
1 by brian
clean slate
1934
{
1935
  switch (sql_command) {
1936
  case SQLCOM_SHOW_CREATE:
1937
  case SQLCOM_SHOW_TABLES:
1938
  case SQLCOM_SHOW_FIELDS:
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1939
    return true;
1 by brian
clean slate
1940
  default:
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1941
    return false;
1 by brian
clean slate
1942
  }
1943
}
1944
1945
/*
1946
  Should Items_ident be printed correctly
1947
1948
  SYNOPSIS
1949
    need_correct_ident()
1950
1951
  RETURN
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1952
    true yes, we need only structure
1953
    false no, we need data
1 by brian
clean slate
1954
*/
575.4.7 by Monty Taylor
More header cleanup.
1955
bool LEX::need_correct_ident()
1 by brian
clean slate
1956
{
1957
  switch(sql_command)
1958
  {
1959
  case SQLCOM_SHOW_CREATE:
1960
  case SQLCOM_SHOW_TABLES:
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1961
    return true;
1 by brian
clean slate
1962
  default:
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1963
    return false;
1 by brian
clean slate
1964
  }
1965
}
1966
1967
/**
1968
  This method should be called only during parsing.
1969
  It is aware of compound statements (stored routine bodies)
1970
  and will initialize the destination with the default
1971
  database of the stored routine, rather than the default
1972
  database of the connection it is parsed in.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1973
  E.g. if one has no current database selected, or current database
1 by brian
clean slate
1974
  set to 'bar' and then issues:
1975
1976
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
1977
1978
  t1 is meant to refer to foo.t1, not to bar.t1.
1979
1980
  This method is needed to support this rule.
1981
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1982
  @return true in case of error (parsing should be aborted, false in
1 by brian
clean slate
1983
  case of success
1984
*/
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
1985
bool LEX::copy_db_to(char **p_db, size_t *p_db_length) const
1 by brian
clean slate
1986
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1987
  return session->copy_db_to(p_db, p_db_length);
1 by brian
clean slate
1988
}
1989
1990
/*
1991
  initialize limit counters
1992
1993
  SYNOPSIS
848 by Brian Aker
typdef class removal (just... use the name of the class).
1994
    Select_Lex_Unit::set_limit()
846 by Brian Aker
Removing on typedeffed class.
1995
    values	- Select_Lex with initial values for counters
1 by brian
clean slate
1996
*/
848 by Brian Aker
typdef class removal (just... use the name of the class).
1997
void Select_Lex_Unit::set_limit(Select_Lex *sl)
1 by brian
clean slate
1998
{
1999
  ha_rows select_limit_val;
151 by Brian Aker
Ulonglong to uint64_t
2000
  uint64_t val;
1 by brian
clean slate
2001
2002
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2003
  select_limit_val= (ha_rows)val;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2004
  /*
151 by Brian Aker
Ulonglong to uint64_t
2005
    Check for overflow : ha_rows can be smaller then uint64_t if
1 by brian
clean slate
2006
    BIG_TABLES is off.
2007
    */
151 by Brian Aker
Ulonglong to uint64_t
2008
  if (val != (uint64_t)select_limit_val)
1 by brian
clean slate
2009
    select_limit_val= HA_POS_ERROR;
2010
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
398.1.8 by Monty Taylor
Enabled -Wlong-long.
2011
                                                 0UL);
1 by brian
clean slate
2012
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2013
  if (select_limit_cnt < select_limit_val)
2014
    select_limit_cnt= HA_POS_ERROR;		// no limit
2015
}
2016
2017
/*
2018
  Unlink the first table from the global table list and the first table from
2019
  outer select (lex->select_lex) local list
2020
2021
  SYNOPSIS
2022
    unlink_first_table()
2023
    link_to_local	Set to 1 if caller should link this table to local list
2024
2025
  NOTES
2026
    We assume that first tables in both lists is the same table or the local
2027
    list is empty.
2028
2029
  RETURN
2030
    0	If 'query_tables' == 0
2031
    unlinked table
2032
      In this case link_to_local is set.
2033
2034
*/
575.4.7 by Monty Taylor
More header cleanup.
2035
TableList *LEX::unlink_first_table(bool *link_to_local)
1 by brian
clean slate
2036
{
327.2.4 by Brian Aker
Refactoring table.h
2037
  TableList *first;
1 by brian
clean slate
2038
  if ((first= query_tables))
2039
  {
2040
    /*
2041
      Exclude from global table list
2042
    */
2043
    if ((query_tables= query_tables->next_global))
2044
      query_tables->prev_global= &query_tables;
2045
    else
2046
      query_tables_last= &query_tables;
2047
    first->next_global= 0;
2048
2049
    /*
2050
      and from local list if it is not empty
2051
    */
2052
    if ((*link_to_local= test(select_lex.table_list.first)))
2053
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2054
      select_lex.context.table_list=
1 by brian
clean slate
2055
        select_lex.context.first_name_resolution_table= first->next_local;
481 by Brian Aker
Remove all of uchar.
2056
      select_lex.table_list.first= (unsigned char*) (first->next_local);
1 by brian
clean slate
2057
      select_lex.table_list.elements--;	//safety
2058
      first->next_local= 0;
2059
      /*
2060
        Ensure that the global list has the same first table as the local
2061
        list.
2062
      */
2063
      first_lists_tables_same();
2064
    }
2065
  }
2066
  return first;
2067
}
2068
2069
/*
2070
  Bring first local table of first most outer select to first place in global
2071
  table list
2072
2073
  SYNOPSYS
575.4.7 by Monty Taylor
More header cleanup.
2074
     LEX::first_lists_tables_same()
1 by brian
clean slate
2075
2076
  NOTES
2077
    In many cases (for example, usual INSERT/DELETE/...) the first table of
846 by Brian Aker
Removing on typedeffed class.
2078
    main Select_Lex have special meaning => check that it is the first table
1 by brian
clean slate
2079
    in global list and re-link to be first in the global list if it is
2080
    necessary.  We need such re-linking only for queries with sub-queries in
2081
    the select list, as only in this case tables of sub-queries will go to
2082
    the global list first.
2083
*/
575.4.7 by Monty Taylor
More header cleanup.
2084
void LEX::first_lists_tables_same()
1 by brian
clean slate
2085
{
327.2.4 by Brian Aker
Refactoring table.h
2086
  TableList *first_table= (TableList*) select_lex.table_list.first;
1 by brian
clean slate
2087
  if (query_tables != first_table && first_table != 0)
2088
  {
327.2.4 by Brian Aker
Refactoring table.h
2089
    TableList *next;
1 by brian
clean slate
2090
    if (query_tables_last == &first_table->next_global)
2091
      query_tables_last= first_table->prev_global;
2092
2093
    if ((next= *first_table->prev_global= first_table->next_global))
2094
      next->prev_global= first_table->prev_global;
2095
    /* include in new place */
2096
    first_table->next_global= query_tables;
2097
    /*
2098
       We are sure that query_tables is not 0, because first_table was not
2099
       first table in the global list => we can use
2100
       query_tables->prev_global without check of query_tables
2101
    */
2102
    query_tables->prev_global= &first_table->next_global;
2103
    first_table->prev_global= &query_tables;
2104
    query_tables= first_table;
2105
  }
2106
}
2107
2108
/*
2109
  Link table back that was unlinked with unlink_first_table()
2110
2111
  SYNOPSIS
2112
    link_first_table_back()
2113
    link_to_local	do we need link this table to local
2114
2115
  RETURN
2116
    global list
2117
*/
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
2118
void LEX::link_first_table_back(TableList *first, bool link_to_local)
1 by brian
clean slate
2119
{
2120
  if (first)
2121
  {
2122
    if ((first->next_global= query_tables))
2123
      query_tables->prev_global= &first->next_global;
2124
    else
2125
      query_tables_last= &first->next_global;
2126
    query_tables= first;
2127
2128
    if (link_to_local)
2129
    {
327.2.4 by Brian Aker
Refactoring table.h
2130
      first->next_local= (TableList*) select_lex.table_list.first;
1 by brian
clean slate
2131
      select_lex.context.table_list= first;
481 by Brian Aker
Remove all of uchar.
2132
      select_lex.table_list.first= (unsigned char*) first;
1 by brian
clean slate
2133
      select_lex.table_list.elements++;	//safety
2134
    }
2135
  }
2136
}
2137
2138
/*
2139
  cleanup lex for case when we open table by table for processing
2140
2141
  SYNOPSIS
575.4.7 by Monty Taylor
More header cleanup.
2142
    LEX::cleanup_after_one_table_open()
1 by brian
clean slate
2143
2144
  NOTE
2145
    This method is mostly responsible for cleaning up of selects lists and
2146
    derived tables state. To rollback changes in Query_tables_list one has
51.1.52 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2147
    to call Query_tables_list::reset_query_tables_list(false).
1 by brian
clean slate
2148
*/
575.4.7 by Monty Taylor
More header cleanup.
2149
void LEX::cleanup_after_one_table_open()
1 by brian
clean slate
2150
{
2151
  /*
520.1.22 by Brian Aker
Second pass of thd cleanup
2152
    session->lex->derived_tables & additional units may be set if we open
2153
    a view. It is necessary to clear session->lex->derived_tables flag
1 by brian
clean slate
2154
    to prevent processing of derived tables during next open_and_lock_tables
2155
    if next table is a real table and cleanup & remove underlying units
520.1.22 by Brian Aker
Second pass of thd cleanup
2156
    NOTE: all units will be connected to session->lex->select_lex, because we
1 by brian
clean slate
2157
    have not UNION on most upper level.
2158
    */
2159
  if (all_selects_list != &select_lex)
2160
  {
2161
    derived_tables= 0;
2162
    /* cleunup underlying units (units of VIEW) */
848 by Brian Aker
typdef class removal (just... use the name of the class).
2163
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
1 by brian
clean slate
2164
         un;
2165
         un= un->next_unit())
2166
      un->cleanup();
2167
    /* reduce all selects list to default state */
2168
    all_selects_list= &select_lex;
2169
    /* remove underlying units (units of VIEW) subtree */
2170
    select_lex.cut_subtree();
2171
  }
2172
}
2173
2174
/*
846 by Brian Aker
Removing on typedeffed class.
2175
  There are Select_Lex::add_table_to_list &
2176
  Select_Lex::set_lock_for_tables are in sql_parse.cc
2177
2178
  Select_Lex::print is in sql_select.cc
2179
848 by Brian Aker
typdef class removal (just... use the name of the class).
2180
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2181
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2182
  Select_Lex_Unit::change_result
1 by brian
clean slate
2183
  are in sql_union.cc
2184
*/
2185
2186
/*
2187
  Sets the kind of hints to be added by the calls to add_index_hint().
2188
2189
  SYNOPSIS
2190
    set_index_hint_type()
2191
      type_arg     The kind of hints to be added from now on.
2192
      clause       The clause to use for hints to be added from now on.
2193
2194
  DESCRIPTION
2195
    Used in filling up the tagged hints list.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2196
    This list is filled by first setting the kind of the hint as a
1 by brian
clean slate
2197
    context variable and then adding hints of the current kind.
2198
    Then the context variable index_hint_type can be reset to the
2199
    next hint type.
2200
*/
1014.4.10 by Jay Pipes
Code style cleanups and removal of st_parsing_options.
2201
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg, index_clause_map clause)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2202
{
1 by brian
clean slate
2203
  current_index_hint_type= type_arg;
2204
  current_index_hint_clause= clause;
2205
}
2206
2207
/*
2208
  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
2209
2210
  SYNOPSIS
2211
    alloc_index_hints()
520.1.22 by Brian Aker
Second pass of thd cleanup
2212
      session         current thread.
1 by brian
clean slate
2213
*/
846 by Brian Aker
Removing on typedeffed class.
2214
void Select_Lex::alloc_index_hints (Session *session)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2215
{
2216
  index_hints= new (session->mem_root) List<Index_hint>();
1 by brian
clean slate
2217
}
2218
2219
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2220
  adds an element to the array storing index usage hints
1 by brian
clean slate
2221
  (ADD/FORCE/IGNORE INDEX).
2222
2223
  SYNOPSIS
2224
    add_index_hint()
520.1.22 by Brian Aker
Second pass of thd cleanup
2225
      session         current thread.
1 by brian
clean slate
2226
      str         name of the index.
2227
      length      number of characters in str.
2228
2229
  RETURN VALUE
2230
    0 on success, non-zero otherwise
2231
*/
846 by Brian Aker
Removing on typedeffed class.
2232
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
1 by brian
clean slate
2233
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2234
  return index_hints->push_front (new (session->mem_root)
1 by brian
clean slate
2235
                                 Index_hint(current_index_hint_type,
2236
                                            current_index_hint_clause,
2237
                                            str, length));
2238
}