aboutsummaryrefslogtreecommitdiff
path: root/mssql/custom/traits.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-07-10 15:17:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-07-10 15:17:16 +0200
commit6b8def06796d1e4fc9e6e7e75ce59bccf6899261 (patch)
treef7d440fda64cf07549173cefd5d45a92bf1845d6 /mssql/custom/traits.hxx
parent7ba8d1469e331f07040352c069cf7b9d24ac57a3 (diff)
Add support for custom database type mapping
New pragma qualifier, map, and specifiers: as, to, from. New tests: <database>/custom.
Diffstat (limited to 'mssql/custom/traits.hxx')
-rw-r--r--mssql/custom/traits.hxx90
1 files changed, 90 insertions, 0 deletions
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