From 6b8def06796d1e4fc9e6e7e75ce59bccf6899261 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Jul 2012 15:17:16 +0200 Subject: Add support for custom database type mapping New pragma qualifier, map, and specifiers: as, to, from. New tests: /custom. --- mssql/custom/driver.cxx | 124 ++++++++++++++++++++++++++++++++ mssql/custom/makefile | 112 +++++++++++++++++++++++++++++ mssql/custom/query.hxx | 184 ++++++++++++++++++++++++++++++++++++++++++++++++ mssql/custom/test.hxx | 78 ++++++++++++++++++++ mssql/custom/test.std | 0 mssql/custom/traits.hxx | 90 +++++++++++++++++++++++ 6 files changed, 588 insertions(+) create mode 100644 mssql/custom/driver.cxx create mode 100644 mssql/custom/makefile create mode 100644 mssql/custom/query.hxx create mode 100644 mssql/custom/test.hxx create mode 100644 mssql/custom/test.std create mode 100644 mssql/custom/traits.hxx (limited to 'mssql/custom') 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 // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr 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 = "AAABBBCCC"; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + t.commit (); + + assert (o == *o1); + } + + // Query. + // + typedef odb::query query; + typedef odb::result result; + + { + transaction t (db->begin ()); + +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + // Point comparison. + // + { + result r (db->query (query::p == o.p)); + assert (!r.empty ()); + } + + // Point comparison using native query. + // + { + result r (db->query ( + query::p + ".STEquals(" + query::_val (o.p) + ") = 1")); + assert (!r.empty ()); + } + + // Access to individual members. + // + { + result r (db->query (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 = "BBBCCCDDD"; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (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 + +#include + +#include "test.hxx" // point + +namespace odb +{ + namespace mssql + { +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + template <> + struct query_column + { + 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 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 (v)); + } + + query + equal (val_bind v) const + { + query q (table_, column_); + q += ".STEquals("; + q.append (v, conversion_); + q += ") = 1"; + return q; + } + + query + equal (ref_bind r) const + { + query q (table_, column_); + q += ".STEquals("; + q.append (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 v) + { + return c.equal (v); + } + + friend query + operator== (val_bind v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind r, const query_column& c) + { + return c.equal (r); + } + + // Column comparison. + // + public: + query + operator== (const query_column& 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 +#include + +#include + +#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). +// 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 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 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 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 // std::numeric_limits +#include +#include // std::memcpy +#include + +#include + +#include "test.hxx" // point + +namespace odb +{ + namespace mssql + { +#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 + template <> + class value_traits + { + 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::digits10); + // os.precision (2 + std::numeric_limits::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 + { + 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 -- cgit v1.1