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