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