1 /* Copyright (c) 2006, 2021, 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"
27 #include "handler.h"                 // enum_schema_tables
28 #include "mysqld_thd_manager.h"      // Find_THD_Impl
29 #include "sql_class.h"               // THD
30 
31 class Comp_creator;
32 class Item;
33 class Object_creation_ctx;
34 class Parser_state;
35 class Table_ident;
36 struct LEX;
37 struct Parse_context;
38 struct TABLE_LIST;
39 class THD;
40 union COM_DATA;
41 typedef struct st_lex_user LEX_USER;
42 typedef struct st_order ORDER;
43 typedef class st_select_lex SELECT_LEX;
44 
45 
46 extern "C" int test_if_data_home_dir(const char *dir);
47 
48 bool stmt_causes_implicit_commit(const THD *thd, uint mask);
49 
50 #ifndef NDEBUG
51 extern void turn_parser_debug_on();
52 #endif
53 
54 bool parse_sql(THD *thd,
55                Parser_state *parser_state,
56                Object_creation_ctx *creation_ctx);
57 
58 void free_items(Item *item);
59 void cleanup_items(Item *item);
60 
61 Comp_creator *comp_eq_creator(bool invert);
62 Comp_creator *comp_equal_creator(bool invert);
63 Comp_creator *comp_ge_creator(bool invert);
64 Comp_creator *comp_gt_creator(bool invert);
65 Comp_creator *comp_le_creator(bool invert);
66 Comp_creator *comp_lt_creator(bool invert);
67 Comp_creator *comp_ne_creator(bool invert);
68 
69 int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
70                          enum enum_schema_tables schema_table_idx);
71 void get_default_definer(THD *thd, LEX_USER *definer);
72 LEX_USER *create_default_definer(THD *thd);
73 LEX_USER *get_current_user(THD *thd, LEX_USER *user);
74 bool check_string_char_length(const LEX_CSTRING &str, const char *err_msg,
75                               size_t max_char_length, const CHARSET_INFO *cs,
76                               bool no_error);
77 const CHARSET_INFO* merge_charset_and_collation(const CHARSET_INFO *cs,
78                                                 const CHARSET_INFO *cl);
79 bool check_host_name(const LEX_CSTRING &str);
80 bool mysql_test_parse_for_slave(THD *thd);
81 bool is_update_query(enum enum_sql_command command);
82 bool is_explainable_query(enum enum_sql_command command);
83 bool is_log_table_write_query(enum enum_sql_command command);
84 bool alloc_query(THD *thd, const char *packet, size_t packet_length);
85 void mysql_parse(THD *thd, Parser_state *parser_state, bool update_userstat);
86 void mysql_reset_thd_for_next_command(THD *thd);
87 void create_select_for_variable(Parse_context *pc, const char *var_name);
88 void create_table_set_open_action_and_adjust_tables(LEX *lex);
89 void mysql_init_multi_delete(LEX *lex);
90 void create_table_set_open_action_and_adjust_tables(LEX *lex);
91 int mysql_execute_command(THD *thd, bool first_level = false);
92 bool do_command(THD *thd);
93 bool dispatch_command(THD *thd, const COM_DATA *com_data,
94                       enum enum_server_command command);
95 bool append_file_to_dir(THD *thd, const char **filename_ptr,
96                         const char *table_name);
97 bool append_file_to_dir(THD *thd, const char **filename_ptr,
98                         const char *table_name);
99 void execute_init_command(THD *thd, LEX_STRING *init_command,
100                           mysql_rwlock_t *var_lock);
101 bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum enum_field_types type,
102 		       char *length, char *decimal,
103 		       uint type_modifier,
104 		       Item *default_value, Item *on_update_value,
105 		       LEX_STRING *comment,
106 		       char *change, List<String> *interval_list,
107 		       const CHARSET_INFO *cs,
108 		       uint uint_geom_type,
109 		       const LEX_CSTRING *zip_dict,
110                        Generated_column *gcol_info);
111 void add_to_list(SQL_I_List<ORDER> &list, ORDER *order);
112 void add_join_on(TABLE_LIST *b,Item *expr);
113 void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields,
114                       SELECT_LEX *lex);
115 bool push_new_name_resolution_context(Parse_context *pc,
116                                       TABLE_LIST *left_op,
117                                       TABLE_LIST *right_op);
118 void store_position_for_column(const char *name);
119 void init_update_queries(void);
120 Item *negate_expression(Parse_context *pc, Item *expr);
121 bool check_stack_overrun(THD *thd, long margin, uchar *dummy);
122 void killall_non_super_threads(THD *thd);
123 bool shutdown(THD *thd, enum mysql_enum_shutdown_level level, enum enum_server_command command);
124 
125 /* Variables */
126 
127 extern uint sql_command_flags[];
128 extern const LEX_STRING command_name[];
129 
130 size_t get_command_name_len(void);
131 
132 // Statement timeout function(s)
133 void reset_statement_timer(THD *thd);
134 
is_supported_parser_charset(const CHARSET_INFO * cs)135 inline bool is_supported_parser_charset(const CHARSET_INFO *cs)
136 {
137   return (cs->mbminlen == 1);
138 }
139 
140 bool sqlcom_can_generate_row_events(enum enum_sql_command command);
141 
142 /**
143   Callback function used by kill_one_thread and timer_notify functions
144   to find "thd" based on the thread id.
145 
146   @note It acquires LOCK_thd_data mutex when it finds matching thd.
147   It is the responsibility of the caller to release this mutex.
148 */
149 class Find_thd_with_id: public Find_THD_Impl
150 {
151 public:
Find_thd_with_id(ulong value,bool daemon_allowed)152   Find_thd_with_id(ulong value, bool daemon_allowed):
153     m_id(value), m_daemon_allowed(daemon_allowed) {}
operator()154   virtual bool operator()(THD *thd)
155   {
156     if (!m_daemon_allowed && thd->get_command() == COM_DAEMON)
157       return false;
158     if (thd->thread_id() == m_id)
159     {
160       mysql_mutex_lock(&thd->LOCK_thd_data);
161       return true;
162     }
163     return false;
164   }
165 private:
166   ulong m_id;
167   const bool  m_daemon_allowed;
168 };
169 
170 
171 #ifdef HAVE_REPLICATION
172 bool all_tables_not_ok(THD *thd, TABLE_LIST *tables);
173 #endif /*HAVE_REPLICATION*/
174 bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables);
175 
176 bool lock_binlog_for_backup(THD *thd);
177 
178 #endif /* SQL_PARSE_INCLUDED */
179