The Battle for Wesnoth  1.15.3+dev
ParameterizedQueryTest.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright The ViaDuck Project 2016 - 2020.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
10 
12  mariadb::statement_ref selectQuery =
13  m_con->create_statement("SELECT * FROM " + m_table_name + " WHERE 1=?;");
14  selectQuery->set_unsigned32(0, 1);
15 
16  mariadb::result_set_ref queryResult = selectQuery->query();
17 
18  ASSERT_TRUE(!!queryResult);
19  ASSERT_TRUE(queryResult->next());
20 }
21 
23  mariadb::statement_ref emptyQuery =
24  m_con->create_statement("SELECT * FROM " + m_table_name + " WHERE 1=?;");
25 
26  mariadb::result_set_ref queryResult = emptyQuery->query();
27  ASSERT_TRUE(!!emptyQuery);
28  ASSERT_TRUE(!!queryResult);
29  ASSERT_FALSE(queryResult->next());
30 }
31 
32 TEST_F(ParameterizedQueryTest, emptyBindQuery) {
33  // fixme: Invalid query object instead of exception maybe?
34  EXPECT_ANY_THROW(mariadb::statement_ref emptyQuery = m_con->create_statement(""));
35 }
36 
37 TEST_F(ParameterizedQueryTest, bindAfterQuery) {
38  mariadb::statement_ref errorQuery =
39  m_con->create_statement("SELECT * FROM " + m_table_name + " WHERE id = ?;");
40  mariadb::result_set_ref queryResult = errorQuery->query();
41 
42  EXPECT_NO_THROW(errorQuery->set_unsigned32(0, 1));
43 }
44 
45 TEST_F(ParameterizedQueryTest, bindAnyDataType) {
46  mariadb::statement_ref errorQuery;
47  mariadb::statement_ref testQuery;
48  mariadb::result_set_ref queryResult;
49 
50  m_con->create_statement("SET sql_mode = 'STRICT_TRANS_TABLES';")->execute();
51 
52 #define ParamTest_TEST(call, call2, name, value) \
53  errorQuery = m_con->create_statement("UPDATE " + m_table_name + " SET " + name + "= ?;"); \
54  call; \
55  errorQuery->execute(); \
56  testQuery = m_con->create_statement("SELECT " + std::string(name) + " FROM " + m_table_name + \
57  " WHERE id = 1;"); \
58  queryResult = testQuery->query(); \
59  ASSERT_TRUE(queryResult->next()); \
60  ASSERT_EQ(call2, value);
61 
62  decimal d = decimal("0.02");
65 
66  ParamTest_TEST(errorQuery->set_unsigned32(0, (unsigned int)299), queryResult->get_unsigned32(0),
67  "preis", 299);
68  ParamTest_TEST(errorQuery->set_string(0, "TESTSTRING"), queryResult->get_string(0), "str",
69  "TESTSTRING");
70  ParamTest_TEST(errorQuery->set_null(0), queryResult->get_string(0), "str", "");
71  ParamTest_TEST(errorQuery->set_null(0), queryResult->get_is_null(0), "str", true);
72  EXPECT_ANY_THROW(ParamTest_TEST(errorQuery->set_null(0), queryResult->get_string(0), "nnstr", ""));
73  ParamTest_TEST(errorQuery->set_string(0, ""), queryResult->get_string(0), "nnstr", "");
74  ParamTest_TEST(errorQuery->set_boolean(0, true), queryResult->get_boolean(0), "b", true);
75  ParamTest_TEST(errorQuery->set_double(0, 0.03), queryResult->get_double(0), "dd", 0.03);
76  ParamTest_TEST(errorQuery->set_double(0, -0.03), queryResult->get_double(0), "dd", -0.03);
77  ParamTest_TEST(errorQuery->set_signed32(0, 100), queryResult->get_signed32(0), "preis", 100);
78  ParamTest_TEST(errorQuery->set_date_time(0, t), queryResult->get_date_time(0), "tim", t);
79  ParamTest_TEST(errorQuery->set_date_time(0, z), queryResult->get_date_time(0), "tim", z);
80  ParamTest_TEST(errorQuery->set_time(0, ti), queryResult->get_time(0), "tiim", ti);
81  ParamTest_TEST(errorQuery->set_decimal(0, d), queryResult->get_decimal(0).str(), "d", "0.02");
82  ParamTest_TEST(errorQuery->set_null(0), queryResult->get_is_null(0), "nul", true);
83 }
84 
86  mariadb::statement_ref crashQuery =
87  m_con->create_statement("INSERT INTO " + m_table_name + " (id, preis) VALUES (2, ?);");
88  crashQuery->set_unsigned32(0, 1);
89 
90  crashQuery->query();
91 }
92 
94  mariadb::statement_ref errorQuery =
95  m_con->create_statement("SELECT * FROM " + m_table_name + " WHERE id = ?;");
96 
97  const char* c =
98  "0123456789012345678901234567890123456789"
99  "0123456789012345678901234567890123456789"
100  "0123456789012345678901234567890123456789"
101  "0123456789012345678901234567890123456789"
102  "0123456789012345678901234567890123456789"
103  "0123456789012345678901234567890123456789"
104  "0123456789012345678901234567890123456789"
105  "0123456789012345678901234567890123456789"
106  "0123456789012345678901234567890123456789"
107  "0123456789012345678901234567890123456789";
108 
109  errorQuery->set_data(0, mariadb::data_ref(new mariadb::data<char>(c, 400)));
110 
111  mariadb::result_set_ref queryResult = errorQuery->query();
112 
113  ASSERT_TRUE(!!queryResult);
114  ASSERT_FALSE(queryResult->next());
115 }
116 
117 TEST_F(ParameterizedQueryTest, bindDataBlobNullPtr) {
118  mariadb::statement_ref errorQuery =
119  m_con->create_statement("SELECT * FROM " + m_table_name + " WHERE id = ?;");
120  errorQuery->set_data(0, nullptr);
121 }
122 
123 TEST_F(ParameterizedQueryTest, bindWithoutParameters) {
124  mariadb::statement_ref errorQuery = m_con->create_statement("SELECT 1;");
125 
126  EXPECT_ANY_THROW(errorQuery->set_unsigned32(1, 100));
127 }
128 
129 TEST_F(ParameterizedQueryTest, bindReuseSimple) {
130  mariadb::statement_ref insertQuery = m_con->create_statement("INSERT INTO " + m_table_name + "(preis) VALUES(?);");
131 
132  // bind 1
133  insertQuery->set_unsigned32(0, 177);
134  u64 row1 = insertQuery->insert();
135 
136  // bind 2
137  insertQuery->set_unsigned32(0, 1337);
138  u64 row2 = insertQuery->insert();
139 
140  // bind 3 - reuse
141  u64 row3 = insertQuery->insert();
142 
143  mariadb::statement_ref selectQuery = m_con->create_statement("SELECT preis FROM " + m_table_name + " WHERE id = ?");
144 
145  // test 1
146  selectQuery->set_unsigned64(0, row1);
147  mariadb::result_set_ref result = selectQuery->query();
148  ASSERT_TRUE(!!result);
149  ASSERT_TRUE(result->next());
150  EXPECT_EQ(177u, result->get_unsigned32(0));
151 
152  // test2
153  selectQuery->set_unsigned64(0, row2);
154  mariadb::result_set_ref result2 = selectQuery->query();
155  ASSERT_TRUE(!!result2);
156  ASSERT_TRUE(result2->next());
157  EXPECT_EQ(1337u, result2->get_unsigned32(0));
158 
159  // test3
160  selectQuery->set_unsigned64(0, row3);
161  mariadb::result_set_ref result3 = selectQuery->query();
162  ASSERT_TRUE(!!result3);
163  ASSERT_TRUE(result3->next());
164  EXPECT_EQ(1337u, result3->get_unsigned32(0));
165 }
166 
167 TEST_F(ParameterizedQueryTest, bindReuseString) {
168  mariadb::statement_ref insertQuery = m_con->create_statement("INSERT INTO " + m_table_name + "(str) VALUES(?);");
169 
170  // bind 1
171  insertQuery->set_string(0, "asdf");
172  u64 row1 = insertQuery->insert();
173 
174  // bind 2
175  insertQuery->set_string(0, "qwertz");
176  u64 row2 = insertQuery->insert();
177 
178  // bind 3
179  insertQuery->set_string(0, "");
180  u64 row3 = insertQuery->insert();
181 
182  mariadb::statement_ref selectQuery = m_con->create_statement("SELECT str FROM " + m_table_name + " WHERE id = ?;");
183 
184  // test 1
185  selectQuery->set_unsigned64(0, row1);
186  mariadb::result_set_ref result = selectQuery->query();
187  ASSERT_TRUE(!!result);
188  ASSERT_TRUE(result->next());
189  EXPECT_EQ("asdf", result->get_string(0));
190 
191  // test2
192  selectQuery->set_unsigned64(0, row2);
193  mariadb::result_set_ref result2 = selectQuery->query();
194  ASSERT_TRUE(!!result2);
195  ASSERT_TRUE(result2->next());
196  EXPECT_EQ("qwertz", result2->get_string(0));
197 
198  // test3
199  selectQuery->set_unsigned64(0, row3);
200  mariadb::result_set_ref result3 = selectQuery->query();
201  ASSERT_TRUE(!!result3);
202  ASSERT_TRUE(result3->next());
203  EXPECT_EQ("", result3->get_string(0));
204 }
std::shared_ptr< result_set > result_set_ref
Definition: result_set.hpp:219
Class representing SQL time.
Definition: time.hpp:23
return decimal(std::string(m_row[index], column_size(index)))
static date_time now()
Gets the current date and time as date_time.
Definition: date_time.cpp:507
#define d
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
#define ParamTest_TEST(call, call2, name, value)
TEST_F(ParameterizedQueryTest, bindNormal)
Class used to represent SQL date_time.
Definition: date_time.hpp:20
static time now()
Uses time.h to determine the current time in the local timezone.
Definition: time.cpp:302
double t
Definition: astarsearch.cpp:64
unsigned long long u64
Definition: types.hpp:34
std::shared_ptr< statement > statement_ref
Definition: statement.hpp:109
mock_char c