Debian Codegraph @

test/test_backend.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]>
4 //
5 // Distributed under:
6 //
7 // the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // or (at your opinion) under:
12 //
13 // The MIT License
14 // (See accompanying file MIT.txt or a copy at
15 // http://www.opensource.org/licenses/mit-license.php)
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 #include <cppdb/backend.h>
19 #include <cppdb/driver_manager.h>
20 #include <sstream>
21 #include <iostream>
22 #include <stdexcept>
23 #include <memory>
24 #include <stdlib.h>
25
26 #include "test.h"
27
28
29 bool test_64bit_integer = true;
30 bool test_blob = true;
31 bool wide_api = false;
32 bool pq_oid = false;
33
34 int sizes[]={ 0, // General 0 length string
35 61,62,63,64,65,66,67, // mysql buffer size
36 510,511,512,513,514, // ODBC buffer size in wchars
37 1020,1021,1022,1023,1024,1025,1026,1027, // ODBC buffer size
38 4094,4095,4096,4097,4098// postgresql driver buffer size for LO
39 };
40
41
42
43 /*
44 void test_template(cppdb::ref_ptr<cppdb::backend::connection> sql)
45 {
46 cppdb::ref_ptr<cppdb::backend::statement> stmt;
47 cppdb::ref_ptr<cppdb::backend::result> res;
48 }
49 */
50
51 void test1(cppdb::ref_ptr<cppdb::backend::connection> sql)
52 {
53 cppdb::ref_ptr<cppdb::backend::statement> stmt;
54 cppdb::ref_ptr<cppdb::backend::result> res;
55 try {
56 stmt = sql->prepare("drop table test");
57 stmt->exec();
58 }catch(...) {}
59
60 if(sql->engine()=="mssql" && wide_api) {
61 stmt = sql->prepare("create table test ( x integer not null, y nvarchar(1000) )");
62 }
63 else
64 stmt = sql->prepare("create table test ( x integer not null, y varchar(1000) )");
65
66 std::cout << "Basic select " << std::endl;
67
68 stmt->exec();
69 stmt = sql->prepare("select * from test");
70 res = stmt->query();
71 TEST(!res->next());
72 stmt = sql->prepare("insert into test(x,y) values(10,'foo?')");
73 stmt->exec();
74 stmt = sql->prepare("select x,y from test");
75
76 res = stmt->query();
77 TEST(res->next());
78 TEST(res->cols()==2);
79 int iv;
80 std::string sv;
81 TEST(res->fetch(0,iv));
82 TEST(iv==10);
83 TEST(res->fetch(1,sv));
84 TEST(sv=="foo?");
85 TEST(!res->next());
86 res.reset();
87 stmt = sql->prepare("insert into test(x,y) values(20,NULL)");
88 stmt->exec();
89 stmt = sql->prepare("select y from test where x=?");
90 stmt->bind(1,20);
91 res = stmt->query();
92 TEST(res->next());
93 TEST(res->is_null(0));
94 sv="xxx";
95 TEST(!res->fetch(0,sv));
96 TEST(sv=="xxx");
97 TEST(!res->next());
98 res.reset();
99 stmt->reset();
100 stmt->bind(1,10);
101 res = stmt->query();
102 TEST(res->next());
103 sv="";
104 TEST(!res->is_null(0));
105 TEST(res->fetch(0,sv));
106 TEST(sv=="foo?");
107 stmt = sql->prepare("DELETE FROM test");
108 stmt->exec();
109 std::cout << "Unicode Test" << std::endl;
110 if(sql->engine()!="mssql" || wide_api) {
111 std::string test_string = "Pease שלום Мир ﺱﻼﻣ";
112 stmt = sql->prepare("insert into test(x,y) values(?,?)");
113 stmt->bind(1,15);
114 stmt->bind(2,test_string);
115 stmt->exec();
116 stmt = sql->prepare("select x,y from test");
117 res = stmt->query();
118 TEST(res->next());
119 sv="";
120 res->fetch(1,sv);
121 TEST(sv==test_string);
122 }
123 else {
124 std::cout << "This does not support unicode, skipping" << std::endl;
125 }
126 }
127
128 void test2(cppdb::ref_ptr<cppdb::backend::connection> sql)
129 {
130 cppdb::ref_ptr<cppdb::backend::statement> stmt;
131 cppdb::ref_ptr<cppdb::backend::result> res;
132
133 try {
134 stmt = sql->prepare("drop table test");
135 stmt->exec();
136 stmt.reset();
137 }catch(...) {}
138 stmt = 0;
139
140 std::cout << "Testing sequences types" << std::endl;
141 if(sql->engine() == "sqlite3") {
142 stmt = sql->prepare("create table test ( id integer primary key autoincrement not null, n integer)");
143 }
144 else if(sql->engine() == "mysql") {
145 stmt = sql->prepare("create table test ( id integer primary key auto_increment not null, n integer)");
146 }
147 else if(sql->engine() == "postgresql" ) {
148 stmt = sql->prepare("create table test ( id serial primary key not null, n integer)");
149 }
150 else if(sql->engine() == "mssql" ) {
151 stmt = sql->prepare("create table test ( id integer identity(1,1) primary key not null,n integer)");
152 }
153
154 if(stmt) {
155 stmt->exec();
156 stmt = sql->prepare("insert into test(n) values(?)");
157 stmt->bind(1,10);
158 stmt->exec();
159 TEST(stmt->sequence_last("test_id_seq") == 1);
160 stmt->reset();
161 stmt->bind(1,20);
162 stmt->exec();
163 TEST(stmt->sequence_last("test_id_seq") == 2);
164 stmt = sql->prepare("drop table test");
165 stmt->exec();
166 stmt.reset();
167 }
168 else {
169 std::cout << "This engine " << sql->engine() << " does not support sequences, skipping" << std::endl;
170 }
171 }
172
173
174 void test3(cppdb::ref_ptr<cppdb::backend::connection> sql)
175 {
176 cppdb::ref_ptr<cppdb::backend::statement> stmt;
177 cppdb::ref_ptr<cppdb::backend::result> res;
178
179 std::cout << "Testing data types" << std::endl;
180 try {
181 stmt = sql->prepare("drop table test");
182 stmt->exec();
183 stmt.reset();
184 }catch(...) {}
185
186 if(sql->engine() == "mssql")
187 stmt = sql->prepare("create table test ( i bigint, r real, t datetime, s varchar(5000), bl varbinary(max))");
188 else if(sql->engine() == "mysql")
189 stmt = sql->prepare("create table test ( i bigint, r real, t datetime default null, s varchar(5000), bl blob) Engine = innodb");
190 else if(sql->engine() == "postgresql") {
191 if(pq_oid) {
192 stmt = sql->prepare("create table test ( i bigint, r real, t timestamp, s varchar(5000), bl oid)");
193 stmt->exec();
194 stmt = sql->prepare( "CREATE TRIGGER t_test BEFORE UPDATE OR DELETE ON test "
195 "FOR EACH ROW EXECUTE PROCEDURE lo_manage(bl)");
196 }
197 else {
198 stmt = sql->prepare("create table test ( i bigint, r real, t timestamp, s varchar(5000), bl bytea)");
199 }
200 }
201 else if(sql->engine() == "sqlite3") {
202 stmt = sql->prepare("create table test ( i integer, r real, t timestamp, s varchar(5000), bl blob)");
203 }
204 else {
205 test_64bit_integer = false;
206 test_blob= false;
207 stmt = sql->prepare("create table test ( i integer, r real, t timestamp, s varchar(5000))");
208 }
209 stmt->exec();
210 if(sql->engine()=="mssql")
211 stmt = sql->prepare("insert into test values(?,?,?,?,cast(? as varbinary(max)))");
212 else if(test_blob)
213 stmt = sql->prepare("insert into test values(?,?,?,?,?)");
214 else
215 stmt = sql->prepare("insert into test values(?,?,?,?)");
216 stmt->bind_null(1);
217 stmt->bind_null(2);
218 stmt->bind_null(3);
219 stmt->bind_null(4);
220 if(test_blob)
221 stmt->bind_null(5);
222 stmt->exec();
223 TEST(stmt->affected()==1);
224 if(pq_oid) {
225 sql->begin();
226 }
227 stmt->reset();
228 stmt->bind(1,10);
229 stmt->bind(2,3.14);
230 std::time_t now=time(0);
231 std::tm t=*std::localtime(&now);
232 stmt->bind(3,t);
233 stmt->bind(4,"'to be' \\'or not' to be");
234 std::istringstream iss;
235 iss.str(std::string("\xFF\0\xFE\1\2",5));
236 if(test_blob)
237 stmt->bind(5,iss);
238 stmt->exec();
239 if(pq_oid) {
240 sql->commit();
241 sql->begin();
242 }
243 if(test_blob)
244 stmt = sql->prepare("select i,r,t,s,bl from test");
245 else
246 stmt = sql->prepare("select i,r,t,s from test");
247 res = stmt->query();
248 {
249 TEST(res->cols()==test_blob ? 5 : 4);
250 TEST(res->column_to_name(0)=="i");
251 TEST(res->column_to_name(1)=="r");
252 TEST(res->column_to_name(2)=="t");
253 TEST(res->column_to_name(3)=="s");
254 if(test_blob)
255 TEST(res->column_to_name(4)=="bl");
256
257 TEST(res->name_to_column("i")==0);
258 TEST(res->name_to_column("r")==1);
259 TEST(res->name_to_column("t")==2);
260 TEST(res->name_to_column("s")==3);
261 if(test_blob)
262 TEST(res->name_to_column("bl")==4);
263 TEST(res->name_to_column("x")==-1);
264
265 TEST(res->next());
266
267 std::ostringstream oss;
268 int i=-1; double r=-1; std::tm t=std::tm(); std::string s="def";
269 TEST(res->is_null(0));
270 TEST(res->is_null(1));
271 TEST(res->is_null(2));
272 TEST(res->is_null(3));
273 if(test_blob)
274 TEST(res->is_null(4));
275 TEST(!res->fetch(0,i));
276 TEST(!res->fetch(1,r));
277 TEST(!res->fetch(2,t));
278 TEST(!res->fetch(3,s));
279 if(test_blob)
280 TEST(!res->fetch(4,oss));
281 TEST(i==-1);
282 TEST(r==-1);
283 TEST(t.tm_year == 0);
284 TEST(s=="def");
285 TEST(oss.str()=="");
286 TEST(res->has_next() == cppdb::backend::result::next_row_unknown || res->has_next() == cppdb::backend::result::next_row_exists);
287 TEST(res->next());
288 TEST(res->fetch(0,i));
289 TEST(res->fetch(1,r));
290 TEST(res->fetch(2,t));
291 TEST(res->fetch(3,s));
292 if(test_blob)
293 TEST(res->fetch(4,oss));
294 TEST(!res->is_null(0));
295 TEST(!res->is_null(1));
296 TEST(!res->is_null(2));
297 TEST(!res->is_null(3));
298 if(test_blob)
299 TEST(!res->is_null(4));
300 TEST(i==10);
301 TEST(3.1399 <= r && r <= 3.1401);
302 TEST(mktime(&t)==now);
303 TEST(s=="'to be' \\'or not' to be");
304 if(test_blob)
305 TEST(oss.str() == std::string("\xFF\0\xFE\1\2",5));
306 TEST(res->has_next() == cppdb::backend::result::next_row_unknown || res->has_next() == cppdb::backend::result::last_row_reached);
307 TEST(!res->next());
308 }
309 if(pq_oid) {
310 sql->commit();
311 }
312 stmt = sql->prepare("DELETE FROM test where 1<>0");
313 stmt->exec();
314 TEST(stmt->affected()==2);
315 }
316
317 void test4(cppdb::ref_ptr<cppdb::backend::connection> sql)
318 {
319 cppdb::ref_ptr<cppdb::backend::statement> stmt;
320 cppdb::ref_ptr<cppdb::backend::result> res;
321
322 std::cout << "Tesing transactions" << std::endl;
323 stmt = sql->prepare("DELETE FROM test where 1<>0");
324 stmt->exec();
325 sql->begin();
326 stmt = sql->prepare("insert into test(i) values(10)");
327 stmt->exec();
328 stmt = sql->prepare("insert into test(i) values(20)");
329 stmt->exec();
330 sql->commit();
331 stmt = sql->prepare("select count(*) from test");
332 res = stmt->query();
333 TEST(res->next());
334 int iv;
335 TEST(res->fetch(0,iv) && iv==2);
336 res.reset();
337 stmt.reset();
338 iv=-1;
339 stmt = sql->prepare("DELETE FROM test where 1<>0");
340 stmt->exec();
341 sql->begin();
342 stmt = sql->prepare("insert into test(i) values(10)");
343 stmt->exec();
344 stmt = sql->prepare("insert into test(i) values(20)");
345 stmt->exec();
346 sql->rollback();
347 stmt = sql->prepare("select count(*) from test");
348 res = stmt->query();
349 TEST(res->next());
350 iv=-1;
351 TEST(res->fetch(0,iv));
352 TEST(iv==0);
353 stmt = sql->prepare("DELETE FROM test where 1<>0");
354 stmt->exec();
355 }
356
357 void test5(cppdb::ref_ptr<cppdb::backend::connection> sql)
358 {
359 cppdb::ref_ptr<cppdb::backend::statement> stmt;
360 cppdb::ref_ptr<cppdb::backend::result> res;
361
362 std::cout << "Tesing variable length data handing: text" << std::endl;
363 sql->begin();
364 stmt = sql->prepare("insert into test(i,s) values(?,?)");
365 for(unsigned i=0;i<sizeof(sizes)/sizeof(int);i++) {
366 int size = sizes[i];
367 std::string value;
368 value.reserve(size);
369 srand(i);
370 for(int j=0;j<size;j++) {
371 value+=char(rand() % 26 + 'a');
372 }
373 stmt->bind(1,size);
374 stmt->bind(2,value);
375 stmt->exec();
376 stmt->reset();
377 }
378 sql->commit();
379 stmt = sql->prepare("select s from test where i=?");
380 for(unsigned i=0;i<sizeof(sizes)/sizeof(int);i++) {
381 int size = sizes[i];
382 std::string value;
383 value.reserve(size);
384 srand(i);
385 for(int j=0;j<size;j++) {
386 value+=char(rand() % 26 + 'a');
387 }
388 stmt->bind(1,size);
389 res = stmt->query();
390 TEST(res->next());
391 std::string v;
392 TEST(res->fetch(0,v));
393 TEST(v==value);
394 res.reset();
395 stmt->reset();
396 }
397 stmt = sql->prepare("DELETE FROM test where 1<>0");
398 stmt->exec();
399 }
400
401 void test6(cppdb::ref_ptr<cppdb::backend::connection> sql)
402 {
403 cppdb::ref_ptr<cppdb::backend::statement> stmt;
404 cppdb::ref_ptr<cppdb::backend::result> res;
405
406 if(test_blob) {
407 std::cout << "Tesing variable length data handing: blob" << std::endl;
408
409 sql->begin();
410 if(sql->engine()=="mssql")
411 stmt = sql->prepare("insert into test(i,bl) values(?,cast(? as varbinary(max)))");
412 else
413 stmt = sql->prepare("insert into test(i,bl) values(?,?)");
414 for(unsigned i=0;i<sizeof(sizes)/sizeof(int);i++) {
415 int size = sizes[i];
416 std::stringstream value;
417 srand(i);
418 for(int j=0;j<size;j++) {
419 value << char(rand() % 26 + 'a');
420 }
421 stmt->bind(1,size);
422 stmt->bind(2,value);
423 stmt->exec();
424 stmt->reset();
425 }
426 sql->commit();
427
428 stmt = sql->prepare("select bl from test where i=?");
429 for(unsigned i=0;i<sizeof(sizes)/sizeof(int);i++) {
430 int size = sizes[i];
431 std::string value;
432 value.reserve(size);
433 srand(i);
434 for(int j=0;j<size;j++) {
435 value+=char(rand() % 26 + 'a');
436 }
437 if(pq_oid)
438 sql->begin();
439 stmt->bind(1,size);
440 res = stmt->query();
441 TEST(res->next());
442 std::ostringstream v;
443 TEST(res->fetch(0,v));
444 TEST(v.str()==value);
445 res.reset();
446 stmt->reset();
447 if(pq_oid)
448 sql->commit();
449 }
450 }
451 stmt = sql->prepare("DELETE FROM test where 1<>0");
452 stmt->exec();
453
454 }
455
456
457 void test7(cppdb::ref_ptr<cppdb::backend::connection> sql)
458 {
459 cppdb::ref_ptr<cppdb::backend::statement> stmt;
460 cppdb::ref_ptr<cppdb::backend::result> res;
461 std::cout << "Testing integer ranges" << std::endl;
462
463 {
464 stmt=sql->prepare("delete from test");
465 stmt->exec();
466 std::cout << "- short" << std::endl;
467 stmt=sql->prepare("insert into test(i) values(32768)");
468 stmt->exec();
469 stmt=sql->prepare("select i from test");
470 res = stmt->query();
471 res->next();
472 short s=0;
473 THROWS(res->fetch(0,s),cppdb::bad_value_cast);
474 unsigned short us=0;
475 TEST(res->fetch(0,us));
476 TEST(us==32768);
477 }
478 if(test_64bit_integer) {
479 stmt=sql->prepare("delete from test");
480 stmt->exec();
481 std::cout << "- int" << std::endl;
482 stmt=sql->prepare("insert into test(i) values(2147483648)");
483 stmt->exec();
484 stmt=sql->prepare("select i from test");
485 res = stmt->query();
486 res->next();
487 int i=0;
488 THROWS(res->fetch(0,i),cppdb::bad_value_cast);
489 unsigned ui=0;
490 TEST(res->fetch(0,ui));
491 TEST(ui==2147483648U);
492 if(sizeof(long)==8) {
493 long l=0;
494 TEST(res->fetch(0,l));
495 TEST(l==2147483648L);
496 }
497 long long ll=0;
498 TEST(res->fetch(0,ll));
499 TEST(ll==2147483648LL);
500 }
501 if(test_64bit_integer && sizeof(long)==4) {
502 stmt=sql->prepare("delete from test");
503 stmt->exec();
504 std::cout << "- long" << std::endl;
505 stmt=sql->prepare("insert into test(i) values(2147483648)");
506 stmt->exec();
507 stmt=sql->prepare("select i from test");
508 res = stmt->query();
509 res->next();
510 long i=0;
511 THROWS(res->fetch(0,i),cppdb::bad_value_cast);
512 unsigned long ul=0;
513 TEST(res->fetch(0,ul));
514 TEST(ul==2147483648UL);
515 long long ll=0;
516 TEST(res->fetch(0,ll));
517 TEST(ll==2147483648LL);
518 }
519 {
520 stmt=sql->prepare("delete from test");
521 stmt->exec();
522 std::cout << "- unsigned short,int,long,long long" << std::endl;
523 stmt=sql->prepare("insert into test(i) values(-1)");
524 stmt->exec();
525 stmt=sql->prepare("select i from test");
526 res = stmt->query();
527 res->next();
528 unsigned short us=0;
529 short s=0;
530 unsigned ui=0;
531 int i=0;
532 unsigned long ul=0;
533 long l=0;
534 unsigned long long ull=0;
535 long long ll=0;
536 THROWS(res->fetch(0,us),cppdb::bad_value_cast);
537 THROWS(res->fetch(0,ui),cppdb::bad_value_cast);
538 THROWS(res->fetch(0,ul),cppdb::bad_value_cast);
539 THROWS(res->fetch(0,ull),cppdb::bad_value_cast);
540 TEST(res->fetch(0,s) && s==-1);
541 TEST(res->fetch(0,i) && i==-1);
542 TEST(res->fetch(0,l) && l==-1);
543 TEST(res->fetch(0,ll) && ll==-1);
544 }
545 std::cout <<"Testing floating -> integer conversions" << std::endl;
546 {
547 stmt=sql->prepare("delete from test");
548 stmt->exec();
549 stmt=sql->prepare("insert into test(r) values(33000.11)");
550 stmt->exec();
551 stmt=sql->prepare("select r from test");
552 res = stmt->query();
553 res->next();
554 int i=0;
555 short s=0;
556 TEST(res->fetch(0,i) && (i==33000 || i==33001));
557 THROWS(res->fetch(0,s),cppdb::bad_value_cast);
558 res.reset();
559 stmt=sql->prepare("delete from test");
560 stmt->exec();
561 stmt=sql->prepare("insert into test(r) values(-1e12)");
562 stmt->exec();
563 long long ll = 0;
564 unsigned long long ull;
565 stmt=sql->prepare("select r from test");
566 res = stmt->query();
567 res->next();
568 THROWS(res->fetch(0,i),cppdb::bad_value_cast);
569 TEST(res->fetch(0,ll) && ll==-1000000000000LL);
570 THROWS(res->fetch(0,ull),cppdb::bad_value_cast);
571 }
572 stmt=sql->prepare("delete from test");
573 stmt->exec();
574 }
575
576 void test8(cppdb::ref_ptr<cppdb::backend::connection> sql)
577 {
578 cppdb::ref_ptr<cppdb::backend::statement> stmt;
579 cppdb::ref_ptr<cppdb::backend::result> res;
580 std::cout << "Testing conversions on fetch" << std::endl;
581
582 {
583 stmt=sql->prepare("delete from test");
584 stmt->exec();
585 std::cout << "- string to integer" << std::endl;
586 stmt=sql->prepare("insert into test(s) values('10')");
587 stmt->exec();
588 stmt=sql->prepare("select s from test");
589 res = stmt->query();
590 res->next();
591 int i=0;
592 TEST(res->fetch(0,i));
593 TEST(i==10);
594 }
595 {
596 stmt=sql->prepare("delete from test");
597 stmt->exec();
598 std::cout << "- string to date-time" << std::endl;
599 stmt=sql->prepare("insert into test(s) values('2008-02-03 11:22:33')");
600 stmt->exec();
601 stmt=sql->prepare("select s from test");
602 res = stmt->query();
603 res->next();
604 std::tm t=std::tm();;
605 TEST(res->fetch(0,t));
606 TEST(t.tm_year == 2008 - 1900);
607 TEST(t.tm_mon == 2 - 1);
608 TEST(t.tm_mday == 3);
609 TEST(t.tm_hour == 11);
610 TEST(t.tm_min == 22);
611 TEST(t.tm_sec == 33);
612 }
613 {
614 stmt=sql->prepare("delete from test");
615 stmt->exec();
616 std::cout << "- integer to string" << std::endl;
617 stmt=sql->prepare("insert into test(i) values(10)");
618 stmt->exec();
619 stmt=sql->prepare("select i from test");
620 res = stmt->query();
621 res->next();
622 int i=0;
623 TEST(res->fetch(0,i));
624 TEST(i==10);
625 }
626
627 }
628
629 void test9(cppdb::ref_ptr<cppdb::backend::connection> sql)
630 {
631 cppdb::ref_ptr<cppdb::backend::statement> stmt;
632 cppdb::ref_ptr<cppdb::backend::result> res;
633 std::cout << "Testing conversions on bind" << std::endl;
634
635 {
636 stmt=sql->prepare("delete from test");
637 stmt->exec();
638 std::cout << "- integer to string" << std::endl;
639 stmt=sql->prepare("insert into test(s) values(?)");
640 stmt->bind(1,10);
641 stmt->exec();
642 stmt=sql->prepare("select s from test");
643 res = stmt->query();
644 res->next();
645 std::string s;
646 TEST(res->fetch(0,s));
647 TEST(s=="10");
648 }
649 if(sql->engine()!="mssql") {
650 stmt=sql->prepare("delete from test");
651 stmt->exec();
652 std::tm t=std::tm();;
653 t.tm_year = 2008 - 1900;
654 t.tm_mon = 2 - 1;
655 t.tm_mday = 3;
656 t.tm_hour = 11;
657 t.tm_min = 22;
658 t.tm_sec = 33;
659 mktime(&t);
660
661 std::cout << "- date-time to string" << std::endl;
662 stmt=sql->prepare("insert into test(s) values(?)");
663 stmt->bind(1,t);
664 stmt->exec();
665 stmt=sql->prepare("select s from test");
666 res = stmt->query();
667 res->next();
668 std::string s;
669 TEST(res->fetch(0,s));
670 TEST(s=="2008-02-03 11:22:33");
671 }
672 if(sql->engine()!="mssql"){
673 stmt=sql->prepare("delete from test");
674 stmt->exec();
675 std::cout << "- string to date-time" << std::endl;
676 stmt=sql->prepare("insert into test(t) values(?)");
677 stmt->bind(1,"2008-02-03 11:22:33");
678 stmt->exec();
679 stmt=sql->prepare("select t from test");
680 res = stmt->query();
681 res->next();
682 std::tm t=std::tm();;
683 TEST(res->fetch(0,t));
684 TEST(t.tm_year == 2008 - 1900);
685 TEST(t.tm_mon == 2 - 1);
686 TEST(t.tm_mday == 3);
687 TEST(t.tm_hour == 11);
688 TEST(t.tm_min == 22);
689 TEST(t.tm_sec == 33);
690 }
691
692 }
693
694
695 void run_test(void (*func)(cppdb::ref_ptr<cppdb::backend::connection>),cppdb::ref_ptr<cppdb::backend::connection> sql)
696 {
697 try {
698 func(sql);
699 }
700 catch(cppdb::cppdb_error const &e) {
701 std::cerr << "Catched exception " << e.what() << std::endl;
702 std::cerr << "Last tested line " << last_line << std::endl;
703 failed++;
704 return;
705 }
706 passed++;
707 }
708
709 void test(std::string conn_str)
710 {
711 cppdb::ref_ptr<cppdb::backend::connection> sql(cppdb::driver_manager::instance().connect(conn_str));
712 cppdb::ref_ptr<cppdb::backend::statement> stmt;
713 cppdb::ref_ptr<cppdb::backend::result> res;
714 wide_api = (sql->driver()=="odbc" && conn_str.find("utf=wide")!=std::string::npos);
715
716 std::cout << "Basic setup" << std::endl;
717
718 if(sql->engine() == "postgresql") {
719 if(sql->driver() == "odbc")
720 pq_oid = true;
721 else if(sql->driver()=="postgresql" && conn_str.find("@blob=bytea") == std::string::npos)
722 pq_oid = true;
723 }
724
725 run_test(test1,sql);
726 run_test(test2,sql);
727 run_test(test3,sql);
728 run_test(test4,sql);
729 run_test(test5,sql);
730 run_test(test6,sql);
731 run_test(test7,sql);
732 run_test(test8,sql);
733 run_test(test9,sql);
734 }
735
736
737
738 int main(int argc,char **argv)
739 {
740 if(argc!=2) {
741 std::cerr << "Usage: test_backend connection_string" << std::endl;
742 return 1;
743 }
744 std::string cs = argv[1];
745 try {
746 test(cs);
747 }
748 CATCH_BLOCK();
749 SUMMARY();
750 }