MySQL  8.0.16
Source Code Documentation
sql_parse.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef SQL_PARSE_INCLUDED
24 #define SQL_PARSE_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "lex_string.h"
30 #include "m_ctype.h"
31 #include "my_command.h"
32 #include "my_sqlcommand.h"
33 #include "mysql_com.h" // enum_server_command
34 #include "sql/handler.h" // enum_schema_tables
35 
36 struct mysql_rwlock_t;
37 template <typename T>
38 class SQL_I_List;
39 
40 /**
41  @addtogroup GROUP_PARSER
42  @{
43 */
44 
45 class Comp_creator;
46 class Item;
48 class Parser_state;
49 class THD;
50 class Table_ident;
51 struct LEX;
52 struct LEX_USER;
53 struct ORDER;
54 struct Parse_context;
55 struct TABLE_LIST;
56 union COM_DATA;
57 
58 extern "C" int test_if_data_home_dir(const char *dir);
59 
60 bool stmt_causes_implicit_commit(const THD *thd, uint mask);
61 
62 #ifndef DBUG_OFF
63 extern void turn_parser_debug_on();
64 #endif
65 
66 bool parse_sql(THD *thd, Parser_state *parser_state,
67  Object_creation_ctx *creation_ctx);
68 
69 void free_items(Item *item);
70 void cleanup_items(Item *item);
71 
72 Comp_creator *comp_eq_creator(bool invert);
73 Comp_creator *comp_equal_creator(bool invert);
74 Comp_creator *comp_ge_creator(bool invert);
75 Comp_creator *comp_gt_creator(bool invert);
76 Comp_creator *comp_le_creator(bool invert);
77 Comp_creator *comp_lt_creator(bool invert);
78 Comp_creator *comp_ne_creator(bool invert);
79 
80 int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
81  enum enum_schema_tables schema_table_idx);
82 void get_default_definer(THD *thd, LEX_USER *definer);
85 bool check_string_char_length(const LEX_CSTRING &str, const char *err_msg,
86  size_t max_char_length, const CHARSET_INFO *cs,
87  bool no_error);
88 bool merge_charset_and_collation(const CHARSET_INFO *charset,
89  const CHARSET_INFO *collation,
90  const CHARSET_INFO **to);
92  const CHARSET_INFO *collation,
93  const CHARSET_INFO **to);
94 bool check_host_name(const LEX_CSTRING &str);
99 bool alloc_query(THD *thd, const char *packet, size_t packet_length);
100 void mysql_parse(THD *thd, Parser_state *parser_state);
102 bool create_select_for_variable(Parse_context *pc, const char *var_name);
104 int mysql_execute_command(THD *thd, bool first_level = false);
105 bool do_command(THD *thd);
106 bool dispatch_command(THD *thd, const COM_DATA *com_data,
108 bool prepare_index_and_data_dir_path(THD *thd, const char **data_file_name,
109  const char **index_file_name,
110  const char *table_name);
111 int append_file_to_dir(THD *thd, const char **filename_ptr,
112  const char *table_name);
113 void execute_init_command(THD *thd, LEX_STRING *init_command,
114  mysql_rwlock_t *var_lock);
115 void add_to_list(SQL_I_List<ORDER> &list, ORDER *order);
116 void add_join_on(TABLE_LIST *b, Item *expr);
118  TABLE_LIST *right_op);
119 void init_sql_command_flags(void);
122 void killall_non_super_threads(THD *thd);
123 bool shutdown(THD *thd, enum mysql_enum_shutdown_level level);
124 bool show_precheck(THD *thd, LEX *lex, bool lock);
125 
126 /* Variables */
127 
128 extern uint sql_command_flags[];
129 extern const LEX_STRING command_name[];
130 
132  return (cs->mbminlen == 1);
133 }
134 
136 
137 bool all_tables_not_ok(THD *thd, TABLE_LIST *tables);
139 
140 bool execute_show(THD *thd, TABLE_LIST *all_tables);
141 
142 // TODO: remove after refactoring of ALTER DATABASE:
143 bool set_default_charset(HA_CREATE_INFO *create_info,
144  const CHARSET_INFO *value);
145 // TODO: remove after refactoring of ALTER DATABASE:
146 bool set_default_collation(HA_CREATE_INFO *create_info,
147  const CHARSET_INFO *value);
148 
149 /* Bits in sql_command_flags */
150 
151 #define CF_CHANGES_DATA (1U << 0)
152 /* The 2nd bit is unused -- it used to be CF_HAS_ROW_COUNT. */
153 #define CF_STATUS_COMMAND (1U << 2)
154 #define CF_SHOW_TABLE_COMMAND (1U << 3)
155 #define CF_WRITE_LOGS_COMMAND (1U << 4)
156 /**
157  Must be set for SQL statements that may contain
158  Item expressions and/or use joins and tables.
159  Indicates that the parse tree of such statement may
160  contain rule-based optimizations that depend on metadata
161  (i.e. number of columns in a table), and consequently
162  that the statement must be re-prepared whenever
163  referenced metadata changes. Must not be set for
164  statements that themselves change metadata, e.g. RENAME,
165  ALTER and other DDL, since otherwise will trigger constant
166  reprepare. Consequently, complex item expressions and
167  joins are currently prohibited in these statements.
168 */
169 #define CF_REEXECUTION_FRAGILE (1U << 5)
170 /**
171  Implicitly commit before the SQL statement is executed.
172 
173  Statements marked with this flag will cause any active
174  transaction to end (commit) before proceeding with the
175  command execution.
176 
177  This flag should be set for statements that probably can't
178  be rolled back or that do not expect any previously metadata
179  locked tables.
180 */
181 #define CF_IMPLICIT_COMMIT_BEGIN (1U << 6)
182 /**
183  Implicitly commit after the SQL statement.
184 
185  Statements marked with this flag are automatically committed
186  at the end of the statement.
187 
188  This flag should be set for statements that will implicitly
189  open and take metadata locks on system tables that should not
190  be carried for the whole duration of a active transaction.
191 */
192 #define CF_IMPLICIT_COMMIT_END (1U << 7)
193 /**
194  CF_IMPLICIT_COMMIT_BEGIN and CF_IMPLICIT_COMMIT_END are used
195  to ensure that the active transaction is implicitly committed
196  before and after every DDL statement and any statement that
197  modifies our currently non-transactional system tables.
198 */
199 #define CF_AUTO_COMMIT_TRANS (CF_IMPLICIT_COMMIT_BEGIN | CF_IMPLICIT_COMMIT_END)
200 
201 /**
202  Diagnostic statement.
203  Diagnostic statements:
204  - SHOW WARNING
205  - SHOW ERROR
206  - GET DIAGNOSTICS (WL#2111)
207  do not modify the Diagnostics Area during execution.
208 */
209 #define CF_DIAGNOSTIC_STMT (1U << 8)
210 
211 /**
212  Identifies statements that may generate row events
213  and that may end up in the binary log.
214 */
215 #define CF_CAN_GENERATE_ROW_EVENTS (1U << 9)
216 
217 /**
218  Identifies statements which may deal with temporary tables and for which
219  temporary tables should be pre-opened to simplify privilege checks.
220 */
221 #define CF_PREOPEN_TMP_TABLES (1U << 10)
222 
223 /**
224  Identifies statements for which open handlers should be closed in the
225  beginning of the statement.
226 */
227 #define CF_HA_CLOSE (1U << 11)
228 
229 /**
230  Identifies statements that can be explained with EXPLAIN.
231 */
232 #define CF_CAN_BE_EXPLAINED (1U << 12)
233 
234 /** Identifies statements which may generate an optimizer trace */
235 #define CF_OPTIMIZER_TRACE (1U << 14)
236 
237 /**
238  Identifies statements that should always be disallowed in
239  read only transactions.
240 */
241 #define CF_DISALLOW_IN_RO_TRANS (1U << 15)
242 
243 /**
244  Identifies statements and commands that can be used with Protocol Plugin
245 */
246 #define CF_ALLOW_PROTOCOL_PLUGIN (1U << 16)
247 
248 /**
249  Identifies statements (typically DDL) which needs auto-commit mode
250  temporarily turned off.
251 
252  @note This is necessary to prevent InnoDB from automatically committing
253  InnoDB transaction each time data-dictionary tables are closed
254  after being updated.
255 
256  @note This is also necessary for ACL DDL, so the code which
257  saves GTID state or slave state in the system tables at the
258  commit time works correctly. This code does statement commit
259  on low-level (see System_table_access:: close_table()) and
260  thus can pre-maturely commit DDL if @@autocommit=1.
261 */
262 #define CF_NEEDS_AUTOCOMMIT_OFF (1U << 17)
263 
264 /**
265  Identifies statements which can return rows of data columns (SELECT, SHOW ...)
266 */
267 #define CF_HAS_RESULT_SET (1U << 18)
268 
269 /**
270  Identifies DDL statements which can be atomic.
271  Having the bit ON does not yet define an atomic.
272  The property is used both on the master and slave.
273  On the master atomicity infers the binlog and gtid_executed system table.
274  On the slave it more involves the slave info table.
275 
276  @note At the momemnt of declaration the covered DDL subset coincides
277  with the of CF_NEEDS_AUTOCOMMIT_OFF.
278 */
279 #define CF_POTENTIAL_ATOMIC_DDL (1U << 19)
280 
281 /**
282  Statement is depending on the ACL cache, which can be disabled by the
283  --skip-grant-tables server option.
284 */
285 #define CF_REQUIRE_ACL_CACHE (1U << 20)
286 
287 /* Bits in server_command_flags */
288 
289 /**
290  Skip the increase of the global query id counter. Commonly set for
291  commands that are stateless (won't cause any change on the server
292  internal states). This is made obsolete as query id is incremented
293  for ping and statistics commands as well because of race condition
294  (Bug#58785).
295 */
296 #define CF_SKIP_QUERY_ID (1U << 0)
297 
298 /**
299  Skip the increase of the number of statements that clients have
300  sent to the server. Commonly used for commands that will cause
301  a statement to be executed but the statement might have not been
302  sent by the user (ie: stored procedure).
303 */
304 #define CF_SKIP_QUESTIONS (1U << 1)
305 
306 /**
307  1U << 16 is reserved for Protocol Plugin statements and commands
308 */
309 
310 /**
311  @} (end of group GROUP_PARSER)
312 */
313 
314 #endif /* SQL_PARSE_INCLUDED */
Comp_creator * comp_gt_creator(bool invert)
Definition: sql_parse.cc:6530
uint mbminlen
Definition: m_ctype.h:381
bool mysql_test_parse_for_slave(THD *thd)
Usable by the replication SQL thread only: just parse a query to know if it can be ignored because of...
Definition: sql_parse.cc:5257
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident, enum enum_schema_tables schema_table_idx)
Create a TABLE_LIST object for an INFORMATION_SCHEMA table.
Definition: sql_parse.cc:2258
bool merge_charset_and_collation(const CHARSET_INFO *charset, const CHARSET_INFO *collation, const CHARSET_INFO **to)
(end of group Runtime_Environment)
Definition: sql_parse.cc:7108
Definition: mysql_lex_string.h:34
Definition: sql_lex.h:3635
Order clause list element.
Definition: table.h:269
static mi_bit_type mask[]
Definition: mi_packrec.cc:138
bool is_log_table_write_query(enum enum_sql_command command)
Check if a sql command is allowed to write to log tables.
Definition: sql_parse.cc:1053
void add_join_on(TABLE_LIST *b, Item *expr)
Add an ON condition to the second operand of a JOIN ...
Definition: sql_parse.cc:6276
bool check_string_char_length(const LEX_CSTRING &str, const char *err_msg, size_t max_char_length, const CHARSET_INFO *cs, bool no_error)
Definition: sql_parse.cc:6772
Comp_creator * comp_le_creator(bool invert)
Definition: sql_parse.cc:6534
mysql_enum_shutdown_level
We want levels to be in growing order of hardness (because we use number comparisons).
Definition: mysql_com.h:929
const LEX_STRING command_name[]
Definition: sql_parse.cc:203
void free_items(Item *item)
Definition: sql_parse.cc:1121
void cleanup_items(Item *item)
This works because items are allocated with sql_alloc().
Definition: sql_parse.cc:1135
void get_default_definer(THD *thd, LEX_USER *definer)
Set the specified definer to the default value, which is the current user in the thread.
Definition: sql_parse.cc:6636
enum_sql_command
Definition: my_sqlcommand.h:45
void killall_non_super_threads(THD *thd)
Definition: sql_parse.cc:6436
Definition: mysql_lex_string.h:39
void mysql_reset_thd_for_next_command(THD *thd)
Reset the part of THD responsible for the state of command processing.
Definition: sql_parse.cc:4942
Object_creation_ctx – interface for creation context of database objects (views, stored routines, events, triggers).
Definition: table.h:186
Simple intrusive linked list.
Definition: item.h:80
bool prepare_index_and_data_dir_path(THD *thd, const char **data_file_name, const char **index_file_name, const char *table_name)
prepares the index and data directory path.
Definition: sql_parse.cc:6455
int mysql_execute_command(THD *thd, bool first_level=false)
Execute command saved in thd and lex->sql_command.
Definition: sql_parse.cc:2598
class udf_list * list
bool alloc_query(THD *thd, const char *packet, size_t packet_length)
Read query from packet and store in thd->query.
Definition: sql_parse.cc:2320
bool check_host_name(const LEX_CSTRING &str)
Check that host name string is valid.
Definition: sql_parse.cc:6840
Definition: com_data.h:106
bool is_update_query(enum enum_sql_command command)
Definition: sql_parse.cc:1038
bool all_tables_not_ok(THD *thd, TABLE_LIST *tables)
Returns true if all tables should be ignored.
Definition: sql_parse.cc:251
int test_if_data_home_dir(const char *dir)
Definition: sql_parse.cc:6801
LEX_USER * create_default_definer(THD *thd)
Create default definer for the specified THD.
Definition: sql_parse.cc:6674
bool do_command(THD *thd)
Read one command from connection and execute it (query or simple command).
Definition: sql_parse.cc:1153
Definition: sql_lex.h:321
bool parse_sql(THD *thd, Parser_state *parser_state, Object_creation_ctx *creation_ctx)
Transform an SQL statement into an AST that is ready for resolving, using the supplied parser state a...
Definition: sql_parse.cc:6935
bool create_select_for_variable(Parse_context *pc, const char *var_name)
Create a select to return the same output as &#39;SELECT @var_name&#39;.
Definition: sql_parse.cc:5045
Common definition between mysql server & client.
Item * negate_expression(Parse_context *pc, Item *expr)
negate given expression.
Definition: sql_parse.cc:6608
Comp_creator * comp_ne_creator(bool invert)
Definition: sql_parse.cc:6542
Internal state of the parser.
Definition: sql_lex.h:4278
Definition: table.h:2339
int append_file_to_dir(THD *thd, const char **filename_ptr, const char *table_name)
If pointer is not a null pointer, append filename to it.
Definition: sql_parse.cc:6495
bool show_precheck(THD *thd, LEX *lex, bool lock)
Do special checking for SHOW statements.
Definition: sql_parse.cc:4726
void create_table_set_open_action_and_adjust_tables(LEX *lex)
Set proper open mode and table type for element representing target table of CREATE TABLE statement...
Definition: sql_parse.cc:6578
bool dispatch_command(THD *thd, const COM_DATA *com_data, enum enum_server_command command)
Perform one connection-level (COM_XXXX) command.
Definition: sql_parse.cc:1448
char * user
Definition: mysqladmin.cc:57
Comp_creator * comp_equal_creator(bool invert)
Definition: sql_parse.cc:6521
bool stmt_causes_implicit_commit(const THD *thd, uint mask)
Returns whether the command in thd->lex->sql_command should cause an implicit commit.
Definition: sql_parse.cc:347
void execute_init_command(THD *thd, LEX_STRING *init_command, mysql_rwlock_t *var_lock)
Definition: sql_parse.cc:1073
bool shutdown(THD *thd, enum mysql_enum_shutdown_level level)
Shutdown the mysqld server.
Definition: sql_parse.cc:2201
Comp_creator * comp_eq_creator(bool invert)
Definition: sql_parse.cc:6517
Abstract factory interface for creating comparison predicates.
Definition: item_cmpfunc.h:340
Definition: item.h:666
unsigned int uint
Definition: uca-dump.cc:29
bool set_default_collation(HA_CREATE_INFO *create_info, const CHARSET_INFO *value)
Definition: parse_tree_nodes.cc:1520
enum_schema_tables
Definition: handler.h:864
Definition: m_ctype.h:358
uint sql_command_flags[]
Mark all commands that somehow changes a table.
Definition: sql_parse.cc:385
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:99
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
bool execute_show(THD *thd, TABLE_LIST *all_tables)
Definition: sql_parse.cc:4845
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:50
bool set_default_charset(HA_CREATE_INFO *create_info, const CHARSET_INFO *value)
Definition: parse_tree_nodes.cc:1497
command
Definition: version_token.cc:278
LEX_USER * get_current_user(THD *thd, LEX_USER *user)
Retuns information about user or current user.
Definition: sql_parse.cc:6696
const CHARSET_INFO * get_bin_collation(const CHARSET_INFO *cs)
Definition: sql_parse.cc:6293
bool sqlcom_can_generate_row_events(enum enum_sql_command command)
Definition: sql_parse.cc:1034
Definition: handler.h:2541
A better implementation of the UNIX ctype(3) library.
void add_to_list(SQL_I_List< ORDER > &list, ORDER *order)
save order by and tables in own lists.
Definition: sql_parse.cc:5448
void turn_parser_debug_on()
const string value("\alue\)
void mysql_parse(THD *thd, Parser_state *parser_state)
Parse a query.
Definition: sql_parse.cc:5082
Definition: table.h:2439
Comp_creator * comp_lt_creator(bool invert)
Definition: sql_parse.cc:6538
Comp_creator * comp_ge_creator(bool invert)
Definition: sql_parse.cc:6526
void init_sql_command_flags(void)
Definition: sql_parse.cc:388
bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables)
Definition: sql_parse.cc:322
bool push_new_name_resolution_context(Parse_context *pc, TABLE_LIST *left_op, TABLE_LIST *right_op)
Push a new name resolution context for a JOIN ...
Definition: sql_parse.cc:6250
bool merge_sp_var_charset_and_collation(const CHARSET_INFO *charset, const CHARSET_INFO *collation, const CHARSET_INFO **to)
Definition: sql_parse.cc:7122
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
bool is_explainable_query(enum enum_sql_command command)
Definition: sql_parse.cc:1043
bool is_supported_parser_charset(const CHARSET_INFO *cs)
Definition: sql_parse.h:131
const char * table_name
Definition: rules_table_service.cc:55