Loading...
Searching...
No Matches
composite.hxx
#ifndef PQXX_H_COMPOSITE
#define PQXX_H_COMPOSITE
#if !defined(PQXX_HEADER_PRE)
# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
#endif
#include "pqxx/internal/array-composite.hxx"
#include "pqxx/internal/concat.hxx"
#include "pqxx/util.hxx"
namespace pqxx
{
template<typename... T>
inline void parse_composite(
pqxx::internal::encoding_group enc, std::string_view text, T &...fields)
{
static_assert(sizeof...(fields) > 0);
auto const scan{pqxx::internal::get_glyph_scanner(enc)};
auto const data{std::data(text)};
auto const size{std::size(text)};
if (size == 0)
throw conversion_error{"Cannot parse composite value from empty string."};
std::size_t here{0}, next{scan(data, size, here)};
if (next != 1 or data[here] != '(')
throw conversion_error{
internal::concat("Invalid composite value string: ", text)};
here = next;
// TODO: Reuse parse_composite_field specialisation across calls.
constexpr auto num_fields{sizeof...(fields)};
std::size_t index{0};
(pqxx::internal::specialize_parse_composite_field<T>(enc)(
index, text, here, fields, num_fields - 1),
...);
if (here != std::size(text))
throw conversion_error{internal::concat(
"Composite value did not end at the closing parenthesis: '", text,
"'.")};
if (text[here - 1] != ')')
throw conversion_error{internal::concat(
"Composive value did not end in parenthesis: '", text, "'")};
}
template<typename... T>
inline void parse_composite(std::string_view text, T &...fields)
{
parse_composite(pqxx::internal::encoding_group::MONOBYTE, text, fields...);
}
} // namespace pqxx
namespace pqxx::internal
{
constexpr char empty_composite_str[]{"()"};
} // namespace pqxx::internal
namespace pqxx
{
template<typename... T>
[[nodiscard]] inline std::size_t
composite_size_buffer(T const &...fields) noexcept
{
constexpr auto num{sizeof...(fields)};
// Size for a multi-field composite includes room for...
// + opening parenthesis
// + field budgets
// + separating comma per field
// - comma after final field
// + closing parenthesis
// + terminating zero
if constexpr (num == 0)
return std::size(pqxx::internal::empty_composite_str);
else
return 1 + (pqxx::internal::size_composite_field_buffer(fields) + ...) +
num + 1;
}
template<typename... T>
inline char *composite_into_buf(char *begin, char *end, T const &...fields)
{
if (std::size_t(end - begin) < composite_size_buffer(fields...))
throw conversion_error{
"Buffer space may not be enough to represent composite value."};
constexpr auto num_fields{sizeof...(fields)};
if constexpr (num_fields == 0)
{
constexpr char empty[]{"()"};
std::memcpy(begin, empty, std::size(empty));
return begin + std::size(empty);
}
char *pos{begin};
*pos++ = '(';
(pqxx::internal::write_composite_field<T>(pos, end, fields), ...);
// If we've got multiple fields, "backspace" that last comma.
if constexpr (num_fields > 1)
--pos;
*pos++ = ')';
*pos++ = '\0';
return pos;
}
} // namespace pqxx
#endif
pqxx::conversion_error
Value conversion failed, e.g. when converting "Hello" to int.
Definition except.hxx:283
pqxx::internal
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
pqxx::internal::concat
std::string concat(TYPE... item)
Efficiently combine a bunch of items into one big string.
Definition concat.hxx:31
pqxx::internal::size_composite_field_buffer
std::size_t size_composite_field_buffer(T const &field)
Conservatively estimate buffer size needed for a composite field.
Definition array-composite.hxx:309
pqxx::internal::get_glyph_scanner
PQXX_LIBEXPORT glyph_scanner_func * get_glyph_scanner(encoding_group)
Look up the glyph scanner function for a given encoding group.
pqxx::internal::specialize_parse_composite_field
composite_field_parser< T > specialize_parse_composite_field(encoding_group enc)
Look up implementation of parse_composite_field for ENC.
Definition array-composite.hxx:274
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
pqxx::composite_into_buf
char * composite_into_buf(char *begin, char *end, T const &...fields)
Render a series of values as a single composite SQL value.
Definition composite.hxx:123
pqxx::composite_size_buffer
std::size_t composite_size_buffer(T const &...fields) noexcept
Estimate the buffer size needed to represent a value of a composite type.
Definition composite.hxx:96
pqxx::parse_composite
void parse_composite(pqxx::internal::encoding_group enc, std::string_view text, T &...fields)
Parse a string representation of a value of a composite type.
Definition composite.hxx:35