From 0d49ea1fe08cf1eab41a00149393a291c65a59d7 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 25 Jan 2024 20:32:06 +0300 Subject: Turn odb-tests repository into package for muti-package repository --- odb-tests/pgsql/custom/traits.hxx | 166 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 odb-tests/pgsql/custom/traits.hxx (limited to 'odb-tests/pgsql/custom/traits.hxx') diff --git a/odb-tests/pgsql/custom/traits.hxx b/odb-tests/pgsql/custom/traits.hxx new file mode 100644 index 0000000..c45dec0 --- /dev/null +++ b/odb-tests/pgsql/custom/traits.hxx @@ -0,0 +1,166 @@ +// file : pgsql/custom/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include // std::numeric_limits +#include +#include +#include // std::memcpy + +#include + +#include "test.hxx" // point + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits + { + public: + typedef point value_type; + typedef point query_type; + typedef details::buffer image_type; + + static void + set_value (point& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (is_null) + v = point (); + else + { + // Point format is "(x,y)". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '(' + is >> v.x; + is >> c; // ',' + is >> v.y; + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const point& v) + { + is_null = false; + std::ostringstream os; + + // The formula for the number of decimla digits required is given in: + // + // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf + // + os.precision (std::numeric_limits::digits10); + // os.precision (2 + std::numeric_limits::digits * 301/1000); + + os << '(' << v.x << ',' << v.y << ')'; + + const std::string& s (os.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), s.c_str (), n); + } + }; + + template <> + struct type_traits + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "(?)::POINT";} + }; + }; + + template <> + class value_traits, id_string> + { + public: + typedef std::vector value_type; + typedef value_type 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) + { + v.clear (); + + if (!is_null) + { + // Array format is "{n1,n2,n3...}". + // + char c; + std::istringstream is (std::string (b.data (), n)); + + is >> c; // '{' + + for (c = static_cast (is.peek ()); c != '}'; is >> c) + { + v.push_back (int ()); + is >> v.back (); + } + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + std::ostringstream os; + + os << '{'; + + for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;) + { + os << *i; + + if (++i != e) + os << ','; + } + + os << '}'; + + const std::string& s (os.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), s.c_str (), n); + } + }; + + template <> + struct type_traits > + { + static const database_type_id db_type_id = id_string; + + struct conversion + { + static const char* to () {return "(?)::INTEGER[]";} + }; + }; + } +} + +#endif // TRAITS_HXX -- cgit v1.1