23 #include "sql_class.h"
25 #include "sql_parse.h"
26 #include "item_create.h"
31 #include "sql_table.h"
34 #include "sql_optimizer.h"
37 static int lex_one_token(
void *arg,
void *yythd);
50 const LEX_STRING empty_lex_str= {(
char *)
"", 0};
56 Query_tables_list::binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT] =
58 ER_BINLOG_UNSAFE_LIMIT,
59 ER_BINLOG_UNSAFE_INSERT_DELAYED,
60 ER_BINLOG_UNSAFE_SYSTEM_TABLE,
61 ER_BINLOG_UNSAFE_AUTOINC_COLUMNS,
63 ER_BINLOG_UNSAFE_SYSTEM_VARIABLE,
64 ER_BINLOG_UNSAFE_SYSTEM_FUNCTION,
65 ER_BINLOG_UNSAFE_NONTRANS_AFTER_TRANS,
66 ER_BINLOG_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
67 ER_BINLOG_UNSAFE_MIXED_STATEMENT,
68 ER_BINLOG_UNSAFE_INSERT_IGNORE_SELECT,
69 ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE,
70 ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT,
71 ER_BINLOG_UNSAFE_REPLACE_SELECT,
72 ER_BINLOG_UNSAFE_CREATE_IGNORE_SELECT,
73 ER_BINLOG_UNSAFE_CREATE_REPLACE_SELECT,
74 ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC,
75 ER_BINLOG_UNSAFE_UPDATE_IGNORE,
76 ER_BINLOG_UNSAFE_INSERT_TWO_KEYS,
77 ER_BINLOG_UNSAFE_AUTOINC_NOT_FIRST
83 #define TOCK_NAME_LENGTH 24
90 static uchar to_upper_lex[]=
92 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
93 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
94 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
95 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
96 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
97 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
98 96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
99 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
100 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
101 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
102 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
103 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
104 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
105 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
106 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
107 208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
115 const char * index_hint_type_name[] =
127 const char *st_select_lex::type_str[SLT_total]=
139 inline int lex_casecmp(
const char *s,
const char *t, uint len)
142 to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
146 #include <lex_hash.h>
152 DBUG_ENTER(
"lex_init");
153 for (i=0 ; i < array_elements(symbols) ; i++)
154 symbols[i].length=(uchar) strlen(symbols[i].
name);
155 for (i=0 ; i < array_elements(sql_functions) ; i++)
156 sql_functions[i].length=(uchar) strlen(sql_functions[i].
name);
164 DBUG_ENTER(
"lex_free");
170 st_parsing_options::reset()
172 allows_variable= TRUE;
173 allows_select_into= TRUE;
174 allows_select_procedure= TRUE;
175 allows_derived= TRUE;
181 void struct_slave_connection::reset()
198 bool Lex_input_stream::init(THD *thd,
202 DBUG_EXECUTE_IF(
"bug42064_simulate_oom",
203 DBUG_SET(
"+d,simulate_out_of_memory"););
205 m_cpp_buf= (
char*) thd->alloc(length + 1);
207 DBUG_EXECUTE_IF(
"bug42064_simulate_oom",
208 DBUG_SET(
"-d,bug42064_simulate_oom"););
210 if (m_cpp_buf == NULL)
229 Lex_input_stream::reset(
char *buffer,
unsigned int length)
235 lookahead_yylval= NULL;
239 m_end_of_query= buffer + length;
240 m_tok_start_prev= NULL;
242 m_buf_length= length;
244 m_cpp_tok_start= NULL;
245 m_cpp_tok_start_prev= NULL;
248 m_cpp_utf8_processed_ptr= NULL;
249 next_state= MY_LEX_START;
250 found_semicolon= NULL;
251 ignore_space=
test(m_thd->variables.sql_mode & MODE_IGNORE_SPACE);
252 stmt_prepare_mode= FALSE;
253 multi_statements= TRUE;
254 in_comment=NO_COMMENT;
255 m_underscore_cs= NULL;
256 m_cpp_ptr= m_cpp_buf;
272 void Lex_input_stream::body_utf8_start(THD *thd,
const char *begin_ptr)
274 DBUG_ASSERT(begin_ptr);
275 DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
277 uint body_utf8_length=
278 (m_buf_length / thd->variables.character_set_client->mbminlen) *
279 my_charset_utf8_bin.mbmaxlen;
281 m_body_utf8= (
char *) thd->alloc(body_utf8_length + 1);
282 m_body_utf8_ptr= m_body_utf8;
285 m_cpp_utf8_processed_ptr= begin_ptr;
309 void Lex_input_stream::body_utf8_append(
const char *ptr,
312 DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
313 DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
318 if (m_cpp_utf8_processed_ptr >= ptr)
321 int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
323 memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
324 m_body_utf8_ptr += bytes_to_copy;
327 m_cpp_utf8_processed_ptr= end_ptr;
338 void Lex_input_stream::body_utf8_append(
const char *ptr)
340 body_utf8_append(ptr, ptr);
355 void Lex_input_stream::body_utf8_append_literal(THD *thd,
360 if (!m_cpp_utf8_processed_ptr)
365 if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
367 thd->convert_string(&utf_txt,
368 &my_charset_utf8_general_ci,
369 txt->str, (uint) txt->length,
374 utf_txt.str= txt->str;
375 utf_txt.length= txt->length;
380 memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
381 m_body_utf8_ptr += utf_txt.length;
384 m_cpp_utf8_processed_ptr= end_ptr;
394 void lex_start(THD *thd)
397 DBUG_ENTER(
"lex_start");
399 lex->thd= lex->unit.thd= thd;
401 lex->context_stack.empty();
402 lex->unit.init_query();
403 lex->unit.init_select();
405 lex->select_lex.parent_lex= lex;
406 lex->select_lex.init_query();
407 lex->load_set_str_list.empty();
408 lex->value_list.empty();
409 lex->update_list.empty();
410 lex->set_var_list.empty();
411 lex->param_list.empty();
412 lex->view_list.empty();
413 lex->prepared_stmt_params.empty();
414 lex->auxiliary_table_list.empty();
415 lex->unit.next= lex->unit.master= lex->unit.link_next= 0;
416 lex->unit.prev= lex->unit.link_prev= 0;
417 lex->unit.slave= lex->unit.global_parameters= lex->current_select=
418 lex->all_selects_list= &lex->select_lex;
419 lex->select_lex.master= &lex->unit;
420 lex->select_lex.prev= &lex->unit.slave;
421 lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
422 lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
423 lex->select_lex.options= 0;
424 lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
425 lex->select_lex.init_order();
426 lex->select_lex.group_list.empty();
427 if (lex->select_lex.group_list_ptrs)
428 lex->select_lex.group_list_ptrs->clear();
429 lex->describe= DESCRIBE_NONE;
430 lex->subqueries= FALSE;
431 lex->context_analysis_only= 0;
432 lex->derived_tables= 0;
433 lex->safe_to_cache_query= 1;
434 lex->leaf_tables_insert= 0;
435 lex->parsing_options.reset();
436 lex->empty_field_list_on_rset= 0;
437 lex->select_lex.select_number= 1;
440 lex->select_lex.in_sum_expr=0;
441 lex->select_lex.ftfunc_list_alloc.empty();
442 lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
443 lex->select_lex.group_list.empty();
444 lex->select_lex.order_list.empty();
445 if (lex->select_lex.order_list_ptrs)
446 lex->select_lex.order_list_ptrs->clear();
447 lex->duplicates= DUP_ERROR;
451 lex->set_sp_current_parsing_ctx(NULL);
452 lex->m_sql_cmd= NULL;
453 lex->proc_analyse= NULL;
454 lex->escape_used= FALSE;
455 lex->query_tables= 0;
456 lex->reset_query_tables_list(FALSE);
457 lex->expr_allows_subselect= TRUE;
458 lex->use_only_table_context= FALSE;
459 lex->contains_plaintext_password=
false;
463 lex->event_parse_data= NULL;
464 lex->profile_options= PROFILE_NONE;
466 lex->allow_sum_func= 0;
467 lex->in_sum_func= NULL;
473 lex->server_options.server_name= 0;
474 lex->server_options.server_name_length= 0;
475 lex->server_options.host= 0;
476 lex->server_options.db= 0;
477 lex->server_options.username= 0;
478 lex->server_options.password= 0;
479 lex->server_options.scheme= 0;
480 lex->server_options.socket= 0;
481 lex->server_options.owner= 0;
482 lex->server_options.port= -1;
483 lex->explain_format= NULL;
484 lex->is_lex_started= TRUE;
486 lex->reset_slave_info.all=
false;
487 lex->is_change_password=
false;
488 lex->is_set_password_sql=
false;
489 lex->mark_broken(
false);
493 void lex_end(LEX *lex)
495 DBUG_ENTER(
"lex_end");
496 DBUG_PRINT(
"enter", (
"lex: 0x%lx", (
long) lex));
499 if (lex->plugins.elements)
501 plugin_unlock_list(0, (
plugin_ref*)lex->plugins.buffer,
502 lex->plugins.elements);
504 reset_dynamic(&lex->plugins);
512 Yacc_state::~Yacc_state()
521 static int find_keyword(Lex_input_stream *lip, uint len,
bool function)
523 const char *tok= lip->get_tok_start();
525 SYMBOL *symbol= get_hash_symbol(tok, len,
function);
528 lip->yylval->symbol.symbol=symbol;
529 lip->yylval->symbol.str= (
char*) tok;
530 lip->yylval->symbol.length=len;
532 if ((symbol->tok == NOT_SYM) &&
533 (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE))
535 if ((symbol->tok == OR_OR_SYM) &&
536 !(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT))
557 bool is_keyword(
const char *
name, uint len)
559 DBUG_ASSERT(len != 0);
560 return get_hash_symbol(name,len,0)!=0;
575 DBUG_ASSERT(name != NULL);
576 return (get_hash_symbol(name->str, (uint) name->length, 1) != 0);
581 static LEX_STRING get_token(Lex_input_stream *lip, uint
skip, uint length)
585 tmp.length=lip->yytoklen=length;
586 tmp.str= lip->m_thd->strmake(lip->get_tok_start() +
skip, tmp.length);
588 lip->m_cpp_text_start= lip->get_cpp_tok_start() +
skip;
589 lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
601 static LEX_STRING get_quoted_token(Lex_input_stream *lip,
603 uint length,
char quote)
606 const char *from, *end;
609 tmp.length= lip->yytoklen=length;
610 tmp.str=(
char*) lip->m_thd->alloc(tmp.length+1);
611 from= lip->get_tok_start() +
skip;
615 lip->m_cpp_text_start= lip->get_cpp_tok_start() +
skip;
616 lip->m_cpp_text_end= lip->m_cpp_text_start + length;
620 if ((*to++= *from++) == quote)
623 lip->m_cpp_text_start++;
636 static char *get_text(Lex_input_stream *lip,
int pre_skip,
int post_skip)
643 sep= lip->yyGetLast();
654 lip->get_end_of_query()))) {
655 lip->skip_binary(l-1);
661 !(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES))
670 if (c == lip->yyGet())
679 const char *str, *end;
682 str= lip->get_tok_start();
687 DBUG_ASSERT(end >= str);
689 if (!(start= (
char*) lip->m_thd->alloc((uint) (end-str)+1)))
692 lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
693 lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
697 lip->yytoklen=(uint) (end-str);
698 memcpy(start,str,lip->yytoklen);
699 start[lip->yytoklen]=0;
705 for (to=start ; str != end ; str++)
710 (l = my_ismbchar(cs, str, end))) {
717 if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
718 *str ==
'\\' && str+1 != end)
748 else if (*str == sep)
754 lip->yytoklen=(uint) (to-start);
771 static const char *long_str=
"2147483647";
772 static const uint long_len=10;
773 static const char *signed_long_str=
"-2147483648";
774 static const char *longlong_str=
"9223372036854775807";
775 static const uint longlong_len=19;
776 static const char *signed_longlong_str=
"-9223372036854775808";
777 static const uint signed_longlong_len=19;
778 static const char *unsigned_longlong_str=
"18446744073709551615";
779 static const uint unsigned_longlong_len=20;
781 static inline uint int_token(
const char *str,uint length)
783 if (length < long_len)
791 else if (*str ==
'-')
796 while (*str ==
'0' && length)
800 if (length < long_len)
807 if (length == long_len)
809 cmp= signed_long_str+1;
813 else if (length < signed_longlong_len)
815 else if (length > signed_longlong_len)
819 cmp=signed_longlong_str+1;
826 if (length == long_len)
832 else if (length < longlong_len)
834 else if (length > longlong_len)
836 if (length > unsigned_longlong_len)
838 cmp=unsigned_longlong_str;
839 smaller=ULONGLONG_NUM;
846 bigger= ULONGLONG_NUM;
849 while (*cmp && *cmp++ == *str++) ;
850 return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
866 bool consume_comment(Lex_input_stream *lip,
int remaining_recursions_permitted)
873 if (remaining_recursions_permitted > 0)
875 if ((c ==
'/') && (lip->yyPeek() ==
'*'))
878 consume_comment(lip, remaining_recursions_permitted-1);
885 if (lip->yyPeek() ==
'/')
908 int MYSQLlex(
void *arg,
void *yythd)
910 THD *thd= (THD *)yythd;
911 Lex_input_stream *lip= & thd->m_parser_state->m_lip;
915 if (lip->lookahead_token >= 0)
921 token= lip->lookahead_token;
922 lip->lookahead_token= -1;
923 *yylval= *(lip->lookahead_yylval);
924 lip->lookahead_yylval= NULL;
925 lip->m_digest_psi= MYSQL_ADD_TOKEN(lip->m_digest_psi, token, yylval);
929 token= lex_one_token(arg, yythd);
940 token= lex_one_token(arg, yythd);
943 lip->m_digest_psi= MYSQL_ADD_TOKEN(lip->m_digest_psi, WITH_CUBE_SYM,
945 return WITH_CUBE_SYM;
947 lip->m_digest_psi= MYSQL_ADD_TOKEN(lip->m_digest_psi, WITH_ROLLUP_SYM,
949 return WITH_ROLLUP_SYM;
954 lip->lookahead_yylval= lip->yylval;
956 lip->lookahead_token= token;
957 lip->m_digest_psi= MYSQL_ADD_TOKEN(lip->m_digest_psi, WITH, yylval);
965 lip->m_digest_psi= MYSQL_ADD_TOKEN(lip->m_digest_psi, token, yylval);
969 int lex_one_token(
void *arg,
void *yythd)
973 int tokval, result_state;
975 enum my_lex_states state;
976 THD *thd= (THD *)yythd;
977 Lex_input_stream *lip= & thd->m_parser_state->m_lip;
981 uchar *state_map= cs->state_map;
982 uchar *ident_map= cs->ident_map;
987 state=lip->next_state;
988 lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
992 case MY_LEX_OPERATOR_OR_IDENT:
995 while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
1004 lip->restart_token();
1006 state= (
enum my_lex_states) state_map[c];
1009 if (lip->yyGet() ==
'N')
1011 yylval->lex_str.str=(
char*)
"\\N";
1012 yylval->lex_str.length=2;
1017 if (c ==
'-' && lip->yyPeek() ==
'-' &&
1018 (my_isspace(cs,lip->yyPeekn(1)) ||
1019 my_iscntrl(cs,lip->yyPeekn(1))))
1021 state=MY_LEX_COMMENT;
1026 lip->next_state= MY_LEX_START;
1039 lip->restart_token();
1049 if (c ==
'?' && lip->stmt_prepare_mode && !ident_map[lip->yyPeek()])
1050 return(PARAM_MARKER);
1055 case MY_LEX_IDENT_OR_NCHAR:
1056 if (lip->yyPeek() !=
'\'')
1058 state= MY_LEX_IDENT;
1063 if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
1068 yylval->lex_str.length= lip->yytoklen;
1069 lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1070 return(NCHAR_STRING);
1072 case MY_LEX_IDENT_OR_HEX:
1073 if (lip->yyPeek() ==
'\'')
1075 state= MY_LEX_HEX_NUMBER;
1078 case MY_LEX_IDENT_OR_BIN:
1079 if (lip->yyPeek() ==
'\'')
1081 state= MY_LEX_BIN_NUMBER;
1086 #if defined(USE_MB) && defined(USE_MB_IDENT)
1089 result_state= IDENT_QUOTED;
1090 if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
1092 int l = my_ismbchar(cs,
1094 lip->get_end_of_query());
1096 state = MY_LEX_CHAR;
1099 lip->skip_binary(l - 1);
1101 while (ident_map[c=lip->yyGet()])
1103 if (my_mbcharlen(cs, c) > 1)
1106 if ((l = my_ismbchar(cs,
1108 lip->get_end_of_query())) == 0)
1110 lip->skip_binary(l-1);
1117 for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) ;
1119 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1121 length= lip->yyLength();
1122 start= lip->get_ptr();
1123 if (lip->ignore_space)
1129 for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) ;
1131 if (start == lip->get_ptr() && c ==
'.' && ident_map[lip->yyPeek()])
1132 lip->next_state=MY_LEX_IDENT_SEP;
1136 if ((tokval = find_keyword(lip, length, c ==
'(')))
1138 lip->next_state= MY_LEX_START;
1143 yylval->lex_str=get_token(lip, 0, length);
1152 if (yylval->lex_str.str[0] ==
'_')
1154 CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
1155 MY_CS_PRIMARY, MYF(0));
1158 yylval->charset= cs;
1159 lip->m_underscore_cs= cs;
1161 lip->body_utf8_append(lip->m_cpp_text_start,
1162 lip->get_cpp_tok_start() + length);
1163 return(UNDERSCORE_CHARSET);
1167 lip->body_utf8_append(lip->m_cpp_text_start);
1169 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1170 lip->m_cpp_text_end);
1172 return(result_state);
1174 case MY_LEX_IDENT_SEP:
1175 yylval->lex_str.str= (
char*) lip->get_ptr();
1176 yylval->lex_str.length= 1;
1178 lip->next_state= MY_LEX_IDENT_START;
1179 if (!ident_map[lip->yyPeek()])
1180 lip->next_state= MY_LEX_START;
1183 case MY_LEX_NUMBER_IDENT:
1184 if (lip->yyGetLast() ==
'0')
1189 while (my_isxdigit(cs,(c = lip->yyGet()))) ;
1190 if ((lip->yyLength() >= 3) && !ident_map[c])
1193 yylval->lex_str=get_token(lip, 2, lip->yyLength()-2);
1197 state= MY_LEX_IDENT_START;
1202 while ((c= lip->yyGet()) ==
'0' || c ==
'1') ;
1203 if ((lip->yyLength() >= 3) && !ident_map[c])
1206 yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
1210 state= MY_LEX_IDENT_START;
1216 while (my_isdigit(cs, (c = lip->yyGet()))) ;
1219 state=MY_LEX_INT_OR_REAL;
1222 if (c ==
'e' || c ==
'E')
1225 if (my_isdigit(cs,lip->yyPeek()) ||
1226 (c=(lip->yyGet())) ==
'+' || c ==
'-')
1228 if (my_isdigit(cs,lip->yyPeek()))
1231 while (my_isdigit(cs,lip->yyGet())) ;
1232 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1239 case MY_LEX_IDENT_START:
1240 result_state= IDENT;
1241 #if defined(USE_MB) && defined(USE_MB_IDENT)
1244 result_state= IDENT_QUOTED;
1245 while (ident_map[c=lip->yyGet()])
1247 if (my_mbcharlen(cs, c) > 1)
1250 if ((l = my_ismbchar(cs,
1252 lip->get_end_of_query())) == 0)
1254 lip->skip_binary(l-1);
1261 for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) ;
1263 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1265 if (c ==
'.' && ident_map[lip->yyPeek()])
1266 lip->next_state=MY_LEX_IDENT_SEP;
1268 yylval->lex_str= get_token(lip, 0, lip->yyLength());
1270 lip->body_utf8_append(lip->m_cpp_text_start);
1272 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1273 lip->m_cpp_text_end);
1275 return(result_state);
1277 case MY_LEX_USER_VARIABLE_DELIMITER:
1279 uint double_quotes= 0;
1291 if ((var_length= my_mbcharlen(cs, c)) == 1)
1293 if (c == quote_char)
1295 if (lip->yyPeek() != quote_char)
1303 else if (use_mb(cs))
1305 if ((var_length= my_ismbchar(cs, lip->get_ptr() - 1,
1306 lip->get_end_of_query())))
1307 lip->skip_binary(var_length-1);
1312 yylval->lex_str=get_quoted_token(lip, 1,
1313 lip->yyLength() - double_quotes -1,
1316 yylval->lex_str=get_token(lip, 1, lip->yyLength() -1);
1317 if (c == quote_char)
1319 lip->next_state= MY_LEX_START;
1321 lip->body_utf8_append(lip->m_cpp_text_start);
1323 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1324 lip->m_cpp_text_end);
1326 return(IDENT_QUOTED);
1328 case MY_LEX_INT_OR_REAL:
1331 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1332 return int_token(yylval->lex_str.str, (uint) yylval->lex_str.length);
1336 while (my_isdigit(cs,c = lip->yyGet())) ;
1338 if (c ==
'e' || c ==
'E')
1341 if (c ==
'-' || c ==
'+')
1343 if (!my_isdigit(cs,c))
1348 while (my_isdigit(cs,lip->yyGet())) ;
1349 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1352 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1353 return(DECIMAL_NUM);
1355 case MY_LEX_HEX_NUMBER:
1357 while (my_isxdigit(cs, (c= lip->yyGet()))) ;
1361 length= lip->yyLength();
1362 if ((length % 2) == 0)
1364 yylval->lex_str=get_token(lip,
1369 case MY_LEX_BIN_NUMBER:
1371 while ((c= lip->yyGet()) ==
'0' || c ==
'1') ;
1375 length= lip->yyLength();
1376 yylval->lex_str= get_token(lip,
1382 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
1383 state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
1385 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1387 lip->next_state= MY_LEX_START;
1390 state = MY_LEX_CHAR;
1393 case MY_LEX_LONG_CMP_OP:
1394 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
1395 state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
1398 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP)
1401 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
1403 lip->next_state= MY_LEX_START;
1406 state = MY_LEX_CHAR;
1410 if (c != lip->yyPeek())
1416 tokval = find_keyword(lip,2,0);
1417 lip->next_state= MY_LEX_START;
1420 case MY_LEX_STRING_OR_DELIMITER:
1421 if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
1423 state= MY_LEX_USER_VARIABLE_DELIMITER;
1428 if (!(yylval->lex_str.str = get_text(lip, 1, 1)))
1433 yylval->lex_str.length=lip->yytoklen;
1435 lip->body_utf8_append(lip->m_cpp_text_start);
1437 lip->body_utf8_append_literal(thd, &yylval->lex_str,
1438 lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1439 lip->m_cpp_text_end);
1441 lip->m_underscore_cs= NULL;
1443 lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1444 return(TEXT_STRING);
1446 case MY_LEX_COMMENT:
1447 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1448 while ((c = lip->yyGet()) !=
'\n' && c) ;
1450 state = MY_LEX_START;
1452 case MY_LEX_LONG_COMMENT:
1453 if (lip->yyPeek() !=
'*')
1458 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1462 lip->save_in_comment_state();
1464 if (lip->yyPeekn(2) ==
'!')
1466 lip->in_comment= DISCARD_COMMENT;
1468 lip->set_echo(FALSE);
1481 char version_str[6];
1482 version_str[0]= lip->yyPeekn(0);
1483 version_str[1]= lip->yyPeekn(1);
1484 version_str[2]= lip->yyPeekn(2);
1485 version_str[3]= lip->yyPeekn(3);
1486 version_str[4]= lip->yyPeekn(4);
1488 if ( my_isdigit(cs, version_str[0])
1489 && my_isdigit(cs, version_str[1])
1490 && my_isdigit(cs, version_str[2])
1491 && my_isdigit(cs, version_str[3])
1492 && my_isdigit(cs, version_str[4])
1496 version=strtol(version_str, NULL, 10);
1498 if (version <= MYSQL_VERSION_ID)
1503 lip->set_echo(TRUE);
1513 char *pcom= lip->yyUnput(
' ');
1514 comment_closed= ! consume_comment(lip, 1);
1515 if (! comment_closed)
1526 lip->set_echo(TRUE);
1532 lip->in_comment= PRESERVE_COMMENT;
1535 comment_closed= ! consume_comment(lip, 0);
1555 if (! comment_closed)
1557 state = MY_LEX_START;
1558 lip->restore_in_comment_state();
1560 case MY_LEX_END_LONG_COMMENT:
1561 if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() ==
'/')
1566 lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1569 lip->set_echo(TRUE);
1580 if (!my_isspace(cs, lip->yyPeek()) &&
1581 lip->get_cpp_ptr() != lip->get_cpp_buf() &&
1582 !my_isspace(cs, *(lip->get_cpp_ptr() - 1)))
1583 lip->cpp_inject(
' ');
1585 lip->in_comment=NO_COMMENT;
1591 case MY_LEX_SET_VAR:
1592 if (lip->yyPeek() !=
'=')
1599 case MY_LEX_SEMICOLON:
1606 lip->set_echo(FALSE);
1608 lip->set_echo(TRUE);
1610 if (lip->in_comment != NO_COMMENT)
1612 lip->next_state=MY_LEX_END;
1613 return(END_OF_INPUT);
1618 lip->next_state=MY_LEX_END;
1622 case MY_LEX_REAL_OR_POINT:
1623 if (my_isdigit(cs,lip->yyPeek()))
1624 state = MY_LEX_REAL;
1627 state= MY_LEX_IDENT_SEP;
1631 case MY_LEX_USER_END:
1632 switch (state_map[lip->yyPeek()]) {
1634 case MY_LEX_USER_VARIABLE_DELIMITER:
1635 case MY_LEX_STRING_OR_DELIMITER:
1637 case MY_LEX_USER_END:
1638 lip->next_state=MY_LEX_SYSTEM_VAR;
1641 lip->next_state=MY_LEX_HOSTNAME;
1644 yylval->lex_str.str=(
char*) lip->get_ptr();
1645 yylval->lex_str.length=1;
1647 case MY_LEX_HOSTNAME:
1648 for (c=lip->yyGet() ;
1649 my_isalnum(cs,c) || c ==
'.' || c ==
'_' || c ==
'$';
1651 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1652 return(LEX_HOSTNAME);
1653 case MY_LEX_SYSTEM_VAR:
1654 yylval->lex_str.str=(
char*) lip->get_ptr();
1655 yylval->lex_str.length=1;
1657 lip->next_state= (state_map[lip->yyPeek()] ==
1658 MY_LEX_USER_VARIABLE_DELIMITER ?
1659 MY_LEX_OPERATOR_OR_IDENT :
1660 MY_LEX_IDENT_OR_KEYWORD);
1662 case MY_LEX_IDENT_OR_KEYWORD:
1669 for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) ;
1671 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1674 lip->next_state=MY_LEX_IDENT_SEP;
1675 length= lip->yyLength();
1678 if ((tokval= find_keyword(lip, length,0)))
1683 yylval->lex_str=get_token(lip, 0, length);
1685 lip->body_utf8_append(lip->m_cpp_text_start);
1687 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1688 lip->m_cpp_text_end);
1690 return(result_state);
1704 while ((str->length > 0) && (my_isspace(cs, str->str[0])))
1714 while ((str->length > 0) && (my_isspace(cs, str->str[str->length-1])))
1718 str->str[str->length]= 0;
1727 void st_select_lex_node::init_query()
1730 sql_cache= SQL_CACHE_UNSPECIFIED;
1731 linkage= UNSPECIFIED_TYPE;
1732 no_error= no_table_names_allowed= 0;
1736 void st_select_lex_node::init_select()
1740 void st_select_lex_unit::init_query()
1742 st_select_lex_node::init_query();
1743 linkage= GLOBAL_OPTIONS_TYPE;
1744 global_parameters= first_select();
1745 select_limit_cnt= HA_POS_ERROR;
1746 offset_limit_cnt= 0;
1748 prepared= optimized= executed= 0;
1756 found_rows_for_union= 0;
1760 void st_select_lex::init_query()
1762 st_select_lex_node::init_query();
1763 resolve_place= RESOLVE_NONE;
1766 top_join_list.empty();
1767 join_list= &top_join_list;
1768 embedding= leaf_tables= 0;
1771 having= prep_having= where= prep_where= 0;
1772 olap= UNSPECIFIED_OLAP_TYPE;
1773 having_fix_field= 0;
1775 context.select_lex=
this;
1786 parent_lex->push_context(&context);
1787 cond_count= between_count= with_wild= 0;
1789 ref_pointer_array.reset();
1790 select_n_where_fields= 0;
1791 select_n_having_items= 0;
1793 n_child_sum_items= 0;
1794 subquery_in_having= explicit_limit= 0;
1795 is_item_list_lookup= 0;
1797 first_natural_join_processing= 1;
1798 first_cond_optimization= 1;
1799 parsing_place= NO_MATTER;
1800 exclude_from_table_unique_test= no_wrap_view_item= FALSE;
1803 select_list_tables= 0;
1804 m_non_agg_field_used=
false;
1805 m_agg_func_used=
false;
1806 with_sum_func=
false;
1807 removed_select= NULL;
1810 void st_select_lex::init_select()
1812 st_select_lex_node::init_select();
1815 if (group_list_ptrs)
1816 group_list_ptrs->clear();
1819 table_join_options= 0;
1820 in_sum_expr= with_wild= 0;
1822 sql_cache= SQL_CACHE_UNSPECIFIED;
1824 interval_list.empty();
1825 ftfunc_list_alloc.empty();
1826 inner_sum_func_list= 0;
1827 ftfunc_list= &ftfunc_list_alloc;
1828 linkage= UNSPECIFIED_TYPE;
1829 order_list.elements= 0;
1830 order_list.first= 0;
1831 order_list.next= &order_list.first;
1832 if (order_list_ptrs)
1833 order_list_ptrs->clear();
1838 cur_pos_in_all_fields= ALL_FIELDS_UNDEF_POS;
1839 non_agg_fields.empty();
1840 cond_value= having_value= Item::COND_UNDEF;
1841 inner_refs_list.empty();
1842 m_non_agg_field_used=
false;
1843 m_agg_func_used=
false;
1851 void st_select_lex_node::include_down(st_select_lex_node *upper)
1853 if ((next= upper->slave))
1855 prev= &upper->slave;
1869 void st_select_lex_node::include_standalone(st_select_lex_node *upper,
1870 st_select_lex_node **ref)
1879 void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1881 if ((next= before->next))
1883 prev= &before->next;
1885 master= before->master;
1890 void st_select_lex_node::include_global(st_select_lex_node **plink)
1892 if ((link_next= *plink))
1893 link_next->link_prev= &link_next;
1899 void st_select_lex_node::fast_exclude()
1903 if ((*link_prev= link_next))
1904 link_next->link_prev= link_prev;
1907 for (; slave; slave= slave->next)
1908 slave->fast_exclude();
1916 void st_select_lex_node::exclude()
1941 void st_select_lex_unit::exclude_level()
1943 SELECT_LEX_UNIT *units= NULL;
1944 SELECT_LEX_UNIT **units_last= &units;
1945 SELECT_LEX *sl= first_select();
1948 SELECT_LEX *next_select= sl->next_select();
1951 if (sl->link_prev && (*sl->link_prev= sl->link_next))
1952 sl->link_next->link_prev= sl->link_prev;
1955 SELECT_LEX_UNIT **last= NULL;
1956 for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1966 for (SELECT_LEX *s= u->first_select(); s; s= s->next_select())
1968 if (s->context.outer_context == &sl->context)
1969 s->context.outer_context= sl->context.outer_context;
1972 last= (SELECT_LEX_UNIT**)&(u->next);
1976 (*units_last)= sl->first_inner_unit();
1981 sl->cleanup_level();
1990 (*units_last)= (SELECT_LEX_UNIT*)next;
1992 next->prev= (SELECT_LEX_NODE**)units_last;
2017 void st_select_lex_unit::exclude_tree()
2019 SELECT_LEX *sl= first_select();
2022 SELECT_LEX *next_select= sl->next_select();
2025 if (sl->link_prev && (*sl->link_prev= sl->link_next))
2026 sl->link_next->link_prev= sl->link_prev;
2029 for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
2057 void st_select_lex_unit::invalidate()
2080 void st_select_lex::mark_as_dependent(st_select_lex *last)
2086 for (SELECT_LEX *s=
this;
2088 s= s->outer_select())
2090 if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
2093 s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
2094 UNCACHEABLE_DEPENDENT;
2095 SELECT_LEX_UNIT *munit= s->master_unit();
2096 munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
2097 UNCACHEABLE_DEPENDENT;
2098 for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
2101 !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
2102 sl->uncacheable|= UNCACHEABLE_UNITED;
2108 bool st_select_lex_node::set_braces(
bool value) {
return 1; }
2109 bool st_select_lex_node::inc_in_sum_expr() {
return 1; }
2110 uint st_select_lex_node::get_in_sum_expr() {
return 0; }
2111 TABLE_LIST* st_select_lex_node::get_table_list() {
return 0; }
2112 List<Item>* st_select_lex_node::get_item_list() {
return 0; }
2113 TABLE_LIST *st_select_lex_node::add_table_to_list(THD *thd, Table_ident *
table,
2115 ulong table_join_options,
2116 thr_lock_type
flags,
2117 enum_mdl_type mdl_type,
2124 ulong st_select_lex_node::get_table_join_options()
2132 bool st_select_lex::test_limit()
2134 if (select_limit != 0)
2136 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
2137 "LIMIT & IN/ALL/ANY/SOME subquery");
2144 st_select_lex_unit* st_select_lex_unit::master_unit()
2150 st_select_lex* st_select_lex_unit::outer_select()
2152 return (st_select_lex*) master;
2156 bool st_select_lex::add_order_to_list(THD *thd,
Item *item,
bool asc)
2162 bool st_select_lex::add_gorder_to_list(THD *thd,
Item *item,
bool asc)
2167 bool st_select_lex::add_item_to_list(THD *thd,
Item *item)
2169 DBUG_ENTER(
"st_select_lex::add_item_to_list");
2170 DBUG_PRINT(
"info", (
"Item: 0x%lx", (
long) item));
2171 DBUG_RETURN(item_list.push_back(item));
2175 bool st_select_lex::add_group_to_list(THD *thd,
Item *item,
bool asc)
2183 return !func || ftfunc_list->push_back(func);
2187 st_select_lex_unit* st_select_lex::master_unit()
2189 return (st_select_lex_unit*) master;
2193 st_select_lex* st_select_lex::outer_select()
2195 return (st_select_lex*) master->get_master();
2203 void st_select_lex::invalidate()
2214 bool st_select_lex::set_braces(
bool value)
2221 bool st_select_lex::inc_in_sum_expr()
2228 uint st_select_lex::get_in_sum_expr()
2236 return table_list.first;
2244 ulong st_select_lex::get_table_join_options()
2246 return table_join_options;
2250 bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
2253 order_group_num*= 2;
2259 Query_arena *arena= thd->stmt_arena;
2260 const uint n_elems= (n_sum_items +
2262 item_list.elements +
2263 select_n_having_items +
2264 select_n_where_fields +
2265 order_group_num) * 5;
2266 DBUG_PRINT(
"info", (
"setup_ref_array this %p %4u : %4u %4u %4u %4u %4u %4u",
2272 select_n_having_items,
2273 select_n_where_fields,
2275 if (!ref_pointer_array.is_null())
2283 if (ref_pointer_array.size() > n_elems)
2284 ref_pointer_array.resize(n_elems);
2293 if (ref_pointer_array.size() == n_elems)
2296 Item **array=
static_cast<Item**
>(arena->alloc(
sizeof(
Item*) * n_elems));
2300 return array == NULL;
2304 void st_select_lex_unit::print(
String *str, enum_query_type query_type)
2306 bool union_all= !union_distinct;
2307 for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
2309 if (sl != first_select())
2311 str->append(STRING_WITH_LEN(
" union "));
2313 str->append(STRING_WITH_LEN(
"all "));
2314 else if (union_distinct == sl)
2319 sl->print(thd, str, query_type);
2323 if (fake_select_lex == global_parameters)
2325 if (fake_select_lex->order_list.elements)
2327 str->append(STRING_WITH_LEN(
" order by "));
2328 fake_select_lex->print_order(str,
2329 fake_select_lex->order_list.first,
2332 fake_select_lex->print_limit(thd, str, query_type);
2337 void st_select_lex::print_order(
String *str,
2339 enum_query_type query_type)
2341 for (; order; order= order->next)
2343 if (order->counter_used)
2346 size_t length= my_snprintf(buffer, 20,
"%d", order->counter);
2347 str->append(buffer, (uint) length);
2350 (*order->item)->print_for_order(str, query_type, order->
used_alias);
2351 if (order->direction == ORDER::ORDER_DESC)
2352 str->append(STRING_WITH_LEN(
" desc"));
2359 void st_select_lex::print_limit(THD *thd,
2361 enum_query_type query_type)
2363 SELECT_LEX_UNIT *unit= master_unit();
2366 if (item && unit->global_parameters ==
this)
2368 Item_subselect::subs_type subs_type= item->substype();
2369 if (subs_type == Item_subselect::EXISTS_SUBS ||
2370 subs_type == Item_subselect::IN_SUBS ||
2371 subs_type == Item_subselect::ALL_SUBS)
2376 str->append(STRING_WITH_LEN(
" limit "));
2379 offset_limit->print(str, query_type);
2382 select_limit->print(str, query_type);
2401 Index_hint::print(THD *thd,
String *str)
2405 case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN(
"IGNORE INDEX"));
break;
2406 case INDEX_HINT_USE: str->append(STRING_WITH_LEN(
"USE INDEX"));
break;
2407 case INDEX_HINT_FORCE: str->append(STRING_WITH_LEN(
"FORCE INDEX"));
break;
2409 str->append (STRING_WITH_LEN(
" ("));
2410 if (key_name.length)
2412 if (thd && !my_strnncoll(system_charset_info,
2413 (
const uchar *)key_name.str, key_name.length,
2414 (
const uchar *)primary_key_name,
2415 strlen(primary_key_name)))
2416 str->append(primary_key_name);
2418 append_identifier(thd, str, key_name.str, key_name.length);
2425 TABLE_LIST **end, enum_query_type query_type)
2427 (*table)->print(thd, str, query_type);
2429 for (
TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
2433 if (curr->outer_join)
2436 str->append(STRING_WITH_LEN(
" left join "));
2438 else if (curr->straight)
2439 str->append(STRING_WITH_LEN(
" straight_join "));
2440 else if (curr->sj_on_expr)
2441 str->append(STRING_WITH_LEN(
" semi join "));
2443 str->append(STRING_WITH_LEN(
" join "));
2444 curr->
print(thd, str, query_type);
2445 if (curr->join_cond())
2447 str->append(STRING_WITH_LEN(
" on("));
2448 curr->join_cond()->
print(str, query_type);
2464 static void print_join(THD *thd,
2467 enum_query_type query_type)
2472 uint non_const_tables= 0;
2475 if (!t->optimized_away)
2477 if (!non_const_tables)
2479 str->append(STRING_WITH_LEN(
"dual"));
2488 TABLE_LIST *tmp, **t= table + (non_const_tables - 1);
2491 if (tmp->optimized_away)
2501 if ((*table)->sj_on_expr)
2506 if (!(*t2)->sj_on_expr)
2515 DBUG_ASSERT(non_const_tables >= 1);
2516 print_table_array(thd, str, table, table + non_const_tables, query_type);
2523 bool db_is_default_db(
const char *db,
size_t db_len,
const THD *thd)
2525 return thd != NULL && thd->db != NULL &&
2526 thd->db_length == db_len && !memcmp(db, thd->db, db_len);
2541 print_join(thd, str, &nested_join->join_list, query_type);
2546 const char *cmp_name;
2550 if (!(belong_to_view &&
2551 belong_to_view->compact_view_format) &&
2552 !((query_type & QT_NO_DEFAULT_DB) &&
2553 db_is_default_db(view_db.str, view_db.length, thd)))
2555 append_identifier(thd, str, view_db.str, view_db.length);
2558 append_identifier(thd, str, view_name.str, view_name.length);
2559 cmp_name= view_name.str;
2564 if (!(query_type & QT_DERIVED_TABLE_ONLY_ALIAS))
2567 derived->print(str, query_type);
2576 if (!(belong_to_view &&
2577 belong_to_view->compact_view_format) &&
2578 !((query_type & QT_NO_DEFAULT_DB) &&
2579 db_is_default_db(db, db_length, thd)))
2581 append_identifier(thd, str, db, db_length);
2586 append_identifier(thd, str, schema_table_name,
2587 strlen(schema_table_name));
2588 cmp_name= schema_table_name;
2592 append_identifier(thd, str, table_name, table_name_length);
2593 cmp_name= table_name;
2595 #ifdef WITH_PARTITION_STORAGE_ENGINE
2596 if (partition_names && partition_names->elements)
2598 int i, num_parts= partition_names->elements;
2600 str->append(STRING_WITH_LEN(
" PARTITION ("));
2601 for (i= 1; i <= num_parts; i++)
2604 append_identifier(thd, str, name->c_ptr(), name->length());
2612 if (my_strcasecmp(table_alias_charset, cmp_name, alias))
2614 char t_alias_buff[MAX_ALIAS_NAME];
2615 const char *t_alias= alias;
2618 if (lower_case_table_names== 1)
2620 if (alias && alias[0])
2622 strmov(t_alias_buff, alias);
2623 my_casedn_str(files_charset_info, t_alias_buff);
2624 t_alias= t_alias_buff;
2628 append_identifier(thd, str, t_alias, strlen(t_alias));
2636 while ((hint= it++))
2638 str->append (STRING_WITH_LEN(
" "));
2639 hint->print (thd, str);
2646 void st_select_lex::print(THD *thd,
String *str, enum_query_type query_type)
2652 if (query_type & QT_SHOW_SELECT_NUMBER)
2655 str->append(
"/* select#");
2656 if (unlikely(select_number >= INT_MAX))
2657 str->append(
"fake");
2659 str->append_ulonglong(select_number);
2660 str->append(
" */ select ");
2663 str->append(STRING_WITH_LEN(
"select "));
2665 if (!thd->lex->describe && join && join->need_tmp)
2674 str->append(STRING_WITH_LEN(
"<already_cleaned_up>"));
2679 if (options & SELECT_STRAIGHT_JOIN)
2680 str->append(STRING_WITH_LEN(
"straight_join "));
2682 str->append(STRING_WITH_LEN(
"high_priority "));
2683 if (options & SELECT_DISTINCT)
2684 str->append(STRING_WITH_LEN(
"distinct "));
2685 if (options & SELECT_SMALL_RESULT)
2686 str->append(STRING_WITH_LEN(
"sql_small_result "));
2687 if (options & SELECT_BIG_RESULT)
2688 str->append(STRING_WITH_LEN(
"sql_big_result "));
2689 if (options & OPTION_BUFFER_RESULT)
2690 str->append(STRING_WITH_LEN(
"sql_buffer_result "));
2691 if (options & OPTION_FOUND_ROWS)
2692 str->append(STRING_WITH_LEN(
"sql_calc_found_rows "));
2696 str->append(STRING_WITH_LEN(
"sql_no_cache "));
2699 str->append(STRING_WITH_LEN(
"sql_cache "));
2701 case SQL_CACHE_UNSPECIFIED:
2711 while ((item= it++))
2724 item->
print(str, query_type);
2727 item->print_item_w_name(str, query_type);
2735 if (table_list.elements)
2737 str->append(STRING_WITH_LEN(
" from "));
2739 print_join(thd, str, &top_join_list, query_type);
2747 str->append(STRING_WITH_LEN(
" from DUAL "));
2751 Item *cur_where= where;
2753 cur_where= join->conds;
2754 if (cur_where || cond_value != Item::COND_UNDEF)
2756 str->append(STRING_WITH_LEN(
" where "));
2758 cur_where->
print(str, query_type);
2760 str->append(cond_value != Item::COND_FALSE ?
"1" :
"0");
2764 if (group_list.elements)
2766 str->append(STRING_WITH_LEN(
" group by "));
2767 print_order(str, group_list.first, query_type);
2771 str->append(STRING_WITH_LEN(
" with cube"));
2774 str->append(STRING_WITH_LEN(
" with rollup"));
2782 Item *cur_having= having;
2784 cur_having= join->having_for_explain;
2786 if (cur_having || having_value != Item::COND_UNDEF)
2788 str->append(STRING_WITH_LEN(
" having "));
2790 cur_having->
print(str, query_type);
2792 str->append(having_value != Item::COND_FALSE ?
"1" :
"0");
2795 if (order_list.elements)
2797 str->append(STRING_WITH_LEN(
" order by "));
2798 print_order(str, order_list.first, query_type);
2802 print_limit(thd, str, query_type);
2820 void LEX::cleanup_lex_after_parse_error(THD *thd)
2832 sp_head *sp= thd->lex->sphead;
2839 thd->lex->sphead= NULL;
2861 void Query_tables_list::reset_query_tables_list(
bool init)
2863 sql_command= SQLCOM_END;
2864 if (!init && query_tables)
2870 if (query_tables_last == &table->next_global ||
2871 !(table= table->next_global))
2876 query_tables_last= &query_tables;
2877 query_tables_own_last= 0;
2884 my_hash_clear(&sroutines);
2886 else if (sroutines.records)
2889 my_hash_reset(&sroutines);
2891 sroutines_list.empty();
2892 sroutines_list_own_last= sroutines_list.next;
2893 sroutines_list_own_elements= 0;
2894 binlog_stmt_flags= 0;
2895 stmt_accessed_table_flag= 0;
2896 lock_tables_state= LTS_NOT_LOCKED;
2908 void Query_tables_list::destroy_query_tables_list()
2910 my_hash_free(&sroutines);
2929 :result(0), option_type(OPT_DEFAULT), is_change_password(false),
2930 is_set_password_sql(false), is_lex_started(0)
2933 my_init_dynamic_array2(&plugins,
sizeof(
plugin_ref),
2934 plugins_static_buffer,
2935 INITIAL_LEX_PLUGIN_LIST_SIZE,
2936 INITIAL_LEX_PLUGIN_LIST_SIZE);
2937 memset(&mi, 0,
sizeof(LEX_MASTER_INFO));
2938 reset_query_tables_list(TRUE);
2960 bool LEX::can_be_merged()
2965 bool selects_allow_merge= select_lex.next_select() == 0;
2966 if (selects_allow_merge)
2968 for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2970 tmp_unit= tmp_unit->next_unit())
2972 if (tmp_unit->first_select()->parent_lex ==
this &&
2973 (tmp_unit->item == 0 ||
2974 (tmp_unit->item->place() != IN_WHERE &&
2975 tmp_unit->item->place() != IN_ON)))
2977 selects_allow_merge= 0;
2983 return (selects_allow_merge &&
2984 select_lex.group_list.elements == 0 &&
2985 select_lex.having == 0 &&
2986 select_lex.with_sum_func == 0 &&
2987 select_lex.table_list.elements >= 1 &&
2988 !(select_lex.options & SELECT_DISTINCT) &&
2989 select_lex.select_limit == 0);
3009 bool LEX::can_use_merged()
3011 switch (sql_command)
3014 case SQLCOM_CREATE_TABLE:
3016 case SQLCOM_UPDATE_MULTI:
3018 case SQLCOM_DELETE_MULTI:
3020 case SQLCOM_INSERT_SELECT:
3021 case SQLCOM_REPLACE:
3022 case SQLCOM_REPLACE_SELECT:
3045 bool LEX::can_not_use_merged()
3047 switch (sql_command)
3049 case SQLCOM_CREATE_VIEW:
3050 case SQLCOM_SHOW_CREATE:
3056 case SQLCOM_SHOW_FIELDS:
3074 bool LEX::only_view_structure()
3076 switch (sql_command) {
3077 case SQLCOM_SHOW_CREATE:
3078 case SQLCOM_SHOW_TABLES:
3079 case SQLCOM_SHOW_FIELDS:
3080 case SQLCOM_REVOKE_ALL:
3083 case SQLCOM_CREATE_VIEW:
3103 bool LEX::need_correct_ident()
3107 case SQLCOM_SHOW_CREATE:
3108 case SQLCOM_SHOW_TABLES:
3109 case SQLCOM_CREATE_VIEW:
3135 if (view->select_lex->master_unit() == &unit &&
3136 which_check_option_applicable())
3137 return (uint8)view->with_check;
3138 return VIEW_CHECK_NONE;
3162 LEX::copy_db_to(
char **p_db,
size_t *p_db_length)
const
3166 DBUG_ASSERT(sphead->m_db.str && sphead->m_db.length);
3171 *p_db= sphead->m_db.str;
3173 *p_db_length= sphead->m_db.length;
3176 return thd->copy_db_to(p_db, p_db_length);
3187 void st_select_lex_unit::set_limit(st_select_lex *sl)
3189 ha_rows select_limit_val;
3192 DBUG_ASSERT(! thd->stmt_arena->is_stmt_prepare());
3193 if (sl->select_limit)
3195 Item *item = sl->select_limit;
3222 bool fix_fields_successful=
true;
3225 fix_fields_successful= !item->fix_fields(thd, NULL);
3227 DBUG_ASSERT(fix_fields_successful);
3229 val= fix_fields_successful ? item->val_uint() : HA_POS_ERROR;
3234 select_limit_val= (ha_rows)val;
3240 if (val != (ulonglong)select_limit_val)
3241 select_limit_val= HA_POS_ERROR;
3243 if (sl->offset_limit)
3245 Item *item = sl->offset_limit;
3247 bool fix_fields_successful=
true;
3250 fix_fields_successful= !item->fix_fields(thd, NULL);
3252 DBUG_ASSERT(fix_fields_successful);
3254 val= fix_fields_successful ? item->val_uint() : HA_POS_ERROR;
3259 offset_limit_cnt= (ha_rows)val;
3262 if (val != (ulonglong)offset_limit_cnt)
3263 offset_limit_cnt= HA_POS_ERROR;
3265 select_limit_cnt= select_limit_val + offset_limit_cnt;
3266 if (select_limit_cnt < select_limit_val)
3267 select_limit_cnt= HA_POS_ERROR;
3294 void LEX::set_trg_event_type_for_tables()
3296 uint8 new_trg_event_map= 0;
3320 switch (sql_command) {
3321 case SQLCOM_LOCK_TABLES:
3326 new_trg_event_map=
static_cast<uint8
>
3327 (1 <<
static_cast<int>(TRG_EVENT_INSERT)) |
3329 (1 <<
static_cast<int>(TRG_EVENT_UPDATE)) |
3331 (1 <<
static_cast<int>(TRG_EVENT_DELETE));
3338 case SQLCOM_INSERT_SELECT:
3353 case SQLCOM_REPLACE:
3354 case SQLCOM_REPLACE_SELECT:
3360 case SQLCOM_CREATE_TABLE:
3361 new_trg_event_map|=
static_cast<uint8
>
3362 (1 <<
static_cast<int>(TRG_EVENT_INSERT));
3366 case SQLCOM_UPDATE_MULTI:
3367 new_trg_event_map|=
static_cast<uint8
>
3368 (1 <<
static_cast<int>(TRG_EVENT_UPDATE));
3372 case SQLCOM_DELETE_MULTI:
3373 new_trg_event_map|=
static_cast<uint8
>
3374 (1 <<
static_cast<int>(TRG_EVENT_DELETE));
3380 switch (duplicates) {
3382 new_trg_event_map|=
static_cast<uint8
>
3383 (1 <<
static_cast<int>(TRG_EVENT_UPDATE));
3386 new_trg_event_map|=
static_cast<uint8
>
3387 (1 <<
static_cast<int>(TRG_EVENT_DELETE));
3399 TABLE_LIST *tables= select_lex.get_table_list();
3411 if (static_cast<int>(tables->lock_type) >=
3412 static_cast<int>(TL_WRITE_ALLOW_WRITE))
3414 tables= tables->next_local;
3437 TABLE_LIST *LEX::unlink_first_table(
bool *link_to_local)
3440 if ((first= query_tables))
3445 if ((query_tables= query_tables->next_global))
3446 query_tables->prev_global= &query_tables;
3448 query_tables_last= &query_tables;
3449 first->next_global= 0;
3451 if (query_tables_own_last == &first->next_global)
3452 query_tables_own_last= &query_tables;
3457 if ((*link_to_local=
test(select_lex.table_list.first)))
3459 select_lex.context.table_list=
3460 select_lex.context.first_name_resolution_table= first->next_local;
3461 select_lex.table_list.first= first->next_local;
3462 select_lex.table_list.elements--;
3463 first->next_local= 0;
3468 first_lists_tables_same();
3491 void LEX::first_lists_tables_same()
3493 TABLE_LIST *first_table= select_lex.table_list.first;
3494 if (query_tables != first_table && first_table != 0)
3497 if (query_tables_last == &first_table->next_global)
3498 query_tables_last= first_table->prev_global;
3500 if ((next= *first_table->prev_global= first_table->next_global))
3501 next->prev_global= first_table->prev_global;
3503 first_table->next_global= query_tables;
3509 query_tables->prev_global= &first_table->next_global;
3510 first_table->prev_global= &query_tables;
3511 query_tables= first_table;
3527 void LEX::link_first_table_back(
TABLE_LIST *first,
3532 if ((first->next_global= query_tables))
3533 query_tables->prev_global= &first->next_global;
3535 query_tables_last= &first->next_global;
3537 if (query_tables_own_last == &query_tables)
3538 query_tables_own_last= &first->next_global;
3540 query_tables= first;
3544 first->next_local= select_lex.table_list.first;
3545 select_lex.context.table_list= first;
3546 select_lex.table_list.first= first;
3547 select_lex.table_list.elements++;
3566 void LEX::cleanup_after_one_table_open()
3576 if (all_selects_list != &select_lex)
3580 for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
3582 un= un->next_unit())
3585 all_selects_list= &select_lex;
3587 select_lex.cut_subtree();
3601 void LEX::reset_n_backup_query_tables_list(Query_tables_list *backup)
3603 backup->set_query_tables_list(
this);
3608 this->reset_query_tables_list(TRUE);
3620 void LEX::restore_backup_query_tables_list(Query_tables_list *backup)
3622 this->destroy_query_tables_list();
3623 this->set_query_tables_list(backup);
3638 bool LEX::table_or_sp_used()
3640 DBUG_ENTER(
"table_or_sp_used");
3642 if (sroutines.records || query_tables)
3664 static void fix_prepare_info_in_table_list(THD *thd,
TABLE_LIST *tbl)
3666 for (; tbl; tbl= tbl->next_local)
3668 if (tbl->join_cond())
3670 tbl->prep_join_cond= tbl->join_cond();
3673 fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
3698 void st_select_lex::fix_prepare_information(THD *thd,
Item **conds,
3699 Item **having_conds)
3701 if (!thd->stmt_arena->is_conventional() && first_execution)
3704 if (group_list.first)
3706 if (!group_list_ptrs)
3708 void *mem= thd->stmt_arena->alloc(
sizeof(Group_list_ptrs));
3709 group_list_ptrs=
new (mem) Group_list_ptrs(thd->stmt_arena->mem_root);
3711 group_list_ptrs->reserve(group_list.elements);
3712 for (
ORDER *order= group_list.first; order; order= order->next)
3714 group_list_ptrs->push_back(order);
3717 if (order_list.first)
3719 if (!order_list_ptrs)
3721 void *mem= thd->stmt_arena->alloc(
sizeof(Group_list_ptrs));
3722 order_list_ptrs=
new (mem) Group_list_ptrs(thd->stmt_arena->mem_root);
3724 order_list_ptrs->reserve(order_list.elements);
3725 for (
ORDER *order= order_list.first; order; order= order->next)
3727 order_list_ptrs->push_back(order);
3744 prep_where= (*conds)->real_item();
3749 prep_having= *having_conds;
3752 fix_prepare_info_in_table_list(thd, table_list.first);
3784 void st_select_lex::set_index_hint_type(
enum index_hint_type type_arg,
3785 index_clause_map clause)
3787 current_index_hint_type= type_arg;
3788 current_index_hint_clause= clause;
3800 void st_select_lex::alloc_index_hints (THD *thd)
3820 bool st_select_lex::add_index_hint (THD *thd,
char *str, uint length)
3822 return index_hints->push_front (
new (thd->mem_root)
3823 Index_hint(current_index_hint_type,
3824 current_index_hint_clause,
3848 bool st_select_lex::handle_derived(LEX *lex,
3853 table_ref= table_ref->next_leaf)
3855 if (table_ref->is_view_or_derived() &&
3856 table_ref->handle_derived(lex, processor))
3865 if (master_unit()->fake_select_lex ==
this)
3866 return SLT_UNION_RESULT;
3867 else if (&thd->lex->select_lex ==
this)
3869 if (first_inner_unit() || next_select())
3874 else if (
this == master_unit()->first_select())
3876 if (linkage == DERIVED_TABLE_TYPE)
3879 return SLT_SUBQUERY;
3897 bool LEX::is_partition_management()
const
3899 return (sql_command == SQLCOM_ALTER_TABLE &&
3900 (alter_info.flags == Alter_info::ALTER_ADD_PARTITION ||
3901 alter_info.flags == Alter_info::ALTER_REORGANIZE_PARTITION));
3908 void st_lex_master_info::set_unspecified()
3910 memset(
this, 0,
sizeof(*
this));
3915 uint binlog_unsafe_map[256];
3917 #define UNSAFE(a, b, c) \
3919 DBUG_PRINT("unsafe_mixed_statement", ("SETTING BASE VALUES: %s, %s, %02X\n", \
3920 LEX::stmt_accessed_table_string(a), \
3921 LEX::stmt_accessed_table_string(b), \
3923 unsafe_mixed_statement(a, b, c); \
3937 void unsafe_mixed_statement(LEX::enum_stmt_accessed_table a,
3938 LEX::enum_stmt_accessed_table b, uint condition)
3941 int index= (1
U << a) | (1
U << b);
3944 for (type= 0; type < 256; type++)
3946 if ((type & index) ==
index)
3948 binlog_unsafe_map[
type] |= condition;
3966 void binlog_unsafe_map_init()
3968 memset((
void*) binlog_unsafe_map, 0,
sizeof(uint) * 256);
4019 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_WRITES_NON_TRANS_TABLE,
4020 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4022 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4023 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4025 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_WRITES_TEMP_TRANS_TABLE,
4026 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4028 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4029 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4031 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE,
4034 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_READS_TEMP_NON_TRANS_TABLE,
4037 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE, LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE,
4040 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE, LEX::STMT_READS_TEMP_NON_TRANS_TABLE,
4043 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_READS_TRANS_TABLE,
4044 (BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF) & TRX_CACHE_NOT_EMPTY);
4046 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_READS_TEMP_TRANS_TABLE,
4047 (BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF) & TRX_CACHE_NOT_EMPTY);
4049 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, LEX::STMT_READS_TRANS_TABLE,
4050 BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY);
4052 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, LEX::STMT_READS_TEMP_TRANS_TABLE,
4053 BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY);
4055 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4056 BINLOG_DIRECT_OFF & TRX_CACHE_NOT_EMPTY);