~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Stewart Smith
  • Date: 2008-10-15 04:21:24 UTC
  • mto: This revision was merged to the branch mainline in revision 516.
  • Revision ID: stewart@flamingspork.com-20081015042124-kdmb74bcbky1k1nz
remove my_pthread_[gs]etspecific

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