1 /*
2 * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is also distributed with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have included with MySQL.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License, version 2.0, for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26 #include "crud_cmd_handler.h"
27
28 #include "ngs_common/protocol_protobuf.h"
29
30 #include "xpl_log.h"
31 #include "expr_generator.h"
32 #include "xpl_error.h"
33 #include "update_statement_builder.h"
34 #include "find_statement_builder.h"
35 #include "delete_statement_builder.h"
36 #include "insert_statement_builder.h"
37 #include "view_statement_builder.h"
38 #include "notices.h"
39 #include "xpl_session.h"
40
41
42 namespace xpl
43 {
44
45 template <typename B, typename M>
execute(Session & session,const B & builder,const M & msg,Status_variable variable,bool (ngs::Protocol_encoder::* send_ok)())46 ngs::Error_code Crud_command_handler::execute(
47 Session &session, const B &builder, const M &msg, Status_variable variable,
48 bool (ngs::Protocol_encoder::*send_ok)())
49 {
50 session.update_status(variable);
51 m_qb.clear();
52 try
53 {
54 builder.build(msg);
55 }
56 catch (const Expression_generator::Error &exc)
57 {
58 return ngs::Error(exc.error(), "%s", exc.what());
59 }
60 catch (const ngs::Error_code &error)
61 {
62 return error;
63 }
64 log_debug("CRUD query: %s", m_qb.get().c_str());
65 Sql_data_context::Result_info info;
66 ngs::Error_code error = sql_execute<M>(session, info);
67 if (error)
68 return error_handling(error, msg);
69
70 notice_handling(session, info, msg);
71 (session.proto().*send_ok)();
72 return ngs::Success();
73 }
74
75
76 template <typename M>
notice_handling(Session & session,const Sql_data_context::Result_info & info,const M &) const77 void Crud_command_handler::notice_handling(
78 Session &session, const Sql_data_context::Result_info &info,
79 const M & /*msg*/) const
80 {
81 notice_handling_common(session, info);
82 }
83
84
notice_handling_common(Session & session,const Sql_data_context::Result_info & info) const85 void Crud_command_handler::notice_handling_common(
86 Session &session, const Sql_data_context::Result_info &info) const
87 {
88 if (info.num_warnings > 0 && session.options().get_send_warnings())
89 notices::send_warnings(session.data_context(), session.proto());
90
91 if (!info.message.empty())
92 notices::send_message(session.proto(), info.message);
93 }
94
95
96 template <typename M>
sql_execute(Session & session,Sql_data_context::Result_info & info) const97 ngs::Error_code Crud_command_handler::sql_execute(
98 Session &session, Sql_data_context::Result_info &info) const
99 {
100 return session.data_context().execute_sql_no_result(
101 m_qb.get().data(), m_qb.get().length(), info);
102 }
103
104
105 // -- Insert
execute_crud_insert(Session & session,const Mysqlx::Crud::Insert & msg)106 ngs::Error_code Crud_command_handler::execute_crud_insert(
107 Session &session, const Mysqlx::Crud::Insert &msg)
108 {
109 Expression_generator gen(m_qb, msg.args(), msg.collection().schema(),
110 is_table_data_model(msg));
111 return execute(session, Insert_statement_builder(gen), msg,
112 &Common_status_variables::m_crud_insert,
113 &ngs::Protocol_encoder::send_exec_ok);
114 }
115
116
117 template <>
error_handling(const ngs::Error_code & error,const Mysqlx::Crud::Insert & msg) const118 ngs::Error_code Crud_command_handler::error_handling(
119 const ngs::Error_code &error, const Mysqlx::Crud::Insert &msg) const
120 {
121 if (is_table_data_model(msg))
122 return error;
123
124 switch (error.error)
125 {
126 case ER_BAD_NULL_ERROR:
127 return ngs::Error(ER_X_DOC_ID_MISSING,
128 "Document is missing a required field");
129
130 case ER_BAD_FIELD_ERROR:
131 return ngs::Error(ER_X_DOC_REQUIRED_FIELD_MISSING,
132 "Table '%s' is not a document collection",
133 msg.collection().name().c_str());
134
135 case ER_DUP_ENTRY:
136 return ngs::Error(ER_X_DOC_ID_DUPLICATE,
137 "Document contains a field value that is not unique but "
138 "required to be");
139 }
140 return error;
141 }
142
143
144 template<>
notice_handling(Session & session,const Sql_data_context::Result_info & info,const Mysqlx::Crud::Insert & msg) const145 void Crud_command_handler::notice_handling(
146 Session &session, const Sql_data_context::Result_info &info,
147 const Mysqlx::Crud::Insert &msg) const
148 {
149 notice_handling_common(session, info);
150 notices::send_rows_affected(session.proto(), info.affected_rows);
151 if (is_table_data_model(msg))
152 notices::send_generated_insert_id(session.proto(), info.last_insert_id);
153 }
154
155
156 // -- Update
execute_crud_update(Session & session,const Mysqlx::Crud::Update & msg)157 ngs::Error_code Crud_command_handler::execute_crud_update(
158 Session &session, const Mysqlx::Crud::Update &msg)
159 {
160 Expression_generator gen(m_qb, msg.args(), msg.collection().schema(),
161 is_table_data_model(msg));
162 return execute(session, Update_statement_builder(gen), msg,
163 &Common_status_variables::m_crud_update,
164 &ngs::Protocol_encoder::send_exec_ok);
165 }
166
167
168 template<>
error_handling(const ngs::Error_code & error,const Mysqlx::Crud::Update & msg) const169 ngs::Error_code Crud_command_handler::error_handling(
170 const ngs::Error_code &error, const Mysqlx::Crud::Update &msg) const
171 {
172 if (is_table_data_model(msg))
173 return error;
174
175 switch (error.error)
176 {
177 case ER_INVALID_JSON_TEXT_IN_PARAM:
178 return ngs::Error(ER_X_BAD_UPDATE_DATA,
179 "Invalid data for update operation on "
180 "document collection table");
181 }
182 return error;
183 }
184
185
186 template<>
notice_handling(Session & session,const Sql_data_context::Result_info & info,const Mysqlx::Crud::Update & msg) const187 void Crud_command_handler::notice_handling(
188 Session &session, const Sql_data_context::Result_info &info,
189 const Mysqlx::Crud::Update &msg) const
190 {
191 notice_handling_common(session, info);
192 notices::send_rows_affected(session.proto(), info.affected_rows);
193 }
194
195
196 // -- Delete
execute_crud_delete(Session & session,const Mysqlx::Crud::Delete & msg)197 ngs::Error_code Crud_command_handler::execute_crud_delete(
198 Session &session, const Mysqlx::Crud::Delete &msg)
199 {
200 Expression_generator gen(m_qb, msg.args(), msg.collection().schema(),
201 is_table_data_model(msg));
202 return execute(session, Delete_statement_builder(gen), msg,
203 &Common_status_variables::m_crud_delete,
204 &ngs::Protocol_encoder::send_exec_ok);
205 }
206
207
208 template<>
notice_handling(Session & session,const Sql_data_context::Result_info & info,const Mysqlx::Crud::Delete & msg) const209 void Crud_command_handler::notice_handling(
210 Session &session, const Sql_data_context::Result_info &info,
211 const Mysqlx::Crud::Delete &msg) const
212 {
213 notice_handling_common(session, info);
214 notices::send_rows_affected(session.proto(), info.affected_rows);
215 }
216
217
218 // -- Find
execute_crud_find(Session & session,const Mysqlx::Crud::Find & msg)219 ngs::Error_code Crud_command_handler::execute_crud_find(
220 Session &session, const Mysqlx::Crud::Find &msg)
221 {
222 Expression_generator gen(m_qb, msg.args(), msg.collection().schema(),
223 is_table_data_model(msg));
224 return execute(session, Find_statement_builder(gen), msg,
225 &Common_status_variables::m_crud_find,
226 &ngs::Protocol_encoder::send_exec_ok);
227 }
228
229 namespace
230 {
check_message(const std::string & msg,const char * pattern,std::string::size_type & pos)231 inline bool check_message(const std::string &msg, const char *pattern,
232 std::string::size_type &pos)
233 {
234 return (pos = msg.find(pattern)) != std::string::npos;
235 }
236 } // namespace
237
238
239 template<>
error_handling(const ngs::Error_code & error,const Mysqlx::Crud::Find & msg) const240 ngs::Error_code Crud_command_handler::error_handling(
241 const ngs::Error_code &error, const Mysqlx::Crud::Find &msg) const
242 {
243 if (is_table_data_model(msg))
244 return error;
245
246 switch (error.error)
247 {
248 case ER_BAD_FIELD_ERROR:
249 std::string::size_type pos = std::string::npos;
250 if (check_message(error.message, "having clause", pos))
251 return ngs::Error(ER_X_EXPR_BAD_VALUE,
252 "Invalid expression in grouping criteria");
253
254 if (check_message(error.message, "where clause", pos))
255 return ngs::Error(ER_X_DOC_REQUIRED_FIELD_MISSING, "%sselection criteria",
256 error.message.substr(0, pos - 1).c_str());
257
258 if (check_message(error.message, "field list", pos))
259 return ngs::Error(ER_X_DOC_REQUIRED_FIELD_MISSING, "%scollection",
260 error.message.substr(0, pos - 1).c_str());
261 }
262 return error;
263 }
264
265
266 template <>
sql_execute(Session & session,Sql_data_context::Result_info & info) const267 ngs::Error_code Crud_command_handler::sql_execute<Mysqlx::Crud::Find>(
268 Session &session, Sql_data_context::Result_info &info) const
269 {
270 return session.data_context().execute_sql_and_stream_results(
271 m_qb.get().data(), m_qb.get().length(), false, info);
272 }
273
274
275 // -- View
execute_create_view(Session & session,const Mysqlx::Crud::CreateView & msg)276 ngs::Error_code Crud_command_handler::execute_create_view(
277 Session &session, const Mysqlx::Crud::CreateView &msg)
278 {
279 Expression_generator gen(m_qb, Expression_generator::Args(),
280 msg.collection().schema(), true);
281 return execute(session, View_statement_builder(gen), msg,
282 &Common_status_variables::m_crud_create_view,
283 &ngs::Protocol_encoder::send_ok);
284 }
285
286
execute_modify_view(Session & session,const Mysqlx::Crud::ModifyView & msg)287 ngs::Error_code Crud_command_handler::execute_modify_view(
288 Session &session, const Mysqlx::Crud::ModifyView &msg)
289 {
290 Expression_generator gen(m_qb, Expression_generator::Args(),
291 msg.collection().schema(), true);
292 return execute(session, View_statement_builder(gen), msg,
293 &Common_status_variables::m_crud_modify_view,
294 &ngs::Protocol_encoder::send_ok);
295 }
296
297
execute_drop_view(Session & session,const Mysqlx::Crud::DropView & msg)298 ngs::Error_code Crud_command_handler::execute_drop_view(
299 Session &session, const Mysqlx::Crud::DropView &msg)
300 {
301 Expression_generator gen(m_qb, Expression_generator::Args(),
302 msg.collection().schema(), true);
303 return execute(session, View_statement_builder(gen), msg,
304 &Common_status_variables::m_crud_drop_view,
305 &ngs::Protocol_encoder::send_ok);
306 }
307
308 } // namespace xpl
309