test-db2.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2011-2013 Denis Chapligin
3 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #include "soci.h"
10 #include "soci-db2.h"
11 #include "common-tests.h"
12 #include <iostream>
13 #include <string>
14 #include <cassert>
15 #include <cstdlib>
16 #include <ctime>
17 
18 using namespace soci;
19 using namespace soci::tests;
20 
21 std::string connectString;
23 
24 
25 //
26 // Support for soci Common Tests
27 //
28 
30 {
32  : table_creator_base(sql)
33  {
34  sql << "CREATE TABLE SOCI_TEST(ID INTEGER, VAL SMALLINT, C CHAR, STR VARCHAR(20), SH SMALLINT, UL NUMERIC(20), D DOUBLE, "
35  "TM TIMESTAMP, I1 INTEGER, I2 INTEGER, I3 INTEGER, NAME VARCHAR(20))";
36  }
37 };
38 
40 {
42  : table_creator_base(sql)
43  {
44  sql << "CREATE TABLE SOCI_TEST(NUM_FLOAT DOUBLE, NUM_INT INTEGER, NAME VARCHAR(20), SOMETIME TIMESTAMP, CHR CHAR)";
45  }
46 };
47 
49 {
51  : table_creator_base(sql)
52  {
53  sql << "CREATE TABLE SOCI_TEST(NAME VARCHAR(100) NOT NULL, PHONE VARCHAR(15))";
54  }
55 };
56 
58 {
60  : table_creator_base(sql)
61  {
62  sql << "CREATE TABLE SOCI_TEST(VAL INTEGER)";
63  }
64 };
65 
67 {
68 public:
69  test_context(backend_factory const & pi_back_end, std::string const & pi_connect_string)
70  : test_context_base(pi_back_end, pi_connect_string) {}
71 
73  {
74  pr_s << "SET CURRENT SCHEMA = 'DB2INST1'";
75  return new table_creator_one(pr_s);
76  }
77 
79  {
80  pr_s << "SET CURRENT SCHEMA = 'DB2INST1'";
81  return new table_creator_two(pr_s);
82  }
83 
85  {
86  pr_s << "SET CURRENT SCHEMA = 'DB2INST1'";
87  return new table_creator_three(pr_s);
88  }
89 
91  {
93  }
94 
95  std::string to_date_time(std::string const & pi_datdt_string) const
96  {
97  return "to_date('" + pi_datdt_string + "', 'YYYY-MM-DD HH24:MI:SS')";
98  }
99 };
100 
101 
102 //
103 // Additional tests to exercise the DB2 backend
104 //
105 
106 void test1()
107 {
108  {
109  session sql(backEnd, connectString);
110 
111  sql << "SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1";
112  sql << "SELECT " << 123 << " FROM SYSIBM.SYSDUMMY1";
113 
114  std::string query = "CREATE TABLE DB2INST1.SOCI_TEST (ID BIGINT,DATA VARCHAR(8))";
115  sql << query;
116 
117  {
118  const int i = 7;
119  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(i,"id");
120  int j = 0;
121  sql << "select id from db2inst1.SOCI_TEST where id=7", into(j);
122  assert(j == i);
123  }
124 
125  {
126  const long int li = 9;
127  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(li,"id");
128  long int lj = 0;;
129  sql << "select id from db2inst1.SOCI_TEST where id=9", into(lj);
130  assert(lj == li);
131  }
132 
133  {
134  const long long ll = 11;
135  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(ll,"id");
136  long long lj = 0;
137  sql << "select id from db2inst1.SOCI_TEST where id=11", into(lj);
138  assert(lj == ll);
139  }
140 
141  {
142  const int i = 13;
143  indicator i_ind = i_ok;
144  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(i,i_ind,"id");
145  int j = 0;
146  indicator j_ind = i_null;
147  sql << "select id from db2inst1.SOCI_TEST where id=13", into(j,j_ind);
148  assert(j == i);
149  assert(j_ind == i_ok);
150  }
151 
152  {
153  std::vector<int> numbers(100);
154  for (int i = 0 ; i < 100 ; i++)
155  {
156  numbers[i] = i + 1000;
157  }
158  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(numbers,"id");
159  sql << "select id from db2inst1.SOCI_TEST where id >= 1000 and id < 2000 order by id", into(numbers);
160  for (int i = 0 ; i < 100 ; i++)
161  {
162  assert(numbers[i] == i + 1000);
163  }
164  }
165 
166  {
167  std::vector<int> numbers(100);
168  std::vector<indicator> inds(100);
169  for (int i = 0 ; i < 100 ; i++)
170  {
171  numbers[i] = i + 2000;
172  inds[i] = i_ok;
173  }
174  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(numbers,inds,"id");
175  for (int i = 0 ; i < 100 ; i++)
176  {
177  numbers[i] = 0;
178  inds[i] = i_null;
179  }
180  sql << "select id from db2inst1.SOCI_TEST where id >= 2000 and id < 3000 order by id", into(numbers,inds);
181  for (int i = 0 ; i < 100 ; i++)
182  {
183  assert(numbers[i] == i + 2000);
184  assert(inds[i] == i_ok);
185  }
186  }
187 
188  {
189  int i = 0;
190  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST where id < 1000", into(i));
191  st.execute();
192  st.fetch();
193  assert (i == 7);
194  st.fetch();
195  assert (i == 9);
196  st.fetch();
197  assert (i == 11);
198  st.fetch();
199  assert (i == 13);
200  }
201 
202  {
203  int i = 0;
204  indicator i_ind = i_null;
205  std::string d;
206  indicator d_ind = i_ok;
207  statement st = (sql.prepare << "select id, data from db2inst1.SOCI_TEST where id = 13", into(i, i_ind), into(d, d_ind));
208  st.execute();
209  st.fetch();
210  assert (i == 13);
211  assert (i_ind == i_ok);
212  assert (d_ind == i_null);
213  }
214 
215  {
216  std::vector<int> numbers(100);
217  for (int i = 0 ; i < 100 ; i++)
218  {
219  numbers[i] = 0;
220  }
221  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST where id >= 1000 order by id", into(numbers));
222  st.execute();
223  st.fetch();
224  for (int i = 0 ; i < 100 ; i++)
225  {
226  assert(numbers[i] == i + 1000);
227  }
228  st.fetch();
229  for (int i = 0 ; i < 100 ; i++)
230  {
231  assert(numbers[i] == i + 2000);
232  }
233  }
234 
235  {
236  std::vector<int> numbers(100);
237  std::vector<indicator> inds(100);
238  for (int i = 0 ; i < 100 ; i++)
239  {
240  numbers[i] = 0;
241  inds[i] = i_null;
242  }
243  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST where id >= 1000 order by id", into(numbers, inds));
244  st.execute();
245  st.fetch();
246  for (int i = 0 ; i < 100 ; i++)
247  {
248  assert(numbers[i] == i + 1000);
249  assert(inds[i] == i_ok);
250  }
251  st.fetch();
252  for (int i = 0 ; i < 100 ; i++)
253  {
254  assert(numbers[i] == i + 2000);
255  assert(inds[i] == i_ok);
256  }
257  }
258 
259  {
260  // XXX: what is the purpose of this test?? what is the expected value?
261  int i = 0;
262  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST", use(i));
263  }
264 
265  {
266  // XXX: what is the purpose of this test?? what is the expected value?
267  int i = 0;
268  indicator ind = i_ok;
269  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST", use(i, ind));
270  }
271 
272  {
273  // XXX: what is the purpose of this test?? what is the expected value?
274  std::vector<int> numbers(100);
275  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST", use(numbers));
276  }
277 
278  {
279  // XXX: what is the purpose of this test?? what is the expected value?
280  std::vector<int> numbers(100);
281  std::vector<indicator> inds(100);
282  statement st = (sql.prepare << "select id from db2inst1.SOCI_TEST", use(numbers, inds));
283  }
284 
285  sql<<"DROP TABLE DB2INST1.SOCI_TEST";
286 
287  sql.commit();
288  }
289 
290  std::cout << "test 1 passed" << std::endl;
291 }
292 
293 void test2() {
294  {
295  session sql(backEnd, connectString);
296 
297  std::string query = "CREATE TABLE DB2INST1.SOCI_TEST (ID BIGINT,DATA VARCHAR(8),DT TIMESTAMP)";
298  sql << query;
299 
300  {
301  int i = 7;
302  std::string n("test");
303  sql << "insert into db2inst1.SOCI_TEST (id,data) values (:id,:name)", use(i,"id"),use(n,"name");
304  int j;
305  std::string m;
306  sql << "select id,data from db2inst1.SOCI_TEST where id=7", into(j),into(m);
307  assert (j == i);
308  assert (m == n);
309  }
310 
311  {
312  int i = 8;
313  sql << "insert into db2inst1.SOCI_TEST (id) values (:id)", use(i,"id");
314  int j;
315  std::string m;
316  indicator ind = i_ok;
317  sql << "select id,data from db2inst1.SOCI_TEST where id=8", into(j),into(m,ind);
318  assert(j == i);
319  assert(ind==i_null);
320  }
321 
322  {
323  std::tm dt;
324  sql << "select current timestamp from sysibm.sysdummy1",into(dt);
325  sql << "insert into db2inst1.SOCI_TEST (dt) values (:dt)",use(dt,"dt");
326  std::tm dt2;
327  sql << "select dt from db2inst1.SOCI_TEST where dt is not null", into(dt2);
328  assert(dt2.tm_year == dt.tm_year && dt2.tm_mon == dt.tm_mon && dt2.tm_mday == dt.tm_mday &&
329  dt2.tm_hour == dt.tm_hour && dt2.tm_min == dt.tm_min && dt2.tm_sec == dt.tm_sec);
330  }
331 
332  sql<<"DROP TABLE DB2INST1.SOCI_TEST";
333  sql.commit();
334  }
335 
336  std::cout << "test 2 passed" << std::endl;
337 }
338 
339 void test3() {
340  {
341  session sql(backEnd, connectString);
342  int i;
343 
344  std::string query = "CREATE TABLE DB2INST1.SOCI_TEST (ID BIGINT,DATA VARCHAR(8),DT TIMESTAMP)";
345  sql << query;
346 
347  std::vector<long long> ids(100);
348  std::vector<std::string> data(100);
349  std::vector<std::tm> dts(100);
350  for (int i = 0; i < 100; i++)
351  {
352  ids[i] = 1000000000LL + i;
353  data[i] = "test";
354  dts[i].tm_year = 112;
355  dts[i].tm_mon = 7;
356  dts[i].tm_mday = 17;
357  dts[i].tm_hour = 0;
358  dts[i].tm_min = 0;
359  dts[i].tm_sec = i % 60;
360  }
361 
362  sql << "insert into db2inst1.SOCI_TEST (id, data, dt) values (:id, :data, :dt)",
363  use(ids, "id"), use(data,"data"), use(dts, "dt");
364 
365  i = 0;
366  rowset<row> rs = (sql.prepare<<"SELECT ID, DATA, DT FROM DB2INST1.SOCI_TEST");
367  for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); it++)
368  {
369  const row & r = *it;
370  const long long id = r.get<long long>(0);
371  const std::string data = r.get<std::string>(1);
372  const std::tm dt = r.get<std::tm>(2);
373 
374  assert(id == 1000000000LL + i);
375  assert(data == "test");
376  assert(dt.tm_year == 112);
377  assert(dt.tm_mon == 7);
378  assert(dt.tm_mday == 17);
379  assert(dt.tm_hour == 0);
380  assert(dt.tm_min == 0);
381  assert(dt.tm_sec == i % 60);
382 
383  i += 1;
384  }
385 
386  sql<<"DROP TABLE DB2INST1.SOCI_TEST";
387  sql.commit();
388  }
389 
390  std::cout << "test 3 passed" << std::endl;
391 }
392 
393 
394 int main(int argc, char** argv)
395 {
396 
397 #ifdef _MSC_VER
398  // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
399  // instead of debug message window.
400  // This hack is required to run asser()-driven tests by Buildbot.
401  // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
402  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
403  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
404 #endif //_MSC_VER
405 
406  if (argc == 2)
407  {
408  connectString = argv[1];
409  }
410  else
411  {
412  std::cout << "usage: " << argv[0]
413  << " connectstring\n"
414  << "example: " << argv[0]
415  << " \'DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;autocommit=off\'\n";
416  std::exit(1);
417  }
418 
419  test_context tc(backEnd, connectString);
420  common_tests tests(tc);
421  tests.run();
422 
423  try
424  {
425  std::cout<<"\nSOCI DB2 Tests:\n\n";
426 
427  session sql(backEnd, connectString);
428 
429  try
430  {
431  // attempt to delete the test table from previous runs
432  sql << "DROP TABLE DB2INST1.SOCI_TEST";
433  }
434  catch (soci_error const & e)
435  {
436  // if the table didn't exist, then proceed
437  }
438 
439  test1();
440  test2();
441  test3();
442  // ...
443 
444  std::cout << "\nOK, all tests passed.\n\n";
445 
446  return EXIT_SUCCESS;
447  }
448  catch (std::exception const & e)
449  {
450  std::cout << e.what() << '\n';
451  }
452 
453  return EXIT_FAILURE;
454 }
table_creator_base * table_creator_1(session &pr_s) const
Definition: test-db2.cpp:72
const_iterator begin() const
Definition: rowset.h:220
T get(std::size_t pos) const
Definition: row.h:66
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
backend_factory const & backEnd
Definition: test-db2.cpp:22
table_creator_base * table_creator_2(session &pr_s) const
Definition: test-db2.cpp:78
void run(bool dbSupportsTransactions=true)
Definition: common-tests.h:293
Definition: row.h:41
int main(int argc, char **argv)
Definition: test-db2.cpp:394
table_creator_one(session &sql)
Definition: test-db2.cpp:31
std::string to_date_time(std::string const &pi_datdt_string) const
Definition: test-db2.cpp:95
table_creator_base * table_creator_4(session &s) const
Definition: test-db2.cpp:90
const_iterator end() const
Definition: rowset.h:227
void test3()
Definition: test-db2.cpp:339
test_context(backend_factory const &pi_back_end, std::string const &pi_connect_string)
Definition: test-db2.cpp:69
void test2()
Definition: test-db2.cpp:293
table_creator_for_get_affected_rows(session &sql)
Definition: test-db2.cpp:59
table_creator_base * table_creator_3(session &pr_s) const
Definition: test-db2.cpp:84
std::string connectString
Definition: test-db2.cpp:21
SOCI_DB2_DECL backend_factory const * factory_db2()
Definition: db2/factory.cpp:30
table_creator_two(session &sql)
Definition: test-db2.cpp:41
void test1()
Definition: test-db2.cpp:106
details::prepare_type prepare
Definition: session.h:69
table_creator_three(session &sql)
Definition: test-db2.cpp:50
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