libpqxx
The C++ client library for PostgreSQL
pqxx::string_traits< TYPE > Struct Template Referencefinal

Traits class for use in string conversions. More...

#include <strconv.hxx>

Static Public Member Functions

static std::size_t size_buffer (TYPE const &value) noexcept
 Estimate how much buffer space is needed to represent value as SQL text. More...
 
static std::string_view to_buf (std::span< char > buf, TYPE const &value, ctx={})
 Return a string_view representing value in SQL text. More...
 
static TYPE from_string (std::string_view text, ctx={})
 Parse a string representation of a TYPE value. More...
 

Detailed Description

template<typename TYPE>
struct pqxx::string_traits< TYPE >

Traits class for use in string conversions.

Specialize this template for a type for which you wish to add to_string and from_string support.

String conversions are not meant to work for nulls. Check for null before converting a value of TYPE to a string, or vice versa, and handle them separately.

Member Function Documentation

◆ from_string()

template<typename TYPE >
std::chrono::year_month_day pqxx::string_traits< TYPE >::from_string ( std::string_view  text,
ctx  = {} 
)
inlinestatic

Parse a string representation of a TYPE value.

Throws conversion_error if value does not meet the expected format for a value of this type.

Warning
A null value has no string representation. Do not parse a null.
If you convert a string to std::string_view, you're basically just getting a pointer into the original buffer. So, the string_view will become invalid when the original string's lifetime ends, or when it gets overwritten. Do not access the string_view after that!

If there is no support for converting from an SQL string to this type, simply leave this function out of the struct.

◆ size_buffer()

template<typename TYPE >
static std::size_t pqxx::string_traits< TYPE >::size_buffer ( TYPE const &  value)
inlinestaticnoexcept

Estimate how much buffer space is needed to represent value as SQL text.

The estimate may be a little pessimistic, if it saves time.

◆ to_buf()

template<typename TYPE >
std::string_view pqxx::string_traits< TYPE >::to_buf ( std::span< char >  buf,
TYPE const &  value,
ctx  = {} 
)
inlinestatic

Return a string_view representing value in SQL text.

Produces a view on a PostgreSQL string representation for value.

Warning
A null value has no string representation. Do not pass a null.

Uses buf to store string contents, if needed. The returned string view may point somewhere inside that buffer, or to a compile-time constant, or just directly to value. Even if the string does live in buf, it may not start at the exact beginning of buf.

The resulting view stays valid for as long as both the buffer space to which buf points, and value, remain accessible and unmodified.

Exceptions
pqxx::conversion_overrunif buf is not large enough. For maximum performance, this is a conservative estimate. It may complain about a buffer which is actually large enough for your value, if an exact check would be too expensive.

If there is no support for converting this type to an SQL string, simply leave this function out of the struct.


The documentation for this struct was generated from the following files: