From e607b4dfb03f17e073725013df12a11547fc6a54 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Wed, 13 Jul 2011 10:03:09 +0200 Subject: Add support for DATE, TIME and TIMESTAMP SQL types --- odb/pgsql/pgsql-types.hxx | 3 + odb/pgsql/query.hxx | 186 +++++++++++++++++++++++++--------------------- odb/pgsql/statement.cxx | 35 ++++++++- odb/pgsql/traits.hxx | 22 +++--- 4 files changed, 145 insertions(+), 101 deletions(-) (limited to 'odb') diff --git a/odb/pgsql/pgsql-types.hxx b/odb/pgsql/pgsql-types.hxx index 3abd4fc..1d83b9f 100644 --- a/odb/pgsql/pgsql-types.hxx +++ b/odb/pgsql/pgsql-types.hxx @@ -30,6 +30,9 @@ namespace odb real, // Buffer is float; size, capacity, truncated are unused. double_, // Buffer is double; size, capacity, truncated are unused. numeric, // Buffer is a char array. + date, // Buffer is int; size, capacity, truncated are unused. + time, // Buffer is long long; size, capacity, truncated are unused. + timestamp,// Buffer is long long; size, capacity, truncated are unused. text, // Buffer is a char array. bytea, // Buffer is a char array. bit, // Buffer is a char array. diff --git a/odb/pgsql/query.hxx b/odb/pgsql/query.hxx index 933ecd9..879517c 100644 --- a/odb/pgsql/query.hxx +++ b/odb/pgsql/query.hxx @@ -1306,107 +1306,125 @@ namespace odb // unsigned long size_; // }; - // @@ DATE + // DATE // - // template - // struct query_param_impl: query_param - // { - // query_param_impl (ref_bind r) : query_param (&r.ref) {} - // query_param_impl (val_bind v) : query_param (0) {init (v.val);} + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} - // virtual bool - // init () - // { - // init (*static_cast (value_)); - // return false; - // } + virtual bool + init () + { + init (*static_cast (value_)); + return false; + } - // virtual void - // bind (MYSQL_BIND* b) - // { - // b->buffer_type = MYSQL_TYPE_DATE; - // b->buffer = &image_; - // } + virtual void + bind (pgsql::bind* b) + { + b->type = bind::date; + b->buffer = &image_; + } - // private: - // void - // init (const T& v) - // { - // bool dummy; - // value_traits::set_image (image_, dummy, v); - // } + virtual unsigned int + oid () const + { + return date_oid; + } - // private: - // MYSQL_TIME image_; - // }; + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + int image_; + }; - // @@ TIME + // TIME // - // template - // struct query_param_impl: query_param - // { - // query_param_impl (ref_bind r) : query_param (&r.ref) {} - // query_param_impl (val_bind v) : query_param (0) {init (v.val);} + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} - // virtual bool - // init () - // { - // init (*static_cast (value_)); - // return false; - // } + virtual bool + init () + { + init (*static_cast (value_)); + return false; + } - // virtual void - // bind (MYSQL_BIND* b) - // { - // b->buffer_type = MYSQL_TYPE_TIME; - // b->buffer = &image_; - // } + virtual void + bind (pgsql::bind* b) + { + b->type = bind::time; + b->buffer = &image_; + } - // private: - // void - // init (const T& v) - // { - // bool dummy; - // value_traits::set_image (image_, dummy, v); - // } + virtual unsigned int + oid () const + { + return time_oid; + } - // private: - // MYSQL_TIME image_; - // }; + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + long long image_; + }; - // @@ TIMESTAMP + // TIMESTAMP // - // template - // struct query_param_impl: query_param - // { - // query_param_impl (ref_bind r) : query_param (&r.ref) {} - // query_param_impl (val_bind v) : query_param (0) {init (v.val);} + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} - // virtual bool - // init () - // { - // init (*static_cast (value_)); - // return false; - // } + virtual bool + init () + { + init (*static_cast (value_)); + return false; + } - // virtual void - // bind (MYSQL_BIND* b) - // { - // b->buffer_type = MYSQL_TYPE_TIMESTAMP; - // b->buffer = &image_; - // } + virtual void + bind (pgsql::bind* b) + { + b->type = bind::timestamp; + b->buffer = &image_; + } - // private: - // void - // init (const T& v) - // { - // bool dummy; - // value_traits::set_image (image_, dummy, v); - // } + virtual unsigned int + oid () const + { + return timestamp_oid; + } - // private: - // MYSQL_TIME image_; - // }; + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + long long image_; + }; // STRING // diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx index e9497df..cb49332 100644 --- a/odb/pgsql/statement.cxx +++ b/odb/pgsql/statement.cxx @@ -89,7 +89,7 @@ namespace odb n.values[i] = reinterpret_cast (current_bind.buffer); - // Use text format for numeric/decimal types and binary format + // Use text format for numeric types and binary format // for all others. // if (current_bind.type == bind::numeric) @@ -132,6 +132,17 @@ namespace odb l = sizeof (double); break; } + case bind::date: + { + l = sizeof (int); + break; + } + case bind::time: + case bind::timestamp: + { + l = sizeof (long long); + break; + } case bind::uuid: { // UUID is a 16-byte sequence. @@ -195,7 +206,9 @@ namespace odb case bind::boolean: { *static_cast (b.buffer) = - reinterpret_cast (v); + *reinterpret_cast (v); + + break; } case bind::smallint: { @@ -206,8 +219,7 @@ namespace odb } case bind::integer: { - *static_cast (b.buffer) = - *reinterpret_cast (v); + *static_cast (b.buffer) = *reinterpret_cast (v); break; } @@ -232,6 +244,20 @@ namespace odb break; } + case bind::date: + { + *static_cast (b.buffer) = *reinterpret_cast (v); + + break; + } + case bind::time: + case bind::timestamp: + { + *static_cast (b.buffer) = + *reinterpret_cast (v); + + break; + } case bind::numeric: case bind::text: case bind::bytea: @@ -256,6 +282,7 @@ namespace odb case bind::uuid: { memcpy (b.buffer, v, 16); + break; } } diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx index 1fbcc26..8308688 100644 --- a/odb/pgsql/traits.hxx +++ b/odb/pgsql/traits.hxx @@ -78,6 +78,15 @@ namespace odb struct image_traits {typedef double image_type;}; template <> + struct image_traits {typedef int image_type;}; + + template <> + struct image_traits {typedef long long image_type;}; + + template <> + struct image_traits {typedef long long image_type;}; + + template <> struct image_traits {typedef details::buffer image_type;}; template <> @@ -94,19 +103,6 @@ namespace odb template <> struct image_traits {typedef unsigned char* image_type;}; - // @@ Date/time binary support in PostgreSQL is sketchy and in some - // cases depends on server compile time constants. Using strings - // to avoid this. - // - template <> - struct image_traits {typedef details::buffer image_type;}; - - template <> - struct image_traits {typedef details::buffer image_type;}; - - template <> - struct image_traits {typedef details::buffer image_type;}; - // // value_traits // -- cgit v1.1