~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
 
#include <config.h>
20
 
 
21
19
#define DRIZZLE_LEX 1
22
 
 
23
 
#include <drizzled/sql_reserved_words.h>
24
 
 
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>
32
 
#include <drizzled/select_result.h>
33
 
 
34
 
#include <cstdio>
35
 
#include <ctype.h>
36
 
 
37
 
union ParserType;
38
 
 
39
 
using namespace std;
40
 
 
41
 
/* Stay outside of the namespace because otherwise bison goes nuts */
42
 
int base_sql_lex(ParserType *arg, drizzled::Session *yysession);
43
 
 
44
 
namespace drizzled
45
 
{
46
 
 
47
 
static int lex_one_token(ParserType *arg, drizzled::Session *yysession);
48
 
 
49
 
/**
50
 
  save order by and tables in own lists.
 
20
#include <drizzled/server_includes.h>
 
21
 
 
22
static int lex_one_token(void *arg, void *yythd);
 
23
 
 
24
/*
 
25
  We are using pointer to this variable for distinguishing between assignment
 
26
  to NEW row field (when parsing trigger definition) and structured variable.
51
27
*/
52
 
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
53
 
{
54
 
  Order *order;
55
 
 
56
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
57
 
    return true;
58
 
 
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;
65
 
  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
66
 
 
67
 
  return false;
68
 
}
 
28
 
 
29
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
69
30
 
70
31
/**
71
32
  LEX_STRING constant for null-string to be used in parser and other places.
72
33
*/
73
34
const LEX_STRING null_lex_str= {NULL, 0};
74
35
 
75
 
Lex_input_stream::Lex_input_stream(Session *session,
 
36
/* Longest standard keyword name */
 
37
 
 
38
#define TOCK_NAME_LENGTH 24
 
39
 
 
40
/*
 
41
  The following data is based on the latin1 character set, and is only
 
42
  used when comparing keywords
 
43
*/
 
44
 
 
45
static uchar to_upper_lex[]=
 
46
{
 
47
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
 
48
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 
49
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 
50
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
 
51
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
52
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
 
53
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
54
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
 
55
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
56
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
 
57
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
 
58
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
 
59
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
60
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
 
61
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
62
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
 
63
};
 
64
 
 
65
/* 
 
66
  Names of the index hints (for error messages). Keep in sync with 
 
67
  index_hint_type 
 
68
*/
 
69
 
 
70
const char * index_hint_type_name[] =
 
71
{
 
72
  "IGNORE INDEX", 
 
73
  "USE INDEX", 
 
74
  "FORCE INDEX"
 
75
};
 
76
 
 
77
inline int lex_casecmp(const char *s, const char *t, uint len)
 
78
{
 
79
  while (len-- != 0 &&
 
80
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
 
81
  return (int) len+1;
 
82
}
 
83
 
 
84
#include <lex_hash.h>
 
85
 
 
86
 
 
87
void lex_init(void)
 
88
{
 
89
  uint i;
 
90
  for (i=0 ; i < array_elements(symbols) ; i++)
 
91
    symbols[i].length=(uchar) strlen(symbols[i].name);
 
92
  for (i=0 ; i < array_elements(sql_functions) ; i++)
 
93
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
 
94
 
 
95
  return;
 
96
}
 
97
 
 
98
 
 
99
void lex_free(void)
 
100
{                                       // Call this when daemon ends
 
101
  return;
 
102
}
 
103
 
 
104
 
 
105
void
 
106
st_parsing_options::reset()
 
107
{
 
108
  allows_variable= true;
 
109
  allows_select_into= true;
 
110
  allows_select_procedure= true;
 
111
  allows_derived= true;
 
112
}
 
113
 
 
114
Lex_input_stream::Lex_input_stream(THD *thd,
76
115
                                   const char* buffer,
77
 
                                   unsigned int length) :
78
 
  m_session(session),
 
116
                                   unsigned int length)
 
117
: m_thd(thd),
79
118
  yylineno(1),
80
119
  yytoklen(0),
81
120
  yylval(NULL),
95
134
  m_body_utf8(NULL),
96
135
  m_cpp_utf8_processed_ptr(NULL),
97
136
  next_state(MY_LEX_START),
 
137
  found_semicolon(NULL),
98
138
  ignore_space(1),
99
 
  in_comment(NO_COMMENT)
 
139
  in_comment(NO_COMMENT),
 
140
  m_underscore_cs(NULL)
100
141
{
101
 
  m_cpp_buf= (char*) session->getMemRoot()->allocate(length + 1);
 
142
  m_cpp_buf= (char*) thd->alloc(length + 1);
102
143
  m_cpp_ptr= m_cpp_buf;
103
144
}
104
145
 
112
153
     statement;
113
154
  2) Determine the beginning of the body.
114
155
 
115
 
  @param session        Thread context.
 
156
  @param thd        Thread context.
116
157
  @param begin_ptr  Pointer to the start of the body in the pre-processed
117
158
                    buffer.
118
159
*/
119
 
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
 
160
 
 
161
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
120
162
{
121
163
  assert(begin_ptr);
122
164
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
123
165
 
124
 
  uint32_t body_utf8_length=
125
 
    (m_buf_length / default_charset_info->mbminlen) *
 
166
  uint body_utf8_length=
 
167
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
126
168
    my_charset_utf8_bin.mbmaxlen;
127
169
 
128
 
  m_body_utf8= (char *) session->getMemRoot()->allocate(body_utf8_length + 1);
 
170
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
129
171
  m_body_utf8_ptr= m_body_utf8;
130
172
  *m_body_utf8_ptr= 0;
131
173
 
152
194
                  m_cpp_utf8_processed_ptr will be set in the end of the
153
195
                  operation.
154
196
*/
 
197
 
155
198
void Lex_input_stream::body_utf8_append(const char *ptr,
156
199
                                        const char *end_ptr)
157
200
{
180
223
  @param ptr  Pointer in the pre-processed buffer, which specifies the end
181
224
              of the chunk, which should be appended to the utf8 body.
182
225
*/
 
226
 
183
227
void Lex_input_stream::body_utf8_append(const char *ptr)
184
228
{
185
229
  body_utf8_append(ptr, ptr);
189
233
  The operation converts the specified text literal to the utf8 and appends
190
234
  the result to the utf8-body.
191
235
 
192
 
  @param session      Thread context.
 
236
  @param thd      Thread context.
193
237
  @param txt      Text literal.
194
238
  @param txt_cs   Character set of the text literal.
195
239
  @param end_ptr  Pointer in the pre-processed buffer, to which
196
240
                  m_cpp_utf8_processed_ptr will be set in the end of the
197
241
                  operation.
198
242
*/
199
 
void Lex_input_stream::body_utf8_append_literal(const LEX_STRING *txt,
 
243
 
 
244
void Lex_input_stream::body_utf8_append_literal(THD *thd,
 
245
                                                const LEX_STRING *txt,
 
246
                                                const CHARSET_INFO * const txt_cs,
200
247
                                                const char *end_ptr)
201
248
{
202
249
  if (!m_cpp_utf8_processed_ptr)
203
250
    return;
204
251
 
 
252
  LEX_STRING utf_txt;
 
253
 
 
254
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
 
255
  {
 
256
    thd->convert_string(&utf_txt,
 
257
                        &my_charset_utf8_general_ci,
 
258
                        txt->str, txt->length,
 
259
                        txt_cs);
 
260
  }
 
261
  else
 
262
  {
 
263
    utf_txt.str= txt->str;
 
264
    utf_txt.length= txt->length;
 
265
  }
 
266
 
205
267
  /* NOTE: utf_txt.length is in bytes, not in symbols. */
206
268
 
207
 
  memcpy(m_body_utf8_ptr, txt->str, txt->length);
208
 
  m_body_utf8_ptr += txt->length;
 
269
  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
 
270
  m_body_utf8_ptr += utf_txt.length;
209
271
  *m_body_utf8_ptr= 0;
210
272
 
211
273
  m_cpp_utf8_processed_ptr= end_ptr;
212
274
}
213
275
 
 
276
 
214
277
/*
215
278
  This is called before every query that is to be parsed.
216
279
  Because of this, it's critical to not do too much things here.
217
280
  (We already do too much here)
218
281
*/
219
 
void LEX::start(Session *arg)
220
 
{
221
 
  lex_start(arg);
222
 
}
223
 
 
224
 
void lex_start(Session *session)
225
 
{
226
 
  LEX *lex= session->getLex();
227
 
 
228
 
  lex->session= lex->unit.session= session;
229
 
 
230
 
  lex->context_stack.clear();
 
282
 
 
283
void lex_start(THD *thd)
 
284
{
 
285
  LEX *lex= thd->lex;
 
286
 
 
287
  lex->thd= lex->unit.thd= thd;
 
288
 
 
289
  lex->context_stack.empty();
231
290
  lex->unit.init_query();
232
291
  lex->unit.init_select();
233
292
  /* 'parent_lex' is used in init_query() so it must be before it. */
234
293
  lex->select_lex.parent_lex= lex;
235
294
  lex->select_lex.init_query();
236
 
  lex->value_list.clear();
237
 
  lex->update_list.clear();
238
 
  lex->auxiliary_table_list.clear();
 
295
  lex->value_list.empty();
 
296
  lex->update_list.empty();
 
297
  lex->param_list.empty();
 
298
  lex->auxiliary_table_list.empty();
239
299
  lex->unit.next= lex->unit.master=
240
300
    lex->unit.link_next= lex->unit.return_to= 0;
241
301
  lex->unit.prev= lex->unit.link_prev= 0;
244
304
  lex->select_lex.master= &lex->unit;
245
305
  lex->select_lex.prev= &lex->unit.slave;
246
306
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
247
 
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
 
307
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
248
308
  lex->select_lex.options= 0;
249
309
  lex->select_lex.init_order();
250
 
  lex->select_lex.group_list.clear();
 
310
  lex->select_lex.group_list.empty();
251
311
  lex->describe= 0;
 
312
  lex->subqueries= false;
252
313
  lex->derived_tables= 0;
253
314
  lex->lock_option= TL_READ;
254
315
  lex->leaf_tables_insert= 0;
255
 
  lex->var_list.clear();
 
316
  lex->parsing_options.reset();
 
317
  lex->empty_field_list_on_rset= 0;
256
318
  lex->select_lex.select_number= 1;
257
319
  lex->length=0;
258
320
  lex->select_lex.in_sum_expr=0;
259
 
  lex->select_lex.group_list.clear();
260
 
  lex->select_lex.order_list.clear();
 
321
  lex->select_lex.ftfunc_list_alloc.empty();
 
322
  lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
 
323
  lex->select_lex.group_list.empty();
 
324
  lex->select_lex.order_list.empty();
261
325
  lex->sql_command= SQLCOM_END;
262
326
  lex->duplicates= DUP_ERROR;
263
327
  lex->ignore= 0;
 
328
  lex->proc_list.first= 0;
264
329
  lex->escape_used= false;
265
330
  lex->query_tables= 0;
266
331
  lex->reset_query_tables_list(false);
272
337
  lex->nest_level=0 ;
273
338
  lex->allow_sum_func= 0;
274
339
  lex->in_sum_func= NULL;
275
 
  lex->type= 0;
 
340
  /*
 
341
    ok, there must be a better solution for this, long-term
 
342
    I tried "memset" in the sql_yacc.yy code, but that for
 
343
    some reason made the values zero, even if they were set
 
344
  */
 
345
  lex->server_options.server_name= 0;
 
346
  lex->server_options.server_name_length= 0;
 
347
  lex->server_options.host= 0;
 
348
  lex->server_options.db= 0;
 
349
  lex->server_options.username= 0;
 
350
  lex->server_options.password= 0;
 
351
  lex->server_options.scheme= 0;
 
352
  lex->server_options.owner= 0;
 
353
  lex->server_options.port= -1;
276
354
 
277
355
  lex->is_lex_started= true;
278
 
  lex->statement= NULL;
279
 
  
280
 
  lex->is_cross= false;
281
 
  lex->reset();
 
356
  return;
282
357
}
283
358
 
284
 
void LEX::end()
 
359
void lex_end(LEX *lex)
285
360
{
286
 
  if (yacc_yyss)
 
361
  if (lex->yacc_yyss)
287
362
  {
288
 
    free(yacc_yyss);
289
 
    free(yacc_yyvs);
290
 
    yacc_yyss= 0;
291
 
    yacc_yyvs= 0;
 
363
    my_free(lex->yacc_yyss, MYF(0));
 
364
    my_free(lex->yacc_yyvs, MYF(0));
 
365
    lex->yacc_yyss= 0;
 
366
    lex->yacc_yyvs= 0;
292
367
  }
293
368
 
294
 
  safe_delete(result);
295
 
  safe_delete(_create_table);
296
 
  _create_table= NULL;
297
 
  _create_field= NULL;
298
 
 
299
 
  result= 0;
300
 
  setCacheable(true);
301
 
 
302
 
  safe_delete(statement);
 
369
  /* release used plugins */
 
370
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
 
371
                     lex->plugins.elements);
 
372
  reset_dynamic(&lex->plugins);
 
373
 
 
374
  return;
303
375
}
304
376
 
305
 
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
 
377
 
 
378
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
306
379
{
307
 
  /* Plenty of memory for the largest lex symbol we have */
308
 
  char tok_upper[64];
309
380
  const char *tok= lip->get_tok_start();
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;
314
381
 
315
 
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
 
382
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
316
383
  if (symbol)
317
384
  {
318
385
    lip->yylval->symbol.symbol=symbol;
321
388
 
322
389
    return symbol->tok;
323
390
  }
324
 
 
325
391
  return 0;
326
392
}
327
393
 
 
394
/*
 
395
  Check if name is a keyword
 
396
 
 
397
  SYNOPSIS
 
398
    is_keyword()
 
399
    name      checked name (must not be empty)
 
400
    len       length of checked name
 
401
 
 
402
  RETURN VALUES
 
403
    0         name is a keyword
 
404
    1         name isn't a keyword
 
405
*/
 
406
 
 
407
bool is_keyword(const char *name, uint len)
 
408
{
 
409
  assert(len != 0);
 
410
  return get_hash_symbol(name,len,0)!=0;
 
411
}
 
412
 
328
413
bool is_lex_native_function(const LEX_STRING *name)
329
414
{
330
415
  assert(name != NULL);
331
 
  return (lookup_symbol(name->str, name->length, 1) != 0);
 
416
  return (get_hash_symbol(name->str, name->length, 1) != 0);
332
417
}
333
418
 
334
419
/* make a copy of token before ptr and set yytoklen */
335
 
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
 
420
 
 
421
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
336
422
{
337
423
  LEX_STRING tmp;
338
424
  lip->yyUnget();                       // ptr points now after last token char
339
425
  tmp.length=lip->yytoklen=length;
340
 
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
 
426
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
341
427
 
342
428
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
343
429
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
345
431
  return tmp;
346
432
}
347
433
 
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
 
434
/* 
 
435
 todo: 
 
436
   There are no dangerous charsets in mysql for function 
 
437
   get_quoted_token yet. But it should be fixed in the 
352
438
   future to operate multichar strings (like ucs2)
353
439
*/
 
440
 
354
441
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
355
 
                                   uint32_t skip,
356
 
                                   uint32_t length, char quote)
 
442
                                   uint skip,
 
443
                                   uint length, char quote)
357
444
{
358
445
  LEX_STRING tmp;
359
446
  const char *from, *end;
360
447
  char *to;
361
448
  lip->yyUnget();                       // ptr points now after last token char
362
449
  tmp.length= lip->yytoklen=length;
363
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
450
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
364
451
  from= lip->get_tok_start() + skip;
365
452
  to= tmp.str;
366
453
  end= to+length;
385
472
  Return an unescaped text literal without quotes
386
473
  Fix sometimes to do only one scan of the string
387
474
*/
 
475
 
388
476
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
389
477
{
390
 
  unsigned char c,sep;
391
 
  bool found_escape= false;
392
 
  const CHARSET_INFO * const cs= lip->m_session->charset();
 
478
  register uchar c,sep;
 
479
  uint found_escape=0;
 
480
  const CHARSET_INFO * const cs= lip->m_thd->charset();
393
481
 
394
482
  lip->tok_bitmap= 0;
395
483
  sep= lip->yyGetLast();                        // String should end with this
397
485
  {
398
486
    c= lip->yyGet();
399
487
    lip->tok_bitmap|= c;
 
488
#ifdef USE_MB
400
489
    {
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
 
        }
 
490
      int l;
 
491
      if (use_mb(cs) &&
 
492
          (l = my_ismbchar(cs,
 
493
                           lip->get_ptr() -1,
 
494
                           lip->get_end_of_query()))) {
 
495
        lip->skip_binary(l-1);
 
496
        continue;
409
497
      }
410
498
    }
 
499
#endif
411
500
    if (c == '\\')
412
501
    {                                   // Escaped character
413
 
      found_escape= true;
 
502
      found_escape=1;
414
503
      if (lip->eof())
415
 
        return 0;
 
504
        return 0;
416
505
      lip->yySkip();
417
506
    }
418
507
    else if (c == sep)
419
508
    {
420
509
      if (c == lip->yyGet())            // Check if two separators in a row
421
510
      {
422
 
        found_escape= true;                 // duplicate. Remember for delete
423
 
        continue;
 
511
        found_escape=1;                 // duplicate. Remember for delete
 
512
        continue;
424
513
      }
425
514
      else
426
515
        lip->yyUnget();
432
521
      str= lip->get_tok_start();
433
522
      end= lip->get_ptr();
434
523
      /* Extract the text from the token */
435
 
      str+= pre_skip;
436
 
      end-= post_skip;
 
524
      str += pre_skip;
 
525
      end -= post_skip;
437
526
      assert(end >= str);
438
527
 
439
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
440
 
        return (char*) "";              // memory::SqlAlloc has set error flag
 
528
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
529
        return (char*) "";              // Sql_alloc has set error flag
441
530
 
442
531
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
443
532
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
444
533
 
445
 
      if (! found_escape)
 
534
      if (!found_escape)
446
535
      {
447
 
        lip->yytoklen= (uint32_t) (end-str);
448
 
        memcpy(start, str, lip->yytoklen);
449
 
        start[lip->yytoklen]= 0;
 
536
        lip->yytoklen=(uint) (end-str);
 
537
        memcpy(start,str,lip->yytoklen);
 
538
        start[lip->yytoklen]=0;
450
539
      }
451
540
      else
452
541
      {
453
542
        char *to;
454
543
 
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:
 
544
        for (to=start ; str != end ; str++)
 
545
        {
 
546
#ifdef USE_MB
 
547
          int l;
 
548
          if (use_mb(cs) &&
 
549
              (l = my_ismbchar(cs, str, end))) {
 
550
              while (l--)
 
551
                  *to++ = *str++;
 
552
              str--;
 
553
              continue;
 
554
          }
 
555
#endif
 
556
          if (*str == '\\' && str+1 != end)
 
557
          {
 
558
            switch(*++str) {
 
559
            case 'n':
 
560
              *to++='\n';
 
561
              break;
 
562
            case 't':
 
563
              *to++= '\t';
 
564
              break;
 
565
            case 'r':
 
566
              *to++ = '\r';
 
567
              break;
 
568
            case 'b':
 
569
              *to++ = '\b';
 
570
              break;
 
571
            case '0':
 
572
              *to++= 0;                 // Ascii null
 
573
              break;
 
574
            case 'Z':                   // ^Z must be escaped on Win32
 
575
              *to++='\032';
 
576
              break;
 
577
            case '_':
 
578
            case '%':
 
579
              *to++= '\\';              // remember prefix for wildcard
 
580
              /* Fall through */
 
581
            default:
494
582
              *to++= *str;
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);
 
583
              break;
 
584
            }
 
585
          }
 
586
          else if (*str == sep)
 
587
            *to++= *str++;              // Two ' or "
 
588
          else
 
589
            *to++ = *str;
 
590
        }
 
591
        *to=0;
 
592
        lip->yytoklen=(uint) (to-start);
505
593
      }
506
594
      return start;
507
595
    }
513
601
/*
514
602
** Calc type of integer; long integer, int64_t integer or real.
515
603
** Returns smallest type that match the string.
516
 
** When using uint64_t values the result is converted to a real
 
604
** When using unsigned long long values the result is converted to a real
517
605
** because else they will be unexpected sign changes because all calculation
518
606
** is done with int64_t or double.
519
607
*/
520
608
 
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;
 
609
static const char *long_str="2147483647";
 
610
static const uint long_len=10;
 
611
static const char *signed_long_str="-2147483648";
 
612
static const char *int64_t_str="9223372036854775807";
 
613
static const uint int64_t_len=19;
 
614
static const char *signed_int64_t_str="-9223372036854775808";
 
615
static const uint signed_int64_t_len=19;
 
616
static const char *unsigned_int64_t_str="18446744073709551615";
 
617
static const uint unsigned_int64_t_len=20;
530
618
 
531
 
static inline uint32_t int_token(const char *str,uint32_t length)
 
619
static inline uint int_token(const char *str,uint length)
532
620
{
533
621
  if (length < long_len)                        // quick normal case
534
622
    return NUM;
550
638
  if (length < long_len)
551
639
    return NUM;
552
640
 
553
 
  uint32_t smaller,bigger;
 
641
  uint smaller,bigger;
554
642
  const char *cmp;
555
643
  if (neg)
556
644
  {
597
685
    }
598
686
  }
599
687
  while (*cmp && *cmp++ == *str++) ;
600
 
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
 
688
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
601
689
}
602
690
 
603
 
} /* namespace drizzled */
 
691
 
604
692
/*
605
 
  base_sql_lex remember the following states from the following sql_baselex()
 
693
  MYSQLlex remember the following states from the following MYSQLlex()
606
694
 
607
695
  - MY_LEX_EOQ                  Found end of query
608
696
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
609
697
                                (which can't be followed by a signed number)
610
698
*/
611
 
int base_sql_lex(union ParserType *yylval, drizzled::Session *session)
 
699
 
 
700
int MYSQLlex(void *arg, void *yythd)
612
701
{
613
 
  drizzled::Lex_input_stream *lip= session->m_lip;
 
702
  THD *thd= (THD *)yythd;
 
703
  Lex_input_stream *lip= thd->m_lip;
 
704
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
705
  int token;
615
706
 
616
707
  if (lip->lookahead_token != END_OF_INPUT)
626
717
    return token;
627
718
  }
628
719
 
629
 
  token= drizzled::lex_one_token(yylval, session);
 
720
  token= lex_one_token(arg, yythd);
630
721
 
631
722
  switch(token) {
632
723
  case WITH:
633
724
    /*
634
 
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
 
725
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
635
726
      which makes the grammar LALR(2).
636
727
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
637
728
      to transform the grammar into a LALR(1) grammar,
638
729
      which sql_yacc.yy can process.
639
730
    */
640
 
    token= drizzled::lex_one_token(yylval, session);
641
 
    if (token == ROLLUP_SYM)
642
 
    {
 
731
    token= lex_one_token(arg, yythd);
 
732
    switch(token) {
 
733
    case CUBE_SYM:
 
734
      return WITH_CUBE_SYM;
 
735
    case ROLLUP_SYM:
643
736
      return WITH_ROLLUP_SYM;
644
 
    }
645
 
    else
646
 
    {
 
737
    default:
647
738
      /*
648
739
        Save the token following 'WITH'
649
740
      */
652
743
      lip->lookahead_token= token;
653
744
      return WITH;
654
745
    }
 
746
    break;
655
747
  default:
656
748
    break;
657
749
  }
659
751
  return token;
660
752
}
661
753
 
662
 
namespace drizzled
663
 
{
664
 
 
665
 
int lex_one_token(ParserType *yylval, drizzled::Session *session)
666
 
{
667
 
  unsigned char c= 0; /* Just set to shutup GCC */
 
754
int lex_one_token(void *arg, void *yythd)
 
755
{
 
756
  register unsigned char c= 0; /* Just set to shutup GCC */
668
757
  bool comment_closed;
669
758
  int   tokval, result_state;
670
759
  unsigned int length;
671
760
  enum my_lex_states state;
672
 
  Lex_input_stream *lip= session->m_lip;
673
 
  LEX *lex= session->getLex();
674
 
  const CHARSET_INFO * const cs= session->charset();
675
 
  unsigned char *state_map= cs->state_map;
676
 
  unsigned char *ident_map= cs->ident_map;
 
761
  THD *thd= (THD *)yythd;
 
762
  Lex_input_stream *lip= thd->m_lip;
 
763
  LEX *lex= thd->lex;
 
764
  YYSTYPE *yylval=(YYSTYPE*) arg;
 
765
  const CHARSET_INFO * const cs= thd->charset();
 
766
  uchar *state_map= cs->state_map;
 
767
  uchar *ident_map= cs->ident_map;
677
768
 
678
769
  lip->yylval=yylval;                   // The global state
679
770
 
688
779
      // Skip starting whitespace
689
780
      while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
690
781
      {
691
 
        if (c == '\n')
692
 
          lip->yylineno++;
 
782
        if (c == '\n')
 
783
          lip->yylineno++;
693
784
 
694
785
        lip->yySkip();
695
786
      }
702
793
    case MY_LEX_ESCAPE:
703
794
      if (lip->yyGet() == 'N')
704
795
      {                                 // Allow \N as shortcut for NULL
705
 
        yylval->lex_str.str=(char*) "\\N";
706
 
        yylval->lex_str.length=2;
707
 
        return NULL_SYM;
 
796
        yylval->lex_str.str=(char*) "\\N";
 
797
        yylval->lex_str.length=2;
 
798
        return NULL_SYM;
708
799
      }
709
800
    case MY_LEX_CHAR:                   // Unknown or single char token
710
801
    case MY_LEX_SKIP:                   // This should not happen
717
808
      }
718
809
 
719
810
      if (c != ')')
720
 
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
811
        lip->next_state= MY_LEX_START;  // Allow signed numbers
721
812
 
722
813
      if (c == ',')
723
814
      {
738
829
    case MY_LEX_IDENT_OR_HEX:
739
830
      if (lip->yyPeek() == '\'')
740
831
      {                                 // Found x'hex-number'
741
 
        state= MY_LEX_HEX_NUMBER;
742
 
        break;
 
832
        state= MY_LEX_HEX_NUMBER;
 
833
        break;
743
834
      }
744
835
    case MY_LEX_IDENT_OR_BIN:
745
836
      if (lip->yyPeek() == '\'')
749
840
      }
750
841
    case MY_LEX_IDENT:
751
842
      const char *start;
 
843
#if defined(USE_MB) && defined(USE_MB_IDENT)
752
844
      if (use_mb(cs))
753
845
      {
754
 
        result_state= IDENT_QUOTED;
 
846
        result_state= IDENT_QUOTED;
755
847
        if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
756
848
        {
757
849
          int l = my_ismbchar(cs,
767
859
        {
768
860
          if (my_mbcharlen(cs, c) > 1)
769
861
          {
770
 
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
771
 
            if (l == 0)
 
862
            int l;
 
863
            if ((l = my_ismbchar(cs,
 
864
                                 lip->get_ptr() -1,
 
865
                                 lip->get_end_of_query())) == 0)
772
866
              break;
773
867
            lip->skip_binary(l-1);
774
868
          }
775
869
        }
776
870
      }
777
871
      else
 
872
#endif
778
873
      {
779
874
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
780
875
        /* If there were non-ASCII characters, mark that we must convert */
791
886
        for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
792
887
      }
793
888
      if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
794
 
              lip->next_state=MY_LEX_IDENT_SEP;
 
889
        lip->next_state=MY_LEX_IDENT_SEP;
795
890
      else
796
891
      {                                 // '(' must follow directly if function
797
892
        lip->yyUnget();
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
 
        }
 
893
        if ((tokval = find_keyword(lip, length, c == '(')))
 
894
        {
 
895
          lip->next_state= MY_LEX_START;        // Allow signed numbers
 
896
          return(tokval);               // Was keyword
 
897
        }
803
898
        lip->yySkip();                  // next state does a unget
804
899
      }
805
900
      yylval->lex_str=get_token(lip, 0, length);
806
901
 
 
902
      /*
 
903
         Note: "SELECT _bla AS 'alias'"
 
904
         _bla should be considered as a IDENT if charset haven't been found.
 
905
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
 
906
         producing an error.
 
907
      */
 
908
 
 
909
      if (yylval->lex_str.str[0] == '_')
 
910
      {
 
911
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
912
                                                             MY_CS_PRIMARY, MYF(0));
 
913
        if (cs)
 
914
        {
 
915
          yylval->charset= cs;
 
916
          lip->m_underscore_cs= cs;
 
917
 
 
918
          lip->body_utf8_append(lip->m_cpp_text_start,
 
919
                                lip->get_cpp_tok_start() + length);
 
920
          return(UNDERSCORE_CHARSET);
 
921
        }
 
922
      }
 
923
 
807
924
      lip->body_utf8_append(lip->m_cpp_text_start);
808
925
 
809
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
926
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
927
                                    lip->m_cpp_text_end);
810
928
 
811
929
      return(result_state);                     // IDENT or IDENT_QUOTED
812
930
 
816
934
      c= lip->yyGet();                  // should be '.'
817
935
      lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
818
936
      if (!ident_map[(uint8_t)lip->yyPeek()])            // Probably ` or "
819
 
        lip->next_state= MY_LEX_START;
 
937
        lip->next_state= MY_LEX_START;
820
938
      return((int) c);
821
939
 
822
940
    case MY_LEX_NUMBER_IDENT:           // number or ident which num-start
855
973
      while (my_isdigit(cs, (c = lip->yyGet()))) ;
856
974
      if (!ident_map[c])
857
975
      {                                 // Can't be identifier
858
 
        state=MY_LEX_INT_OR_REAL;
859
 
        break;
 
976
        state=MY_LEX_INT_OR_REAL;
 
977
        break;
860
978
      }
861
979
      if (c == 'e' || c == 'E')
862
980
      {
863
 
        // The following test is written this way to allow numbers of type 1e1
 
981
        // The following test is written this way to allow numbers of type 1e1
864
982
        if (my_isdigit(cs,lip->yyPeek()) ||
865
983
            (c=(lip->yyGet())) == '+' || c == '-')
866
 
        {                               // Allow 1E+10
 
984
        {                               // Allow 1E+10
867
985
          if (my_isdigit(cs,lip->yyPeek()))     // Number must have digit after sign
868
 
          {
 
986
          {
869
987
            lip->yySkip();
870
988
            while (my_isdigit(cs,lip->yyGet())) ;
871
989
            yylval->lex_str=get_token(lip, 0, lip->yyLength());
872
 
            return(FLOAT_NUM);
873
 
          }
874
 
        }
 
990
            return(FLOAT_NUM);
 
991
          }
 
992
        }
875
993
        lip->yyUnget();
876
994
      }
877
995
      // fall through
878
996
    case MY_LEX_IDENT_START:                    // We come here after '.'
879
997
      result_state= IDENT;
 
998
#if defined(USE_MB) && defined(USE_MB_IDENT)
880
999
      if (use_mb(cs))
881
1000
      {
882
 
        result_state= IDENT_QUOTED;
 
1001
        result_state= IDENT_QUOTED;
883
1002
        while (ident_map[c=lip->yyGet()])
884
1003
        {
885
1004
          if (my_mbcharlen(cs, c) > 1)
886
1005
          {
887
 
            int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
888
 
            if (l == 0)
 
1006
            int l;
 
1007
            if ((l = my_ismbchar(cs,
 
1008
                                 lip->get_ptr() -1,
 
1009
                                 lip->get_end_of_query())) == 0)
889
1010
              break;
890
1011
            lip->skip_binary(l-1);
891
1012
          }
892
1013
        }
893
1014
      }
894
1015
      else
 
1016
#endif
895
1017
      {
896
1018
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
897
1019
        /* If there were non-ASCII characters, mark that we must convert */
898
1020
        result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
899
1021
      }
900
1022
      if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
901
 
        lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
 
1023
        lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
902
1024
 
903
1025
      yylval->lex_str= get_token(lip, 0, lip->yyLength());
904
1026
 
905
1027
      lip->body_utf8_append(lip->m_cpp_text_start);
906
1028
 
907
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1029
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1030
                                    lip->m_cpp_text_end);
908
1031
 
909
1032
      return(result_state);
910
1033
 
911
1034
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
912
1035
    {
913
 
      uint32_t double_quotes= 0;
 
1036
      uint double_quotes= 0;
914
1037
      char quote_char= c;                       // Used char
915
1038
      while ((c=lip->yyGet()))
916
1039
      {
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
 
1040
        int var_length;
 
1041
        if ((var_length= my_mbcharlen(cs, c)) == 1)
 
1042
        {
 
1043
          if (c == quote_char)
 
1044
          {
 
1045
            if (lip->yyPeek() != quote_char)
 
1046
              break;
 
1047
            c=lip->yyGet();
 
1048
            double_quotes++;
 
1049
            continue;
 
1050
          }
 
1051
        }
 
1052
#ifdef USE_MB
 
1053
        else if (var_length < 1)
 
1054
          break;                                // Error
931
1055
        lip->skip_binary(var_length-1);
 
1056
#endif
932
1057
      }
933
1058
      if (double_quotes)
934
 
              yylval->lex_str=get_quoted_token(lip, 1, lip->yyLength() - double_quotes -1, quote_char);
 
1059
        yylval->lex_str=get_quoted_token(lip, 1,
 
1060
                                         lip->yyLength() - double_quotes -1,
 
1061
                                         quote_char);
935
1062
      else
936
1063
        yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
937
1064
      if (c == quote_char)
938
1065
        lip->yySkip();                  // Skip end `
939
1066
      lip->next_state= MY_LEX_START;
 
1067
 
940
1068
      lip->body_utf8_append(lip->m_cpp_text_start);
941
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1069
 
 
1070
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1071
                                    lip->m_cpp_text_end);
 
1072
 
942
1073
      return(IDENT_QUOTED);
943
1074
    }
944
1075
    case MY_LEX_INT_OR_REAL:            // Complete int or incomplete real
945
1076
      if (c != '.')
946
1077
      {                                 // Found complete integer number.
947
1078
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
948
 
        return int_token(yylval->lex_str.str,yylval->lex_str.length);
 
1079
        return int_token(yylval->lex_str.str,yylval->lex_str.length);
949
1080
      }
950
1081
      // fall through
951
1082
    case MY_LEX_REAL:                   // Incomplete real number
954
1085
      if (c == 'e' || c == 'E')
955
1086
      {
956
1087
        c = lip->yyGet();
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
 
        }
 
1088
        if (c == '-' || c == '+')
 
1089
          c = lip->yyGet();                     // Skip sign
 
1090
        if (!my_isdigit(cs,c))
 
1091
        {                               // No digit after sign
 
1092
          state= MY_LEX_CHAR;
 
1093
          break;
 
1094
        }
964
1095
        while (my_isdigit(cs,lip->yyGet())) ;
965
1096
        yylval->lex_str=get_token(lip, 0, lip->yyLength());
966
 
        return(FLOAT_NUM);
 
1097
        return(FLOAT_NUM);
967
1098
      }
968
1099
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
969
1100
      return(DECIMAL_NUM);
1000
1131
        lip->yySkip();
1001
1132
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1002
1133
      {
1003
 
        lip->next_state= MY_LEX_START;  // Allow signed numbers
1004
 
        return(tokval);
 
1134
        lip->next_state= MY_LEX_START;  // Allow signed numbers
 
1135
        return(tokval);
1005
1136
      }
1006
1137
      state = MY_LEX_CHAR;              // Something fishy found
1007
1138
      break;
1016
1147
      }
1017
1148
      if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1018
1149
      {
1019
 
        lip->next_state= MY_LEX_START;  // Found long op
1020
 
        return(tokval);
 
1150
        lip->next_state= MY_LEX_START;  // Found long op
 
1151
        return(tokval);
1021
1152
      }
1022
1153
      state = MY_LEX_CHAR;              // Something fishy found
1023
1154
      break;
1025
1156
    case MY_LEX_BOOL:
1026
1157
      if (c != lip->yyPeek())
1027
1158
      {
1028
 
        state=MY_LEX_CHAR;
1029
 
        break;
 
1159
        state=MY_LEX_CHAR;
 
1160
        break;
1030
1161
      }
1031
1162
      lip->yySkip();
1032
1163
      tokval = find_keyword(lip,2,0);   // Is a bool operator
1043
1174
    case MY_LEX_STRING:                 // Incomplete text string
1044
1175
      if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
1045
1176
      {
1046
 
        state= MY_LEX_CHAR;             // Read char by char
1047
 
        break;
 
1177
        state= MY_LEX_CHAR;             // Read char by char
 
1178
        break;
1048
1179
      }
1049
1180
      yylval->lex_str.length=lip->yytoklen;
1050
1181
 
1051
1182
      lip->body_utf8_append(lip->m_cpp_text_start);
1052
1183
 
1053
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1184
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1185
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
 
1186
        lip->m_cpp_text_end);
 
1187
 
 
1188
      lip->m_underscore_cs= NULL;
1054
1189
 
1055
1190
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1056
1191
      return(TEXT_STRING);
1064
1199
    case MY_LEX_LONG_COMMENT:           /* Long C comment? */
1065
1200
      if (lip->yyPeek() != '*')
1066
1201
      {
1067
 
        state=MY_LEX_CHAR;              // Probable division
1068
 
        break;
 
1202
        state=MY_LEX_CHAR;              // Probable division
 
1203
        break;
1069
1204
      }
1070
1205
      lex->select_lex.options|= OPTION_FOUND_COMMENT;
1071
1206
      /* Reject '/' '*', since we might need to turn off the echo */
1082
1217
 
1083
1218
        /*
1084
1219
          The special comment format is very strict:
1085
 
          '/' '*' '!', followed by digits ended by a non-digit.
1086
 
          There must be at least 5 digits for it to count
 
1220
          '/' '*' '!', followed by exactly
 
1221
          1 digit (major), 2 digits (minor), then 2 digits (dot).
 
1222
          32302 -> 3.23.02
 
1223
          50032 -> 5.0.32
 
1224
          50114 -> 5.1.14
1087
1225
        */
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);
 
1226
        char version_str[6];
 
1227
        version_str[0]= lip->yyPeekn(0);
 
1228
        version_str[1]= lip->yyPeekn(1);
 
1229
        version_str[2]= lip->yyPeekn(2);
 
1230
        version_str[3]= lip->yyPeekn(3);
 
1231
        version_str[4]= lip->yyPeekn(4);
 
1232
        version_str[5]= 0;
 
1233
        if (  my_isdigit(cs, version_str[0])
 
1234
           && my_isdigit(cs, version_str[1])
 
1235
           && my_isdigit(cs, version_str[2])
 
1236
           && my_isdigit(cs, version_str[3])
 
1237
           && my_isdigit(cs, version_str[4])
 
1238
           )
 
1239
        {
 
1240
          ulong version;
 
1241
          version=strtol(version_str, NULL, 10);
1104
1242
 
1105
1243
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1106
 
          lip->yySkipn(pos-1);
 
1244
          lip->yySkipn(5);
1107
1245
 
1108
1246
          if (version <= DRIZZLE_VERSION_ID)
1109
1247
          {
1172
1310
        state=MY_LEX_START;
1173
1311
      }
1174
1312
      else
1175
 
        state=MY_LEX_CHAR;              // Return '*'
 
1313
        state=MY_LEX_CHAR;              // Return '*'
1176
1314
      break;
1177
1315
    case MY_LEX_SET_VAR:                // Check if ':='
1178
1316
      if (lip->yyPeek() != '=')
1179
1317
      {
1180
 
        state=MY_LEX_CHAR;              // Return ':'
1181
 
        break;
 
1318
        state=MY_LEX_CHAR;              // Return ':'
 
1319
        break;
1182
1320
      }
1183
1321
      lip->yySkip();
1184
1322
      return (SET_VAR);
1185
1323
    case MY_LEX_SEMICOLON:                      // optional line terminator
1186
1324
      if (lip->yyPeek())
1187
1325
      {
 
1326
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1327
        {
 
1328
          lip->found_semicolon= lip->get_ptr();
 
1329
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1330
          lip->next_state= MY_LEX_END;
 
1331
          lip->set_echo(true);
 
1332
          return (END_OF_INPUT);
 
1333
        }
1188
1334
        state= MY_LEX_CHAR;             // Return ';'
1189
 
        break;
 
1335
        break;
1190
1336
      }
1191
1337
      lip->next_state=MY_LEX_END;       // Mark for next loop
1192
1338
      return(END_OF_INPUT);
1207
1353
      break;
1208
1354
    case MY_LEX_END:
1209
1355
      lip->next_state=MY_LEX_END;
1210
 
      return false;                     // We found end of input last time
1211
 
 
 
1356
      return(0);                        // We found end of input last time
 
1357
      
1212
1358
      /* Actually real shouldn't start with . but allow them anyhow */
1213
1359
    case MY_LEX_REAL_OR_POINT:
1214
1360
      if (my_isdigit(cs,lip->yyPeek()))
1215
 
        state= MY_LEX_REAL;             // Real
 
1361
        state = MY_LEX_REAL;            // Real
1216
1362
      else
1217
1363
      {
1218
 
        state= MY_LEX_IDENT_SEP;        // return '.'
 
1364
        state= MY_LEX_IDENT_SEP;        // return '.'
1219
1365
        lip->yyUnget();                 // Put back '.'
1220
1366
      }
1221
1367
      break;
1224
1370
      case MY_LEX_STRING:
1225
1371
      case MY_LEX_USER_VARIABLE_DELIMITER:
1226
1372
      case MY_LEX_STRING_OR_DELIMITER:
1227
 
        break;
 
1373
        break;
1228
1374
      case MY_LEX_USER_END:
1229
 
        lip->next_state=MY_LEX_SYSTEM_VAR;
1230
 
        break;
 
1375
        lip->next_state=MY_LEX_SYSTEM_VAR;
 
1376
        break;
1231
1377
      default:
1232
 
        lip->next_state=MY_LEX_HOSTNAME;
1233
 
        break;
 
1378
        lip->next_state=MY_LEX_HOSTNAME;
 
1379
        break;
1234
1380
      }
1235
1381
      yylval->lex_str.str=(char*) lip->get_ptr();
1236
1382
      yylval->lex_str.length=1;
1237
1383
      return((int) '@');
1238
1384
    case MY_LEX_HOSTNAME:               // end '@' of user@hostname
1239
1385
      for (c=lip->yyGet() ;
1240
 
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
 
1386
           my_isalnum(cs,c) || c == '.' || c == '_' ||  c == '$';
1241
1387
           c= lip->yyGet()) ;
1242
1388
      yylval->lex_str=get_token(lip, 0, lip->yyLength());
1243
1389
      return(LEX_HOSTNAME);
1252
1398
      return((int) '@');
1253
1399
    case MY_LEX_IDENT_OR_KEYWORD:
1254
1400
      /*
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
 
1401
        We come here when we have found two '@' in a row.
 
1402
        We should now be able to handle:
 
1403
        [(global | local | session) .]variable_name
1258
1404
      */
1259
1405
 
1260
1406
      for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1262
1408
      result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1263
1409
 
1264
1410
      if (c == '.')
1265
 
        lip->next_state=MY_LEX_IDENT_SEP;
 
1411
        lip->next_state=MY_LEX_IDENT_SEP;
1266
1412
      length= lip->yyLength();
1267
1413
      if (length == 0)
1268
1414
        return(ABORT_SYM);              // Names must be nonempty.
1269
1415
      if ((tokval= find_keyword(lip, length,0)))
1270
1416
      {
1271
1417
        lip->yyUnget();                         // Put back 'c'
1272
 
        return(tokval);                         // Was keyword
 
1418
        return(tokval);                         // Was keyword
1273
1419
      }
1274
1420
      yylval->lex_str=get_token(lip, 0, length);
1275
1421
 
1276
1422
      lip->body_utf8_append(lip->m_cpp_text_start);
1277
1423
 
1278
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1424
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1425
                                    lip->m_cpp_text_end);
1279
1426
 
1280
1427
      return(result_state);
1281
1428
    }
1282
1429
  }
1283
1430
}
1284
1431
 
 
1432
 
 
1433
/**
 
1434
  Construct a copy of this object to be used for mysql_alter_table
 
1435
  and mysql_create_table.
 
1436
 
 
1437
  Historically, these two functions modify their Alter_info
 
1438
  arguments. This behaviour breaks re-execution of prepared
 
1439
  statements and stored procedures and is compensated by always
 
1440
  supplying a copy of Alter_info to these functions.
 
1441
 
 
1442
  @return You need to use check the error in THD for out
 
1443
  of memory condition after calling this function.
 
1444
*/
 
1445
 
 
1446
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
 
1447
  :drop_list(rhs.drop_list, mem_root),
 
1448
  alter_list(rhs.alter_list, mem_root),
 
1449
  key_list(rhs.key_list, mem_root),
 
1450
  create_list(rhs.create_list, mem_root),
 
1451
  flags(rhs.flags),
 
1452
  keys_onoff(rhs.keys_onoff),
 
1453
  tablespace_op(rhs.tablespace_op),
 
1454
  no_parts(rhs.no_parts),
 
1455
  build_method(rhs.build_method),
 
1456
  datetime_field(rhs.datetime_field),
 
1457
  error_if_not_empty(rhs.error_if_not_empty)
 
1458
{
 
1459
  /*
 
1460
    Make deep copies of used objects.
 
1461
    This is not a fully deep copy - clone() implementations
 
1462
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
 
1463
    do not copy string constants. At the same length the only
 
1464
    reason we make a copy currently is that ALTER/CREATE TABLE
 
1465
    code changes input Alter_info definitions, but string
 
1466
    constants never change.
 
1467
  */
 
1468
  list_copy_and_replace_each_value(drop_list, mem_root);
 
1469
  list_copy_and_replace_each_value(alter_list, mem_root);
 
1470
  list_copy_and_replace_each_value(key_list, mem_root);
 
1471
  list_copy_and_replace_each_value(create_list, mem_root);
 
1472
}
 
1473
 
 
1474
 
1285
1475
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1286
1476
{
1287
1477
  /*
1289
1479
    This code assumes that there are no multi-bytes characters
1290
1480
    that can be considered white-space.
1291
1481
  */
 
1482
 
1292
1483
  while ((str->length > 0) && (my_isspace(cs, str->str[0])))
1293
1484
  {
1294
 
    str->length--;
1295
 
    str->str++;
 
1485
    str->length --;
 
1486
    str->str ++;
1296
1487
  }
1297
1488
 
1298
1489
  /*
1301
1492
  */
1302
1493
  while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
1303
1494
  {
1304
 
    str->length--;
 
1495
    str->length --;
1305
1496
  }
1306
1497
}
1307
1498
 
 
1499
 
1308
1500
/*
1309
 
  Select_Lex structures initialisations
 
1501
  st_select_lex structures initialisations
1310
1502
*/
1311
 
void Select_Lex_Node::init_query()
 
1503
 
 
1504
void st_select_lex_node::init_query()
1312
1505
{
1313
1506
  options= 0;
1314
1507
  linkage= UNSPECIFIED_TYPE;
1315
1508
  no_error= no_table_names_allowed= 0;
1316
 
  uncacheable.reset();
1317
 
}
1318
 
 
1319
 
void Select_Lex_Node::init_select()
1320
 
{
1321
 
}
1322
 
 
1323
 
void Select_Lex_Unit::init_query()
1324
 
{
1325
 
  Select_Lex_Node::init_query();
 
1509
  uncacheable= 0;
 
1510
}
 
1511
 
 
1512
void st_select_lex_node::init_select()
 
1513
{
 
1514
}
 
1515
 
 
1516
void st_select_lex_unit::init_query()
 
1517
{
 
1518
  st_select_lex_node::init_query();
1326
1519
  linkage= GLOBAL_OPTIONS_TYPE;
1327
1520
  global_parameters= first_select();
1328
1521
  select_limit_cnt= HA_POS_ERROR;
1334
1527
  table= 0;
1335
1528
  fake_select_lex= 0;
1336
1529
  cleaned= 0;
1337
 
  item_list.clear();
 
1530
  item_list.empty();
1338
1531
  describe= 0;
1339
1532
  found_rows_for_union= 0;
1340
1533
}
1341
1534
 
1342
 
void Select_Lex::init_query()
 
1535
void st_select_lex::init_query()
1343
1536
{
1344
 
  Select_Lex_Node::init_query();
1345
 
  table_list.clear();
1346
 
  top_join_list.clear();
 
1537
  st_select_lex_node::init_query();
 
1538
  table_list.empty();
 
1539
  top_join_list.empty();
1347
1540
  join_list= &top_join_list;
1348
1541
  embedding= leaf_tables= 0;
1349
 
  item_list.clear();
 
1542
  item_list.empty();
1350
1543
  join= 0;
1351
1544
  having= where= 0;
1352
1545
  olap= UNSPECIFIED_OLAP_TYPE;
1365
1558
  parent_lex->push_context(&context);
1366
1559
  cond_count= between_count= with_wild= 0;
1367
1560
  max_equal_elems= 0;
 
1561
  conds_processed_with_permanent_arena= 0;
1368
1562
  ref_pointer_array= 0;
1369
1563
  select_n_where_fields= 0;
1370
1564
  select_n_having_items= 0;
1371
1565
  subquery_in_having= explicit_limit= 0;
1372
1566
  is_item_list_lookup= 0;
 
1567
  first_execution= 1;
 
1568
  first_cond_optimization= 1;
1373
1569
  parsing_place= NO_MATTER;
1374
1570
  exclude_from_table_unique_test= false;
1375
1571
  nest_level= 0;
1376
1572
  link_next= 0;
1377
1573
}
1378
1574
 
1379
 
void Select_Lex::init_select()
 
1575
void st_select_lex::init_select()
1380
1576
{
1381
 
  sj_nests.clear();
1382
 
  group_list.clear();
1383
 
  db= 0;
 
1577
  st_select_lex_node::init_select();
 
1578
  sj_nests.empty();
 
1579
  group_list.empty();
 
1580
  type= db= 0;
1384
1581
  having= 0;
 
1582
  table_join_options= 0;
1385
1583
  in_sum_expr= with_wild= 0;
1386
1584
  options= 0;
1387
1585
  braces= 0;
1388
 
  interval_list.clear();
 
1586
  interval_list.empty();
 
1587
  ftfunc_list_alloc.empty();
1389
1588
  inner_sum_func_list= 0;
 
1589
  ftfunc_list= &ftfunc_list_alloc;
1390
1590
  linkage= UNSPECIFIED_TYPE;
1391
1591
  order_list.elements= 0;
1392
1592
  order_list.first= 0;
1393
 
  order_list.next= (unsigned char**) &order_list.first;
 
1593
  order_list.next= (uchar**) &order_list.first;
1394
1594
  /* Set limit and offset to default values */
1395
1595
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1396
1596
  offset_limit= 0;      /* denotes the default offset = 0 */
1397
1597
  with_sum_func= 0;
1398
 
  is_cross= false;
1399
1598
  is_correlated= 0;
1400
1599
  cur_pos_in_select_list= UNDEF_POS;
1401
 
  non_agg_fields.clear();
 
1600
  non_agg_fields.empty();
1402
1601
  cond_value= having_value= Item::COND_UNDEF;
1403
 
  inner_refs_list.clear();
1404
 
  full_group_by_flag.reset();
 
1602
  inner_refs_list.empty();
 
1603
  full_group_by_flag= 0;
1405
1604
}
1406
1605
 
1407
1606
/*
1408
 
  Select_Lex structures linking
 
1607
  st_select_lex structures linking
1409
1608
*/
1410
1609
 
1411
1610
/* include on level down */
1412
 
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
 
1611
void st_select_lex_node::include_down(st_select_lex_node *upper)
1413
1612
{
1414
1613
  if ((next= upper->slave))
1415
1614
    next->prev= &next;
1423
1622
  include on level down (but do not link)
1424
1623
 
1425
1624
  SYNOPSYS
1426
 
    Select_Lex_Node::include_standalone()
 
1625
    st_select_lex_node::include_standalone()
1427
1626
    upper - reference on node underr which this node should be included
1428
1627
    ref - references on reference on this node
1429
1628
*/
1430
 
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1431
 
                                            Select_Lex_Node **ref)
 
1629
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
 
1630
                                            st_select_lex_node **ref)
1432
1631
{
1433
1632
  next= 0;
1434
1633
  prev= ref;
1437
1636
}
1438
1637
 
1439
1638
/* include neighbour (on same level) */
1440
 
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
 
1639
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1441
1640
{
1442
1641
  if ((next= before->next))
1443
1642
    next->prev= &next;
1447
1646
  slave= 0;
1448
1647
}
1449
1648
 
1450
 
/* including in global Select_Lex list */
1451
 
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
 
1649
/* including in global SELECT_LEX list */
 
1650
void st_select_lex_node::include_global(st_select_lex_node **plink)
1452
1651
{
1453
1652
  if ((link_next= *plink))
1454
1653
    link_next->link_prev= &link_next;
1457
1656
}
1458
1657
 
1459
1658
//excluding from global list (internal function)
1460
 
void Select_Lex_Node::fast_exclude()
 
1659
void st_select_lex_node::fast_exclude()
1461
1660
{
1462
1661
  if (link_prev)
1463
1662
  {
1467
1666
  // Remove slave structure
1468
1667
  for (; slave; slave= slave->next)
1469
1668
    slave->fast_exclude();
1470
 
 
 
1669
  
1471
1670
}
1472
1671
 
1473
1672
/*
1474
1673
  excluding select_lex structure (except first (first select can't be
1475
1674
  deleted, because it is most upper select))
1476
1675
*/
1477
 
void Select_Lex_Node::exclude()
 
1676
void st_select_lex_node::exclude()
1478
1677
{
1479
1678
  //exclude from global list
1480
1679
  fast_exclude();
1481
1680
  //exclude from other structures
1482
1681
  if ((*prev= next))
1483
1682
    next->prev= prev;
1484
 
  /*
1485
 
     We do not need following statements, because prev pointer of first
 
1683
  /* 
 
1684
     We do not need following statements, because prev pointer of first 
1486
1685
     list element point to master->slave
1487
1686
     if (master->slave == this)
1488
1687
       master->slave= next;
1494
1693
  Exclude level of current unit from tree of SELECTs
1495
1694
 
1496
1695
  SYNOPSYS
1497
 
    Select_Lex_Unit::exclude_level()
 
1696
    st_select_lex_unit::exclude_level()
1498
1697
 
1499
1698
  NOTE: units which belong to current will be brought up on level of
1500
 
  currernt unit
 
1699
  currernt unit 
1501
1700
*/
1502
 
void Select_Lex_Unit::exclude_level()
 
1701
void st_select_lex_unit::exclude_level()
1503
1702
{
1504
 
  Select_Lex_Unit *units= 0, **units_last= &units;
1505
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1703
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
 
1704
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1506
1705
  {
1507
1706
    // unlink current level from global SELECTs list
1508
1707
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1509
1708
      sl->link_next->link_prev= sl->link_prev;
1510
1709
 
1511
1710
    // bring up underlay levels
1512
 
    Select_Lex_Unit **last= 0;
1513
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1711
    SELECT_LEX_UNIT **last= 0;
 
1712
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1514
1713
    {
1515
1714
      u->master= master;
1516
 
      last= (Select_Lex_Unit**)&(u->next);
 
1715
      last= (SELECT_LEX_UNIT**)&(u->next);
1517
1716
    }
1518
1717
    if (last)
1519
1718
    {
1525
1724
  {
1526
1725
    // include brought up levels in place of current
1527
1726
    (*prev)= units;
1528
 
    (*units_last)= (Select_Lex_Unit*)next;
 
1727
    (*units_last)= (SELECT_LEX_UNIT*)next;
1529
1728
    if (next)
1530
 
      next->prev= (Select_Lex_Node**)units_last;
 
1729
      next->prev= (SELECT_LEX_NODE**)units_last;
1531
1730
    units->prev= prev;
1532
1731
  }
1533
1732
  else
1539
1738
  }
1540
1739
}
1541
1740
 
 
1741
 
1542
1742
/*
1543
1743
  Exclude subtree of current unit from tree of SELECTs
 
1744
 
 
1745
  SYNOPSYS
 
1746
    st_select_lex_unit::exclude_tree()
1544
1747
*/
1545
 
void Select_Lex_Unit::exclude_tree()
 
1748
void st_select_lex_unit::exclude_tree()
1546
1749
{
1547
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1750
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1548
1751
  {
1549
1752
    // unlink current level from global SELECTs list
1550
1753
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1551
1754
      sl->link_next->link_prev= sl->link_prev;
1552
1755
 
1553
1756
    // unlink underlay levels
1554
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1757
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1555
1758
    {
1556
1759
      u->exclude_level();
1557
1760
    }
1562
1765
    next->prev= prev;
1563
1766
}
1564
1767
 
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
 
 */
1572
 
void Select_Lex::mark_as_dependent(Select_Lex *last)
 
1768
 
 
1769
/*
 
1770
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1771
  this to 'last' as dependent
 
1772
 
 
1773
  SYNOPSIS
 
1774
    last - pointer to last st_select_lex struct, before wich all 
 
1775
           st_select_lex have to be marked as dependent
 
1776
 
 
1777
  NOTE
 
1778
    'last' should be reachable from this st_select_lex_node
 
1779
*/
 
1780
 
 
1781
void st_select_lex::mark_as_dependent(st_select_lex *last)
1573
1782
{
1574
1783
  /*
1575
1784
    Mark all selects from resolved to 1 before select where was
1576
1785
    found table as depended (of select where was found table)
1577
1786
  */
1578
 
  for (Select_Lex *s= this;
 
1787
  for (SELECT_LEX *s= this;
1579
1788
       s && s != last;
1580
1789
       s= s->outer_select())
1581
1790
  {
1582
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1791
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1583
1792
    {
1584
1793
      // Select is dependent of outer select
1585
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1586
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
1587
 
      Select_Lex_Unit *munit= s->master_unit();
1588
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1589
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1590
 
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1794
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1795
                       UNCACHEABLE_DEPENDENT;
 
1796
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1797
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1798
                       UNCACHEABLE_DEPENDENT;
 
1799
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1591
1800
      {
1592
1801
        if (sl != s &&
1593
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1594
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1802
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1803
          sl->uncacheable|= UNCACHEABLE_UNITED;
1595
1804
      }
1596
1805
    }
1597
1806
    s->is_correlated= true;
1601
1810
  }
1602
1811
}
1603
1812
 
1604
 
bool Select_Lex_Node::set_braces(bool)
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
 
 
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 *)
1626
 
{
1627
 
  return 0;
1628
 
}
1629
 
 
 
1813
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
 
1814
{ return 1; }
 
1815
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
 
1816
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1817
TableList* st_select_lex_node::get_table_list()     { return 0; }
 
1818
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
 
1819
TableList *st_select_lex_node::add_table_to_list (THD *thd __attribute__((unused)),
 
1820
                                                   Table_ident *table __attribute__((unused)),
 
1821
                                                  LEX_STRING *alias __attribute__((unused)),
 
1822
                                                  uint32_t table_join_options __attribute__((unused)),
 
1823
                                                  thr_lock_type flags __attribute__((unused)),
 
1824
                                                  List<Index_hint> *hints __attribute__((unused)),
 
1825
                                                  LEX_STRING *option __attribute__((unused)))
 
1826
{
 
1827
  return 0;
 
1828
}
 
1829
uint32_t st_select_lex_node::get_table_join_options()
 
1830
{
 
1831
  return 0;
 
1832
}
1630
1833
 
1631
1834
/*
1632
1835
  prohibit using LIMIT clause
1633
1836
*/
1634
 
bool Select_Lex::test_limit()
 
1837
bool st_select_lex::test_limit()
1635
1838
{
1636
1839
  if (select_limit != 0)
1637
1840
  {
1638
1841
    my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1639
1842
             "LIMIT & IN/ALL/ANY/SOME subquery");
1640
 
    return true;
 
1843
    return(1);
1641
1844
  }
1642
 
  return false;
1643
 
}
1644
 
 
1645
 
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1646
 
{
1647
 
  return this;
1648
 
}
1649
 
 
1650
 
Select_Lex* Select_Lex_Unit::outer_select()
1651
 
{
1652
 
  return (Select_Lex*) master;
1653
 
}
1654
 
 
1655
 
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1656
 
{
1657
 
  return add_to_list(session, order_list, item, asc);
1658
 
}
1659
 
 
1660
 
bool Select_Lex::add_item_to_list(Session *, Item *item)
 
1845
  return(0);
 
1846
}
 
1847
 
 
1848
 
 
1849
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1850
{
 
1851
    return this;
 
1852
}
 
1853
 
 
1854
 
 
1855
st_select_lex* st_select_lex_unit::outer_select()
 
1856
{
 
1857
  return (st_select_lex*) master;
 
1858
}
 
1859
 
 
1860
 
 
1861
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1862
{
 
1863
  return add_to_list(thd, order_list, item, asc);
 
1864
}
 
1865
 
 
1866
 
 
1867
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
 
1868
                                     Item *item)
1661
1869
{
1662
1870
  return(item_list.push_back(item));
1663
1871
}
1664
1872
 
1665
 
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1666
 
{
1667
 
  return add_to_list(session, group_list, item, asc);
1668
 
}
1669
 
 
1670
 
Select_Lex_Unit* Select_Lex::master_unit()
1671
 
{
1672
 
  return (Select_Lex_Unit*) master;
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)
 
1873
 
 
1874
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1875
{
 
1876
  return add_to_list(thd, group_list, item, asc);
 
1877
}
 
1878
 
 
1879
 
 
1880
st_select_lex_unit* st_select_lex::master_unit()
 
1881
{
 
1882
  return (st_select_lex_unit*) master;
 
1883
}
 
1884
 
 
1885
 
 
1886
st_select_lex* st_select_lex::outer_select()
 
1887
{
 
1888
  return (st_select_lex*) master->get_master();
 
1889
}
 
1890
 
 
1891
 
 
1892
bool st_select_lex::set_braces(bool value)
1681
1893
{
1682
1894
  braces= value;
1683
 
  return false;
 
1895
  return 0; 
1684
1896
}
1685
1897
 
1686
 
bool Select_Lex::inc_in_sum_expr()
 
1898
 
 
1899
bool st_select_lex::inc_in_sum_expr()
1687
1900
{
1688
1901
  in_sum_expr++;
1689
 
  return false;
 
1902
  return 0;
1690
1903
}
1691
1904
 
1692
 
uint32_t Select_Lex::get_in_sum_expr()
 
1905
 
 
1906
uint st_select_lex::get_in_sum_expr()
1693
1907
{
1694
1908
  return in_sum_expr;
1695
1909
}
1696
1910
 
1697
 
TableList* Select_Lex::get_table_list()
 
1911
 
 
1912
TableList* st_select_lex::get_table_list()
1698
1913
{
1699
1914
  return (TableList*) table_list.first;
1700
1915
}
1701
1916
 
1702
 
List<Item>* Select_Lex::get_item_list()
 
1917
List<Item>* st_select_lex::get_item_list()
1703
1918
{
1704
1919
  return &item_list;
1705
1920
}
1706
1921
 
1707
 
 
1708
 
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
 
1922
uint32_t st_select_lex::get_table_join_options()
 
1923
{
 
1924
  return table_join_options;
 
1925
}
 
1926
 
 
1927
 
 
1928
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1709
1929
{
1710
1930
  if (ref_pointer_array)
1711
 
    return false;
 
1931
    return 0;
1712
1932
 
 
1933
  /*
 
1934
    We have to create array in prepared statement memory if it is
 
1935
    prepared statement
 
1936
  */
 
1937
  Query_arena *arena= thd->stmt_arena;
1713
1938
  return (ref_pointer_array=
1714
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1939
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1715
1940
                                                 item_list.elements +
1716
1941
                                                 select_n_having_items +
1717
1942
                                                 select_n_where_fields +
1718
1943
                                                 order_group_num)*5)) == 0;
1719
1944
}
1720
1945
 
1721
 
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
 
1946
 
 
1947
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1722
1948
{
1723
1949
  bool union_all= !union_distinct;
1724
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1950
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1725
1951
  {
1726
1952
    if (sl != first_select())
1727
1953
    {
1733
1959
    }
1734
1960
    if (sl->braces)
1735
1961
      str->append('(');
1736
 
    sl->print(session, str, query_type);
 
1962
    sl->print(thd, str, query_type);
1737
1963
    if (sl->braces)
1738
1964
      str->append(')');
1739
1965
  }
1744
1970
      str->append(STRING_WITH_LEN(" order by "));
1745
1971
      fake_select_lex->print_order(
1746
1972
        str,
1747
 
        (Order *) fake_select_lex->order_list.first,
 
1973
        (order_st *) fake_select_lex->order_list.first,
1748
1974
        query_type);
1749
1975
    }
1750
 
    fake_select_lex->print_limit(session, str, query_type);
 
1976
    fake_select_lex->print_limit(thd, str, query_type);
1751
1977
  }
1752
1978
}
1753
1979
 
1754
 
void Select_Lex::print_order(String *str,
1755
 
                                Order *order,
 
1980
 
 
1981
void st_select_lex::print_order(String *str,
 
1982
                                order_st *order,
1756
1983
                                enum_query_type query_type)
1757
1984
{
1758
1985
  for (; order; order= order->next)
1760
1987
    if (order->counter_used)
1761
1988
    {
1762
1989
      char buffer[20];
1763
 
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
 
1990
      uint length= snprintf(buffer, 20, "%d", order->counter);
1764
1991
      str->append(buffer, length);
1765
1992
    }
1766
1993
    else
1771
1998
      str->append(',');
1772
1999
  }
1773
2000
}
 
2001
 
1774
2002
 
1775
 
void Select_Lex::print_limit(Session *, String *str,
 
2003
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
 
2004
                                String *str,
1776
2005
                                enum_query_type query_type)
1777
2006
{
1778
 
  Select_Lex_Unit *unit= master_unit();
 
2007
  SELECT_LEX_UNIT *unit= master_unit();
1779
2008
  Item_subselect *item= unit->item;
1780
2009
 
1781
2010
  if (item && unit->global_parameters == this)
1794
2023
                    ((Item_in_subselect*)item)->exec_method ==
1795
2024
                    Item_in_subselect::MATERIALIZATION) ?
1796
2025
                   true :
1797
 
                   (select_limit->val_int() == 1L) &&
 
2026
                   (select_limit->val_int() == 1LL) &&
1798
2027
                   offset_limit == 0));
1799
2028
      return;
1800
2029
    }
1811
2040
  }
1812
2041
}
1813
2042
 
1814
 
LEX::~LEX()
 
2043
/**
 
2044
  @brief Restore the LEX and THD in case of a parse error.
 
2045
 
 
2046
  This is a clean up call that is invoked by the Bison generated
 
2047
  parser before returning an error from MYSQLparse. If your
 
2048
  semantic actions manipulate with the global thread state (which
 
2049
  is a very bad practice and should not normally be employed) and
 
2050
  need a clean-up in case of error, and you can not use %destructor
 
2051
  rule in the grammar file itself, this function should be used
 
2052
  to implement the clean up.
 
2053
*/
 
2054
 
 
2055
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
1815
2056
{
1816
 
  delete _create_table;
1817
2057
}
1818
2058
 
1819
2059
/*
1833
2073
    this method to reset state of already initialized Query_tables_list
1834
2074
    so it can be used for processing of new statement.
1835
2075
*/
 
2076
 
1836
2077
void Query_tables_list::reset_query_tables_list(bool init)
1837
2078
{
1838
2079
  if (!init && query_tables)
1848
2089
  query_tables= 0;
1849
2090
  query_tables_last= &query_tables;
1850
2091
  query_tables_own_last= 0;
1851
 
}
 
2092
  if (init)
 
2093
  {
 
2094
    /*
 
2095
      We delay real initialization of hash (and therefore related
 
2096
      memory allocation) until first insertion into this hash.
 
2097
    */
 
2098
    hash_clear(&sroutines);
 
2099
  }
 
2100
  else if (sroutines.records)
 
2101
  {
 
2102
    /* Non-zero sroutines.records means that hash was initialized. */
 
2103
    my_hash_reset(&sroutines);
 
2104
  }
 
2105
  sroutines_list.empty();
 
2106
  sroutines_list_own_last= sroutines_list.next;
 
2107
  sroutines_list_own_elements= 0;
 
2108
  binlog_stmt_flags= 0;
 
2109
}
 
2110
 
 
2111
 
 
2112
/*
 
2113
  Destroy Query_tables_list object with freeing all resources used by it.
 
2114
 
 
2115
  SYNOPSIS
 
2116
    destroy_query_tables_list()
 
2117
*/
 
2118
 
 
2119
void Query_tables_list::destroy_query_tables_list()
 
2120
{
 
2121
  hash_free(&sroutines);
 
2122
}
 
2123
 
1852
2124
 
1853
2125
/*
1854
2126
  Initialize LEX object.
1855
2127
 
1856
2128
  SYNOPSIS
1857
 
    LEX::LEX()
 
2129
    st_lex::st_lex()
1858
2130
 
1859
2131
  NOTE
1860
2132
    LEX object initialized with this constructor can be used as part of
1861
 
    Session object for which one can safely call open_tables(), lock_tables()
 
2133
    THD object for which one can safely call open_tables(), lock_tables()
1862
2134
    and close_thread_tables() functions. But it is not yet ready for
1863
2135
    statement parsing. On should use lex_start() function to prepare LEX
1864
2136
    for this.
1865
2137
*/
1866
 
LEX::LEX() :
1867
 
    result(0), 
1868
 
    yacc_yyss(0), 
1869
 
    yacc_yyvs(0),
1870
 
    session(NULL),
1871
 
    charset(NULL),
1872
 
    var_list(),
1873
 
    sql_command(SQLCOM_END), 
1874
 
    statement(NULL),
1875
 
    option_type(OPT_DEFAULT), 
1876
 
    is_lex_started(0),
1877
 
    cacheable(true),
1878
 
    sum_expr_used(false),
1879
 
    _create_table(NULL),
1880
 
    _create_field(NULL),
1881
 
    _exists(false)
 
2138
 
 
2139
st_lex::st_lex()
 
2140
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
2141
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1882
2142
{
 
2143
 
 
2144
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
 
2145
                         plugins_static_buffer,
 
2146
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
 
2147
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
1883
2148
  reset_query_tables_list(true);
1884
2149
}
1885
2150
 
 
2151
 
 
2152
/*
 
2153
  Check whether the merging algorithm can be used on this VIEW
 
2154
 
 
2155
  SYNOPSIS
 
2156
    st_lex::can_be_merged()
 
2157
 
 
2158
  DESCRIPTION
 
2159
    We can apply merge algorithm if it is single SELECT view  with
 
2160
    subqueries only in WHERE clause (we do not count SELECTs of underlying
 
2161
    views, and second level subqueries) and we have not grpouping, ordering,
 
2162
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
 
2163
    several underlying tables.
 
2164
 
 
2165
  RETURN
 
2166
    false - only temporary table algorithm can be used
 
2167
    true  - merge algorithm can be used
 
2168
*/
 
2169
 
 
2170
bool st_lex::can_be_merged()
 
2171
{
 
2172
  // TODO: do not forget implement case when select_lex.table_list.elements==0
 
2173
 
 
2174
  /* find non VIEW subqueries/unions */
 
2175
  bool selects_allow_merge= select_lex.next_select() == 0;
 
2176
  if (selects_allow_merge)
 
2177
  {
 
2178
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
 
2179
         tmp_unit;
 
2180
         tmp_unit= tmp_unit->next_unit())
 
2181
    {
 
2182
      if (tmp_unit->first_select()->parent_lex == this &&
 
2183
          (tmp_unit->item == 0 ||
 
2184
           (tmp_unit->item->place() != IN_WHERE &&
 
2185
            tmp_unit->item->place() != IN_ON)))
 
2186
      {
 
2187
        selects_allow_merge= 0;
 
2188
        break;
 
2189
      }
 
2190
    }
 
2191
  }
 
2192
 
 
2193
  return (selects_allow_merge &&
 
2194
          select_lex.group_list.elements == 0 &&
 
2195
          select_lex.having == 0 &&
 
2196
          select_lex.with_sum_func == 0 &&
 
2197
          select_lex.table_list.elements >= 1 &&
 
2198
          !(select_lex.options & SELECT_DISTINCT) &&
 
2199
          select_lex.select_limit == 0);
 
2200
}
 
2201
 
 
2202
 
 
2203
/*
 
2204
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
 
2205
 
 
2206
  SYNOPSIS
 
2207
    st_lex::can_use_merged()
 
2208
 
 
2209
  DESCRIPTION
 
2210
    Only listed here commands can use merge algorithm in top level
 
2211
    SELECT_LEX (for subqueries will be used merge algorithm if
 
2212
    st_lex::can_not_use_merged() is not true).
 
2213
 
 
2214
  RETURN
 
2215
    false - command can't use merged VIEWs
 
2216
    true  - VIEWs with MERGE algorithms can be used
 
2217
*/
 
2218
 
 
2219
bool st_lex::can_use_merged()
 
2220
{
 
2221
  switch (sql_command)
 
2222
  {
 
2223
  case SQLCOM_SELECT:
 
2224
  case SQLCOM_CREATE_TABLE:
 
2225
  case SQLCOM_UPDATE:
 
2226
  case SQLCOM_UPDATE_MULTI:
 
2227
  case SQLCOM_DELETE:
 
2228
  case SQLCOM_DELETE_MULTI:
 
2229
  case SQLCOM_INSERT:
 
2230
  case SQLCOM_INSERT_SELECT:
 
2231
  case SQLCOM_REPLACE:
 
2232
  case SQLCOM_REPLACE_SELECT:
 
2233
  case SQLCOM_LOAD:
 
2234
    return true;
 
2235
  default:
 
2236
    return false;
 
2237
  }
 
2238
}
 
2239
 
 
2240
/*
 
2241
  Check if command can't use merged views in any part of command
 
2242
 
 
2243
  SYNOPSIS
 
2244
    st_lex::can_not_use_merged()
 
2245
 
 
2246
  DESCRIPTION
 
2247
    Temporary table algorithm will be used on all SELECT levels for queries
 
2248
    listed here (see also st_lex::can_use_merged()).
 
2249
 
 
2250
  RETURN
 
2251
    false - command can't use merged VIEWs
 
2252
    true  - VIEWs with MERGE algorithms can be used
 
2253
*/
 
2254
 
 
2255
bool st_lex::can_not_use_merged()
 
2256
{
 
2257
  switch (sql_command)
 
2258
  {
 
2259
  /*
 
2260
    SQLCOM_SHOW_FIELDS is necessary to make 
 
2261
    information schema tables working correctly with views.
 
2262
    see get_schema_tables_result function
 
2263
  */
 
2264
  case SQLCOM_SHOW_FIELDS:
 
2265
    return true;
 
2266
  default:
 
2267
    return false;
 
2268
  }
 
2269
}
 
2270
 
 
2271
/*
 
2272
  Detect that we need only table structure of derived table/view
 
2273
 
 
2274
  SYNOPSIS
 
2275
    only_view_structure()
 
2276
 
 
2277
  RETURN
 
2278
    true yes, we need only structure
 
2279
    false no, we need data
 
2280
*/
 
2281
 
 
2282
bool st_lex::only_view_structure()
 
2283
{
 
2284
  switch (sql_command) {
 
2285
  case SQLCOM_SHOW_CREATE:
 
2286
  case SQLCOM_SHOW_TABLES:
 
2287
  case SQLCOM_SHOW_FIELDS:
 
2288
    return true;
 
2289
  default:
 
2290
    return false;
 
2291
  }
 
2292
}
 
2293
 
 
2294
 
 
2295
/*
 
2296
  Should Items_ident be printed correctly
 
2297
 
 
2298
  SYNOPSIS
 
2299
    need_correct_ident()
 
2300
 
 
2301
  RETURN
 
2302
    true yes, we need only structure
 
2303
    false no, we need data
 
2304
*/
 
2305
 
 
2306
 
 
2307
bool st_lex::need_correct_ident()
 
2308
{
 
2309
  switch(sql_command)
 
2310
  {
 
2311
  case SQLCOM_SHOW_CREATE:
 
2312
  case SQLCOM_SHOW_TABLES:
 
2313
    return true;
 
2314
  default:
 
2315
    return false;
 
2316
  }
 
2317
}
 
2318
 
 
2319
 
1886
2320
/**
1887
2321
  This method should be called only during parsing.
1888
2322
  It is aware of compound statements (stored routine bodies)
1889
2323
  and will initialize the destination with the default
1890
2324
  database of the stored routine, rather than the default
1891
2325
  database of the connection it is parsed in.
1892
 
  E.g. if one has no current database selected, or current database
 
2326
  E.g. if one has no current database selected, or current database 
1893
2327
  set to 'bar' and then issues:
1894
2328
 
1895
2329
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
1901
2335
  @return true in case of error (parsing should be aborted, false in
1902
2336
  case of success
1903
2337
*/
1904
 
bool LEX::copy_db_to(char **p_db, size_t *p_db_length) const
 
2338
 
 
2339
bool
 
2340
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
1905
2341
{
1906
 
  return session->copy_db_to(p_db, p_db_length);
 
2342
  return thd->copy_db_to(p_db, p_db_length);
1907
2343
}
1908
2344
 
1909
2345
/*
1910
2346
  initialize limit counters
1911
2347
 
1912
2348
  SYNOPSIS
1913
 
    Select_Lex_Unit::set_limit()
1914
 
    values      - Select_Lex with initial values for counters
 
2349
    st_select_lex_unit::set_limit()
 
2350
    values      - SELECT_LEX with initial values for counters
1915
2351
*/
1916
 
void Select_Lex_Unit::set_limit(Select_Lex *sl)
 
2352
 
 
2353
void st_select_lex_unit::set_limit(st_select_lex *sl)
1917
2354
{
1918
2355
  ha_rows select_limit_val;
1919
2356
  uint64_t val;
1920
2357
 
1921
2358
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
1922
2359
  select_limit_val= (ha_rows)val;
1923
 
  /*
 
2360
#ifndef BIG_TABLES
 
2361
  /* 
1924
2362
    Check for overflow : ha_rows can be smaller then uint64_t if
1925
2363
    BIG_TABLES is off.
1926
2364
    */
1927
2365
  if (val != (uint64_t)select_limit_val)
1928
2366
    select_limit_val= HA_POS_ERROR;
 
2367
#endif
1929
2368
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
1930
 
                                                 0UL);
 
2369
                                                 0ULL);
1931
2370
  select_limit_cnt= select_limit_val + offset_limit_cnt;
1932
2371
  if (select_limit_cnt < select_limit_val)
1933
2372
    select_limit_cnt= HA_POS_ERROR;             // no limit
1934
2373
}
1935
2374
 
 
2375
 
1936
2376
/*
1937
2377
  Unlink the first table from the global table list and the first table from
1938
2378
  outer select (lex->select_lex) local list
1951
2391
      In this case link_to_local is set.
1952
2392
 
1953
2393
*/
1954
 
TableList *LEX::unlink_first_table(bool *link_to_local)
 
2394
TableList *st_lex::unlink_first_table(bool *link_to_local)
1955
2395
{
1956
2396
  TableList *first;
1957
2397
  if ((first= query_tables))
1970
2410
    */
1971
2411
    if ((*link_to_local= test(select_lex.table_list.first)))
1972
2412
    {
1973
 
      select_lex.context.table_list=
 
2413
      select_lex.context.table_list= 
1974
2414
        select_lex.context.first_name_resolution_table= first->next_local;
1975
 
      select_lex.table_list.first= (unsigned char*) (first->next_local);
 
2415
      select_lex.table_list.first= (uchar*) (first->next_local);
1976
2416
      select_lex.table_list.elements--; //safety
1977
2417
      first->next_local= 0;
1978
2418
      /*
1985
2425
  return first;
1986
2426
}
1987
2427
 
 
2428
 
1988
2429
/*
1989
2430
  Bring first local table of first most outer select to first place in global
1990
2431
  table list
1991
2432
 
1992
2433
  SYNOPSYS
1993
 
     LEX::first_lists_tables_same()
 
2434
     st_lex::first_lists_tables_same()
1994
2435
 
1995
2436
  NOTES
1996
2437
    In many cases (for example, usual INSERT/DELETE/...) the first table of
1997
 
    main Select_Lex have special meaning => check that it is the first table
 
2438
    main SELECT_LEX have special meaning => check that it is the first table
1998
2439
    in global list and re-link to be first in the global list if it is
1999
2440
    necessary.  We need such re-linking only for queries with sub-queries in
2000
2441
    the select list, as only in this case tables of sub-queries will go to
2001
2442
    the global list first.
2002
2443
*/
2003
 
void LEX::first_lists_tables_same()
 
2444
 
 
2445
void st_lex::first_lists_tables_same()
2004
2446
{
2005
2447
  TableList *first_table= (TableList*) select_lex.table_list.first;
2006
2448
  if (query_tables != first_table && first_table != 0)
2024
2466
  }
2025
2467
}
2026
2468
 
 
2469
 
2027
2470
/*
2028
2471
  Link table back that was unlinked with unlink_first_table()
2029
2472
 
2034
2477
  RETURN
2035
2478
    global list
2036
2479
*/
2037
 
void LEX::link_first_table_back(TableList *first, bool link_to_local)
 
2480
 
 
2481
void st_lex::link_first_table_back(TableList *first,
 
2482
                                   bool link_to_local)
2038
2483
{
2039
2484
  if (first)
2040
2485
  {
2048
2493
    {
2049
2494
      first->next_local= (TableList*) select_lex.table_list.first;
2050
2495
      select_lex.context.table_list= first;
2051
 
      select_lex.table_list.first= (unsigned char*) first;
 
2496
      select_lex.table_list.first= (uchar*) first;
2052
2497
      select_lex.table_list.elements++; //safety
2053
2498
    }
2054
2499
  }
2055
2500
}
2056
2501
 
 
2502
 
 
2503
 
2057
2504
/*
2058
2505
  cleanup lex for case when we open table by table for processing
2059
2506
 
2060
2507
  SYNOPSIS
2061
 
    LEX::cleanup_after_one_table_open()
 
2508
    st_lex::cleanup_after_one_table_open()
2062
2509
 
2063
2510
  NOTE
2064
2511
    This method is mostly responsible for cleaning up of selects lists and
2065
2512
    derived tables state. To rollback changes in Query_tables_list one has
2066
2513
    to call Query_tables_list::reset_query_tables_list(false).
2067
2514
*/
2068
 
void LEX::cleanup_after_one_table_open()
 
2515
 
 
2516
void st_lex::cleanup_after_one_table_open()
2069
2517
{
2070
2518
  /*
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
2073
 
    to prevent processing of derived tables during next openTablesLock
 
2519
    thd->lex->derived_tables & additional units may be set if we open
 
2520
    a view. It is necessary to clear thd->lex->derived_tables flag
 
2521
    to prevent processing of derived tables during next open_and_lock_tables
2074
2522
    if next table is a real table and cleanup & remove underlying units
2075
 
    NOTE: all units will be connected to session->getLex()->select_lex, because we
 
2523
    NOTE: all units will be connected to thd->lex->select_lex, because we
2076
2524
    have not UNION on most upper level.
2077
2525
    */
2078
2526
  if (all_selects_list != &select_lex)
2079
2527
  {
2080
2528
    derived_tables= 0;
2081
2529
    /* cleunup underlying units (units of VIEW) */
2082
 
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
 
2530
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
2083
2531
         un;
2084
2532
         un= un->next_unit())
2085
2533
      un->cleanup();
2090
2538
  }
2091
2539
}
2092
2540
 
2093
 
/*
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
 
 
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
 
2541
 
 
2542
/*
 
2543
  Save current state of Query_tables_list for this LEX, and prepare it
 
2544
  for processing of new statemnt.
 
2545
 
 
2546
  SYNOPSIS
 
2547
    reset_n_backup_query_tables_list()
 
2548
      backup  Pointer to Query_tables_list instance to be used for backup
 
2549
*/
 
2550
 
 
2551
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2552
{
 
2553
}
 
2554
 
 
2555
 
 
2556
/*
 
2557
  Restore state of Query_tables_list for this LEX from backup.
 
2558
 
 
2559
  SYNOPSIS
 
2560
    restore_backup_query_tables_list()
 
2561
      backup  Pointer to Query_tables_list instance used for backup
 
2562
*/
 
2563
 
 
2564
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2565
{
 
2566
}
 
2567
 
 
2568
 
 
2569
/*
 
2570
  Checks for usage of routines and/or tables in a parsed statement
 
2571
 
 
2572
  SYNOPSIS
 
2573
    st_lex:table_or_sp_used()
 
2574
 
 
2575
  RETURN
 
2576
    false  No routines and tables used
 
2577
    true   Either or both routines and tables are used.
 
2578
*/
 
2579
 
 
2580
bool st_lex::table_or_sp_used()
 
2581
{
 
2582
  if (sroutines.records || query_tables)
 
2583
    return(true);
 
2584
 
 
2585
  return(false);
 
2586
}
 
2587
 
 
2588
 
 
2589
/*
 
2590
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
 
2591
 
 
2592
  SYNOPSIS
 
2593
    fix_prepare_info_in_table_list()
 
2594
      thd  Thread handle
 
2595
      tbl  List of tables to process
 
2596
 
 
2597
  DESCRIPTION
 
2598
    Perform end-end-of prepare fixup for list of tables, if any of the tables
 
2599
    is a merge-algorithm VIEW, recursively fix up its underlying tables as
 
2600
    well.
 
2601
 
 
2602
*/
 
2603
 
 
2604
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
 
2605
{
 
2606
  for (; tbl; tbl= tbl->next_local)
 
2607
  {
 
2608
    if (tbl->on_expr)
 
2609
    {
 
2610
      tbl->prep_on_expr= tbl->on_expr;
 
2611
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2612
    }
 
2613
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2614
  }
 
2615
}
 
2616
 
 
2617
 
 
2618
/*
 
2619
  There are st_select_lex::add_table_to_list &
 
2620
  st_select_lex::set_lock_for_tables are in sql_parse.cc
 
2621
 
 
2622
  st_select_lex::print is in sql_select.cc
 
2623
 
 
2624
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
 
2625
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
 
2626
  st_select_lex_unit::change_result
2102
2627
  are in sql_union.cc
2103
2628
*/
2104
2629
 
2112
2637
 
2113
2638
  DESCRIPTION
2114
2639
    Used in filling up the tagged hints list.
2115
 
    This list is filled by first setting the kind of the hint as a
 
2640
    This list is filled by first setting the kind of the hint as a 
2116
2641
    context variable and then adding hints of the current kind.
2117
2642
    Then the context variable index_hint_type can be reset to the
2118
2643
    next hint type.
2119
2644
*/
2120
 
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg, index_clause_map clause)
2121
 
{
 
2645
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2646
                                        index_clause_map clause)
 
2647
2122
2648
  current_index_hint_type= type_arg;
2123
2649
  current_index_hint_clause= clause;
2124
2650
}
2125
2651
 
 
2652
 
2126
2653
/*
2127
2654
  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
2128
2655
 
2129
2656
  SYNOPSIS
2130
2657
    alloc_index_hints()
2131
 
      session         current thread.
 
2658
      thd         current thread.
2132
2659
*/
2133
 
void Select_Lex::alloc_index_hints (Session *session)
2134
 
{
2135
 
  index_hints= new (session->mem_root) List<Index_hint>();
 
2660
 
 
2661
void st_select_lex::alloc_index_hints (THD *thd)
 
2662
 
2663
  index_hints= new (thd->mem_root) List<Index_hint>(); 
2136
2664
}
2137
2665
 
 
2666
 
 
2667
 
2138
2668
/*
2139
 
  adds an element to the array storing index usage hints
 
2669
  adds an element to the array storing index usage hints 
2140
2670
  (ADD/FORCE/IGNORE INDEX).
2141
2671
 
2142
2672
  SYNOPSIS
2143
2673
    add_index_hint()
2144
 
      session         current thread.
 
2674
      thd         current thread.
2145
2675
      str         name of the index.
2146
2676
      length      number of characters in str.
2147
2677
 
2148
2678
  RETURN VALUE
2149
2679
    0 on success, non-zero otherwise
2150
2680
*/
2151
 
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
 
2681
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2152
2682
{
2153
 
  return index_hints->push_front (new (session->mem_root)
 
2683
  return index_hints->push_front (new (thd->mem_root) 
2154
2684
                                 Index_hint(current_index_hint_type,
2155
2685
                                            current_index_hint_clause,
2156
2686
                                            str, length));
2157
2687
}
2158
 
 
2159
 
bool check_for_sql_keyword(drizzled::lex_string_t const& string)
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
 
 
2176
 
} /* namespace drizzled */