- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathinsert_statement_builder.cc
205 lines (182 loc) · 7.13 KB
/
insert_statement_builder.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
/*
* 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"plugin/x/src/insert_statement_builder.h"
#include"plugin/x/src/json_utils.h"
#include"plugin/x/src/ngs/protocol/protocol_protobuf.h"
#include"plugin/x/src/xpl_error.h"
#include"plugin/x/src/xpl_log.h"
namespacexpl {
voidInsert_statement_builder::build(const Insert &msg) const {
m_builder.put("INSERT INTO ");
constbool is_relational = is_table_data_model(msg);
add_collection(msg.collection());
add_projection(msg.projection(), is_relational);
if (is_relational)
add_values(msg.row(), msg.projection().size());
else
add_documents(msg.row());
if (msg.upsert()) add_upsert(is_relational);
}
voidInsert_statement_builder::add_projection(const Projection_list &projection,
constbool is_relational) const {
if (is_relational) {
if (projection.size() != 0)
m_builder.put(" (")
.put_list(projection, std::bind(&Generator::put_identifier, m_builder,
std::bind(&Mysqlx::Crud::Column::name,
std::placeholders::_1)))
.put(")");
} else {
if (projection.size() != 0)
throwngs::Error_code(ER_X_BAD_PROJECTION,
"Invalid projection for document operation");
m_builder.put(" (doc)");
}
}
voidInsert_statement_builder::add_values(const Row_list &values,
constint projection_size) const {
if (values.size() == 0)
throwngs::Error_code(ER_X_MISSING_ARGUMENT, "Missing row data for Insert");
m_builder.put(" VALUES ")
.put_list(values, [&](const Row_list::value_type &row) {
this->add_row(row.field(), projection_size);
});
}
voidInsert_statement_builder::add_row(const Field_list &row,
constint projection_size) const {
if ((row.size() == 0) || (projection_size && row.size() != projection_size))
throwngs::Error_code(ER_X_BAD_INSERT_DATA,
"Wrong number of fields in row being inserted");
m_builder.put("(").put_list(row, &Generator::put_expr).put(")");
}
voidInsert_statement_builder::add_documents(const Row_list &values) const {
if (values.size() == 0)
throwngs::Error_code(ER_X_MISSING_ARGUMENT, "Missing row data for Insert");
m_builder.put(" VALUES ")
.put_list(values, [this](const Row_list::value_type &row) {
this->add_document(row.field());
});
}
voidInsert_statement_builder::add_document(const Field_list &row) const {
if (row.size() != 1)
throwngs::Error_code(ER_X_BAD_INSERT_DATA,
"Wrong number of fields in row being inserted");
const Mysqlx::Expr::Expr &doc = row.Get(0);
if (is_prep_stmt_mode()) {
m_builder
.put(
"((SELECT JSON_INSERT(`_DERIVED_TABLE_`.`value`,'$._id',"
"CONVERT(MYSQLX_GENERATE_DOCUMENT_ID(@@AUTO_INCREMENT_OFFSET,"
"@@AUTO_INCREMENT_INCREMENT,"
"JSON_CONTAINS_PATH(`_DERIVED_TABLE_`.`value`,'one','$._id')) "
"USING utf8mb4)) FROM (SELECT ")
.put_expr(doc)
.put(" AS `value`) AS `_DERIVED_TABLE_`))");
return;
}
switch (doc.type()) {
case Mysqlx::Expr::Expr::LITERAL:
if (add_document_literal(doc.literal())) return;
break;
case Mysqlx::Expr::Expr::PLACEHOLDER:
if (add_document_placeholder(doc.position())) return;
break;
case Mysqlx::Expr::Expr::OBJECT:
add_document_object(doc.object());
return;
default: {
}
}
m_builder.put("(").put_expr(doc).put(")");
}
voidInsert_statement_builder::add_upsert(constbool is_relational) const {
if (is_relational)
throwngs::Error_code(
ER_X_BAD_INSERT_DATA,
"Unable update on duplicate key for TABLE data model");
m_builder.put(
" AS _UPSERT_NEW_VALUES_(_NEW_DOC_)"
" ON DUPLICATE KEY UPDATE"
" doc = IF(JSON_UNQUOTE(JSON_EXTRACT(doc, '$._id'))"
" = JSON_UNQUOTE(JSON_EXTRACT(_UPSERT_NEW_VALUES_._NEW_DOC_, '$._id')),"
" _UPSERT_NEW_VALUES_._NEW_DOC_, MYSQLX_ERROR("STRINGIFY_ARG(
ER_X_BAD_UPSERT_DATA) "))");
}
boolInsert_statement_builder::add_document_literal(
const Mysqlx::Datatypes::Scalar &arg) const {
switch (arg.type()) {
case Mysqlx::Datatypes::Scalar::V_OCTETS:
if (arg.v_octets().content_type() != Expression_generator::CT_PLAIN &&
arg.v_octets().content_type() != Expression_generator::CT_JSON)
returnfalse;
if (is_id_in_json(arg.v_octets().value()))
m_builder.put("(").put_quote(arg.v_octets().value()).put(")");
else
m_builder.put("(JSON_SET(")
.put_quote(arg.v_octets().value())
.put(", '$._id', ")
.put_quote(m_document_id_aggregator->generate_id())
.put("))");
returntrue;
case Mysqlx::Datatypes::Scalar::V_STRING:
if (is_id_in_json(arg.v_string().value()))
m_builder.put("(").put_expr(arg).put(")");
else
m_builder.put("(JSON_SET(")
.put_quote(arg.v_string().value())
.put(", '$._id', ")
.put_quote(m_document_id_aggregator->generate_id())
.put("))");
returntrue;
default: {
}
}
returnfalse;
}
boolInsert_statement_builder::add_document_placeholder(
const Placeholder &arg) const {
if (arg < static_cast<Placeholder>(m_builder.args().size()))
returnadd_document_literal(m_builder.args().Get(arg));
returnfalse;
}
namespace {
boolis_id_in_object(const Mysqlx::Expr::Object &arg) {
for (constauto &field : arg.fld())
if (field.key() == "_id") returntrue;
returnfalse;
}
} // namespace
voidInsert_statement_builder::add_document_object(
const Mysqlx::Expr::Object &arg) const {
if (is_id_in_object(arg))
m_builder.put("(").put_expr(arg).put(")");
else
m_builder.put("(JSON_SET(")
.put_expr(arg)
.put(", '$._id', ")
.put_quote(m_document_id_aggregator->generate_id())
.put("))");
}
} // namespace xpl