-
Notifications
You must be signed in to change notification settings - Fork 472
/
test-db2.cpp
209 lines (171 loc) · 6.37 KB
/
test-db2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "soci.h"
<<<<<<< HEAD
#include "soci-db2.h"
=======
#include "soci-empty.h"
>>>>>>> Ignored ide files
#include <iostream>
#include <string>
#include <cassert>
#include <cstdlib>
#include <ctime>
using namespace soci;
std::string connectString;
<<<<<<< HEAD
backend_factory const &backEnd = *soci::factory_db2();
=======
backend_factory const &backEnd = *soci::factory_empty();
>>>>>>> Ignored ide files
// NOTE:
// This file is supposed to serve two purposes:
// 1. To be a starting point for implementing new tests (for new backends).
// 2. To exercise (at least some of) the syntax and try the SOCI library
// against different compilers, even in those environments where there
// is no database. SOCI uses advanced template techniques which are known
// to cause problems on different versions of popular compilers, and this
// test is handy to verify that the code is accepted by as many compilers
// as possible.
//
// Both of these purposes mean that the actual code here is meaningless
// from the database-development point of view. For new tests, you may wish
// to remove this code and keep only the general structure of this file.
void test1()
{
{
session sql(backEnd, connectString);
sql << "SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1";
sql << "SELECT " << 123 << " FROM SYSIBM.SYSDUMMY1";
std::string query = "CREATE TABLE DB2INST1.TEST (ID BIGINT,DATA VARCHAR(8))";
sql << query;
int i = 7;
sql << "insert into db2inst1.TEST (id) values (:id)", use(i,"id");
sql << "select id from db2inst1.TEST where id=7", into(i);
#if defined (__LP64__) || ( __WORDSIZE == 64 )
long int li = 9;
sql << "insert into db2inst1.TEST (id) values (:id)", use(li,"id");
sql << "select id from db2inst1.TEST where id=9", into(li);
#endif
long long ll = 11;
sql << "insert into db2inst1.TEST (id) values (:id)", use(ll,"id");
sql << "select id from db2inst1.TEST where id=11", into(ll);
indicator ind = i_ok;
sql << "insert into db2inst1.TEST (id) values (:id)", use(i,ind,"id");
sql << "select id from db2inst1.TEST where id=7", into(i,ind);
std::vector<int> numbers(100);
sql << "insert into db2inst1.TEST (id) values (:id)", use(numbers,"id");
sql << "select id from db2inst1.TEST", into(numbers);
std::vector<indicator> inds(100);
sql << "insert into db2inst1.TEST (id) values (:id)", use(numbers,inds,"id");
sql << "select id from db2inst1.TEST", into(numbers,inds);
{
statement st = (sql.prepare << "select id from db2inst1.TEST", into(i));
st.execute();
st.fetch();
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", into(i, ind));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", into(numbers));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", into(numbers, inds));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", use(i));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", use(i, ind));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", use(numbers));
}
{
statement st = (sql.prepare << "select id from db2inst1.TEST", use(numbers, inds));
}
sql<<"DROP TABLE DB2INST1.TEST";
sql.commit();
}
std::cout << "test 1 passed" << std::endl;
}
void test2() {
{
session sql(backEnd, connectString);
std::string query = "CREATE TABLE DB2INST1.TEST (ID BIGINT,DATA VARCHAR(8),DT TIMESTAMP)";
sql << query;
int i = 7;
std::string n("test");
sql << "insert into db2inst1.TEST (id,data) values (:id,:name)", use(i,"id"),use(n,"name");
sql << "select id,data from db2inst1.TEST where id=7", into(i),into(n);
i = 8;
indicator ind = i_ok;
sql << "insert into db2inst1.TEST (id) values (:id)", use(i,"id");
sql << "select id,data from db2inst1.TEST where id=8", into(i),into(n,ind);
assert(ind==i_null);
std::tm dt;
sql << "select current timestamp from sysibm.sysdummy1",into(dt);
sql << "insert into db2inst1.TEST (dt) values (:dt)",use(dt,"dt");
sql<<"DROP TABLE DB2INST1.TEST";
sql.commit();
}
std::cout << "test 2 passed" << std::endl;
}
void test3() {
{
session sql(backEnd, connectString);
std::string query = "CREATE TABLE DB2INST1.TEST (ID BIGINT,DATA VARCHAR(8),DT TIMESTAMP)";
sql << query;
std::vector<std::string> strings(100);
for(std::vector<std::string>::iterator it=strings.begin();it!=strings.end();it++) {
*it="test";
}
sql << "insert into db2inst1.TEST (data) values (:data)", use(strings,"data");
rowset<std::string> rs = (sql.prepare<<"SELECT data from db2inst1.TEST");
sql<<"DROP TABLE DB2INST1.TEST";
sql.commit();
}
std::cout << "test 3 passed" << std::endl;
}
int main(int argc, char** argv)
{
#ifdef _MSC_VER
// Redirect errors, unrecoverable problems, and assert() failures to STDERR,
// instead of debug message window.
// This hack is required to run asser()-driven tests by Buildbot.
// NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
{
connectString = argv[1];
}
else
{
std::cout << "usage: " << argv[0]
<< " connectstring\n"
<< "example: " << argv[0]
<< " \'DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;autocommit=off\'\n";
std::exit(1);
}
try
{
test1();
test2();
test3();
// ...
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
}