-
Notifications
You must be signed in to change notification settings - Fork 472
/
test-postgresql.cpp
716 lines (564 loc) · 16.9 KB
/
test-postgresql.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//
// Copyright (C) 2004-2008 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"
#include "soci-postgresql.h"
#include "common-tests.h"
#include <iostream>
#include <sstream>
#include <string>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace soci;
using namespace soci::tests;
std::string connectString;
backend_factory const &backEnd = *soci::factory_postgresql();
// Postgres-specific tests
struct oid_table_creator : public table_creator_base
{
oid_table_creator(session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test ("
" id integer,"
" name varchar(100)"
") with oids";
}
};
// ROWID test
// Note: in PostgreSQL, there is no ROWID, there is OID.
// It is still provided as a separate type for "portability",
// whatever that means.
void test1()
{
{
session sql(backEnd, connectString);
oid_table_creator tableCreator(sql);
sql << "insert into soci_test(id, name) values(7, \'John\')";
rowid rid(sql);
sql << "select oid from soci_test where id = 7", into(rid);
int id;
std::string name;
#ifndef SOCI_POSTGRESQL_NOPARAMS
sql << "select id, name from soci_test where oid = :rid",
into(id), into(name), use(rid);
#else
// Older PostgreSQL does not support use elements.
postgresql_rowid_backend *rbe
= static_cast<postgresql_rowid_backend *>(rid.get_backend());
unsigned long oid = rbe->value_;
sql << "select id, name from soci_test where oid = " << oid,
into(id), into(name);
#endif // SOCI_POSTGRESQL_NOPARAMS
assert(id == 7);
assert(name == "John");
}
std::cout << "test 1 passed" << std::endl;
}
// function call test
class function_creator : function_creator_base
{
public:
function_creator(session & sql)
: function_creator_base(sql)
{
// before a language can be used it must be defined
// if it has already been defined then an error will occur
try { sql << "create language plpgsql"; }
catch (soci_error const &) {} // ignore if error
#ifndef SOCI_POSTGRESQL_NOPARAMS
sql <<
"create or replace function soci_test(msg varchar) "
"returns varchar as $$ "
"begin "
" return msg; "
"end $$ language plpgsql";
#else
sql <<
"create or replace function soci_test(varchar) "
"returns varchar as \' "
"begin "
" return $1; "
"end \' language plpgsql";
#endif
}
protected:
std::string drop_statement()
{
return "drop function soci_test(varchar)";
}
};
void test2()
{
{
session sql(backEnd, connectString);
function_creator functionCreator(sql);
std::string in("my message");
std::string out;
#ifndef SOCI_POSTGRESQL_NOPARAMS
statement st = (sql.prepare <<
"select soci_test(:input)",
into(out),
use(in, "input"));
#else
// Older PostgreSQL does not support use elements.
statement st = (sql.prepare <<
"select soci_test(\'" << in << "\')",
into(out));
#endif // SOCI_POSTGRESQL_NOPARAMS
st.execute(true);
assert(out == in);
// explicit procedure syntax
{
std::string in("my message2");
std::string out;
#ifndef SOCI_POSTGRESQL_NOPARAMS
procedure proc = (sql.prepare <<
"soci_test(:input)",
into(out), use(in, "input"));
#else
// Older PostgreSQL does not support use elements.
procedure proc = (sql.prepare <<
"soci_test(\'" << in << "\')", into(out));
#endif // SOCI_POSTGRESQL_NOPARAMS
proc.execute(true);
assert(out == in);
}
}
std::cout << "test 2 passed" << std::endl;
}
// BLOB test
struct blob_table_creator : public table_creator_base
{
blob_table_creator(session & sql)
: table_creator_base(sql)
{
sql <<
"create table soci_test ("
" id integer,"
" img oid"
")";
}
};
void test3()
{
{
session sql(backEnd, connectString);
blob_table_creator tableCreator(sql);
char buf[] = "abcdefghijklmnopqrstuvwxyz";
sql << "insert into soci_test(id, img) values(7, lo_creat(-1))";
// in PostgreSQL, BLOB operations must be within transaction block
transaction tr(sql);
{
blob b(sql);
sql << "select img from soci_test where id = 7", into(b);
assert(b.get_len() == 0);
b.write(0, buf, sizeof(buf));
assert(b.get_len() == sizeof(buf));
b.append(buf, sizeof(buf));
assert(b.get_len() == 2 * sizeof(buf));
}
{
blob b(sql);
sql << "select img from soci_test where id = 7", into(b);
assert(b.get_len() == 2 * sizeof(buf));
char buf2[100];
b.read(0, buf2, 10);
assert(std::strncmp(buf2, "abcdefghij", 10) == 0);
}
unsigned long oid;
sql << "select img from soci_test where id = 7", into(oid);
sql << "select lo_unlink(" << oid << ")";
}
std::cout << "test 3 passed" << std::endl;
}
struct longlong_table_creator : table_creator_base
{
longlong_table_creator(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val int8)";
}
};
// long long test
void test4()
{
{
session sql(backEnd, connectString);
longlong_table_creator tableCreator(sql);
long long v1 = 1000000000000LL;
assert(v1 / 1000000 == 1000000);
sql << "insert into soci_test(val) values(:val)", use(v1);
long long v2 = 0LL;
sql << "select val from soci_test", into(v2);
assert(v2 == v1);
}
// vector<long long>
{
session sql(backEnd, connectString);
longlong_table_creator tableCreator(sql);
std::vector<long long> v1;
v1.push_back(1000000000000LL);
v1.push_back(1000000000001LL);
v1.push_back(1000000000002LL);
v1.push_back(1000000000003LL);
v1.push_back(1000000000004LL);
sql << "insert into soci_test(val) values(:val)", use(v1);
std::vector<long long> v2(10);
sql << "select val from soci_test order by val desc", into(v2);
assert(v2.size() == 5);
assert(v2[0] == 1000000000004LL);
assert(v2[1] == 1000000000003LL);
assert(v2[2] == 1000000000002LL);
assert(v2[3] == 1000000000001LL);
assert(v2[4] == 1000000000000LL);
}
std::cout << "test 4 passed" << std::endl;
}
// unsigned long long test
void test4ul()
{
{
session sql(backEnd, connectString);
longlong_table_creator tableCreator(sql);
unsigned long long v1 = 1000000000000ULL;
assert(v1 / 1000000 == 1000000);
sql << "insert into soci_test(val) values(:val)", use(v1);
unsigned long long v2 = 0ULL;
sql << "select val from soci_test", into(v2);
assert(v2 == v1);
}
}
struct boolean_table_creator : table_creator_base
{
boolean_table_creator(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val boolean)";
}
};
void test5()
{
{
session sql(backEnd, connectString);
boolean_table_creator tableCreator(sql);
int i1 = 0;
sql << "insert into soci_test(val) values(:val)", use(i1);
int i2 = 7;
sql << "select val from soci_test", into(i2);
assert(i2 == i1);
sql << "update soci_test set val = true";
sql << "select val from soci_test", into(i2);
assert(i2 == 1);
}
std::cout << "test 5 passed" << std::endl;
}
// dynamic backend test
void test6()
{
try
{
session sql("nosuchbackend://" + connectString);
assert(false);
}
catch (soci_error const & e)
{
assert(e.what() == std::string("Failed to open: libsoci_nosuchbackend.so"));
}
{
dynamic_backends::register_backend("pgsql", backEnd);
std::vector<std::string> backends = dynamic_backends::list_all();
assert(backends.size() == 1);
assert(backends[0] == "pgsql");
{
session sql("pgsql://" + connectString);
}
dynamic_backends::unload("pgsql");
backends = dynamic_backends::list_all();
assert(backends.empty());
}
{
session sql("postgresql://" + connectString);
}
std::cout << "test 6 passed" << std::endl;
}
void test7()
{
{
session sql(backEnd, connectString);
int i;
sql << "select 123", into(i);
assert(i == 123);
try
{
sql << "select 'ABC'", into (i);
assert(false);
}
catch (soci_error const & e)
{
assert(e.what() == std::string("Cannot convert data."));
}
}
std::cout << "test 7 passed" << std::endl;
}
void test8()
{
{
session sql(backEnd, connectString);
assert(sql.get_backend_name() == "postgresql");
}
std::cout << "test 8 passed" << std::endl;
}
// test for double-colon cast in SQL expressions
void test9()
{
{
session sql(backEnd, connectString);
int a = 123;
int b = 0;
sql << "select :a::integer", use(a), into(b);
assert(b == a);
}
std::cout << "test 9 passed" << std::endl;
}
// test for date, time and timestamp parsing
void test10()
{
{
session sql(backEnd, connectString);
std::string someDate = "2009-06-17 22:51:03.123";
std::tm t1, t2, t3;
sql << "select :sd::date, :sd::time, :sd::timestamp",
use(someDate, "sd"), into(t1), into(t2), into(t3);
// t1 should contain only the date part
assert(t1.tm_year == 2009 - 1900);
assert(t1.tm_mon == 6 - 1);
assert(t1.tm_mday == 17);
assert(t1.tm_hour == 0);
assert(t1.tm_min == 0);
assert(t1.tm_sec == 0);
// t2 should contain only the time of day part
assert(t2.tm_year == 0);
assert(t2.tm_mon == 0);
assert(t2.tm_mday == 1);
assert(t2.tm_hour == 22);
assert(t2.tm_min == 51);
assert(t2.tm_sec == 3);
// t3 should contain all information
assert(t3.tm_year == 2009 - 1900);
assert(t3.tm_mon == 6 - 1);
assert(t3.tm_mday == 17);
assert(t3.tm_hour == 22);
assert(t3.tm_min == 51);
assert(t3.tm_sec == 3);
}
std::cout << "test 10 passed" << std::endl;
}
// test for number of affected rows
struct table_creator_for_test11 : table_creator_base
{
table_creator_for_test11(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val integer)";
}
};
void test11()
{
{
session sql(backEnd, connectString);
table_creator_for_test11 tableCreator(sql);
for (int i = 0; i != 10; i++)
{
sql << "insert into soci_test(val) values(:val)", use(i);
}
statement st1 = (sql.prepare <<
"update soci_test set val = val + 1");
st1.execute(false);
assert(st1.get_affected_rows() == 10);
statement st2 = (sql.prepare <<
"delete from soci_test where val <= 5");
st2.execute(false);
assert(st2.get_affected_rows() == 5);
}
std::cout << "test 11 passed" << std::endl;
}
// test INSERT INTO ... RETURNING syntax
struct table_creator_for_test12 : table_creator_base
{
table_creator_for_test12(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(sid serial, txt text)";
}
};
void test12()
{
{
session sql(backEnd, connectString);
table_creator_for_test12 tableCreator(sql);
std::vector<long> ids(10);
for (std::size_t i = 0; i != ids.size(); i++)
{
long sid(0);
std::string txt("abc");
sql << "insert into soci_test(txt) values(:txt) returning sid", use(txt, "txt"), into(sid);
ids[i] = sid;
}
std::vector<long> ids2(ids.size());
sql << "select sid from soci_test order by sid", into(ids2);
assert(std::equal(ids.begin(), ids.end(), ids2.begin()));
}
std::cout << "test 12 passed" << std::endl;
}
struct bytea_table_creator : public table_creator_base
{
bytea_table_creator(session& sql)
: table_creator_base(sql)
{
sql << "drop table if exists soci_test;";
sql << "create table soci_test ( val bytea null )";
}
};
void test_bytea()
{
{
session sql(backEnd, connectString);
bytea_table_creator tableCreator(sql);
int v = 0x0A0B0C0D;
unsigned char* b = reinterpret_cast<unsigned char*>(&v);
std::string data;
std::copy(b, b + sizeof(v), std::back_inserter(data));
{
sql << "insert into soci_test(val) values(:val)", use(data);
// 1) into string, no Oid mapping
std::string bin1;
sql << "select val from soci_test", into(bin1);
assert(bin1 == "\\x0d0c0b0a");
// 2) Oid-to-dt_string mapped
row r;
sql << "select * from soci_test", into(r);
assert(r.size() == 1);
column_properties const& props = r.get_properties(0);
assert(props.get_data_type() == soci::dt_string);
std::string bin2 = r.get<std::string>(0);
assert(bin2 == "\\x0d0c0b0a");
}
}
std::cout << "test_bytea passed" << std::endl;
}
// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
table_creator_one(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, val integer, c char, "
"str varchar(20), sh int2, ul numeric(20), d float8, "
"tm timestamp, i1 integer, i2 integer, i3 integer, "
"name varchar(20))";
}
};
struct table_creator_two : public table_creator_base
{
table_creator_two(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(num_float float8, num_int integer,"
" name varchar(20), sometime timestamp, chr char)";
}
};
struct table_creator_three : public table_creator_base
{
table_creator_three(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(name varchar(100) not null, "
"phone varchar(15))";
}
};
//
// Support for soci Common Tests
//
class test_context : public test_context_base
{
public:
test_context(backend_factory const &backEnd, std::string const &connectString)
: test_context_base(backEnd, connectString)
{}
table_creator_base* table_creator_1(session& s) const
{
return new table_creator_one(s);
}
table_creator_base* table_creator_2(session& s) const
{
return new table_creator_two(s);
}
table_creator_base* table_creator_3(session& s) const
{
return new table_creator_three(s);
}
std::string to_date_time(std::string const &datdt_string) const
{
return "timestamptz(\'" + datdt_string + "\')";
}
};
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]
<< " \'connect_string_for_PostgreSQL\'\n";
return EXIT_FAILURE;
}
try
{
test_bytea();
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
std::cout << "\nSOCI PostgreSQL Tests:\n\n";
test1();
test2();
test3();
test4();
test4ul();
test5();
// test6();
std::cout << "test 6 skipped (dynamic backend)\n";
test7();
test8();
test9();
test10();
test11();
test12();
test_bytea();
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
}