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