1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* A lexical scanner on a temporary buffer with a yacc interface */
20
#include "mysql_priv.h"
21
#include "item_create.h"
25
static int lex_one_token(void *arg, void *yythd);
28
We are using pointer to this variable for distinguishing between assignment
29
to NEW row field (when parsing trigger definition) and structured variable.
32
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
35
LEX_STRING constant for null-string to be used in parser and other places.
37
const LEX_STRING null_lex_str= {NULL, 0};
39
/* Longest standard keyword name */
41
#define TOCK_NAME_LENGTH 24
44
The following data is based on the latin1 character set, and is only
45
used when comparing keywords
48
static uchar to_upper_lex[]=
50
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
52
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
53
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
54
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
55
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
56
96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
57
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
58
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
59
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
60
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
61
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
62
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
63
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
64
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
65
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
69
Names of the index hints (for error messages). Keep in sync with
73
const char * index_hint_type_name[] =
80
inline int lex_casecmp(const char *s, const char *t, uint len)
83
to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
93
DBUG_ENTER("lex_init");
94
for (i=0 ; i < array_elements(symbols) ; i++)
95
symbols[i].length=(uchar) strlen(symbols[i].name);
96
for (i=0 ; i < array_elements(sql_functions) ; i++)
97
sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
104
{ // Call this when daemon ends
105
DBUG_ENTER("lex_free");
111
st_parsing_options::reset()
113
allows_variable= TRUE;
114
allows_select_into= TRUE;
115
allows_select_procedure= TRUE;
116
allows_derived= TRUE;
119
Lex_input_stream::Lex_input_stream(THD *thd,
126
lookahead_token(END_OF_INPUT),
127
lookahead_yylval(NULL),
131
m_end_of_query(buffer + length),
132
m_tok_start_prev(NULL),
134
m_buf_length(length),
136
m_cpp_tok_start(NULL),
137
m_cpp_tok_start_prev(NULL),
140
m_cpp_utf8_processed_ptr(NULL),
141
next_state(MY_LEX_START),
142
found_semicolon(NULL),
144
stmt_prepare_mode(FALSE),
145
in_comment(NO_COMMENT),
146
m_underscore_cs(NULL)
148
m_cpp_buf= (char*) thd->alloc(length + 1);
149
m_cpp_ptr= m_cpp_buf;
152
Lex_input_stream::~Lex_input_stream()
156
The operation is called from the parser in order to
157
1) designate the intention to have utf8 body;
158
1) Indicate to the lexer that we will need a utf8 representation of this
160
2) Determine the beginning of the body.
162
@param thd Thread context.
163
@param begin_ptr Pointer to the start of the body in the pre-processed
167
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
169
DBUG_ASSERT(begin_ptr);
170
DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
172
uint body_utf8_length=
173
(m_buf_length / thd->variables.character_set_client->mbminlen) *
174
my_charset_utf8_bin.mbmaxlen;
176
m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
177
m_body_utf8_ptr= m_body_utf8;
180
m_cpp_utf8_processed_ptr= begin_ptr;
184
@brief The operation appends unprocessed part of pre-processed buffer till
185
the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.
187
The idea is that some tokens in the pre-processed buffer (like character
188
set introducers) should be skipped.
191
CPP buffer: SELECT 'str1', _latin1 'str2';
192
m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
193
In order to skip "_latin1", the following call should be made:
194
body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)
196
@param ptr Pointer in the pre-processed buffer, which specifies the
197
end of the chunk, which should be appended to the utf8
199
@param end_ptr Pointer in the pre-processed buffer, to which
200
m_cpp_utf8_processed_ptr will be set in the end of the
204
void Lex_input_stream::body_utf8_append(const char *ptr,
207
DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
208
DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
213
if (m_cpp_utf8_processed_ptr >= ptr)
216
int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
218
memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
219
m_body_utf8_ptr += bytes_to_copy;
222
m_cpp_utf8_processed_ptr= end_ptr;
226
The operation appends unprocessed part of the pre-processed buffer till
227
the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.
229
@param ptr Pointer in the pre-processed buffer, which specifies the end
230
of the chunk, which should be appended to the utf8 body.
233
void Lex_input_stream::body_utf8_append(const char *ptr)
235
body_utf8_append(ptr, ptr);
239
The operation converts the specified text literal to the utf8 and appends
240
the result to the utf8-body.
242
@param thd Thread context.
243
@param txt Text literal.
244
@param txt_cs Character set of the text literal.
245
@param end_ptr Pointer in the pre-processed buffer, to which
246
m_cpp_utf8_processed_ptr will be set in the end of the
250
void Lex_input_stream::body_utf8_append_literal(THD *thd,
251
const LEX_STRING *txt,
252
CHARSET_INFO *txt_cs,
255
if (!m_cpp_utf8_processed_ptr)
260
if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
262
thd->convert_string(&utf_txt,
263
&my_charset_utf8_general_ci,
264
txt->str, txt->length,
269
utf_txt.str= txt->str;
270
utf_txt.length= txt->length;
273
/* NOTE: utf_txt.length is in bytes, not in symbols. */
275
memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
276
m_body_utf8_ptr += utf_txt.length;
279
m_cpp_utf8_processed_ptr= end_ptr;
284
This is called before every query that is to be parsed.
285
Because of this, it's critical to not do too much things here.
286
(We already do too much here)
289
void lex_start(THD *thd)
292
DBUG_ENTER("lex_start");
294
lex->thd= lex->unit.thd= thd;
296
lex->context_stack.empty();
297
lex->unit.init_query();
298
lex->unit.init_select();
299
/* 'parent_lex' is used in init_query() so it must be before it. */
300
lex->select_lex.parent_lex= lex;
301
lex->select_lex.init_query();
302
lex->value_list.empty();
303
lex->update_list.empty();
304
lex->param_list.empty();
305
lex->view_list.empty();
306
lex->auxiliary_table_list.empty();
307
lex->unit.next= lex->unit.master=
308
lex->unit.link_next= lex->unit.return_to= 0;
309
lex->unit.prev= lex->unit.link_prev= 0;
310
lex->unit.slave= lex->unit.global_parameters= lex->current_select=
311
lex->all_selects_list= &lex->select_lex;
312
lex->select_lex.master= &lex->unit;
313
lex->select_lex.prev= &lex->unit.slave;
314
lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
315
lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
316
lex->select_lex.options= 0;
317
lex->select_lex.init_order();
318
lex->select_lex.group_list.empty();
320
lex->subqueries= FALSE;
321
lex->derived_tables= 0;
322
lex->lock_option= TL_READ;
323
lex->leaf_tables_insert= 0;
324
lex->parsing_options.reset();
325
lex->empty_field_list_on_rset= 0;
326
lex->select_lex.select_number= 1;
328
lex->select_lex.in_sum_expr=0;
329
lex->select_lex.ftfunc_list_alloc.empty();
330
lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
331
lex->select_lex.group_list.empty();
332
lex->select_lex.order_list.empty();
333
lex->sql_command= SQLCOM_END;
334
lex->duplicates= DUP_ERROR;
336
lex->proc_list.first= 0;
337
lex->escape_used= FALSE;
338
lex->query_tables= 0;
339
lex->reset_query_tables_list(FALSE);
340
lex->expr_allows_subselect= TRUE;
341
lex->use_only_table_context= FALSE;
346
lex->allow_sum_func= 0;
347
lex->in_sum_func= NULL;
349
ok, there must be a better solution for this, long-term
350
I tried "bzero" in the sql_yacc.yy code, but that for
351
some reason made the values zero, even if they were set
353
lex->server_options.server_name= 0;
354
lex->server_options.server_name_length= 0;
355
lex->server_options.host= 0;
356
lex->server_options.db= 0;
357
lex->server_options.username= 0;
358
lex->server_options.password= 0;
359
lex->server_options.scheme= 0;
360
lex->server_options.socket= 0;
361
lex->server_options.owner= 0;
362
lex->server_options.port= -1;
364
lex->is_lex_started= TRUE;
368
void lex_end(LEX *lex)
370
DBUG_ENTER("lex_end");
371
DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
374
my_free(lex->yacc_yyss, MYF(0));
375
my_free(lex->yacc_yyvs, MYF(0));
380
/* release used plugins */
381
plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer,
382
lex->plugins.elements);
383
reset_dynamic(&lex->plugins);
389
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
391
const char *tok= lip->get_tok_start();
393
SYMBOL *symbol= get_hash_symbol(tok, len, function);
396
lip->yylval->symbol.symbol=symbol;
397
lip->yylval->symbol.str= (char*) tok;
398
lip->yylval->symbol.length=len;
406
Check if name is a keyword
410
name checked name (must not be empty)
411
len length of checked name
415
1 name isn't a keyword
418
bool is_keyword(const char *name, uint len)
420
DBUG_ASSERT(len != 0);
421
return get_hash_symbol(name,len,0)!=0;
424
bool is_lex_native_function(const LEX_STRING *name)
426
DBUG_ASSERT(name != NULL);
427
return (get_hash_symbol(name->str, name->length, 1) != 0);
430
/* make a copy of token before ptr and set yytoklen */
432
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
435
lip->yyUnget(); // ptr points now after last token char
436
tmp.length=lip->yytoklen=length;
437
tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
439
lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
440
lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
447
There are no dangerous charsets in mysql for function
448
get_quoted_token yet. But it should be fixed in the
449
future to operate multichar strings (like ucs2)
452
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
454
uint length, char quote)
457
const char *from, *end;
459
lip->yyUnget(); // ptr points now after last token char
460
tmp.length= lip->yytoklen=length;
461
tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
462
from= lip->get_tok_start() + skip;
466
lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
467
lip->m_cpp_text_end= lip->m_cpp_text_start + length;
471
if ((*to++= *from++) == quote)
473
from++; // Skip double quotes
474
lip->m_cpp_text_start++;
477
*to= 0; // End null for safety
483
Return an unescaped text literal without quotes
484
Fix sometimes to do only one scan of the string
487
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
489
register uchar c,sep;
491
CHARSET_INFO *cs= lip->m_thd->charset();
494
sep= lip->yyGetLast(); // String should end with this
505
lip->get_end_of_query()))) {
506
lip->skip_binary(l-1);
512
{ // Escaped character
520
if (c == lip->yyGet()) // Check if two separators in a row
522
found_escape=1; // duplicate. Remember for delete
528
/* Found end. Unescape and return string */
529
const char *str, *end;
532
str= lip->get_tok_start();
534
/* Extract the text from the token */
537
DBUG_ASSERT(end >= str);
539
if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
540
return (char*) ""; // Sql_alloc has set error flag
542
lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
543
lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
547
lip->yytoklen=(uint) (end-str);
548
memcpy(start,str,lip->yytoklen);
549
start[lip->yytoklen]=0;
555
for (to=start ; str != end ; str++)
560
(l = my_ismbchar(cs, str, end))) {
567
if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
568
*str == '\\' && str+1 != end)
584
*to++= 0; // Ascii null
586
case 'Z': // ^Z must be escaped on Win32
591
*to++= '\\'; // remember prefix for wildcard
598
else if (*str == sep)
599
*to++= *str++; // Two ' or "
604
lip->yytoklen=(uint) (to-start);
609
return 0; // unexpected end of query
614
** Calc type of integer; long integer, longlong integer or real.
615
** Returns smallest type that match the string.
616
** When using unsigned long long values the result is converted to a real
617
** because else they will be unexpected sign changes because all calculation
618
** is done with longlong or double.
621
static const char *long_str="2147483647";
622
static const uint long_len=10;
623
static const char *signed_long_str="-2147483648";
624
static const char *longlong_str="9223372036854775807";
625
static const uint longlong_len=19;
626
static const char *signed_longlong_str="-9223372036854775808";
627
static const uint signed_longlong_len=19;
628
static const char *unsigned_longlong_str="18446744073709551615";
629
static const uint unsigned_longlong_len=20;
631
static inline uint int_token(const char *str,uint length)
633
if (length < long_len) // quick normal case
637
if (*str == '+') // Remove sign and pre-zeros
641
else if (*str == '-')
646
while (*str == '0' && length)
650
if (length < long_len)
657
if (length == long_len)
659
cmp= signed_long_str+1;
660
smaller=NUM; // If <= signed_long_str
661
bigger=LONG_NUM; // If >= signed_long_str
663
else if (length < signed_longlong_len)
665
else if (length > signed_longlong_len)
669
cmp=signed_longlong_str+1;
670
smaller=LONG_NUM; // If <= signed_longlong_str
676
if (length == long_len)
682
else if (length < longlong_len)
684
else if (length > longlong_len)
686
if (length > unsigned_longlong_len)
688
cmp=unsigned_longlong_str;
689
smaller=ULONGLONG_NUM;
696
bigger= ULONGLONG_NUM;
699
while (*cmp && *cmp++ == *str++) ;
700
return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
705
MYSQLlex remember the following states from the following MYSQLlex()
707
- MY_LEX_EOQ Found end of query
708
- MY_LEX_OPERATOR_OR_IDENT Last state was an ident, text or number
709
(which can't be followed by a signed number)
712
int MYSQLlex(void *arg, void *yythd)
714
THD *thd= (THD *)yythd;
715
Lex_input_stream *lip= thd->m_lip;
716
YYSTYPE *yylval=(YYSTYPE*) arg;
719
if (lip->lookahead_token != END_OF_INPUT)
722
The next token was already parsed in advance,
725
token= lip->lookahead_token;
726
lip->lookahead_token= END_OF_INPUT;
727
*yylval= *(lip->lookahead_yylval);
728
lip->lookahead_yylval= NULL;
732
token= lex_one_token(arg, yythd);
737
Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
738
which makes the grammar LALR(2).
739
Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
740
to transform the grammar into a LALR(1) grammar,
741
which sql_yacc.yy can process.
743
token= lex_one_token(arg, yythd);
746
return WITH_CUBE_SYM;
748
return WITH_ROLLUP_SYM;
751
Save the token following 'WITH'
753
lip->lookahead_yylval= lip->yylval;
755
lip->lookahead_token= token;
766
int lex_one_token(void *arg, void *yythd)
768
register unsigned char c= 0; /* Just set to shutup GCC */
770
int tokval, result_state;
772
enum my_lex_states state;
773
THD *thd= (THD *)yythd;
774
Lex_input_stream *lip= thd->m_lip;
776
YYSTYPE *yylval=(YYSTYPE*) arg;
777
CHARSET_INFO *cs= thd->charset();
778
uchar *state_map= cs->state_map;
779
uchar *ident_map= cs->ident_map;
781
lip->yylval=yylval; // The global state
784
state=lip->next_state;
785
lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
789
case MY_LEX_OPERATOR_OR_IDENT: // Next is operator or keyword
790
case MY_LEX_START: // Start of token
791
// Skip starting whitespace
792
while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
800
/* Start of real token */
801
lip->restart_token();
803
state= (enum my_lex_states) state_map[c];
806
if (lip->yyGet() == 'N')
807
{ // Allow \N as shortcut for NULL
808
yylval->lex_str.str=(char*) "\\N";
809
yylval->lex_str.length=2;
812
case MY_LEX_CHAR: // Unknown or single char token
813
case MY_LEX_SKIP: // This should not happen
814
if (c == '-' && lip->yyPeek() == '-' &&
815
(my_isspace(cs,lip->yyPeekn(1)) ||
816
my_iscntrl(cs,lip->yyPeekn(1))))
818
state=MY_LEX_COMMENT;
823
lip->next_state= MY_LEX_START; // Allow signed numbers
829
This is a work around, to make the "remember_name" rule in
830
sql/sql_yacc.yy work properly.
831
The problem is that, when parsing "select expr1, expr2",
832
the code generated by bison executes the *pre* action
833
remember_name (see select_item) *before* actually parsing the
834
first token of expr2.
836
lip->restart_token();
841
Check for a placeholder: it should not precede a possible identifier
842
because of binlogging: when a placeholder is replaced with
843
its value in a query for the binlog, the query must stay
844
grammatically correct.
846
if (c == '?' && lip->stmt_prepare_mode && !ident_map[(uint8_t)(lip->yyPeek())])
847
return(PARAM_MARKER);
852
case MY_LEX_IDENT_OR_NCHAR:
853
if (lip->yyPeek() != '\'')
858
/* Found N'string' */
859
lip->yySkip(); // Skip '
860
if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
862
state= MY_LEX_CHAR; // Read char by char
865
yylval->lex_str.length= lip->yytoklen;
866
lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
867
return(NCHAR_STRING);
869
case MY_LEX_IDENT_OR_HEX:
870
if (lip->yyPeek() == '\'')
871
{ // Found x'hex-number'
872
state= MY_LEX_HEX_NUMBER;
875
case MY_LEX_IDENT_OR_BIN:
876
if (lip->yyPeek() == '\'')
877
{ // Found b'bin-number'
878
state= MY_LEX_BIN_NUMBER;
883
#if defined(USE_MB) && defined(USE_MB_IDENT)
886
result_state= IDENT_QUOTED;
887
if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
889
int l = my_ismbchar(cs,
891
lip->get_end_of_query());
896
lip->skip_binary(l - 1);
898
while (ident_map[c=lip->yyGet()])
900
if (my_mbcharlen(cs, c) > 1)
903
if ((l = my_ismbchar(cs,
905
lip->get_end_of_query())) == 0)
907
lip->skip_binary(l-1);
914
for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
915
/* If there were non-ASCII characters, mark that we must convert */
916
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
918
length= lip->yyLength();
919
start= lip->get_ptr();
920
if (lip->ignore_space)
923
If we find a space then this can't be an identifier. We notice this
924
below by checking start != lex->ptr.
926
for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
928
if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
929
lip->next_state=MY_LEX_IDENT_SEP;
931
{ // '(' must follow directly if function
933
if ((tokval = find_keyword(lip, length, c == '(')))
935
lip->next_state= MY_LEX_START; // Allow signed numbers
936
return(tokval); // Was keyword
938
lip->yySkip(); // next state does a unget
940
yylval->lex_str=get_token(lip, 0, length);
943
Note: "SELECT _bla AS 'alias'"
944
_bla should be considered as a IDENT if charset haven't been found.
945
So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
949
if (yylval->lex_str.str[0] == '_')
951
CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
952
MY_CS_PRIMARY, MYF(0));
956
lip->m_underscore_cs= cs;
958
lip->body_utf8_append(lip->m_cpp_text_start,
959
lip->get_cpp_tok_start() + length);
960
return(UNDERSCORE_CHARSET);
964
lip->body_utf8_append(lip->m_cpp_text_start);
966
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
967
lip->m_cpp_text_end);
969
return(result_state); // IDENT or IDENT_QUOTED
971
case MY_LEX_IDENT_SEP: // Found ident and now '.'
972
yylval->lex_str.str= (char*) lip->get_ptr();
973
yylval->lex_str.length= 1;
974
c= lip->yyGet(); // should be '.'
975
lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
976
if (!ident_map[(uint8_t)lip->yyPeek()]) // Probably ` or "
977
lip->next_state= MY_LEX_START;
980
case MY_LEX_NUMBER_IDENT: // number or ident which num-start
981
if (lip->yyGetLast() == '0')
986
while (my_isxdigit(cs,(c = lip->yyGet()))) ;
987
if ((lip->yyLength() >= 3) && !ident_map[c])
990
yylval->lex_str=get_token(lip, 2, lip->yyLength()-2);
994
state= MY_LEX_IDENT_START;
999
while ((c= lip->yyGet()) == '0' || c == '1') {};
1000
if ((lip->yyLength() >= 3) && !ident_map[c])
1003
yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
1007
state= MY_LEX_IDENT_START;
1013
while (my_isdigit(cs, (c = lip->yyGet()))) ;
1015
{ // Can't be identifier
1016
state=MY_LEX_INT_OR_REAL;
1019
if (c == 'e' || c == 'E')
1021
// The following test is written this way to allow numbers of type 1e1
1022
if (my_isdigit(cs,lip->yyPeek()) ||
1023
(c=(lip->yyGet())) == '+' || c == '-')
1025
if (my_isdigit(cs,lip->yyPeek())) // Number must have digit after sign
1028
while (my_isdigit(cs,lip->yyGet())) ;
1029
yylval->lex_str=get_token(lip, 0, lip->yyLength());
1036
case MY_LEX_IDENT_START: // We come here after '.'
1037
result_state= IDENT;
1038
#if defined(USE_MB) && defined(USE_MB_IDENT)
1041
result_state= IDENT_QUOTED;
1042
while (ident_map[c=lip->yyGet()])
1044
if (my_mbcharlen(cs, c) > 1)
1047
if ((l = my_ismbchar(cs,
1049
lip->get_end_of_query())) == 0)
1051
lip->skip_binary(l-1);
1058
for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1059
/* If there were non-ASCII characters, mark that we must convert */
1060
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1062
if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
1063
lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
1065
yylval->lex_str= get_token(lip, 0, lip->yyLength());
1067
lip->body_utf8_append(lip->m_cpp_text_start);
1069
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1070
lip->m_cpp_text_end);
1072
return(result_state);
1074
case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char
1076
uint double_quotes= 0;
1077
char quote_char= c; // Used char
1078
while ((c=lip->yyGet()))
1081
if ((var_length= my_mbcharlen(cs, c)) == 1)
1083
if (c == quote_char)
1085
if (lip->yyPeek() != quote_char)
1093
else if (var_length < 1)
1095
lip->skip_binary(var_length-1);
1099
yylval->lex_str=get_quoted_token(lip, 1,
1100
lip->yyLength() - double_quotes -1,
1103
yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
1104
if (c == quote_char)
1105
lip->yySkip(); // Skip end `
1106
lip->next_state= MY_LEX_START;
1108
lip->body_utf8_append(lip->m_cpp_text_start);
1110
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1111
lip->m_cpp_text_end);
1113
return(IDENT_QUOTED);
1115
case MY_LEX_INT_OR_REAL: // Complete int or incomplete real
1117
{ // Found complete integer number.
1118
yylval->lex_str=get_token(lip, 0, lip->yyLength());
1119
return int_token(yylval->lex_str.str,yylval->lex_str.length);
1122
case MY_LEX_REAL: // Incomplete real number
1123
while (my_isdigit(cs,c = lip->yyGet())) ;
1125
if (c == 'e' || c == 'E')
1128
if (c == '-' || c == '+')
1129
c = lip->yyGet(); // Skip sign
1130
if (!my_isdigit(cs,c))
1131
{ // No digit after sign
1135
while (my_isdigit(cs,lip->yyGet())) ;
1136
yylval->lex_str=get_token(lip, 0, lip->yyLength());
1139
yylval->lex_str=get_token(lip, 0, lip->yyLength());
1140
return(DECIMAL_NUM);
1142
case MY_LEX_HEX_NUMBER: // Found x'hexstring'
1143
lip->yySkip(); // Accept opening '
1144
while (my_isxdigit(cs, (c= lip->yyGet()))) ;
1146
return(ABORT_SYM); // Illegal hex constant
1147
lip->yySkip(); // Accept closing '
1148
length= lip->yyLength(); // Length of hexnum+3
1149
if ((length % 2) == 0)
1150
return(ABORT_SYM); // odd number of hex digits
1151
yylval->lex_str=get_token(lip,
1153
length-3); // don't count x' and last '
1156
case MY_LEX_BIN_NUMBER: // Found b'bin-string'
1157
lip->yySkip(); // Accept opening '
1158
while ((c= lip->yyGet()) == '0' || c == '1') {};
1160
return(ABORT_SYM); // Illegal hex constant
1161
lip->yySkip(); // Accept closing '
1162
length= lip->yyLength(); // Length of bin-num + 3
1163
yylval->lex_str= get_token(lip,
1165
length-3); // don't count b' and last '
1168
case MY_LEX_CMP_OP: // Incomplete comparison operator
1169
if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
1170
state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
1172
if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1174
lip->next_state= MY_LEX_START; // Allow signed numbers
1177
state = MY_LEX_CHAR; // Something fishy found
1180
case MY_LEX_LONG_CMP_OP: // Incomplete comparison operator
1181
if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
1182
state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
1185
if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP)
1188
if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1190
lip->next_state= MY_LEX_START; // Found long op
1193
state = MY_LEX_CHAR; // Something fishy found
1197
if (c != lip->yyPeek())
1203
tokval = find_keyword(lip,2,0); // Is a bool operator
1204
lip->next_state= MY_LEX_START; // Allow signed numbers
1207
case MY_LEX_STRING_OR_DELIMITER:
1210
state= MY_LEX_USER_VARIABLE_DELIMITER;
1213
/* " used for strings */
1214
case MY_LEX_STRING: // Incomplete text string
1215
if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
1217
state= MY_LEX_CHAR; // Read char by char
1220
yylval->lex_str.length=lip->yytoklen;
1222
lip->body_utf8_append(lip->m_cpp_text_start);
1224
lip->body_utf8_append_literal(thd, &yylval->lex_str,
1225
lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1226
lip->m_cpp_text_end);
1228
lip->m_underscore_cs= NULL;
1230
lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1231
return(TEXT_STRING);
1233
case MY_LEX_COMMENT: // Comment
1234
lex->select_lex.options|= OPTION_FOUND_COMMENT;
1235
while ((c = lip->yyGet()) != '\n' && c) ;
1236
lip->yyUnget(); // Safety against eof
1237
state = MY_LEX_START; // Try again
1239
case MY_LEX_LONG_COMMENT: /* Long C comment? */
1240
if (lip->yyPeek() != '*')
1242
state=MY_LEX_CHAR; // Probable division
1245
lex->select_lex.options|= OPTION_FOUND_COMMENT;
1246
/* Reject '/' '*', since we might need to turn off the echo */
1249
if (lip->yyPeekn(2) == '!')
1251
lip->in_comment= DISCARD_COMMENT;
1252
/* Accept '/' '*' '!', but do not keep this marker. */
1253
lip->set_echo(FALSE);
1259
The special comment format is very strict:
1260
'/' '*' '!', followed by exactly
1261
1 digit (major), 2 digits (minor), then 2 digits (dot).
1266
char version_str[6];
1267
version_str[0]= lip->yyPeekn(0);
1268
version_str[1]= lip->yyPeekn(1);
1269
version_str[2]= lip->yyPeekn(2);
1270
version_str[3]= lip->yyPeekn(3);
1271
version_str[4]= lip->yyPeekn(4);
1273
if ( my_isdigit(cs, version_str[0])
1274
&& my_isdigit(cs, version_str[1])
1275
&& my_isdigit(cs, version_str[2])
1276
&& my_isdigit(cs, version_str[3])
1277
&& my_isdigit(cs, version_str[4])
1281
version=strtol(version_str, NULL, 10);
1283
/* Accept 'M' 'm' 'm' 'd' 'd' */
1286
if (version <= MYSQL_VERSION_ID)
1288
/* Expand the content of the special comment as real code */
1289
lip->set_echo(TRUE);
1297
lip->set_echo(TRUE);
1303
lip->in_comment= PRESERVE_COMMENT;
1304
lip->yySkip(); // Accept /
1305
lip->yySkip(); // Accept *
1309
- regular '/' '*' comments,
1310
- special comments '/' '*' '!' for a future version,
1311
by scanning until we find a closing '*' '/' marker.
1312
Note: There is no such thing as nesting comments,
1313
the first '*' '/' sequence seen will mark the end.
1315
comment_closed= FALSE;
1316
while (! lip->eof())
1321
if (lip->yyPeek() == '/')
1324
comment_closed= TRUE;
1325
state = MY_LEX_START;
1332
/* Unbalanced comments with a missing '*' '/' are a syntax error */
1333
if (! comment_closed)
1335
state = MY_LEX_START; // Try again
1336
lip->in_comment= NO_COMMENT;
1337
lip->set_echo(TRUE);
1339
case MY_LEX_END_LONG_COMMENT:
1340
if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1342
/* Reject '*' '/' */
1344
/* Accept '*' '/', with the proper echo */
1345
lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1347
/* And start recording the tokens again */
1348
lip->set_echo(TRUE);
1349
lip->in_comment=NO_COMMENT;
1353
state=MY_LEX_CHAR; // Return '*'
1355
case MY_LEX_SET_VAR: // Check if ':='
1356
if (lip->yyPeek() != '=')
1358
state=MY_LEX_CHAR; // Return ':'
1363
case MY_LEX_SEMICOLON: // optional line terminator
1366
if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) &&
1367
!lip->stmt_prepare_mode)
1369
lip->found_semicolon= lip->get_ptr();
1370
thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
1371
lip->next_state= MY_LEX_END;
1372
lip->set_echo(TRUE);
1373
return (END_OF_INPUT);
1375
state= MY_LEX_CHAR; // Return ';'
1378
lip->next_state=MY_LEX_END; // Mark for next loop
1379
return(END_OF_INPUT);
1383
lip->yyUnget(); // Reject the last '\0'
1384
lip->set_echo(FALSE);
1386
lip->set_echo(TRUE);
1387
/* Unbalanced comments with a missing '*' '/' are a syntax error */
1388
if (lip->in_comment != NO_COMMENT)
1390
lip->next_state=MY_LEX_END; // Mark for next loop
1391
return(END_OF_INPUT);
1396
lip->next_state=MY_LEX_END;
1397
return(0); // We found end of input last time
1399
/* Actually real shouldn't start with . but allow them anyhow */
1400
case MY_LEX_REAL_OR_POINT:
1401
if (my_isdigit(cs,lip->yyPeek()))
1402
state = MY_LEX_REAL; // Real
1405
state= MY_LEX_IDENT_SEP; // return '.'
1406
lip->yyUnget(); // Put back '.'
1409
case MY_LEX_USER_END: // end '@' of user@hostname
1410
switch (state_map[(uint8_t)lip->yyPeek()]) {
1412
case MY_LEX_USER_VARIABLE_DELIMITER:
1413
case MY_LEX_STRING_OR_DELIMITER:
1415
case MY_LEX_USER_END:
1416
lip->next_state=MY_LEX_SYSTEM_VAR;
1419
lip->next_state=MY_LEX_HOSTNAME;
1422
yylval->lex_str.str=(char*) lip->get_ptr();
1423
yylval->lex_str.length=1;
1425
case MY_LEX_HOSTNAME: // end '@' of user@hostname
1426
for (c=lip->yyGet() ;
1427
my_isalnum(cs,c) || c == '.' || c == '_' || c == '$';
1429
yylval->lex_str=get_token(lip, 0, lip->yyLength());
1430
return(LEX_HOSTNAME);
1431
case MY_LEX_SYSTEM_VAR:
1432
yylval->lex_str.str=(char*) lip->get_ptr();
1433
yylval->lex_str.length=1;
1434
lip->yySkip(); // Skip '@'
1435
lip->next_state= (state_map[(uint8_t)lip->yyPeek()] ==
1436
MY_LEX_USER_VARIABLE_DELIMITER ?
1437
MY_LEX_OPERATOR_OR_IDENT :
1438
MY_LEX_IDENT_OR_KEYWORD);
1440
case MY_LEX_IDENT_OR_KEYWORD:
1442
We come here when we have found two '@' in a row.
1443
We should now be able to handle:
1444
[(global | local | session) .]variable_name
1447
for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1448
/* If there were non-ASCII characters, mark that we must convert */
1449
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1452
lip->next_state=MY_LEX_IDENT_SEP;
1453
length= lip->yyLength();
1455
return(ABORT_SYM); // Names must be nonempty.
1456
if ((tokval= find_keyword(lip, length,0)))
1458
lip->yyUnget(); // Put back 'c'
1459
return(tokval); // Was keyword
1461
yylval->lex_str=get_token(lip, 0, length);
1463
lip->body_utf8_append(lip->m_cpp_text_start);
1465
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1466
lip->m_cpp_text_end);
1468
return(result_state);
1475
Construct a copy of this object to be used for mysql_alter_table
1476
and mysql_create_table.
1478
Historically, these two functions modify their Alter_info
1479
arguments. This behaviour breaks re-execution of prepared
1480
statements and stored procedures and is compensated by always
1481
supplying a copy of Alter_info to these functions.
1483
@return You need to use check the error in THD for out
1484
of memory condition after calling this function.
1487
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
1488
:drop_list(rhs.drop_list, mem_root),
1489
alter_list(rhs.alter_list, mem_root),
1490
key_list(rhs.key_list, mem_root),
1491
create_list(rhs.create_list, mem_root),
1493
keys_onoff(rhs.keys_onoff),
1494
tablespace_op(rhs.tablespace_op),
1495
no_parts(rhs.no_parts),
1496
build_method(rhs.build_method),
1497
datetime_field(rhs.datetime_field),
1498
error_if_not_empty(rhs.error_if_not_empty)
1501
Make deep copies of used objects.
1502
This is not a fully deep copy - clone() implementations
1503
of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
1504
do not copy string constants. At the same length the only
1505
reason we make a copy currently is that ALTER/CREATE TABLE
1506
code changes input Alter_info definitions, but string
1507
constants never change.
1509
list_copy_and_replace_each_value(drop_list, mem_root);
1510
list_copy_and_replace_each_value(alter_list, mem_root);
1511
list_copy_and_replace_each_value(key_list, mem_root);
1512
list_copy_and_replace_each_value(create_list, mem_root);
1516
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
1520
This code assumes that there are no multi-bytes characters
1521
that can be considered white-space.
1524
while ((str->length > 0) && (my_isspace(cs, str->str[0])))
1532
Also, parsing backward is not safe with multi bytes characters
1534
while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
1542
st_select_lex structures initialisations
1545
void st_select_lex_node::init_query()
1548
linkage= UNSPECIFIED_TYPE;
1549
no_error= no_table_names_allowed= 0;
1553
void st_select_lex_node::init_select()
1557
void st_select_lex_unit::init_query()
1559
st_select_lex_node::init_query();
1560
linkage= GLOBAL_OPTIONS_TYPE;
1561
global_parameters= first_select();
1562
select_limit_cnt= HA_POS_ERROR;
1563
offset_limit_cnt= 0;
1565
prepared= optimized= executed= 0;
1573
found_rows_for_union= 0;
1576
void st_select_lex::init_query()
1578
st_select_lex_node::init_query();
1580
top_join_list.empty();
1581
join_list= &top_join_list;
1582
embedding= leaf_tables= 0;
1585
having= prep_having= where= prep_where= 0;
1586
olap= UNSPECIFIED_OLAP_TYPE;
1587
having_fix_field= 0;
1588
context.select_lex= this;
1591
Add the name resolution context of the current (sub)query to the
1592
stack of contexts for the whole query.
1594
push_context may return an error if there is no memory for a new
1595
element in the stack, however this method has no return value,
1596
thus push_context should be moved to a place where query
1597
initialization is checked for failure.
1599
parent_lex->push_context(&context);
1600
cond_count= between_count= with_wild= 0;
1602
conds_processed_with_permanent_arena= 0;
1603
ref_pointer_array= 0;
1604
select_n_where_fields= 0;
1605
select_n_having_items= 0;
1606
subquery_in_having= explicit_limit= 0;
1607
is_item_list_lookup= 0;
1609
first_cond_optimization= 1;
1610
parsing_place= NO_MATTER;
1611
exclude_from_table_unique_test= no_wrap_view_item= FALSE;
1616
void st_select_lex::init_select()
1618
st_select_lex_node::init_select();
1623
table_join_options= 0;
1624
in_sum_expr= with_wild= 0;
1627
interval_list.empty();
1628
ftfunc_list_alloc.empty();
1629
inner_sum_func_list= 0;
1630
ftfunc_list= &ftfunc_list_alloc;
1631
linkage= UNSPECIFIED_TYPE;
1632
order_list.elements= 0;
1633
order_list.first= 0;
1634
order_list.next= (uchar**) &order_list.first;
1635
/* Set limit and offset to default values */
1636
select_limit= 0; /* denotes the default limit = HA_POS_ERROR */
1637
offset_limit= 0; /* denotes the default offset = 0 */
1640
cur_pos_in_select_list= UNDEF_POS;
1641
non_agg_fields.empty();
1642
cond_value= having_value= Item::COND_UNDEF;
1643
inner_refs_list.empty();
1644
full_group_by_flag= 0;
1648
st_select_lex structures linking
1651
/* include on level down */
1652
void st_select_lex_node::include_down(st_select_lex_node *upper)
1654
if ((next= upper->slave))
1656
prev= &upper->slave;
1663
include on level down (but do not link)
1666
st_select_lex_node::include_standalone()
1667
upper - reference on node underr which this node should be included
1668
ref - references on reference on this node
1670
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
1671
st_select_lex_node **ref)
1679
/* include neighbour (on same level) */
1680
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1682
if ((next= before->next))
1684
prev= &before->next;
1686
master= before->master;
1690
/* including in global SELECT_LEX list */
1691
void st_select_lex_node::include_global(st_select_lex_node **plink)
1693
if ((link_next= *plink))
1694
link_next->link_prev= &link_next;
1699
//excluding from global list (internal function)
1700
void st_select_lex_node::fast_exclude()
1704
if ((*link_prev= link_next))
1705
link_next->link_prev= link_prev;
1707
// Remove slave structure
1708
for (; slave; slave= slave->next)
1709
slave->fast_exclude();
1714
excluding select_lex structure (except first (first select can't be
1715
deleted, because it is most upper select))
1717
void st_select_lex_node::exclude()
1719
//exclude from global list
1721
//exclude from other structures
1725
We do not need following statements, because prev pointer of first
1726
list element point to master->slave
1727
if (master->slave == this)
1728
master->slave= next;
1734
Exclude level of current unit from tree of SELECTs
1737
st_select_lex_unit::exclude_level()
1739
NOTE: units which belong to current will be brought up on level of
1742
void st_select_lex_unit::exclude_level()
1744
SELECT_LEX_UNIT *units= 0, **units_last= &units;
1745
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1747
// unlink current level from global SELECTs list
1748
if (sl->link_prev && (*sl->link_prev= sl->link_next))
1749
sl->link_next->link_prev= sl->link_prev;
1751
// bring up underlay levels
1752
SELECT_LEX_UNIT **last= 0;
1753
for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1756
last= (SELECT_LEX_UNIT**)&(u->next);
1760
(*units_last)= sl->first_inner_unit();
1766
// include brought up levels in place of current
1768
(*units_last)= (SELECT_LEX_UNIT*)next;
1770
next->prev= (SELECT_LEX_NODE**)units_last;
1775
// exclude currect unit from list of nodes
1784
Exclude subtree of current unit from tree of SELECTs
1787
st_select_lex_unit::exclude_tree()
1789
void st_select_lex_unit::exclude_tree()
1791
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1793
// unlink current level from global SELECTs list
1794
if (sl->link_prev && (*sl->link_prev= sl->link_next))
1795
sl->link_next->link_prev= sl->link_prev;
1797
// unlink underlay levels
1798
for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1803
// exclude currect unit from list of nodes
1811
st_select_lex_node::mark_as_dependent mark all st_select_lex struct from
1812
this to 'last' as dependent
1815
last - pointer to last st_select_lex struct, before wich all
1816
st_select_lex have to be marked as dependent
1819
'last' should be reachable from this st_select_lex_node
1822
void st_select_lex::mark_as_dependent(st_select_lex *last)
1825
Mark all selects from resolved to 1 before select where was
1826
found table as depended (of select where was found table)
1828
for (SELECT_LEX *s= this;
1830
s= s->outer_select())
1832
if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1834
// Select is dependent of outer select
1835
s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1836
UNCACHEABLE_DEPENDENT;
1837
SELECT_LEX_UNIT *munit= s->master_unit();
1838
munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1839
UNCACHEABLE_DEPENDENT;
1840
for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1843
!(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1844
sl->uncacheable|= UNCACHEABLE_UNITED;
1847
s->is_correlated= TRUE;
1848
Item_subselect *subquery_predicate= s->master_unit()->item;
1849
if (subquery_predicate)
1850
subquery_predicate->is_correlated= TRUE;
1854
bool st_select_lex_node::set_braces(bool value) { return 1; }
1855
bool st_select_lex_node::inc_in_sum_expr() { return 1; }
1856
uint st_select_lex_node::get_in_sum_expr() { return 0; }
1857
TABLE_LIST* st_select_lex_node::get_table_list() { return 0; }
1858
List<Item>* st_select_lex_node::get_item_list() { return 0; }
1859
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
1861
ulong table_join_options,
1862
thr_lock_type flags,
1863
List<Index_hint> *hints,
1868
ulong st_select_lex_node::get_table_join_options()
1874
prohibit using LIMIT clause
1876
bool st_select_lex::test_limit()
1878
if (select_limit != 0)
1880
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1881
"LIMIT & IN/ALL/ANY/SOME subquery");
1888
st_select_lex_unit* st_select_lex_unit::master_unit()
1894
st_select_lex* st_select_lex_unit::outer_select()
1896
return (st_select_lex*) master;
1900
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
1902
return add_to_list(thd, order_list, item, asc);
1906
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
1908
DBUG_ENTER("st_select_lex::add_item_to_list");
1909
DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
1910
DBUG_RETURN(item_list.push_back(item));
1914
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
1916
return add_to_list(thd, group_list, item, asc);
1920
st_select_lex_unit* st_select_lex::master_unit()
1922
return (st_select_lex_unit*) master;
1926
st_select_lex* st_select_lex::outer_select()
1928
return (st_select_lex*) master->get_master();
1932
bool st_select_lex::set_braces(bool value)
1939
bool st_select_lex::inc_in_sum_expr()
1946
uint st_select_lex::get_in_sum_expr()
1952
TABLE_LIST* st_select_lex::get_table_list()
1954
return (TABLE_LIST*) table_list.first;
1957
List<Item>* st_select_lex::get_item_list()
1962
ulong st_select_lex::get_table_join_options()
1964
return table_join_options;
1968
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1970
if (ref_pointer_array)
1974
We have to create array in prepared statement memory if it is
1977
Query_arena *arena= thd->stmt_arena;
1978
return (ref_pointer_array=
1979
(Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1980
item_list.elements +
1981
select_n_having_items +
1982
select_n_where_fields +
1983
order_group_num)*5)) == 0;
1987
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1989
bool union_all= !union_distinct;
1990
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1992
if (sl != first_select())
1994
str->append(STRING_WITH_LEN(" union "));
1996
str->append(STRING_WITH_LEN("all "));
1997
else if (union_distinct == sl)
2002
sl->print(thd, str, query_type);
2006
if (fake_select_lex == global_parameters)
2008
if (fake_select_lex->order_list.elements)
2010
str->append(STRING_WITH_LEN(" order by "));
2011
fake_select_lex->print_order(
2013
(ORDER *) fake_select_lex->order_list.first,
2016
fake_select_lex->print_limit(thd, str, query_type);
2021
void st_select_lex::print_order(String *str,
2023
enum_query_type query_type)
2025
for (; order; order= order->next)
2027
if (order->counter_used)
2030
uint length= my_snprintf(buffer, 20, "%d", order->counter);
2031
str->append(buffer, length);
2034
(*order->item)->print(str, query_type);
2036
str->append(STRING_WITH_LEN(" desc"));
2043
void st_select_lex::print_limit(THD *thd,
2045
enum_query_type query_type)
2047
SELECT_LEX_UNIT *unit= master_unit();
2048
Item_subselect *item= unit->item;
2050
if (item && unit->global_parameters == this)
2052
Item_subselect::subs_type subs_type= item->substype();
2053
if (subs_type == Item_subselect::EXISTS_SUBS ||
2054
subs_type == Item_subselect::IN_SUBS ||
2055
subs_type == Item_subselect::ALL_SUBS)
2057
DBUG_ASSERT(!item->fixed ||
2059
If not using materialization both:
2060
select_limit == 1, and there should be no offset_limit.
2062
(((subs_type == Item_subselect::IN_SUBS) &&
2063
((Item_in_subselect*)item)->exec_method ==
2064
Item_in_subselect::MATERIALIZATION) ?
2066
(select_limit->val_int() == LL(1)) &&
2067
offset_limit == 0));
2073
str->append(STRING_WITH_LEN(" limit "));
2076
offset_limit->print(str, query_type);
2079
select_limit->print(str, query_type);
2084
@brief Restore the LEX and THD in case of a parse error.
2086
This is a clean up call that is invoked by the Bison generated
2087
parser before returning an error from MYSQLparse. If your
2088
semantic actions manipulate with the global thread state (which
2089
is a very bad practice and should not normally be employed) and
2090
need a clean-up in case of error, and you can not use %destructor
2091
rule in the grammar file itself, this function should be used
2092
to implement the clean up.
2095
void st_lex::cleanup_lex_after_parse_error(THD *thd)
2100
Initialize (or reset) Query_tables_list object.
2103
reset_query_tables_list()
2104
init TRUE - we should perform full initialization of object with
2105
allocating needed memory
2106
FALSE - object is already initialized so we should only reset
2107
its state so it can be used for parsing/processing
2111
This method initializes Query_tables_list so it can be used as part
2112
of LEX object for parsing/processing of statement. One can also use
2113
this method to reset state of already initialized Query_tables_list
2114
so it can be used for processing of new statement.
2117
void Query_tables_list::reset_query_tables_list(bool init)
2119
if (!init && query_tables)
2121
TABLE_LIST *table= query_tables;
2124
if (query_tables_last == &table->next_global ||
2125
!(table= table->next_global))
2130
query_tables_last= &query_tables;
2131
query_tables_own_last= 0;
2135
We delay real initialization of hash (and therefore related
2136
memory allocation) until first insertion into this hash.
2138
hash_clear(&sroutines);
2140
else if (sroutines.records)
2142
/* Non-zero sroutines.records means that hash was initialized. */
2143
my_hash_reset(&sroutines);
2145
sroutines_list.empty();
2146
sroutines_list_own_last= sroutines_list.next;
2147
sroutines_list_own_elements= 0;
2148
binlog_stmt_flags= 0;
2153
Destroy Query_tables_list object with freeing all resources used by it.
2156
destroy_query_tables_list()
2159
void Query_tables_list::destroy_query_tables_list()
2161
hash_free(&sroutines);
2166
Initialize LEX object.
2172
LEX object initialized with this constructor can be used as part of
2173
THD object for which one can safely call open_tables(), lock_tables()
2174
and close_thread_tables() functions. But it is not yet ready for
2175
statement parsing. On should use lex_start() function to prepare LEX
2180
:result(0), yacc_yyss(0), yacc_yyvs(0),
2181
sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2184
my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2185
plugins_static_buffer,
2186
INITIAL_LEX_PLUGIN_LIST_SIZE,
2187
INITIAL_LEX_PLUGIN_LIST_SIZE);
2188
reset_query_tables_list(TRUE);
2193
Check whether the merging algorithm can be used on this VIEW
2196
st_lex::can_be_merged()
2199
We can apply merge algorithm if it is single SELECT view with
2200
subqueries only in WHERE clause (we do not count SELECTs of underlying
2201
views, and second level subqueries) and we have not grpouping, ordering,
2202
HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2203
several underlying tables.
2206
FALSE - only temporary table algorithm can be used
2207
TRUE - merge algorithm can be used
2210
bool st_lex::can_be_merged()
2212
// TODO: do not forget implement case when select_lex.table_list.elements==0
2214
/* find non VIEW subqueries/unions */
2215
bool selects_allow_merge= select_lex.next_select() == 0;
2216
if (selects_allow_merge)
2218
for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2220
tmp_unit= tmp_unit->next_unit())
2222
if (tmp_unit->first_select()->parent_lex == this &&
2223
(tmp_unit->item == 0 ||
2224
(tmp_unit->item->place() != IN_WHERE &&
2225
tmp_unit->item->place() != IN_ON)))
2227
selects_allow_merge= 0;
2233
return (selects_allow_merge &&
2234
select_lex.group_list.elements == 0 &&
2235
select_lex.having == 0 &&
2236
select_lex.with_sum_func == 0 &&
2237
select_lex.table_list.elements >= 1 &&
2238
!(select_lex.options & SELECT_DISTINCT) &&
2239
select_lex.select_limit == 0);
2244
check if command can use VIEW with MERGE algorithm (for top VIEWs)
2247
st_lex::can_use_merged()
2250
Only listed here commands can use merge algorithm in top level
2251
SELECT_LEX (for subqueries will be used merge algorithm if
2252
st_lex::can_not_use_merged() is not TRUE).
2255
FALSE - command can't use merged VIEWs
2256
TRUE - VIEWs with MERGE algorithms can be used
2259
bool st_lex::can_use_merged()
2261
switch (sql_command)
2264
case SQLCOM_CREATE_TABLE:
2266
case SQLCOM_UPDATE_MULTI:
2268
case SQLCOM_DELETE_MULTI:
2270
case SQLCOM_INSERT_SELECT:
2271
case SQLCOM_REPLACE:
2272
case SQLCOM_REPLACE_SELECT:
2281
Check if command can't use merged views in any part of command
2284
st_lex::can_not_use_merged()
2287
Temporary table algorithm will be used on all SELECT levels for queries
2288
listed here (see also st_lex::can_use_merged()).
2291
FALSE - command can't use merged VIEWs
2292
TRUE - VIEWs with MERGE algorithms can be used
2295
bool st_lex::can_not_use_merged()
2297
switch (sql_command)
2300
SQLCOM_SHOW_FIELDS is necessary to make
2301
information schema tables working correctly with views.
2302
see get_schema_tables_result function
2304
case SQLCOM_SHOW_FIELDS:
2312
Detect that we need only table structure of derived table/view
2315
only_view_structure()
2318
TRUE yes, we need only structure
2319
FALSE no, we need data
2322
bool st_lex::only_view_structure()
2324
switch (sql_command) {
2325
case SQLCOM_SHOW_CREATE:
2326
case SQLCOM_SHOW_TABLES:
2327
case SQLCOM_SHOW_FIELDS:
2336
Should Items_ident be printed correctly
2339
need_correct_ident()
2342
TRUE yes, we need only structure
2343
FALSE no, we need data
2347
bool st_lex::need_correct_ident()
2351
case SQLCOM_SHOW_CREATE:
2352
case SQLCOM_SHOW_TABLES:
2360
Get effective type of CHECK OPTION for given view
2363
get_effective_with_check()
2367
It have not sense to set CHECK OPTION for SELECT satement or subqueries,
2371
VIEW_CHECK_NONE no need CHECK OPTION
2372
VIEW_CHECK_LOCAL CHECK OPTION LOCAL
2373
VIEW_CHECK_CASCADED CHECK OPTION CASCADED
2376
uint8 st_lex::get_effective_with_check(TABLE_LIST *view)
2383
This method should be called only during parsing.
2384
It is aware of compound statements (stored routine bodies)
2385
and will initialize the destination with the default
2386
database of the stored routine, rather than the default
2387
database of the connection it is parsed in.
2388
E.g. if one has no current database selected, or current database
2389
set to 'bar' and then issues:
2391
CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2393
t1 is meant to refer to foo.t1, not to bar.t1.
2395
This method is needed to support this rule.
2397
@return TRUE in case of error (parsing should be aborted, FALSE in
2402
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
2404
return thd->copy_db_to(p_db, p_db_length);
2408
initialize limit counters
2411
st_select_lex_unit::set_limit()
2412
values - SELECT_LEX with initial values for counters
2415
void st_select_lex_unit::set_limit(st_select_lex *sl)
2417
ha_rows select_limit_val;
2420
val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2421
select_limit_val= (ha_rows)val;
2424
Check for overflow : ha_rows can be smaller then ulonglong if
2427
if (val != (ulonglong)select_limit_val)
2428
select_limit_val= HA_POS_ERROR;
2430
offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2432
select_limit_cnt= select_limit_val + offset_limit_cnt;
2433
if (select_limit_cnt < select_limit_val)
2434
select_limit_cnt= HA_POS_ERROR; // no limit
2439
Unlink the first table from the global table list and the first table from
2440
outer select (lex->select_lex) local list
2443
unlink_first_table()
2444
link_to_local Set to 1 if caller should link this table to local list
2447
We assume that first tables in both lists is the same table or the local
2451
0 If 'query_tables' == 0
2453
In this case link_to_local is set.
2456
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
2459
if ((first= query_tables))
2462
Exclude from global table list
2464
if ((query_tables= query_tables->next_global))
2465
query_tables->prev_global= &query_tables;
2467
query_tables_last= &query_tables;
2468
first->next_global= 0;
2471
and from local list if it is not empty
2473
if ((*link_to_local= test(select_lex.table_list.first)))
2475
select_lex.context.table_list=
2476
select_lex.context.first_name_resolution_table= first->next_local;
2477
select_lex.table_list.first= (uchar*) (first->next_local);
2478
select_lex.table_list.elements--; //safety
2479
first->next_local= 0;
2481
Ensure that the global list has the same first table as the local
2484
first_lists_tables_same();
2492
Bring first local table of first most outer select to first place in global
2496
st_lex::first_lists_tables_same()
2499
In many cases (for example, usual INSERT/DELETE/...) the first table of
2500
main SELECT_LEX have special meaning => check that it is the first table
2501
in global list and re-link to be first in the global list if it is
2502
necessary. We need such re-linking only for queries with sub-queries in
2503
the select list, as only in this case tables of sub-queries will go to
2504
the global list first.
2507
void st_lex::first_lists_tables_same()
2509
TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
2510
if (query_tables != first_table && first_table != 0)
2513
if (query_tables_last == &first_table->next_global)
2514
query_tables_last= first_table->prev_global;
2516
if ((next= *first_table->prev_global= first_table->next_global))
2517
next->prev_global= first_table->prev_global;
2518
/* include in new place */
2519
first_table->next_global= query_tables;
2521
We are sure that query_tables is not 0, because first_table was not
2522
first table in the global list => we can use
2523
query_tables->prev_global without check of query_tables
2525
query_tables->prev_global= &first_table->next_global;
2526
first_table->prev_global= &query_tables;
2527
query_tables= first_table;
2533
Link table back that was unlinked with unlink_first_table()
2536
link_first_table_back()
2537
link_to_local do we need link this table to local
2543
void st_lex::link_first_table_back(TABLE_LIST *first,
2548
if ((first->next_global= query_tables))
2549
query_tables->prev_global= &first->next_global;
2551
query_tables_last= &first->next_global;
2552
query_tables= first;
2556
first->next_local= (TABLE_LIST*) select_lex.table_list.first;
2557
select_lex.context.table_list= first;
2558
select_lex.table_list.first= (uchar*) first;
2559
select_lex.table_list.elements++; //safety
2567
cleanup lex for case when we open table by table for processing
2570
st_lex::cleanup_after_one_table_open()
2573
This method is mostly responsible for cleaning up of selects lists and
2574
derived tables state. To rollback changes in Query_tables_list one has
2575
to call Query_tables_list::reset_query_tables_list(FALSE).
2578
void st_lex::cleanup_after_one_table_open()
2581
thd->lex->derived_tables & additional units may be set if we open
2582
a view. It is necessary to clear thd->lex->derived_tables flag
2583
to prevent processing of derived tables during next open_and_lock_tables
2584
if next table is a real table and cleanup & remove underlying units
2585
NOTE: all units will be connected to thd->lex->select_lex, because we
2586
have not UNION on most upper level.
2588
if (all_selects_list != &select_lex)
2591
/* cleunup underlying units (units of VIEW) */
2592
for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
2594
un= un->next_unit())
2596
/* reduce all selects list to default state */
2597
all_selects_list= &select_lex;
2598
/* remove underlying units (units of VIEW) subtree */
2599
select_lex.cut_subtree();
2605
Save current state of Query_tables_list for this LEX, and prepare it
2606
for processing of new statemnt.
2609
reset_n_backup_query_tables_list()
2610
backup Pointer to Query_tables_list instance to be used for backup
2613
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup)
2619
Restore state of Query_tables_list for this LEX from backup.
2622
restore_backup_query_tables_list()
2623
backup Pointer to Query_tables_list instance used for backup
2626
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup)
2632
Checks for usage of routines and/or tables in a parsed statement
2635
st_lex:table_or_sp_used()
2638
FALSE No routines and tables used
2639
TRUE Either or both routines and tables are used.
2642
bool st_lex::table_or_sp_used()
2644
DBUG_ENTER("table_or_sp_used");
2646
if (sroutines.records || query_tables)
2654
Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2657
fix_prepare_info_in_table_list()
2659
tbl List of tables to process
2662
Perform end-end-of prepare fixup for list of tables, if any of the tables
2663
is a merge-algorithm VIEW, recursively fix up its underlying tables as
2668
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
2670
for (; tbl; tbl= tbl->next_local)
2674
tbl->prep_on_expr= tbl->on_expr;
2675
tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
2677
fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
2683
Save WHERE/HAVING/ON clauses and replace them with disposable copies
2686
st_select_lex::fix_prepare_information
2688
conds in/out pointer to WHERE condition to be met at execution
2689
having_conds in/out pointer to HAVING condition to be met at execution
2692
The passed WHERE and HAVING are to be saved for the future executions.
2693
This function saves it, and returns a copy which can be thrashed during
2694
this execution of the statement. By saving/thrashing here we mean only
2696
The function also calls fix_prepare_info_in_table_list that saves all
2700
void st_select_lex::fix_prepare_information(THD *thd, Item **conds,
2701
Item **having_conds)
2703
if (thd->stmt_arena->is_conventional() == false && first_execution)
2709
*conds= where= prep_where->copy_andor_structure(thd);
2713
prep_having= *having_conds;
2714
*having_conds= having= prep_having->copy_andor_structure(thd);
2716
fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
2722
There are st_select_lex::add_table_to_list &
2723
st_select_lex::set_lock_for_tables are in sql_parse.cc
2725
st_select_lex::print is in sql_select.cc
2727
st_select_lex_unit::prepare, st_select_lex_unit::exec,
2728
st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2729
st_select_lex_unit::change_result
2734
Sets the kind of hints to be added by the calls to add_index_hint().
2737
set_index_hint_type()
2738
type_arg The kind of hints to be added from now on.
2739
clause The clause to use for hints to be added from now on.
2742
Used in filling up the tagged hints list.
2743
This list is filled by first setting the kind of the hint as a
2744
context variable and then adding hints of the current kind.
2745
Then the context variable index_hint_type can be reset to the
2748
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
2749
index_clause_map clause)
2751
current_index_hint_type= type_arg;
2752
current_index_hint_clause= clause;
2757
Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
2764
void st_select_lex::alloc_index_hints (THD *thd)
2766
index_hints= new (thd->mem_root) List<Index_hint>();
2772
adds an element to the array storing index usage hints
2773
(ADD/FORCE/IGNORE INDEX).
2778
str name of the index.
2779
length number of characters in str.
2782
0 on success, non-zero otherwise
2784
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2786
return index_hints->push_front (new (thd->mem_root)
2787
Index_hint(current_index_hint_type,
2788
current_index_hint_clause,