From 10a88bacf1ec2b8b0700a27ea022d503f295825d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Jan 2013 15:10:21 +0200 Subject: Add support for mapping char[N] to CHAR/VARCHAR database types Also improve query support for arrays (decaying). --- odb/pgsql/traits.hxx | 121 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 112 insertions(+), 9 deletions(-) (limited to 'odb/pgsql/traits.hxx') diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx index 0267527..7983571 100644 --- a/odb/pgsql/traits.hxx +++ b/odb/pgsql/traits.hxx @@ -403,7 +403,7 @@ namespace odb const std::string&); }; - // const char* specialization + // char*/const char* specialization // // Specialization for const char* which only supports initialization // of an image from the value but not the other way around. This way @@ -423,23 +423,114 @@ namespace odb }; template <> + struct LIBODB_PGSQL_EXPORT default_value_traits: + c_string_value_traits {}; + + template <> struct LIBODB_PGSQL_EXPORT default_value_traits: - c_string_value_traits + c_string_value_traits {}; + + // char[N] specializations. + // + struct LIBODB_PGSQL_EXPORT c_array_value_traits_base { - typedef const char* query_type; + static void + set_value (char* const& v, + const details::buffer& b, + std::size_t n, + bool is_null, + std::size_t N); + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const char* v, + std::size_t N); }; template - struct default_value_traits: c_string_value_traits + struct default_value_traits { + typedef char* value_type; typedef char query_type[N]; + typedef details::buffer image_type; + + static void + set_value (char* const& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + c_array_value_traits_base::set_value (v, b, n, is_null, N); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const char* v) + { + c_array_value_traits_base::set_image (b, n, is_null, v, N); + } }; + // std::array (string) specialization. + // +#ifdef ODB_CXX11 template - struct default_value_traits: - c_string_value_traits + struct default_value_traits, id_string> { - typedef const char query_type[N]; + typedef std::array value_type; + typedef std::array query_type; + typedef details::buffer image_type; + + static void + set_value (value_type& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + c_array_value_traits_base::set_value (v.data (), b, n, is_null, N); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + c_array_value_traits_base::set_image (b, n, is_null, v.data (), N); + } + }; +#endif + + // char specialization. + // + template <> + struct LIBODB_PGSQL_EXPORT default_value_traits + { + typedef char value_type; + typedef char query_type; + typedef details::buffer image_type; + + static void + set_value (char& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + c_array_value_traits_base::set_value (&v, b, n, is_null, 1); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + char v) + { + c_array_value_traits_base::set_image (b, n, is_null, &v, 1); + } }; // std::vector (buffer) specialization. @@ -789,6 +880,12 @@ namespace odb }; template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_string; + }; + + template <> struct default_type_traits { static const database_type_id db_type_id = id_string; @@ -800,14 +897,20 @@ namespace odb static const database_type_id db_type_id = id_string; }; - template - struct default_type_traits + template <> + struct default_type_traits { static const database_type_id db_type_id = id_string; }; // Binary types. // + template + struct default_type_traits + { + static const database_type_id db_type_id = id_bytea; + }; + template <> struct default_type_traits > { -- cgit v1.1