test-odbc-db2.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include "soci.h"
9 #include "soci-odbc.h"
10 #include "common-tests.h"
11 #include <iostream>
12 #include <string>
13 #include <cassert>
14 #include <ctime>
15 #include <cmath>
16 
17 using namespace soci;
18 using namespace soci::tests;
19 
20 std::string connectString;
22 
23 // DDL Creation objects for common tests
25 {
27  : table_creator_base(sql)
28  {
29  sql << "CREATE TABLE SOCI_TEST(ID INTEGER, VAL SMALLINT, C CHAR, STR VARCHAR(20), SH SMALLINT, UL NUMERIC(20), D DOUBLE, "
30  "TM TIMESTAMP(9), I1 INTEGER, I2 INTEGER, I3 INTEGER, NAME VARCHAR(20))";
31  }
32 };
33 
35 {
37  : table_creator_base(sql)
38  {
39  sql << "CREATE TABLE SOCI_TEST(NUM_FLOAT DOUBLE, NUM_INT INTEGER, NAME VARCHAR(20), SOMETIME TIMESTAMP, CHR CHAR)";
40  }
41 };
42 
44 {
46  : table_creator_base(sql)
47  {
48  sql << "CREATE TABLE SOCI_TEST(NAME VARCHAR(100) NOT NULL, PHONE VARCHAR(15))";
49  }
50 };
51 
53 {
55  : table_creator_base(sql)
56  {
57  sql << "CREATE TABLE SOCI_TEST(VAL INTEGER)";
58  }
59 };
60 
61 //
62 // Support for SOCI Common Tests
63 //
64 
65 class test_context : public test_context_base
66 {
67 public:
68  test_context(backend_factory const &backEnd,
69  std::string const &connectString)
70  : test_context_base(backEnd, connectString) {}
71 
73  {
74  return new table_creator_one(s);
75  }
76 
78  {
79  return new table_creator_two(s);
80  }
81 
83  {
84  return new table_creator_three(s);
85  }
86 
88  {
90  }
91 
92  std::string to_date_time(std::string const &datdt_string) const
93  {
94  return "\'" + datdt_string + "\'";
95  }
96 };
97 
99 {
101  : table_creator_base(sql)
102  {
103  sql << "CREATE TABLE SOCI_TEST (VAL BIGINT)";
104  }
105 };
106 
108 {
109  const int num_recs = 100;
110  session sql(backEnd, connectString);
111  table_creator_bigint table(sql);
112 
113  {
114  long long n;
115  statement st = (sql.prepare <<
116  "INSERT INTO SOCI_TEST (VAL) VALUES (:val)", use(n));
117  for (int i = 0; i < num_recs; i++)
118  {
119  n = 1000000000LL + i;
120  st.execute();
121  }
122  }
123  {
124  long long n2;
125  statement st = (sql.prepare <<
126  "SELECT VAL FROM SOCI_TEST ORDER BY VAL", into(n2));
127  st.execute();
128  for (int i = 0; i < num_recs; i++)
129  {
130  st.fetch();
131  assert(n2 == 1000000000LL + i);
132  }
133  }
134 
135  std::cout << "test odbc_db2_long_long passed" << std::endl;
136 }
137 
139 {
140  const int num_recs = 100;
141  session sql(backEnd, connectString);
142  table_creator_bigint table(sql);
143 
144  {
145  unsigned long long n;
146  statement st = (sql.prepare <<
147  "INSERT INTO SOCI_TEST (VAL) VALUES (:val)", use(n));
148  for (int i = 0; i < num_recs; i++)
149  {
150  n = 1000000000LL + i;
151  st.execute();
152  }
153  }
154  {
155  unsigned long long n2;
156  statement st = (sql.prepare <<
157  "SELECT VAL FROM SOCI_TEST ORDER BY VAL", into(n2));
158  st.execute();
159  for (int i = 0; i < num_recs; i++)
160  {
161  st.fetch();
162  assert(n2 == 1000000000LL + i);
163  }
164  }
165 
166  std::cout << "test odbc_db2_unsigned_long_long passed" << std::endl;
167 }
168 
170 {
171  const std::size_t num_recs = 100;
172  session sql(backEnd, connectString);
173  table_creator_bigint table(sql);
174 
175  {
176  std::vector<long long> v(num_recs);
177  for (std::size_t i = 0; i < num_recs; i++)
178  {
179  v[i] = 1000000000LL + i;
180  }
181 
182  sql << "INSERT INTO SOCI_TEST (VAL) VALUES (:bi)", use(v);
183  }
184  {
185  std::size_t recs = 0;
186 
187  std::vector<long long> v(num_recs / 2 + 1);
188  statement st = (sql.prepare <<
189  "SELECT VAL FROM SOCI_TEST ORDER BY VAL", into(v));
190  st.execute();
191  while (true)
192  {
193  if (!st.fetch())
194  {
195  break;
196  }
197 
198  const std::size_t vsize = v.size();
199  for (std::size_t i = 0; i < vsize; i++)
200  {
201  assert(v[i] == 1000000000LL +
202  static_cast<long long>(recs));
203  recs++;
204  }
205  }
206  assert(recs == num_recs);
207  }
208 
209  std::cout << "test odbc_db2_long_long_vector passed" << std::endl;
210 }
211 
213 {
214  const std::size_t num_recs = 100;
215  session sql(backEnd, connectString);
216  table_creator_bigint table(sql);
217 
218  {
219  std::vector<unsigned long long> v(num_recs);
220  for (std::size_t i = 0; i < num_recs; i++)
221  {
222  v[i] = 1000000000LL + i;
223  }
224 
225  sql << "INSERT INTO SOCI_TEST (VAL) VALUES (:bi)", use(v);
226  }
227  {
228  std::size_t recs = 0;
229 
230  std::vector<unsigned long long> v(num_recs / 2 + 1);
231  statement st = (sql.prepare <<
232  "SELECT VAL FROM SOCI_TEST ORDER BY VAL", into(v));
233  st.execute();
234  while (true)
235  {
236  if (!st.fetch())
237  {
238  break;
239  }
240 
241  const std::size_t vsize = v.size();
242  for (std::size_t i = 0; i < vsize; i++)
243  {
244  assert(v[i] == 1000000000LL +
245  static_cast<unsigned long long>(recs));
246  recs++;
247  }
248  }
249  assert(recs == num_recs);
250  }
251 
252  std::cout << "test odbc_db2_unsigned_long_long_vector passed" << std::endl;
253 }
254 
255 int main(int argc, char** argv)
256 {
257 #ifdef _MSC_VER
258  // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
259  // instead of debug message window.
260  // This hack is required to run asser()-driven tests by Buildbot.
261  // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
262  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
263  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
264 #endif //_MSC_VER
265 
266  if (argc == 2)
267  {
268  connectString = argv[1];
269  }
270  else
271  {
272  std::cerr << std::endl <<
273  "usage: test-odbc-db2 \"DSN=<db>;Uid=<user>;Pwd=<password>\"" <<
274  std::endl << std::endl;
275  return EXIT_FAILURE;
276  }
277  try
278  {
279  std::cout << "\nSOCI ODBC with DB2 Tests:\n\n";
280 
281  test_context tc(backEnd, connectString);
282  common_tests tests(tc);
283  tests.run();
284 
285  std::cout << "\nSOCI DB2 Specific Tests:\n\n";
290 
291  std::cout << "\nOK, all tests passed.\n\n";
292  return EXIT_SUCCESS;
293  }
294  catch (soci::odbc_soci_error const & e)
295  {
296  std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
297  << "Native Error Code: " << e.native_error_code() << std::endl
298  << "SOCI Message: " << e.what() << std::endl
299  << "ODBC Message: " << e.odbc_error_message() << std::endl;
300  }
301  catch (std::exception const & e)
302  {
303  std::cout << "STD::EXECEPTION " << e.what() << '\n';
304  }
305  return EXIT_FAILURE;
306 }
SQLCHAR const * odbc_error_message() const
Definition: soci-odbc.h:341
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
test_context(backend_factory const &backEnd, std::string const &connectString)
void test_odbc_db2_unsigned_long_long()
table_creator_base * table_creator_2(session &s) const
void run(bool dbSupportsTransactions=true)
Definition: common-tests.h:293
void test_odbc_db2_unsigned_long_long_vector()
table_creator_one(session &sql)
table_creator_base * table_creator_4(session &s) const
backend_factory const & backEnd
void test_odbc_db2_long_long()
SQLINTEGER native_error_code() const
Definition: soci-odbc.h:337
std::string connectString
table_creator_base * table_creator_1(session &s) const
void test_odbc_db2_long_long_vector()
table_creator_base * table_creator_3(session &s) const
SQLCHAR const * odbc_error_code() const
Definition: soci-odbc.h:333
std::string to_date_time(std::string const &datdt_string) const
int main(int argc, char **argv)
table_creator_bigint(session &sql)
table_creator_two(session &sql)
SOCI_ODBC_DECL backend_factory const * factory_odbc()
details::prepare_type prepare
Definition: session.h:69
table_creator_three(session &sql)
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
Definition: use.h:43
bool execute(bool withDataExchange=false)
Definition: statement.h:208


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41