17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
20
19
#define DRIZZLE_LEX 1
21
#include "drizzled/configmake.h"
22
#include "drizzled/item/num.h"
23
#include "drizzled/error.h"
24
#include "drizzled/session.h"
25
#include "drizzled/sql_base.h"
26
#include "drizzled/lookup_symbol.h"
27
#include "drizzled/index_hint.h"
34
/* Stay outside of the namespace because otherwise bison goes nuts */
35
int DRIZZLElex(void *arg, void *yysession);
20
#include <drizzled/server_includes.h>
40
22
static int lex_one_token(void *arg, void *yysession);
43
save order by and tables in own lists.
25
We are using pointer to this variable for distinguishing between assignment
26
to NEW row field (when parsing trigger definition) and structured variable.
45
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
48
if (!(order = (order_st *) session->alloc(sizeof(order_st))))
50
order->item_ptr= item;
51
order->item= &order->item_ptr;
55
order->counter_used= 0;
56
list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
29
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
61
32
LEX_STRING constant for null-string to be used in parser and other places.
63
34
const LEX_STRING null_lex_str= {NULL, 0};
38
The following data is based on the latin1 character set, and is only
39
used when comparing keywords
42
static unsigned char to_upper_lex[]=
44
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
45
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
46
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
47
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
48
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
49
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
50
96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
51
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
52
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
53
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
54
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
55
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
56
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
57
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
58
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
59
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
63
Names of the index hints (for error messages). Keep in sync with
67
const char * index_hint_type_name[] =
74
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
77
to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
87
for (i=0 ; i < array_elements(symbols) ; i++)
88
symbols[i].length=(unsigned char) strlen(symbols[i].name);
89
for (i=0 ; i < array_elements(sql_functions) ; i++)
90
sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
97
{ // Call this when daemon ends
103
st_parsing_options::reset()
105
allows_variable= true;
106
allows_select_into= true;
107
allows_select_procedure= true;
108
allows_derived= true;
65
111
Lex_input_stream::Lex_input_stream(Session *session,
66
112
const char* buffer,
67
113
unsigned int length)
229
301
lex->select_lex.master= &lex->unit;
230
302
lex->select_lex.prev= &lex->unit.slave;
231
303
lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
232
lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
304
lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
233
305
lex->select_lex.options= 0;
234
306
lex->select_lex.init_order();
235
307
lex->select_lex.group_list.empty();
236
308
lex->describe= 0;
309
lex->subqueries= false;
237
310
lex->derived_tables= 0;
238
311
lex->lock_option= TL_READ;
239
312
lex->leaf_tables_insert= 0;
313
lex->parsing_options.reset();
314
lex->empty_field_list_on_rset= 0;
240
315
lex->select_lex.select_number= 1;
242
317
lex->select_lex.in_sum_expr=0;
318
lex->select_lex.ftfunc_list_alloc.empty();
319
lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc;
243
320
lex->select_lex.group_list.empty();
244
321
lex->select_lex.order_list.empty();
245
322
lex->sql_command= SQLCOM_END;
246
323
lex->duplicates= DUP_ERROR;
325
lex->proc_list.first= 0;
248
326
lex->escape_used= false;
249
327
lex->query_tables= 0;
250
328
lex->reset_query_tables_list(false);
251
329
lex->expr_allows_subselect= true;
252
330
lex->use_only_table_context= false;
331
lex->parse_vcol_expr= false;
254
333
lex->name.str= 0;
255
334
lex->name.length= 0;
256
335
lex->nest_level=0 ;
257
336
lex->allow_sum_func= 0;
258
337
lex->in_sum_func= NULL;
339
ok, there must be a better solution for this, long-term
340
I tried "memset" in the sql_yacc.yy code, but that for
341
some reason made the values zero, even if they were set
343
lex->server_options.server_name= 0;
344
lex->server_options.server_name_length= 0;
345
lex->server_options.host= 0;
346
lex->server_options.db= 0;
347
lex->server_options.username= 0;
348
lex->server_options.password= 0;
349
lex->server_options.scheme= 0;
350
lex->server_options.owner= 0;
351
lex->server_options.port= -1;
260
353
lex->is_lex_started= true;
261
lex->statement= NULL;
263
lex->is_cross= false;
268
357
void lex_end(LEX *lex)
777
887
for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
779
889
if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
780
lip->next_state=MY_LEX_IDENT_SEP;
890
lip->next_state=MY_LEX_IDENT_SEP;
782
892
{ // '(' must follow directly if function
784
if ((tokval = find_keyword(lip, length, c == '(')))
786
lip->next_state= MY_LEX_START; // Allow signed numbers
787
return(tokval); // Was keyword
894
if ((tokval = find_keyword(lip, length, c == '(')))
896
lip->next_state= MY_LEX_START; // Allow signed numbers
897
return(tokval); // Was keyword
789
899
lip->yySkip(); // next state does a unget
791
901
yylval->lex_str=get_token(lip, 0, length);
904
Note: "SELECT _bla AS 'alias'"
905
_bla should be considered as a IDENT if charset haven't been found.
906
So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
910
if (yylval->lex_str.str[0] == '_')
912
const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
913
MY_CS_PRIMARY, MYF(0));
917
lip->m_underscore_cs= cs;
919
lip->body_utf8_append(lip->m_cpp_text_start,
920
lip->get_cpp_tok_start() + length);
921
return(UNDERSCORE_CHARSET);
793
925
lip->body_utf8_append(lip->m_cpp_text_start);
795
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
927
lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
928
lip->m_cpp_text_end);
797
930
return(result_state); // IDENT or IDENT_QUOTED
841
974
while (my_isdigit(cs, (c = lip->yyGet()))) ;
842
975
if (!ident_map[c])
843
976
{ // Can't be identifier
844
state=MY_LEX_INT_OR_REAL;
977
state=MY_LEX_INT_OR_REAL;
847
980
if (c == 'e' || c == 'E')
849
// The following test is written this way to allow numbers of type 1e1
982
// The following test is written this way to allow numbers of type 1e1
850
983
if (my_isdigit(cs,lip->yyPeek()) ||
851
984
(c=(lip->yyGet())) == '+' || c == '-')
853
986
if (my_isdigit(cs,lip->yyPeek())) // Number must have digit after sign
856
989
while (my_isdigit(cs,lip->yyGet())) ;
857
990
yylval->lex_str=get_token(lip, 0, lip->yyLength());
864
997
case MY_LEX_IDENT_START: // We come here after '.'
865
998
result_state= IDENT;
999
#if defined(USE_MB) && defined(USE_MB_IDENT)
868
result_state= IDENT_QUOTED;
1002
result_state= IDENT_QUOTED;
869
1003
while (ident_map[c=lip->yyGet()])
871
1005
if (my_mbcharlen(cs, c) > 1)
873
int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
1008
if ((l = my_ismbchar(cs,
1010
lip->get_end_of_query())) == 0)
876
1012
lip->skip_binary(l-1);
882
1019
for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
883
1020
/* If there were non-ASCII characters, mark that we must convert */
884
1021
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
886
1023
if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
887
lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
1024
lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
889
1026
yylval->lex_str= get_token(lip, 0, lip->yyLength());
891
1028
lip->body_utf8_append(lip->m_cpp_text_start);
893
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
1030
lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1031
lip->m_cpp_text_end);
895
1033
return(result_state);
1248
1409
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1251
lip->next_state=MY_LEX_IDENT_SEP;
1412
lip->next_state=MY_LEX_IDENT_SEP;
1252
1413
length= lip->yyLength();
1253
1414
if (length == 0)
1254
1415
return(ABORT_SYM); // Names must be nonempty.
1255
1416
if ((tokval= find_keyword(lip, length,0)))
1257
1418
lip->yyUnget(); // Put back 'c'
1258
return(tokval); // Was keyword
1419
return(tokval); // Was keyword
1260
1421
yylval->lex_str=get_token(lip, 0, length);
1262
1423
lip->body_utf8_append(lip->m_cpp_text_start);
1264
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
1425
lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1426
lip->m_cpp_text_end);
1266
1428
return(result_state);
1435
Construct a copy of this object to be used for mysql_alter_table
1436
and mysql_create_table.
1438
Historically, these two functions modify their Alter_info
1439
arguments. This behaviour breaks re-execution of prepared
1440
statements and stored procedures and is compensated by always
1441
supplying a copy of Alter_info to these functions.
1443
@return You need to use check the error in Session for out
1444
of memory condition after calling this function.
1447
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
1448
:drop_list(rhs.drop_list, mem_root),
1449
alter_list(rhs.alter_list, mem_root),
1450
key_list(rhs.key_list, mem_root),
1451
create_list(rhs.create_list, mem_root),
1453
keys_onoff(rhs.keys_onoff),
1454
tablespace_op(rhs.tablespace_op),
1455
no_parts(rhs.no_parts),
1456
build_method(rhs.build_method),
1457
datetime_field(rhs.datetime_field),
1458
error_if_not_empty(rhs.error_if_not_empty)
1461
Make deep copies of used objects.
1462
This is not a fully deep copy - clone() implementations
1463
of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
1464
do not copy string constants. At the same length the only
1465
reason we make a copy currently is that ALTER/CREATE TABLE
1466
code changes input Alter_info definitions, but string
1467
constants never change.
1469
list_copy_and_replace_each_value(drop_list, mem_root);
1470
list_copy_and_replace_each_value(alter_list, mem_root);
1471
list_copy_and_replace_each_value(key_list, mem_root);
1472
list_copy_and_replace_each_value(create_list, mem_root);
1271
1476
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1617
1834
prohibit using LIMIT clause
1619
bool Select_Lex::test_limit()
1836
bool st_select_lex::test_limit()
1621
1838
if (select_limit != 0)
1623
1840
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1624
1841
"LIMIT & IN/ALL/ANY/SOME subquery");
1630
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1635
Select_Lex* Select_Lex_Unit::outer_select()
1637
return (Select_Lex*) master;
1640
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1848
st_select_lex_unit* st_select_lex_unit::master_unit()
1854
st_select_lex* st_select_lex_unit::outer_select()
1856
return (st_select_lex*) master;
1860
bool st_select_lex::add_order_to_list(Session *session, Item *item, bool asc)
1642
1862
return add_to_list(session, order_list, item, asc);
1645
bool Select_Lex::add_item_to_list(Session *, Item *item)
1866
bool st_select_lex::add_item_to_list(Session *session __attribute__((unused)),
1647
1869
return(item_list.push_back(item));
1650
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1873
bool st_select_lex::add_group_to_list(Session *session, Item *item, bool asc)
1652
1875
return add_to_list(session, group_list, item, asc);
1655
Select_Lex_Unit* Select_Lex::master_unit()
1657
return (Select_Lex_Unit*) master;
1660
Select_Lex* Select_Lex::outer_select()
1662
return (Select_Lex*) master->get_master();
1665
bool Select_Lex::set_braces(bool value)
1879
st_select_lex_unit* st_select_lex::master_unit()
1881
return (st_select_lex_unit*) master;
1885
st_select_lex* st_select_lex::outer_select()
1887
return (st_select_lex*) master->get_master();
1891
bool st_select_lex::set_braces(bool value)
1671
bool Select_Lex::inc_in_sum_expr()
1898
bool st_select_lex::inc_in_sum_expr()
1677
uint32_t Select_Lex::get_in_sum_expr()
1905
uint32_t st_select_lex::get_in_sum_expr()
1679
1907
return in_sum_expr;
1682
TableList* Select_Lex::get_table_list()
1911
TableList* st_select_lex::get_table_list()
1684
1913
return (TableList*) table_list.first;
1687
List<Item>* Select_Lex::get_item_list()
1916
List<Item>* st_select_lex::get_item_list()
1689
1918
return &item_list;
1692
uint32_t Select_Lex::get_table_join_options()
1921
uint32_t st_select_lex::get_table_join_options()
1694
1923
return table_join_options;
1697
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1927
bool st_select_lex::setup_ref_array(Session *session, uint32_t order_group_num)
1699
1929
if (ref_pointer_array)
1702
1932
return (ref_pointer_array=
1703
1933
(Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1862
2129
statement parsing. On should use lex_start() function to prepare LEX
1871
sql_command(SQLCOM_END),
1872
option_type(OPT_DEFAULT),
1875
sum_expr_used(false)
2134
:result(0), yacc_yyss(0), yacc_yyvs(0),
2135
sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2138
my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2139
plugins_static_buffer,
2140
INITIAL_LEX_PLUGIN_LIST_SIZE,
2141
INITIAL_LEX_PLUGIN_LIST_SIZE);
1877
2142
reset_query_tables_list(true);
2147
Check whether the merging algorithm can be used on this VIEW
2150
st_lex::can_be_merged()
2153
We can apply merge algorithm if it is single SELECT view with
2154
subqueries only in WHERE clause (we do not count SELECTs of underlying
2155
views, and second level subqueries) and we have not grpouping, ordering,
2156
HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2157
several underlying tables.
2160
false - only temporary table algorithm can be used
2161
true - merge algorithm can be used
2164
bool st_lex::can_be_merged()
2166
// TODO: do not forget implement case when select_lex.table_list.elements==0
2168
/* find non VIEW subqueries/unions */
2169
bool selects_allow_merge= select_lex.next_select() == 0;
2170
if (selects_allow_merge)
2172
for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2174
tmp_unit= tmp_unit->next_unit())
2176
if (tmp_unit->first_select()->parent_lex == this &&
2177
(tmp_unit->item == 0 ||
2178
(tmp_unit->item->place() != IN_WHERE &&
2179
tmp_unit->item->place() != IN_ON)))
2181
selects_allow_merge= 0;
2187
return (selects_allow_merge &&
2188
select_lex.group_list.elements == 0 &&
2189
select_lex.having == 0 &&
2190
select_lex.with_sum_func == 0 &&
2191
select_lex.table_list.elements >= 1 &&
2192
!(select_lex.options & SELECT_DISTINCT) &&
2193
select_lex.select_limit == 0);
2198
check if command can use VIEW with MERGE algorithm (for top VIEWs)
2201
st_lex::can_use_merged()
2204
Only listed here commands can use merge algorithm in top level
2205
SELECT_LEX (for subqueries will be used merge algorithm if
2206
st_lex::can_not_use_merged() is not true).
2209
false - command can't use merged VIEWs
2210
true - VIEWs with MERGE algorithms can be used
2213
bool st_lex::can_use_merged()
2215
switch (sql_command)
2218
case SQLCOM_CREATE_TABLE:
2220
case SQLCOM_UPDATE_MULTI:
2222
case SQLCOM_DELETE_MULTI:
2224
case SQLCOM_INSERT_SELECT:
2225
case SQLCOM_REPLACE:
2226
case SQLCOM_REPLACE_SELECT:
2235
Check if command can't use merged views in any part of command
2238
st_lex::can_not_use_merged()
2241
Temporary table algorithm will be used on all SELECT levels for queries
2242
listed here (see also st_lex::can_use_merged()).
2245
false - command can't use merged VIEWs
2246
true - VIEWs with MERGE algorithms can be used
2249
bool st_lex::can_not_use_merged()
2251
switch (sql_command)
2254
SQLCOM_SHOW_FIELDS is necessary to make
2255
information schema tables working correctly with views.
2256
see get_schema_tables_result function
2258
case SQLCOM_SHOW_FIELDS:
2125
There are Select_Lex::add_table_to_list &
2126
Select_Lex::set_lock_for_tables are in sql_parse.cc
2128
Select_Lex::print is in sql_select.cc
2130
Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2131
Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2132
Select_Lex_Unit::change_result
2535
Save current state of Query_tables_list for this LEX, and prepare it
2536
for processing of new statemnt.
2539
reset_n_backup_query_tables_list()
2540
backup Pointer to Query_tables_list instance to be used for backup
2543
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2549
Restore state of Query_tables_list for this LEX from backup.
2552
restore_backup_query_tables_list()
2553
backup Pointer to Query_tables_list instance used for backup
2556
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2562
Checks for usage of routines and/or tables in a parsed statement
2565
st_lex:table_or_sp_used()
2568
false No routines and tables used
2569
true Either or both routines and tables are used.
2572
bool st_lex::table_or_sp_used()
2574
if (sroutines.records || query_tables)
2582
Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2585
fix_prepare_info_in_table_list()
2586
session Thread handle
2587
tbl List of tables to process
2590
Perform end-end-of prepare fixup for list of tables, if any of the tables
2591
is a merge-algorithm VIEW, recursively fix up its underlying tables as
2596
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
2598
for (; tbl; tbl= tbl->next_local)
2602
tbl->prep_on_expr= tbl->on_expr;
2603
tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
2605
fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
2611
There are st_select_lex::add_table_to_list &
2612
st_select_lex::set_lock_for_tables are in sql_parse.cc
2614
st_select_lex::print is in sql_select.cc
2616
st_select_lex_unit::prepare, st_select_lex_unit::exec,
2617
st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2618
st_select_lex_unit::change_result
2133
2619
are in sql_union.cc