8 #ifndef SOCI_COMMON_TESTS_H_INCLUDED 9 #define SOCI_COMMON_TESTS_H_INCLUDED 16 #include <boost/version.hpp> 20 #if defined(BOOST_VERSION) && BOOST_VERSION >= 103500 23 #endif // BOOST_VERSION 47 void setName(std::string
const & n) { name_ = n; }
48 std::string
getName()
const {
return name_; }
50 void setPhone(std::string
const & p) { phone_ = p; }
51 std::string
getPhone()
const {
return phone_; }
64 void set(
int i) { i_ = i; }
65 int get()
const {
return i_; }
101 pe.
name = v.
get<std::string>(
"NAME");
102 pe.
phone = v.
get<std::string>(
"PHONE",
"<NULL>");
122 pe.
name = v.
get<std::string>(
"NAME");
124 pe.
phone = ind ==
i_null ?
"<NULL>" : v.
get<std::string>(
"PHONE");
144 pe.
setPhone(v.
get<std::string>(
"PHONE",
"<NULL>"));
164 #define SOCI_TEST_ENSURE_CONNECTED(sql, method) { \ 168 assert(!"exception expected"); \ 169 } catch (soci_error const &e) { msg = e.what(); } \ 170 assert(msg.empty() == false); } (void)sql 172 #define SOCI_TEST_ENSURE_CONNECTED2(sql, method) { \ 174 try { std::string seq; long v(0); \ 175 (sql.method)(seq, v); \ 176 assert(!"exception expected"); \ 177 } catch (soci_error const &e) { msg = e.what(); } \ 178 assert(msg.empty() == false); } (void)sql 185 double const epsilon(std::numeric_limits<float>::epsilon() * 100);
186 double const scale(1.0);
187 return std::fabs(a - b) < epsilon * (scale + (std::max)(std::fabs(a), std::fabs(b)));
196 : msession(sql) { drop(); }
204 msession <<
"drop table soci_test";
219 : msession(sql) { drop(); }
225 try { msession <<
"drop procedure soci_test"; }
catch (
soci_error&) {}
234 : msession(sql) { drop(); }
241 return "drop function soci_test";
247 try { msession << dropstatement(); }
catch (
soci_error&) {}
257 : backEndFactory_(backEnd),
258 connectString_(connectString) {}
262 return backEndFactory_;
267 return connectString_;
270 virtual std::string to_date_time(std::string
const &dateTime)
const = 0;
289 backEndFactory_(tc.get_backend_factory()),
290 connectString_(tc.get_connect_string())
293 void run(
bool dbSupportsTransactions =
true)
295 std::cout<<
"\nSOCI Common Tests:\n\n";
308 if (dbSupportsTransactions)
314 std::cout<<
"skipping test 10 (database doesn't support transactions)\n";
338 test_get_affected_rows();
339 test_query_transformation();
340 test_query_transformation_with_connection_pool();
343 test_prepared_insert_with_orm_type();
345 test_placeholder_partial_matching_with_orm_type();
369 std::cout <<
"test 0 passed\n";
373 session sql(backEndFactory_, connectString_);
381 sql <<
"drop table soci_test_nosuchtable";
388 assert(msg.empty() ==
false);
390 sql <<
"insert into soci_test (id) values (" << 123 <<
")";
392 sql <<
"select id from soci_test",
into(
id);
395 std::cout <<
"test 1 passed\n";
402 session sql(backEndFactory_, connectString_);
408 sql <<
"insert into soci_test(c) values(:c)",
use(c);
409 sql <<
"select c from soci_test",
into(c);
416 std::string helloSOCI(
"Hello, SOCI!");
417 sql <<
"insert into soci_test(str) values(:s)",
use(helloSOCI);
419 sql <<
"select str from soci_test",
into(str);
420 assert(str ==
"Hello, SOCI!");
427 sql <<
"insert into soci_test(sh) values(:id)",
use(three);
429 sql <<
"select sh from soci_test",
into(sh);
437 sql <<
"insert into soci_test(id) values(:id)",
use(five);
439 sql <<
"select id from soci_test",
into(i);
445 unsigned long seven(7);
446 sql <<
"insert into soci_test(ul) values(:ul)",
use(seven);
448 sql <<
"select ul from soci_test",
into(ul);
455 double pi(3.14159265);
456 sql <<
"insert into soci_test(d) values(:d)",
use(pi);
458 sql <<
"select d from soci_test",
into(d);
473 sql <<
"insert into soci_test(tm) values(:tm)",
use(nov15);
476 sql <<
"select tm from soci_test",
into(t);
477 assert(t.tm_year == 105);
478 assert(t.tm_mon == 10);
479 assert(t.tm_mday == 15);
480 assert(t.tm_hour == 0);
481 assert(t.tm_min == 0);
482 assert(t.tm_sec == 0);
496 sql <<
"insert into soci_test(tm) values(:tm)",
use(nov15);
499 sql <<
"select tm from soci_test",
into(t);
500 assert(t.tm_year == 105);
501 assert(t.tm_mon == 10);
502 assert(t.tm_mday == 15);
503 assert(t.tm_hour == 22);
504 assert(t.tm_min == 14);
505 assert(t.tm_sec == 17);
513 std::string str(
"Hello");
514 sql <<
"insert into soci_test(id, str) values(:id, :str)",
519 sql <<
"select id from soci_test",
into(i, ind);
527 sql <<
"insert into soci_test(id,tm) values(NULL,NULL)";
530 sql <<
"select id from soci_test",
into(i, ind);
535 sql <<
"select tm from soci_test",
into(t, ind);
541 sql <<
"select id from soci_test",
into(i);
546 std::string error = e.what();
548 "Null value fetched and no indicator defined.");
551 sql <<
"select id from soci_test where id = 1000",
into(i, ind);
552 assert(sql.got_data() ==
false);
555 sql <<
"select id from soci_test where id = 1000",
into(i);
556 assert(sql.got_data() ==
false);
560 sql <<
"select id from soci_test where id = :id",
use(
id),
into(i);
561 assert(sql.got_data() ==
false);
565 std::cout <<
"test 2 passed" << std::endl;
572 session sql(backEndFactory_, connectString_);
580 for (c =
'a'; c <=
'z'; ++c)
582 sql <<
"insert into soci_test(c) values(\'" << c <<
"\')";
586 sql <<
"select count(*) from soci_test",
into(count);
587 assert(count ==
'z' -
'a' + 1);
593 "select c from soci_test order by c",
into(c));
601 assert(c2 ==
'a' + count);
606 std::vector<char> vec(10);
608 "select c from soci_test order by c",
into(vec));
612 for (std::size_t i = 0; i != vec.size(); ++i)
614 assert(c2 == vec[i]);
620 assert(c2 ==
'a' + count);
625 std::vector<char> vec;
628 sql <<
"select c from soci_test",
into(vec);
633 std::string msg = e.what();
634 assert(msg ==
"Vectors of size 0 are not allowed.");
645 int const rowsToTest = 10;
646 for (
int i = 0; i != rowsToTest; ++i)
648 std::ostringstream ss;
651 sql <<
"insert into soci_test(str) values(\'" 652 << ss.str() <<
"\')";
656 sql <<
"select count(*) from soci_test",
into(count);
657 assert(count == rowsToTest);
663 "select str from soci_test order by str",
into(s));
668 std::ostringstream ss;
670 assert(s == ss.str());
673 assert(i == rowsToTest);
678 std::vector<std::string> vec(4);
680 "select str from soci_test order by str",
into(vec));
684 for (std::size_t j = 0; j != vec.size(); ++j)
686 std::ostringstream ss;
688 assert(ss.str() == vec[j]);
694 assert(i == rowsToTest);
703 short const rowsToTest = 100;
705 for (sh = 0; sh != rowsToTest; ++sh)
707 sql <<
"insert into soci_test(sh) values(" << sh <<
")";
711 sql <<
"select count(*) from soci_test",
into(count);
712 assert(count == rowsToTest);
718 "select sh from soci_test order by sh",
into(sh));
726 assert(sh2 == rowsToTest);
731 std::vector<short> vec(8);
733 "select sh from soci_test order by sh",
into(vec));
737 for (std::size_t i = 0; i != vec.size(); ++i)
739 assert(sh2 == vec[i]);
745 assert(sh2 == rowsToTest);
754 int const rowsToTest = 100;
756 for (i = 0; i != rowsToTest; ++i)
758 sql <<
"insert into soci_test(id) values(" << i <<
")";
762 sql <<
"select count(*) from soci_test",
into(count);
763 assert(count == rowsToTest);
769 "select id from soci_test order by id",
into(i));
777 assert(i2 == rowsToTest);
786 "select id from soci_test where id >= :cond order by id",
795 assert(i2 == rowsToTest);
800 std::vector<int> vec(8);
802 "select id from soci_test order by id",
into(vec));
806 for (std::size_t i = 0; i != vec.size(); ++i)
808 assert(i2 == vec[i]);
814 assert(i2 == rowsToTest);
823 unsigned int const rowsToTest = 100;
825 for (ul = 0; ul != rowsToTest; ++ul)
827 sql <<
"insert into soci_test(ul) values(" << ul <<
")";
831 sql <<
"select count(*) from soci_test",
into(count);
832 assert(count == static_cast<int>(rowsToTest));
835 unsigned int ul2 = 0;
838 "select ul from soci_test order by ul",
into(ul));
846 assert(ul2 == rowsToTest);
849 unsigned int ul2 = 0;
851 std::vector<unsigned int> vec(8);
853 "select ul from soci_test order by ul",
into(vec));
857 for (std::size_t i = 0; i != vec.size(); ++i)
859 assert(ul2 == vec[i]);
865 assert(ul2 == rowsToTest);
874 int const rowsToTest = 100;
876 for (
int i = 0; i != rowsToTest; ++i)
878 sql <<
"insert into soci_test(d) values(" << d <<
")";
883 sql <<
"select count(*) from soci_test",
into(count);
884 assert(count == rowsToTest);
891 "select d from soci_test order by d",
into(d));
900 assert(i == rowsToTest);
906 std::vector<double> vec(8);
908 "select d from soci_test order by d",
into(vec));
912 for (std::size_t j = 0; j != vec.size(); ++j)
921 assert(i == rowsToTest);
930 int const rowsToTest = 8;
931 for (
int i = 0; i != rowsToTest; ++i)
933 std::ostringstream ss;
934 ss << 2000 + i <<
"-0" << 1 + i <<
'-' << 20 - i <<
' ' 935 << 15 + i <<
':' << 50 - i <<
':' << 40 + i;
937 sql <<
"insert into soci_test(id, tm) values(" << i
942 sql <<
"select count(*) from soci_test",
into(count);
943 assert(count == rowsToTest);
950 "select tm from soci_test order by id",
into(t));
955 assert(t.tm_year + 1900 == 2000 + i);
956 assert(t.tm_mon + 1 == 1 + i);
957 assert(t.tm_mday == 20 - i);
958 assert(t.tm_hour == 15 + i);
959 assert(t.tm_min == 50 - i);
960 assert(t.tm_sec == 40 + i);
964 assert(i == rowsToTest);
969 std::vector<std::tm> vec(3);
971 "select tm from soci_test order by id",
into(vec));
975 for (std::size_t j = 0; j != vec.size(); ++j)
977 assert(vec[j].tm_year + 1900 == 2000 + i);
978 assert(vec[j].tm_mon + 1 == 1 + i);
979 assert(vec[j].tm_mday == 20 - i);
980 assert(vec[j].tm_hour == 15 + i);
981 assert(vec[j].tm_min == 50 - i);
982 assert(vec[j].tm_sec == 40 + i);
989 assert(i == rowsToTest);
994 std::cout <<
"test 3 passed" << std::endl;
1000 session sql(backEndFactory_, connectString_);
1005 sql <<
"insert into soci_test(id, val) values(1, 10)";
1006 sql <<
"insert into soci_test(id, val) values(2, 11)";
1007 sql <<
"insert into soci_test(id, val) values(3, NULL)";
1008 sql <<
"insert into soci_test(id, val) values(4, NULL)";
1009 sql <<
"insert into soci_test(id, val) values(5, 12)";
1016 "select val from soci_test order by id",
into(val, ind));
1019 bool gotData = st.
fetch();
1021 assert(ind ==
i_ok);
1023 gotData = st.
fetch();
1025 assert(ind ==
i_ok);
1027 gotData = st.
fetch();
1030 gotData = st.
fetch();
1033 gotData = st.
fetch();
1035 assert(ind ==
i_ok);
1037 gotData = st.
fetch();
1038 assert(gotData ==
false);
1041 std::vector<int> vals(3);
1042 std::vector<indicator> inds(3);
1045 "select val from soci_test order by id",
into(vals, inds));
1048 bool gotData = st.
fetch();
1050 assert(vals.size() == 3);
1051 assert(inds.size() == 3);
1052 assert(inds[0] ==
i_ok);
1053 assert(vals[0] == 10);
1054 assert(inds[1] ==
i_ok);
1055 assert(vals[1] == 11);
1056 assert(inds[2] ==
i_null);
1057 gotData = st.
fetch();
1059 assert(vals.size() == 2);
1060 assert(inds[0] ==
i_null);
1061 assert(inds[1] ==
i_ok);
1062 assert(vals[1] == 12);
1063 gotData = st.
fetch();
1064 assert(gotData ==
false);
1069 std::vector<int> vals(3);
1070 std::vector<indicator> inds(3);
1073 "select val from soci_test where 0 = 1",
into(vals, inds));
1075 bool gotData = st.
execute(
true);
1076 assert(gotData ==
false);
1079 assert(vals.empty());
1080 assert(inds.empty());
1089 gotData = st.
fetch();
1090 assert(gotData ==
false);
1091 assert(vals.empty());
1092 assert(inds.empty());
1097 std::vector<int> vals(3);
1098 std::vector<indicator> inds(3);
1100 sql <<
"select val from soci_test where 0 = 1",
1104 assert(vals.empty());
1105 assert(inds.empty());
1109 std::cout <<
"test 4 passed" << std::endl;
1116 session sql(backEndFactory_, connectString_);
1121 sql <<
"insert into soci_test(id, val) values(1, 10)";
1122 sql <<
"insert into soci_test(id, val) values(2, 11)";
1123 sql <<
"insert into soci_test(id, val) values(3, NULL)";
1124 sql <<
"insert into soci_test(id, val) values(4, NULL)";
1125 sql <<
"insert into soci_test(id, val) values(5, 12)";
1128 std::vector<int> vals(4);
1129 std::vector<indicator> inds;
1132 "select val from soci_test order by id",
into(vals, inds));
1136 assert(vals.size() == 4);
1137 assert(inds.size() == 4);
1140 assert(vals.size() == 1);
1141 assert(inds.size() == 1);
1145 std::cout <<
"test 5 passed" << std::endl;
1152 #ifndef SOCI_POSTGRESQL_NOPARAMS 1154 session sql(backEndFactory_, connectString_);
1160 sql <<
"insert into soci_test(c) values(:c)",
use(c);
1163 sql <<
"select c from soci_test",
into(c);
1171 std::string s =
"Hello SOCI!";
1172 sql <<
"insert into soci_test(str) values(:s)",
use(s);
1175 sql <<
"select str from soci_test",
into(str);
1177 assert(str ==
"Hello SOCI!");
1184 sql <<
"insert into soci_test(id) values(:id)",
use(s);
1187 sql <<
"select id from soci_test",
into(s2);
1196 sql <<
"insert into soci_test(id) values(:i)",
use(i);
1199 sql <<
"select id from soci_test",
into(i2);
1201 assert(i2 == -12345678);
1207 unsigned long ul = 4000000000ul;
1208 sql <<
"insert into soci_test(ul) values(:num)",
use(ul);
1210 unsigned long ul2 = 0;
1211 sql <<
"select ul from soci_test",
into(ul2);
1213 assert(ul2 == 4000000000ul);
1219 double d = 3.14159265;
1220 sql <<
"insert into soci_test(d) values(:d)",
use(d);
1223 sql <<
"select d from soci_test",
into(d2);
1238 sql <<
"insert into soci_test(tm) values(:t)",
use(t);
1248 sql <<
"select tm from soci_test",
into(t2);
1250 assert(t.tm_year == 105);
1251 assert(t.tm_mon == 10);
1252 assert(t.tm_mday == 19);
1253 assert(t.tm_hour == 21);
1254 assert(t.tm_min == 39);
1255 assert(t.tm_sec == 57);
1263 <<
"insert into soci_test(id) values(:id)",
use(i));
1272 std::vector<int> v(5);
1273 sql <<
"select id from soci_test order by id",
into(v);
1275 assert(v.size() == 3);
1287 sql <<
"insert into soci_test(c) values(:c)",
use(c);
1290 sql <<
"select c from soci_test",
into(c2);
1298 std::string
const s =
"Hello const SOCI!";
1299 sql <<
"insert into soci_test(str) values(:s)",
use(s);
1302 sql <<
"select str from soci_test",
into(str);
1304 assert(str ==
"Hello const SOCI!");
1310 short const s = 123;
1311 sql <<
"insert into soci_test(id) values(:id)",
use(s);
1314 sql <<
"select id from soci_test",
into(s2);
1322 int const i = -12345678;
1323 sql <<
"insert into soci_test(id) values(:i)",
use(i);
1326 sql <<
"select id from soci_test",
into(i2);
1328 assert(i2 == -12345678);
1334 unsigned long const ul = 4000000000ul;
1335 sql <<
"insert into soci_test(ul) values(:num)",
use(ul);
1337 unsigned long ul2 = 0;
1338 sql <<
"select ul from soci_test",
into(ul2);
1340 assert(ul2 == 4000000000ul);
1346 double const d = 3.14159265;
1347 sql <<
"insert into soci_test(d) values(:d)",
use(d);
1350 sql <<
"select d from soci_test",
into(d2);
1365 std::tm
const & ct = t;
1366 sql <<
"insert into soci_test(tm) values(:t)",
use(ct);
1376 sql <<
"select tm from soci_test",
into(t2);
1378 assert(t.tm_year == 105);
1379 assert(t.tm_mon == 10);
1380 assert(t.tm_mday == 19);
1381 assert(t.tm_hour == 21);
1382 assert(t.tm_min == 39);
1383 assert(t.tm_sec == 57);
1387 std::cout <<
"test 6 passed" << std::endl;
1388 #endif // SOCI_POSTGRESQL_NOPARAMS 1395 session sql(backEndFactory_, connectString_);
1403 #ifndef SOCI_POSTGRESQL_NOPARAMS 1405 sql <<
"insert into soci_test(i1, i2, i3) values(:i1, :i2, :i3)",
1411 sql <<
"insert into soci_test(i1, i2, i3) values(5, 6, 7)";
1413 #endif // SOCI_POSTGRESQL_NOPARAMS 1418 sql <<
"select i1, i2, i3 from soci_test",
1426 sql <<
"delete from soci_test";
1432 #ifndef SOCI_POSTGRESQL_NOPARAMS 1435 <<
"insert into soci_test(i1, i2, i3) values(:i1, :i2, :i3)",
1454 sql <<
"insert into soci_test(i1, i2, i3) values(1, 2, 3)";
1455 sql <<
"insert into soci_test(i1, i2, i3) values(4, 5, 6)";
1456 sql <<
"insert into soci_test(i1, i2, i3) values(7, 8, 9)";
1458 #endif // SOCI_POSTGRESQL_NOPARAMS 1460 std::vector<int> v1(5);
1461 std::vector<int> v2(5);
1462 std::vector<int> v3(5);
1464 sql <<
"select i1, i2, i3 from soci_test order by i1",
1467 assert(v1.size() == 3);
1468 assert(v2.size() == 3);
1469 assert(v3.size() == 3);
1482 std::cout <<
"test 7 passed" << std::endl;
1489 #ifndef SOCI_POSTGRESQL_NOPARAMS 1492 session sql(backEndFactory_, connectString_);
1498 std::vector<char> v;
1504 sql <<
"insert into soci_test(c) values(:c)",
use(v);
1506 std::vector<char> v2(4);
1508 sql <<
"select c from soci_test order by c",
into(v2);
1509 assert(v2.size() == 4);
1510 assert(v2[0] ==
'a');
1511 assert(v2[1] ==
'b');
1512 assert(v2[2] ==
'c');
1513 assert(v2[3] ==
'd');
1520 std::vector<std::string> v;
1523 v.push_back(
"kota");
1525 sql <<
"insert into soci_test(str) values(:s)",
use(v);
1527 std::vector<std::string> v2(4);
1529 sql <<
"select str from soci_test order by str",
into(v2);
1530 assert(v2.size() == 3);
1531 assert(v2[0] ==
"ala");
1532 assert(v2[1] ==
"kota");
1533 assert(v2[2] ==
"ma");
1540 std::vector<short> v;
1546 sql <<
"insert into soci_test(sh) values(:sh)",
use(v);
1548 std::vector<short> v2(4);
1550 sql <<
"select sh from soci_test order by sh",
into(v2);
1551 assert(v2.size() == 4);
1552 assert(v2[0] == -5);
1555 assert(v2[3] == 123);
1563 v.push_back(-2000000000);
1566 v.push_back(2000000000);
1568 sql <<
"insert into soci_test(id) values(:i)",
use(v);
1570 std::vector<int> v2(4);
1572 sql <<
"select id from soci_test order by id",
into(v2);
1573 assert(v2.size() == 4);
1574 assert(v2[0] == -2000000000);
1577 assert(v2[3] == 2000000000);
1584 std::vector<unsigned int> v;
1590 sql <<
"insert into soci_test(ul) values(:ul)",
use(v);
1592 std::vector<unsigned int> v2(4);
1594 sql <<
"select ul from soci_test order by ul",
into(v2);
1595 assert(v2.size() == 4);
1598 assert(v2[2] == 123);
1599 assert(v2[3] == 1000);
1606 std::vector<double> v;
1608 v.push_back(-0.0001);
1609 v.push_back(0.0001);
1610 v.push_back(3.1415926);
1612 sql <<
"insert into soci_test(d) values(:d)",
use(v);
1614 std::vector<double> v2(4);
1616 sql <<
"select d from soci_test order by d",
into(v2);
1617 assert(v2.size() == 4);
1628 std::vector<std::tm> v;
1645 sql <<
"insert into soci_test(tm) values(:t)",
use(v);
1647 std::vector<std::tm> v2(4);
1649 sql <<
"select tm from soci_test order by tm",
into(v2);
1650 assert(v2.size() == 3);
1651 assert(v2[0].tm_year == 105);
1652 assert(v2[0].tm_mon == 10);
1653 assert(v2[0].tm_mday == 25);
1654 assert(v2[0].tm_hour == 22);
1655 assert(v2[0].tm_min == 45);
1656 assert(v2[0].tm_sec == 37);
1657 assert(v2[1].tm_year == 105);
1658 assert(v2[1].tm_mon == 10);
1659 assert(v2[1].tm_mday == 26);
1660 assert(v2[1].tm_hour == 22);
1661 assert(v2[1].tm_min == 45);
1662 assert(v2[1].tm_sec == 17);
1663 assert(v2[2].tm_year == 105);
1664 assert(v2[2].tm_mon == 10);
1665 assert(v2[2].tm_mday == 26);
1666 assert(v2[2].tm_hour == 22);
1667 assert(v2[2].tm_min == 45);
1668 assert(v2[2].tm_sec == 37);
1676 v.push_back(-2000000000);
1679 v.push_back(2000000000);
1681 std::vector<int>
const & cv = v;
1683 sql <<
"insert into soci_test(id) values(:i)",
use(cv);
1685 std::vector<int> v2(4);
1687 sql <<
"select id from soci_test order by id",
into(v2);
1688 assert(v2.size() == 4);
1689 assert(v2[0] == -2000000000);
1692 assert(v2[3] == 2000000000);
1696 std::cout <<
"test 8 passed" << std::endl;
1698 #endif // SOCI_POSTGRESQL_NOPARAMS 1706 #ifndef SOCI_POSTGRESQL_NOPARAMS 1709 session sql(backEndFactory_, connectString_);
1720 sql <<
"insert into soci_test(i1, i2) values(:i1, :i2)",
1727 std::string what(e.what());
1729 "Binding for use elements must be either by position " 1734 sql <<
"insert into soci_test(i1, i2) values(:i1, :i2)",
1735 use(i1,
"i1"),
use(i2,
"i2");
1739 sql <<
"select i1, i2 from soci_test",
into(i1),
into(i2);
1744 sql <<
"select i2 from soci_test where i1 = :i1",
into(i2),
use(i1);
1747 sql <<
"delete from soci_test";
1751 std::vector<int> v1;
1756 std::vector<int> v2;
1761 sql <<
"insert into soci_test(i1, i2) values(:i1, :i2)",
1762 use(v1,
"i1"),
use(v2,
"i2");
1764 sql <<
"select i2, i1 from soci_test order by i1 desc",
1766 assert(v1.size() == 3);
1767 assert(v2.size() == 3);
1777 std::cout <<
"test 9 passed" << std::endl;
1779 #endif // SOCI_POSTGRESQL_NOPARAMS 1787 session sql(backEndFactory_, connectString_);
1792 sql <<
"select count(*) from soci_test",
into(count);
1798 sql <<
"insert into soci_test (id, name) values(1, 'John')";
1799 sql <<
"insert into soci_test (id, name) values(2, 'Anna')";
1800 sql <<
"insert into soci_test (id, name) values(3, 'Mike')";
1807 sql <<
"select count(*) from soci_test",
into(count);
1810 sql <<
"insert into soci_test (id, name) values(4, 'Stan')";
1812 sql <<
"select count(*) from soci_test",
into(count);
1817 sql <<
"select count(*) from soci_test",
into(count);
1823 sql <<
"delete from soci_test";
1825 sql <<
"select count(*) from soci_test",
into(count);
1830 sql <<
"select count(*) from soci_test",
into(count);
1844 std::string msg = e.what();
1846 "The transaction object cannot be handled twice.");
1851 std::cout <<
"test 10 passed" << std::endl;
1857 #ifndef SOCI_POSTGRESQL_NOPARAMS 1859 session sql(backEndFactory_, connectString_);
1869 sql <<
"insert into soci_test(id, val) values(:id, :val)",
1870 use(
id, ind1),
use(val, ind2);
1875 sql <<
"insert into soci_test(id, val) values(:id, :val)",
1876 use(
id, ind1),
use(val, ind2);
1878 sql <<
"select val from soci_test where id = 1",
into(val, ind2);
1879 assert(ind2 ==
i_ok);
1881 sql <<
"select val from soci_test where id = 2",
into(val, ind2);
1884 std::vector<int> ids;
1888 std::vector<int> vals;
1892 std::vector<indicator> inds;
1893 inds.push_back(
i_ok);
1895 inds.push_back(
i_ok);
1897 sql <<
"insert into soci_test(id, val) values(:id, :val)",
1898 use(ids),
use(vals, inds);
1902 sql <<
"select id, val from soci_test order by id desc",
1905 assert(ids.size() == 5);
1906 assert(ids[0] == 5);
1907 assert(ids[1] == 4);
1908 assert(ids[2] == 3);
1909 assert(ids[3] == 2);
1910 assert(ids[4] == 1);
1911 assert(inds.size() == 5);
1912 assert(inds[0] ==
i_ok);
1913 assert(inds[1] ==
i_null);
1914 assert(inds[2] ==
i_ok);
1915 assert(inds[3] ==
i_null);
1916 assert(inds[4] ==
i_ok);
1917 assert(vals.size() == 5);
1918 assert(vals[0] == 14);
1919 assert(vals[2] == 12);
1920 assert(vals[4] == 10);
1923 std::cout <<
"test 11 passed" << std::endl;
1925 #endif // SOCI_POSTGRESQL_NOPARAMS 1932 session sql(backEndFactory_, connectString_);
1939 sql <<
"select * from soci_test",
into(r);
1942 sql <<
"insert into soci_test" 1943 " values(3.14, 123, \'Johny\'," 1951 "select * from soci_test",
into(r));
1953 assert(r.
size() == 5);
1973 assert(r.
get<
int>(1) == 123);
1974 assert(r.
get<std::string>(2) ==
"Johny");
1976 t = r.
get<std::tm>(3);
1977 assert(t.tm_year == 105);
1980 assert(r.
get<std::string>(4) ==
"a");
1983 assert(r.
get<
int>(
"NUM_INT") == 123);
1984 assert(r.
get<std::string>(
"NAME") ==
"Johny");
1985 assert(r.
get<std::string>(
"CHR") ==
"a");
1990 bool caught =
false;
1993 r.
get<std::string>(0);
1995 catch (std::bad_cast
const &)
2009 r >> d >> i >> s >> t >> c;
2013 assert(s ==
"Johny");
2014 assert(t.tm_year == 105);
2015 assert(t.tm_mon == 11);
2016 assert(t.tm_mday == 19);
2017 assert(t.tm_hour == 22);
2018 assert(t.tm_min == 14);
2019 assert(t.tm_sec == 17);
2028 sql <<
"select * from soci_test",
into(r);
2030 assert(r.
size() == 5);
2037 sql <<
"select name, num_int from soci_test",
into(r);
2039 assert(r.
size() == 2);
2046 std::cout <<
"test 12 passed" << std::endl;
2052 session sql(backEndFactory_, connectString_);
2056 sql <<
"insert into soci_test(id, val) values(1, 10)";
2057 sql <<
"insert into soci_test(id, val) values(2, 20)";
2058 sql <<
"insert into soci_test(id, val) values(3, 30)";
2060 #ifndef SOCI_POSTGRESQL_NOPARAMS 2064 sql <<
"select val from soci_test where id = :id",
use(
id),
into(r);
2066 assert(r.
size() == 1);
2068 assert(r.
get<
int>(0) == 20);
2074 "select val from soci_test where id = :id",
use(
id),
into(r));
2078 assert(r.
size() == 1);
2080 assert(r.
get<
int>(0) == 20);
2084 assert(r.
size() == 1);
2086 assert(r.
get<
int>(0) == 30);
2090 assert(r.
size() == 1);
2092 assert(r.
get<
int>(0) == 10);
2097 sql <<
"select val from soci_test where id = 2",
into(r);
2099 assert(r.
size() == 1);
2101 assert(r.
get<
int>(0) == 20);
2103 #endif // SOCI_POSTGRESQL_NOPARAMS 2105 std::cout <<
"test 13 passed" << std::endl;
2112 session sql(backEndFactory_, connectString_);
2119 sql <<
"select * from soci_test",
into(r1);
2122 sql <<
"insert into soci_test values('david', '(404)123-4567')";
2123 sql <<
"insert into soci_test values('john', '(404)123-4567')";
2124 sql <<
"insert into soci_test values('doe', '(404)123-4567')";
2127 statement st = (sql.prepare <<
"select * from soci_test",
into(r2));
2130 assert(r2.
size() == 2);
2136 assert(r2.
get<std::string>(
"PHONE") ==
"(404)123-4567");
2140 std::cout <<
"test 14 passed" << std::endl;
2146 session sql(backEndFactory_, connectString_);
2157 sql <<
"insert into soci_test(id) values(:id)",
use(mi);
2160 sql <<
"select id from soci_test",
into(i);
2163 sql <<
"update soci_test set id = id + 1";
2165 sql <<
"select id from soci_test",
into(mi);
2166 assert(mi.get() == 124);
2177 MyInt const & cmi = mi;
2178 sql <<
"insert into soci_test(id) values(:id)",
use(cmi);
2181 sql <<
"select id from soci_test",
into(i);
2191 sql <<
"select * from soci_test",
into(p1);
2192 assert(p1.name ==
"");
2193 assert(p1.phone ==
"");
2203 sql <<
"insert into soci_test values(:NAME, :PHONE)",
use(p1);
2204 sql <<
"insert into soci_test values('john', '(404)123-4567')";
2205 sql <<
"insert into soci_test values('doe', '(404)123-4567')";
2215 if (p2.
name ==
"david")
2218 assert(p2.
phone ==
"<NULL>");
2222 assert(p2.
phone ==
"(404)123-4567");
2234 p1.
name =
"Joe Coder";
2235 p1.phone =
"123-456";
2239 sql <<
"insert into soci_test values(:NAME, :PHONE)",
use(cp1);
2242 sql <<
"select * from soci_test",
into(p2);
2245 assert(p2.
name ==
"Joe Coder");
2246 assert(p2.
phone ==
"123-456");
2256 p1.setPhone(
"10010110");
2258 sql <<
"insert into soci_test values(:NAME, :PHONE)",
use(p1);
2261 sql <<
"select * from soci_test",
into(p2);
2264 assert(p2.
getName() ==
"Joe Hacker");
2265 assert(p2.
getPhone() ==
"10010110");
2274 sql <<
"select * from soci_test",
into(p1);
2275 assert(p1.name ==
"");
2276 assert(p1.phone ==
"");
2279 sql <<
"insert into soci_test values(:NAME, :PHONE)",
use(p1);
2280 sql <<
"insert into soci_test values('john', '(404)123-4567')";
2281 sql <<
"insert into soci_test values('doe', '(404)123-4567')";
2291 if (p2.
name ==
"david")
2294 assert(p2.
phone ==
"<NULL>");
2298 assert(p2.
phone ==
"(404)123-4567");
2304 std::cout <<
"test 15 passed" << std::endl;
2310 session sql(backEndFactory_, connectString_);
2320 statement insertStatement = (sql.
prepare <<
"insert into soci_test values (:NAME, :PHONE)",
use(temp));
2323 insertStatement.
execute(
true);
2325 insertStatement.
execute(
true);
2329 sql <<
"select count(*) from soci_test where NAME in ('name1', 'name2')",
into(count);
2334 std::cout <<
"test test_prepared_insert_with_orm_type passed" << std::endl;
2340 session sql(backEndFactory_, connectString_);
2345 std::string
name =
"nameA";
2346 sql <<
"insert into soci_test values (:NAMED, :PHONE)",
use(in),
use(name,
"NAMED");
2349 sql <<
"select * from soci_test where PHONE = 'phone1'",
into(out);
2350 assert(out.
name ==
"nameA");
2351 assert(out.
phone ==
"phone1");
2354 std::cout <<
"test test_placeholder_partial_matching_with_orm_type passed" << std::endl;
2360 #ifndef SOCI_POSTGRESQL_NOPARAMS 2362 session sql(backEndFactory_, connectString_);
2366 sql <<
"insert into soci_test(name, id) values('john', 1)";
2367 sql <<
"insert into soci_test(name, id) values('george', 2)";
2368 sql <<
"insert into soci_test(name, id) values('anthony', 1)";
2369 sql <<
"insert into soci_test(name, id) values('marc', 3)";
2370 sql <<
"insert into soci_test(name, id) values('julian', 1)";
2373 std::vector<std::string> names(10);
2374 sql <<
"select name from soci_test where id = :id order by name",
2377 assert(names.size() == 3);
2378 assert(names[0] ==
"anthony");
2379 assert(names[1] ==
"john");
2380 assert(names[2] ==
"julian");
2382 #endif // SOCI_POSTGRESQL_NOPARAMS 2384 std::cout <<
"test 16 passed" << std::endl;
2390 session sql(backEndFactory_, connectString_);
2392 std::ostringstream log;
2397 sql <<
"drop table soci_test1";
2407 sql <<
"drop table soci_test2";
2417 sql <<
"drop table soci_test3";
2423 "drop table soci_test1\n" 2424 "drop table soci_test3\n");
2426 std::cout <<
"test 17 passed\n";
2432 session sql(backEndFactory_, connectString_);
2451 assert(rs1.
end() == rs2.
end());
2452 assert(rs1.
end() == rs3.
end());
2465 assert(rs1.
end() == rs2.
end());
2466 assert(rs1.
end() == rs3.
end());
2468 std::cout <<
"test 18 passed" << std::endl;
2474 session sql(backEndFactory_, connectString_);
2479 sql <<
"insert into soci_test(id, val) values(1, 10)";
2480 sql <<
"insert into soci_test(id, val) values(2, 11)";
2481 sql <<
"insert into soci_test(id, val) values(3, NULL)";
2482 sql <<
"insert into soci_test(id, val) values(4, NULL)";
2483 sql <<
"insert into soci_test(id, val) values(5, 12)";
2487 assert(5 == std::distance(rs.
begin(), rs.
end()));
2491 std::cout <<
"test 19 passed" << std::endl;
2497 session sql(backEndFactory_, connectString_);
2507 assert(0 == std::distance(rs.
begin(), rs.
end()));
2512 sql <<
"insert into soci_test values(3.14, 123, \'Johny\'," 2515 sql <<
"insert into soci_test values(6.28, 246, \'Robert\'," 2522 assert(it != rs.
end());
2527 row const & r1 = (*it);
2530 assert(r1.size() == 5);
2531 assert(r1.get_properties(0).get_data_type() ==
dt_double);
2532 assert(r1.get_properties(1).get_data_type() ==
dt_integer);
2533 assert(r1.get_properties(2).get_data_type() ==
dt_string);
2534 assert(r1.get_properties(3).get_data_type() ==
dt_date);
2535 assert(r1.get_properties(4).get_data_type() ==
dt_string);
2536 assert(r1.get_properties(
"NUM_INT").get_data_type() ==
dt_integer);
2546 std::string
name = r1.get<std::string>(2);
2548 assert(name ==
"Johny" || name ==
"Robert");
2549 if (name ==
"Johny")
2552 assert(r1.get<
int>(1) == 123);
2553 assert(r1.get<std::string>(2) ==
"Johny");
2555 t1 = r1.get<std::tm>(3);
2556 assert(t1.tm_year == 105);
2557 assert(r1.get<std::string>(4) ==
"a");
2558 assert(
equal_approx(r1.get<
double>(
"NUM_FLOAT"), 3.14));
2559 assert(r1.get<
int>(
"NUM_INT") == 123);
2560 assert(r1.get<std::string>(
"NAME") ==
"Johny");
2561 assert(r1.get<std::string>(
"CHR") ==
"a");
2566 assert(r1.get<
int>(1) == 246);
2567 assert(r1.get<std::string>(2) ==
"Robert");
2568 std::tm t1 = r1.get<std::tm>(3);
2569 assert(t1.tm_year == 104);
2570 assert(r1.get<std::string>(4) ==
"b");
2571 assert(
equal_approx(r1.get<
double>(
"NUM_FLOAT"), 6.28));
2572 assert(r1.get<
int>(
"NUM_INT") == 246);
2573 assert(r1.get<std::string>(
"NAME") ==
"Robert");
2574 assert(r1.get<std::string>(
"CHR") ==
"b");
2581 assert(it != rs.
end());
2586 row const & r2 = (*it);
2589 assert(r2.size() == 5);
2590 assert(r2.get_properties(0).get_data_type() ==
dt_double);
2591 assert(r2.get_properties(1).get_data_type() ==
dt_integer);
2592 assert(r2.get_properties(2).get_data_type() ==
dt_string);
2593 assert(r2.get_properties(3).get_data_type() ==
dt_date);
2594 assert(r2.get_properties(4).get_data_type() ==
dt_string);
2595 assert(r2.get_properties(
"NUM_INT").get_data_type() ==
dt_integer);
2597 std::string newName = r2.
get<std::string>(2);
2598 assert(name != newName);
2599 assert(newName ==
"Johny" || newName ==
"Robert");
2601 if (newName ==
"Johny")
2604 assert(r2.get<
int>(1) == 123);
2605 assert(r2.get<std::string>(2) ==
"Johny");
2606 std::tm t2 = r2.get<std::tm>(3);
2607 assert(t2.tm_year == 105);
2608 assert(r2.get<std::string>(4) ==
"a");
2609 assert(
equal_approx(r2.get<
double>(
"NUM_FLOAT"), 3.14));
2610 assert(r2.get<
int>(
"NUM_INT") == 123);
2611 assert(r2.get<std::string>(
"NAME") ==
"Johny");
2612 assert(r2.get<std::string>(
"CHR") ==
"a");
2617 assert(r2.get<
int>(1) == 246);
2618 assert(r2.get<std::string>(2) ==
"Robert");
2619 std::tm t2 = r2.get<std::tm>(3);
2620 assert(t2.tm_year == 104);
2621 assert(r2.get<std::string>(4) ==
"b");
2622 assert(
equal_approx(r2.get<
double>(
"NUM_FLOAT"), 6.28));
2623 assert(r2.get<
int>(
"NUM_INT") == 246);
2624 assert(r2.get<std::string>(
"NAME") ==
"Robert");
2625 assert(r2.get<std::string>(
"CHR") ==
"b");
2631 sql <<
"insert into soci_test " 2632 <<
"(num_int, num_float , name, sometime, chr) " 2633 <<
"values (0, NULL, NULL, NULL, NULL)";
2636 <<
"select num_int, num_float, name, sometime, chr " 2637 <<
"from soci_test where num_int = 0");
2640 assert(it != rs.
end());
2645 row const& r1 = (*it);
2648 assert(r1.size() == 5);
2649 assert(r1.get_properties(0).get_data_type() ==
dt_integer);
2650 assert(r1.get_properties(1).get_data_type() ==
dt_double);
2651 assert(r1.get_properties(2).get_data_type() ==
dt_string);
2652 assert(r1.get_properties(3).get_data_type() ==
dt_date);
2653 assert(r1.get_properties(4).get_data_type() ==
dt_string);
2657 assert(r1.get<
int>(0) == 0);
2665 std::cout <<
"test 20 passed" << std::endl;
2671 session sql(backEndFactory_, connectString_);
2676 sql <<
"insert into soci_test(id) values(1)";
2677 sql <<
"insert into soci_test(id) values(2)";
2678 sql <<
"insert into soci_test(id) values(3)";
2679 sql <<
"insert into soci_test(id) values(4)";
2680 sql <<
"insert into soci_test(id) values(5)";
2686 assert(1 == (*pos));
2689 std::advance(pos, 2);
2690 assert(3 == (*pos));
2693 std::advance(pos, 2);
2694 assert(5 == (*pos));
2698 assert(pos == rs.
end());
2702 std::cout <<
"test 21 passed" << std::endl;
2708 session sql(backEndFactory_, connectString_);
2713 sql <<
"insert into soci_test(str) values('abc')";
2714 sql <<
"insert into soci_test(str) values('def')";
2715 sql <<
"insert into soci_test(str) values('ghi')";
2716 sql <<
"insert into soci_test(str) values('jkl')";
2719 std::string idle(
"def");
2721 <<
"select str from soci_test where str = :idle",
2724 assert(1 == std::distance(rs1.
begin(), rs1.
end()));
2729 <<
"select str from soci_test where str = :idle",
2732 assert(idle == *(rs2.
begin()));
2736 std::cout <<
"test 22 passed" << std::endl;
2742 session sql(backEndFactory_, connectString_);
2747 sql <<
"insert into soci_test(str) values('abc')";
2750 bool caught =
false;
2753 std::string troublemaker;
2755 into(troublemaker));
2763 std::cout <<
"test 23 passed" << std::endl;
2772 session sql(backEndFactory_, connectString_);
2777 sql <<
"insert into soci_test(val) values(1)";
2778 sql <<
"insert into soci_test(val) values(2)";
2779 sql <<
"insert into soci_test(val) values(NULL)";
2780 sql <<
"insert into soci_test(val) values(3)";
2783 bool caught =
false;
2804 std::cout <<
"test 24 passed" << std::endl;
2811 session sql(backEndFactory_, connectString_);
2819 sql <<
"select * from soci_test",
into(p1);
2820 assert(p1.name ==
"");
2821 assert(p1.phone ==
"");
2825 sql <<
"insert into soci_test values(:NAME, :PHONE)",
use(p1);
2826 sql <<
"insert into soci_test values('john', '(404)123-4567')";
2827 sql <<
"insert into soci_test values('doe', '(404)123-4567')";
2836 if (p2.
name ==
"david")
2839 assert(p2.
phone ==
"<NULL>");
2843 assert(p2.
phone ==
"(404)123-4567");
2849 std::cout <<
"test 25 passed" << std::endl;
2858 session sql(backEndFactory_, connectString_);
2863 sql <<
"insert into soci_test(val) values(7)";
2867 boost::optional<int> opt;
2868 sql <<
"select val from soci_test",
into(opt);
2869 assert(opt.is_initialized());
2870 assert(opt.get() == 7);
2877 sql <<
"select val from soci_test",
into(opt, ind);
2878 assert(opt.is_initialized());
2879 assert(opt.get() == 7);
2880 assert(ind ==
i_ok);
2883 sql <<
"select i1 from soci_test",
into(opt);
2884 assert(opt.is_initialized() ==
false);
2888 sql <<
"select i1 from soci_test",
into(opt, ind);
2889 assert(opt.is_initialized() ==
false);
2894 sql <<
"update soci_test set val = :v",
use(opt);
2896 sql <<
"select val from soci_test",
into(j);
2901 sql <<
"update soci_test set val = :v",
use(opt);
2903 sql <<
"select val from soci_test",
into(j, ind);
2910 sql <<
"delete from soci_test";
2914 sql <<
"insert into soci_test(id, val, str) values(1, 5, \'abc\')";
2915 sql <<
"insert into soci_test(id, val, str) values(2, 6, \'def\')";
2916 sql <<
"insert into soci_test(id, val, str) values(3, 7, \'ghi\')";
2917 sql <<
"insert into soci_test(id, val, str) values(4, 8, null)";
2918 sql <<
"insert into soci_test(id, val, str) values(5, 9, \'mno\')";
2920 std::vector<boost::optional<int> > v(10);
2921 sql <<
"select val from soci_test order by val",
into(v);
2923 assert(v.size() == 5);
2924 assert(v[0].is_initialized());
2925 assert(v[0].
get() == 5);
2926 assert(v[1].is_initialized());
2927 assert(v[1].
get() == 6);
2928 assert(v[2].is_initialized());
2929 assert(v[2].
get() == 7);
2930 assert(v[3].is_initialized());
2931 assert(v[3].
get() == 8);
2932 assert(v[4].is_initialized());
2933 assert(v[4].
get() == 9);
2937 sql <<
"update soci_test set val = null where id = 2 or id = 4";
2939 std::vector<int> ids(5);
2940 sql <<
"select id, val from soci_test order by id",
into(ids),
into(v);
2942 assert(v.size() == 5);
2943 assert(ids.size() == 5);
2944 assert(v[0].is_initialized());
2945 assert(v[0].
get() == 5);
2946 assert(v[1].is_initialized() ==
false);
2947 assert(v[2].is_initialized());
2948 assert(v[2].
get() == 7);
2949 assert(v[3].is_initialized() ==
false);
2950 assert(v[4].is_initialized());
2951 assert(v[4].
get() == 9);
2960 "select id, val from soci_test order by id",
into(ids),
into(v));
2964 for (std::size_t i = 0; i != v.size(); ++i)
2966 assert(
id == ids[i]);
2968 if (
id == 2 ||
id == 4)
2970 assert(v[i].is_initialized() ==
false);
2974 assert(v[i].is_initialized() && v[i].
get() ==
id + 4);
2992 assert(it != rs.
end());
2994 row const& r1 = (*it);
2996 assert(r1.size() == 3);
3016 assert(r1.get_properties(1).get_data_type() ==
dt_integer);
3017 assert(r1.get_properties(2).get_data_type() ==
dt_string);
3019 assert(r1.get<
int>(1) == 5);
3020 assert(r1.get<std::string>(2) ==
"abc");
3021 assert(r1.get<boost::optional<int> >(1).is_initialized());
3022 assert(r1.get<boost::optional<int> >(1).get() == 5);
3023 assert(r1.get<boost::optional<std::string> >(2).is_initialized());
3024 assert(r1.get<boost::optional<std::string> >(2).get() ==
"abc");
3028 row const& r2 = (*it);
3030 assert(r2.
size() == 3);
3039 (void)r1.
get<
int>(1);
3045 assert(r2.
get<boost::optional<int> >(1).is_initialized() ==
false);
3050 row const &r3 = (*it);
3052 boost::optional<int> io;
3053 boost::optional<std::string> so;
3058 assert(io.is_initialized() && io.get() == 7);
3059 assert(so.is_initialized() && so.get() ==
"ghi");
3062 row const &r4 = (*it);
3067 assert(io.is_initialized() ==
false);
3068 assert(so.is_initialized() ==
false);
3074 sql <<
"delete from soci_test";
3076 std::vector<int> ids;
3077 std::vector<boost::optional<int> > v;
3079 ids.push_back(10); v.push_back(20);
3080 ids.push_back(11); v.push_back(21);
3081 ids.push_back(12); v.push_back(22);
3082 ids.push_back(13); v.push_back(23);
3084 sql <<
"insert into soci_test(id, val) values(:id, :val)",
3085 use(ids,
"id"),
use(v,
"val");
3088 sql <<
"select sum(val) from soci_test",
into(sum);
3093 sql <<
"delete from soci_test";
3098 sql <<
"insert into soci_test(id, val) values(:id, :val)",
3099 use(ids,
"id"),
use(v,
"val");
3101 sql <<
"select sum(val) from soci_test",
into(sum);
3108 sql <<
"delete from soci_test";
3110 boost::optional<MyInt> omi1;
3111 boost::optional<MyInt> omi2;
3116 sql <<
"insert into soci_test(id, val) values(:id, :val)",
3119 sql <<
"select id, val from soci_test",
into(omi2),
into(omi1);
3121 assert(omi1.is_initialized() ==
false);
3122 assert(omi2.is_initialized() && omi2.get().get() == 125);
3128 sql <<
"delete from soci_test";
3130 boost::optional<MyInt> omi1;
3131 boost::optional<MyInt> omi2;
3136 boost::optional<MyInt>
const & comi1 = omi1;
3137 boost::optional<MyInt>
const & comi2 = omi2;
3139 sql <<
"insert into soci_test(id, val) values(:id, :val)",
3142 sql <<
"select id, val from soci_test",
into(omi2),
into(omi1);
3144 assert(omi1.is_initialized() ==
false);
3145 assert(omi2.is_initialized() && omi2.get().get() == 125);
3153 sql <<
"insert into soci_test(id, val) values(1, 10)";
3154 sql <<
"insert into soci_test(id, val) values(2, 11)";
3155 sql <<
"insert into soci_test(id, val) values(3, NULL)";
3156 sql <<
"insert into soci_test(id, val) values(4, 13)";
3159 "select val from soci_test order by id asc");
3163 assert((*pos).is_initialized());
3164 assert(10 == (*pos).get());
3168 assert((*pos).is_initialized());
3169 assert(11 == (*pos).get());
3173 assert((*pos).is_initialized() ==
false);
3177 assert((*pos).is_initialized());
3178 assert(13 == (*pos).get());
3182 std::cout <<
"test 26 passed" << std::endl;
3184 std::cout <<
"test 26 skipped (no Boost)" << std::endl;
3185 #endif // HAVE_BOOST 3205 assert(e.what() == std::string(
3206 "Cannot reconnect without previous connection."));
3210 sql.
open(backEndFactory_, connectString_);
3219 sql.
open(backEndFactory_, connectString_);
3224 assert(e.what() == std::string(
3225 "Cannot open already connected session."));
3231 sql.
open(backEndFactory_, connectString_);
3242 sql <<
"this statement cannot execute";
3247 assert(e.what() == std::string(
"Session is not connected."));
3251 std::cout <<
"test 27 passed" << std::endl;
3257 session sql(backEndFactory_, connectString_);
3261 boost::tuple<double, int, std::string> t1(3.5, 7,
"Joe Hacker");
3263 assert(t1.get<1>() == 7);
3264 assert(t1.get<2>() ==
"Joe Hacker");
3266 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3270 boost::tuple<double, int, std::string> t2;
3271 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3274 assert(t2.get<1>() == 7);
3275 assert(t2.get<2>() ==
"Joe Hacker");
3277 sql <<
"delete from soci_test";
3284 boost::tuple<double, boost::optional<int>, std::string> t1(
3285 3.5, boost::optional<int>(7),
"Joe Hacker");
3287 assert(t1.get<1>().is_initialized());
3288 assert(t1.get<1>().get() == 7);
3289 assert(t1.get<2>() ==
"Joe Hacker");
3291 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3294 boost::tuple<double, boost::optional<int>, std::string> t2;
3295 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3298 assert(t2.get<1>().is_initialized());
3299 assert(t2.get<1>().get() == 7);
3300 assert(t2.get<2>() ==
"Joe Hacker");
3302 sql <<
"delete from soci_test";
3309 boost::tuple<double, MyInt, std::string> t1(3.5, 7,
"Joe Hacker");
3311 assert(t1.get<1>().get() == 7);
3312 assert(t1.get<2>() ==
"Joe Hacker");
3314 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3317 boost::tuple<double, MyInt, std::string> t2;
3319 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3322 assert(t2.get<1>().get() == 7);
3323 assert(t2.get<2>() ==
"Joe Hacker");
3325 sql <<
"delete from soci_test";
3332 boost::tuple<double, boost::optional<MyInt>, std::string> t1(
3333 3.5, boost::optional<MyInt>(7),
"Joe Hacker");
3335 assert(t1.get<1>().is_initialized());
3336 assert(t1.get<1>().get().get() == 7);
3337 assert(t1.get<2>() ==
"Joe Hacker");
3339 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3342 boost::tuple<double, boost::optional<MyInt>, std::string> t2;
3344 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3347 assert(t2.get<1>().is_initialized());
3348 assert(t2.get<1>().get().get() == 7);
3349 assert(t2.get<2>() ==
"Joe Hacker");
3351 sql <<
"update soci_test set num_int = NULL";
3353 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3356 assert(t2.get<1>().is_initialized() ==
false);
3357 assert(t2.get<2>() ==
"Joe Hacker");
3363 sql <<
"insert into soci_test(num_float, num_int, name) values(4.0, 8, 'Tony Coder')";
3364 sql <<
"insert into soci_test(num_float, num_int, name) values(4.5, NULL, 'Cecile Sharp')";
3365 sql <<
"insert into soci_test(num_float, num_int, name) values(5.0, 10, 'Djhava Ravaa')";
3367 typedef boost::tuple<double, boost::optional<int>, std::string> T;
3370 <<
"select num_float, num_int, name from soci_test order by num_float asc");
3375 assert(pos->get<1>().is_initialized() ==
false);
3376 assert(pos->get<2>() ==
"Joe Hacker");
3380 assert(pos->get<1>().is_initialized());
3381 assert(pos->get<1>().get() == 8);
3382 assert(pos->get<2>() ==
"Tony Coder");
3386 assert(pos->get<1>().is_initialized() ==
false);
3387 assert(pos->get<2>() ==
"Cecile Sharp");
3391 assert(pos->get<1>().is_initialized());
3392 assert(pos->get<1>().get() == 10);
3393 assert(pos->get<2>() ==
"Djhava Ravaa");
3396 assert(pos == rs.
end());
3399 std::cout <<
"test 28 passed" << std::endl;
3401 std::cout <<
"test 28 skipped (no Boost)" << std::endl;
3402 #endif // HAVE_BOOST 3408 #if defined(BOOST_VERSION) && BOOST_VERSION >= 103500 3410 session sql(backEndFactory_, connectString_);
3414 boost::fusion::vector<double, int, std::string> t1(3.5, 7,
"Joe Hacker");
3416 assert(boost::fusion::at_c<1>(t1) == 7);
3417 assert(boost::fusion::at_c<2>(t1) ==
"Joe Hacker");
3419 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3423 boost::fusion::vector<double, int, std::string> t2;
3424 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3427 assert(boost::fusion::at_c<1>(t2) == 7);
3428 assert(boost::fusion::at_c<2>(t2) ==
"Joe Hacker");
3430 sql <<
"delete from soci_test";
3437 boost::fusion::vector<double, boost::optional<int>, std::string> t1(
3438 3.5, boost::optional<int>(7),
"Joe Hacker");
3440 assert(boost::fusion::at_c<1>(t1).is_initialized());
3441 assert(boost::fusion::at_c<1>(t1).
get() == 7);
3442 assert(boost::fusion::at_c<2>(t1) ==
"Joe Hacker");
3444 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3447 boost::fusion::vector<double, boost::optional<int>, std::string> t2;
3448 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3451 assert(boost::fusion::at_c<1>(t2).is_initialized());
3452 assert(boost::fusion::at_c<1>(t2) == 7);
3453 assert(boost::fusion::at_c<2>(t2) ==
"Joe Hacker");
3455 sql <<
"delete from soci_test";
3462 boost::fusion::vector<double, MyInt, std::string> t1(3.5, 7,
"Joe Hacker");
3464 assert(boost::fusion::at_c<1>(t1).
get() == 7);
3465 assert(boost::fusion::at_c<2>(t1) ==
"Joe Hacker");
3467 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3470 boost::fusion::vector<double, MyInt, std::string> t2;
3472 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3475 assert(boost::fusion::at_c<1>(t2).
get() == 7);
3476 assert(boost::fusion::at_c<2>(t2) ==
"Joe Hacker");
3478 sql <<
"delete from soci_test";
3485 boost::fusion::vector<double, boost::optional<MyInt>, std::string> t1(
3486 3.5, boost::optional<MyInt>(7),
"Joe Hacker");
3488 assert(boost::fusion::at_c<1>(t1).is_initialized());
3489 assert(boost::fusion::at_c<1>(t1).
get().
get() == 7);
3490 assert(boost::fusion::at_c<2>(t1) ==
"Joe Hacker");
3492 sql <<
"insert into soci_test(num_float, num_int, name) values(:d, :i, :s)",
use(t1);
3495 boost::fusion::vector<double, boost::optional<MyInt>, std::string> t2;
3497 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3500 assert(boost::fusion::at_c<1>(t2).is_initialized());
3501 assert(boost::fusion::at_c<1>(t2).
get().
get() == 7);
3502 assert(boost::fusion::at_c<2>(t2) ==
"Joe Hacker");
3504 sql <<
"update soci_test set num_int = NULL";
3506 sql <<
"select num_float, num_int, name from soci_test",
into(t2);
3509 assert(boost::fusion::at_c<1>(t2).is_initialized() ==
false);
3510 assert(boost::fusion::at_c<2>(t2) ==
"Joe Hacker");
3516 sql <<
"insert into soci_test(num_float, num_int, name) values(4.0, 8, 'Tony Coder')";
3517 sql <<
"insert into soci_test(num_float, num_int, name) values(4.5, NULL, 'Cecile Sharp')";
3518 sql <<
"insert into soci_test(num_float, num_int, name) values(5.0, 10, 'Djhava Ravaa')";
3520 typedef boost::fusion::vector<double, boost::optional<int>, std::string> T;
3523 <<
"select num_float, num_int, name from soci_test order by num_float asc");
3527 assert(
equal_approx(boost::fusion::at_c<0>(*pos), 3.5));
3528 assert(boost::fusion::at_c<1>(*pos).is_initialized() ==
false);
3529 assert(boost::fusion::at_c<2>(*pos) ==
"Joe Hacker");
3532 assert(
equal_approx(boost::fusion::at_c<0>(*pos), 4.0));
3533 assert(boost::fusion::at_c<1>(*pos).is_initialized());
3534 assert(boost::fusion::at_c<1>(*pos).get() == 8);
3535 assert(boost::fusion::at_c<2>(*pos) ==
"Tony Coder");
3538 assert(
equal_approx(boost::fusion::at_c<0>(*pos), 4.5));
3539 assert(boost::fusion::at_c<1>(*pos).is_initialized() ==
false);
3540 assert(boost::fusion::at_c<2>(*pos) ==
"Cecile Sharp");
3543 assert(
equal_approx(boost::fusion::at_c<0>(*pos), 5.0));
3544 assert(boost::fusion::at_c<1>(*pos).is_initialized());
3545 assert(boost::fusion::at_c<1>(*pos).get() == 10);
3546 assert(boost::fusion::at_c<2>(*pos) ==
"Djhava Ravaa");
3549 assert(pos == rs.
end());
3552 std::cout <<
"test 29 passed" << std::endl;
3555 std::cout <<
"test 29 skipped (no boost::fusion)" << std::endl;
3556 #endif // BOOST_VERSION 3559 std::cout <<
"test 29 skipped (no Boost)" << std::endl;
3560 #endif // HAVE_BOOST 3568 session sql(backEndFactory_, connectString_);
3574 nov15.tm_year = 105;
3581 sql <<
"insert into soci_test(tm) values(:tm)",
use(nov15);
3583 boost::gregorian::date bgd;
3584 sql <<
"select tm from soci_test",
into(bgd);
3586 assert(bgd.year() == 2005);
3587 assert(bgd.month() == 11);
3588 assert(bgd.day() == 15);
3590 sql <<
"update soci_test set tm = NULL";
3593 sql <<
"select tm from soci_test",
into(bgd);
3598 assert(e.what() == std::string(
"Null value not allowed for this type"));
3605 boost::gregorian::date bgd(2008, boost::gregorian::May, 5);
3607 sql <<
"insert into soci_test(tm) values(:tm)",
use(bgd);
3610 sql <<
"select tm from soci_test",
into(t);
3612 assert(t.tm_year == 108);
3613 assert(t.tm_mon == 4);
3614 assert(t.tm_mday == 5);
3617 std::cout <<
"test 30 passed" << std::endl;
3619 std::cout <<
"test 30 skipped (no Boost)" << std::endl;
3620 #endif // HAVE_BOOST 3628 const size_t pool_size = 10;
3631 for (std::size_t i = 0; i != pool_size; ++i)
3634 sql.
open(backEndFactory_, connectString_);
3638 for (std::size_t i = 0; i != pool_size; ++i)
3648 sql <<
"insert into soci_test(c) values(:c)",
use(c);
3649 sql <<
"select c from soci_test",
into(c);
3654 std::cout <<
"test 31 passed\n";
3665 return query +
" WHERE c < 'g'";
3676 return query +
" WHERE " + where_;
3688 for (
char c =
'a'; c <=
'z'; ++c)
3690 sql <<
"insert into soci_test(c) values(\'" << c <<
"\')";
3693 char const* query =
"select count(*) from soci_test";
3699 sql << query,
into(count);
3700 assert(count ==
'z' -
'a' + 1);
3707 sql << query,
into(count);
3708 assert(count ==
'g' -
'a');
3715 sql << query,
into(count);
3716 assert(count ==
'j' -
'h');
3719 sql << query,
into(count);
3720 assert(count ==
'z' -
's');
3725 #if defined _MSC_VER && _MSC_VER>=1600 3726 #define SOCI_HAVE_CPP11 1 3727 #elif __cplusplus >= 201103L 3728 #define SOCI_HAVE_CPP11 1 3730 #undef SOCI_HAVE_CPP11 3733 #ifdef SOCI_HAVE_CPP11 3737 [](std::string
const& query) {
3738 return query +
" WHERE c > 'g' AND c < 'j'";
3742 sql << query,
into(count);
3743 assert(count ==
'j' -
'h');
3746 #undef SOCI_HAVE_CPP11 3759 assert(count ==
'g' -
'a');
3765 assert(count ==
'g' -
'a');
3780 assert(count ==
'z' -
'a' + 1);
3787 session sql(backEndFactory_, connectString_);
3788 run_query_transformation_test(sql);
3790 std::cout <<
"test query_transformation passed" << std::endl;
3796 const size_t pool_size = 10;
3799 for (std::size_t i = 0; i != pool_size; ++i)
3802 sql.
open(backEndFactory_, connectString_);
3806 run_query_transformation_test(sql);
3808 std::cout <<
"test query_transformation with connection pool passed" << std::endl;
3819 session sql(backEndFactory_, connectString_);
3821 if (!tableCreator.get())
3823 std::cout <<
"test get_affected_rows skipped (function not implemented)" << std::endl;
3827 for (
int i = 0; i != 10; i++)
3829 sql <<
"insert into soci_test(val) values(:val)",
use(i);
3833 "update soci_test set val = val + 1");
3839 "delete from soci_test where val <= 5");
3842 assert(st2.get_affected_rows() == 5);
3845 "update soci_test set val = val + 1");
3848 assert(st3.get_affected_rows() == 5);
3850 std::vector<int> v(5, 0);
3851 for (std::size_t i = 0; i < v.size(); ++i)
3858 "delete from soci_test where val = :v",
use(v));
3863 std::vector<std::string> w(2,
"1");
3866 "insert into soci_test(val) values(:val)",
use(w));
3872 sql <<
"select count(val) from soci_test",
into(val);
3881 std::cout <<
"test get_affected_rows passed" << std::endl;
3888 const size_t pool_size = 1;
3891 for (std::size_t i = 0; i != pool_size; ++i)
3894 sql.
open(backEndFactory_, connectString_);
3902 std::cout <<
"test pull-5 passed\n";
3910 session sql(backEndFactory_, connectString_);
3926 assert(
"expected exception caught");
3927 std::cout <<
"test issue-67 passed - check memory debugger output for leaks" << std::endl;
3938 session sql(backEndFactory_, connectString_);
3940 sql <<
"insert into soci_test(id) values (1)";
3947 st.
prepare(
"select id from soci_test where id = :1");
3957 std::vector<int> ids(1, 2);
3958 std::vector<int> vals(1, 1);
3963 st.
prepare(
"insert into soci_test(id, val) values (:1, :2)");
3969 sql <<
"select val from soci_test where id = 2",
into(val);
3972 std::cout <<
"test issue-154 passed - check memory debugger output for leaks" << std::endl;
3981 #endif // SOCI_COMMON_TESTS_H_INCLUDED
column_properties const & get_properties(std::size_t pos) const
std::string getName() const
const_iterator begin() const
static std::string lower_than_g(std::string query)
T get(std::size_t pos) const
details::into_container< T, details::no_indicator > into(T &t)
std::string get_last_query() const
backend_factory const & backEnd
void set(std::string const &name, T const &value, indicator indic=i_ok)
virtual table_creator_base * table_creator_4(session &) const =0
test_context_base const & tc_
bool equal_approx(double const a, double const b)
#define SOCI_TEST_ENSURE_CONNECTED(sql, method)
data_type get_data_type() const
void run(bool dbSupportsTransactions=true)
static void to_base(PhonebookEntry const &pe, values &v, indicator &ind)
static std::string no_op_transform(std::string query)
std::string get_name() const
void run_query_transformation_test(session &sql)
virtual ~function_creator_base()
indicator get_indicator(std::size_t pos) const
table_creator_base(session &sql)
virtual ~procedure_creator_base()
session & at(std::size_t pos)
std::string const connectString_
virtual table_creator_base * table_creator_1(session &) const =0
void skip(std::size_t num=1) const
const_iterator end() const
void prepare(std::string const &query, details::statement_type eType=details::st_repeatable_query)
static void from_base(values const &v, indicator, PhonebookEntry2 &pe)
static void from_base(values const &v, indicator, PhonebookEntry &pe)
std::string const connectString_
virtual std::string to_date_time(std::string const &dateTime) const =0
long long get_affected_rows()
void uppercase_column_names(bool forceToUpper)
backend_factory const & backEndFactory_
indicator get_indicator(std::size_t pos) const
void setPhone(std::string const &p)
void setName(std::string const &n)
static void from_base(values const &v, indicator, PhonebookEntry3 &pe)
std::string get_connect_string() const
static void to_base(PhonebookEntry3 const &pe, values &v, indicator &ind)
#define SOCI_TEST_ENSURE_CONNECTED2(sql, method)
void set_log_stream(std::ostream *s)
virtual table_creator_base * table_creator_3(session &) const =0
backend_factory const & get_backend_factory() const
virtual ~table_creator_base()
void test_prepared_insert_with_orm_type()
void set_query_transformation(T callback)
void open(connection_parameters const ¶meters)
std::string getPhone() const
std::string connectString
virtual ~test_context_base()
static void to_base(PhonebookEntry2 const &pe, values &v, indicator &ind)
std::auto_ptr< table_creator_base > auto_table_creator
void test_query_transformation()
common_tests(test_context_base const &tc)
T get(std::size_t pos) const
result_type operator()(argument_type query) const
where_condition(std::string const &where)
virtual table_creator_base * table_creator_2(session &) const =0
procedure_creator_base(session &sql)
details::prepare_type prepare
void exchange(details::into_type_ptr const &i)
void test_query_transformation_with_connection_pool()
test_context_base(backend_factory const &backEnd, std::string const &connectString)
void test_placeholder_partial_matching_with_orm_type()
function_creator_base(session &sql)
void test_get_affected_rows()
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
static void from_base(int i, indicator ind, MyInt &mi)
bool execute(bool withDataExchange=false)
static void to_base(MyInt const &mi, int &i, indicator &ind)
backend_factory const & backEndFactory_
virtual std::string dropstatement()