test-mysql.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
3 // MySQL backend copyright (C) 2006 Pawel Aleksander Fedorynski
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #include "soci.h"
10 #include "soci-mysql.h"
11 #include "test/common-tests.h"
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 #include <cassert>
17 #include <cmath>
18 #include <ctime>
19 #include <ciso646>
20 #include <cstdlib>
21 #include <mysqld_error.h>
22 #include <errmsg.h>
23 
24 using namespace soci;
25 using namespace soci::tests;
26 
27 std::string connectString;
29 
30 
31 // procedure call test
32 void test1()
33 {
34  {
35  session sql(backEnd, connectString);
36 
37  mysql_session_backend *sessionBackEnd
38  = static_cast<mysql_session_backend *>(sql.get_backend());
39  std::string version = mysql_get_server_info(sessionBackEnd->conn_);
40  int v;
41  std::istringstream iss(version);
42  if ((iss >> v) and v < 5)
43  {
44  std::cout << "skipping test 1 (MySQL server version ";
45  std::cout << version << " does not support stored procedures)\n";
46  return;
47  }
48 
49  try { sql << "drop function myecho"; }
50  catch (soci_error const &) {}
51 
52  sql <<
53  "create function myecho(msg text) "
54  "returns text deterministic "
55  " return msg; ";
56 
57  std::string in("my message");
58  std::string out;
59 
60  statement st = (sql.prepare <<
61  "select myecho(:input)",
62  into(out),
63  use(in, "input"));
64 
65  st.execute(1);
66  assert(out == in);
67 
68  // explicit procedure syntax
69  {
70  std::string in("my message2");
71  std::string out;
72 
73  procedure proc = (sql.prepare <<
74  "myecho(:input)",
75  into(out), use(in, "input"));
76 
77  proc.execute(1);
78  assert(out == in);
79  }
80 
81  sql << "drop function myecho";
82  }
83 
84  std::cout << "test 1 passed" << std::endl;
85 }
86 
87 // MySQL error reporting test.
88 void test2()
89 {
90  {
91  try
92  {
93  session sql(backEnd, "host=test.soci.invalid");
94  }
95  catch (mysql_soci_error const &e)
96  {
97  assert(e.err_num_ == CR_UNKNOWN_HOST ||
98  e.err_num_ == CR_CONN_HOST_ERROR);
99  }
100  }
101 
102  {
103  session sql(backEnd, connectString);
104  sql << "create table soci_test (id integer)";
105  try
106  {
107  int n;
108  sql << "select id from soci_test_nosuchtable", into(n);
109  }
110  catch (mysql_soci_error const &e)
111  {
112  assert(e.err_num_ == ER_NO_SUCH_TABLE);
113  }
114  try
115  {
116  sql << "insert into soci_test (invalid) values (256)";
117  }
118  catch (mysql_soci_error const &e)
119  {
120  assert(e.err_num_ == ER_BAD_FIELD_ERROR);
121  }
122  // A bulk operation.
123  try
124  {
125  std::vector<int> v(3, 5);
126  sql << "insert into soci_test_nosuchtable values (:n)", use(v);
127  }
128  catch (mysql_soci_error const &e)
129  {
130  assert(e.err_num_ == ER_NO_SUCH_TABLE);
131  }
132  sql << "drop table soci_test";
133  }
134 
135  std::cout << "test 2 passed" << std::endl;
136 }
137 
139 {
141  : table_creator_base(sql)
142  {
143  sql << "create table soci_test(val bigint)";
144  }
145 };
146 
148 {
150  : table_creator_base(sql)
151  {
152  sql << "create table soci_test(val bigint unsigned)";
153  }
154 };
155 
156 // long long test
157 void test3()
158 {
159  {
160  session sql(backEnd, connectString);
161 
162  bigint_table_creator tableCreator(sql);
163 
164  long long v1 = 1000000000000LL;
165  assert(v1 / 1000000 == 1000000);
166 
167  sql << "insert into soci_test(val) values(:val)", use(v1);
168 
169  long long v2 = 0LL;
170  sql << "select val from soci_test", into(v2);
171 
172  assert(v2 == v1);
173  }
174 
175  // vector<long long>
176  {
177  session sql(backEnd, connectString);
178 
179  bigint_table_creator tableCreator(sql);
180 
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);
187 
188  sql << "insert into soci_test(val) values(:val)", use(v1);
189 
190  std::vector<long long> v2(10);
191  sql << "select val from soci_test order by val desc", into(v2);
192 
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);
199  }
200 
201  {
202  session sql(backEnd, connectString);
203 
204  bigint_unsigned_table_creator tableCreator(sql);
205 
206  sql << "insert into soci_test set val = 18446744073709551615";
207  row v;
208  sql << "select * from soci_test", into(v);
209  }
210 
211  {
212  session sql(backEnd, connectString);
213 
214  bigint_unsigned_table_creator tableCreator(sql);
215 
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;
221  buf << vv;
222  assert(buf.str() == source);
223  }
224 
225  {
226  session sql(backEnd, connectString);
227 
228  bigint_unsigned_table_creator tableCreator(sql);
229 
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;
235  buf << v.at(0);
236  assert(buf.str() == source);
237  }
238 
239  {
240  session sql(backEnd, connectString);
241 
242  bigint_unsigned_table_creator tableCreator(sql);
243 
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);
248  assert(n == m);
249  }
250 
251  {
252  session sql(backEnd, connectString);
253 
254  bigint_unsigned_table_creator tableCreator(sql);
255 
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);
261 
262  std::vector<unsigned long long> v2(10);
263  sql << "select val from soci_test order by val", into(v2);
264 
265  assert(v2.size() == 3);
266  assert(v2[0] == 18446744073709551613ULL);
267  assert(v2[1] == 18446744073709551614ULL);
268  assert(v2[2] == 18446744073709551615ULL);
269  }
270 
271  std::cout << "test 3 passed" << std::endl;
272 }
273 
274 template <typename T>
275 void test_num(const char* s, bool valid, T value)
276 {
277  try
278  {
279  session sql(backEnd, connectString);
280  T val;
281  sql << "select \'" << s << "\'", into(val);
282  if (valid)
283  {
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)));
290  }
291  else
292  {
293  std::cout << "string \"" << s << "\" parsed as " << val
294  << " but should have failed.\n";
295  assert(false);
296  }
297  }
298  catch (soci_error const& e)
299  {
300  if (valid)
301  {
302  std::cout << "couldn't parse number: \"" << s << "\"\n";
303  assert(false);
304  }
305  else
306  {
307  assert(std::string(e.what()) == "Cannot convert data.");
308  }
309  }
310 }
311 
312 // Number conversion test.
313 void test4()
314 {
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",
322  true, 1.23412e+52);
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",
338  false, 0);
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);
347 
348  test_num<short>("123", true, 123);
349  test_num<short>("100000", false, 0);
350 
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);
355  // -2147483648 causes a warning because it is interpreted as
356  // 2147483648 (which doesn't fit in an integer) to which a negation
357  // is applied.
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);
362 
363  test_num<long long>("123", true, 123);
364  test_num<long long>("9223372036854775807", true, 9223372036854775807LL);
365  test_num<long long>("9223372036854775808", false, 0);
366 
367  std::cout << "test 4 passed" << std::endl;
368 }
369 
370 void test5()
371 {
372  session sql(backEnd, connectString);
373  std::tm t;
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);
381 
382  std::cout << "test 5 passed" << std::endl;
383 }
384 
385 // TEXT and BLOB types support test.
386 void test6()
387 {
388  session sql(backEnd, connectString);
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);
392  // The maximum length for TEXT and BLOB is 65536.
393  std::string x(60000, 'X');
394  std::string y(60000, 'Y');
395  // The default max_allowed_packet value for a MySQL server is 1M,
396  // so let's limit ourselves to 800k, even though the maximum length
397  // for LONGBLOB is 4G.
398  std::string z(800000, 'Z');
399 
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)",
407  use(a), use(b), use(c);
408  sql << "insert into soci_test values (4, :x, :y, :z)",
409  use(x), use(y), use(z);
410 
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",
416  into(text_vec), into(blob_vec), into(longblob_vec);
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);
432 
433  std::string text, blob, longblob;
434  sql << "select text_value, blob_value, longblob_value "
435  << "from soci_test where id = 1",
436  into(text), into(blob), into(longblob);
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",
442  into(text), into(blob), into(longblob);
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",
448  into(text), into(blob), into(longblob);
449  assert(text == a);
450  assert(blob == b);
451  assert(longblob == c);
452  sql << "select text_value, blob_value, longblob_value "
453  << "from soci_test where id = 4",
454  into(text), into(blob), into(longblob);
455  assert(text == x);
456  assert(blob == y);
457  assert(longblob == z);
458 
459  rowset<row> rs =
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");
469  ++r;
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));
477  ++r;
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);
484  ++r;
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);
491  ++r;
492  assert(r == rs.end());
493 
494  sql << "drop table soci_test";
495 
496  std::cout << "test 6 passed" << std::endl;
497 }
498 
499 // test for number of affected rows
500 
502 {
504  : table_creator_base(sql)
505  {
506  sql << "create table soci_test(val integer)";
507  }
508 };
509 
510 void test7()
511 {
512  {
513  session sql(backEnd, connectString);
514 
515  integer_value_table_creator tableCreator(sql);
516 
517  for (int i = 0; i != 10; i++)
518  {
519  sql << "insert into soci_test(val) values(:val)", use(i);
520  }
521 
522  statement st1 = (sql.prepare <<
523  "update soci_test set val = val + 1");
524  st1.execute(false);
525 
526  assert(st1.get_affected_rows() == 10);
527 
528  statement st2 = (sql.prepare <<
529  "delete from soci_test where val <= 5");
530  st2.execute(false);
531 
532  assert(st2.get_affected_rows() == 5);
533  }
534 
535  std::cout << "test 7 passed" << std::endl;
536 }
537 
538 
539 // The prepared statements should survive session::reconnect().
540 void test8()
541 {
542  {
543  session sql(backEnd, connectString);
544 
545  integer_value_table_creator tableCreator(sql);
546 
547  int i;
548  statement st = (sql.prepare
549  << "insert into soci_test(val) values(:val)", use(i));
550  i = 5;
551  st.execute(true);
552 
553  sql.reconnect();
554 
555  i = 6;
556  st.execute(true);
557 
558  sql.close();
559  sql.reconnect();
560 
561  i = 7;
562  st.execute(true);
563 
564  std::vector<int> v(5);
565  sql << "select val from soci_test order by val", into(v);
566  assert(v.size() == 3);
567  assert(v[0] == 5);
568  assert(v[1] == 6);
569  assert(v[2] == 7);
570  }
571 
572  std::cout << "test 8 passed" << std::endl;
573 }
574 
576 {
578  : table_creator_base(sql)
579  {
580  sql << "create table soci_test(val int unsigned)";
581  }
582 };
583 
584 // rowset<> should be able to take INT UNSIGNED.
585 void test9()
586 {
587  {
588  session sql(backEnd, connectString);
589 
590  unsigned_value_table_creator tableCreator(sql);
591 
592  unsigned int mask = 0xffffff00;
593  sql << "insert into soci_test set val = " << mask;
594  soci::rowset<> rows(sql.prepare << "select val from soci_test");
595  int cnt = 0;
596  for (soci::rowset<>::iterator it = rows.begin(), end = rows.end();
597  it != end; ++it)
598  {
599  cnt++;
600  }
601  assert(cnt == 1);
602  }
603 
604  std::cout << "test 9 passed" << std::endl;
605 }
606 
607 void test10()
608 {
609  session sql(backEnd, connectString);
610 
611  row r;
612 
613  sql << "set @day = '5'";
614  sql << "set @mm = 'december'";
615  sql << "set @year = '2012'";
616  sql << "select concat(@day,' ',@mm,' ',@year)", into(r);
617 
618  std::cout << "test 10 passed" << std::endl;
619 }
620 
622 {
624  : table_creator_base(sql)
625  {
626  sql << "create table soci_test(val double)";
627  }
628 };
629 
630 void test11()
631 {
632  const std::string expectedError =
633  "Use element used with infinity or NaN, which are "
634  "not supported by the MySQL server.";
635  {
636  session sql(backEnd, connectString);
637 
638  double x = std::numeric_limits<double>::quiet_NaN();
639  statement st = (sql.prepare << "SELECT :x", use(x, "x"));
640  try {
641  st.execute(true);
642  } catch (soci_error const &e) {
643  if (e.what() != expectedError) {
644  throw;
645  }
646  }
647  }
648  {
649  session sql(backEnd, connectString);
650 
651  double x = std::numeric_limits<double>::infinity();
652  statement st = (sql.prepare << "SELECT :x", use(x, "x"));
653  try {
654  st.execute(true);
655  } catch (soci_error const &e) {
656  if (e.what() != expectedError) {
657  throw;
658  }
659  }
660  }
661  {
662  session sql(backEnd, connectString);
663  double_value_table_creator tableCreator(sql);
664 
665  std::vector<double> v(1, std::numeric_limits<double>::quiet_NaN());
666  try {
667  sql << "insert into soci_test (val) values (:val)", use(v);
668  } catch (soci_error const &e) {
669  if (e.what() != expectedError) {
670  throw;
671  }
672  }
673  }
674  {
675  session sql(backEnd, connectString);
676  double_value_table_creator tableCreator(sql);
677 
678  std::vector<double> v(1, std::numeric_limits<double>::infinity());
679  try {
680  sql << "insert into soci_test (val) values (:val)", use(v);
681  } catch (soci_error const &e) {
682  if (e.what() != expectedError) {
683  throw;
684  }
685  }
686  }
687 
688  std::cout << "test 11 passed" << std::endl;
689 }
690 
692 {
694  : table_creator_base(sql)
695  {
696  sql << "create table soci_test(val tinyint)";
697  }
698 };
699 
701 {
703  : table_creator_base(sql)
704  {
705  sql << "create table soci_test(val tinyint unsigned)";
706  }
707 };
708 
709 void test12()
710 {
711  {
712  session sql(backEnd, connectString);
713  unsigned_value_table_creator tableCreator(sql);
714  unsigned int mask = 0xffffff00;
715  sql << "insert into soci_test set val = " << mask;
716  row r;
717  sql << "select val from soci_test", into(r);
718  assert(r.size() == 1);
719  assert(r.get_properties("val").get_data_type() == dt_long_long);
720  assert(r.get<long long>("val") == 0xffffff00);
721  assert(r.get<unsigned>("val") == 0xffffff00);
722  }
723  {
724  session sql(backEnd, connectString);
725  tinyint_value_table_creator tableCreator(sql);
726  sql << "insert into soci_test set val = -123";
727  row r;
728  sql << "select val from soci_test", into(r);
729  assert(r.size() == 1);
730  assert(r.get_properties("val").get_data_type() == dt_integer);
731  assert(r.get<int>("val") == -123);
732  }
733  {
734  session sql(backEnd, connectString);
735  tinyint_unsigned_value_table_creator tableCreator(sql);
736  sql << "insert into soci_test set val = 123";
737  row r;
738  sql << "select val from soci_test", into(r);
739  assert(r.size() == 1);
740  assert(r.get_properties("val").get_data_type() == dt_integer);
741  assert(r.get<int>("val") == 123);
742  }
743  {
744  session sql(backEnd, connectString);
745  bigint_unsigned_table_creator tableCreator(sql);
746  sql << "insert into soci_test set val = 123456789012345";
747  row r;
748  sql << "select val from soci_test", into(r);
749  assert(r.size() == 1);
750  assert(r.get_properties("val").get_data_type() == dt_unsigned_long_long);
751  assert(r.get<unsigned long long>("val") == 123456789012345ULL);
752  }
753  {
754  session sql(backEnd, connectString);
755  bigint_table_creator tableCreator(sql);
756  sql << "insert into soci_test set val = -123456789012345";
757  row r;
758  sql << "select val from soci_test", into(r);
759  assert(r.size() == 1);
760  assert(r.get_properties("val").get_data_type() == dt_long_long);
761  assert(r.get<long long>("val") == -123456789012345LL);
762  }
763 
764  std::cout << "test 12 passed" << std::endl;
765 }
766 
768 {
770  : table_creator_base(sql)
771  {
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'))";
776  }
777 };
778 
779 void test13()
780 {
781  {
782  session sql(backEnd, connectString);
783  strings_table_creator tableCreator(sql);
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, "
789  "\'foo\')",
790  use(text), use(text), use(text), use(text), use(text), use(text),
791  use(binary), use(binary), use(binary), use(binary), use(binary),
792  use(binary);
793  row r;
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++) {
798  assert(r.get_properties(i).get_data_type() == dt_string);
799  if (i < 6) {
800  assert(r.get<std::string>(i) == text);
801  } else if (i < 12) {
802  assert(r.get<std::string>(i) == binary);
803  } else {
804  assert(r.get<std::string>(i) == "foo");
805  }
806  }
807  }
808 
809  std::cout << "test 13 passed" << std::endl;
810 }
811 
812 std::string escape_string(soci::session& sql, const std::string& s)
813 {
814  mysql_session_backend* backend = static_cast<mysql_session_backend*>(
815  sql.get_backend());
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;
819  delete [] escaped;
820  return retv;
821 }
822 
823 void test14()
824 {
825  {
826  session sql(backEnd, connectString);
827  strings_table_creator tableCreator(sql);
828  std::string s = "word1'word2:word3";
829  std::string escaped = escape_string(sql, s);
830  std::string query = "insert into soci_test (s5) values ('";
831  query.append(escaped);
832  query.append("')");
833  sql << query;
834  std::string s2;
835  sql << "select s5 from soci_test", into(s2);
836  assert(s == s2);
837  }
838 
839  std::cout << "test 14 passed" << std::endl;
840 }
841 
842 void test15()
843 {
844  {
845  session sql(backEnd, connectString);
846  int n;
847  sql << "select @a := 123", into(n);
848  assert(n == 123);
849  }
850 
851  std::cout << "test 15 passed" << std::endl;
852 }
853 
854 // DDL Creation objects for common tests
856 {
858  : table_creator_base(sql)
859  {
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";
864  }
865 };
866 
868 {
870  : table_creator_base(sql)
871  {
872  sql << "create table soci_test(num_float float8, num_int integer,"
873  " name varchar(20), sometime datetime, chr char)";
874  }
875 };
876 
878 {
880  : table_creator_base(sql)
881  {
882  sql << "create table soci_test(name varchar(100) not null, "
883  "phone varchar(15))";
884  }
885 };
886 
888 {
890  : table_creator_base(sql)
891  {
892  sql << "create table soci_test(val integer)";
893  }
894 };
895 
896 //
897 // Support for SOCI Common Tests
898 //
899 
900 class test_context : public test_context_base
901 {
902 public:
904  std::string const &connectString)
905  : test_context_base(backEnd, connectString) {}
906 
908  {
909  return new table_creator_one(s);
910  }
911 
913  {
914  return new table_creator_two(s);
915  }
916 
918  {
919  return new table_creator_three(s);
920  }
921 
923  {
925  }
926 
927  std::string to_date_time(std::string const &datdt_string) const
928  {
929  return "\'" + datdt_string + "\'";
930  }
931 
932 };
933 
935 {
936  session sql(backEnd, connectString);
937  sql << "drop table if exists soci_test";
938  sql << "create table soci_test (id int) engine=InnoDB";
939  row r;
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";
943  return retv;
944 }
945 
946 int main(int argc, char** argv)
947 {
948  if (argc == 2)
949  {
950  connectString = argv[1];
951  }
952  else
953  {
954  std::cout << "usage: " << argv[0]
955  << " connectstring\n"
956  << "example: " << argv[0]
957  << " \"dbname=test user=root password=\'Ala ma kota\'\"\n";
958  std::exit(1);
959  }
960 
961  try
962  {
963  test_context tc(backEnd, connectString);
964  common_tests tests(tc);
965  bool checkTransactions = are_transactions_supported();
966  tests.run(checkTransactions);
967 
968  std::cout << "\nSOCI MySQL Tests:\n\n";
969 
970  test1();
971  test2();
972  test3();
973  test4();
974  test5();
975  test6();
976  test7();
977  // Test 8 commented out because a bug in soci can make it crash
978  // https://github.com/SOCI/soci/issues/136
979  //test8();
980  test9();
981  test10();
982  if (std::numeric_limits<double>::is_iec559) {
983  test11();
984  } else {
985  std::cout << "Skipping test11 "
986  << "(C++ implementation's double type is not IEC-559)\n";
987  }
988  test12();
989  test13();
990  test14();
991  test15();
992 
993  std::cout << "\nOK, all tests passed.\n\n";
994  return EXIT_SUCCESS;
995  }
996  catch (std::exception const & e)
997  {
998  std::cout << e.what() << '\n';
999  }
1000 
1001  return EXIT_FAILURE;
1002 }
column_properties const & get_properties(std::size_t pos) const
Definition: row.cpp:91
const_iterator begin() const
Definition: rowset.h:220
void test8()
Definition: test-mysql.cpp:540
T get(std::size_t pos) const
Definition: row.h:66
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
std::string escape_string(soci::session &sql, const std::string &s)
Definition: test-mysql.cpp:812
tinyint_unsigned_value_table_creator(session &sql)
Definition: test-mysql.cpp:702
test_context(backend_factory const &backEnd, std::string const &connectString)
Definition: test-mysql.cpp:903
std::string connectString
Definition: test-mysql.cpp:27
table_creator_base * table_creator_2(session &s) const
Definition: test-mysql.cpp:912
data_type get_data_type() const
Definition: row.h:31
void run(bool dbSupportsTransactions=true)
Definition: common-tests.h:293
Definition: row.h:41
details::session_backend * get_backend()
Definition: session.h:118
unsigned_value_table_creator(session &sql)
Definition: test-mysql.cpp:577
std::size_t size() const
Definition: row.cpp:60
bool are_transactions_supported()
Definition: test-mysql.cpp:934
table_creator_one(session &sql)
Definition: test-mysql.cpp:857
table_creator_base * table_creator_4(session &s) const
Definition: test-mysql.cpp:922
const_iterator end() const
Definition: rowset.h:227
void test4()
Definition: test-mysql.cpp:313
void test12()
Definition: test-mysql.cpp:709
SOCI_MYSQL_DECL backend_factory const * factory_mysql()
bool execute(bool withDataExchange=false)
Definition: procedure.h:65
void test2()
Definition: test-mysql.cpp:88
void test6()
Definition: test-mysql.cpp:386
backend_factory const & backEnd
Definition: test-mysql.cpp:28
long long get_affected_rows()
Definition: statement.h:214
void test15()
Definition: test-mysql.cpp:842
tinyint_value_table_creator(session &sql)
Definition: test-mysql.cpp:693
void test5()
Definition: test-mysql.cpp:370
table_creator_base * table_creator_1(session &s) const
Definition: test-mysql.cpp:907
void test3()
Definition: test-mysql.cpp:157
table_creator_base * table_creator_3(session &s) const
Definition: test-mysql.cpp:917
integer_value_table_creator(session &sql)
Definition: test-mysql.cpp:503
table_creator_for_get_affected_rows(session &sql)
Definition: test-mysql.cpp:889
void test_num(const char *s, bool valid, T value)
Definition: test-mysql.cpp:275
bigint_unsigned_table_creator(session &sql)
Definition: test-mysql.cpp:149
std::string to_date_time(std::string const &datdt_string) const
Definition: test-mysql.cpp:927
void test9()
Definition: test-mysql.cpp:585
void test14()
Definition: test-mysql.cpp:823
table_creator_two(session &sql)
Definition: test-mysql.cpp:869
int main(int argc, char **argv)
Definition: test-mysql.cpp:946
double_value_table_creator(session &sql)
Definition: test-mysql.cpp:623
void test7()
Definition: test-mysql.cpp:510
void test13()
Definition: test-mysql.cpp:779
details::prepare_type prepare
Definition: session.h:69
table_creator_three(session &sql)
Definition: test-mysql.cpp:879
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
Definition: use.h:43
void test10()
Definition: test-mysql.cpp:607
void test1()
Definition: test-mysql.cpp:32
void test11()
Definition: test-mysql.cpp:630
bigint_table_creator(session &sql)
Definition: test-mysql.cpp:140
bool execute(bool withDataExchange=false)
Definition: statement.h:208
strings_table_creator(session &sql)
Definition: test-mysql.cpp:769
unsigned int err_num_
Definition: soci-mysql.h:44


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41