From 866f0ce4fa567db60d46741a5c865cdf2741c619 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 10 Sep 2012 12:12:06 +0200 Subject: Add support for alternative UTF-16 image for TEXT in SQLite Use it to handle QString and support std::wstring on Windows. --- odb/sqlite/traits.hxx | 118 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 12 deletions(-) (limited to 'odb/sqlite/traits.hxx') diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index 65883b6..0de67f9 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -20,6 +20,7 @@ #include #include +#include #include namespace odb @@ -38,20 +39,27 @@ namespace odb // image_traits // - template + template struct image_traits; - template <> - struct image_traits {typedef long long image_type;}; + template + struct image_traits {typedef long long image_type;}; - template <> - struct image_traits {typedef double image_type;}; + template + struct image_traits {typedef double image_type;}; - 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;}; + // By default the text is in UTF-8. + // + static const bind::buffer_type bind_value = bind::text; + }; + + template + struct image_traits {typedef details::buffer image_type;}; // // value_traits @@ -97,7 +105,7 @@ namespace odb typedef W value_type; typedef wrapped_type query_type; - typedef typename image_traits::image_type image_type; + typedef typename image_traits::image_type image_type; typedef value_traits vtraits; @@ -136,7 +144,7 @@ namespace odb typedef W value_type; typedef wrapped_type query_type; - typedef typename image_traits::image_type image_type; + typedef typename image_traits::image_type image_type; typedef value_traits vtraits; @@ -184,7 +192,7 @@ namespace odb { typedef T value_type; typedef T query_type; - typedef typename image_traits::image_type image_type; + typedef typename image_traits::image_type image_type; static void set_value (T& v, const image_type& i, bool is_null) @@ -305,6 +313,92 @@ namespace odb { }; +#ifdef _WIN32 + // std::wstring specialization. Using UTF-16 binding. + // + template <> + struct image_traits + { + typedef details::buffer image_type; + static const bind::buffer_type bind_value = bind::text16; + }; + + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits + { + typedef std::wstring value_type; + typedef std::wstring query_type; + typedef details::buffer image_type; + + static void + set_value (std::wstring& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (reinterpret_cast (b.data ()), n / 2); + else + v.erase (); + } + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const std::wstring&); + }; + + // const wchar_t* specialization + // + template <> + 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; + typedef const wchar_t* query_type; + typedef details::buffer image_type; + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const wchar_t*); + }; + + template <> + struct image_traits: c_wstring_image_traits {}; + + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits: + c_wstring_value_traits + { + }; + + template + struct image_traits: c_wstring_image_traits {}; + + template + struct default_value_traits: + c_wstring_value_traits + { + }; + + template + struct image_traits: c_wstring_image_traits {}; + + template + struct default_value_traits: + c_wstring_value_traits + { + }; +#endif // _WIN32 + // std::vector (buffer) specialization. // template <> -- cgit v1.1