From e440e73a889c8929730632d62ebc84e32475b549 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Wed, 13 Jul 2011 11:03:13 +0200 Subject: Add PostgreSQL --- mapping/traits-pgsql.hxx | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 mapping/traits-pgsql.hxx (limited to 'mapping/traits-pgsql.hxx') 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 +// copyright : not copyrighted - public domain + +#ifndef TRAITS_PGSQL_HXX +#define TRAITS_PGSQL_HXX + +// +// PostgreSQL implementation. +// + +#include +#include + +#include +#include + +#include "person.hxx" // date + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits + { + 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 + { + 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 (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 ((v_tt - pg_epoch_tt) / seconds_per_day)); + } + }; + } +} + +#endif // TRAITS_PGSQL_HXX -- cgit v1.1