| 1 | /* |
| 2 | * Copyright (c) 2015, 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 "mysqld_error.h" // NOLINT(build/include_subdir) |
| 29 | #include "plugin/x/src/update_statement_builder.h" |
| 30 | #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h" |
| 31 | |
| 32 | namespace xpl { |
| 33 | namespace test { |
| 34 | |
| 35 | class Update_statement_builder_stub : public Update_statement_builder { |
| 36 | public: |
| 37 | explicit Update_statement_builder_stub(Expression_generator *gen) |
| 38 | : Update_statement_builder(*gen) {} |
| 39 | using Update_statement_builder::add_document_operation; |
| 40 | using Update_statement_builder::add_document_operation_item; |
| 41 | using Update_statement_builder::add_operation; |
| 42 | using Update_statement_builder::add_table_operation; |
| 43 | using Update_statement_builder::Operation_list; |
| 44 | }; |
| 45 | |
| 46 | class Update_statement_builder_test : public ::testing::Test { |
| 47 | public: |
| 48 | Update_statement_builder_stub &builder() { |
| 49 | expr_gen.reset(new Expression_generator(&query, args, schema, |
| 50 | is_table_data_model(msg))); |
| 51 | stub.reset(new Update_statement_builder_stub(expr_gen.get())); |
| 52 | return *stub; |
| 53 | } |
| 54 | |
| 55 | Update_statement_builder::Update msg; |
| 56 | Expression_generator::Arg_list &args = *msg.mutable_args(); |
| 57 | Query_string_builder query; |
| 58 | std::string schema; |
| 59 | std::unique_ptr<Expression_generator> expr_gen; |
| 60 | std::unique_ptr<Update_statement_builder_stub> stub; |
| 61 | |
| 62 | Update_operation::Update_type operation_id = |
| 63 | Update_operation::Base::ITEM_REMOVE; |
| 64 | |
| 65 | enum { DM_DOCUMENT = 0, DM_TABLE = 1 }; |
| 66 | |
| 67 | void fill_table_msg(const Limit &limit) { |
| 68 | msg = Update({"xtable", "xschema"}, Mysqlx::Crud::TABLE) |
| 69 | .operation({Update_operation::Base::SET, |
| 70 | Column_identifier{"yfield"}, Scalar{"booom"}}) |
| 71 | .criteria(Operator(">", Column_identifier{"xfield"}, Scalar{1.0})) |
| 72 | .order({Column_identifier("xfield"), Order::Base::DESC}) |
| 73 | .limit(limit); |
| 74 | } |
| 75 | }; // namespace test |
| 76 | |
| 77 | TEST_F(Update_statement_builder_test, add_operation_empty_list) { |
| 78 | EXPECT_THROW(builder().add_operation(Operation_list(), DM_TABLE), |
| 79 | ngs::Error_code); |
| 80 | } |
| 81 | |
| 82 | TEST_F(Update_statement_builder_test, add_table_operation_one_item) { |
| 83 | EXPECT_NO_THROW(builder().add_table_operation( |
| 84 | Operation_list{{Update_operation::Base::SET, Column_identifier("xfield"), |
| 85 | Scalar(1.0)}})); |
| 86 | EXPECT_STREQ("`xfield`=1", query.get().c_str()); |
| 87 | } |
| 88 | |
| 89 | TEST_F(Update_statement_builder_test, add_table_operation_two_items) { |
| 90 | EXPECT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 91 | {Update_operation::Base::SET, Column_identifier("xfield"), Scalar(1.0)}, |
| 92 | {Update_operation::Base::SET, Column_identifier("yfield"), |
| 93 | Scalar("two")}})); |
| 94 | EXPECT_STREQ("`xfield`=1,`yfield`='two'", query.get().c_str()); |
| 95 | } |
| 96 | |
| 97 | TEST_F(Update_statement_builder_test, |
| 98 | add_table_operation_two_items_same_source) { |
| 99 | EXPECT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 100 | {Update_operation::Base::SET, Column_identifier("xfield"), Scalar(1.0)}, |
| 101 | {Update_operation::Base::SET, Column_identifier("xfield"), |
| 102 | Scalar("two")}})); |
| 103 | EXPECT_STREQ("`xfield`=1,`xfield`='two'", query.get().c_str()); |
| 104 | } |
| 105 | |
| 106 | TEST_F(Update_statement_builder_test, |
| 107 | add_table_operation_two_items_placeholder) { |
| 108 | args = Expression_list{2.2}; |
| 109 | |
| 110 | EXPECT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 111 | {Update_operation::Base::SET, Column_identifier("xfield"), Scalar(1.0)}, |
| 112 | {Update_operation::Base::SET, Column_identifier("yfield"), |
| 113 | Placeholder(0)}})); |
| 114 | EXPECT_STREQ("`xfield`=1,`yfield`=2.2", query.get().c_str()); |
| 115 | } |
| 116 | |
| 117 | TEST_F(Update_statement_builder_test, add_table_operation_empty_name) { |
| 118 | EXPECT_THROW( |
| 119 | builder().add_table_operation(Operation_list{ |
| 120 | {Update_operation::Base::SET, Column_identifier(""), Scalar(1.0)}}), |
| 121 | ngs::Error_code); |
| 122 | } |
| 123 | |
| 124 | TEST_F(Update_statement_builder_test, |
| 125 | add_table_operation_item_name_with_table) { |
| 126 | EXPECT_THROW(builder().add_table_operation(Operation_list{ |
| 127 | {Update_operation::Base::SET, |
| 128 | Column_identifier("xfield", "xtable"), Scalar(1.0)}}), |
| 129 | ngs::Error_code); |
| 130 | } |
| 131 | |
| 132 | TEST_F(Update_statement_builder_test, |
| 133 | add_table_operation_item_name_with_table_and_schema) { |
| 134 | EXPECT_THROW( |
| 135 | builder().add_table_operation(Operation_list{ |
| 136 | {Update_operation::Base::SET, |
| 137 | Column_identifier("xfield", "xtable", "xschema"), Scalar(1.0)}}), |
| 138 | ngs::Error_code); |
| 139 | } |
| 140 | |
| 141 | TEST_F(Update_statement_builder_test, add_operation_one_item_for_table) { |
| 142 | EXPECT_NO_THROW(builder().add_operation( |
| 143 | Operation_list{{Update_operation::Base::SET, Column_identifier("xfield"), |
| 144 | Scalar(1.0)}}, |
| 145 | DM_TABLE)); |
| 146 | EXPECT_STREQ(" SET `xfield`=1", query.get().c_str()); |
| 147 | } |
| 148 | |
| 149 | TEST_F(Update_statement_builder_test, build_update_for_table) { |
| 150 | fill_table_msg(Limit(2)); |
| 151 | EXPECT_NO_THROW(builder().build(msg)); |
| 152 | EXPECT_STREQ( |
| 153 | "UPDATE `xschema`.`xtable`" |
| 154 | " SET `yfield`='booom'" |
| 155 | " WHERE (`xfield` > 1)" |
| 156 | " ORDER BY `xfield` DESC" |
| 157 | " LIMIT 2", |
| 158 | query.get().c_str()); |
| 159 | } |
| 160 | |
| 161 | TEST_F(Update_statement_builder_test, |
| 162 | build_update_for_table_forrbiden_offset_in_limit) { |
| 163 | fill_table_msg(Limit(2, 5)); |
| 164 | EXPECT_THROW(builder().build(msg), ngs::Error_code); |
| 165 | } |
| 166 | |
| 167 | TEST_F(Update_statement_builder_test, add_document_operation_not_allowed_set) { |
| 168 | EXPECT_THROW( |
| 169 | builder().add_document_operation(Operation_list{ |
| 170 | {Update_operation::Base::SET, Document_path{"first"}, Scalar(1.0)}}), |
| 171 | ngs::Error_code); |
| 172 | } |
| 173 | |
| 174 | TEST_F(Update_statement_builder_test, add_document_operation_remove) { |
| 175 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 176 | {Update_operation::Base::ITEM_REMOVE, Document_path{"first"}}})); |
| 177 | EXPECT_STREQ( |
| 178 | "doc=JSON_SET(JSON_REMOVE(doc,'$.first'),'$._id'," |
| 179 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 180 | query.get().c_str()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(Update_statement_builder_test, add_document_operation_remove_value) { |
| 184 | EXPECT_THROW( |
| 185 | builder().add_document_operation(Operation_list{ |
| 186 | {Update_operation::Base::ITEM_REMOVE, Document_path{"first"}, 666}}), |
| 187 | ngs::Error_code); |
| 188 | } |
| 189 | |
| 190 | TEST_F(Update_statement_builder_test, add_document_operation_set) { |
| 191 | EXPECT_NO_THROW(builder().add_document_operation( |
| 192 | Operation_list{{Update_operation::Base::ITEM_SET, Document_path{"first"}, |
| 193 | Scalar(1.0)}})); |
| 194 | EXPECT_STREQ( |
| 195 | "doc=JSON_SET(JSON_SET(doc,'$.first',1),'$._id'," |
| 196 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 197 | query.get().c_str()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(Update_statement_builder_test, add_document_operation_replace) { |
| 201 | EXPECT_NO_THROW(builder().add_document_operation( |
| 202 | Operation_list{{Update_operation::Base::ITEM_REPLACE, |
| 203 | Document_path{"first"}, Scalar(1.0)}})); |
| 204 | EXPECT_STREQ( |
| 205 | "doc=JSON_SET(JSON_REPLACE(doc,'$.first',1),'$._id'," |
| 206 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 207 | query.get().c_str()); |
| 208 | } |
| 209 | |
| 210 | TEST_F(Update_statement_builder_test, add_document_operation_array_insert) { |
| 211 | EXPECT_NO_THROW(builder().add_document_operation( |
| 212 | Operation_list{{Update_operation::Base::ARRAY_INSERT, |
| 213 | Document_path{"first", 0}, Scalar(1.0)}})); |
| 214 | EXPECT_STREQ( |
| 215 | "doc=JSON_SET(JSON_ARRAY_INSERT(doc,'$.first[0]',1),'$._id'," |
| 216 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 217 | query.get().c_str()); |
| 218 | } |
| 219 | |
| 220 | TEST_F(Update_statement_builder_test, add_document_operation_array_append) { |
| 221 | EXPECT_NO_THROW(builder().add_document_operation( |
| 222 | Operation_list{{Update_operation::Base::ARRAY_APPEND, |
| 223 | Document_path{"first"}, Scalar(1.0)}})); |
| 224 | EXPECT_STREQ( |
| 225 | "doc=JSON_SET(JSON_ARRAY_APPEND(doc,'$.first',1),'$._id'," |
| 226 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 227 | query.get().c_str()); |
| 228 | } |
| 229 | |
| 230 | TEST_F(Update_statement_builder_test, |
| 231 | add_document_operation_array_append_twice) { |
| 232 | EXPECT_NO_THROW(builder().add_document_operation( |
| 233 | Operation_list{{Update_operation::Base::ARRAY_APPEND, |
| 234 | Document_path{"first"}, Scalar(1.0)}, |
| 235 | {Update_operation::Base::ARRAY_APPEND, |
| 236 | Document_path{"first"}, Scalar("two")}})); |
| 237 | EXPECT_STREQ( |
| 238 | "doc=JSON_SET(JSON_ARRAY_APPEND(doc,'$.first',1,'$.first','two'),'$._id'," |
| 239 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 240 | query.get().c_str()); |
| 241 | } |
| 242 | |
| 243 | TEST_F(Update_statement_builder_test, add_document_operation_remove_twice) { |
| 244 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 245 | {Update_operation::Base::ITEM_REMOVE, Document_path{"first"}}, |
| 246 | {Update_operation::Base::ITEM_REMOVE, Document_path{"second"}}})); |
| 247 | EXPECT_STREQ( |
| 248 | "doc=JSON_SET(JSON_REMOVE(doc,'$.first','$.second'),'$._id'," |
| 249 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 250 | query.get().c_str()); |
| 251 | } |
| 252 | |
| 253 | TEST_F(Update_statement_builder_test, add_document_operation_set_twice) { |
| 254 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 255 | {Update_operation::Base::ITEM_SET, Document_path{"first"}, Scalar(1.0)}, |
| 256 | {Update_operation::Base::ITEM_SET, Document_path{"second"}, |
| 257 | Scalar("two")}})); |
| 258 | EXPECT_STREQ( |
| 259 | "doc=JSON_SET(JSON_SET(doc,'$.first',1,'$.second','two'),'$._id'," |
| 260 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 261 | query.get().c_str()); |
| 262 | } |
| 263 | |
| 264 | TEST_F(Update_statement_builder_test, |
| 265 | add_document_operation_set_twice_placeholder) { |
| 266 | args = Expression_list{2.2}; |
| 267 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 268 | {Update_operation::Base::ITEM_SET, Document_path{"first"}, Scalar(1.0)}, |
| 269 | {Update_operation::Base::ITEM_SET, Document_path{"second"}, |
| 270 | Placeholder(0)}})); |
| 271 | EXPECT_STREQ( |
| 272 | "doc=JSON_SET(JSON_SET(doc,'$.first',1,'$.second',2.2),'$._id'," |
| 273 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 274 | query.get().c_str()); |
| 275 | } |
| 276 | |
| 277 | TEST_F(Update_statement_builder_test, |
| 278 | add_document_operation_merge_no_empty_path) { |
| 279 | EXPECT_THROW(builder().add_document_operation(Operation_list{ |
| 280 | {Update_operation::Base::ITEM_MERGE, Document_path{"first"}, |
| 281 | Scalar("{\"two\": 2.0}")}}), |
| 282 | ngs::Error_code); |
| 283 | } |
| 284 | |
| 285 | TEST_F(Update_statement_builder_test, add_document_operation_merge_to_array) { |
| 286 | EXPECT_THROW(builder().add_document_operation(Operation_list{ |
| 287 | {Update_operation::Base::ITEM_MERGE, |
| 288 | Document_path{"first", 3}, Scalar("{\"two\": 2.0}")}}), |
| 289 | ngs::Error_code); |
| 290 | } |
| 291 | |
| 292 | TEST_F(Update_statement_builder_test, add_document_operation_remove_set) { |
| 293 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 294 | {Update_operation::Base::ITEM_REMOVE, Document_path{"first"}}, |
| 295 | {Update_operation::Base::ITEM_SET, Document_path{"second"}, |
| 296 | Scalar("two")}})); |
| 297 | EXPECT_STREQ( |
| 298 | "doc=JSON_SET(JSON_SET(JSON_REMOVE(doc,'$.first'),'$.second','two')," |
| 299 | "'$._id',JSON_EXTRACT(`doc`,'$._id'))", |
| 300 | query.get().c_str()); |
| 301 | } |
| 302 | |
| 303 | TEST_F(Update_statement_builder_test, add_document_operation_remove_twice_set) { |
| 304 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 305 | {Update_operation::Base::ITEM_REMOVE, Document_path{"first"}}, |
| 306 | {Update_operation::Base::ITEM_REMOVE, Document_path{"second"}}, |
| 307 | {Update_operation::Base::ITEM_SET, Document_path{"third"}, Scalar(-3)}})); |
| 308 | EXPECT_STREQ( |
| 309 | "doc=JSON_SET(JSON_SET(JSON_REMOVE(doc,'$.first','$.second')," |
| 310 | "'$.third',-3),'$._id',JSON_EXTRACT(`doc`,'$._id'))", |
| 311 | query.get().c_str()); |
| 312 | } |
| 313 | |
| 314 | TEST_F(Update_statement_builder_test, add_document_operation_set_remove_set) { |
| 315 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 316 | {Update_operation::Base::ITEM_SET, Document_path{"first"}, Scalar(1.0)}, |
| 317 | {Update_operation::Base::ITEM_REMOVE, Document_path{"second"}}, |
| 318 | {Update_operation::Base::ITEM_SET, Document_path{"third"}, Scalar(-3)}})); |
| 319 | EXPECT_STREQ( |
| 320 | "doc=JSON_SET(JSON_SET(JSON_REMOVE(JSON_SET(doc,'$.first',1),'$.second')," |
| 321 | "'$.third',-3),'$._id',JSON_EXTRACT(`doc`,'$._id'))", |
| 322 | query.get().c_str()); |
| 323 | } |
| 324 | |
| 325 | TEST_F(Update_statement_builder_test, |
| 326 | add_document_operation_item_forbiden_column) { |
| 327 | ASSERT_THROW(builder().add_document_operation_item( |
| 328 | Update_operation(Update_operation::Base::ITEM_SET, |
| 329 | Column_identifier("xcolumn"), Scalar(-3)), |
| 330 | &operation_id), |
| 331 | ngs::Error_code); |
| 332 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 333 | } |
| 334 | |
| 335 | TEST_F(Update_statement_builder_test, |
| 336 | add_document_operation_item_forbiden_schema) { |
| 337 | ASSERT_THROW( |
| 338 | builder().add_document_operation_item( |
| 339 | Update_operation(Update_operation::Base::ITEM_SET, |
| 340 | Column_identifier("", "", "xschema"), Scalar(-3)), |
| 341 | &operation_id), |
| 342 | ngs::Error_code); |
| 343 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 344 | } |
| 345 | |
| 346 | TEST_F(Update_statement_builder_test, |
| 347 | add_document_operation_item_forbiden_table) { |
| 348 | ASSERT_THROW( |
| 349 | builder().add_document_operation_item( |
| 350 | Update_operation(Update_operation::Base::ITEM_SET, |
| 351 | Column_identifier("", "xtable"), Scalar(-3)), |
| 352 | &operation_id), |
| 353 | ngs::Error_code); |
| 354 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 355 | } |
| 356 | |
| 357 | TEST_F(Update_statement_builder_test, |
| 358 | add_document_operation_item_forbiden_id_change) { |
| 359 | ASSERT_THROW(builder().add_document_operation_item( |
| 360 | Update_operation(Update_operation::Base::ITEM_SET, |
| 361 | Document_path{"_id"}, Scalar(-3)), |
| 362 | &operation_id), |
| 363 | ngs::Error_code); |
| 364 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 365 | } |
| 366 | |
| 367 | TEST_F(Update_statement_builder_test, |
| 368 | add_document_operation_item_empty_document_path) { |
| 369 | operation_id = Update_operation::Base::ITEM_SET; |
| 370 | ASSERT_NO_THROW(builder().add_document_operation_item( |
| 371 | Update_operation(Update_operation::Base::ITEM_SET, Column_identifier(), |
| 372 | Scalar(-3)), |
| 373 | &operation_id)); |
| 374 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 375 | EXPECT_STREQ(",'$',-3", query.get().c_str()); |
| 376 | } |
| 377 | |
| 378 | TEST_F(Update_statement_builder_test, add_document_operation_item_root_path) { |
| 379 | ASSERT_NO_THROW(builder().add_document_operation_item( |
| 380 | Update_operation(Update_operation::Base::ITEM_SET, Document_path(), |
| 381 | Scalar(-3)), |
| 382 | &operation_id)); |
| 383 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 384 | } |
| 385 | |
| 386 | TEST_F(Update_statement_builder_test, |
| 387 | add_document_operation_item_empty_member) { |
| 388 | ASSERT_THROW(builder().add_document_operation_item( |
| 389 | Update_operation(Update_operation::Base::ITEM_SET, |
| 390 | Document_path{"first", ""}, Scalar(-3)), |
| 391 | &operation_id), |
| 392 | xpl::Expression_generator::Error); |
| 393 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 394 | } |
| 395 | |
| 396 | TEST_F(Update_statement_builder_test, |
| 397 | add_document_operation_item_empty_member_reverse) { |
| 398 | ASSERT_THROW(builder().add_document_operation_item( |
| 399 | Update_operation(Update_operation::Base::ITEM_SET, |
| 400 | Document_path{"", "first"}, Scalar(-3)), |
| 401 | &operation_id), |
| 402 | xpl::Expression_generator::Error); |
| 403 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 404 | } |
| 405 | |
| 406 | TEST_F(Update_statement_builder_test, |
| 407 | add_document_operation_item_root_as_array) { |
| 408 | ASSERT_THROW(builder().add_document_operation_item( |
| 409 | Update_operation(Update_operation::Base::ITEM_SET, |
| 410 | Document_path{0}, Scalar(-3)), |
| 411 | &operation_id), |
| 412 | ngs::Error_code); |
| 413 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 414 | } |
| 415 | |
| 416 | TEST_F(Update_statement_builder_test, |
| 417 | add_document_operation_item_root_as_array_asterisk) { |
| 418 | ASSERT_THROW( |
| 419 | builder().add_document_operation_item( |
| 420 | Update_operation( |
| 421 | Update_operation::Base::ITEM_SET, |
| 422 | Document_path{Document_path_item::Base::ARRAY_INDEX_ASTERISK}, |
| 423 | Scalar(-3)), |
| 424 | &operation_id), |
| 425 | ngs::Error_code); |
| 426 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 427 | } |
| 428 | |
| 429 | TEST_F(Update_statement_builder_test, |
| 430 | add_document_operation_item_root_double_asterisk) { |
| 431 | ASSERT_THROW(builder().add_document_operation_item( |
| 432 | Update_operation( |
| 433 | Update_operation::Base::ITEM_SET, |
| 434 | Document_path{Document_path_item::Base::DOUBLE_ASTERISK}, |
| 435 | Scalar(-3)), |
| 436 | &operation_id), |
| 437 | ngs::Error_code); |
| 438 | ASSERT_EQ(Update_operation::Base::ITEM_SET, operation_id); |
| 439 | } |
| 440 | |
| 441 | TEST_F(Update_statement_builder_test, add_operation_one_item_for_document) { |
| 442 | ASSERT_NO_THROW(builder().add_operation( |
| 443 | Operation_list{{Update_operation::Base::ITEM_SET, Document_path{"first"}, |
| 444 | Scalar(1.0)}}, |
| 445 | DM_DOCUMENT)); |
| 446 | EXPECT_STREQ( |
| 447 | " SET doc=JSON_SET(JSON_SET(doc,'$.first',1),'$._id'," |
| 448 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 449 | query.get().c_str()); |
| 450 | } |
| 451 | |
| 452 | TEST_F(Update_statement_builder_test, build_update_for_document) { |
| 453 | msg = |
| 454 | Update({"xtable", "xschema"}, Mysqlx::Crud::DOCUMENT) |
| 455 | .operation({Update_operation::Base::ITEM_SET, Document_path{"first"}, |
| 456 | Scalar(1.0)}) |
| 457 | .criteria(Operator(">", Column_identifier{Document_path{"second"}}, |
| 458 | Scalar{1.0})) |
| 459 | .order({Column_identifier{Document_path{"third"}}, Order::Base::DESC}) |
| 460 | .limit({2}); |
| 461 | EXPECT_NO_THROW(builder().build(msg)); |
| 462 | EXPECT_STREQ( |
| 463 | "UPDATE `xschema`.`xtable`" |
| 464 | " SET doc=JSON_SET(JSON_SET(doc,'$.first',1)," |
| 465 | "'$._id',JSON_EXTRACT(`doc`,'$._id'))" |
| 466 | " WHERE (JSON_EXTRACT(doc,'$.second') > 1)" |
| 467 | " ORDER BY JSON_EXTRACT(doc,'$.third')" |
| 468 | " DESC LIMIT 2", |
| 469 | query.get().c_str()); |
| 470 | } |
| 471 | |
| 472 | TEST_F(Update_statement_builder_test, add_document_operation_set_whole_doc) { |
| 473 | ASSERT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 474 | {Update_operation::Base::ITEM_SET, Document_path{""}, Scalar("two")}})); |
| 475 | EXPECT_STREQ( |
| 476 | "doc=JSON_SET(JSON_SET(doc,'$','two'),'$._id'," |
| 477 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 478 | query.get().c_str()); |
| 479 | } |
| 480 | |
| 481 | TEST_F(Update_statement_builder_test, |
| 482 | add_table_operation_set_needless_doc_path) { |
| 483 | ASSERT_THROW( |
| 484 | builder().add_table_operation(Operation_list{ |
| 485 | {Update_operation::Base::SET, |
| 486 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}}), |
| 487 | ngs::Error_code); |
| 488 | } |
| 489 | |
| 490 | TEST_F(Update_statement_builder_test, |
| 491 | add_table_operation_item_set_missing_doc_path) { |
| 492 | ASSERT_THROW(builder().add_table_operation( |
| 493 | Operation_list{{Update_operation::Base::ITEM_SET, |
| 494 | Column_identifier("xfield"), Scalar(1.0)}}), |
| 495 | ngs::Error_code); |
| 496 | } |
| 497 | |
| 498 | TEST_F(Update_statement_builder_test, add_table_operation_item_set) { |
| 499 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 500 | {Update_operation::Base::ITEM_SET, |
| 501 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}})); |
| 502 | EXPECT_STREQ("`xfield`=JSON_SET(`xfield`,'$.first',1)", query.get().c_str()); |
| 503 | } |
| 504 | |
| 505 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_twice) { |
| 506 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 507 | {Update_operation::Base::ITEM_SET, |
| 508 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 509 | {Update_operation::Base::ITEM_SET, |
| 510 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}})); |
| 511 | EXPECT_STREQ("`xfield`=JSON_SET(`xfield`,'$.first',1,'$.second','two')", |
| 512 | query.get().c_str()); |
| 513 | } |
| 514 | |
| 515 | TEST_F(Update_statement_builder_test, |
| 516 | add_table_operation_item_set_twice_but_different) { |
| 517 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 518 | {Update_operation::Base::ITEM_SET, |
| 519 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 520 | {Update_operation::Base::ITEM_SET, |
| 521 | Column_identifier(Document_path{"second"}, "yfield"), Scalar("two")}})); |
| 522 | EXPECT_STREQ( |
| 523 | "`xfield`=JSON_SET(`xfield`,'$.first',1)," |
| 524 | "`yfield`=JSON_SET(`yfield`,'$.second','two')", |
| 525 | query.get().c_str()); |
| 526 | } |
| 527 | |
| 528 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_triple) { |
| 529 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 530 | {Update_operation::Base::ITEM_SET, |
| 531 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 532 | {Update_operation::Base::ITEM_SET, |
| 533 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}, |
| 534 | {Update_operation::Base::ITEM_SET, |
| 535 | Column_identifier(Document_path{"third"}, "xfield"), Scalar(-3)}})); |
| 536 | EXPECT_STREQ( |
| 537 | "`xfield`=JSON_SET(`xfield`,'$.first',1,'$.second','two','$.third',-3)", |
| 538 | query.get().c_str()); |
| 539 | } |
| 540 | |
| 541 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_mix_first) { |
| 542 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 543 | {Update_operation::Base::SET, Column_identifier("xfield"), Scalar(1.0)}, |
| 544 | {Update_operation::Base::ITEM_SET, |
| 545 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}, |
| 546 | {Update_operation::Base::ITEM_SET, |
| 547 | Column_identifier(Document_path{"third"}, "xfield"), Scalar(-3)}})); |
| 548 | EXPECT_STREQ( |
| 549 | "`xfield`=1," |
| 550 | "`xfield`=JSON_SET(`xfield`,'$.second','two','$.third',-3)", |
| 551 | query.get().c_str()); |
| 552 | } |
| 553 | |
| 554 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_mix_last) { |
| 555 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 556 | {Update_operation::Base::ITEM_SET, |
| 557 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}, |
| 558 | {Update_operation::Base::ITEM_SET, |
| 559 | Column_identifier(Document_path{"third"}, "xfield"), Scalar(-3)}, |
| 560 | {Update_operation::Base::SET, Column_identifier("xfield"), |
| 561 | Scalar(1.0)}})); |
| 562 | EXPECT_STREQ( |
| 563 | "`xfield`=JSON_SET(`xfield`,'$.second','two','$.third',-3)," |
| 564 | "`xfield`=1", |
| 565 | query.get().c_str()); |
| 566 | } |
| 567 | |
| 568 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_mix_middle) { |
| 569 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 570 | {Update_operation::Base::ITEM_SET, |
| 571 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}, |
| 572 | {Update_operation::Base::SET, Column_identifier("xfield"), Scalar(1.0)}, |
| 573 | {Update_operation::Base::ITEM_SET, |
| 574 | Column_identifier(Document_path{"third"}, "xfield"), Scalar(-3)}})); |
| 575 | EXPECT_STREQ( |
| 576 | "`xfield`=JSON_SET(`xfield`,'$.second','two')," |
| 577 | "`xfield`=1," |
| 578 | "`xfield`=JSON_SET(`xfield`,'$.third',-3)", |
| 579 | query.get().c_str()); |
| 580 | } |
| 581 | |
| 582 | TEST_F(Update_statement_builder_test, add_table_operation_item_set_fourth) { |
| 583 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 584 | {Update_operation::Base::ITEM_SET, |
| 585 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 586 | {Update_operation::Base::ITEM_SET, |
| 587 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}, |
| 588 | {Update_operation::Base::ITEM_SET, |
| 589 | Column_identifier(Document_path{"first"}, "yfield"), Scalar(1.0)}, |
| 590 | {Update_operation::Base::ITEM_SET, |
| 591 | Column_identifier(Document_path{"second"}, "yfield"), Scalar("two")}})); |
| 592 | EXPECT_STREQ( |
| 593 | "`xfield`=JSON_SET(`xfield`,'$.first',1,'$.second','two')," |
| 594 | "`yfield`=JSON_SET(`yfield`,'$.first',1,'$.second','two')", |
| 595 | query.get().c_str()); |
| 596 | } |
| 597 | |
| 598 | TEST_F(Update_statement_builder_test, add_table_operation_item_remove_one) { |
| 599 | ASSERT_NO_THROW(builder().add_table_operation( |
| 600 | Operation_list{{Update_operation::Base::ITEM_REMOVE, |
| 601 | Column_identifier(Document_path{"first"}, "xfield")}})); |
| 602 | EXPECT_STREQ("`xfield`=JSON_REMOVE(`xfield`,'$.first')", query.get().c_str()); |
| 603 | } |
| 604 | |
| 605 | TEST_F(Update_statement_builder_test, add_table_operation_item_remove_twice) { |
| 606 | ASSERT_NO_THROW(builder().add_table_operation( |
| 607 | Operation_list{{Update_operation::Base::ITEM_REMOVE, |
| 608 | Column_identifier(Document_path{"first"}, "xfield")}, |
| 609 | {Update_operation::Base::ITEM_REMOVE, |
| 610 | Column_identifier(Document_path{"second"}, "xfield")}})); |
| 611 | EXPECT_STREQ("`xfield`=JSON_REMOVE(`xfield`,'$.first','$.second')", |
| 612 | query.get().c_str()); |
| 613 | } |
| 614 | |
| 615 | TEST_F(Update_statement_builder_test, add_table_operation_item_replace_one) { |
| 616 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 617 | {Update_operation::Base::ITEM_REPLACE, |
| 618 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}})); |
| 619 | EXPECT_STREQ("`xfield`=JSON_REPLACE(`xfield`,'$.first',1)", |
| 620 | query.get().c_str()); |
| 621 | } |
| 622 | |
| 623 | TEST_F(Update_statement_builder_test, add_table_operation_item_replace_twice) { |
| 624 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 625 | {Update_operation::Base::ITEM_REPLACE, |
| 626 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 627 | {Update_operation::Base::ITEM_REPLACE, |
| 628 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}})); |
| 629 | EXPECT_STREQ("`xfield`=JSON_REPLACE(`xfield`,'$.first',1,'$.second','two')", |
| 630 | query.get().c_str()); |
| 631 | } |
| 632 | |
| 633 | TEST_F(Update_statement_builder_test, add_table_operation_array_insert_one) { |
| 634 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 635 | {Update_operation::Base::ARRAY_INSERT, |
| 636 | Column_identifier(Document_path{0}, "xfield"), Scalar(1.0)}})); |
| 637 | EXPECT_STREQ("`xfield`=JSON_ARRAY_INSERT(`xfield`,'$[0]',1)", |
| 638 | query.get().c_str()); |
| 639 | } |
| 640 | |
| 641 | TEST_F(Update_statement_builder_test, add_table_operation_array_insert_twice) { |
| 642 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 643 | {Update_operation::Base::ARRAY_INSERT, |
| 644 | Column_identifier(Document_path{0}, "xfield"), Scalar(1.0)}, |
| 645 | {Update_operation::Base::ARRAY_INSERT, |
| 646 | Column_identifier(Document_path{1}, "xfield"), Scalar("two")}})); |
| 647 | EXPECT_STREQ("`xfield`=JSON_ARRAY_INSERT(`xfield`,'$[0]',1,'$[1]','two')", |
| 648 | query.get().c_str()); |
| 649 | } |
| 650 | |
| 651 | TEST_F(Update_statement_builder_test, add_table_operation_array_append_one) { |
| 652 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 653 | {Update_operation::Base::ARRAY_APPEND, |
| 654 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}})); |
| 655 | EXPECT_STREQ("`xfield`=JSON_ARRAY_APPEND(`xfield`,'$.first',1)", |
| 656 | query.get().c_str()); |
| 657 | } |
| 658 | |
| 659 | TEST_F(Update_statement_builder_test, add_table_operation_array_append_twice) { |
| 660 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 661 | {Update_operation::Base::ARRAY_APPEND, |
| 662 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 663 | {Update_operation::Base::ARRAY_APPEND, |
| 664 | Column_identifier(Document_path{"second"}, "xfield"), Scalar("two")}})); |
| 665 | EXPECT_STREQ( |
| 666 | "`xfield`=JSON_ARRAY_APPEND(`xfield`,'$.first',1,'$.second','two')", |
| 667 | query.get().c_str()); |
| 668 | } |
| 669 | |
| 670 | TEST_F(Update_statement_builder_test, |
| 671 | add_table_operation_array_append_twice_placeholder) { |
| 672 | args = Expression_list{2.2}; |
| 673 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 674 | {Update_operation::Base::ARRAY_APPEND, |
| 675 | Column_identifier(Document_path{"first"}, "xfield"), Scalar(1.0)}, |
| 676 | {Update_operation::Base::ARRAY_APPEND, |
| 677 | Column_identifier(Document_path{"second"}, "xfield"), Placeholder(0)}})); |
| 678 | EXPECT_STREQ( |
| 679 | "`xfield`=JSON_ARRAY_APPEND(`xfield`,'$.first',1,'$.second',2.2)", |
| 680 | query.get().c_str()); |
| 681 | } |
| 682 | |
| 683 | class Merge_param { |
| 684 | public: |
| 685 | Merge_param(const Update_operation::Update_type type, |
| 686 | const std::string &function) |
| 687 | : m_type(type), m_function(function) {} |
| 688 | |
| 689 | const Update_operation::Update_type m_type; |
| 690 | const std::string m_function; |
| 691 | }; |
| 692 | |
| 693 | class Update_statement_builder_op_merge_test |
| 694 | : public Update_statement_builder_test, |
| 695 | public ::testing::WithParamInterface<Merge_param> {}; |
| 696 | |
| 697 | TEST_P(Update_statement_builder_op_merge_test, |
| 698 | add_document_operation_merge_twice) { |
| 699 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 700 | {GetParam().m_type, Document_path{""}, Scalar("{\"two\": 2.0}")}, |
| 701 | {GetParam().m_type, Document_path{""}, Scalar("{\"three\": 3.0}")}})); |
| 702 | EXPECT_EQ("doc=JSON_SET(" + GetParam().m_function + |
| 703 | "(doc,'{\\\"two\\\": 2.0}'," |
| 704 | "'{\\\"three\\\": 3.0}'),'$._id'," |
| 705 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 706 | query.get().c_str()); |
| 707 | } |
| 708 | |
| 709 | TEST_P(Update_statement_builder_op_merge_test, |
| 710 | add_document_operation_set_merge) { |
| 711 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 712 | {Update_operation::Base::ITEM_SET, Document_path{"first"}, Scalar(1.0)}, |
| 713 | {GetParam().m_type, Column_identifier(), Scalar("{\"three\": 3.0}")}})); |
| 714 | EXPECT_EQ("doc=JSON_SET(" + GetParam().m_function + |
| 715 | "(JSON_SET(doc,'$.first',1)," |
| 716 | "'{\\\"three\\\": 3.0}'),'$._id',JSON_EXTRACT(`doc`,'$._id'))", |
| 717 | query.get().c_str()); |
| 718 | } |
| 719 | |
| 720 | TEST_P(Update_statement_builder_op_merge_test, add_document_operation_merge) { |
| 721 | EXPECT_NO_THROW(builder().add_document_operation(Operation_list{ |
| 722 | {GetParam().m_type, Document_path{""}, Scalar("{\"two\": 2.0}")}})); |
| 723 | EXPECT_EQ("doc=JSON_SET(" + GetParam().m_function + |
| 724 | "(doc,'{\\\"two\\\": 2.0}'),'$._id'," |
| 725 | "JSON_EXTRACT(`doc`,'$._id'))", |
| 726 | query.get().c_str()); |
| 727 | } |
| 728 | |
| 729 | TEST_P(Update_statement_builder_op_merge_test, |
| 730 | add_table_operation_item_merge_twice) { |
| 731 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 732 | {GetParam().m_type, Column_identifier(Document_path{"first"}, "xfield"), |
| 733 | Scalar(1.0)}, |
| 734 | {GetParam().m_type, Column_identifier(Document_path{"second"}, "xfield"), |
| 735 | Scalar("two")}})); |
| 736 | EXPECT_EQ("`xfield`=" + GetParam().m_function + "(`xfield`,1,'two')", |
| 737 | query.get().c_str()); |
| 738 | } |
| 739 | |
| 740 | TEST_P(Update_statement_builder_op_merge_test, |
| 741 | add_table_operation_item_merge_one) { |
| 742 | ASSERT_NO_THROW(builder().add_table_operation(Operation_list{ |
| 743 | {GetParam().m_type, Column_identifier(Document_path{"first"}, "xfield"), |
| 744 | Scalar(1.0)}})); |
| 745 | EXPECT_EQ("`xfield`=" + GetParam().m_function + "(`xfield`,1)", |
| 746 | query.get().c_str()); |
| 747 | } |
| 748 | |
| 749 | INSTANTIATE_TEST_SUITE_P( |
| 750 | AllMerge_functions, Update_statement_builder_op_merge_test, |
| 751 | ::testing::Values( |
| 752 | Merge_param(Update_operation::Base::ITEM_MERGE, "JSON_MERGE_PRESERVE"), |
| 753 | Merge_param(Update_operation::Base::MERGE_PATCH, "JSON_MERGE_PATCH"))); |
| 754 | |
| 755 | } // namespace test |
| 756 | } // namespace xpl |