Debian Codegraph @

unittest/gunit/xplugin/xpl/prepared_statement_builder_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2018, 2026, 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 designed to work 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 either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27
28 #include "plugin/x/src/delete_statement_builder.h"
29 #include "plugin/x/src/find_statement_builder.h"
30 #include "plugin/x/src/prepared_statement_builder.h"
31 #include "unittest/gunit/xplugin/xpl/assert_error_code.h"
32 #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h"
33
34 namespace xpl {
35 namespace test {
36
37 using Placeholder_list = Expression_generator::Prep_stmt_placeholder_list;
38
39 template <typename M>
40 struct Param_prepared_statement_builder {
41 std::string expect;
42 Placeholder_list expect_ids;
43 M msg;
44 };
45
46 Param_prepared_statement_builder<Find> find_param[] = {
47 {"SELECT JSON_OBJECT('zeta', ?) AS doc FROM `schema`.`col`",
48 {0},
49 Find({"col", "schema"}).projection({Placeholder{0}, "zeta"})},
50 {"SELECT JSON_OBJECT('zeta', JSON_EXTRACT(doc,'$.alpha')) AS doc "
51 "FROM `schema`.`col` WHERE (JSON_EXTRACT(doc,'$.delta') > ?)",
52 {0},
53 Find({"col", "schema"})
54 .projection({Column_identifier{Document_path{"alpha"}}, "zeta"})
55 .criteria(Operator(">", Column_identifier{Document_path{"delta"}},
56 Placeholder{0}))},
57 {"SELECT JSON_OBJECT('zeta', JSON_EXTRACT(doc,'$.alpha')) AS doc "
58 "FROM `schema`.`col` ORDER BY ?",
59 {0},
60 Find({"col", "schema"})
61 .projection({Column_identifier{Document_path{"alpha"}}, "zeta"})
62 .order({Placeholder{0}})},
63 {"SELECT JSON_OBJECT('zeta', `_DERIVED_TABLE_`.`zeta`) AS doc FROM ("
64 "SELECT JSON_EXTRACT(doc,'$.alpha') AS `zeta` "
65 "FROM `schema`.`col` GROUP BY ?) AS `_DERIVED_TABLE_`",
66 {0},
67 Find({"col", "schema"})
68 .projection({Column_identifier{Document_path{"alpha"}}, "zeta"})
69 .grouping(Placeholder{0})},
70 {"SELECT JSON_OBJECT('zeta', JSON_EXTRACT(doc,'$.alpha')) AS doc "
71 "FROM `schema`.`col` HAVING (JSON_EXTRACT(doc,'$.delta') > ?)",
72 {0},
73 Find({"col", "schema"})
74 .projection({Column_identifier{Document_path{"alpha"}}, "zeta"})
75 .grouping_criteria(Operator(
76 ">", Column_identifier{Document_path{"delta"}}, Placeholder{0}))},
77 };
78
79 Param_prepared_statement_builder<Delete> delete_param[] = {
80 {"DELETE FROM `schema`.`col` WHERE (JSON_EXTRACT(doc,'$.delta') > ?)",
81 {0},
82 Delete({"col", "schema"})
83 .criteria(Operator(">", Column_identifier{Document_path{"delta"}},
84 Placeholder{0}))},
85 {"DELETE FROM `schema`.`col` ORDER BY ?",
86 {0},
87 Delete({"col", "schema"}).order({Placeholder{0}})},
88 };
89
90 Param_prepared_statement_builder<Update> update_param[] = {
91 {"UPDATE `schema`.`col` "
92 "SET doc=JSON_SET(JSON_SET(doc,'$.first',?),'$._id',"
93 "JSON_EXTRACT(`doc`,'$._id'))",
94 {0},
95 Update({"col", "schema"})
96 .operation({Update_operation::Base::ITEM_SET, Document_path{"first"},
97 Placeholder{0}})},
98 {"UPDATE `schema`.`col` SET "
99 "doc=JSON_SET(JSON_SET(doc,'$.first',1),'$._id',"
100 "JSON_EXTRACT(`doc`,'$._id')) "
101 "WHERE (JSON_EXTRACT(doc,'$.delta') > ?)",
102 {0},
103 Update({"col", "schema"})
104 .operation({Update_operation::Base::ITEM_SET, Document_path{"first"},
105 Scalar{1.0}})
106 .criteria(Operator(">", Column_identifier{Document_path{"delta"}},
107 Placeholder{0}))},
108 {"UPDATE `schema`.`col` "
109 "SET doc=JSON_SET(JSON_SET(doc,'$.first',1),'$._id',"
110 "JSON_EXTRACT(`doc`,'$._id')) ORDER BY ?",
111 {0},
112 Update({"col", "schema"})
113 .operation({Update_operation::Base::ITEM_SET, Document_path{"first"},
114 Scalar{1.0}})
115 .order({Placeholder{0}})},
116 };
117
118 #define EXPECT_VALUE(val) \
119 "(SELECT JSON_INSERT(`_DERIVED_TABLE_`.`value`,'$._id'," \
120 "CONVERT(MYSQLX_GENERATE_DOCUMENT_ID(@@AUTO_INCREMENT_OFFSET," \
121 "@@AUTO_INCREMENT_INCREMENT,JSON_CONTAINS_PATH(`_DERIVED_TABLE_`.`value`," \
122 "'one','$._id')) USING utf8mb4)) " \
123 "FROM (SELECT " val " AS `value`) AS `_DERIVED_TABLE_`)"
124
125 Param_prepared_statement_builder<Insert> insert_param[] = {
126 {"INSERT INTO `schema`.`col` (doc) VALUES (" EXPECT_VALUE(
127 "'{\\\"three\\\":3}'") ")",
128 {},
129 Insert({"col", "schema"})
130 .args({Scalar(R"({"three":3})")})
131 .row({{Placeholder{0}}})},
132 {"INSERT INTO `schema`.`col` (doc) VALUES (" EXPECT_VALUE("?") ")",
133 {0},
134 Insert({"col", "schema"}).row({{Placeholder{0}}})},
135 {"INSERT INTO `schema`.`col` (doc) VALUES (" EXPECT_VALUE(
136 "'{\\\"three\\\":3}'") "),(" EXPECT_VALUE("?") ")",
137 {0},
138 Insert({"col", "schema"})
139 .args({Scalar(R"({"three":3})")})
140 .row({{Placeholder{0}}, {Placeholder{1}}})},
141 {"INSERT INTO `schema`.`col` (doc) VALUES (" EXPECT_VALUE(
142 "?") "),(" EXPECT_VALUE("?") ")",
143 {0, 1},
144 Insert({"col", "schema"}).row({{Placeholder{0}}, {Placeholder{1}}})},
145 };
146
147 Param_prepared_statement_builder<Stmt_execute> stmt_execute_param[] = {
148 {"SELECT 1", {}, Stmt_execute("SELECT 1")},
149 {"SELECT ?", {0}, Stmt_execute("SELECT ?")},
150 {"SELECT 1", {}, Stmt_execute("SELECT ?").args({Scalar(1)})},
151 {"SELECT 1, ?", {0}, Stmt_execute("SELECT ?, ?").args({Scalar(1)})},
152 };
153
154 template <typename M>
155 class Prepared_statement_builder_test
156 : public testing::TestWithParam<Param_prepared_statement_builder<M>> {
157 public:
158 using Param = Param_prepared_statement_builder<M>;
159 Query_string_builder query;
160 Expression_generator::Prep_stmt_placeholder_list placeholders;
161 Prepared_statement_builder builder{&query, &placeholders};
162 void assert_placeholders() {
163 const Param &param = this->GetParam();
164 ASSERT_ERROR_CODE(ER_X_SUCCESS, builder.build(param.msg));
165 EXPECT_STREQ(param.expect.c_str(), query.get().c_str());
166 EXPECT_EQ(param.expect_ids, placeholders);
167 }
168 };
169
170 using Find_test = Prepared_statement_builder_test<Find>;
171 TEST_P(Find_test, placeholders) { assert_placeholders(); }
172 INSTANTIATE_TEST_SUITE_P(Prepared_statement_builder, Find_test,
173 testing::ValuesIn(find_param));
174
175 using Delete_test = Prepared_statement_builder_test<Delete>;
176 TEST_P(Delete_test, placeholders) { assert_placeholders(); }
177 INSTANTIATE_TEST_SUITE_P(Prepared_statement_builder, Delete_test,
178 testing::ValuesIn(delete_param));
179
180 using Update_test = Prepared_statement_builder_test<Update>;
181 TEST_P(Update_test, placeholders) { assert_placeholders(); }
182 INSTANTIATE_TEST_SUITE_P(Prepared_statement_builder, Update_test,
183 testing::ValuesIn(update_param));
184
185 using Insert_test = Prepared_statement_builder_test<Insert>;
186 TEST_P(Insert_test, placeholders) { assert_placeholders(); }
187 INSTANTIATE_TEST_SUITE_P(Prepared_statement_builder, Insert_test,
188 testing::ValuesIn(insert_param));
189
190 using Stmt_execute_test = Prepared_statement_builder_test<Stmt_execute>;
191 TEST_P(Stmt_execute_test, placeholders) { assert_placeholders(); }
192 INSTANTIATE_TEST_SUITE_P(Prepared_statement_builder, Stmt_execute_test,
193 testing::ValuesIn(stmt_execute_param));
194 } // namespace test
195 } // namespace xpl