libpqxx
The C++ client library for PostgreSQL
pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR > Class Template Referencefinal

An SQL array received from the database. More...

#include <array.hxx>

Public Types

using value_type = ELEMENT
 The element type of values in this array. More...
 

Public Member Functions

 array (std::string_view data, connection const &cx, sl loc=sl::current())
 Parse an SQL array, read as text from a pqxx::result or stream. More...
 
 array (std::string_view data, encoding_group enc, sl loc=sl::current())
 
PQXX_PURE std::array< std::size_t, DIMENSIONS > const & sizes () const noexcept
 Return the sizes of this array in each of its dimensions. More...
 
template<std::integral... INDEX>
ELEMENT const & at (INDEX... index) const
 
template<std::integral... INDEX>
PQXX_PURE ELEMENT const & operator[] (INDEX... index) const
 Access element (without bounds check). More...
 

Static Public Member Functions

static constexpr PQXX_PURE std::size_t dimensions () noexcept
 How many dimensions does this array have? More...
 
static constexpr PQXX_PURE char separator () noexcept
 What is the separator used for parsing this array's values? More...
 

Array iteration

There is no "nice" way to iterate over a multi-dimensional array as yet. Instead, you can iterate over all the elements as if it were a simple, flat, one-dimensional array.

If this is a multi-dimensional array, iteration proceeds in row-major order. So for example, a two-dimensional array a would start at a[0, 0], then a[0, 1], and so on. Once it reaches the end of that first row, it moves on to element a[1, 0], and continues from there.

constexpr PQXX_PURE auto cbegin () const noexcept
 Begin iteration of individual elements. More...
 
constexpr PQXX_PURE auto cend () const noexcept
 Return end point of iteration. More...
 
constexpr PQXX_PURE auto begin () const noexcept
 Begin iteration of individual elements. More...
 
constexpr PQXX_PURE auto end () const noexcept
 Return endpoint of iteration. More...
 
constexpr PQXX_PURE auto crbegin () const noexcept
 Begin reverse iteration. More...
 
constexpr PQXX_PURE auto crend () const noexcept
 Return end point of reverse iteration. More...
 
constexpr PQXX_PURE auto rbegin () const noexcept
 
constexpr PQXX_PURE auto rend () const noexcept
 Return end point of reverse iteration. More...
 
constexpr std::size_t size () const noexcept
 Number of elements in the array. More...
 
constexpr auto ssize () const noexcept
 Number of elements in the array (as a signed number). More...
 
constexpr auto const & front () const noexcept
 Refer to the first element, if any. More...
 
constexpr auto const & back () const noexcept
 Refer to the last element, if any. More...
 

Detailed Description

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
class pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >

An SQL array received from the database.

Parses an SQL array from its text format, making it available as a container of C++-side values.

The array can have one or more dimensions. You must specify the number of dimensions at compile time. In each dimension, the array has a size which the array constructor determines at run time based on the SQL array's textual representation. The sizes of a given SQL array are consistent: if your array has two dimensions, for example, then it will have one "horizontal" size which determines the number of elements in each row; and it will have one "vertical" size which determines the number of rows.

Physical memory storage is "row-major." This means that the last of the dimensions represents a row. So in memory, element a[m][n] comes right before a[m][n+1].

Member Typedef Documentation

◆ value_type

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
using pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::value_type = ELEMENT

The element type of values in this array.

Constructor & Destructor Documentation

◆ array() [1/2]

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::array ( std::string_view  data,
connection const &  cx,
sl  loc = sl::current() 
)
inline

Parse an SQL array, read as text from a pqxx::result or stream.

Uses cx only during construction, to find out the text encoding in which it should interpret data.

Once the array constructor completes, destroying or moving the connection will not affect the array object in any way.

Exceptions
pqxx::unexpected_nullif the array contains a null value, and the ELEMENT type does not support null values.

◆ array() [2/2]

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::array ( std::string_view  data,
encoding_group  enc,
sl  loc = sl::current() 
)
inline

Member Function Documentation

◆ at()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
template<std::integral... INDEX>
ELEMENT const& pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::at ( INDEX...  index) const
inline

◆ back()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr auto const& pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::back ( ) const
inlineconstexprnoexcept

Refer to the last element, if any.

If the array is empty, dereferencing this results in undefined behaviour.

◆ begin()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::begin ( ) const
inlineconstexprnoexcept

Begin iteration of individual elements.

◆ cbegin()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::cbegin ( ) const
inlineconstexprnoexcept

Begin iteration of individual elements.

◆ cend()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::cend ( ) const
inlineconstexprnoexcept

Return end point of iteration.

◆ crbegin()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::crbegin ( ) const
inlineconstexprnoexcept

Begin reverse iteration.

◆ crend()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::crend ( ) const
inlineconstexprnoexcept

Return end point of reverse iteration.

◆ dimensions()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
static constexpr PQXX_PURE std::size_t pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::dimensions ( )
inlinestaticconstexprnoexcept

How many dimensions does this array have?

This value is known at compile time.

◆ end()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::end ( ) const
inlineconstexprnoexcept

Return endpoint of iteration.

◆ front()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr auto const& pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::front ( ) const
inlineconstexprnoexcept

Refer to the first element, if any.

If the array is empty, dereferencing this results in undefined behaviour.

◆ operator[]()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
template<std::integral... INDEX>
PQXX_PURE ELEMENT const& pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::operator[] ( INDEX...  index) const
inline

Access element (without bounds check).

Return element at given index. Blindly assumes that the index lies within the bounds of the array. This is likely to be slightly faster than at().

Multi-dimensional indexing using operator[] only works in C++23 or better. In older versions of C++ it will work only with single-dimensional arrays.

Warning
This function is marked as "pure." This means that if it fails, depending on your compiler, the exception may occur in a different place than you expected. The compiler may even find scenarios where it can avoid calling this operator, meaning that the exception does not happen at all. If you need more deterministic exception behaviour, use at().

◆ rbegin()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::rbegin ( ) const
inlineconstexprnoexcept

◆ rend()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr PQXX_PURE auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::rend ( ) const
inlineconstexprnoexcept

Return end point of reverse iteration.

◆ separator()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
static constexpr PQXX_PURE char pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::separator ( )
inlinestaticconstexprnoexcept

What is the separator used for parsing this array's values?

This value is known at compile time.

◆ size()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr std::size_t pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::size ( ) const
inlineconstexprnoexcept

Number of elements in the array.

This includes all elements, in all dimensions. Therefore it is the product of all values in sizes().

◆ sizes()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
PQXX_PURE std::array<std::size_t, DIMENSIONS> const& pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::sizes ( ) const
inlinenoexcept

Return the sizes of this array in each of its dimensions.

The last of the sizes is the number of elements in a single row. The size before that is the number of rows of elements, and so on. The first is the "outer" size.

◆ ssize()

template<not_borrowed ELEMENT, std::size_t DIMENSIONS = 1u, char SEPARATOR = array_separator<ELEMENT>>
constexpr auto pqxx::array< ELEMENT, DIMENSIONS, SEPARATOR >::ssize ( ) const
inlineconstexprnoexcept

Number of elements in the array (as a signed number).

This includes all elements, in all dimensions. Therefore it is the product of all values in sizes().

In principle, the size could get so large that it had no signed equivalent. If that can ever happen, this is your own problem and behaviour is undefined.

In practice however, I don't think ssize() could ever overflow. You'd need an array where each element takes up just one byte, such as Booleans, filling up more than half your address space. But the input string for that array would need at least two bytes per value: one for the value, one for the separating comma between elements. So even then you wouldn't have enough address space to create the array, even if your system allowed you to use your full address space.


The documentation for this class was generated from the following file: