14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
22 #include <type_traits>
26 #include "pqxx/connection.hxx"
27 #include "pqxx/internal/array-composite.hxx"
28 #include "pqxx/internal/encoding_group.hxx"
29 #include "pqxx/internal/encodings.hxx"
53 typename ELEMENT, std::size_t DIMENSIONS = 1u,
54 char SEPARATOR = array_separator<ELEMENT>>
69 array{data,
pqxx::internal::enc_group(conn.encoding_id())}
75 constexpr std::size_t
dimensions() noexcept {
return DIMENSIONS; }
82 std::array<std::size_t, DIMENSIONS>
const &
sizes() noexcept
87 template<
typename... INDEX> ELEMENT
const &
at(INDEX... index)
const
89 static_assert(
sizeof...(index) == DIMENSIONS);
90 check_bounds(index...);
91 return m_elts.at(locate(index...));
103 template<
typename... INDEX> ELEMENT
const &
operator[](INDEX... index)
const
105 static_assert(
sizeof...(index) == DIMENSIONS);
106 return m_elts[locate(index...)];
115 constexpr
auto cbegin() const noexcept {
return m_elts.cbegin(); }
117 constexpr
auto cend() const noexcept {
return m_elts.cend(); }
119 constexpr
auto crbegin() const noexcept {
return m_elts.crbegin(); }
121 constexpr
auto crend() const noexcept {
return m_elts.crend(); }
127 constexpr std::size_t
size() const noexcept {
return m_elts.size(); }
145 constexpr
auto ssize() const noexcept
147 return static_cast<std::ptrdiff_t
>(
size());
153 constexpr
auto front() const noexcept {
return m_elts.front(); }
158 constexpr
auto back() const noexcept {
return m_elts.back(); }
170 void check_dims(std::string_view data)
172 auto sz{std::size(data)};
173 if (sz < DIMENSIONS * 2)
174 throw conversion_error{pqxx::internal::concat(
175 "Trying to parse a ", DIMENSIONS,
"-dimensional array out of '", data,
188 throw conversion_error{
"Malformed array: does not start with '{'."};
189 for (std::size_t i{0}; i < DIMENSIONS; ++i)
191 throw conversion_error{pqxx::internal::concat(
192 "Expecting ", DIMENSIONS,
"-dimensional array, but found ", i,
".")};
193 if (data[DIMENSIONS] ==
'{')
194 throw conversion_error{pqxx::internal::concat(
195 "Tried to parse ", DIMENSIONS,
196 "-dimensional array from array data that has more dimensions.")};
197 for (std::size_t i{0}; i < DIMENSIONS; ++i)
198 if (data[sz - 1 - i] !=
'}')
199 throw conversion_error{
200 "Malformed array: does not end in the right number of '}'."};
203 explicit array(std::string_view data, pqxx::internal::encoding_group enc)
205 using group = pqxx::internal::encoding_group;
208 case group::MONOBYTE: parse<group::MONOBYTE>(data);
break;
209 case group::BIG5: parse<group::BIG5>(data);
break;
210 case group::EUC_CN: parse<group::EUC_CN>(data);
break;
211 case group::EUC_JP: parse<group::EUC_JP>(data);
break;
212 case group::EUC_KR: parse<group::EUC_KR>(data);
break;
213 case group::EUC_TW: parse<group::EUC_TW>(data);
break;
214 case group::GB18030: parse<group::GB18030>(data);
break;
215 case group::GBK: parse<group::GBK>(data);
break;
216 case group::JOHAB: parse<group::JOHAB>(data);
break;
217 case group::MULE_INTERNAL: parse<group::MULE_INTERNAL>(data);
break;
218 case group::SJIS: parse<group::SJIS>(data);
break;
219 case group::UHC: parse<group::UHC>(data);
break;
220 case group::UTF8: parse<group::UTF8>(data);
break;
228 std::size_t parse_field_end(std::string_view data, std::size_t here)
const
230 auto const sz{std::size(data)};
237 throw conversion_error{
"Array looks truncated."};
241 throw conversion_error{
"Array contains double separator."};
242 case '}':
throw conversion_error{
"Array contains trailing separator."};
248 throw conversion_error{pqxx::internal::concat(
249 "Unexpected character in array: ",
250 static_cast<unsigned>(
static_cast<unsigned char>(data[here])),
251 " where separator or closing brace expected.")};
262 constexpr std::size_t estimate_elements(std::string_view data)
const noexcept
267 auto const separators{
268 std::count(std::begin(data), std::end(data), SEPARATOR)};
272 return static_cast<std::size_t
>(separators + 1);
275 template<pqxx::
internal::encoding_group ENC>
276 void parse(std::string_view data)
278 static_assert(DIMENSIONS > 0u,
"Can't create a zero-dimensional array.");
279 auto const sz{std::size(data)};
282 m_elts.reserve(estimate_elements(data));
288 std::size_t know_extents_from{DIMENSIONS};
296 constexpr std::size_t outer{std::size_t{0u} - std::size_t{1u}};
301 std::size_t dim{outer};
305 std::array<std::size_t, DIMENSIONS> extents{};
307 for (
auto const e : extents) assert(e == 0u);
314 if (data[here] ==
'{')
319 if (know_extents_from != DIMENSIONS)
320 throw conversion_error{
321 "Array text representation closed and reopened its outside "
327 if (dim >= (DIMENSIONS - 1))
328 throw conversion_error{
329 "Array seems to have inconsistent number of dimensions."};
337 else if (data[here] ==
'}')
340 throw conversion_error{
"Array has spurious '}'."};
341 if (dim < know_extents_from)
345 m_extents[dim] = extents[dim];
346 know_extents_from = dim;
350 if (extents[dim] != m_extents[dim])
351 throw conversion_error{
"Rows in array have inconsistent sizes."};
357 here = parse_field_end(data, here);
363 if (dim != DIMENSIONS - 1)
364 throw conversion_error{
365 "Malformed array: found element where sub-array was expected."};
366 assert(dim != outer);
371 case '\0':
throw conversion_error{
"Unexpected zero byte in array."};
372 case ',':
throw conversion_error{
"Array contains empty field."};
383 end = pqxx::internal::scan_double_quoted_string<ENC>(
384 std::data(data), std::size(data), here);
386 std::string
const buf{
387 pqxx::internal::parse_double_quoted_string<ENC>(
388 std::data(data), end, here)};
389 m_elts.emplace_back(from_string<ELEMENT>(buf));
396 end = pqxx::internal::scan_unquoted_string<ENC, SEPARATOR, '}'>(
397 std::data(data), std::size(data), here);
398 std::string_view
const field{
399 std::string_view{std::data(data) + here, end - here}};
405 throw unexpected_null{pqxx::internal::concat(
406 "Array contains a null ", type_name<ELEMENT>,
407 ". Consider making it an array of std::optional<",
408 type_name<ELEMENT>,
"> instead.")};
411 m_elts.emplace_back(from_string<ELEMENT>(field));
415 here = parse_field_end(data, here);
420 throw conversion_error{
"Malformed array; may be truncated."};
421 assert(know_extents_from == 0);
427 void init_factors() noexcept
429 std::size_t factor{1};
430 for (std::size_t dim{DIMENSIONS - 1}; dim > 0; --dim)
432 factor *= m_extents[dim];
433 m_factors[dim - 1] = factor;
438 template<
typename... INDEX> std::size_t locate(INDEX... index)
const noexcept
441 sizeof...(index) == DIMENSIONS,
442 "Indexing array with wrong number of dimensions.");
443 return add_index(index...);
446 template<
typename OUTER,
typename... INDEX>
447 constexpr std::size_t add_index(OUTER outer, INDEX... indexes)
const noexcept
449 std::size_t
const first{check_cast<std::size_t>(outer,
"array index"sv)};
450 if constexpr (
sizeof...(indexes) == 0)
456 static_assert(
sizeof...(indexes) < DIMENSIONS);
458 constexpr
auto dimension{DIMENSIONS - (
sizeof...(indexes) + 1)};
459 static_assert(dimension < DIMENSIONS);
460 return first * m_factors[dimension] + add_index(indexes...);
467 template<
typename OUTER,
typename... INDEX>
468 constexpr
void check_bounds(OUTER outer, INDEX... indexes)
const
470 std::size_t
const first{check_cast<std::size_t>(outer,
"array index"sv)};
471 static_assert(
sizeof...(indexes) < DIMENSIONS);
473 constexpr
auto dimension{DIMENSIONS - (
sizeof...(indexes) + 1)};
474 static_assert(dimension < DIMENSIONS);
475 if (first >= m_extents[dimension])
476 throw range_error{pqxx::internal::concat(
477 "Array index for dimension ", dimension,
" is out of bounds: ", first,
478 " >= ", m_extents[dimension])};
481 if constexpr (
sizeof...(indexes) > 0)
482 check_bounds(indexes...);
486 std::vector<ELEMENT> m_elts;
489 std::
array<std::
size_t, DIMENSIONS> m_extents;
499 std::
array<std::
size_t, DIMENSIONS - 1> m_factors;
550 std::string_view input,
551 internal::encoding_group = internal::encoding_group::MONOBYTE);
560 std::pair<juncture, std::string>
get_next() {
return (this->*m_impl)(); }
563 std::string_view m_input;
566 std::size_t m_pos = 0u;
574 using implementation = std::pair<juncture, std::string> (
array_parser::*)();
577 static implementation
578 specialize_for_encoding(pqxx::internal::encoding_group enc);
581 implementation m_impl;
584 template<pqxx::
internal::encoding_group>
585 std::pair<juncture, std::string> parse_array_step();
587 template<pqxx::
internal::encoding_group>
588 std::string::size_type scan_double_quoted_string()
const;
589 template<pqxx::
internal::encoding_group>
590 std::string parse_double_quoted_string(std::string::size_type end)
const;
591 template<pqxx::
internal::encoding_group>
592 std::string::size_type scan_unquoted_string()
const;
593 template<pqxx::
internal::encoding_group>
594 std::string parse_unquoted_string(std::string::size_type end)
const;
596 template<pqxx::
internal::encoding_group>
597 std::string::size_type scan_glyph(std::string::size_type pos)
const;
598 template<pqxx::
internal::encoding_group>
599 std::string::size_type
600 scan_glyph(std::string::size_type pos, std::string::size_type end)
const;
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:33
An SQL array received from the database.
Definition: array.hxx:56
constexpr auto back() const noexcept
Refer to the last element, if any.
Definition: array.hxx:158
constexpr auto cend() const noexcept
Return end point of iteration.
Definition: array.hxx:117
std::array< std::size_t, DIMENSIONS > const & sizes() noexcept
Return the sizes of this array in each of its dimensions.
Definition: array.hxx:82
constexpr auto crbegin() const noexcept
Begin reverse iteration.
Definition: array.hxx:119
constexpr std::size_t size() const noexcept
Number of elements in the array.
Definition: array.hxx:127
ELEMENT const & operator[](INDEX... index) const
Access element (without bounds check).
Definition: array.hxx:103
constexpr auto ssize() const noexcept
Number of elements in the array (as a signed number).
Definition: array.hxx:145
constexpr std::size_t dimensions() noexcept
How many dimensions does this array have?
Definition: array.hxx:75
constexpr auto cbegin() const noexcept
Begin iteration of individual elements.
Definition: array.hxx:115
ELEMENT const & at(INDEX... index) const
Definition: array.hxx:87
array(std::string_view data, connection const &conn)
Parse an SQL array, read as text from a pqxx::result or stream.
Definition: array.hxx:68
constexpr auto crend() const noexcept
Return end point of reverse iteration.
Definition: array.hxx:121
constexpr auto front() const noexcept
Refer to the first element, if any.
Definition: array.hxx:153
Low-level array parser.
Definition: array.hxx:527
juncture
What's the latest thing found in the array?
Definition: array.hxx:531
std::pair< juncture, std::string > get_next()
Parse the next step in the array.
Definition: array.hxx:560
Connection to a database.
Definition: connection.hxx:233
static TYPE null()
Return a null value.
static bool has_null
Does this type have a null value?
Definition: strconv.hxx:93