diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-07-13 11:03:13 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-07-13 11:03:13 +0200 |
commit | e440e73a889c8929730632d62ebc84e32475b549 (patch) | |
tree | cefbcd5cac5e14e54c5a482af58e19d5973ea2e0 /mapping | |
parent | 292f71768c16e14369c7aea4ef0590b0a741c3bc (diff) |
Add PostgreSQL
Diffstat (limited to 'mapping')
-rw-r--r-- | mapping/database.hxx | 8 | ||||
-rw-r--r-- | mapping/makefile | 2 | ||||
-rw-r--r-- | mapping/traits-pgsql.cxx | 19 | ||||
-rw-r--r-- | mapping/traits-pgsql.hxx | 107 | ||||
-rw-r--r-- | mapping/traits.hxx | 2 |
5 files changed, 136 insertions, 2 deletions
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 <boris@codesynthesis.com> // copyright : not copyrighted - public domain @@ -22,6 +22,8 @@ # include <odb/transaction.hxx> # include <odb/schema-catalog.hxx> # include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> #endif inline std::auto_ptr<odb::database> @@ -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<database> 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 <constantin@codesynthesis.com> +// 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<date, id_date>::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 <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 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 |