diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:16 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:16 +0200 |
commit | 6b8def06796d1e4fc9e6e7e75ce59bccf6899261 (patch) | |
tree | f7d440fda64cf07549173cefd5d45a92bf1845d6 | |
parent | 7ba8d1469e331f07040352c069cf7b9d24ac57a3 (diff) |
Add support for custom database type mapping
New pragma qualifier, map, and specifiers: as, to, from. New tests:
<database>/custom.
33 files changed, 2364 insertions, 0 deletions
diff --git a/mssql/custom/driver.cxx b/mssql/custom/driver.cxx new file mode 100644 index 0000000..7b0891d --- /dev/null +++ b/mssql/custom/driver.cxx @@ -0,0 +1,124 @@ +// file : mssql/custom/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in SQL Server. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/mssql/database.hxx> +#include <odb/mssql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + object o (1); + +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + 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)); + o.pv.push_back (point (0.0000001, 0.000000001)); // Scientific notation. +#endif + + o.xml = "<root x=\"1\"><a>AAA</a><b>BBB</b><c>CCC</c></root>"; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef odb::query<object> query; + typedef odb::result<object> result; + + { + transaction t (db->begin ()); + +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + // 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 + ".STEquals(" + query::_val (o.p) + ") = 1")); + assert (!r.empty ()); + } + + // Access to individual members. + // + { + result r (db->query<object> (query::p.x == o.p.x)); + assert (!r.empty ()); + } +#endif + + t.commit (); + } + + // Update. + // +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + o.p.x++; + o.p.y--; + o.pv[1].x--; + o.pv[1].y++; +#endif + + o.xml = "<root x=\"2\"><a>BBB</a><b>CCC</b><c>DDD</c></root>"; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_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/mssql/custom/makefile b/mssql/custom/makefile new file mode 100644 index 0000000..27f7823 --- /dev/null +++ b/mssql/custom/makefile @@ -0,0 +1,112 @@ +# file : mssql/custom/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database mssql --generate-schema \ +--generate-query --hxx-prologue '\#include "traits.hxx"' \ +--hxx-prologue '\#include "query.hxx"' --table-prefix mssql_custom_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export extra_headers := traits.hxx query.hxx +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/mssql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/mssql/custom/query.hxx b/mssql/custom/query.hxx new file mode 100644 index 0000000..21dee89 --- /dev/null +++ b/mssql/custom/query.hxx @@ -0,0 +1,184 @@ +// file : mssql/custom/query.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef QUERY_HXX +#define QUERY_HXX + +#include <string> + +#include <odb/mssql/query.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace mssql + { +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + template <> + struct query_column<point, id_string> + { + private: + const char* table_; + const char* column_; + const char* conversion_; + + unsigned short prec_; + unsigned short scale_; + + std::string x_column_; + std::string y_column_; + + // Sub-columns for individual members. + // + public: + query_column<double, id_float8> x, y; + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + // = + // + public: + query + equal (const point& v) const + { + return equal (val_bind<point> (v)); + } + + query + equal (val_bind<point> v) const + { + query q (table_, column_); + q += ".STEquals("; + q.append<point, id_string> (v, conversion_); + q += ") = 1"; + return q; + } + + query + equal (ref_bind<point> r) const + { + query q (table_, column_); + q += ".STEquals("; + q.append<point, id_string> (r, conversion_); + q += ") = 1"; + return q; + } + + friend query + operator== (const query_column& c, const point& v) + { + return c.equal (v); + } + + friend query + operator== (const point& v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, val_bind<point> v) + { + return c.equal (v); + } + + friend query + operator== (val_bind<point> v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind<point> r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind<point> r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query + operator== (const query_column<point, id_string>& c) const + { + query q (table_, column_); + q += ".STEquals("; + q.append (c.table (), c.column ()); + q += ") = 1"; + return q; + } + + public: + query_column (const char* table, + const char* column, + const char* conv, + unsigned short prec = 0, + unsigned short scale = 0xFFFF) + : table_ (table), column_ (column), conversion_ (conv), + prec_ (prec), scale_ (scale), + x_column_ (std::string (column) + ".STX"), + y_column_ (std::string (column) + ".STY"), + x (table, x_column_.c_str (), 0), + y (table, y_column_.c_str (), 0) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + const char* + conversion () const + { + return conversion_; + } + + unsigned short + prec () const + { + return prec_; + } + + unsigned short + scale () const + { + return scale_; + } + }; +#endif // SQL Server > 2005 + } +} + +#endif // QUERY_HXX diff --git a/mssql/custom/test.hxx b/mssql/custom/test.hxx new file mode 100644 index 0000000..5548baa --- /dev/null +++ b/mssql/custom/test.hxx @@ -0,0 +1,78 @@ +// file : mssql/types/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 +// Map GEOMETRY SQL Server type to the point C++ struct. The other half +// of this mapping is in traits.hxx (value_traits<point, id_string>). +// Note that GEOMETRY is not available in SQL Server 2005. +// +#pragma db map type("GEOMETRY") \ + as("VARCHAR(256)") \ + to("GEOMETRY::STGeomFromText((?), 0)") \ + from("(?).STAsText()") + +#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; +} +#endif // SQL Server > 2005 + +// Map XML SQL Server type to std::string (or any other type that provides +// the value_traits<?, id_long_string> specialization). Note also that +// another alternative would be to interface with the XML data type using +// VARBINARY or NVARCHAR. Here we use implicit string to/from XML conversion, +// however, CAST/CONVERT can be used instead for greater control over +// whitespace handling, etc. +// +#pragma db map type("XML *(\\(.+\\))?") as("VARCHAR(max)") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_) : id (id_) {} + + #pragma db id + unsigned long id; + +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + point p; + std::vector<point> pv; +#endif + + #pragma db type("XML") + std::string xml; + + bool + operator== (const object& y) const + { + return id == y.id +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + && p == y.p + && pv == y.pv +#endif + && xml == y.xml; + } +}; + +#endif // TEST_HXX diff --git a/mssql/custom/test.std b/mssql/custom/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mssql/custom/test.std diff --git a/mssql/custom/traits.hxx b/mssql/custom/traits.hxx new file mode 100644 index 0000000..34081ed --- /dev/null +++ b/mssql/custom/traits.hxx @@ -0,0 +1,90 @@ +// file : mssql/types/traits.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// 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 <cassert> + +#include <odb/mssql/traits.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace mssql + { +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + 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 char* 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 + 7, n - 7)); + + is >> v.x; + is >> v.y; + } + } + + static void + set_image (char* b, + std::size_t c, + 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 (); + assert (n <= c); + std::memcpy (b, 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 "GEOMETRY::STGeomFromText((?), 0)";} + }; + }; +#endif // SQL Server > 2005 + } +} + +#endif // TRAITS_HXX diff --git a/mssql/makefile b/mssql/makefile index de9390a..2c62cc6 100644 --- a/mssql/makefile +++ b/mssql/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ template \ +custom \ native \ query \ types diff --git a/mysql/custom/driver.cxx b/mysql/custom/driver.cxx new file mode 100644 index 0000000..b591095 --- /dev/null +++ b/mysql/custom/driver.cxx @@ -0,0 +1,112 @@ +// file : mysql/custom/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in MySQL. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_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)); + o.pv.push_back (point (0.0000001, 0.000000001)); // Scientific notation. + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef odb::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 ()); + auto_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/mysql/custom/makefile b/mysql/custom/makefile new file mode 100644 index 0000000..4edfd2d --- /dev/null +++ b/mysql/custom/makefile @@ -0,0 +1,112 @@ +# file : mysql/custom/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database mysql --generate-schema \ +--generate-query --hxx-prologue '\#include "traits.hxx"' \ +--hxx-prologue '\#include "query.hxx"' --table-prefix mysql_custom_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export extra_headers := traits.hxx query.hxx +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/mysql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/mysql/custom/query.hxx b/mysql/custom/query.hxx new file mode 100644 index 0000000..110624a --- /dev/null +++ b/mysql/custom/query.hxx @@ -0,0 +1,161 @@ +// file : mysql/custom/query.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// 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 + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + // = + // + public: + query + equal (const point& v) const + { + return equal (val_bind<point> (v)); + } + + query + equal (val_bind<point> v) const + { + query q (table_, column_); + q += "="; + q.append<point, id_string> (v, conversion_); + return q; + } + + query + equal (ref_bind<point> r) const + { + query q (table_, column_); + q += "="; + q.append<point, id_string> (r, conversion_); + return q; + } + + friend query + operator== (const query_column& c, const point& v) + { + return c.equal (v); + } + + friend query + operator== (const point& v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, val_bind<point> v) + { + return c.equal (v); + } + + friend query + operator== (val_bind<point> v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind<point> r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind<point> r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query + operator== (const query_column<point, id_string>& c) const + { + query 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_ ("X(" + std::string (table)), // @@ Not very clean. + y_table_ ("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/mysql/custom/test.hxx b/mysql/custom/test.hxx new file mode 100644 index 0000000..5d739a1 --- /dev/null +++ b/mysql/custom/test.hxx @@ -0,0 +1,55 @@ +// file : mysql/custom/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// 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("GeomFromText((?))") \ + from("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/mysql/custom/test.std b/mysql/custom/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mysql/custom/test.std diff --git a/mysql/custom/traits.hxx b/mysql/custom/traits.hxx new file mode 100644 index 0000000..bf4ceb4 --- /dev/null +++ b/mysql/custom/traits.hxx @@ -0,0 +1,89 @@ +// file : mysql/types/traits.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// 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 "GeomFromText((?))";} + }; + }; + } +} + +#endif // TRAITS_HXX diff --git a/mysql/makefile b/mysql/makefile index 1872888..b3fc74f 100644 --- a/mysql/makefile +++ b/mysql/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ template \ +custom \ native \ truncation \ types diff --git a/oracle/custom/custom.sql b/oracle/custom/custom.sql new file mode 100644 index 0000000..3200e74 --- /dev/null +++ b/oracle/custom/custom.sql @@ -0,0 +1,61 @@ +/* This file contains custom type definitions and helper functions. + */ + +SET FEEDBACK OFF; +WHENEVER SQLERROR EXIT FAILURE; +WHENEVER OSERROR EXIT FAILURE; + +-- @@ Temporary workaround: we cannot replace a type if there are +-- tables that use it. So need to drop the tables first, then +-- create/replace the type, and then create the tables. +-- +--CREATE OR REPLACE TYPE Numbers AS VARRAY(100) OF NUMBER(10); + +BEGIN + BEGIN + EXECUTE IMMEDIATE 'CREATE TYPE Numbers AS VARRAY(100) OF NUMBER(10)'; + EXCEPTION + WHEN OTHERS THEN + IF SQLCODE != -955 THEN RAISE; END IF; + END; +END; +/ + +CREATE OR REPLACE FUNCTION string_to_numbers(in_str IN VARCHAR2) RETURN Numbers +IS + ret Numbers := Numbers(); + s_pos NUMBER := 1; + e_pos NUMBER := 0; +BEGIN + IF in_str IS NOT NULL THEN + LOOP + e_pos := INSTR(in_str, ',', s_pos); + EXIT WHEN e_pos = 0; + ret.extend; + ret(ret.COUNT) := CAST(SUBSTR(in_str, s_pos, e_pos - s_pos) AS NUMBER); + s_pos := e_pos + 1; + END LOOP; + ret.extend; + ret(ret.COUNT) := CAST(SUBSTR(in_str, s_pos) AS NUMBER); + END IF; + RETURN ret; +END; +/ + +CREATE OR REPLACE FUNCTION numbers_to_string(in_nums IN Numbers) RETURN VARCHAR2 +IS + ret VARCHAR2(1500); +BEGIN + IF in_nums.COUNT != 0 THEN + FOR i IN in_nums.FIRST .. in_nums.LAST LOOP + IF i != in_nums.FIRST THEN + ret := ret || ','; + END IF; + ret := ret || in_nums(i); + END LOOP; + END IF; + RETURN ret; +END; +/ + +EXIT; diff --git a/oracle/custom/driver.cxx b/oracle/custom/driver.cxx new file mode 100644 index 0000000..2425b29 --- /dev/null +++ b/oracle/custom/driver.cxx @@ -0,0 +1,77 @@ +// file : oracle/custom/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in Oracle. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/oracle/database.hxx> +#include <odb/oracle/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + object o (1); + o.iv.push_back (123); + o.iv.push_back (234); + o.iv.push_back (-345); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Update. + // + o.iv[0]++; + o.iv.pop_back (); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_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/oracle/custom/makefile b/oracle/custom/makefile new file mode 100644 index 0000000..97cdb2c --- /dev/null +++ b/oracle/custom/makefile @@ -0,0 +1,112 @@ +# file : oracle/custom/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database oracle --generate-schema \ +--generate-query --hxx-prologue '\#include "traits.hxx"' \ +--table-prefix oracle_custom_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export extra_headers := traits.hxx custom.sql +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/oracle/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema, custom.sql) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/oracle/custom/test.hxx b/oracle/custom/test.hxx new file mode 100644 index 0000000..cc46512 --- /dev/null +++ b/oracle/custom/test.hxx @@ -0,0 +1,41 @@ +// file : oracle/custom/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <odb/core.hxx> + +// Map Numbers VARRAY Oracle type to std::vector<int>. This type is defined +// in the custom.sql file along with two helper functions that convert +// between Numbers and its string representation. The other half of this +// mapping is in traits.hxx (value_traits<std::vector<int>, id_string>). +// +#pragma db map type("Numbers") \ + as("VARCHAR2(1500)") \ + to("CAST(string_to_numbers((?)) AS Numbers)") \ + from("numbers_to_string((?))") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_) : id (id_) {} + + #pragma db id + unsigned long id; + + #pragma db type("Numbers") + std::vector<int> iv; + + bool + operator== (const object& y) const + { + return id == y.id && iv == y.iv; + } +}; + +#endif // TEST_HXX diff --git a/oracle/custom/test.std b/oracle/custom/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/oracle/custom/test.std diff --git a/oracle/custom/traits.hxx b/oracle/custom/traits.hxx new file mode 100644 index 0000000..8f3e81e --- /dev/null +++ b/oracle/custom/traits.hxx @@ -0,0 +1,77 @@ +// file : oracle/types/traits.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <vector> +#include <sstream> +#include <cstring> // std::memcpy +#include <cassert> // std::memcpy + +#include <odb/oracle/traits.hxx> + +namespace odb +{ + namespace oracle + { + + template <> + class value_traits<std::vector<int>, id_string> + { + public: + typedef std::vector<int> value_type; + typedef value_type query_type; + typedef details::buffer image_type; + + static void + set_value (value_type& v, + const char* b, + std::size_t n, + bool is_null) + { + v.clear (); + + if (!is_null) + { + // Array format is "n1,n2,n3...". + // + std::istringstream is (std::string (b, n)); + + for (char c; !is.eof (); is >> c) + { + v.push_back (int ()); + is >> v.back (); + } + } + } + + static void + set_image (char* b, + std::size_t c, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + std::ostringstream os; + + for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;) + { + os << *i; + + if (++i != e) + os << ','; + } + + const std::string& s (os.str ()); + n = s.size (); + assert (n <= c); + std::memcpy (b, s.c_str (), n); + } + }; + } +} + +#endif // TRAITS_HXX diff --git a/oracle/makefile b/oracle/makefile index 5f29177..0ed2e3c 100644 --- a/oracle/makefile +++ b/oracle/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ template \ +custom \ native \ types diff --git a/pgsql/custom/driver.cxx b/pgsql/custom/driver.cxx new file mode 100644 index 0000000..f1f70f8 --- /dev/null +++ b/pgsql/custom/driver.cxx @@ -0,0 +1,123 @@ +// file : pgsql/custom/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in PostgreSQL. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_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)); + o.pv.push_back (point (0.0000001, 0.000000001)); // Scientific notation. + + o.n1 = "23.5154"; + o.n2 = "235154"; + o.n3 = "2222222222222222222222222222.111111111111111111111111111111"; + + o.iv.push_back (123); + o.iv.push_back (234); + o.iv.push_back (-345); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef odb::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++; + o.n3 += "999"; + o.iv[0]++; + o.iv.pop_back (); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_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/pgsql/custom/makefile b/pgsql/custom/makefile new file mode 100644 index 0000000..30ce36d --- /dev/null +++ b/pgsql/custom/makefile @@ -0,0 +1,112 @@ +# file : pgsql/custom/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database pgsql --generate-schema \ +--generate-query --hxx-prologue '\#include "traits.hxx"' \ +--hxx-prologue '\#include "query.hxx"' --table-prefix pgsql_custom_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export extra_headers := traits.hxx query.hxx +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/pgsql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/pgsql/custom/query.hxx b/pgsql/custom/query.hxx new file mode 100644 index 0000000..4b29b45 --- /dev/null +++ b/pgsql/custom/query.hxx @@ -0,0 +1,159 @@ +// file : pgsql/custom/query.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef QUERY_HXX +#define QUERY_HXX + +#include <string> + +#include <odb/pgsql/query.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace pgsql + { + template <> + struct query_column<point, id_string> + { + private: + const char* table_; + const char* column_; + const char* conversion_; + + std::string x_column_; + std::string y_column_; + + // Sub-columns for individual members. + // + public: + query_column<double, id_double> x, y; + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + // = + // + public: + query + equal (const point& v) const + { + return equal (val_bind<point> (v)); + } + + query + equal (val_bind<point> v) const + { + query q (table_, column_); + q += "~="; + q.append<point, id_string> (v, conversion_); + return q; + } + + query + equal (ref_bind<point> r) const + { + query q (table_, column_); + q += "~="; + q.append<point, id_string> (r, conversion_); + return q; + } + + friend query + operator== (const query_column& c, const point& v) + { + return c.equal (v); + } + + friend query + operator== (const point& v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, val_bind<point> v) + { + return c.equal (v); + } + + friend query + operator== (val_bind<point> v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind<point> r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind<point> r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query + operator== (const query_column<point, id_string>& c) const + { + query 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_column_ (std::string (column) + "[0]"), + y_column_ (std::string (column) + "[1]"), + x (table, x_column_.c_str (), 0), + y (table, y_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/pgsql/custom/test.hxx b/pgsql/custom/test.hxx new file mode 100644 index 0000000..1f83031 --- /dev/null +++ b/pgsql/custom/test.hxx @@ -0,0 +1,87 @@ +// file : pgsql/custom/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +// Map POINT PostgreSQL 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("POINT") as("TEXT") to("(?)::POINT") from("(?)::TEXT") + +#pragma db value type("POINT") +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; +} + +// Map NUMERIC PostgreSQL type to std::string (or any other type that +// provides the value_traits<?, id_string> specialization). +// +#pragma db map type("NUMERIC *(\\(.+\\))?") \ + as("TEXT") \ + to("(?)::NUMERIC$1") \ + from("(?)::TEXT") + +// Map INTEGER[] PostgreSQL type to std::vector<int>. The other half of +// this mapping is in traits.hxx (value_traits<std::vector<int>, id_string>). +// +#pragma db map type("INTEGER *\\[(\\d+)\\]") \ + as("TEXT") \ + to("(?)::INTEGER[$1]") \ + from("(?)::TEXT") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_) : id (id_) {} + + #pragma db id + unsigned long id; + + point p; + std::vector<point> pv; + + #pragma db type("NUMERIC(6, 4)") + std::string n1; + + #pragma db type("NUMERIC(6)") + std::string n2; + + #pragma db type("NUMERIC") + std::string n3; + + #pragma db type("INTEGER [123]") + std::vector<int> iv; + + bool + operator== (const object& y) const + { + return id == y.id && + p == y.p && + pv == y.pv && + n1 == y.n1 && + n2 == y.n2 && + n3 == y.n3 && + iv == y.iv; + } +}; + +#endif // TEST_HXX diff --git a/pgsql/custom/test.std b/pgsql/custom/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pgsql/custom/test.std diff --git a/pgsql/custom/traits.hxx b/pgsql/custom/traits.hxx new file mode 100644 index 0000000..800bfdb --- /dev/null +++ b/pgsql/custom/traits.hxx @@ -0,0 +1,167 @@ +// file : pgsql/custom/traits.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <limits> // std::numeric_limits +#include <vector> +#include <sstream> +#include <cstring> // std::memcpy + +#include <odb/pgsql/traits.hxx> + +#include "test.hxx" // point + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits<point, id_string> + { + public: + typedef point value_type; + typedef point query_type; + typedef details::buffer 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 "(x,y)". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '(' + is >> v.x; + is >> c; // ',' + 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 << '(' << 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 "(?)::POINT";} + }; + }; + + template <> + class value_traits<std::vector<int>, id_string> + { + public: + typedef std::vector<int> value_type; + typedef value_type query_type; + typedef details::buffer image_type; + + static void + set_value (value_type& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.clear (); + + if (!is_null) + { + // Array format is "{n1,n2,n3...}". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '{' + + for (c = static_cast<char> (is.peek ()); c != '}'; is >> c) + { + v.push_back (int ()); + is >> v.back (); + } + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + std::ostringstream os; + + os << '{'; + + for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;) + { + os << *i; + + if (++i != e) + os << ','; + } + + os << '}'; + + 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<std::vector<int> > + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "(?)::INTEGER[]";} + }; + }; + } +} + +#endif // TRAITS_HXX diff --git a/pgsql/makefile b/pgsql/makefile index 6add9be..852e017 100644 --- a/pgsql/makefile +++ b/pgsql/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ template \ +custom \ native \ truncation \ types diff --git a/sqlite/custom/driver.cxx b/sqlite/custom/driver.cxx new file mode 100644 index 0000000..9e387b0 --- /dev/null +++ b/sqlite/custom/driver.cxx @@ -0,0 +1,76 @@ +// file : sqlite/custom/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in SQLite. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + object o (1); + o.nv.push_back ("123"); // INTEGER + o.nv.push_back ("1.23"); // REAL + o.nv.push_back ("abc"); // TEXT + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Update. + // + o.nv[1] += "4"; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_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/sqlite/custom/makefile b/sqlite/custom/makefile new file mode 100644 index 0000000..5de140f --- /dev/null +++ b/sqlite/custom/makefile @@ -0,0 +1,110 @@ +# file : sqlite/custom/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database sqlite --generate-schema \ +--generate-query --table-prefix sqlitex_custom_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/sqlite/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/sqlite/custom/test.hxx b/sqlite/custom/test.hxx new file mode 100644 index 0000000..ea20912 --- /dev/null +++ b/sqlite/custom/test.hxx @@ -0,0 +1,40 @@ +// file : sqlite/custom/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +// Map NUMERIC SQLite type to std::string (or any other type that +// provides the value_traits<?, id_string> specialization). By +// default ODB treats NUMERIC as REAL. Note also that we don't +// need to specify to/from conversions since SQLite will convert +// implicitly. +// +#pragma db map type("NUMERIC") as("TEXT") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_): id (id_) {} + + #pragma db id + unsigned long id; + + #pragma db value_type("NUMERIC") + std::vector<std::string> nv; + + bool + operator== (const object& y) const + { + return id == y.id && nv == y.nv; + } +}; + +#endif // TEST_HXX diff --git a/sqlite/custom/test.std b/sqlite/custom/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sqlite/custom/test.std diff --git a/sqlite/makefile b/sqlite/makefile index 9e61fc5..a584535 100644 --- a/sqlite/makefile +++ b/sqlite/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ template \ +custom \ native \ truncation \ types |