From a849d159fd03d3c954df3fc60826680f5d1afd65 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/sqlite/traits.hxx | 255 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 225 insertions(+), 30 deletions(-) (limited to 'odb/sqlite/traits.hxx') diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index e6abf2d..0cdf171 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -284,7 +284,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 @@ -303,35 +303,129 @@ namespace odb }; template <> + struct LIBODB_SQLITE_EXPORT default_value_traits: + c_string_value_traits {}; + + template <> struct LIBODB_SQLITE_EXPORT default_value_traits: - c_string_value_traits + c_string_value_traits {}; + + // char[N] specialization. + // + struct LIBODB_SQLITE_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_text> { - 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_SQLITE_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); + } }; #ifdef _WIN32 // std::wstring specialization. Using UTF-16 binding. // - template <> - struct image_traits + struct wstring_image_traits { typedef details::buffer image_type; static const bind::buffer_type bind_value = bind::text16; }; template <> + struct image_traits: wstring_image_traits {}; + + template <> struct LIBODB_SQLITE_EXPORT default_value_traits { typedef std::wstring value_type; @@ -357,14 +451,8 @@ namespace odb const std::wstring&); }; - // const wchar_t* specialization + // wchar_t*/const wchar_t* specialization. // - struct c_wstring_image_traits - { - typedef details::buffer image_type; - static const bind::buffer_type bind_value = bind::text16; - }; - struct LIBODB_SQLITE_EXPORT c_wstring_value_traits { typedef const wchar_t* value_type; @@ -378,33 +466,130 @@ namespace odb }; template <> - struct image_traits: c_wstring_image_traits {}; + struct image_traits: wstring_image_traits {}; + + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits: + c_wstring_value_traits {}; + + template <> + struct image_traits: wstring_image_traits {}; template <> struct LIBODB_SQLITE_EXPORT default_value_traits: - c_wstring_value_traits + c_wstring_value_traits {}; + + // wchar_t[N] specialization. + // + struct LIBODB_SQLITE_EXPORT c_warray_value_traits_base { - typedef const wchar_t* query_type; + static void + set_value (wchar_t* 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 wchar_t* v, + std::size_t N); }; template - struct image_traits: c_wstring_image_traits {}; + struct image_traits: wstring_image_traits {}; template - struct default_value_traits: - c_wstring_value_traits + struct default_value_traits { + typedef wchar_t* value_type; typedef wchar_t query_type[N]; + typedef details::buffer image_type; + + static void + set_value (wchar_t* const& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + c_warray_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 wchar_t* v) + { + c_warray_value_traits_base::set_image (b, n, is_null, v, N); + } }; + // std::array (string) specialization. + // +#ifdef ODB_CXX11 template - struct image_traits: c_wstring_image_traits {}; + struct image_traits, id_text>: + wstring_image_traits {}; template - struct default_value_traits: - c_wstring_value_traits + struct default_value_traits, id_text> + { + 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_warray_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_warray_value_traits_base::set_image (b, n, is_null, v.data (), N); + } + }; +#endif + + // wchar_t specialization. + // + template <> + struct image_traits: wstring_image_traits {}; + + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits { - typedef const wchar_t query_type[N]; + typedef wchar_t value_type; + typedef wchar_t query_type; + typedef details::buffer image_type; + + static void + set_value (wchar_t& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + c_warray_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, + wchar_t v) + { + c_warray_value_traits_base::set_image (b, n, is_null, &v, 1); + } }; #endif // _WIN32 @@ -634,9 +819,7 @@ namespace odb struct default_type_traits; template - class type_traits: public default_type_traits - { - }; + class type_traits: public default_type_traits {}; // Integral types. // @@ -729,6 +912,12 @@ namespace odb }; template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_text; + }; + + template <> struct default_type_traits { static const database_type_id db_type_id = id_text; @@ -740,14 +929,20 @@ namespace odb static const database_type_id db_type_id = id_text; }; - template - struct default_type_traits + template <> + struct default_type_traits { static const database_type_id db_type_id = id_text; }; // Binary types. // + template + struct default_type_traits + { + static const database_type_id db_type_id = id_blob; + }; + template <> struct default_type_traits > { -- cgit v1.1