21 #include <mysqld_error.h> 39 std::string version = mysql_get_server_info(sessionBackEnd->
conn_);
41 std::istringstream iss(version);
42 if ((iss >> v) and v < 5)
44 std::cout <<
"skipping test 1 (MySQL server version ";
45 std::cout << version <<
" does not support stored procedures)\n";
49 try { sql <<
"drop function myecho"; }
53 "create function myecho(msg text) " 54 "returns text deterministic " 57 std::string in(
"my message");
61 "select myecho(:input)",
70 std::string in(
"my message2");
81 sql <<
"drop function myecho";
84 std::cout <<
"test 1 passed" << std::endl;
93 session sql(backEnd,
"host=test.soci.invalid");
97 assert(e.
err_num_ == CR_UNKNOWN_HOST ||
104 sql <<
"create table soci_test (id integer)";
108 sql <<
"select id from soci_test_nosuchtable",
into(n);
112 assert(e.
err_num_ == ER_NO_SUCH_TABLE);
116 sql <<
"insert into soci_test (invalid) values (256)";
120 assert(e.
err_num_ == ER_BAD_FIELD_ERROR);
125 std::vector<int> v(3, 5);
126 sql <<
"insert into soci_test_nosuchtable values (:n)",
use(v);
130 assert(e.
err_num_ == ER_NO_SUCH_TABLE);
132 sql <<
"drop table soci_test";
135 std::cout <<
"test 2 passed" << std::endl;
143 sql <<
"create table soci_test(val bigint)";
152 sql <<
"create table soci_test(val bigint unsigned)";
164 long long v1 = 1000000000000LL;
165 assert(v1 / 1000000 == 1000000);
167 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
170 sql <<
"select val from soci_test",
into(v2);
181 std::vector<long long> v1;
182 v1.push_back(1000000000000LL);
183 v1.push_back(1000000000001LL);
184 v1.push_back(1000000000002LL);
185 v1.push_back(1000000000003LL);
186 v1.push_back(1000000000004LL);
188 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
190 std::vector<long long> v2(10);
191 sql <<
"select val from soci_test order by val desc",
into(v2);
193 assert(v2.size() == 5);
194 assert(v2[0] == 1000000000004LL);
195 assert(v2[1] == 1000000000003LL);
196 assert(v2[2] == 1000000000002LL);
197 assert(v2[3] == 1000000000001LL);
198 assert(v2[4] == 1000000000000LL);
206 sql <<
"insert into soci_test set val = 18446744073709551615";
208 sql <<
"select * from soci_test",
into(v);
216 const char* source =
"18446744073709551615";
217 sql <<
"insert into soci_test set val = " << source;
218 unsigned long long vv = 0;
219 sql <<
"select val from soci_test",
into(vv);
220 std::stringstream buf;
222 assert(buf.str() == source);
230 const char* source =
"18446744073709551615";
231 sql <<
"insert into soci_test set val = " << source;
232 std::vector<unsigned long long> v(1);
233 sql <<
"select val from soci_test",
into(v);
234 std::stringstream buf;
236 assert(buf.str() == source);
244 unsigned long long n = 18446744073709551615ULL;
245 sql <<
"insert into soci_test(val) values (:n)",
use(n);
246 unsigned long long m = 0;
247 sql <<
"select val from soci_test",
into(m);
256 std::vector<unsigned long long> v1;
257 v1.push_back(18446744073709551615ULL);
258 v1.push_back(18446744073709551614ULL);
259 v1.push_back(18446744073709551613ULL);
260 sql <<
"insert into soci_test(val) values(:val)",
use(v1);
262 std::vector<unsigned long long> v2(10);
263 sql <<
"select val from soci_test order by val",
into(v2);
265 assert(v2.size() == 3);
266 assert(v2[0] == 18446744073709551613ULL);
267 assert(v2[1] == 18446744073709551614ULL);
268 assert(v2[2] == 18446744073709551615ULL);
271 std::cout <<
"test 3 passed" << std::endl;
274 template <
typename T>
281 sql <<
"select \'" << s <<
"\'",
into(val);
284 double v1 =
static_cast<double>(value);
285 double v2 =
static_cast<double>(val);
286 double d = std::fabs(v1 - v2);
287 double epsilon = 0.001;
288 assert(d < epsilon ||
289 d < epsilon * (std::fabs(v1) + std::fabs(v2)));
293 std::cout <<
"string \"" << s <<
"\" parsed as " << val
294 <<
" but should have failed.\n";
302 std::cout <<
"couldn't parse number: \"" << s <<
"\"\n";
307 assert(std::string(e.what()) ==
"Cannot convert data.");
315 test_num<double>(
"",
false, 0);
316 test_num<double>(
"foo",
false, 0);
317 test_num<double>(
"1",
true, 1);
318 test_num<double>(
"12",
true, 12);
319 test_num<double>(
"123",
true, 123);
320 test_num<double>(
"12345",
true, 12345);
321 test_num<double>(
"12341234123412341234123412341234123412341234123412341",
323 test_num<double>(
"99999999999999999999999912222222222222222222222222223" 324 "9999999999999999999999991222222222222222222222222222333333333333" 325 "9999999999999999999999991222222222222222222222222222333333333333" 326 "9999999999999999999999991222222222222222222222222222333333333333" 327 "9999999999999999999999991222222222222222222222222222333333333333" 328 "9999999999999999999999991222222222222222222222222222333333333333" 329 "9999999999999999999999991222222222222222222222222222333333333333" 330 "9999999999999999999999991222222222222222222222222222333333333333" 331 "9999999999999999999999991222222222222222222222222222333333333333" 332 "9999999999999999999999991222222222222222222222222222333333333333" 333 "9999999999999999999999991222222222222222222222222222333333333333" 334 "9999999999999999999999991222222222222222222222222222333333333333" 335 "9999999999999999999999991222222222222222222222222222333333333333" 336 "9999999999999999999999991222222222222222222222222222333333333333" 337 "9999999999999999999999991222222222222222222222222222333333333333",
339 test_num<double>(
"1e3",
true, 1000);
340 test_num<double>(
"1.2",
true, 1.2);
341 test_num<double>(
"1.2345e2",
true, 123.45);
342 test_num<double>(
"1 ",
false, 0);
343 test_num<double>(
" 123",
true, 123);
344 test_num<double>(
"1,2",
false, 0);
345 test_num<double>(
"123abc",
false, 0);
346 test_num<double>(
"-0",
true, 0);
348 test_num<short>(
"123",
true, 123);
349 test_num<short>(
"100000",
false, 0);
351 test_num<int>(
"123",
true, 123);
352 test_num<int>(
"2147483647",
true, 2147483647);
353 test_num<int>(
"2147483647a",
false, 0);
354 test_num<int>(
"2147483648",
false, 0);
358 test_num<int>(
"-2147483648",
true, -2147483647 - 1);
359 test_num<int>(
"-2147483649",
false, 0);
360 test_num<int>(
"-0",
true, 0);
361 test_num<int>(
"1.1",
false, 0);
363 test_num<long long>(
"123",
true, 123);
364 test_num<long long>(
"9223372036854775807",
true, 9223372036854775807LL);
365 test_num<long long>(
"9223372036854775808",
false, 0);
367 std::cout <<
"test 4 passed" << std::endl;
374 sql <<
"select maketime(19, 54, 52)",
into(t);
375 assert(t.tm_year == 100);
376 assert(t.tm_mon == 0);
377 assert(t.tm_mday == 1);
378 assert(t.tm_hour == 19);
379 assert(t.tm_min == 54);
380 assert(t.tm_sec == 52);
382 std::cout <<
"test 5 passed" << std::endl;
389 std::string a(
"asdfg\0hjkl", 10);
390 std::string b(
"lkjhg\0fd\0\0sa\0", 13);
391 std::string c(
"\\0aa\\0bb\\0cc\\0", 10);
393 std::string x(60000,
'X');
394 std::string y(60000,
'Y');
398 std::string z(800000,
'Z');
400 sql <<
"create table soci_test (id int, text_value text, " 401 "blob_value blob, longblob_value longblob)";
402 sql <<
"insert into soci_test values (1, \'foo\', \'bar\', \'baz\')";
403 sql <<
"insert into soci_test " 404 <<
"values (2, \'qwerty\\0uiop\', \'zxcv\\0bnm\', " 405 <<
"\'qwerty\\0uiop\\0zxcvbnm\\0\')";
406 sql <<
"insert into soci_test values (3, :a, :b, :c)",
408 sql <<
"insert into soci_test values (4, :x, :y, :z)",
411 std::vector<std::string> text_vec(100);
412 std::vector<std::string> blob_vec(100);
413 std::vector<std::string> longblob_vec(100);
414 sql <<
"select text_value, blob_value, longblob_value " 415 <<
"from soci_test order by id",
417 assert(text_vec.size() == 4);
418 assert(blob_vec.size() == 4);
419 assert(longblob_vec.size() == 4);
420 assert(text_vec[0] ==
"foo");
421 assert(blob_vec[0] ==
"bar");
422 assert(longblob_vec[0] ==
"baz");
423 assert(text_vec[1] == std::string(
"qwerty\0uiop", 11));
424 assert(blob_vec[1] == std::string(
"zxcv\0bnm", 8));
425 assert(longblob_vec[1] == std::string(
"qwerty\0uiop\0zxcvbnm\0", 20));
426 assert(text_vec[2] == a);
427 assert(blob_vec[2] == b);
428 assert(longblob_vec[2] == c);
429 assert(text_vec[3] == x);
430 assert(blob_vec[3] == y);
431 assert(longblob_vec[3] == z);
433 std::string text,
blob, longblob;
434 sql <<
"select text_value, blob_value, longblob_value " 435 <<
"from soci_test where id = 1",
437 assert(text ==
"foo");
438 assert(blob ==
"bar");
439 assert(longblob ==
"baz");
440 sql <<
"select text_value, blob_value, longblob_value " 441 <<
"from soci_test where id = 2",
443 assert(text == std::string(
"qwerty\0uiop", 11));
444 assert(blob == std::string(
"zxcv\0bnm", 8));
445 assert(longblob == std::string(
"qwerty\0uiop\0zxcvbnm\0", 20));
446 sql <<
"select text_value, blob_value, longblob_value " 447 <<
"from soci_test where id = 3",
451 assert(longblob == c);
452 sql <<
"select text_value, blob_value, longblob_value " 453 <<
"from soci_test where id = 4",
457 assert(longblob == z);
460 (sql.
prepare <<
"select text_value, blob_value, longblob_value " 461 "from soci_test order by id");
463 assert(r->get_properties(0).get_data_type() ==
dt_string);
464 assert(r->get<std::string>(0) ==
"foo");
465 assert(r->get_properties(1).get_data_type() ==
dt_string);
466 assert(r->get<std::string>(1) ==
"bar");
467 assert(r->get_properties(2).get_data_type() ==
dt_string);
468 assert(r->get<std::string>(2) ==
"baz");
470 assert(r->get_properties(0).get_data_type() ==
dt_string);
471 assert(r->get<std::string>(0) == std::string(
"qwerty\0uiop", 11));
472 assert(r->get_properties(1).get_data_type() ==
dt_string);
473 assert(r->get<std::string>(1) == std::string(
"zxcv\0bnm", 8));
474 assert(r->get_properties(2).get_data_type() ==
dt_string);
475 assert(r->get<std::string>(2) ==
476 std::string(
"qwerty\0uiop\0zxcvbnm\0", 20));
478 assert(r->get_properties(0).get_data_type() ==
dt_string);
479 assert(r->get<std::string>(0) == a);
480 assert(r->get_properties(1).get_data_type() ==
dt_string);
481 assert(r->get<std::string>(1) == b);
482 assert(r->get_properties(2).get_data_type() ==
dt_string);
483 assert(r->get<std::string>(2) == c);
485 assert(r->get_properties(0).get_data_type() ==
dt_string);
486 assert(r->get<std::string>(0) == x);
487 assert(r->get_properties(1).get_data_type() ==
dt_string);
488 assert(r->get<std::string>(1) == y);
489 assert(r->get_properties(2).get_data_type() ==
dt_string);
490 assert(r->get<std::string>(2) == z);
492 assert(r == rs.
end());
494 sql <<
"drop table soci_test";
496 std::cout <<
"test 6 passed" << std::endl;
506 sql <<
"create table soci_test(val integer)";
517 for (
int i = 0; i != 10; i++)
519 sql <<
"insert into soci_test(val) values(:val)",
use(i);
523 "update soci_test set val = val + 1");
529 "delete from soci_test where val <= 5");
532 assert(st2.get_affected_rows() == 5);
535 std::cout <<
"test 7 passed" << std::endl;
549 <<
"insert into soci_test(val) values(:val)",
use(i));
564 std::vector<int> v(5);
565 sql <<
"select val from soci_test order by val",
into(v);
566 assert(v.size() == 3);
572 std::cout <<
"test 8 passed" << std::endl;
580 sql <<
"create table soci_test(val int unsigned)";
592 unsigned int mask = 0xffffff00;
593 sql <<
"insert into soci_test set val = " << mask;
604 std::cout <<
"test 9 passed" << std::endl;
613 sql <<
"set @day = '5'";
614 sql <<
"set @mm = 'december'";
615 sql <<
"set @year = '2012'";
616 sql <<
"select concat(@day,' ',@mm,' ',@year)",
into(r);
618 std::cout <<
"test 10 passed" << std::endl;
626 sql <<
"create table soci_test(val double)";
632 const std::string expectedError =
633 "Use element used with infinity or NaN, which are " 634 "not supported by the MySQL server.";
638 double x = std::numeric_limits<double>::quiet_NaN();
643 if (e.what() != expectedError) {
651 double x = std::numeric_limits<double>::infinity();
656 if (e.what() != expectedError) {
665 std::vector<double> v(1, std::numeric_limits<double>::quiet_NaN());
667 sql <<
"insert into soci_test (val) values (:val)",
use(v);
669 if (e.what() != expectedError) {
678 std::vector<double> v(1, std::numeric_limits<double>::infinity());
680 sql <<
"insert into soci_test (val) values (:val)",
use(v);
682 if (e.what() != expectedError) {
688 std::cout <<
"test 11 passed" << std::endl;
696 sql <<
"create table soci_test(val tinyint)";
705 sql <<
"create table soci_test(val tinyint unsigned)";
714 unsigned int mask = 0xffffff00;
715 sql <<
"insert into soci_test set val = " << mask;
717 sql <<
"select val from soci_test",
into(r);
718 assert(r.
size() == 1);
720 assert(r.
get<
long long>(
"val") == 0xffffff00);
721 assert(r.
get<
unsigned>(
"val") == 0xffffff00);
726 sql <<
"insert into soci_test set val = -123";
728 sql <<
"select val from soci_test",
into(r);
729 assert(r.
size() == 1);
731 assert(r.
get<
int>(
"val") == -123);
736 sql <<
"insert into soci_test set val = 123";
738 sql <<
"select val from soci_test",
into(r);
739 assert(r.
size() == 1);
741 assert(r.
get<
int>(
"val") == 123);
746 sql <<
"insert into soci_test set val = 123456789012345";
748 sql <<
"select val from soci_test",
into(r);
749 assert(r.
size() == 1);
751 assert(r.
get<
unsigned long long>(
"val") == 123456789012345ULL);
756 sql <<
"insert into soci_test set val = -123456789012345";
758 sql <<
"select val from soci_test",
into(r);
759 assert(r.
size() == 1);
761 assert(r.
get<
long long>(
"val") == -123456789012345LL);
764 std::cout <<
"test 12 passed" << std::endl;
772 sql <<
"create table soci_test(s1 char(20), s2 varchar(20), " 773 "s3 tinytext, s4 mediumtext, s5 text, s6 longtext, " 774 "b1 binary(20), b2 varbinary(20), b3 tinyblob, b4 mediumblob, " 775 "b5 blob, b6 longblob, e1 enum ('foo', 'bar', 'baz'))";
784 std::string text =
"Ala ma kota.";
785 std::string binary(
"Ala\0ma\0kota.........", 20);
786 sql <<
"insert into soci_test " 787 "(s1, s2, s3, s4, s5, s6, b1, b2, b3, b4, b5, b6, e1) values " 788 "(:s1, :s2, :s3, :s4, :d5, :s6, :b1, :b2, :b3, :b4, :b5, :b6, " 794 sql <<
"select s1, s2, s3, s4, s5, s6, b1, b2, b3, b4, b5, b6, e1 " 795 "from soci_test",
into(r);
796 assert(r.
size() == 13);
797 for (
int i = 0; i < 13; i++) {
800 assert(r.
get<std::string>(i) == text);
802 assert(r.
get<std::string>(i) == binary);
804 assert(r.
get<std::string>(i) ==
"foo");
809 std::cout <<
"test 13 passed" << std::endl;
816 char* escaped =
new char[2 * s.size() + 1];
817 mysql_real_escape_string(backend->
conn_, escaped, s.data(), s.size());
818 std::string retv = escaped;
828 std::string s =
"word1'word2:word3";
830 std::string query =
"insert into soci_test (s5) values ('";
831 query.append(escaped);
835 sql <<
"select s5 from soci_test",
into(s2);
839 std::cout <<
"test 14 passed" << std::endl;
847 sql <<
"select @a := 123",
into(n);
851 std::cout <<
"test 15 passed" << std::endl;
860 sql <<
"create table soci_test(id integer, val integer, c char, " 861 "str varchar(20), sh int2, ul numeric(20), d float8, " 862 "tm datetime, i1 integer, i2 integer, i3 integer, " 863 "name varchar(20)) engine=InnoDB";
872 sql <<
"create table soci_test(num_float float8, num_int integer," 873 " name varchar(20), sometime datetime, chr char)";
882 sql <<
"create table soci_test(name varchar(100) not null, " 883 "phone varchar(15))";
892 sql <<
"create table soci_test(val integer)";
929 return "\'" + datdt_string +
"\'";
937 sql <<
"drop table if exists soci_test";
938 sql <<
"create table soci_test (id int) engine=InnoDB";
940 sql <<
"show table status like \'soci_test\'",
into(r);
941 bool retv = (r.
get<std::string>(1) ==
"InnoDB");
942 sql <<
"drop table soci_test";
946 int main(
int argc,
char** argv)
954 std::cout <<
"usage: " << argv[0]
955 <<
" connectstring\n" 956 <<
"example: " << argv[0]
957 <<
" \"dbname=test user=root password=\'Ala ma kota\'\"\n";
966 tests.
run(checkTransactions);
968 std::cout <<
"\nSOCI MySQL Tests:\n\n";
982 if (std::numeric_limits<double>::is_iec559) {
985 std::cout <<
"Skipping test11 " 986 <<
"(C++ implementation's double type is not IEC-559)\n";
993 std::cout <<
"\nOK, all tests passed.\n\n";
996 catch (std::exception
const & e)
998 std::cout << e.what() <<
'\n';
1001 return EXIT_FAILURE;
column_properties const & get_properties(std::size_t pos) const
const_iterator begin() const
T get(std::size_t pos) const
details::into_container< T, details::no_indicator > into(T &t)
std::string escape_string(soci::session &sql, const std::string &s)
tinyint_unsigned_value_table_creator(session &sql)
test_context(backend_factory const &backEnd, std::string const &connectString)
std::string connectString
table_creator_base * table_creator_2(session &s) const
data_type get_data_type() const
void run(bool dbSupportsTransactions=true)
details::session_backend * get_backend()
unsigned_value_table_creator(session &sql)
bool are_transactions_supported()
table_creator_one(session &sql)
table_creator_base * table_creator_4(session &s) const
const_iterator end() const
SOCI_MYSQL_DECL backend_factory const * factory_mysql()
bool execute(bool withDataExchange=false)
backend_factory const & backEnd
long long get_affected_rows()
tinyint_value_table_creator(session &sql)
table_creator_base * table_creator_1(session &s) const
table_creator_base * table_creator_3(session &s) const
integer_value_table_creator(session &sql)
table_creator_for_get_affected_rows(session &sql)
void test_num(const char *s, bool valid, T value)
bigint_unsigned_table_creator(session &sql)
std::string to_date_time(std::string const &datdt_string) const
table_creator_two(session &sql)
int main(int argc, char **argv)
double_value_table_creator(session &sql)
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())
bigint_table_creator(session &sql)
bool execute(bool withDataExchange=false)
strings_table_creator(session &sql)