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/database.hxx | 8 +++- mapping/makefile | 2 +- mapping/traits-pgsql.cxx | 19 +++++++++ mapping/traits-pgsql.hxx | 107 +++++++++++++++++++++++++++++++++++++++++++++++ mapping/traits.hxx | 2 + 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 mapping/traits-pgsql.cxx create mode 100644 mapping/traits-pgsql.hxx (limited to 'mapping') diff --git a/mapping/database.hxx b/mapping/database.hxx index 7920d93..fe815e9 100644 --- a/mapping/database.hxx +++ b/mapping/database.hxx @@ -1,4 +1,4 @@ -// file : mapping/database.hxx +// file : template/database.hxx // author : Boris Kolpackov // copyright : not copyrighted - public domain @@ -22,6 +22,8 @@ # include # include # include +#elif defined(DATABASE_PGSQL) +# include #endif inline std::auto_ptr @@ -39,6 +41,8 @@ create_database (int& argc, char* argv[]) odb::mysql::database::print_usage (cerr); #elif defined(DATABASE_SQLITE) odb::sqlite::database::print_usage (cerr); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cerr); #endif exit (0); @@ -58,6 +62,8 @@ create_database (int& argc, char* argv[]) schema_catalog::create_schema (*db); t.commit (); } +#elif defined(DATABASE_PGSQL) + auto_ptr db (new odb::pgsql::database (argc, argv)); #endif return db; diff --git a/mapping/makefile b/mapping/makefile index 1b50fe2..b3c029d 100644 --- a/mapping/makefile +++ b/mapping/makefile @@ -5,7 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -cxx_tun := driver.cxx +cxx_tun := driver.cxx traits-pgsql.cxx odb_hdr := person.hxx cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) cxx_od := $(cxx_obj:.o=.o.d) diff --git a/mapping/traits-pgsql.cxx b/mapping/traits-pgsql.cxx new file mode 100644 index 0000000..026e15f --- /dev/null +++ b/mapping/traits-pgsql.cxx @@ -0,0 +1,19 @@ +// file : mapping/traits-pgsql.cxx +// author : Constantin Michael +// copyright : not copyrighted - public domain + +#include "traits-pgsql.hxx" + +namespace odb +{ + namespace pgsql + { + namespace + { + tm pg_epoch_tm = {0, 0, 0, 1, 1, 100, 0, 0, 0, 0, 0}; + } + + const time_t value_traits::pg_epoch_tt ( + mktime (&pg_epoch_tm)); + } +} 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 diff --git a/mapping/traits.hxx b/mapping/traits.hxx index 1009265..1d2412b 100644 --- a/mapping/traits.hxx +++ b/mapping/traits.hxx @@ -11,6 +11,8 @@ # include "traits-mysql.hxx" #elif defined(DATABASE_SQLITE) # include "traits-sqlite.hxx" +#elif defined(DATABASE_PGSQL) +# include "traits-pgsql.hxx" #endif #endif // TRAITS_HXX -- cgit v1.1