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 */
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
20
#ifndef DRIZZLE_SERVER_SQL_LEX_H
17
21
#define DRIZZLE_SERVER_SQL_LEX_H
20
24
@defgroup Semantic_Analysis Semantic Analysis
27
#include <drizzled/sql_udf.h>
28
#include <drizzled/name_resolution_context.h>
29
#include <drizzled/item/subselect.h>
30
#include <drizzled/item/param.h>
31
#include <drizzled/item/outer_ref.h>
32
#include <drizzled/table_list.h>
33
#include <drizzled/function/math/real.h>
34
#include <drizzled/alter_drop.h>
35
#include <drizzled/alter_column.h>
36
#include <drizzled/key.h>
37
#include <drizzled/foreign_key.h>
38
#include <drizzled/item/param.h>
40
class select_result_interceptor;
41
class virtual_column_info;
23
43
/* YACC and LEX Definitions */
25
45
/* These may not be declared yet */
27
47
class sql_exchange;
32
52
The following hack is needed because mysql_yacc.cc does not define
33
53
YYSTYPE before including this file
39
#define LEX_YYSTYPE void *
41
#if defined(DRIZZLE_LEX)
42
#include "lex_symbol.h"
44
#define LEX_YYSTYPE YYSTYPE *
46
#define LEX_YYSTYPE void *
52
When a command is added here, be sure it's also added in mysqld.cc
53
in "struct show_var_st status_vars[]= {" ...
55
If the command returns a result set or is not allowed in stored
56
functions or triggers, please also make sure that
57
sp_get_flags_for_command (sp_head.cc) returns proper flags for the
61
enum enum_sql_command {
62
SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
63
SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
64
SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
65
SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
66
SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
67
SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
68
SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
69
SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
70
SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
71
SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
72
SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
73
SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
74
SQLCOM_OPTIMIZE, SQLCOM_CHECK,
75
SQLCOM_ASSIGN_TO_KEYCACHE,
76
SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
77
SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
78
SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
79
SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
80
SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
82
SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
83
SQLCOM_SHOW_OPEN_TABLES,
84
SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
85
SQLCOM_SHOW_BINLOG_EVENTS,
86
SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
88
SQLCOM_BINLOG_BASE64_EVENT,
91
When a command is added here, be sure it's also added in mysqld.cc
92
in "struct show_var_st status_vars[]= {" ...
94
/* This should be the last !!! */
57
# include <drizzled/set_var.h>
58
# include <drizzled/item/func.h>
60
# define LEX_YYSTYPE void *
62
# if defined(DRIZZLE_LEX)
63
# include <drizzled/lex_symbol.h>
64
# include <drizzled/sql_yacc.h>
65
# define LEX_YYSTYPE YYSTYPE *
67
# define LEX_YYSTYPE void *
68
# endif /* defined(DRIZZLE_LEX) */
69
# endif /* DRIZZLE_YACC */
70
#endif /* DRIZZLE_SERVER */
98
72
// describe/explain types
99
73
#define DESCRIBE_NORMAL 1
365
335
virtual bool set_braces(bool value);
366
336
virtual bool inc_in_sum_expr();
367
337
virtual uint32_t get_in_sum_expr();
368
virtual TABLE_LIST* get_table_list();
338
virtual TableList* get_table_list();
369
339
virtual List<Item>* get_item_list();
370
340
virtual uint32_t get_table_join_options();
371
virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
341
virtual TableList *add_table_to_list(Session *session, Table_ident *table,
372
342
LEX_STRING *alias,
373
343
uint32_t table_options,
374
344
thr_lock_type flags= TL_UNLOCK,
375
345
List<Index_hint> *hints= 0,
376
346
LEX_STRING *option= 0);
377
virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
347
virtual void set_lock_for_tables(thr_lock_type)
380
350
friend class st_select_lex_unit;
381
friend bool mysql_new_select(struct st_lex *lex, bool move_down);
351
friend bool mysql_new_select(LEX *lex, bool move_down);
383
353
void fast_exclude();
385
355
typedef class st_select_lex_node SELECT_LEX_NODE;
388
SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
358
SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
392
362
class select_result;
394
364
class select_union;
395
365
class st_select_lex_unit: public st_select_lex_node {
397
TABLE_LIST result_table_list;
367
TableList result_table_list;
398
368
select_union *union_result;
399
TABLE *table; /* temporary table using for appending UNION results */
369
Table *table; /* temporary table using for appending UNION results */
401
371
select_result *result;
402
372
uint64_t found_rows_for_union;
493
463
Name_resolution_context context;
495
465
Item *where, *having; /* WHERE & HAVING clauses */
496
Item *prep_where; /* saved WHERE clause for prepared statement processing */
497
Item *prep_having;/* saved HAVING clause for prepared statement processing */
498
466
/* Saved values of the WHERE and HAVING clauses*/
499
467
Item::cond_result cond_value, having_value;
500
468
/* point on lex in which it was created, used in view subquery detection */
502
470
enum olap_type olap;
503
/* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
471
/* FROM clause - points to the beginning of the TableList::next_local list. */
504
472
SQL_LIST table_list;
505
473
SQL_LIST group_list; /* GROUP BY clause. */
506
474
List<Item> item_list; /* list of fields & expressions */
507
475
List<String> interval_list;
508
476
bool is_item_list_lookup;
510
Despite their names, the following are used in unions. This should
478
Despite their names, the following are used in unions. This should
511
479
be rewritten. -Brian
513
481
List<Item_real_func> *ftfunc_list;
514
482
List<Item_real_func> ftfunc_list_alloc;
515
483
JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
516
List<TABLE_LIST> top_join_list; /* join list of the top level */
517
List<TABLE_LIST> *join_list; /* list for the currently parsed join */
518
TABLE_LIST *embedding; /* table embedding to the above list */
519
List<TABLE_LIST> sj_nests;
484
List<TableList> top_join_list; /* join list of the top level */
485
List<TableList> *join_list; /* list for the currently parsed join */
486
TableList *embedding; /* table embedding to the above list */
487
List<TableList> sj_nests;
521
489
Beginning of the list of leaves in a FROM clause, where the leaves
522
490
inlcude all base tables including view tables. The tables are connected
523
by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
491
by TableList::next_leaf, so leaf_tables points to the left-most leaf.
525
TABLE_LIST *leaf_tables;
493
TableList *leaf_tables;
526
494
const char *type; /* type of select for EXPLAIN */
528
496
SQL_LIST order_list; /* ORDER clause */
578
541
bool subquery_in_having;
579
542
/* true <=> this SELECT is correlated w.r.t. some ancestor select */
580
543
bool is_correlated;
582
This variable is required to ensure proper work of subqueries and
583
stored procedures. Generally, one should use the states of
584
Query_arena to determine if it's a statement prepare or first
585
execution of a stored procedure. However, in case when there was an
586
error during the first execution of a stored procedure, the SP body
587
is not expelled from the SP cache. Therefore, a deeply nested
588
subquery might be left unoptimized. So we need this per-subquery
589
variable to inidicate the optimization/execution state of every
590
subquery. Prepared statements work OK in that regard, as in
591
case of an error during prepare the PS is not created.
593
bool first_execution;
594
bool first_cond_optimization;
595
544
/* exclude this select from check of unique_table() */
596
545
bool exclude_from_table_unique_test;
597
546
/* List of fields that aren't under an aggregate function */
652
601
bool inc_in_sum_expr();
653
602
uint32_t get_in_sum_expr();
655
bool add_item_to_list(THD *thd, Item *item);
656
bool add_group_to_list(THD *thd, Item *item, bool asc);
657
bool add_order_to_list(THD *thd, Item *item, bool asc);
658
TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
604
bool add_item_to_list(Session *session, Item *item);
605
bool add_group_to_list(Session *session, Item *item, bool asc);
606
bool add_order_to_list(Session *session, Item *item, bool asc);
607
TableList* add_table_to_list(Session *session, Table_ident *table,
659
608
LEX_STRING *alias,
660
609
uint32_t table_options,
661
610
thr_lock_type flags= TL_UNLOCK,
662
611
List<Index_hint> *hints= 0,
663
612
LEX_STRING *option= 0);
664
TABLE_LIST* get_table_list();
665
bool init_nested_join(THD *thd);
666
TABLE_LIST *end_nested_join(THD *thd);
667
TABLE_LIST *nest_last_join(THD *thd);
668
void add_joined_table(TABLE_LIST *table);
669
TABLE_LIST *convert_right_join();
613
TableList* get_table_list();
614
bool init_nested_join(Session *session);
615
TableList *end_nested_join(Session *session);
616
TableList *nest_last_join(Session *session);
617
void add_joined_table(TableList *table);
618
TableList *convert_right_join();
670
619
List<Item>* get_item_list();
671
620
uint32_t get_table_join_options();
672
621
void set_lock_for_tables(thr_lock_type lock_type);
685
634
void cut_subtree() { slave= 0; }
686
635
bool test_limit();
688
friend void lex_start(THD *thd);
637
friend void lex_start(Session *session);
689
638
st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
690
639
void make_empty_select()
695
bool setup_ref_array(THD *thd, uint32_t order_group_num);
696
void print(THD *thd, String *str, enum_query_type query_type);
644
bool setup_ref_array(Session *session, uint32_t order_group_num);
645
void print(Session *session, String *str, enum_query_type query_type);
697
646
static void print_order(String *str,
699
648
enum_query_type query_type);
700
void print_limit(THD *thd, String *str, enum_query_type query_type);
701
void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
649
void print_limit(Session *session, String *str, enum_query_type query_type);
650
void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
703
652
Destroy the used execution plan (JOIN) of this subtree (this
704
653
SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
842
791
/* Global list of all tables used by this statement */
843
TABLE_LIST *query_tables;
792
TableList *query_tables;
844
793
/* Pointer to next_global member of last element in the previous list. */
845
TABLE_LIST **query_tables_last;
794
TableList **query_tables_last;
847
796
If non-0 then indicates that query requires prelocking and points to
848
797
next_global member of last own element in query table list (i.e. last
849
798
table which was not added to it as part of preparation to prelocking).
850
799
0 - indicates that this query does not need prelocking.
852
TABLE_LIST **query_tables_own_last;
854
Set of stored routines called by statement.
855
(Note that we use lazy-initialization for this hash).
857
enum { START_SROUTINES_HASH_SIZE= 16 };
860
List linking elements of 'sroutines' set. Allows you to add new elements
861
to this set as you iterate through the list of existing elements.
862
'sroutines_list_own_last' is pointer to ::next member of last element of
863
this list which represents routine which is explicitly used by query.
864
'sroutines_list_own_elements' number of explicitly used routines.
865
We use these two members for restoring of 'sroutines_list' to the state
866
in which it was right after query parsing.
868
SQL_LIST sroutines_list;
869
uchar **sroutines_list_own_last;
870
uint32_t sroutines_list_own_elements;
801
TableList **query_tables_own_last;
873
804
These constructor and destructor serve for creation/destruction
908
839
query_tables_own_last= 0;
913
Has the parser/scanner detected that this statement is unsafe?
915
inline bool is_stmt_unsafe() const {
916
return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
920
Flag the current (top-level) statement as unsafe.
922
The flag will be reset after the statement has finished.
925
inline void set_stmt_unsafe() {
926
binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
929
inline void clear_stmt_unsafe() {
930
binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
934
true if the parsed tree contains references to stored procedures
935
or functions, false otherwise
937
bool uses_stored_routines() const
938
{ return sroutines_list.elements != 0; }
941
enum enum_binlog_stmt_flag {
942
BINLOG_STMT_FLAG_UNSAFE,
943
BINLOG_STMT_FLAG_COUNT
947
Tells if the parsing stage detected properties of the statement,
948
for example: that some items require row-based binlogging to give
949
a reliable binlog/replication, or if we will use stored functions
950
or triggers which themselves need require row-based binlogging.
952
uint32_t binlog_stmt_flags;
1386
1273
LEX_STRING name;
1387
1274
char *help_arg;
1388
1275
char* to_log; /* For PURGE MASTER LOGS TO */
1389
char* x509_subject,*x509_issuer,*ssl_cipher;
1391
1277
sql_exchange *exchange;
1392
1278
select_result *result;
1393
1279
Item *default_value, *on_update_value;
1394
1280
LEX_STRING comment, ident;
1396
uchar* yacc_yyss, *yacc_yyvs;
1399
/* maintain a list of used plugins for this LEX */
1400
DYNAMIC_ARRAY plugins;
1401
plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
1282
unsigned char* yacc_yyss, *yacc_yyvs;
1284
virtual_column_info *vcol_info;
1403
1286
const CHARSET_INFO *charset;
1404
1287
bool text_string_is_7bit;
1405
1288
/* store original leaf_tables for INSERT SELECT and PS/SP */
1406
TABLE_LIST *leaf_tables_insert;
1289
TableList *leaf_tables_insert;
1408
1291
List<Key_part_spec> col_list;
1409
1292
List<Key_part_spec> ref_list;
1500
1390
Alter_info alter_info;
1503
field_list was created for view and should be removed before PS/SP
1506
bool empty_field_list_on_rset;
1509
1393
Pointers to part of LOAD DATA statement that should be rewritten
1510
1394
during replication ("LOCAL 'filename' REPLACE INTO" part).
1512
1396
const char *fname_start;
1513
1397
const char *fname_end;
1516
During name resolution search only in the table list given by
1400
During name resolution search only in the table list given by
1517
1401
Name_resolution_context::first_name_resolution_table and
1518
1402
Name_resolution_context::last_name_resolution_table
1519
(see Item_field::fix_fields()).
1403
(see Item_field::fix_fields()).
1521
1405
bool use_only_table_context;
1523
1407
bool escape_used;
1524
1408
bool is_lex_started; /* If lex_start() did run. For debugging. */
1530
1414
destroy_query_tables_list();
1531
plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1532
delete_dynamic(&plugins);
1535
TABLE_LIST *unlink_first_table(bool *link_to_local);
1536
void link_first_table_back(TABLE_LIST *first, bool link_to_local);
1417
TableList *unlink_first_table(bool *link_to_local);
1418
void link_first_table_back(TableList *first, bool link_to_local);
1537
1419
void first_lists_tables_same();
1539
bool can_be_merged();
1540
bool can_use_merged();
1541
bool can_not_use_merged();
1542
1421
bool only_view_structure();
1543
1422
bool need_correct_ident();
1561
1440
return context_stack.head();
1564
Restore the LEX and THD in case of a parse error.
1443
Restore the LEX and Session in case of a parse error.
1566
static void cleanup_lex_after_parse_error(THD *thd);
1568
void reset_n_backup_query_tables_list(Query_tables_list *backup);
1569
void restore_backup_query_tables_list(Query_tables_list *backup);
1571
bool table_or_sp_used();
1445
static void cleanup_lex_after_parse_error(Session *session);
1574
1448
@brief check if the statement is a single-level join
1575
1449
@return result of the check
1576
@retval true The statement doesn't contain subqueries, unions and
1450
@retval true The statement doesn't contain subqueries, unions and
1577
1451
stored procedure calls.
1578
1452
@retval false There are subqueries, UNIONs or stored procedure calls.
1580
bool is_single_level_stmt()
1454
bool is_single_level_stmt()
1583
1457
This check exploits the fact that the last added to all_select_list is
1584
on its top. So select_lex (as the first added) will be at the tail
1458
on its top. So select_lex (as the first added) will be at the tail
1587
if (&select_lex == all_selects_list && !sroutines.records)
1461
if (&select_lex == all_selects_list)
1589
1463
assert(!all_selects_list->next_select_in_list());
1596
struct st_lex_local: public st_lex
1470
struct st_lex_local: public LEX
1598
1472
static void *operator new(size_t size) throw()
1604
1478
return (void*) alloc_root(mem_root, (uint32_t) size);
1606
static void operator delete(void *ptr __attribute__((unused)),
1607
size_t size __attribute__((unused)))
1480
static void operator delete(void *, size_t)
1608
1481
{ TRASH(ptr, size); }
1609
static void operator delete(void *ptr __attribute__((unused)),
1610
MEM_ROOT *mem_root __attribute__((unused)))
1482
static void operator delete(void *, MEM_ROOT *)
1611
1483
{ /* Never called */ }
1614
1486
extern void lex_init(void);
1615
1487
extern void lex_free(void);
1616
extern void lex_start(THD *thd);
1488
extern void lex_start(Session *session);
1617
1489
extern void lex_end(LEX *lex);
1619
1491
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
1621
1493
extern bool is_lex_native_function(const LEX_STRING *name);
1495
int lex_casecmp(const char *s, const char *t, uint32_t len);
1624
1498
@} (End of group Semantic_Analysis)