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 "my_global.h"                          /* NO_EMBEDDED_ACCESS_CHECKS */
27 #include "sql_acl.h"                            /* GLOBAL_ACLS */
28 
29 class Comp_creator;
30 class Item;
31 class Object_creation_ctx;
32 class Parser_state;
33 struct TABLE_LIST;
34 class THD;
35 class Table_ident;
36 struct LEX;
37 
38 enum enum_mysql_completiontype {
39   ROLLBACK_RELEASE=-2, ROLLBACK=1,  ROLLBACK_AND_CHAIN=7,
40   COMMIT_RELEASE=-1,   COMMIT=0,    COMMIT_AND_CHAIN=6
41 };
42 
43 extern "C" int test_if_data_home_dir(const char *dir);
44 
45 bool stmt_causes_implicit_commit(const THD *thd, uint mask);
46 
47 bool select_precheck(THD *thd, LEX *lex, TABLE_LIST *tables,
48                      TABLE_LIST *first_table);
49 bool multi_update_precheck(THD *thd, TABLE_LIST *tables);
50 bool multi_delete_precheck(THD *thd, TABLE_LIST *tables);
51 int mysql_multi_update_prepare(THD *thd);
52 int mysql_multi_delete_prepare(THD *thd, uint *table_count);
53 bool mysql_insert_select_prepare(THD *thd);
54 bool update_precheck(THD *thd, TABLE_LIST *tables);
55 bool delete_precheck(THD *thd, TABLE_LIST *tables);
56 bool insert_precheck(THD *thd, TABLE_LIST *tables);
57 bool create_table_precheck(THD *thd, TABLE_LIST *tables,
58                            TABLE_LIST *create_table);
59 bool check_fk_parent_table_access(THD *thd,
60                                   const char *child_table_db,
61                                   HA_CREATE_INFO *create_info,
62                                   Alter_info *alter_info);
63 bool check_lock_view_underlying_table_access(THD *thd, TABLE_LIST *tbl,
64                                              bool *fake_lock_tables_acl);
65 
66 bool parse_sql(THD *thd,
67                Parser_state *parser_state,
68                Object_creation_ctx *creation_ctx);
69 
70 uint kill_one_thread(THD *thd, ulong id, bool only_kill_query);
71 
72 void free_items(Item *item);
73 void cleanup_items(Item *item);
74 
75 Comp_creator *comp_eq_creator(bool invert);
76 Comp_creator *comp_ge_creator(bool invert);
77 Comp_creator *comp_gt_creator(bool invert);
78 Comp_creator *comp_le_creator(bool invert);
79 Comp_creator *comp_lt_creator(bool invert);
80 Comp_creator *comp_ne_creator(bool invert);
81 
82 int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
83                          enum enum_schema_tables schema_table_idx);
84 void get_default_definer(THD *thd, LEX_USER *definer);
85 LEX_USER *create_default_definer(THD *thd);
86 LEX_USER *create_definer(THD *thd, LEX_STRING *user_name, LEX_STRING *host_name);
87 LEX_USER *get_current_user(THD *thd, LEX_USER *user);
88 bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
89                               uint max_byte_length);
90 bool check_string_char_length(LEX_STRING *str, const char *err_msg,
91                               uint max_char_length, const CHARSET_INFO *cs,
92                               bool no_error);
93 const CHARSET_INFO* merge_charset_and_collation(const CHARSET_INFO *cs,
94                                                 const CHARSET_INFO *cl);
95 bool check_host_name(LEX_STRING *str);
96 bool check_identifier_name(LEX_STRING *str, uint max_char_length,
97                            uint err_code, const char *param_for_err_msg);
98 bool mysql_test_parse_for_slave(THD *thd,char *inBuf,uint length);
99 bool is_update_query(enum enum_sql_command command);
100 bool is_explainable_query(enum enum_sql_command command);
101 bool is_log_table_write_query(enum enum_sql_command command);
102 bool alloc_query(THD *thd, const char *packet, uint packet_length);
103 void mysql_init_select(LEX *lex);
104 void mysql_parse(THD *thd, char *rawbuf, uint length,
105                  Parser_state *parser_state, bool update_userstat);
106 void mysql_reset_thd_for_next_command(THD *thd);
107 bool mysql_new_select(LEX *lex, bool move_down);
108 void create_select_for_variable(const char *var_name);
109 void create_table_set_open_action_and_adjust_tables(LEX *lex);
110 void mysql_init_multi_delete(LEX *lex);
111 bool multi_delete_set_locks_and_link_aux_tables(LEX *lex);
112 void create_table_set_open_action_and_adjust_tables(LEX *lex);
113 pthread_handler_t handle_bootstrap(void *arg);
114 int mysql_execute_command(THD *thd);
115 bool do_command(THD *thd);
116 void do_handle_bootstrap(THD *thd);
117 bool dispatch_command(enum enum_server_command command, THD *thd,
118 		      char* packet, uint packet_length);
119 void log_slow_statement(THD *thd);
120 bool log_slow_applicable(THD *thd);
121 void log_slow_do(THD *thd);
122 bool append_file_to_dir(THD *thd, const char **filename_ptr,
123                         const char *table_name);
124 bool append_file_to_dir(THD *thd, const char **filename_ptr,
125                         const char *table_name);
126 void execute_init_command(THD *thd, LEX_STRING *init_command,
127                           mysql_rwlock_t *var_lock);
128 bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum enum_field_types type,
129 		       char *length, char *decimal,
130 		       uint type_modifier,
131 		       Item *default_value, Item *on_update_value,
132 		       LEX_STRING *comment,
133 		       char *change, List<String> *interval_list,
134 		       const CHARSET_INFO *cs,
135 		       uint uint_geom_type, const LEX_CSTRING *zip_dict);
136 bool add_to_list(THD *thd, SQL_I_List<ORDER> &list, Item *group, bool asc);
137 void add_join_on(TABLE_LIST *b,Item *expr);
138 void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields,
139                       SELECT_LEX *lex);
140 bool push_new_name_resolution_context(THD *thd,
141                                       TABLE_LIST *left_op,
142                                       TABLE_LIST *right_op);
143 void store_position_for_column(const char *name);
144 void init_update_queries(void);
145 bool check_simple_select();
146 Item *negate_expression(THD *thd, Item *expr);
147 bool check_stack_overrun(THD *thd, long margin, uchar *dummy);
148 
149 /* Variables */
150 
151 extern const char* any_db;
152 extern uint sql_command_flags[];
153 extern uint server_command_flags[];
154 extern const LEX_STRING command_name[];
155 extern uint server_command_flags[];
156 
157 /* Inline functions */
check_identifier_name(LEX_STRING * str,uint err_code)158 inline bool check_identifier_name(LEX_STRING *str, uint err_code)
159 {
160   return check_identifier_name(str, NAME_CHAR_LEN, err_code, "");
161 }
162 
check_identifier_name(LEX_STRING * str)163 inline bool check_identifier_name(LEX_STRING *str)
164 {
165   return check_identifier_name(str, NAME_CHAR_LEN, 0, "");
166 }
167 
168 #ifndef NO_EMBEDDED_ACCESS_CHECKS
169 bool check_one_table_access(THD *thd, ulong privilege, TABLE_LIST *tables);
170 bool check_single_table_access(THD *thd, ulong privilege,
171 			   TABLE_LIST *tables, bool no_errors);
172 bool check_routine_access(THD *thd,ulong want_access,char *db,char *name,
173 			  bool is_proc, bool no_errors);
174 bool check_some_access(THD *thd, ulong want_access, TABLE_LIST *table);
175 bool check_some_routine_access(THD *thd, const char *db, const char *name, bool is_proc);
176 bool check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv,
177                   GRANT_INTERNAL_INFO *grant_internal_info,
178                   bool dont_check_global_grants, bool no_errors);
179 bool check_table_access(THD *thd, ulong requirements,TABLE_LIST *tables,
180                         bool any_combination_of_privileges_will_do,
181                         uint number,
182                         bool no_errors);
183 #else
check_one_table_access(THD * thd,ulong privilege,TABLE_LIST * tables)184 inline bool check_one_table_access(THD *thd, ulong privilege, TABLE_LIST *tables)
185 { return false; }
check_single_table_access(THD * thd,ulong privilege,TABLE_LIST * tables,bool no_errors)186 inline bool check_single_table_access(THD *thd, ulong privilege,
187 			   TABLE_LIST *tables, bool no_errors)
188 { return false; }
check_routine_access(THD * thd,ulong want_access,char * db,char * name,bool is_proc,bool no_errors)189 inline bool check_routine_access(THD *thd,ulong want_access,char *db,
190                                  char *name, bool is_proc, bool no_errors)
191 { return false; }
check_some_access(THD * thd,ulong want_access,TABLE_LIST * table)192 inline bool check_some_access(THD *thd, ulong want_access, TABLE_LIST *table)
193 {
194   table->grant.privilege= want_access;
195   return false;
196 }
check_some_routine_access(THD * thd,const char * db,const char * name,bool is_proc)197 inline bool check_some_routine_access(THD *thd, const char *db,
198                                       const char *name, bool is_proc)
199 { return false; }
check_access(THD *,ulong,const char *,ulong * save_priv,GRANT_INTERNAL_INFO *,bool,bool)200 inline bool check_access(THD *, ulong, const char *, ulong *save_priv,
201                          GRANT_INTERNAL_INFO *, bool, bool)
202 {
203   if (save_priv)
204     *save_priv= GLOBAL_ACLS;
205   return false;
206 }
207 inline bool
check_table_access(THD * thd,ulong requirements,TABLE_LIST * tables,bool any_combination_of_privileges_will_do,uint number,bool no_errors)208 check_table_access(THD *thd, ulong requirements,TABLE_LIST *tables,
209                    bool any_combination_of_privileges_will_do,
210                    uint number,
211                    bool no_errors)
212 { return false; }
213 #endif /*NO_EMBEDDED_ACCESS_CHECKS*/
214 
215 /* These were under the INNODB_COMPATIBILITY_HOOKS */
216 
217 bool check_global_access(THD *thd, ulong want_access);
218 
is_supported_parser_charset(const CHARSET_INFO * cs)219 inline bool is_supported_parser_charset(const CHARSET_INFO *cs)
220 {
221   return (cs->mbminlen == 1);
222 }
223 
224 
225 extern "C" bool sqlcom_can_generate_row_events(enum enum_sql_command command);
226 
227 bool lock_binlog_for_backup(THD *thd);
228 
229 #endif /* SQL_PARSE_INCLUDED */
230