1 /* Copyright (c) 2006, 2020, Oracle and/or its affiliates.
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;
47 class Object_creation_ctx;
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);
83 LEX_USER *create_default_definer(THD *thd);
84 LEX_USER *get_current_user(THD *thd, LEX_USER *user);
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);
91 bool merge_sp_var_charset_and_collation(const CHARSET_INFO *charset,
92                                         const CHARSET_INFO *collation,
93                                         const CHARSET_INFO **to);
94 bool check_host_name(const LEX_CSTRING &str);
95 bool mysql_test_parse_for_slave(THD *thd);
96 bool is_update_query(enum enum_sql_command command);
97 bool is_explainable_query(enum enum_sql_command command);
98 bool is_log_table_write_query(enum enum_sql_command command);
99 bool alloc_query(THD *thd, const char *packet, size_t packet_length);
100 void mysql_parse(THD *thd, Parser_state *parser_state);
101 void mysql_reset_thd_for_next_command(THD *thd);
102 bool create_select_for_variable(Parse_context *pc, const char *var_name);
103 void create_table_set_open_action_and_adjust_tables(LEX *lex);
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,
107                       enum enum_server_command command);
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);
117 bool push_new_name_resolution_context(Parse_context *pc, TABLE_LIST *left_op,
118                                       TABLE_LIST *right_op);
119 void init_sql_command_flags(void);
120 const CHARSET_INFO *get_bin_collation(const CHARSET_INFO *cs);
121 void killall_non_super_threads(THD *thd);
122 bool shutdown(THD *thd, enum mysql_enum_shutdown_level level);
123 bool show_precheck(THD *thd, LEX *lex, bool lock);
124 
125 /* Variables */
126 
127 extern uint sql_command_flags[];
128 extern const LEX_CSTRING command_name[];
129 
130 bool sqlcom_can_generate_row_events(enum enum_sql_command command);
131 
132 bool all_tables_not_ok(THD *thd, TABLE_LIST *tables);
133 bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables);
134 
135 bool execute_show(THD *thd, TABLE_LIST *all_tables);
136 
137 // TODO: remove after refactoring of ALTER DATABASE:
138 bool set_default_charset(HA_CREATE_INFO *create_info,
139                          const CHARSET_INFO *value);
140 // TODO: remove after refactoring of ALTER DATABASE:
141 bool set_default_collation(HA_CREATE_INFO *create_info,
142                            const CHARSET_INFO *value);
143 
144 /* Bits in sql_command_flags */
145 
146 #define CF_CHANGES_DATA (1U << 0)
147 /* The 2nd bit is unused -- it used to be CF_HAS_ROW_COUNT. */
148 #define CF_STATUS_COMMAND (1U << 2)
149 #define CF_SHOW_TABLE_COMMAND (1U << 3)
150 #define CF_WRITE_LOGS_COMMAND (1U << 4)
151 /**
152   Must be set for SQL statements that may contain
153   Item expressions and/or use joins and tables.
154   Indicates that the parse tree of such statement may
155   contain rule-based optimizations that depend on metadata
156   (i.e. number of columns in a table), and consequently
157   that the statement must be re-prepared whenever
158   referenced metadata changes. Must not be set for
159   statements that themselves change metadata, e.g. RENAME,
160   ALTER and other DDL, since otherwise will trigger constant
161   reprepare. Consequently, complex item expressions and
162   joins are currently prohibited in these statements.
163 */
164 #define CF_REEXECUTION_FRAGILE (1U << 5)
165 /**
166   Implicitly commit before the SQL statement is executed.
167 
168   Statements marked with this flag will cause any active
169   transaction to end (commit) before proceeding with the
170   command execution.
171 
172   This flag should be set for statements that probably can't
173   be rolled back or that do not expect any previously metadata
174   locked tables.
175 */
176 #define CF_IMPLICIT_COMMIT_BEGIN (1U << 6)
177 /**
178   Implicitly commit after the SQL statement.
179 
180   Statements marked with this flag are automatically committed
181   at the end of the statement.
182 
183   This flag should be set for statements that will implicitly
184   open and take metadata locks on system tables that should not
185   be carried for the whole duration of a active transaction.
186 */
187 #define CF_IMPLICIT_COMMIT_END (1U << 7)
188 /**
189   CF_IMPLICIT_COMMIT_BEGIN and CF_IMPLICIT_COMMIT_END are used
190   to ensure that the active transaction is implicitly committed
191   before and after every DDL statement and any statement that
192   modifies our currently non-transactional system tables.
193 */
194 #define CF_AUTO_COMMIT_TRANS (CF_IMPLICIT_COMMIT_BEGIN | CF_IMPLICIT_COMMIT_END)
195 
196 /**
197   Diagnostic statement.
198   Diagnostic statements:
199   - SHOW WARNING
200   - SHOW ERROR
201   - GET DIAGNOSTICS (WL#2111)
202   do not modify the Diagnostics Area during execution.
203 */
204 #define CF_DIAGNOSTIC_STMT (1U << 8)
205 
206 /**
207   Identifies statements that may generate row events
208   and that may end up in the binary log.
209 */
210 #define CF_CAN_GENERATE_ROW_EVENTS (1U << 9)
211 
212 /**
213   Identifies statements which may deal with temporary tables and for which
214   temporary tables should be pre-opened to simplify privilege checks.
215 */
216 #define CF_PREOPEN_TMP_TABLES (1U << 10)
217 
218 /**
219   Identifies statements for which open handlers should be closed in the
220   beginning of the statement.
221 */
222 #define CF_HA_CLOSE (1U << 11)
223 
224 /**
225   Identifies statements that can be explained with EXPLAIN.
226 */
227 #define CF_CAN_BE_EXPLAINED (1U << 12)
228 
229 /** Identifies statements which may generate an optimizer trace */
230 #define CF_OPTIMIZER_TRACE (1U << 14)
231 
232 /**
233    Identifies statements that should always be disallowed in
234    read only transactions.
235 */
236 #define CF_DISALLOW_IN_RO_TRANS (1U << 15)
237 
238 /**
239   Identifies statements and commands that can be used with Protocol Plugin
240 */
241 #define CF_ALLOW_PROTOCOL_PLUGIN (1U << 16)
242 
243 /**
244   Identifies statements (typically DDL) which needs auto-commit mode
245   temporarily turned off.
246 
247   @note This is necessary to prevent InnoDB from automatically committing
248         InnoDB transaction each time data-dictionary tables are closed
249         after being updated.
250 
251   @note This is also necessary for ACL DDL, so the code which
252         saves GTID state or slave state in the system tables at the
253         commit time works correctly. This code does statement commit
254         on low-level (see System_table_access:: close_table()) and
255         thus can pre-maturely commit DDL if @@autocommit=1.
256 */
257 #define CF_NEEDS_AUTOCOMMIT_OFF (1U << 17)
258 
259 /**
260   Identifies statements which can return rows of data columns (SELECT, SHOW ...)
261 */
262 #define CF_HAS_RESULT_SET (1U << 18)
263 
264 /**
265   Identifies DDL statements which can be atomic.
266   Having the bit ON does not yet define an atomic.
267   The property is used both on the master and slave.
268   On the master atomicity infers the binlog and gtid_executed system table.
269   On the slave it more involves the slave info table.
270 
271   @note At the momemnt of declaration the covered DDL subset coincides
272         with the of CF_NEEDS_AUTOCOMMIT_OFF.
273 */
274 #define CF_POTENTIAL_ATOMIC_DDL (1U << 19)
275 
276 /**
277   Statement is depending on the ACL cache, which can be disabled by the
278   --skip-grant-tables server option.
279 */
280 #define CF_REQUIRE_ACL_CACHE (1U << 20)
281 
282 /**
283   Identifies statements as SHOW commands using INFORMATION_SCHEMA system views.
284 */
285 #define CF_SHOW_USES_SYSTEM_VIEW (1U << 21)
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 */
315