| 1 | /* |
| 2 | * Copyright (c) 2017, 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 "my_compiler.h" // NOLINT(build/include_subdir) |
| 27 | |
| 28 | #include "unittest/gunit/xplugin/xcl/message_helpers.h" |
| 29 | #include "unittest/gunit/xplugin/xcl/mock/query_result.h" |
| 30 | #include "unittest/gunit/xplugin/xcl/session_t.h" |
| 31 | |
| 32 | namespace xcl { |
| 33 | namespace test { |
| 34 | namespace { |
| 35 | |
| 36 | const char *expected_sql = "SELECT 1;"; |
| 37 | const int expected_error_code = 2003; |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | template <bool connected> |
| 42 | class Xcl_session_impl_tests_execute : public Xcl_session_impl_tests { |
| 43 | public: |
| 44 | using StmtExecute = Mysqlx::Sql::StmtExecute; |
| 45 | |
| 46 | public: |
| 47 | void SetUp() override { m_sut = std::move(make_sut(connected)); } |
| 48 | }; |
| 49 | |
| 50 | using Xcl_session_impl_tests_execute_connected = |
| 51 | Xcl_session_impl_tests_execute<true>; |
| 52 | using Xcl_session_impl_tests_execute_disconnected = |
| 53 | Xcl_session_impl_tests_execute<false>; |
| 54 | using XQuery_result_ptr = XQuery_result *; |
| 55 | |
| 56 | TEST_F(Xcl_session_impl_tests_execute_disconnected, execute_sql_not_usable) { |
| 57 | XError out_error; |
| 58 | |
| 59 | auto query_result = m_sut->execute_sql(expected_sql, &out_error); |
| 60 | |
| 61 | ASSERT_FALSE(query_result); |
| 62 | ASSERT_EQ(CR_CONNECTION_ERROR, out_error.error()); |
| 63 | } |
| 64 | |
| 65 | TEST_F(Xcl_session_impl_tests_execute_connected, execute_sql_fails) { |
| 66 | XError out_error; |
| 67 | EXPECT_CALL(*m_mock_protocol, |
| 68 | execute_stmt_raw(Cmp_msg("stmt: \"SELECT 1;\""), _)) |
| 69 | .WillOnce(Invoke([](const StmtExecute &stmt [[maybe_unused]], |
| 70 | XError *out_r) -> XQuery_result_ptr { |
| 71 | *out_r = XError{expected_error_code, ""}; |
| 72 | return {}; |
| 73 | })); |
| 74 | |
| 75 | auto query_result = m_sut->execute_sql(expected_sql, &out_error); |
| 76 | ASSERT_FALSE(query_result); |
| 77 | ASSERT_EQ(expected_error_code, out_error.error()); |
| 78 | |
| 79 | expect_connection_close(); |
| 80 | } |
| 81 | |
| 82 | TEST_F(Xcl_session_impl_tests_execute_disconnected, execute_stmt_not_usable) { |
| 83 | XError out_error; |
| 84 | |
| 85 | auto query_result = m_sut->execute_stmt("", expected_sql, {}, &out_error); |
| 86 | |
| 87 | ASSERT_FALSE(query_result); |
| 88 | ASSERT_EQ(CR_CONNECTION_ERROR, out_error.error()); |
| 89 | } |
| 90 | |
| 91 | TEST_F(Xcl_session_impl_tests_execute_connected, execute_stmt_fails) { |
| 92 | XError out_error; |
| 93 | EXPECT_CALL( |
| 94 | *m_mock_protocol, |
| 95 | execute_stmt_raw( |
| 96 | Cmp_msg("stmt: \"SELECT 1;\" namespace: \"our-namespace\""), _)) |
| 97 | .WillOnce(Invoke([](const StmtExecute &stmt [[maybe_unused]], |
| 98 | XError *out_r) -> XQuery_result_ptr { |
| 99 | *out_r = XError{expected_error_code, ""}; |
| 100 | return {}; |
| 101 | })); |
| 102 | |
| 103 | auto query_result = |
| 104 | m_sut->execute_stmt("our-namespace", expected_sql, {}, &out_error); |
| 105 | |
| 106 | ASSERT_FALSE(query_result); |
| 107 | ASSERT_EQ(expected_error_code, out_error.error()); |
| 108 | |
| 109 | expect_connection_close(); |
| 110 | } |
| 111 | |
| 112 | TEST_F(Xcl_session_impl_tests_execute_connected, execute_stmt_no_args) { |
| 113 | XError out_error; |
| 114 | std::unique_ptr<XQuery_result> dummy_result{new mock::XQuery_result}; |
| 115 | auto dummy_result_ptr = dummy_result.get(); |
| 116 | |
| 117 | EXPECT_CALL( |
| 118 | *m_mock_protocol, |
| 119 | execute_stmt_raw( |
| 120 | Cmp_msg("stmt: \"SELECT 1;\" namespace: \"our-namespace\""), _)) |
| 121 | .WillOnce(Invoke([&dummy_result](const StmtExecute &stmt [[maybe_unused]], |
| 122 | XError *out_r) -> XQuery_result_ptr { |
| 123 | *out_r = {}; |
| 124 | return dummy_result.release(); |
| 125 | })); |
| 126 | |
| 127 | auto query_result = |
| 128 | m_sut->execute_stmt("our-namespace", expected_sql, {}, &out_error); |
| 129 | |
| 130 | ASSERT_EQ(dummy_result_ptr, query_result.get()); |
| 131 | ASSERT_FALSE(out_error); |
| 132 | |
| 133 | expect_connection_close(); |
| 134 | } |
| 135 | |
| 136 | TEST_F(Xcl_session_impl_tests_execute_connected, execute_stmt_args_one_scalar) { |
| 137 | XError out_error; |
| 138 | const int64_t argument_value = 1002; |
| 139 | std::unique_ptr<XQuery_result> dummy_result{new mock::XQuery_result}; |
| 140 | auto dummy_result_ptr = dummy_result.get(); |
| 141 | |
| 142 | EXPECT_CALL(*m_mock_protocol, |
| 143 | execute_stmt_raw(Cmp_msg("stmt: \"SELECT 1;\" " |
| 144 | "namespace: \"s\" " |
| 145 | "args { type:SCALAR scalar {" |
| 146 | " type: V_SINT " |
| 147 | " v_signed_int: 1002} }"), |
| 148 | _)) |
| 149 | .WillOnce(Invoke([&dummy_result](const StmtExecute &stmt [[maybe_unused]], |
| 150 | XError *out_r) -> XQuery_result_ptr { |
| 151 | *out_r = {}; |
| 152 | return dummy_result.release(); |
| 153 | })); |
| 154 | |
| 155 | auto query_result = m_sut->execute_stmt( |
| 156 | "s", expected_sql, {Argument_value(argument_value)}, &out_error); |
| 157 | |
| 158 | ASSERT_EQ(dummy_result_ptr, query_result.get()); |
| 159 | ASSERT_FALSE(out_error); |
| 160 | |
| 161 | expect_connection_close(); |
| 162 | } |
| 163 | |
| 164 | TEST_F(Xcl_session_impl_tests_execute_connected, |
| 165 | execute_stmt_args_all_scalars) { |
| 166 | XError out_error; |
| 167 | const int64_t argument_value_int = 1002; |
| 168 | const uint64_t argument_value_uint = 1003; |
| 169 | const bool argument_value_bool = true; |
| 170 | const double argument_value_double = 20.1; |
| 171 | const float argument_value_float = 10.1f; |
| 172 | const std::string argument_value_string = "str1"; |
| 173 | const std::string argument_value_octet = "octet2"; |
| 174 | const std::string argument_value_decimal = "decimal3"; |
| 175 | |
| 176 | std::unique_ptr<XQuery_result> dummy_result{new mock::XQuery_result}; |
| 177 | auto dummy_result_ptr = dummy_result.get(); |
| 178 | |
| 179 | EXPECT_CALL(*m_mock_protocol, |
| 180 | execute_stmt_raw(Cmp_msg("stmt: \"SELECT 1;\" " |
| 181 | "namespace: \"s\" " |
| 182 | "args { type:SCALAR scalar {" |
| 183 | " type: V_SINT " |
| 184 | " v_signed_int: 1002} }" |
| 185 | "args { type:SCALAR scalar {" |
| 186 | " type: V_UINT " |
| 187 | " v_unsigned_int: 1003} }" |
| 188 | "args { type:SCALAR scalar {" |
| 189 | " type: V_BOOL " |
| 190 | " v_bool: true} }" |
| 191 | "args { type:SCALAR scalar {" |
| 192 | " type: V_DOUBLE " |
| 193 | " v_double: 20.1} }" |
| 194 | "args { type:SCALAR scalar {" |
| 195 | " type: V_FLOAT " |
| 196 | " v_float: 10.1} }" |
| 197 | "args { type:SCALAR scalar {" |
| 198 | " type: V_STRING " |
| 199 | " v_string { " |
| 200 | " value: \"str1\" }}} " |
| 201 | "args { type:SCALAR scalar {" |
| 202 | " type: V_OCTETS " |
| 203 | " v_octets { " |
| 204 | " value: \"octet2\" }}} " |
| 205 | "args { type:SCALAR scalar {" |
| 206 | " type: V_STRING " |
| 207 | " v_string { " |
| 208 | " value: \"decimal3\" }}} " |
| 209 | "args { type:SCALAR scalar {" |
| 210 | " type: V_NULL } }"), |
| 211 | _)) |
| 212 | .WillOnce(Invoke([&dummy_result](const StmtExecute &stmt [[maybe_unused]], |
| 213 | XError *out_r) -> XQuery_result_ptr { |
| 214 | *out_r = {}; |
| 215 | return dummy_result.release(); |
| 216 | })); |
| 217 | |
| 218 | auto query_result = m_sut->execute_stmt( |
| 219 | "s", expected_sql, |
| 220 | {Argument_value(argument_value_int), Argument_value(argument_value_uint), |
| 221 | Argument_value(argument_value_bool), |
| 222 | Argument_value(argument_value_double), |
| 223 | Argument_value(argument_value_float), |
| 224 | Argument_value(argument_value_string, |
| 225 | Argument_value::String_type::k_string), |
| 226 | Argument_value(argument_value_octet, |
| 227 | Argument_value::String_type::k_octets), |
| 228 | Argument_value(argument_value_decimal, |
| 229 | Argument_value::String_type::k_decimal), |
| 230 | Argument_value()}, |
| 231 | &out_error); |
| 232 | |
| 233 | ASSERT_EQ(dummy_result_ptr, query_result.get()); |
| 234 | ASSERT_FALSE(out_error); |
| 235 | |
| 236 | expect_connection_close(); |
| 237 | } |
| 238 | |
| 239 | TEST_F(Xcl_session_impl_tests_execute_connected, |
| 240 | execute_stmt_args_object_scalars) { |
| 241 | XError out_error; |
| 242 | const Argument_value argument_value_int{static_cast<uint64_t>(1002)}; |
| 243 | const Argument_value argument_value_double{20.1}; |
| 244 | const Argument_value argument_value_string{"str1"}; |
| 245 | |
| 246 | std::unique_ptr<XQuery_result> dummy_result{new mock::XQuery_result}; |
| 247 | auto dummy_result_ptr = dummy_result.get(); |
| 248 | |
| 249 | EXPECT_CALL(*m_mock_protocol, |
| 250 | execute_stmt_raw(Cmp_msg("stmt: \"SELECT 1;\" " |
| 251 | "namespace: \"s\" " |
| 252 | "args { type:OBJECT obj { " |
| 253 | " fld { " |
| 254 | " key: \"key1\" " |
| 255 | " value { " |
| 256 | " type: SCALAR scalar { " |
| 257 | " type: V_UINT " |
| 258 | " v_unsigned_int: 1002 " |
| 259 | " } } } " |
| 260 | " fld { " |
| 261 | " key: \"key2\" " |
| 262 | " value { " |
| 263 | " type: SCALAR scalar { " |
| 264 | " type: V_DOUBLE " |
| 265 | " v_double: 20.1 " |
| 266 | " } } } " |
| 267 | " fld { " |
| 268 | " key: \"key3\" " |
| 269 | " value { " |
| 270 | " type: SCALAR scalar { " |
| 271 | " type: V_STRING " |
| 272 | " v_string { " |
| 273 | " value: \"str1\" } " |
| 274 | " } } } " |
| 275 | "} }"), |
| 276 | _)) |
| 277 | .WillOnce(Invoke([&dummy_result](const StmtExecute &stmt [[maybe_unused]], |
| 278 | XError *out_r) -> XQuery_result_ptr { |
| 279 | *out_r = {}; |
| 280 | return dummy_result.release(); |
| 281 | })); |
| 282 | |
| 283 | Argument_value::Object obj; |
| 284 | obj.emplace("key1", argument_value_int); |
| 285 | obj.emplace("key2", argument_value_double); |
| 286 | obj.emplace("key3", argument_value_string); |
| 287 | |
| 288 | auto query_result = |
| 289 | m_sut->execute_stmt("s", expected_sql, {Argument_value{obj}}, &out_error); |
| 290 | |
| 291 | ASSERT_EQ(dummy_result_ptr, query_result.get()); |
| 292 | ASSERT_FALSE(out_error); |
| 293 | |
| 294 | expect_connection_close(); |
| 295 | } |
| 296 | |
| 297 | TEST_F(Xcl_session_impl_tests_execute_connected, |
| 298 | execute_stmt_args_array_scalars) { |
| 299 | XError out_error; |
| 300 | const Argument_value argument_value_int{static_cast<uint64_t>(1002)}; |
| 301 | const Argument_value argument_value_double{20.1}; |
| 302 | const Argument_value argument_value_string{"str1"}; |
| 303 | |
| 304 | std::unique_ptr<XQuery_result> dummy_result{new mock::XQuery_result}; |
| 305 | auto dummy_result_ptr = dummy_result.get(); |
| 306 | |
| 307 | EXPECT_CALL(*m_mock_protocol, |
| 308 | execute_stmt_raw(Cmp_msg("stmt: \"SELECT 1;\" " |
| 309 | "namespace: \"s\" " |
| 310 | "args { type:ARRAY array { " |
| 311 | " value { " |
| 312 | " type: SCALAR scalar { " |
| 313 | " type: V_UINT " |
| 314 | " v_unsigned_int: 1002 " |
| 315 | " } } " |
| 316 | " value { " |
| 317 | " type: SCALAR scalar { " |
| 318 | " type: V_DOUBLE " |
| 319 | " v_double: 20.1 " |
| 320 | " } } " |
| 321 | " value { " |
| 322 | " type: SCALAR scalar { " |
| 323 | " type: V_STRING " |
| 324 | " v_string { " |
| 325 | " value: \"str1\" } " |
| 326 | " } } " |
| 327 | "} }"), |
| 328 | _)) |
| 329 | .WillOnce(Invoke([&dummy_result](const StmtExecute &stmt [[maybe_unused]], |
| 330 | XError *out_r) -> XQuery_result_ptr { |
| 331 | *out_r = {}; |
| 332 | return dummy_result.release(); |
| 333 | })); |
| 334 | |
| 335 | auto query_result = m_sut->execute_stmt( |
| 336 | "s", expected_sql, |
| 337 | {Argument_value{Argument_array{argument_value_int, argument_value_double, |
| 338 | argument_value_string}}}, |
| 339 | &out_error); |
| 340 | |
| 341 | ASSERT_EQ(dummy_result_ptr, query_result.get()); |
| 342 | ASSERT_FALSE(out_error); |
| 343 | |
| 344 | expect_connection_close(); |
| 345 | } |
| 346 | |
| 347 | } // namespace test |
| 348 | } // namespace xcl |