aboutsummaryrefslogtreecommitdiff
path: root/mapping/traits-pgsql.hxx
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-07-13 11:03:13 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-07-13 11:03:13 +0200
commite440e73a889c8929730632d62ebc84e32475b549 (patch)
treecefbcd5cac5e14e54c5a482af58e19d5973ea2e0 /mapping/traits-pgsql.hxx
parent292f71768c16e14369c7aea4ef0590b0a741c3bc (diff)
Add PostgreSQL
Diffstat (limited to 'mapping/traits-pgsql.hxx')
-rw-r--r--mapping/traits-pgsql.hxx107
1 files changed, 107 insertions, 0 deletions
diff --git a/mapping/traits-pgsql.hxx b/mapping/traits-pgsql.hxx
new file mode 100644
index 0000000..6ee396d
--- /dev/null
+++ b/mapping/traits-pgsql.hxx
@@ -0,0 +1,107 @@
+// file : mapping/traits-pgsql.hxx
+// author : Constantin Michael <constantin@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef TRAITS_PGSQL_HXX
+#define TRAITS_PGSQL_HXX
+
+//
+// PostgreSQL implementation.
+//
+
+#include <cstring>
+#include <ctime>
+
+#include <odb/pgsql/traits.hxx>
+#include <odb/pgsql/details/endian-traits.hxx>
+
+#include "person.hxx" // date
+
+namespace odb
+{
+ namespace pgsql
+ {
+ template <>
+ class value_traits<bool, 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, id_date>
+ {
+ public:
+ typedef date value_type;
+ typedef date query_type;
+ typedef int image_type;
+
+ static const time_t pg_epoch_tt;
+ static const time_t seconds_per_day = 86400;
+
+ static void
+ set_value (date& v, const int& i, bool is_null)
+ {
+ if (is_null)
+ {
+ v = date (0, 0, 0);
+ return;
+ }
+
+ time_t v_tt (pg_epoch_tt +
+ static_cast<time_t> (details::endian_traits::ntoh (i)) *
+ seconds_per_day);
+
+ tm v_tm (*localtime (&v_tt));
+
+ v = date (v_tm.tm_year + 1900, v_tm.tm_mon, v_tm.tm_mday);
+ }
+
+ static void
+ set_image (int& i, bool& is_null, const date& v)
+ {
+ is_null = false;
+
+ tm v_tm;
+ memset (&v_tm, 0, sizeof (v_tm));
+
+ v_tm.tm_mday = v.day ();
+ v_tm.tm_mon = v.month ();
+ v_tm.tm_year = v.year () - 1900;
+
+ time_t v_tt (mktime (&v_tm));
+
+ i = details::endian_traits::hton (
+ static_cast<int> ((v_tt - pg_epoch_tt) / seconds_per_day));
+ }
+ };
+ }
+}
+
+#endif // TRAITS_PGSQL_HXX