test-sqlite3.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-sqlite3.h"
10 #include "common-tests.h"
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 #include <cassert>
15 #include <cmath>
16 #include <cstring>
17 #include <ctime>
18 
19 using namespace soci;
20 using namespace soci::tests;
21 
22 std::string connectString;
24 
25 // ROWID test
26 // In sqlite3 the row id can be called ROWID, _ROWID_ or oid
27 void test1()
28 {
29  {
30  session sql(backEnd, connectString);
31 
32  try { sql << "drop table test1"; }
33  catch (soci_error const &) {} // ignore if error
34 
35  sql <<
36  "create table test1 ("
37  " id integer,"
38  " name varchar(100)"
39  ")";
40 
41  sql << "insert into test1(id, name) values(7, \'John\')";
42 
43  rowid rid(sql);
44  sql << "select oid from test1 where id = 7", into(rid);
45 
46  int id;
47  std::string name;
48 
49  sql << "select id, name from test1 where oid = :rid",
50  into(id), into(name), use(rid);
51 
52  assert(id == 7);
53  assert(name == "John");
54 
55  sql << "drop table test1";
56  }
57 
58  std::cout << "test 1 passed" << std::endl;
59 }
60 
61 // BLOB test
63 {
65  : table_creator_base(sql)
66  {
67  sql <<
68  "create table soci_test ("
69  " id integer,"
70  " img blob"
71  ")";
72  }
73 };
74 
75 void test2()
76 {
77  {
78  session sql(backEnd, connectString);
79 
80  blob_table_creator tableCreator(sql);
81 
82  char buf[] = "abcdefghijklmnopqrstuvwxyz";
83 
84  sql << "insert into soci_test(id, img) values(7, '')";
85 
86  {
87  blob b(sql);
88 
89  sql << "select img from soci_test where id = 7", into(b);
90  assert(b.get_len() == 0);
91 
92  b.write(0, buf, sizeof(buf));
93  assert(b.get_len() == sizeof(buf));
94  sql << "update soci_test set img=? where id = 7", use(b);
95 
96  b.append(buf, sizeof(buf));
97  assert(b.get_len() == 2 * sizeof(buf));
98  sql << "insert into soci_test(id, img) values(8, ?)", use(b);
99  }
100  {
101  blob b(sql);
102  sql << "select img from soci_test where id = 8", into(b);
103  assert(b.get_len() == 2 * sizeof(buf));
104  char buf2[100];
105  b.read(0, buf2, 10);
106  assert(std::strncmp(buf2, "abcdefghij", 10) == 0);
107 
108  sql << "select img from soci_test where id = 7", into(b);
109  assert(b.get_len() == sizeof(buf));
110 
111  }
112  }
113 
114  std::cout << "test 2 passed" << std::endl;
115 }
116 
117 // This test was put in to fix a problem that occurs when there are both
118 // into and use elements in the same query and one of them (into) binds
119 // to a vector object.
120 
122 {
124  {
125  sql << "create table soci_test( id integer, name varchar, subname varchar);";
126  }
127 };
128 
129 void test3()
130 {
131  {
132  session sql(backEnd, connectString);
133 
134  test3_table_creator tableCreator(sql);
135 
136  sql << "insert into soci_test(id,name,subname) values( 1,'john','smith')";
137  sql << "insert into soci_test(id,name,subname) values( 2,'george','vals')";
138  sql << "insert into soci_test(id,name,subname) values( 3,'ann','smith')";
139  sql << "insert into soci_test(id,name,subname) values( 4,'john','grey')";
140  sql << "insert into soci_test(id,name,subname) values( 5,'anthony','wall')";
141 
142  {
143  std::vector<int> v(10);
144 
145  statement s(sql.prepare << "Select id from soci_test where name = :name");
146 
147  std::string name = "john";
148 
149  s.exchange(use(name, "name"));
150  s.exchange(into(v));
151 
152  s.define_and_bind();
153  s.execute(true);
154 
155  assert(v.size() == 2);
156  }
157  }
158  std::cout << "test 3 passed" << std::endl;
159 }
160 
161 
162 // Test case from Amnon David 11/1/2007
163 // I've noticed that table schemas in SQLite3 can sometimes have typeless
164 // columns. One (and only?) example is the sqlite_sequence that sqlite
165 // creates for autoincrement . Attempting to traverse this table caused
166 // SOCI to crash. I've made the following code change in statement.cpp to
167 // create a workaround:
168 
170 {
172  {
173  sql << "create table soci_test (col INTEGER PRIMARY KEY AUTOINCREMENT, name char)";
174  }
175 };
176 
177 void test4()
178 {
179  {
180  // we need to have an table that uses autoincrement to test this.
181  session sql(backEnd, connectString);
182 
183  test4_table_creator tableCreator(sql);
184 
185  sql << "insert into soci_test(name) values('john')";
186  sql << "insert into soci_test(name) values('james')";
187 
188  {
189  int key;
190  std::string name;
191  sql << "select * from soci_test", into(key), into(name);
192  assert(name == "john");
193 
194  rowset<row> rs = (sql.prepare << "select * from sqlite_sequence");
196  row const& r1 = (*it);
197  assert(r1.get<std::string>(0) == "soci_test");
198  assert(r1.get<std::string>(1) == "2");
199  }
200  }
201  std::cout << "test 4 passed" << std::endl;
202 }
203 
205 {
207  : table_creator_base(sql)
208  {
209  sql << "create table soci_test(val number(20))";
210  }
211 };
212 
213 // long long test
214 void test5()
215 {
216  {
217  session sql(backEnd, connectString);
218 
219  longlong_table_creator tableCreator(sql);
220 
221  long long v1 = 1000000000000LL;
222  assert(v1 / 1000000 == 1000000);
223 
224  sql << "insert into soci_test(val) values(:val)", use(v1);
225 
226  long long v2 = 0LL;
227  sql << "select val from soci_test", into(v2);
228 
229  assert(v2 == v1);
230  }
231 
232  // vector<long long>
233  {
234  session sql(backEnd, connectString);
235 
236  longlong_table_creator tableCreator(sql);
237 
238  std::vector<long long> v1;
239  v1.push_back(1000000000000LL);
240  v1.push_back(1000000000001LL);
241  v1.push_back(1000000000002LL);
242  v1.push_back(1000000000003LL);
243  v1.push_back(1000000000004LL);
244 
245  sql << "insert into soci_test(val) values(:val)", use(v1);
246 
247  std::vector<long long> v2(10);
248  sql << "select val from soci_test order by val desc", into(v2);
249 
250  assert(v2.size() == 5);
251  assert(v2[0] == 1000000000004LL);
252  assert(v2[1] == 1000000000003LL);
253  assert(v2[2] == 1000000000002LL);
254  assert(v2[3] == 1000000000001LL);
255  assert(v2[4] == 1000000000000LL);
256  }
257 
258  std::cout << "test 5 passed" << std::endl;
259 }
260 
262 {
264  {
265  sql << "create table soci_test (id INTEGER PRIMARY KEY, name char)";
266  }
267 };
268 
269 void test6()
270 {
271  {
272  session sql(backEnd, connectString);
273 
274  test6_table_creator tableCreator(sql);
275 
276  sql << "insert into soci_test(id, name) values(1, 'john')";
277 
278  {
279  try
280  {
281  // using same primary key causes constraint violation error
282  sql << "insert into soci_test(id, name) values(1, 'jack')";
283  }
284  catch (sqlite3_soci_error const& e)
285  {
286  assert(SQLITE_CONSTRAINT == (0xFF & e.result()));
287  }
288  }
289  }
290  std::cout << "test 6 passed" << std::endl;
291 }
292 
293 // DDL Creation objects for common tests
295 {
297  : table_creator_base(sql)
298  {
299  sql << "create table soci_test(id integer, val integer, c char, "
300  "str varchar(20), sh smallint, ul numeric(20), d float, "
301  "tm datetime, i1 integer, i2 integer, i3 integer, "
302  "name varchar(20))";
303  }
304 };
305 
307 {
309  : table_creator_base(sql)
310  {
311  sql << "create table soci_test(num_float float, num_int integer,"
312  " name varchar(20), sometime datetime, chr char)";
313  }
314 };
315 
317 {
319  : table_creator_base(sql)
320  {
321  sql << "create table soci_test(name varchar(100) not null, "
322  "phone varchar(15))";
323  }
324 };
325 
326 // Originally, submitted to SQLite3 backend and later moved to common test.
327 // Test commit b394d039530f124802d06c3b1a969c3117683152
328 // Author: Mika Fischer <mika.fischer@zoopnet.de>
329 // Date: Thu Nov 17 13:28:07 2011 +0100
330 // Implement get_affected_rows for SQLite3 backend
332 {
334  : table_creator_base(sql)
335  {
336  sql << "create table soci_test(val integer)";
337  }
338 };
339 
340 //
341 // Support for SOCI Common Tests
342 //
343 
344 class test_context : public test_context_base
345 {
346 public:
348  std::string const &connectString)
349  : test_context_base(backEnd, connectString) {}
350 
352  {
353  return new table_creator_one(s);
354  }
355 
357  {
358  return new table_creator_two(s);
359  }
360 
362  {
363  return new table_creator_three(s);
364  }
365 
367  {
369  }
370 
371  std::string to_date_time(std::string const &datdt_string) const
372  {
373  return "datetime(\'" + datdt_string + "\')";
374  }
375 };
376 
377 int main(int argc, char** argv)
378 {
379 
380 #ifdef _MSC_VER
381  // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
382  // instead of debug message window.
383  // This hack is required to run asser()-driven tests by Buildbot.
384  // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
385  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
386  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
387 #endif //_MSC_VER
388 
389  if (argc == 2)
390  {
391  connectString = argv[1];
392  }
393  else
394  {
395  // If no file name is specfied then work in-memory
396  connectString = ":memory:";
397  }
398 
399  try
400  {
401  test_context tc(backEnd, connectString);
402  common_tests tests(tc);
403  tests.run();
404 
405  std::cout << "\nSOCI sqlite3 Tests:\n\n";
406 
407  test1();
408  test2();
409  test3();
410  test4();
411  test5();
412  test6();
413 
414  std::cout << "\nOK, all tests passed.\n\n";
415 
416  return EXIT_SUCCESS;
417  }
418  catch (soci::soci_error const & e)
419  {
420  std::cout << "SOCIERROR: " << e.what() << '\n';
421  }
422  catch (std::exception const & e)
423  {
424  std::cout << "EXCEPTION: " << e.what() << '\n';
425  }
426 
427  return EXIT_FAILURE;
428 }
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
void test6()
test_context(backend_factory const &backEnd, std::string const &connectString)
test6_table_creator(session &sql)
void test3()
table_creator_base * table_creator_2(session &s) const
void run(bool dbSupportsTransactions=true)
Definition: common-tests.h:293
Definition: row.h:41
std::size_t read(std::size_t offset, char *buf, std::size_t toRead)
Definition: core/blob.cpp:31
backend_factory const & backEnd
int main(int argc, char **argv)
table_creator_one(session &sql)
table_creator_base * table_creator_4(session &s) const
std::string connectString
blob_table_creator(session &sql)
void test1()
table_creator_base * table_creator_1(session &s) const
test4_table_creator(session &sql)
longlong_table_creator(session &sql)
std::size_t append(char const *buf, std::size_t toWrite)
Definition: core/blob.cpp:42
table_creator_base * table_creator_3(session &s) const
void test2()
void define_and_bind()
Definition: statement.h:206
void test5()
std::string to_date_time(std::string const &datdt_string) const
std::size_t get_len()
Definition: core/blob.cpp:26
table_creator_two(session &sql)
details::prepare_type prepare
Definition: session.h:69
void exchange(details::into_type_ptr const &i)
Definition: statement.h:192
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
test3_table_creator(session &sql)
SOCI_SQLITE3_DECL backend_factory const * factory_sqlite3()
bool execute(bool withDataExchange=false)
Definition: statement.h:208
std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite)
Definition: core/blob.cpp:36
void test4()


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