29 std::time_t now = std::time(NULL);
31 t2 = *std::localtime(&now);
33 sql <<
"select t from (select :t as t from dual)",
36 assert(t1.tm_sec == t2.tm_sec);
37 assert(t1.tm_min == t2.tm_min);
38 assert(t1.tm_hour == t2.tm_hour);
39 assert(t1.tm_mday == t2.tm_mday);
40 assert(t1.tm_mon == t2.tm_mon);
41 assert(t1.tm_year == t2.tm_year);
42 assert(t1.tm_wday == t2.tm_wday);
43 assert(t1.tm_yday == t2.tm_yday);
44 assert(t1.tm_isdst == t2.tm_isdst);
48 strftime(buf,
sizeof(buf),
"%m-%d-%Y %H:%M:%S", &t2);
51 std::string format(
"MM-DD-YYYY HH24:MI:SS");
52 sql <<
"select to_char(t, :format) from (select :t as t from dual)",
55 assert(t_out == std::string(buf));
60 std::time_t then = std::time(NULL) - 17*365*24*60*60;
62 t2 = *std::localtime(&then);
64 sql <<
"select t from (select :t as t from dual)",
67 assert(t1.tm_sec == t2.tm_sec);
68 assert(t1.tm_min == t2.tm_min);
69 assert(t1.tm_hour == t2.tm_hour);
70 assert(t1.tm_mday == t2.tm_mday);
71 assert(t1.tm_mon == t2.tm_mon);
72 assert(t1.tm_year == t2.tm_year);
73 assert(t1.tm_wday == t2.tm_wday);
74 assert(t1.tm_yday == t2.tm_yday);
75 assert(t1.tm_isdst == t2.tm_isdst);
79 strftime(buf,
sizeof(buf),
"%m-%d-%Y %H:%M:%S", &t2);
82 std::string format(
"MM-DD-YYYY HH24:MI:SS");
83 sql <<
"select to_char(t, :format) from (select :t as t from dual)",
86 assert(t_out == std::string(buf));
89 std::cout <<
"test 1 passed" << std::endl;
101 st.
prepare(
"select 7 from dual");
106 std::cout <<
"test 2 passed" << std::endl;
117 "create table soci_test (" 118 " id number(10) not null," 130 char buf[] =
"abcdefghijklmnopqrstuvwxyz";
131 sql <<
"insert into soci_test (id, img) values (7, empty_blob())";
142 OCILobDisableBuffering(sessionBackEnd->
svchp_,
145 sql <<
"select img from soci_test where id = 7",
into(b);
149 b.
write(1, buf,
sizeof(buf));
150 assert(b.
get_len() ==
sizeof(buf));
162 sql <<
"select img from soci_test where id = 7",
into(b);
167 assert(strncmp(buf2,
"abcdefghij", 10) == 0);
170 std::cout <<
"test 3 passed" << std::endl;
182 "create table soci_test (" 183 " id number(5) not null," 184 " name varchar2(100)," 199 "insert into soci_test (id, name) values (:id, :name)",
202 id = 1; name =
"John"; st1.
execute(1);
203 id = 2; name =
"Anna"; st1.
execute(1);
204 id = 3; name =
"Mike"; st1.
execute(1);
209 "select cursor(select name from soci_test order by id)" 210 " from soci_test where id = 1",
216 std::vector<std::string> names;
217 while (stInner.
fetch()) { names.push_back(name); }
219 assert(names.size() == 3);
220 assert(names[0] ==
"John");
221 assert(names[1] ==
"Anna");
222 assert(names[2] ==
"Mike");
224 std::cout <<
"test 4 passed" << std::endl;
234 sql <<
"insert into soci_test(id, name) values(7, \'John\')";
237 sql <<
"select rowid from soci_test where id = 7",
into(rid);
241 sql <<
"select id, name from soci_test where rowid = :rid",
245 assert(name ==
"John");
247 std::cout <<
"test 5 passed" << std::endl;
257 "create or replace procedure soci_test(output out varchar2," 258 "input in varchar2) as " 259 "begin output := input; end;";
269 std::string in(
"my message");
272 "begin soci_test(:output, :input); end;",
280 std::string in(
"my message2");
283 "soci_test(:output, :input)",
284 use(out,
"output"),
use(in,
"input"));
290 std::cout <<
"test 6 passed" << std::endl;
299 std::string
get()
const {
return s_; }
329 sql <<
"create or replace procedure soci_test(s in out varchar2)" 330 " as begin s := s || s; end;";
339 sql <<
"create or replace procedure soci_test(s in out varchar2)" 340 " as begin s := NULL; end;";
353 sql <<
"insert into soci_test(id, name) values(:id, :name)",
use(
id),
use(in);
356 sql <<
"select name from soci_test",
into(out);
357 assert(out.
get() ==
"my string");
360 sql <<
"select * from soci_test",
into(r);
362 assert(dynamicOut.
get() ==
"my string");
365 std::cout <<
"test 7 passed" << std::endl;
377 std::string sh(
"test");
380 assert(sh ==
"testtest");
390 assert(sh.
get() ==
"testtest");
393 std::cout <<
"test 7-inout passed" << std::endl;
412 std::cout <<
"test 7-outnull passed" << std::endl;
424 std::vector<int> ids;
427 std::vector<int> codes;
433 sql <<
"insert into soci_test(id,code) values(:id,:code)",
440 assert(error.find(
"Bind variable size mismatch")
441 != std::string::npos);
445 sql <<
"select from soci_test",
into(ids),
into(codes);
447 catch (std::exception
const &e)
451 assert(error.find(
"Bind variable size mismatch")
452 != std::string::npos);
457 std::vector<int> ids;
459 ids.push_back(1000000);
464 sql <<
"insert into soci_test (id) values(:id)",
use(ids,
"id");
472 assert(error.find(
"ORA-01438") != std::string::npos);
474 sql <<
"select count(*) from soci_test",
into(count);
476 sql <<
"delete from soci_test";
481 std::vector<int> ids;
482 for (
int i = 0; i != 3; ++i)
491 sql <<
"select count(*) from soci_test",
into(count);
497 std::vector<int> ids;
501 sql <<
"select id from soci_test",
into(ids);
512 std::vector<int> ids;
516 sql <<
"insert into soci_test(id) values(:id)",
use(ids);
527 std::vector<indicator> inds(3);
528 std::vector<int> ids_out(3);
530 into(ids_out, inds));
533 assert(st.
execute(1) ==
false);
541 std::vector<int> ids(3);
542 sql <<
"select id from soci_test",
into(ids);
544 std::vector<indicator> inds_in;
545 inds_in.push_back(
i_ok);
546 inds_in.push_back(
i_null);
547 inds_in.push_back(
i_ok);
549 std::vector<int> new_codes;
550 new_codes.push_back(10);
551 new_codes.push_back(11);
552 new_codes.push_back(10);
554 sql <<
"update soci_test set code = :code where id = :id",
555 use(new_codes, inds_in),
use(ids);
557 std::vector<indicator> inds_out(3);
558 std::vector<int> codes(3);
560 sql <<
"select code from soci_test",
into(codes, inds_out);
561 assert(codes.size() == 3 && inds_out.size() == 3);
562 assert(codes[0] == 10 && codes[2] == 10);
563 assert(inds_out[0] ==
i_ok && inds_out[1] ==
i_null 564 && inds_out[2] ==
i_ok);
571 std::vector<int> intos(3);
574 sql <<
"select code from soci_test",
into(intos);
580 assert(msg ==
"Null value fetched and no indicator defined." );
586 std::vector<indicator> inds(sz);
587 std::vector<int> ids_out(sz);
589 into(ids_out, inds));
590 const bool gotData = st.
execute(
true);
592 assert(ids_out.size() == sz);
593 assert(ids_out[0] == 10);
594 assert(ids_out[2] == 12);
595 assert(inds.size() == 3 && inds[0] ==
i_ok 596 && inds[1] ==
i_ok && inds[2] ==
i_ok);
601 std::vector<int> ids_out(2);
606 assert(ids_out.size() == 2);
607 bool gotData = st.
fetch();
609 assert(ids_out.size() == 2 && ids_out[0] == 10 && ids_out[1] == 11);
610 gotData = st.
fetch();
612 assert(ids_out.size() == 1 && ids_out[0] == 12);
613 gotData = st.
fetch();
614 assert(gotData ==
false);
620 std::vector<int> ids_out(4);
623 bool gotData = st2.
execute(
true);
625 assert(ids_out.size() == 3);
626 assert(ids_out[0] == 10);
627 assert(ids_out[2] == 12);
632 std::vector<int> more;
635 sql <<
"insert into soci_test(id) values(:id)",
use(more);
637 std::vector<int> ids(2);
639 bool gotData = st3.
execute(
true);
641 assert(ids[0] == 10);
642 assert(ids[1] == 11);
644 gotData = st3.
fetch();
646 assert(ids[0] == 12);
647 assert(ids[1] == 13);
649 gotData = st3.
fetch();
651 assert(ids.size() == 1);
652 assert(ids[0] == 14);
654 gotData = st3.
fetch();
655 assert(gotData ==
false);
658 std::cout <<
"test 8 passed" << std::endl;
669 for (
int i = 1; i <= 10; ++i)
674 sql <<
"insert into soci_test (id) values(:id)",
use(in);
677 sql <<
"select count(*) from soci_test",
into(count);
684 std::vector<int> out(4);
686 "select id from soci_test",
into(out));
691 assert(out.size() == 4);
704 assert(std::string(e.what()) ==
705 "Increasing the size of the output vector is not supported.");
711 std::vector<int> out(4);
713 "select id from soci_test",
into(out));
718 assert(out.size() == 4);
725 assert(out.size() == 3);
731 assert(out.size() == 3);
734 assert(out[2] == 10);
735 bool gotData = st.
fetch();
736 assert(gotData ==
false);
739 std::cout <<
"test 9 passed" << std::endl;
765 p.
id = v.
get<
int>(
"ID");
768 p.
gender = v.
get<std::string>(
"GENDER",
"unknown");
787 sql <<
"create table soci_test(id numeric(5,0) NOT NULL," 788 <<
" last_name varchar2(20), first_name varchar2(20), " 789 " gender varchar2(10))";
798 sql <<
"create or replace procedure soci_test(id in out number)" 799 " as begin id := id * 100; end;";
814 sql <<
"insert into soci_test(id, first_name, last_name, gender) " 815 <<
"values(:ID, :FIRST_NAME, :LAST_NAME, :GENDER)",
use(p);
823 sql <<
"select * from soci_test",
into(p1);
825 assert(p1.firstName + p1.lastName.get() ==
"PatSmith");
826 assert(p1.gender ==
"unknown");
829 sql <<
"update soci_test set first_name = :FIRST_NAME " 830 "where id = :ID",
use(p);
837 assert(p.
gender ==
"unknown");
840 sql <<
"select * from soci_test",
into(p2);
847 p3.firstName =
"Joe";
848 p3.lastName =
"Smith";
849 sql <<
"insert into soci_test(id, first_name, last_name, gender) " 850 <<
"values(:ID, :FIRST_NAME, :LAST_NAME, :GENDER)",
use(p3);
857 bool gotData = st.
fetch();
862 gotData = st.
fetch();
866 gotData = st.
fetch();
867 assert(gotData ==
false);
901 assert(msg ==
"Null value not allowed for this type");
906 assert(p.
gender ==
"unknown");
909 std::cout <<
"test 10 passed" << std::endl;
935 p.
id = v.
get<
int>(0);
938 p.
gender = v.
get<std::string>(3,
"whoknows");
967 sql <<
"insert into soci_test(id, first_name, last_name, gender) " 968 <<
"values(:ID, :FIRST_NAME, :LAST_NAME, :GENDER)",
use(p);
972 sql <<
"select id, first_name, last_name, gender from soci_test",
into(p3);
975 assert(p3.
gender ==
"whoknows");
977 sql <<
"update soci_test set gender = 'F' where id = 1";
981 sql <<
"select id, first_name, last_name, gender from soci_test",
into(p4);
986 std::cout <<
"test 11 passed" << std::endl;
998 sql <<
"create table soci_test(l long)";
1007 const std::string::size_type max = 32768;
1008 std::string in(max,
'X');
1010 sql <<
"insert into soci_test values(:l)",
use(in);
1013 sql <<
"select l from soci_test",
into(out);
1015 assert(out.size() == max);
1018 std::cout <<
"test 12 passed" << std::endl;
1028 "select 2 * :i into :i from dual; " 1036 "select 2 * :i into :i from dual;" 1043 const std::string msg = e.what();
1044 assert(msg ==
"Attempted modification of const use element");
1047 std::cout <<
"test 13 passed" << std::endl;
1055 sql <<
"create table soci_test(val number(20))";
1067 long long v1 = 1000000000000LL;
1068 assert(v1 / 1000000 == 1000000);
1070 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
1073 sql <<
"select val from soci_test",
into(v2);
1084 std::vector<long long> v1;
1085 v1.push_back(1000000000000LL);
1086 v1.push_back(1000000000001LL);
1087 v1.push_back(1000000000002LL);
1088 v1.push_back(1000000000003LL);
1089 v1.push_back(1000000000004LL);
1091 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
1093 std::vector<long long> v2(10);
1094 sql <<
"select val from soci_test order by val desc",
into(v2);
1096 assert(v2.size() == 5);
1097 assert(v2[0] == 1000000000004LL);
1098 assert(v2[1] == 1000000000003LL);
1099 assert(v2[2] == 1000000000002LL);
1100 assert(v2[3] == 1000000000001LL);
1101 assert(v2[4] == 1000000000000LL);
1104 std::cout <<
"test 14 passed" << std::endl;
1116 sql <<
"create table soci_test(id number(10,0), val number(4,0), c char, " 1117 "str varchar2(20), sh number, ul number, d number, " 1118 "tm date, i1 number, i2 number, i3 number, name varchar2(20))";
1127 sql <<
"create table soci_test(num_float number, num_int numeric(4,0)," 1128 " name varchar2(20), sometime date, chr char)";
1137 sql <<
"create table soci_test(name varchar2(100) not null, " 1138 "phone varchar2(15))";
1147 sql <<
"create table soci_test(val number)";
1180 return "to_date('" + datdt_string +
"', 'YYYY-MM-DD HH24:MI:SS')";
1191 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
1192 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1201 std::cout <<
"usage: " << argv[0]
1202 <<
" connectstring\n" 1203 <<
"example: " << argv[0]
1204 <<
" \'service=orcl user=scott password=tiger\'\n";
1214 std::cout <<
"\nsoci Oracle tests:\n\n";
1232 std::cout <<
"\nOK, all tests passed.\n\n";
1234 return EXIT_SUCCESS;
1236 catch (std::exception
const & e)
1238 std::cout << e.what() <<
'\n';
1240 return EXIT_FAILURE;
details::into_container< T, details::no_indicator > into(T &t)
static void from_base(const std::string &s, indicator, string_holder &sh)
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_four(session &sql)
table_creator_base * table_creator_2(session &s) const
void run(bool dbSupportsTransactions=true)
SOCI_ORACLE_DECL backend_factory const * factory_oracle()
details::session_backend * get_backend()
void trim(std::size_t newLen)
std::size_t read(std::size_t offset, char *buf, std::size_t toRead)
in_out_procedure_creator(session &sql)
static void to_base(person const &p, values &v, indicator &ind)
string_holder(const char *s)
table_creator_one(session &sql)
backend_factory const & backEnd
table_creator_base * table_creator_4(session &s) const
times100_procedure_creator(session &sql)
static void from_base(values const &v, indicator, person2 &p)
void prepare(std::string const &query, details::statement_type eType=details::st_repeatable_query)
static void from_base(values const &v, indicator, person3 &p)
bool execute(bool withDataExchange=false)
static void from_base(values const &v, indicator, person &p)
blob_table_creator(session &sql)
string_holder(std::string s)
returns_null_procedure_creator(session &sql)
table_creator_base * table_creator_1(session &s) const
longlong_table_creator(session &sql)
table_creator_base * table_creator_3(session &s) const
std::string connectString
person_table_creator(session &sql)
procedure_creator(session &sql)
long_table_creator(session &sql)
details::blob_backend * get_backend()
T get(std::size_t pos) const
std::string to_date_time(std::string const &datdt_string) const
basic_table_creator(session &sql)
table_creator_two(session &sql)
int main(int argc, char **argv)
details::prepare_type prepare
static void to_base(const string_holder &sh, std::string &s, indicator &ind)
void exchange(details::into_type_ptr const &i)
table_creator_three(session &sql)
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
bool execute(bool withDataExchange=false)
std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite)