33 sql <<
"create table soci_test (" 52 sql <<
"insert into soci_test(id, name) values(7, \'John\')";
55 sql <<
"select oid from soci_test where id = 7",
into(rid);
60 #ifndef SOCI_POSTGRESQL_NOPARAMS 62 sql <<
"select id, name from soci_test where oid = :rid",
71 unsigned long oid = rbe->
value_;
73 sql <<
"select id, name from soci_test where oid = " << oid,
76 #endif // SOCI_POSTGRESQL_NOPARAMS 79 assert(name ==
"John");
88 std::cout <<
"test 1 passed" << std::endl;
101 try { sql <<
"create language plpgsql"; }
104 #ifndef SOCI_POSTGRESQL_NOPARAMS 107 "create or replace function soci_test(msg varchar) " 108 "returns varchar as $$ " 109 "declare x int := 1;" 112 "end $$ language plpgsql";
116 "create or replace function soci_test(varchar) " 117 "returns varchar as \' " 118 "declare x int := 1;" 121 "end \' language plpgsql";
129 return "drop function soci_test(varchar)";
140 std::string in(
"my message");
143 #ifndef SOCI_POSTGRESQL_NOPARAMS 146 "select soci_test(:input)",
154 "select soci_test(\'" << in <<
"\')",
157 #endif // SOCI_POSTGRESQL_NOPARAMS 164 std::string in(
"my message2");
167 #ifndef SOCI_POSTGRESQL_NOPARAMS 177 "soci_test(\'" << in <<
"\')",
into(out));
179 #endif // SOCI_POSTGRESQL_NOPARAMS 186 std::cout <<
"test 2 passed" << std::endl;
196 "create table soci_test (" 210 char buf[] =
"abcdefghijklmnopqrstuvwxyz";
212 sql <<
"insert into soci_test(id, img) values(7, lo_creat(-1))";
220 sql <<
"select img from soci_test where id = 7",
into(b);
223 b.
write(0, buf,
sizeof(buf));
224 assert(b.
get_len() ==
sizeof(buf));
226 b.
append(buf,
sizeof(buf));
227 assert(b.
get_len() == 2 *
sizeof(buf));
231 sql <<
"select img from soci_test where id = 7",
into(b);
232 assert(b.
get_len() == 2 *
sizeof(buf));
235 assert(std::strncmp(buf2,
"abcdefghij", 10) == 0);
239 sql <<
"select img from soci_test where id = 7",
into(oid);
240 sql <<
"select lo_unlink(" << oid <<
")";
243 std::cout <<
"test 3 passed" << std::endl;
251 sql <<
"create table soci_test(val int8)";
263 long long v1 = 1000000000000LL;
264 assert(v1 / 1000000 == 1000000);
266 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
269 sql <<
"select val from soci_test",
into(v2);
280 std::vector<long long> v1;
281 v1.push_back(1000000000000LL);
282 v1.push_back(1000000000001LL);
283 v1.push_back(1000000000002LL);
284 v1.push_back(1000000000003LL);
285 v1.push_back(1000000000004LL);
287 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
289 std::vector<long long> v2(10);
290 sql <<
"select val from soci_test order by val desc",
into(v2);
292 assert(v2.size() == 5);
293 assert(v2[0] == 1000000000004LL);
294 assert(v2[1] == 1000000000003LL);
295 assert(v2[2] == 1000000000002LL);
296 assert(v2[3] == 1000000000001LL);
297 assert(v2[4] == 1000000000000LL);
300 std::cout <<
"test 4 passed" << std::endl;
311 unsigned long long v1 = 1000000000000ULL;
312 assert(v1 / 1000000 == 1000000);
314 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
316 unsigned long long v2 = 0ULL;
317 sql <<
"select val from soci_test",
into(v2);
328 sql <<
"create table soci_test(val boolean)";
341 sql <<
"insert into soci_test(val) values(:val)",
use(i1);
344 sql <<
"select val from soci_test",
into(i2);
348 sql <<
"update soci_test set val = true";
349 sql <<
"select val from soci_test",
into(i2);
353 std::cout <<
"test 5 passed" << std::endl;
366 assert(e.what() == std::string(
"Failed to open: libsoci_nosuchbackend.so"));
373 assert(backends.size() == 1);
374 assert(backends[0] ==
"pgsql");
383 assert(backends.empty());
390 std::cout <<
"test 6 passed" << std::endl;
399 sql <<
"select 123",
into(i);
404 sql <<
"select 'ABC'",
into (i);
409 assert(e.what() == std::string(
"Cannot convert data."));
413 std::cout <<
"test 7 passed" << std::endl;
424 std::cout <<
"test 8 passed" << std::endl;
435 sql <<
"select :a::integer",
use(a),
into(b);
439 std::cout <<
"test 9 passed" << std::endl;
448 std::string someDate =
"2009-06-17 22:51:03.123";
451 sql <<
"select :sd::date, :sd::time, :sd::timestamp",
455 assert(t1.tm_year == 2009 - 1900);
456 assert(t1.tm_mon == 6 - 1);
457 assert(t1.tm_mday == 17);
458 assert(t1.tm_hour == 0);
459 assert(t1.tm_min == 0);
460 assert(t1.tm_sec == 0);
463 assert(t2.tm_year == 0);
464 assert(t2.tm_mon == 0);
465 assert(t2.tm_mday == 1);
466 assert(t2.tm_hour == 22);
467 assert(t2.tm_min == 51);
468 assert(t2.tm_sec == 3);
471 assert(t3.tm_year == 2009 - 1900);
472 assert(t3.tm_mon == 6 - 1);
473 assert(t3.tm_mday == 17);
474 assert(t3.tm_hour == 22);
475 assert(t3.tm_min == 51);
476 assert(t3.tm_sec == 3);
479 std::cout <<
"test 10 passed" << std::endl;
489 sql <<
"create table soci_test(val integer)";
500 for (
int i = 0; i != 10; i++)
502 sql <<
"insert into soci_test(val) values(:val)",
use(i);
506 "update soci_test set val = val + 1");
512 "delete from soci_test where val <= 5");
515 assert(st2.get_affected_rows() == 5);
518 std::cout <<
"test 11 passed" << std::endl;
528 sql <<
"create table soci_test(sid serial, txt text)";
539 std::vector<long> ids(10);
540 for (std::size_t i = 0; i != ids.size(); i++)
543 std::string txt(
"abc");
544 sql <<
"insert into soci_test(txt) values(:txt) returning sid",
use(txt,
"txt"),
into(sid);
548 std::vector<long> ids2(ids.size());
549 sql <<
"select sid from soci_test order by sid",
into(ids2);
550 assert(std::equal(ids.begin(), ids.end(), ids2.begin()));
553 std::cout <<
"test 12 passed" << std::endl;
561 sql <<
"drop table if exists soci_test;";
562 sql <<
"create table soci_test ( val bytea null )";
573 unsigned char* b =
reinterpret_cast<unsigned char*
>(&v);
575 std::copy(b, b +
sizeof(v), std::back_inserter(data));
578 sql <<
"insert into soci_test(val) values(:val)",
use(data);
582 sql <<
"select val from soci_test",
into(bin1);
583 assert(bin1 ==
"\\x0d0c0b0a");
587 sql <<
"select * from soci_test",
into(r);
589 assert(r.
size() == 1);
592 std::string bin2 = r.
get<std::string>(0);
593 assert(bin2 ==
"\\x0d0c0b0a");
596 std::cout <<
"test bytea passed" << std::endl;
605 sql <<
"drop table if exists soci_json_test;";
606 sql <<
"create table soci_json_test(data json)";
616 std::pair<int,int> result;
617 sql <<
"select version()",
into(version);
618 if (sscanf(version.c_str(),
"PostgreSQL %i.%i", &result.first, &result.second) < 2)
620 throw std::runtime_error(
"Failed to retrieve PostgreSQL version number");
632 bool exception =
false;
634 std::string valid_input =
"{\"tool\":\"soci\",\"result\":42}";
635 std::string invalid_input =
"{\"tool\":\"other\",\"result\":invalid}";
639 sql <<
"insert into soci_json_test (data) values(:data)",
use(valid_input);
640 sql <<
"select data from soci_json_test",
into(result);
641 assert(result == valid_input);
645 sql <<
"insert into soci_json_test (data) values(:data)",
use(invalid_input);
653 std::cout <<
"test json passed" << std::endl;
657 std::cout <<
"test json skipped (PostgreSQL >= 9.2 required, found " << version.first <<
"." << version.second <<
")" << std::endl;
665 sql <<
"drop table if exists soci_test;";
666 sql <<
"create table soci_test(name varchar(20))";
684 <<
"select * from soci_test where name=9999");
689 std::string
const msg(e.what());
691 assert(msg.find(
"prepared statement") == std::string::npos);
692 assert(msg.find(
"operator does not exist") != std::string::npos);
695 std::cout <<
"test_statement_prepare_failure passed" << std::endl;
704 sql <<
"select :a::int",
use(v);
712 sql <<
"create table soci_test(val uuid)";
718 sql <<
"insert into soci_test(val) values(:val)",
use(uuid);
722 sql <<
"select val from soci_test",
into(uuid1);
723 assert(uuid1 == uuidExpected);
727 sql <<
"select * from soci_test",
into(r);
729 assert(r.
size() == 1);
732 std::string uuid2 = r.
get<std::string>(0);
733 assert(uuid2 == uuidExpected);
742 static const std::string standardUuid(
"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
743 std::vector<std::string> uuidFormats;
745 uuidFormats.push_back(standardUuid);
746 uuidFormats.push_back(
"A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11");
747 uuidFormats.push_back(
"{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}");
748 uuidFormats.push_back(
"a0eebc999c0b4ef8bb6d6bb9bd380a11");
749 uuidFormats.push_back(
"a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11");
750 uuidFormats.push_back(
"{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}");
752 std::vector<std::string>::const_iterator it = uuidFormats.begin();
753 while (it != uuidFormats.end())
755 std::string uuid = *it++;
759 std::cout <<
"test uuid passed" << std::endl;
772 sql <<
"create table soci_test(id integer, val integer, c char, " 773 "str varchar(20), sh int2, ul numeric(20), d float8, " 774 "tm timestamp, i1 integer, i2 integer, i3 integer, " 784 sql <<
"create table soci_test(num_float float8, num_int integer," 785 " name varchar(20), sometime timestamp, chr char)";
794 sql <<
"create table soci_test(name varchar(100) not null, " 795 "phone varchar(15))";
804 sql <<
"create table soci_test(val integer)";
838 return "timestamptz(\'" + datdt_string +
"\')";
842 int main(
int argc,
char** argv)
850 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
851 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
860 std::cout <<
"usage: " << argv[0]
861 <<
" connectstring\n" 862 <<
"example: " << argv[0]
863 <<
" \'connect_string_for_PostgreSQL\'\n";
873 std::cout <<
"\nSOCI PostgreSQL Tests:\n\n";
881 std::cout <<
"test 6 skipped (dynamic backend)\n";
894 std::cout <<
"\nOK, all tests passed.\n\n";
898 catch (std::exception
const & e)
900 std::cout << e.what() <<
'\n';
column_properties const & get_properties(std::size_t pos) const
void test_uuid_column_type_support()
T get(std::size_t pos) const
details::into_container< T, details::no_indicator > into(T &t)
void set(std::string const &name, T const &value, indicator indic=i_ok)
test_context(backend_factory const &backEnd, std::string const &connectString)
table_creator_for_test12(session &sql)
SOCI_DECL std::vector< std::string > list_all()
table_creator_base * table_creator_2(session &s) const
table_creator_for_uuid_column_type_support(session &sql)
void run(bool dbSupportsTransactions=true)
std::string get_backend_name() const
backend_factory const & backEnd
details::rowid_backend * get_backend()
int main(int argc, char **argv)
std::size_t read(std::size_t offset, char *buf, std::size_t toRead)
void try_one_uuid_format(session &sql, const std::string &uuid, const std::string &uuidExpected)
table_creator_for_test11(session &sql)
table_creator_one(session &sql)
std::string connectString
SOCI_DECL void register_backend(std::string const &name, std::string const &shared_object=std::string())
table_creator_base * table_creator_4(session &s) const
void prepare(std::string const &query, details::statement_type eType=details::st_repeatable_query)
table_creator_json(session &sql)
bool execute(bool withDataExchange=false)
blob_table_creator(session &sql)
table_creator_text(session &sql)
long long get_affected_rows()
SOCI_POSTGRESQL_DECL backend_factory const * factory_postgresql()
table_creator_base * table_creator_1(session &s) const
longlong_table_creator(session &sql)
std::size_t append(char const *buf, std::size_t toWrite)
table_creator_base * table_creator_3(session &s) const
server_version get_postgresql_version(session &sql)
table_creator_for_get_affected_rows(session &sql)
std::string drop_statement()
function_creator(session &sql)
SOCI_DECL void unload(std::string const &name)
boolean_table_creator(session &sql)
std::string to_date_time(std::string const &datdt_string) const
oid_table_creator(session &sql)
table_creator_two(session &sql)
std::pair< int, int > server_version
details::prepare_type prepare
table_creator_three(session &sql)
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
bytea_table_creator(session &sql)
void test_statement_prepare_failure()
bool execute(bool withDataExchange=false)
std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite)