diff options
Diffstat (limited to 'odb-tests/mysql/custom')
-rw-r--r-- | odb-tests/mysql/custom/buildfile | 40 | ||||
-rw-r--r-- | odb-tests/mysql/custom/driver.cxx | 117 | ||||
-rw-r--r-- | odb-tests/mysql/custom/query.hxx | 160 | ||||
-rw-r--r-- | odb-tests/mysql/custom/test.hxx | 54 | ||||
-rw-r--r-- | odb-tests/mysql/custom/testscript | 11 | ||||
-rw-r--r-- | odb-tests/mysql/custom/traits.hxx | 88 |
6 files changed, 470 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 |