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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
|
//
// 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
// https://www.boost.org/LICENSE_1_0.txt)
//
#include "soci/soci.h"
#include "soci/postgresql/soci-postgresql.h"
#include "test-context.h"
#include "test-myint.h"
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <catch.hpp>
using namespace soci;
using namespace soci::tests;
std::string connectString;
backend_factory const &backEnd = *soci::factory_postgresql();
// Postgres-specific tests
enum TestStringEnum
{
VALUE_STR_1=0,
VALUE_STR_2,
VALUE_STR_3
};
enum TestIntEnum
{
VALUE_INT_1=0,
VALUE_INT_2,
VALUE_INT_3
};
namespace soci {
template <> struct type_conversion<TestStringEnum>
{
typedef std::string base_type;
static void from_base(const std::string & v, indicator & ind, TestStringEnum & p)
{
if ( ind == i_null )
throw soci_error("Null value not allowed for this type");
if ( v.compare("A") == 0 )
p = TestStringEnum::VALUE_STR_1;
else if ( v.compare("B") == 0 )
p = TestStringEnum::VALUE_STR_2;
else if ( v.compare("C") == 0 )
p = TestStringEnum::VALUE_STR_3;
else
throw soci_error("Value not allowed for this type");
}
static void to_base(TestStringEnum & p, std::string & v, indicator & ind)
{
switch ( p )
{
case TestStringEnum::VALUE_STR_1:
v = "A";
ind = i_ok;
return;
case TestStringEnum::VALUE_STR_2:
v = "B";
ind = i_ok;
return;
case TestStringEnum::VALUE_STR_3:
v = "C";
ind = i_ok;
return;
default:
throw soci_error("Value not allowed for this type");
}
}
};
template <> struct type_conversion<TestIntEnum>
{
typedef int base_type;
static void from_base(const int & v, indicator & ind, TestIntEnum & p)
{
if ( ind == i_null )
throw soci_error("Null value not allowed for this type");
switch( v )
{
case 0:
case 1:
case 2:
p = (TestIntEnum)v;
ind = i_ok;
return;
default:
throw soci_error("Null value not allowed for this type");
}
}
static void to_base(TestIntEnum & p, int & v, indicator & ind)
{
switch( p )
{
case TestIntEnum::VALUE_INT_1:
case TestIntEnum::VALUE_INT_2:
case TestIntEnum::VALUE_INT_3:
v = (int)p;
ind = i_ok;
return;
default:
throw soci_error("Value not allowed for this type");
}
}
};
}
struct oid_table_creator : public table_creator_base
{
oid_table_creator(soci::session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test ("
" id integer,"
" name varchar(100)"
") with oids";
}
};
TEST_CASE("PostgreSQL connection string", "[postgresql][connstring]")
{
// There are no required parts in libpq connection string, so we can only
// test that invalid options are detected.
CHECK_THROWS_WITH(soci::session(backEnd, "bloordyblop=1"),
Catch::Contains(R"(invalid connection option "bloordyblop")"));
CHECK_THROWS_WITH(soci::session(backEnd, "sslmode=bloordyblop"),
Catch::Contains(R"(invalid sslmode value: "bloordyblop")"));
// This tests that quoted strings work as expected
CHECK_THROWS_WITH(soci::session(backEnd, "sslmode='dummy value'"),
Catch::Contains(R"(invalid sslmode value: "dummy value")"));
CHECK_THROWS_WITH(soci::session(backEnd, "sslmode=\"dummy value\""),
Catch::Contains(R"(invalid sslmode value: "dummy value")"));
}
// 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.
TEST_CASE("PostgreSQL ROWID", "[postgresql][rowid][oid]")
{
soci::session sql(backEnd, connectString);
int server_version_num;
sql << "show server_version_num", into(server_version_num);
if ( server_version_num >= 120000 )
{
WARN("Skipping test because OIDs are no longer supported in PostgreSQL "
<< server_version_num);
return;
}
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;
sql << "select id, name from soci_test where oid = :rid",
into(id), into(name), use(rid);
CHECK(id == 7);
CHECK(name == "John");
}
TEST_CASE("PostgreSQL prepare error", "[postgresql][exception]")
{
soci::session sql(backEnd, connectString);
// Must not cause the application to crash.
statement st(sql);
st.prepare(""); // Throws an exception in some versions.
}
// function call test
class function_creator
{
public:
explicit function_creator(soci::session & sql)
: sql_(sql)
{
drop();
// 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
sql <<
"create or replace function soci_test(msg varchar) "
"returns varchar as $$ "
"declare x int := 1;"
"begin "
" return msg; "
"end $$ language plpgsql";
}
~function_creator() { drop(); }
private:
void drop()
{
try { sql_ << "drop function soci_test(varchar)"; } catch (soci_error&) {}
}
session& sql_;
SOCI_NOT_COPYABLE(function_creator)
};
TEST_CASE("PostgreSQL function call", "[postgresql][function]")
{
soci::session sql(backEnd, connectString);
function_creator functionCreator(sql);
std::string in("my message");
std::string out;
statement st = (sql.prepare <<
"select soci_test(:input)",
into(out),
use(in, "input"));
st.execute(true);
CHECK(out == in);
// explicit procedure syntax
{
procedure proc = (sql.prepare <<
"soci_test(:input)",
into(out), use(in, "input"));
proc.execute(true);
CHECK(out == in);
}
}
struct longlong_table_creator : table_creator_base
{
longlong_table_creator(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val int8)";
}
};
// long long test
TEST_CASE("PostgreSQL long long", "[postgresql][longlong]")
{
soci::session sql(backEnd, connectString);
longlong_table_creator tableCreator(sql);
long long v1 = 1000000000000LL;
sql << "insert into soci_test(val) values(:val)", use(v1);
long long v2 = 0LL;
sql << "select val from soci_test", into(v2);
CHECK(v2 == v1);
}
// vector<long long>
TEST_CASE("PostgreSQL vector long long", "[postgresql][vector][longlong]")
{
soci::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);
REQUIRE(v2.size() == 5);
CHECK(v2[0] == 1000000000004LL);
CHECK(v2[1] == 1000000000003LL);
CHECK(v2[2] == 1000000000002LL);
CHECK(v2[3] == 1000000000001LL);
CHECK(v2[4] == 1000000000000LL);
}
// unsigned long long test
TEST_CASE("PostgreSQL unsigned long long", "[postgresql][unsigned][longlong]")
{
soci::session sql(backEnd, connectString);
longlong_table_creator tableCreator(sql);
unsigned long long v1 = 1000000000000ULL;
sql << "insert into soci_test(val) values(:val)", use(v1);
unsigned long long v2 = 0ULL;
sql << "select val from soci_test", into(v2);
CHECK(v2 == v1);
}
struct boolean_table_creator : table_creator_base
{
boolean_table_creator(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val boolean)";
}
};
TEST_CASE("PostgreSQL boolean", "[postgresql][boolean]")
{
soci::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;
row r;
sql << "select val from soci_test", into(i2);
sql << "select val from soci_test", into(r);
CHECK(i2 == i1);
CHECK(r.get<int8_t>(0) == i1);
sql << "update soci_test set val = true";
sql << "select val from soci_test", into(i2);
sql << "select val from soci_test", into(r);
CHECK(i2 == 1);
CHECK(r.get<int8_t>(0) == 1);
}
struct uuid_table_creator : table_creator_base
{
uuid_table_creator(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val uuid)";
}
};
// uuid test
TEST_CASE("PostgreSQL uuid", "[postgresql][uuid]")
{
soci::session sql(backEnd, connectString);
uuid_table_creator tableCreator(sql);
std::string v1("cd2dcb78-3817-442e-b12a-17c7e42669a0");
sql << "insert into soci_test(val) values(:val)", use(v1);
std::string v2;
sql << "select val from soci_test", into(v2);
CHECK(v2 == v1);
}
// dynamic backend test -- currently skipped by default
TEST_CASE("PostgreSQL dynamic backend", "[postgresql][backend][.]")
{
try
{
soci::session sql("nosuchbackend://" + connectString);
FAIL("expected exception not thrown");
}
catch (soci_error const & e)
{
CHECK(e.get_error_message() ==
"Failed to open: libsoci_nosuchbackend.so");
}
{
dynamic_backends::register_backend("pgsql", backEnd);
std::vector<std::string> backends = dynamic_backends::list_all();
REQUIRE(backends.size() == 1);
CHECK(backends[0] == "pgsql");
{
soci::session sql("pgsql://" + connectString);
}
dynamic_backends::unload("pgsql");
backends = dynamic_backends::list_all();
CHECK(backends.empty());
}
{
soci::session sql("postgresql://" + connectString);
}
}
TEST_CASE("PostgreSQL literals", "[postgresql][into]")
{
soci::session sql(backEnd, connectString);
int i;
sql << "select 123", into(i);
CHECK(i == 123);
try
{
sql << "select 'ABC'", into (i);
FAIL("expected exception not thrown");
}
catch (soci_error const & e)
{
CHECK_THAT( e.what(), Catch::StartsWith("Cannot convert data") );
}
}
TEST_CASE("PostgreSQL backend name", "[postgresql][backend]")
{
soci::session sql(backEnd, connectString);
CHECK(sql.get_backend_name() == "postgresql");
}
// test for double-colon cast in SQL expressions
TEST_CASE("PostgreSQL double colon cast", "[postgresql][cast]")
{
soci::session sql(backEnd, connectString);
int a = 123;
int b = 0;
sql << "select :a::integer", use(a), into(b);
CHECK(b == a);
}
// test for date, time and timestamp parsing
TEST_CASE("PostgreSQL datetime", "[postgresql][datetime]")
{
soci::session sql(backEnd, connectString);
std::string someDate = "2009-06-17 22:51:03.123";
std::tm t1 = std::tm(), t2 = std::tm(), t3 = std::tm();
sql << "select :sd::date, :sd::time, :sd::timestamp",
use(someDate, "sd"), into(t1), into(t2), into(t3);
// t1 should contain only the date part
CHECK(t1.tm_year == 2009 - 1900);
CHECK(t1.tm_mon == 6 - 1);
CHECK(t1.tm_mday == 17);
CHECK(t1.tm_hour == 0);
CHECK(t1.tm_min == 0);
CHECK(t1.tm_sec == 0);
// t2 should contain only the time of day part
CHECK(t2.tm_year == 0);
CHECK(t2.tm_mon == 0);
CHECK(t2.tm_mday == 1);
CHECK(t2.tm_hour == 22);
CHECK(t2.tm_min == 51);
CHECK(t2.tm_sec == 3);
// t3 should contain all information
CHECK(t3.tm_year == 2009 - 1900);
CHECK(t3.tm_mon == 6 - 1);
CHECK(t3.tm_mday == 17);
CHECK(t3.tm_hour == 22);
CHECK(t3.tm_min == 51);
CHECK(t3.tm_sec == 3);
}
// test for number of affected rows
struct table_creator_for_test11 : table_creator_base
{
table_creator_for_test11(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val integer)";
}
};
TEST_CASE("PostgreSQL get affected rows", "[postgresql][affected-rows]")
{
soci::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);
CHECK(st1.get_affected_rows() == 10);
statement st2 = (sql.prepare <<
"delete from soci_test where val <= 5");
st2.execute(false);
CHECK(st2.get_affected_rows() == 5);
}
// test INSERT INTO ... RETURNING syntax
struct table_creator_for_test12 : table_creator_base
{
table_creator_for_test12(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(sid serial, txt text)";
}
};
TEST_CASE("PostgreSQL insert into ... returning", "[postgresql]")
{
soci::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);
CHECK(std::equal(ids.begin(), ids.end(), ids2.begin()));
}
struct bytea_table_creator : public table_creator_base
{
bytea_table_creator(soci::session& sql)
: table_creator_base(sql)
{
sql << "drop table if exists soci_test;";
sql << "create table soci_test ( val bytea null )";
}
};
TEST_CASE("PostgreSQL bytea", "[postgresql][bytea]")
{
soci::session sql(backEnd, connectString);
// PostgreSQL supports two different output formats for bytea values:
// historical "escape" format, which is the only one supported until
// PostgreSQL 9.0, and "hex" format used by default since 9.0, we need
// to determine which one is actually in use.
std::string bytea_output_format;
sql << "select setting from pg_settings where name='bytea_output'",
into(bytea_output_format);
char const* expectedBytea;
if (bytea_output_format.empty() || bytea_output_format == "escape")
expectedBytea = "\\015\\014\\013\\012";
else if (bytea_output_format == "hex")
expectedBytea = "\\x0d0c0b0a";
else
throw std::runtime_error("Unknown PostgreSQL bytea_output \"" +
bytea_output_format + "\"");
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);
CHECK(bin1 == expectedBytea);
// 2) Oid-to-dt_string mapped
row r;
sql << "select * from soci_test", into(r);
REQUIRE(r.size() == 1);
column_properties const& props = r.get_properties(0);
CHECK(props.get_data_type() == soci::dt_string);
CHECK(props.get_db_type() == soci::db_string);
std::string bin2 = r.get<std::string>(0);
CHECK(bin2 == expectedBytea);
}
}
// json
struct table_creator_json : public table_creator_base
{
table_creator_json(soci::session& sql)
: table_creator_base(sql)
{
sql << "drop table if exists soci_json_test;";
sql << "create table soci_json_test(data json)";
}
};
// Return 9,2 for 9.2.3
typedef std::pair<int,int> server_version;
server_version get_postgresql_version(soci::session& sql)
{
std::string version;
std::pair<int,int> result;
sql << "select version()",into(version);
if (sscanf(version.c_str(),"PostgreSQL %i.%i", &result.first, &result.second) < 2)
{
throw std::runtime_error("Failed to retrieve PostgreSQL version number");
}
return result;
}
// Test JSON. Only valid for PostgreSQL Server 9.2++
TEST_CASE("PostgreSQL JSON", "[postgresql][json]")
{
soci::session sql(backEnd, connectString);
server_version version = get_postgresql_version(sql);
if ( version >= server_version(9,2))
{
std::string result;
std::string valid_input = "{\"tool\":\"soci\",\"result\":42}";
std::string invalid_input = "{\"tool\":\"other\",\"result\":invalid}";
table_creator_json tableCreator(sql);
sql << "insert into soci_json_test (data) values(:data)",use(valid_input);
sql << "select data from soci_json_test",into(result);
CHECK(result == valid_input);
CHECK_THROWS_AS((
sql << "insert into soci_json_test (data) values(:data)",use(invalid_input)),
soci_error
);
}
else
{
WARN("JSON test skipped (PostgreSQL >= 9.2 required, found " << version.first << "." << version.second << ")");
}
}
struct table_creator_text : public table_creator_base
{
table_creator_text(soci::session& sql) : table_creator_base(sql)
{
sql << "drop table if exists soci_test;";
sql << "create table soci_test(name varchar(20))";
}
};
// Test deallocate_prepared_statement called for non-existing statement
// which creation failed due to invalid SQL syntax.
// https://github.com/SOCI/soci/issues/116
TEST_CASE("PostgreSQL statement prepare failure", "[postgresql][prepare]")
{
soci::session sql(backEnd, connectString);
table_creator_text tableCreator(sql);
try
{
// types mismatch should lead to PQprepare failure
statement get_trades =
(sql.prepare
<< "select * from soci_test where name=9999");
FAIL("expected exception not thrown");
}
catch (postgresql_soci_error const& e)
{
CHECK( e.get_error_category() == soci_error::invalid_statement );
CHECK_THAT( e.what(), Catch::Contains("operator does not exist") );
}
}
// Test the support of PostgreSQL-style casts with ORM
TEST_CASE("PostgreSQL ORM cast", "[postgresql][orm]")
{
soci::session sql(backEnd, connectString);
values v;
v.set("a", 1);
sql << "select :a::int", use(v); // Must not throw an exception!
}
std::string get_table_name_without_schema(const std::string& table_name_with_schema)
{
// Find the first occurrence of "."
size_t dotPos = table_name_with_schema.find('.');
// Check if the "." exists and there's exactly one "."
if (dotPos == std::string::npos || table_name_with_schema.find('.', dotPos + 1) != std::string::npos) {
return table_name_with_schema;
}
// Extract the substring after the "."
return table_name_with_schema.substr(dotPos + 1);
}
std::string get_schema_from_table_name(const std::string& table_name_with_schema)
{
// Find the first occurrence of "."
size_t dotPos = table_name_with_schema.find('.');
// Check if the "." exists and there's exactly one "."
if (dotPos == std::string::npos || table_name_with_schema.find('.', dotPos + 1) != std::string::npos) {
return "";
}
// Extract the substring before the "."
return table_name_with_schema.substr(0, dotPos);
}
// Test the DDL and metadata functionality
TEST_CASE("PostgreSQL DDL with metadata", "[postgresql][ddl]")
{
soci::session sql(backEnd, connectString);
// note: prepare_column_descriptions expects l-value
std::string ddl_t1 = "ddl_t1";
std::string ddl_t2 = "ddl_t2";
std::string ddl_t3 = "ddl_t3";
// single-expression variant:
sql.create_table(ddl_t1).column("i", soci::dt_integer).column("j", soci::dt_integer);
// check whether this table was created:
bool ddl_t1_found = false;
bool ddl_t2_found = false;
bool ddl_t3_found = false;
std::string table_name;
soci::statement st = (sql.prepare_table_names(), into(table_name));
st.execute();
while (st.fetch())
{
if (get_table_name_without_schema(table_name) == ddl_t1) { ddl_t1_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t2) { ddl_t2_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t3) { ddl_t3_found = true; }
}
CHECK(ddl_t1_found);
CHECK(ddl_t2_found == false);
CHECK(ddl_t3_found == false);
// check whether ddl_t1 has the right structure:
bool i_found = false;
bool j_found = false;
bool other_found = false;
soci::column_info ci;
soci::statement st1 = (sql.prepare_column_descriptions(ddl_t1), into(ci));
st1.execute();
while (st1.fetch())
{
if (ci.name == "i")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
i_found = true;
}
else if (ci.name == "j")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
j_found = true;
}
else
{
other_found = true;
}
}
CHECK(i_found);
CHECK(j_found);
CHECK(other_found == false);
// two more tables:
// separately defined columns:
// (note: statement is executed when ddl object goes out of scope)
{
soci::ddl_type ddl = sql.create_table(ddl_t2);
ddl.column("i", soci::dt_integer);
ddl.column("j", soci::dt_integer);
ddl.column("k", soci::dt_integer)("not null");
ddl.primary_key("t2_pk", "j");
}
sql.add_column(ddl_t1, "k", soci::dt_integer);
sql.add_column(ddl_t1, "big", soci::dt_string, 0); // "unlimited" length -> text
sql.drop_column(ddl_t1, "i");
// or with constraint as in t2:
sql.add_column(ddl_t2, "m", soci::dt_integer)("not null");
// third table with a foreign key to the second one
{
soci::ddl_type ddl = sql.create_table(ddl_t3);
ddl.column("x", soci::dt_integer);
ddl.column("y", soci::dt_integer);
ddl.foreign_key("t3_fk", "x", ddl_t2, "j");
}
// check if all tables were created:
ddl_t1_found = false;
ddl_t2_found = false;
ddl_t3_found = false;
soci::statement st2 = (sql.prepare_table_names(), into(table_name));
st2.execute();
while (st2.fetch())
{
if (get_table_name_without_schema(table_name) == ddl_t1) { ddl_t1_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t2) { ddl_t2_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t3) { ddl_t3_found = true; }
}
CHECK(ddl_t1_found);
CHECK(ddl_t2_found);
CHECK(ddl_t3_found);
// check if ddl_t1 has the right structure (it was altered):
i_found = false;
j_found = false;
bool k_found = false;
bool big_found = false;
other_found = false;
soci::statement st3 = (sql.prepare_column_descriptions(ddl_t1), into(ci));
st3.execute();
while (st3.fetch())
{
if (ci.name == "j")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
j_found = true;
}
else if (ci.name == "k")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
k_found = true;
}
else if (ci.name == "big")
{
CHECK(ci.type == soci::dt_string);
CHECK(ci.dataType == soci::db_string);
CHECK(ci.precision == 0); // "unlimited" for strings
big_found = true;
}
else
{
other_found = true;
}
}
CHECK(i_found == false);
CHECK(j_found);
CHECK(k_found);
CHECK(big_found);
CHECK(other_found == false);
// check if ddl_t2 has the right structure:
i_found = false;
j_found = false;
k_found = false;
bool m_found = false;
other_found = false;
soci::statement st4 = (sql.prepare_column_descriptions(ddl_t2), into(ci));
st4.execute();
while (st4.fetch())
{
if (ci.name == "i")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
i_found = true;
}
else if (ci.name == "j")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable == false); // primary key
j_found = true;
}
else if (ci.name == "k")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable == false);
k_found = true;
}
else if (ci.name == "m")
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable == false);
m_found = true;
}
else
{
other_found = true;
}
}
CHECK(i_found);
CHECK(j_found);
CHECK(k_found);
CHECK(m_found);
CHECK(other_found == false);
sql.drop_table(ddl_t1);
sql.drop_table(ddl_t3); // note: this must be dropped before ddl_t2
sql.drop_table(ddl_t2);
// check if all tables were dropped:
ddl_t1_found = false;
ddl_t2_found = false;
ddl_t3_found = false;
st2 = (sql.prepare_table_names(), into(table_name));
st2.execute();
while (st2.fetch())
{
if (get_table_name_without_schema(table_name) == ddl_t1) { ddl_t1_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t2) { ddl_t2_found = true; }
if (get_table_name_without_schema(table_name) == ddl_t3) { ddl_t3_found = true; }
}
CHECK(ddl_t1_found == false);
CHECK(ddl_t2_found == false);
CHECK(ddl_t3_found == false);
int i = -1;
sql << "select lo_unlink(" + sql.empty_blob() + ")", into(i);
CHECK(i == 1);
sql << "select " + sql.nvl() + "(1, 2)", into(i);
CHECK(i == 1);
sql << "select " + sql.nvl() + "(NULL, 2)", into(i);
CHECK(i == 2);
}
// Test cross-schema metadata
TEST_CASE("Cross-schema metadata", "[postgresql][cross-schema]")
{
soci::session sql(backEnd, connectString);
// note: prepare_column_descriptions expects l-value
std::string tables = "tables";
std::string column_name = "table_name";
// single-expression variant:
sql.create_table(tables).column(column_name, soci::dt_integer);
// check whether this table was created:
bool tables_found = false;
std::string schema;
std::string table_name;
soci::statement st = (sql.prepare_table_names(), into(table_name));
st.execute();
while (st.fetch())
{
if (get_table_name_without_schema(table_name) == tables)
{
tables_found = true;
schema = get_schema_from_table_name(table_name);
}
}
CHECK(tables_found);
CHECK(!schema.empty());
// Get information for the tables table we just created and not
// the tables table in information_schema which isn't in our path.
int records = 0;
soci::column_info ci;
soci::statement st1 = (sql.prepare_column_descriptions(tables), into(ci));
st1.execute();
while (st1.fetch())
{
if (ci.name == column_name)
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
records++;
}
}
CHECK(records == 1);
// Run the same query but this time specific with the schema.
std::string schemaTables = schema + "." + tables;
records = 0;
soci::statement st2 = (sql.prepare_column_descriptions(schemaTables), into(ci));
st2.execute();
while (st2.fetch())
{
if (ci.name == column_name)
{
CHECK(ci.type == soci::dt_integer);
CHECK(ci.dataType == soci::db_int32);
CHECK(ci.nullable);
records++;
}
}
CHECK(records == 1);
// Finally run the query with the information_schema.
std::string information_schemaTables = "information_schema." + tables;
records = 0;
soci::statement st3 = (sql.prepare_column_descriptions(information_schemaTables), into(ci));
st3.execute();
while (st3.fetch())
{
if (ci.name == column_name)
{
CHECK(ci.type == soci::dt_string);
CHECK(ci.dataType == soci::db_string);
CHECK(ci.nullable);
records++;
}
}
CHECK(records == 1);
// Delete table and check that it is gone
sql.drop_table(tables);
tables_found = false;
st3 = (sql.prepare_table_names(), into(table_name));
st3.execute();
while (st.fetch())
{
if (table_name == tables)
{
tables_found = true;
}
}
CHECK(tables_found == false);
}
// Test the bulk iterators functionality
TEST_CASE("Bulk iterators", "[postgresql][bulkiters]")
{
soci::session sql(backEnd, connectString);
sql << "create table t (i integer)";
// test bulk iterators with basic types
{
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
std::size_t begin = 2;
std::size_t end = 5;
sql << "insert into t (i) values (:v)", soci::use(v, begin, end);
v.clear();
v.resize(20);
begin = 5;
end = 20;
sql << "select i from t", soci::into(v, begin, end);
CHECK(end == 8);
for (std::size_t i = 0; i != 5; ++i)
{
CHECK(v[i] == 0);
}
CHECK(v[5] == 30);
CHECK(v[6] == 40);
CHECK(v[7] == 50);
for (std::size_t i = end; i != 20; ++i)
{
CHECK(v[i] == 0);
}
}
sql << "delete from t";
// test bulk iterators with user types
{
std::vector<MyInt> v;
v.push_back(MyInt(10));
v.push_back(MyInt(20));
v.push_back(MyInt(30));
v.push_back(MyInt(40));
v.push_back(MyInt(50));
std::size_t begin = 2;
std::size_t end = 5;
sql << "insert into t (i) values (:v)", soci::use(v, begin, end);
v.clear();
for (std::size_t i = 0; i != 20; ++i)
{
v.push_back(MyInt(-1));
}
begin = 5;
end = 20;
sql << "select i from t", soci::into(v, begin, end);
CHECK(end == 8);
for (std::size_t i = 0; i != 5; ++i)
{
CHECK(v[i].get() == -1);
}
CHECK(v[5].get() == 30);
CHECK(v[6].get() == 40);
CHECK(v[7].get() == 50);
for (std::size_t i = end; i != 20; ++i)
{
CHECK(v[i].get() == -1);
}
}
sql << "drop table t";
}
// false_bind_variable_inside_identifier
struct test_false_bind_variable_inside_identifier_table_creator : table_creator_base
{
test_false_bind_variable_inside_identifier_table_creator(soci::session & sql)
: table_creator_base(sql)
, msession(sql)
{
try
{
sql << "CREATE TABLE soci_test( \"column_with:colon\" integer)";
sql << "CREATE TYPE \"type_with:colon\" AS ENUM ('en_one', 'en_two');";
sql << "CREATE FUNCTION \"function_with:colon\"() RETURNS integer LANGUAGE 'sql' AS "
"$BODY$"
" SELECT \"column_with:colon\" FROM soci_test LIMIT 1; "
"$BODY$;"
;
}
catch(...)
{
drop();
}
}
~test_false_bind_variable_inside_identifier_table_creator(){
drop();
}
private:
void drop()
{
try
{
msession << "DROP FUNCTION IF EXISTS \"function_with:colon\"();";
msession << "DROP TYPE IF EXISTS \"type_with:colon\" ;";
}
catch (soci_error const&){}
}
soci::session& msession;
};
TEST_CASE("false_bind_variable_inside_identifier", "[postgresql][bind-variables]")
{
std::string col_name;
int fct_return_value;
std::string type_value;
{
soci::session sql(backEnd, connectString);
test_false_bind_variable_inside_identifier_table_creator tableCreator(sql);
sql << "insert into soci_test(\"column_with:colon\") values(2020)";
sql << "SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'soci_test';", into(col_name);
sql << "SELECT \"function_with:colon\"() ;", into(fct_return_value);
sql << "SELECT unnest(enum_range(NULL::\"type_with:colon\")) ORDER BY 1 LIMIT 1;", into(type_value);
}
CHECK(col_name.compare("column_with:colon") == 0);
CHECK(fct_return_value == 2020);
CHECK(type_value.compare("en_one")==0);
}
// test_enum_with_explicit_custom_type_string_rowset
struct test_enum_with_explicit_custom_type_string_rowset : table_creator_base
{
test_enum_with_explicit_custom_type_string_rowset(soci::session & sql)
: table_creator_base(sql)
, msession(sql)
{
try
{
sql << "CREATE TYPE EnumType AS ENUM ('A','B','C');";
sql << "CREATE TABLE soci_test (Type EnumType NOT NULL DEFAULT 'A');";
}
catch (...)
{
drop();
}
}
~test_enum_with_explicit_custom_type_string_rowset()
{
drop();
}
private:
void drop()
{
try
{
msession << "drop table if exists soci_test;";
msession << "DROP TYPE IF EXISTS EnumType ;";
}
catch (soci_error const& e){
std::cerr << e.what() << std::endl;
}
}
soci::session& msession;
};
TEST_CASE("test_enum_with_explicit_custom_type_string_rowset", "[postgresql][bind-variables]")
{
TestStringEnum test_value = TestStringEnum::VALUE_STR_2;
TestStringEnum type_value;
{
soci::session sql(backEnd, connectString);
test_enum_with_explicit_custom_type_string_rowset tableCreator(sql);
statement s1 = (sql.prepare << "insert into soci_test values(:val);", use(test_value, "val"));
statement s2 = (sql.prepare << "SELECT Type FROM soci_test;");
s1.execute(false);
soci::row result;
s2.define_and_bind();
s2.exchange_for_rowset(soci::into(result));
s2.execute(true);
type_value = result.get<TestStringEnum>("type");
}
CHECK(type_value==TestStringEnum::VALUE_STR_2);
}
TEST_CASE("test_enum_with_explicit_custom_type_string_into", "[postgresql][bind-variables]")
{
TestStringEnum test_value = TestStringEnum::VALUE_STR_2;
TestStringEnum type_value;
{
soci::session sql(backEnd, connectString);
test_enum_with_explicit_custom_type_string_rowset tableCreator(sql);
statement s1 = (sql.prepare << "insert into soci_test values(:val);", use(test_value, "val"));
statement s2 = (sql.prepare << "SELECT Type FROM soci_test;", into(type_value));
s1.execute(false);
s2.execute(true);
}
CHECK(type_value==TestStringEnum::VALUE_STR_2);
}
// test_enum_with_explicit_custom_type_int_rowset
struct test_enum_with_explicit_custom_type_int_rowset : table_creator_base
{
test_enum_with_explicit_custom_type_int_rowset(soci::session & sql)
: table_creator_base(sql)
, msession(sql)
{
try
{
sql << "CREATE TABLE soci_test( Type smallint)";
;
}
catch (...)
{
drop();
}
}
~test_enum_with_explicit_custom_type_int_rowset()
{
drop();
}
private:
void drop()
{
try
{
msession << "drop table if exists soci_test;";
}
catch (soci_error const& e){
std::cerr << e.what() << std::endl;
}
}
soci::session& msession;
};
TEST_CASE("test_enum_with_explicit_custom_type_int_rowset", "[postgresql][bind-variables]")
{
TestIntEnum test_value = TestIntEnum::VALUE_INT_2;
TestIntEnum type_value;
{
soci::session sql(backEnd, connectString);
test_enum_with_explicit_custom_type_int_rowset tableCreator(sql);
statement s1 = (sql.prepare << "insert into soci_test(Type) values(:val)", use(test_value, "val"));
statement s2 = (sql.prepare << "SELECT Type FROM soci_test ;");
s1.execute(false);
soci::row result;
s2.define_and_bind();
s2.exchange_for_rowset(soci::into(result));
s2.execute(true);
type_value = result.get<TestIntEnum>("type");
}
CHECK(type_value==TestIntEnum::VALUE_INT_2);
}
TEST_CASE("test_enum_with_explicit_custom_type_int_into", "[postgresql][bind-variables]")
{
TestIntEnum test_value = TestIntEnum::VALUE_INT_2;
TestIntEnum type_value;
{
soci::session sql(backEnd, connectString);
test_enum_with_explicit_custom_type_int_rowset tableCreator(sql);
statement s1 = (sql.prepare << "insert into soci_test(Type) values(:val)", use(test_value, "val"));
statement s2 = (sql.prepare << "SELECT Type FROM soci_test ;", into(type_value));
s1.execute(false);
s2.execute(true);
}
CHECK(type_value==TestIntEnum::VALUE_INT_2);
}
// false_bind_variable_inside_identifier
struct table_creator_colon_in_double_quotes_in_single_quotes :
table_creator_base
{
table_creator_colon_in_double_quotes_in_single_quotes(soci::session & sql)
: table_creator_base(sql)
{
sql << "CREATE TABLE soci_test( \"column_with:colon\" text)";
}
};
TEST_CASE("colon_in_double_quotes_in_single_quotes",
"[postgresql][bind-variables]")
{
std::string return_value;
{
soci::session sql(backEnd, connectString);
table_creator_colon_in_double_quotes_in_single_quotes
tableCreator(sql);
sql << "insert into soci_test(\"column_with:colon\") values('hello "
"it is \"10:10\"')";
sql << "SELECT \"column_with:colon\" from soci_test ;", into
(return_value);
}
CHECK(return_value == "hello it is \"10:10\"");
}
//
// Support for soci Common Tests
//
// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
table_creator_one(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, val integer, c char, "
"str varchar(20), sh int2, ll bigint, ul numeric(20), "
"d float8, num76 numeric(7,6), "
"tm timestamp, i1 integer, i2 integer, i3 integer, "
"name varchar(20))";
}
};
struct table_creator_two : public table_creator_base
{
table_creator_two(soci::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(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(name varchar(100) not null, "
"phone varchar(15))";
}
};
struct table_creator_for_get_affected_rows : table_creator_base
{
table_creator_for_get_affected_rows(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val integer)";
}
};
struct table_creator_for_xml : table_creator_base
{
table_creator_for_xml(soci::session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, x xml)";
}
};
struct table_creator_for_clob : table_creator_base
{
table_creator_for_clob(soci::session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, s text)";
}
};
struct table_creator_for_blob : public tests::table_creator_base
{
table_creator_for_blob(soci::session & sql)
: tests::table_creator_base(sql)
{
sql << "create table soci_test(id integer, b oid)";
}
};
class test_context : public test_context_common
{
public:
test_context() = default;
std::string get_example_connection_string() const override
{
return "host=localhost port=5432 dbname=test user=postgres password=postgres";
}
std::string get_backend_name() const override
{
return "postgresql";
}
table_creator_base* table_creator_1(soci::session& s) const override
{
return new table_creator_one(s);
}
table_creator_base* table_creator_2(soci::session& s) const override
{
return new table_creator_two(s);
}
table_creator_base* table_creator_3(soci::session& s) const override
{
return new table_creator_three(s);
}
table_creator_base* table_creator_4(soci::session& s) const override
{
return new table_creator_for_get_affected_rows(s);
}
table_creator_base* table_creator_xml(soci::session& s) const override
{
return new table_creator_for_xml(s);
}
table_creator_base* table_creator_clob(soci::session& s) const override
{
return new table_creator_for_clob(s);
}
table_creator_base* table_creator_blob(soci::session& s) const override
{
return new table_creator_for_blob(s);
}
bool has_real_xml_support() const override
{
return true;
}
std::string to_date_time(std::string const &datdt_string) const override
{
return "timestamptz(\'" + datdt_string + "\')";
}
bool has_fp_bug() const override
{
return false;
}
std::string sql_length(std::string const& s) const override
{
return "char_length(" + s + ")";
}
};
test_context tc_postgresql;
|