diff options
Diffstat (limited to 'odb-tests/mysql')
25 files changed, 1818 insertions, 0 deletions
diff --git a/odb-tests/mysql/custom/buildfile b/odb-tests/mysql/custom/buildfile new file mode 100644 index 0000000..f18b16b --- /dev/null +++ b/odb-tests/mysql/custom/buildfile @@ -0,0 +1,40 @@ +# file : mysql/custom/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} + +import libs = libodb-mysql%lib{odb-mysql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix mysql_custom_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --hxx-prologue '#include "traits.hxx"' \ + --hxx-prologue '#include "query.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/custom/driver.cxx b/odb-tests/mysql/custom/driver.cxx new file mode 100644 index 0000000..526dbdc --- /dev/null +++ b/odb-tests/mysql/custom/driver.cxx @@ -0,0 +1,117 @@ +// file : mysql/custom/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in MySQL. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + object o (1); + o.p = point (1.1111, 2222222222.2); + o.pv.push_back (point (1.1234, 2.2345)); + o.pv.push_back (point (3.3456, 4.4567)); + // VC just cannot roundtrip this. +#ifndef _MSC_VER + o.pv.push_back (point (0.0000001, 0.000000001)); // Scientific notation. +#endif + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef mysql::query<object> query; + typedef odb::result<object> result; + + { + transaction t (db->begin ()); + + // Point comparison. + // + { + result r (db->query<object> (query::p == o.p)); + assert (!r.empty ()); + } + + // Point comparison using native query. + // + { + result r (db->query<object> (query::p + "=" + query::_val (o.p))); + assert (!r.empty ()); + } + + // Access to individual members. + // + { + result r (db->query<object> (query::p.x == o.p.x)); + assert (!r.empty ()); + } + + t.commit (); + } + + // Update. + // + o.p.x++; + o.p.y--; + o.pv[1].x--; + o.pv[1].y++; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/mysql/custom/query.hxx b/odb-tests/mysql/custom/query.hxx new file mode 100644 index 0000000..2fa8f73 --- /dev/null +++ b/odb-tests/mysql/custom/query.hxx @@ -0,0 +1,160 @@ +// file : mysql/custom/query.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef QUERY_HXX +#define QUERY_HXX + +#include <string> + +#include <odb/mysql/query.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace mysql + { + template <> + struct query_column<point, id_string> + { + private: + const char* table_; + const char* column_; + const char* conversion_; + + std::string x_table_; + std::string y_table_; + std::string s_column_; + + // Sub-columns for individual members. + // + public: + query_column<double, id_double> x, y; + + // is_null, is_not_null + // + public: + query_base + is_null () const + { + query_base q (table_, column_); + q += "IS NULL"; + return q; + } + + query_base + is_not_null () const + { + query_base q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + // = + // + public: + query_base + equal (const point& v) const + { + return equal (val_bind<point> (v)); + } + + query_base + equal (val_bind<point> v) const + { + query_base q (table_, column_); + q += "="; + q.append<point, id_string> (v, conversion_); + return q; + } + + query_base + equal (ref_bind<point> r) const + { + query_base q (table_, column_); + q += "="; + q.append<point, id_string> (r, conversion_); + return q; + } + + friend query_base + operator== (const query_column& c, const point& v) + { + return c.equal (v); + } + + friend query_base + operator== (const point& v, const query_column& c) + { + return c.equal (v); + } + + friend query_base + operator== (const query_column& c, val_bind<point> v) + { + return c.equal (v); + } + + friend query_base + operator== (val_bind<point> v, const query_column& c) + { + return c.equal (v); + } + + friend query_base + operator== (const query_column& c, ref_bind<point> r) + { + return c.equal (r); + } + + friend query_base + operator== (ref_bind<point> r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query_base + operator== (const query_column<point, id_string>& c) const + { + query_base q (table_, column_); + q += "="; + q.append (c.table (), c.column ()); + return q; + } + + public: + query_column (const char* table, const char* column, const char* conv) + : table_ (table), column_ (column), conversion_ (conv), + x_table_ ("ST_X(" + std::string (table)), // @@ Not very clean. + y_table_ ("ST_Y(" + std::string (table)), + s_column_ (std::string (column) + ")"), // X & Y column suffix. + x (x_table_.c_str (), s_column_.c_str (), 0), + y (y_table_.c_str (), s_column_.c_str (), 0) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + const char* + conversion () const + { + return conversion_; + } + }; + } +} + +#endif // QUERY_HXX diff --git a/odb-tests/mysql/custom/test.hxx b/odb-tests/mysql/custom/test.hxx new file mode 100644 index 0000000..82cc59d --- /dev/null +++ b/odb-tests/mysql/custom/test.hxx @@ -0,0 +1,54 @@ +// file : mysql/custom/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <odb/core.hxx> + +// Map GEOMETRY MySQL type to the point C++ struct. The other half +// of this mapping is in traits.hxx (value_traits<point, id_string>). +// +#pragma db map type("GEOMETRY") \ + as("VARCHAR(256)") \ + to("ST_GeomFromText((?))") \ + from("ST_AsText((?))") + +#pragma db value type("GEOMETRY") +struct point +{ + point () {} + point (double x_, double y_): x (x_), y (y_) {} + + double x; + double y; +}; + +inline bool +operator== (const point& a, const point& b) +{ + return a.x == b.x && a.y == b.y; +} + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_) : id (id_) {} + + #pragma db id + unsigned long id; + + point p; + std::vector<point> pv; + + bool + operator== (const object& y) const + { + return id == y.id && p == y.p && pv == y.pv; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/mysql/custom/testscript b/odb-tests/mysql/custom/testscript new file mode 100644 index 0000000..9bc8839 --- /dev/null +++ b/odb-tests/mysql/custom/testscript @@ -0,0 +1,11 @@ +# file : mysql/custom/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/mysql/custom/traits.hxx b/odb-tests/mysql/custom/traits.hxx new file mode 100644 index 0000000..5386d86 --- /dev/null +++ b/odb-tests/mysql/custom/traits.hxx @@ -0,0 +1,88 @@ +// file : mysql/types/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <limits> // std::numeric_limits +#include <sstream> +#include <cstring> // std::memcpy + +#include <odb/mysql/traits.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace mysql + { + template <> + class value_traits<point, id_string> + { + public: + typedef point value_type; + typedef point query_type; + + typedef char* image_type; + + static void + set_value (point& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (is_null) + v = point (); + else + { + // Point format is "POINT(x y)". + // + std::istringstream is (std::string (b.data () + 6, n - 6)); + + is >> v.x; + is >> v.y; + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const point& v) + { + is_null = false; + std::ostringstream os; + + // The formula for the number of decimla digits required is given in: + // + // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf + // + os.precision (std::numeric_limits<double>::digits10); + // os.precision (2 + std::numeric_limits<double>::digits * 301/1000); + + os << "POINT(" << v.x << ' ' << v.y << ')'; + + const std::string& s (os.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), s.c_str (), n); + } + }; + + template <> + struct type_traits<point> + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "ST_GeomFromText((?))";} + }; + }; + } +} + +#endif // TRAITS_HXX diff --git a/odb-tests/mysql/database/buildfile b/odb-tests/mysql/database/buildfile new file mode 100644 index 0000000..5731f6f --- /dev/null +++ b/odb-tests/mysql/database/buildfile @@ -0,0 +1,14 @@ +# file : mysql/database/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libs = libodb-mysql%lib{odb-mysql} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/mysql/database/driver.cxx b/odb-tests/mysql/database/driver.cxx new file mode 100644 index 0000000..525ee87 --- /dev/null +++ b/odb-tests/mysql/database/driver.cxx @@ -0,0 +1,72 @@ +// file : mysql/database/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test that database constructors are unambiguous (compilation only). +// + +#include <string> + +#include <odb/mysql/database.hxx> + +#undef NDEBUG +#include <cassert> + +using std::string; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + // This code should not execute. + // + if (argc != 0) + return 0; + + { + database d1 (0, 0, 0); + database d2 ("bob", "secret", "db1"); + database d3 ("bob", "secret", "db1", "server1"); + database d4 ("bob", "secret", "db1", "server1", 999); + database d5 ("bob", "secret", "db1", "server1", 999, "sock1"); + database d6 ("bob", "secret", "db1", "server1", 999, "sock1", "charset1"); + } + + std::string u ("bob"), p ("secret"), db ("bd1"), h ("server1"), + s ("sock1"), cs ("charset1"); + + { + database d1 (u, p, db); + database d2 (u, p, db, h); + database d3 (u, p, db, h, 999); + database d4 (u, p, db, h, 999, &s); + database d5 (u, p, db, h, 999, &s, cs); + } + + { + database d1 (u, 0, db); + database d2 (u, &p, db); + database d3 (u, &p, db, h); + database d4 (u, &p, db, h, 999); + database d5 (u, &p, db, h, 999, &s); + database d6 (u, &p, db, h, 999, &s, cs); + } + + { + database d1 (u, p, db, h, 999, "socket1"); + database d2 (u, p, db, h, 999, s); + database d3 (u, p, db, h, 999, s, cs); + } + + { + database d1 (u, 0, db, h, 999, s); + database d2 (u, &p, db, h, 999, "socket1"); + database d3 (u, &p, db, h, 999, s, cs); + } + + { + database d1 (argc, argv); + database d2 (argc, argv, false); + database d3 (argc, argv, true, "charset1"); + } +} diff --git a/odb-tests/mysql/database/testscript b/odb-tests/mysql/database/testscript new file mode 100644 index 0000000..c2ff256 --- /dev/null +++ b/odb-tests/mysql/database/testscript @@ -0,0 +1,6 @@ +# file : mysql/database/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +: basics +: +$* diff --git a/odb-tests/mysql/index/buildfile b/odb-tests/mysql/index/buildfile new file mode 100644 index 0000000..0ad9d8f --- /dev/null +++ b/odb-tests/mysql/index/buildfile @@ -0,0 +1,37 @@ +# file : mysql/index/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} + +import libs = libodb-mysql%lib{odb-mysql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix mysql_index_ \ + --generate-schema \ + --default-database common + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/index/driver.cxx b/odb-tests/mysql/index/driver.cxx new file mode 100644 index 0000000..4d0b7ad --- /dev/null +++ b/odb-tests/mysql/index/driver.cxx @@ -0,0 +1,44 @@ +// file : mysql/index/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test MySQL index creation. See also the common test. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + try + { + // This is just a schema creation test. + // + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + { + transaction t (db->begin ()); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/mysql/index/test.hxx b/odb-tests/mysql/index/test.hxx new file mode 100644 index 0000000..b4f6ae8 --- /dev/null +++ b/odb-tests/mysql/index/test.hxx @@ -0,0 +1,20 @@ +// file : mysql/template/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <odb/core.hxx> + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id_; + + std::string s; + #pragma db index method("BTREE") member(s, "(200) DESC") +}; + +#endif // TEST_HXX diff --git a/odb-tests/mysql/index/testscript b/odb-tests/mysql/index/testscript new file mode 100644 index 0000000..26dfc4f --- /dev/null +++ b/odb-tests/mysql/index/testscript @@ -0,0 +1,11 @@ +# file : mysql/index/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/mysql/native/buildfile b/odb-tests/mysql/native/buildfile new file mode 100644 index 0000000..1ae0212 --- /dev/null +++ b/odb-tests/mysql/native/buildfile @@ -0,0 +1,19 @@ +# file : mysql/native/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libs = libodb-mysql%lib{odb-mysql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/native/driver.cxx b/odb-tests/mysql/native/driver.cxx new file mode 100644 index 0000000..9b34fd2 --- /dev/null +++ b/odb-tests/mysql/native/driver.cxx @@ -0,0 +1,75 @@ +// file : mysql/native/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test native SQL execution. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + + db->execute ("DROP TABLE IF EXISTS mysql_native_test"); + + db->execute ("CREATE TABLE mysql_native_test (n INT PRIMARY KEY) " + "ENGINE=InnoDB"); + + t.commit (); + } + + // Insert a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("INSERT INTO mysql_native_test (n) VALUES (1)") == 1); + + assert ( + db->execute ("INSERT INTO mysql_native_test (n) VALUES (2)") == 1); + + t.commit (); + } + + // Select a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("SELECT n FROM mysql_native_test WHERE n < 3") == 2); + + assert ( + db->execute ("SELECT n FROM mysql_native_test WHERE n > 3") == 0); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/mysql/native/testscript b/odb-tests/mysql/native/testscript new file mode 100644 index 0000000..21327ff --- /dev/null +++ b/odb-tests/mysql/native/testscript @@ -0,0 +1,9 @@ +# file : mysql/native/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + +: basics +: +$* diff --git a/odb-tests/mysql/truncation/buildfile b/odb-tests/mysql/truncation/buildfile new file mode 100644 index 0000000..94c7429 --- /dev/null +++ b/odb-tests/mysql/truncation/buildfile @@ -0,0 +1,38 @@ +# file : mysql/truncation/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} + +import libs = libodb-mysql%lib{odb-mysql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix mysql_truncation_ \ + --generate-schema \ + --default-database common \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/truncation/driver.cxx b/odb-tests/mysql/truncation/driver.cxx new file mode 100644 index 0000000..21084f5 --- /dev/null +++ b/odb-tests/mysql/truncation/driver.cxx @@ -0,0 +1,192 @@ +// file : mysql/truncation/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test insufficient buffer/truncation handling. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/connection.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + // The default pre-allocated buffer is 256 bytes long. + // + string long_str (300, 'c'); // This will get the buffer to 512 + string longer_str (1025, 'b'); + + try + { + // Test basic operations. + // + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Run persist/load so that the initial bindings are established + // (version == 0). + // + { + object1 o (1); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (1, o); + t.commit (); + } + + { + object2 o (2); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (2, o); + t.commit (); + } + + // Store/load the long string which should trigger buffer growth. + // + { + object1 o (3); + o.str_ = long_str; + + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == long_str); + t.commit (); + } + + // Store/load longer string. + // + { + object1 o (3); + o.str_ = longer_str; + + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } + + // Test query. + // + { + typedef mysql::query<object1> query; + typedef odb::result<object1> result; + + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Run persist/query so that the initial bindings are established + // (version == 0). + // + { + object1 o (20); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + o.id_++; + db->persist (o); + o.id_++; + db->persist (o); + + result r (db->query<object1> (query::id == 20)); + assert (r.begin ()->id_ == 20); + t.commit (); + } + + // Test buffer growth with cached result. + // + { + object1 o; + + transaction t (db->begin ()); + + result r (db->query<object1> (query::id >= 20)); + result::iterator i (r.begin ()); + + o.id_ = i->id_; + o.str_ = long_str; + + // This forces buffer growth in the middle of result iteration. + // + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + o.id_ = i->id_; + o.str_ = longer_str; + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + t.commit (); + } + } + + // Test containers. + // + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + // Use different connections to persist and load the object. + // + connection_ptr c1 (db->connection ()); + connection_ptr c2 (db->connection ()); + + container o (1); + o.vec_.push_back (string (513, 'x')); + + { + transaction t (c1->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (c2->begin ()); + unique_ptr<container> p (db->load<container> (1)); + t.commit (); + + assert (p->vec_ == o.vec_); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/mysql/truncation/test.hxx b/odb-tests/mysql/truncation/test.hxx new file mode 100644 index 0000000..1883b0e --- /dev/null +++ b/odb-tests/mysql/truncation/test.hxx @@ -0,0 +1,48 @@ +// file : mysql/truncation/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +#pragma db object table ("test") +struct object1 +{ + object1 () {} + object1 (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#pragma db object table ("test") +struct object2 +{ + object2 () {} + object2 (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#pragma db object +struct container +{ + container () {} + container (unsigned long id) : id_ (id) {} + + #pragma db id + unsigned long id_; + + std::vector<std::string> vec_; +}; + +#endif // TEST_HXX diff --git a/odb-tests/mysql/truncation/testscript b/odb-tests/mysql/truncation/testscript new file mode 100644 index 0000000..b9b6792 --- /dev/null +++ b/odb-tests/mysql/truncation/testscript @@ -0,0 +1,11 @@ +# file : mysql/truncation/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/mysql/types/buildfile b/odb-tests/mysql/types/buildfile new file mode 100644 index 0000000..9065b7d --- /dev/null +++ b/odb-tests/mysql/types/buildfile @@ -0,0 +1,39 @@ +# file : mysql/types/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($mysql) "mysql should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import libodb = libodb%lib{odb} + +import libs = libodb-mysql%lib{odb-mysql} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix mysql_types_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --hxx-prologue '#include "traits.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../alias{mysql-client}: include = adhoc diff --git a/odb-tests/mysql/types/driver.cxx b/odb-tests/mysql/types/driver.cxx new file mode 100644 index 0000000..2354b04 --- /dev/null +++ b/odb-tests/mysql/types/driver.cxx @@ -0,0 +1,168 @@ +// file : mysql/types/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test MySQL type conversion. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +namespace mysql = odb::mysql; +using namespace mysql; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + mysql_version v; + { + transaction t (db->begin ()); + db->query<mysql_version> ().begin ().load (v); + t.commit (); + } + + //cerr << "MySQL " << v.major << '.' << v.minor << '.' << v.release + // << " protocol " << v.protocol << endl; + + object o (1); + + o.bool_ = true; + o.schar_ = -123; + o.uchar_ = 123; + o.short_ = -12345; + o.ushort_ = 12345; + o.mint_ = -123456; + o.umint_ = 123456; + o.int_ = -123456; + o.uint_ = 123456; + o.long_long_ = -123456; + o.ulong_long_ = 123456; + + o.float_ = 1.123F; + o.float8_ = 1.123; + o.double_ = 1.123; + o.decimal_ = "123.456"; + + o.date_ = date_time (false, 2010, 8, 29, 0, 0, 0); + o.time_ = date_time (true, 0, 0, 0, 12, 26, 59); + o.date_time_ = date_time (false, 2010, 8, 29, 12, 26, 59); + o.timestamp_ = date_time (false, 2010, 8, 29, 12, 26, 59); + o.year_ = 2010; + + // If we are running against MySQL 5.6.4 or later, add fractional + // seconds and also alter the table to allow sub-second precision. + // + if (v.major > 5 || + (v.major == 5 && (v.minor > 6 || + (v.minor == 6 && v.release >= 4)))) + { + o.time_.microseconds = 123456; + o.date_time_.microseconds = 234567; + o.timestamp_.microseconds = 345678; + + transaction t (db->begin ()); + db->execute ("ALTER TABLE `mysql_types_object`" \ + " MODIFY COLUMN `time` TIME(6)," \ + " MODIFY COLUMN `date_time` DATETIME(6)," \ + " MODIFY COLUMN `timestamp` TIMESTAMP(6)"); + t.commit (); + } + + string short_str (128, 's'); + string medium_str (250, 'm'); + string long_str (2040, 'l'); + + const char* sb (short_str.c_str ()), *se (sb + short_str.size ()); + const char* mb (medium_str.c_str ()), *me (mb + medium_str.size ()); + const char* lb (long_str.c_str ()), *le (lb + long_str.size ()); + + o.char_ = short_str; + o.binary_.assign (sb, se); + o.varchar_ = medium_str; + o.varbinary_.assign (mb, me); + o.tinytext_ = short_str; + o.tinyblob_.assign (sb, se); + o.text_ = long_str; + o.blob_.assign (lb, le); + o.mediumtext_ = long_str; + o.mediumblob_.assign (lb, le); + o.longtext_ = long_str; + o.longblob_.assign (lb, le); + + o.bit_.a = 1; + o.bit_.b = 0; + o.bit_.c = 0; + o.bit_.d = 1; + + o.enum_def_ = green; + o.enum_cst_ = blue; + o.enum_str_ = "green"; + o.set_.insert ("green"); + o.set_.insert ("red"); + o.set_.insert ("blue"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // + // + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<char_array> p1 (db->load<char_array> (1)); + unique_ptr<char_array> p2 (db->load<char_array> (2)); + unique_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/mysql/types/test.hxx b/odb-tests/mysql/types/test.hxx new file mode 100644 index 0000000..6a8527b --- /dev/null +++ b/odb-tests/mysql/types/test.hxx @@ -0,0 +1,336 @@ +// file : mysql/types/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <set> +#include <string> +#include <vector> +#include <memory> // std::unique_ptr +#include <cstring> // std::memcpy, std::str[n]cmp, std::strlen + +#include <odb/core.hxx> + +typedef std::vector<char> buffer; + +struct date_time +{ + date_time () + : negative (false), + year (0), + month (0), + day (0), + hour (0), + minute (0), + second (0), + microseconds (0) + { + } + + date_time (bool n, + unsigned int y, + unsigned int m, + unsigned int d, + unsigned int h, + unsigned int min, + unsigned int sec, + unsigned int msec = 0) + : negative (n), + year (y), + month (m), + day (d), + hour (h), + minute (min), + second (sec), + microseconds (msec) + { + } + + bool + operator== (const date_time& y) const + { + return + negative == y.negative && + year == y.year && + month == y.month && + day == y.day && + hour == y.hour && + minute == y.minute && + second == y.second && + microseconds == y.microseconds; + } + + bool negative; + unsigned int year; + unsigned int month; + unsigned int day; + unsigned int hour; + unsigned int minute; + unsigned int second; + unsigned int microseconds; +}; + +struct bitfield +{ + unsigned int a: 1; + unsigned int b: 1; + unsigned int c: 1; + unsigned int d: 1; +}; + +inline bool +operator== (bitfield x, bitfield y) +{ + return + x.a == y.a && + x.b == y.b && + x.c == y.c && + x.d == y.d; +} + +#pragma db value(bitfield) type ("BIT(4)") + +typedef std::set<std::string> set; +typedef std::unique_ptr<std::string> string_ptr; + +enum color {red, green, blue}; + +#pragma db object +struct object +{ + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + // Integral types. + // + #pragma db type ("BOOL") + bool bool_; + + #pragma db type ("TINYINT") + signed char schar_; + + #pragma db type ("TINYINT UNSIGNED") + unsigned char uchar_; + + #pragma db type ("SMALLINT") + short short_; + + #pragma db type ("SMALLINT UNSIGNED") + unsigned short ushort_; + + #pragma db type ("MEDIUMINT") + int mint_; + + #pragma db type ("MEDIUMINT UNSIGNED") + unsigned int umint_; + + #pragma db type ("INT") + int int_; + + #pragma db type ("INT UNSIGNED") + unsigned int uint_; + + #pragma db type ("BIGINT") + long long long_long_; + + #pragma db type ("BIGINT UNSIGNED") + unsigned long long ulong_long_; + + // Float types. + // + #pragma db type ("FLOAT") + float float_; + + #pragma db type ("FLOAT(32)") + double float8_; + + #pragma db type ("DOUBLE") + double double_; + + #pragma db type ("DECIMAL(6,3)") + std::string decimal_; + + // Data-time types. + // + #pragma db type ("DATE") + date_time date_; + + #pragma db type ("TIME") + date_time time_; + + #pragma db type ("DATETIME") + date_time date_time_; + + #pragma db type ("TIMESTAMP") + date_time timestamp_; + + #pragma db type ("YEAR") + short year_; + + // String and binary types. + // + #pragma db type ("CHAR(128)") + std::string char_; + + #pragma db type ("BINARY(128)") + buffer binary_; + + #pragma db type ("VARCHAR(256)") + std::string varchar_; + + #pragma db type ("VARBINARY(256)") + buffer varbinary_; + + #pragma db type ("TINYTEXT") + std::string tinytext_; + + #pragma db type ("TINYBLOB") + buffer tinyblob_; + + #pragma db type ("TEXT") + std::string text_; + + #pragma db type ("BLOB") + buffer blob_; + + #pragma db type ("MEDIUMTEXT") + std::string mediumtext_; + + #pragma db type ("MEDIUMBLOB") + buffer mediumblob_; + + #pragma db type ("LONGTEXT") + std::string longtext_; + + #pragma db type ("LONGBLOB") + buffer longblob_; + + // Other types. + // + // #pragma db type ("BIT(4)") - assigned by #pragma db value + bitfield bit_; + + // Test ENUM representations (integer and string). + // + color enum_def_; + + // Map to a custom MySQL ENUM type. + // + #pragma db type ("ENUM('R', 'G', 'B')") + color enum_cst_; + + #pragma db type ("ENUM('red', 'green', 'blue')") + std::string enum_str_; + + #pragma db type ("SET('red', 'green', 'blue')") + set set_; + + // Test NULL value. + // + #pragma db type ("TEXT") null + string_ptr null_; + + bool + operator== (const object& y) const + { + return + id_ == y.id_ && + bool_ == y.bool_ && + schar_ == y.schar_ && + uchar_ == y.uchar_ && + short_ == y.short_ && + ushort_ == y.ushort_ && + mint_ == y.mint_ && + umint_ == y.umint_ && + int_ == y.int_ && + uint_ == y.uint_ && + long_long_ == y.long_long_ && + ulong_long_ == y.ulong_long_ && + float_ == y.float_ && + float8_ == y.float8_ && + double_ == y.double_ && + decimal_ == y.decimal_ && + date_ == y.date_ && + time_ == y.time_ && + date_time_ == y.date_time_ && + timestamp_ == y.timestamp_ && + year_ == y.year_ && + char_ == y.char_ && + binary_ == y.binary_ && + varchar_ == y.varchar_ && + varbinary_ == y.varbinary_ && + tinytext_ == y.tinytext_ && + tinyblob_ == y.tinyblob_ && + text_ == y.text_ && + blob_ == y.blob_ && + mediumtext_ == y.mediumtext_ && + mediumblob_ == y.mediumblob_ && + longtext_ == y.longtext_ && + longblob_ == y.longblob_ && + bit_ == y.bit_ && + enum_def_ == y.enum_def_ && + enum_cst_ == y.enum_cst_ && + enum_str_ == y.enum_str_ && + set_ == y.set_ && + ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_); + } +}; + +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation. + std::memcpy (s2, s, std::strlen (s) + 1); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1; + } +}; + +// MySQL server version view. +// +#pragma db view query( \ + "SELECT " \ + "CAST(SUBSTRING_INDEX(@@version, '.', 1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '.', 2), '.', -1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '-', 1), '.', -1) AS UNSIGNED)," \ + "@@protocol_version") +struct mysql_version +{ + unsigned int major; + unsigned int minor; + unsigned int release; + + unsigned int protocol; +}; + +#endif // TEST_HXX diff --git a/odb-tests/mysql/types/testscript b/odb-tests/mysql/types/testscript new file mode 100644 index 0000000..2962d1c --- /dev/null +++ b/odb-tests/mysql/types/testscript @@ -0,0 +1,11 @@ +# file : mysql/types/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../mysql.testscript + ++$create_schema + +: basics +: +$* diff --git a/odb-tests/mysql/types/traits.hxx b/odb-tests/mysql/types/traits.hxx new file mode 100644 index 0000000..d4ce200 --- /dev/null +++ b/odb-tests/mysql/types/traits.hxx @@ -0,0 +1,198 @@ +// file : mysql/types/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <cstring> // std::memcpy, std::memset + +#include <odb/mysql/traits.hxx> + +#include "test.hxx" // date_time, string_ptr + +namespace odb +{ + namespace mysql + { + template <database_type_id ID> + class value_traits<date_time, ID> + { + public: + typedef date_time value_type; + typedef date_time query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (date_time& v, const MYSQL_TIME& i, bool is_null) + { + if (!is_null) + { + v.negative = i.neg; + v.year = i.year; + v.month = i.month; + v.day = i.day; + v.hour = i.hour; + v.minute = i.minute; + v.second = i.second; + v.microseconds = static_cast<unsigned int> (i.second_part); + } + else + v = date_time (); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const date_time& v) + { + is_null = false; + i.neg = v.negative; + i.year = v.year; + i.month = v.month; + i.day = v.day; + i.hour = v.hour; + i.minute = v.minute; + i.second = v.second; + i.second_part = v.microseconds; + } + }; + + template <> + class value_traits<bitfield, id_bit> + { + public: + typedef bitfield value_type; + typedef bitfield query_type; + typedef unsigned char* image_type; + + static void + set_value (bitfield& v, + const unsigned char* s, + std::size_t, + bool is_null) + { + if (!is_null) + { + v.a = *s & 1; + v.b = (*s >> 1) & 1; + v.c = (*s >> 2) & 1; + v.d = (*s >> 3) & 1; + } + else + v.a = v.b = v.c = v.d = 0; + } + + static void + set_image (unsigned char* s, + std::size_t, + std::size_t& n, + bool& is_null, + bitfield v) + { + is_null = false; + n = 1; + *s = v.a | (v.b << 1) | (v.c << 2) | (v.d << 3); + } + }; + + template <> + class value_traits<set, id_set> + { + public: + typedef set value_type; + typedef set query_type; + typedef details::buffer image_type; + + static void + set_value (set& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.clear (); + + if (!is_null) + { + const char* s (b.data ()); + const char* e (s + n); + + while (s < e) + { + const char* p (s); + + while (p < e && *p != ',') + ++p; + + v.insert (std::string (s, p - s)); + s = p; + + if (p != e) + ++s; + } + } + } + + static void + set_image (details::buffer& buf, + std::size_t& n, + bool& is_null, + const set& v) + { + is_null = false; + n = 0; + + for (set::const_iterator b (v.begin ()), i (b); i != v.end (); ++i) + { + std::size_t m (i->size () + (i != b ? 1 : 0)); + + if (n + m > buf.capacity ()) + buf.capacity (n + m, n); + + if (i != b) + buf.data ()[n++] = ','; + + std::memcpy (buf.data () + n, i->c_str (), i->size ()); + n += i->size (); + } + } + }; + + template <> + class value_traits<string_ptr, id_string> + { + public: + typedef string_ptr value_type; + typedef std::string query_type; + typedef details::buffer image_type; + + static void + set_value (string_ptr& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.reset (is_null ? 0 : new std::string (b.data (), n)); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const string_ptr& v) + { + is_null = v.get () == 0; + + if (!is_null) + { + n = v->size (); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + std::memcpy (b.data (), v->c_str (), n); + } + } + }; + } +} + +#endif // TRAITS_HXX |