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