aboutsummaryrefslogtreecommitdiff
path: root/mapping/traits.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-09-23 09:32:31 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-09-23 09:32:31 +0200
commit2f17222fd6b2f3cfa251e57daf67ee58d25d6e43 (patch)
tree3122acf600ad51dcab80e5f353f81f86d616fdff /mapping/traits.hxx
parentc542a0841869618f82188bc193e21a6a6972f0ba (diff)
Add the mapping example
It shows how to map between C++ value types and SQL types
Diffstat (limited to 'mapping/traits.hxx')
-rw-r--r--mapping/traits.hxx87
1 files changed, 87 insertions, 0 deletions
diff --git a/mapping/traits.hxx b/mapping/traits.hxx
new file mode 100644
index 0000000..0533360
--- /dev/null
+++ b/mapping/traits.hxx
@@ -0,0 +1,87 @@
+// file : mapping/traits.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_HXX
+#define TRAITS_HXX
+
+#include <cstring>
+
+#include "person.hxx" // date
+
+// MySQL implementation.
+//
+#if defined(DATABASE_MYSQL)
+
+#include <odb/mysql/traits.hxx>
+
+namespace odb
+{
+ namespace mysql
+ {
+ template <>
+ class value_traits<bool, details::buffer, id_string>
+ {
+ public:
+ typedef bool value_type;
+ typedef bool query_type;
+ typedef details::buffer image_type;
+
+ static void
+ set_value (bool& v,
+ const details::buffer& b,
+ std::size_t n,
+ bool is_null)
+ {
+ v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0);
+ }
+
+ static void
+ set_image (details::buffer& b,
+ std::size_t& n,
+ bool& is_null,
+ bool v)
+ {
+ is_null = false;
+ n = v ? 4 : 5;
+
+ if (n > b.capacity ())
+ b.capacity (n);
+
+ std::memcpy (b.data (), (v ? "true" : "false"), n);
+ }
+ };
+
+ template <>
+ class value_traits<date, MYSQL_TIME, id_date>
+ {
+ public:
+ typedef date value_type;
+ typedef date query_type;
+ typedef MYSQL_TIME image_type;
+
+ static void
+ set_value (date& v, const MYSQL_TIME& i, bool is_null)
+ {
+ if (!is_null)
+ v = date (i.year, i.month, i.day);
+ else
+ v = date (0, 0, 0);
+ }
+
+ static void
+ set_image (MYSQL_TIME& i, bool& is_null, const date& v)
+ {
+ is_null = false;
+ i.neg = false;
+ i.year = v.year ();
+ i.month = v.month ();
+ i.day = v.day ();
+ }
+ };
+ }
+}
+
+#endif // DATABASE_MYSQL
+
+#endif // TRAITS_HXX