| 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 <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | #include "plugin/x/src/sql_statement_builder.h" |
| 30 | |
| 31 | #include "plugin/x/src/ngs/error_code.h" |
| 32 | #include "plugin/x/src/query_string_builder.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h" |
| 34 | |
| 35 | namespace xpl { |
| 36 | namespace test { |
| 37 | |
| 38 | using Placeholder_list = Sql_statement_builder::Placeholder_list; |
| 39 | |
| 40 | template <typename T> |
| 41 | class Sql_statement_builder_test_base : public testing::TestWithParam<T> { |
| 42 | public: |
| 43 | Query_string_builder m_qb; |
| 44 | Sql_statement_builder m_builder{&m_qb}; |
| 45 | }; |
| 46 | |
| 47 | struct Param_sql_statement_builder { |
| 48 | std::string expect_query; |
| 49 | std::string query; |
| 50 | Any_list args; |
| 51 | }; |
| 52 | |
| 53 | Param_sql_statement_builder sql_statement_builder_success_param[] = { |
| 54 | {"", "", {}}, |
| 55 | {"?", "?", {}}, |
| 56 | {"abc", "abc", {}}, |
| 57 | {"1", "?", {Scalar(1)}}, |
| 58 | {"'abc'", "?", {Scalar::String("abc")}}, |
| 59 | {"'efg'", "?", {Scalar::Octets("efg")}}, |
| 60 | {"3.14", "?", {Scalar(3.14f)}}, |
| 61 | {"2.7182", "?", {Scalar(2.7182)}}, |
| 62 | {"2, 3, 1", "?, ?, ?", {Scalar(2), Scalar(3), Scalar(1)}}, |
| 63 | {"2, 3, ?", "?, ?, ?", {Scalar(2), Scalar(3)}}, |
| 64 | }; |
| 65 | |
| 66 | using Sql_statement_builder_success_test = |
| 67 | Sql_statement_builder_test_base<Param_sql_statement_builder>; |
| 68 | |
| 69 | TEST_P(Sql_statement_builder_success_test, build_query_success) { |
| 70 | const ParamType ¶m = GetParam(); |
| 71 | ASSERT_NO_THROW(m_builder.build(param.query, param.args)); |
| 72 | EXPECT_STREQ(param.expect_query.c_str(), m_qb.get().c_str()); |
| 73 | } |
| 74 | |
| 75 | INSTANTIATE_TEST_SUITE_P( |
| 76 | Sql_statement_builder, Sql_statement_builder_success_test, |
| 77 | testing::ValuesIn(sql_statement_builder_success_param)); |
| 78 | |
| 79 | struct Param_sql_statement_builder_fail { |
| 80 | std::string query; |
| 81 | Any_list args; |
| 82 | }; |
| 83 | |
| 84 | Param_sql_statement_builder_fail sql_statement_builder_fail_param[] = { |
| 85 | {"", {Scalar(1)}}, |
| 86 | {"?", {Scalar(2), Scalar(3)}}, |
| 87 | {"?", {Any::Array{1, 2, 3}}}, |
| 88 | {"?", {Any::Object{{"a", 1}}}}, |
| 89 | }; |
| 90 | |
| 91 | using Sql_statement_builder_fail_test = |
| 92 | Sql_statement_builder_test_base<Param_sql_statement_builder_fail>; |
| 93 | |
| 94 | TEST_P(Sql_statement_builder_fail_test, build_query_fail) { |
| 95 | const ParamType ¶m = GetParam(); |
| 96 | ASSERT_THROW(m_builder.build(param.query, param.args), ngs::Error_code); |
| 97 | } |
| 98 | |
| 99 | INSTANTIATE_TEST_SUITE_P(Sql_statement_builder, Sql_statement_builder_fail_test, |
| 100 | testing::ValuesIn(sql_statement_builder_fail_param)); |
| 101 | |
| 102 | struct Param_sql_statement_builder_placeholders { |
| 103 | std::string expect_query; |
| 104 | Placeholder_list expect_placeholders; |
| 105 | std::string query; |
| 106 | Any_list args; |
| 107 | }; |
| 108 | |
| 109 | Param_sql_statement_builder_placeholders |
| 110 | sql_statement_builder_placeholders_param[] = { |
| 111 | {"", {}, "", {}}, |
| 112 | {"?", {0}, "?", {}}, |
| 113 | {"abc", {}, "abc", {}}, |
| 114 | {"1", {}, "?", {Scalar(1)}}, |
| 115 | {"'abc'", {}, "?", {Scalar::String("abc")}}, |
| 116 | {"'efg'", {}, "?", {Scalar::Octets("efg")}}, |
| 117 | {"3.14", {}, "?", {Scalar(3.14f)}}, |
| 118 | {"2.7182", {}, "?", {Scalar(2.7182)}}, |
| 119 | {"2, 3, 1", {}, "?, ?, ?", {Scalar(2), Scalar(3), Scalar(1)}}, |
| 120 | {"2, 3, ?", {0}, "?, ?, ?", {Scalar(2), Scalar(3)}}, |
| 121 | {"?, ?, ?", {0, 1, 2}, "?, ?, ?", {}}, |
| 122 | }; |
| 123 | |
| 124 | using Sql_statement_builder_placeholders_test = |
| 125 | Sql_statement_builder_test_base<Param_sql_statement_builder_placeholders>; |
| 126 | |
| 127 | TEST_P(Sql_statement_builder_placeholders_test, |
| 128 | build_query_placeholders_success) { |
| 129 | const ParamType ¶m = GetParam(); |
| 130 | Placeholder_list phs; |
| 131 | ASSERT_NO_THROW(m_builder.build(param.query, param.args, &phs)); |
| 132 | EXPECT_STREQ(param.expect_query.c_str(), m_qb.get().c_str()); |
| 133 | EXPECT_EQ(param.expect_placeholders, phs); |
| 134 | } |
| 135 | |
| 136 | INSTANTIATE_TEST_SUITE_P( |
| 137 | Sql_statement_builder_placeholders, Sql_statement_builder_placeholders_test, |
| 138 | testing::ValuesIn(sql_statement_builder_placeholders_param)); |
| 139 | |
| 140 | } // namespace test |
| 141 | } // namespace xpl |