8 #ifndef TIP_DB_PG_PROTOCOL_IO_TRAITS_HPP_
9 #define TIP_DB_PG_PROTOCOL_IO_TRAITS_HPP_
17 #include <type_traits>
18 #include <boost/optional.hpp>
22 #include <tip/util/streambuf.hpp>
56 template <
typename T >
76 template <
typename T, protocol_data_format >
79 template <
typename T, protocol_data_format >
87 template <
typename T, protocol_data_format F >
103 template < protocol_data_format F,
typename T >
125 template < protocol_data_format F,
typename T,
typename InputIterator >
141 template < protocol_data_format F,
typename T >
142 typename protocol_io_traits< T, F >::formatter_type
160 template < protocol_data_format F,
typename T >
164 return protocol_writer<F>(value)(buffer);
177 template < protocol_data_format F,
typename T,
typename OutputIterator >
181 return protocol_writer<F>(value)(out);
190 template <
typename T >
202 template <
typename T >
216 template <
typename T, protocol_binary_type TYPE >
226 template <
typename T >
242 template <
typename InputIterator >
244 operator()( InputIterator begin, InputIterator end );
247 template <
typename T >
248 struct binary_data_parser < T,
OTHER >;
256 template <
typename T, protocol_binary_type TYPE >
266 template <
typename T >
280 operator()(std::vector<byte>& buffer);
282 template <
typename OutputIterator >
284 operator()(OutputIterator);
291 template < o
ids::type::o
id_type TypeO
id,
typename T >
294 typedef typename std::decay<T>::type
type;
321 template < o
ids::type::o
id_type TypeO
id >
331 template <
typename T >
336 template <
typename T>
339 static void test(...);
340 template <
typename U >
static bool test(U&);
341 static std::istream& is;
344 static constexpr
bool value = std::is_same<
345 decltype( test( std::declval< std::istream& >() >> std::declval< T& >()) ),
350 template <
typename T>
353 static void test(...);
354 template <
typename U >
static bool test(U&);
356 static constexpr
bool value = std::is_same<
357 decltype( test( std::declval< std::ostream& >() << std::declval< T >() ) ),
362 template <
typename T, protocol_data_format format >
364 template <
typename T >
366 : std::integral_constant< bool, has_input_operator< T >::value > {};
371 template <
typename T, protocol_data_format format >
373 template <
typename T >
375 : std::integral_constant< bool, has_output_operator< T >::value > {};
388 template <
typename T >
400 template <
typename T >
412 "Text format parser for smallint");
414 "Binary format parser for smallint");
416 "Best parser for smallint is binary");
419 "Text format writer for smallint");
421 "Binary format writer for smallint");
423 "Best writer for smallint is binary");
426 "Text format parser for integer");
428 "Binary format parser for integer");
430 "Best parser for integer is binary");
433 "Text format writer for integer");
435 "Binary format writer for integer");
437 "Best writer for integer is binary");
440 "Text format parser for bigint");
442 "Binary format parser for bigint");
444 "Best parser for bigint is binary");
447 "Text format writer for bigint");
449 "Binary format writer for bigint");
451 "Best writer for bigint is binary");
456 "Text format parser for float");
461 "Best parser for float is text");
464 "Text format writer for float");
469 "Best writer for float is text");
472 "Text format parser for double");
477 "Best parser for double is text");
480 "Text format writer for double");
485 "Best writer for double is text");
493 template <
typename T >
503 std::ostringstream os;
504 os << base_type::value;
505 return os.str().size();
510 std::ostringstream os;
511 os << base_type::value;
513 std::copy(str.begin(), str.end(), std::back_inserter(buffer));
521 template <
typename T >
536 template <
typename InputIterator >
538 operator()(InputIterator begin, InputIterator end);
541 template <
typename T >
544 detail::protocol_binary_selector< typename std::decay<T>::type >::value > {
553 template <
typename T >
556 detail::protocol_binary_selector< typename std::decay<T>::type >::value > {
579 return base_type::value.size();
582 template <
typename InputIterator >
584 operator()(InputIterator begin, InputIterator end);
588 static_assert(has_parser<std::string, TEXT_DATA_FORMAT>::value,
589 "Text data parser for std::string");
590 static_assert(!has_parser<std::string, BINARY_DATA_FORMAT>::value,
591 "No binary data parser for std::string");
593 "Best parser for std::string is binary");
607 return base_type::value.size();
612 out << base_type::value;
618 auto iter = std::copy(base_type::value.begin(), base_type::value.end(),
619 std::back_inserter(buffer));
626 static_assert(has_formatter<std::string, TEXT_DATA_FORMAT>::value,
627 "Text data parser for std::string");
628 static_assert(!has_formatter<std::string, BINARY_DATA_FORMAT>::value,
629 "No binary data parser for std::string");
631 "Best parser for std::string is binary");
655 template <
typename InputIterator >
657 operator()(InputIterator begin, InputIterator end);
677 template <
typename InputIterator >
679 operator()( InputIterator begin, InputIterator end );
685 "Text data parser for bool");
687 "Binary data parser for bool");
689 "Best parser for bool is binary");
695 template <
typename T >
707 operator() (std::istream& in)
710 if (query_parse(tmp)(in)) {
722 if (query_parse(tmp)(buffer)) {
730 template <
typename InputIterator >
732 operator()(InputIterator begin, InputIterator end);
738 template <
typename T >
753 if (base_type::value)
758 template <
typename InputIterator >
763 InputIterator c = protocol_read< BINARY_DATA_FORMAT >(begin, end, tmp);
788 return base_type::value.data.size();
791 operator() (std::istream& in);
794 operator() (buffer_type& buffer);
796 template <
typename InputIterator >
798 operator()( InputIterator begin, InputIterator end );
814 return base_type::value.data.size();
816 template <
typename InputIterator >
818 operator()( InputIterator begin, InputIterator end );
boost::int_t< 64 >::exact bigint
8-byte integer, to match PostgreSQL bigint and bigserial types
std::decay< T >::type type
FLOATING_POINT Floating point types, requiring endianness conversion.
Template for specifying mapping between a C++ type and PostgreSQL type oid.
tip::util::input_iterator_buffer buffer_type
tip::util::input_iterator_buffer buffer_type
detail::formatter_base< T > base_type
std::decay< T >::type value_type
OTHER Other types, require specialization.
std::integral_constant< protocol_binary_type, INTEGRAL > integral_binary_type
Integral datatypes. Selects binary parser specialization with network byte order conversion.
InputIterator protocol_read(InputIterator begin, InputIterator end, T &value)
Read value from input buffer.
protocol_parser(value_type &v)
detail::parser_base< T > base_type
base_type::value_type value_type
static constexpr protocol_data_format value
tip::util::input_iterator_buffer buffer_type
boost::int_t< 32 >::exact integer
4-byte integer, to match PostgreSQL integer and serial types
tip::util::input_iterator_buffer input_buffer_type
boost::uint_t< 64 >::exact ubigint
8-byte unsigned integer
Template parser selector.
base_type::value_type value_type
Base structure for a binary data parser. Has no definition.
binary_data_parser(value_type &val)
detail::parser_base< bytea > base_type
protocol_binary_type
Enumeration for binary parser/formatter template selection.
base_type::value_type value_type
protocol_formatter(value_type const &val)
protocol_parser(value_type &v)
protocol_formatter(value_type const &v)
bool operator()(std::ostream &out)
protocol_parser(value_type &v)
base_type::value_type value_type
boost::uint_t< 16 >::exact usmallint
2-byte unsigned integer
base_type::value_type value_type
Base template struct for a data parser.
detail::formatter_base< std::string > base_type
parser_base< T > base_type
protocol_formatter< T, F > formatter_type
protocol_parser(value_type &v)
base_type::value_type value_type
std::integral_constant< protocol_binary_type, FLOATING_POINT > floating_point_binary_type
Floating point datatypes. Binary parser is not implemented yet.
std::integral_constant< protocol_binary_type, OTHER > other_binary_type
Types other then integral or floating point. Require explicit specialization of a binary data parser...
size_t size() const
data size
tip::util::input_iterator_buffer buffer_type
boost::uint_t< 32 >::exact uinteger
4-byte unsigned integer
base_type::value_type value_type
Binary data, matches PostgreSQL bytea type.
Base structure for specifying mapping between C++ data type and PostgreSQL type oid.
boost::int_t< 16 >::exact smallint
2-byte integer, to match PostgreSQL smallint and smallserial types
void register_binary_parser(oids::type::oid_type id)
Mark a type oid that it has a binary parser, so that pg_async can request data in binary format...
base_type::value_type value_type
static constexpr oids::type::oid_type type_oid
bool protocol_write(std::vector< byte > &buffer, T const &value)
Write value to a buffer.
bool operator()(std::vector< char > &buffer)
bool has_binary_parser(oids::type::oid_type id)
Check if there is a binary parser for the specified oid.
protocol_parser< T, F > parser_type
protocol_io_traits< T, F >::parser_type protocol_reader(T &value)
Helper function to create a protocol parser.
parser_base(value_type &val)
detail::parser_base< boost::optional< T > > base_type
static constexpr bool value
bool operator()(std::vector< char > &buffer)
detail::parser_base< std::string > base_type
protocol_parser< T, value > type
protocol_io_traits< T, F >::formatter_type protocol_writer(T const &value)
Helper function to create a protocol formatter.
protocol_parser(value_type &v)
Metafunction for specifying binary procotocol type.
INTEGRAL Integral types, requiring endianness conversion.
detail::parser_base< bool > base_type