11 #ifndef PQXX_STRCONV_HXX
12 #define PQXX_STRCONV_HXX
14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
69 template<
typename TYPE,
typename ENABLE =
void>
struct nullness final
132 template<
typename TYPE, TYPE null_value>
struct all_null
147 [[nodiscard]]
PQXX_PURE static constexpr TYPE
null() {
return null_value; }
217 [[nodiscard]]
static inline std::size_t
241 [[nodiscard]]
static inline std::string_view
242 to_buf(std::span<char> buf, TYPE
const &value,
ctx = {});
258 [[nodiscard]]
static inline TYPE
275 [[noreturn]]
static std::string_view
276 to_buf(std::span<char>, TYPE
const &,
ctx = {}) =
delete;
278 [[noreturn]]
static std::size_t
size_buffer(TYPE
const &) noexcept =
delete;
359 template<
typename TYPE>
361 requires(
zview out,
char *begin,
char *end, TYPE
const &value) {
366 template<
typename TYPE>
368 std::string_view out, std::span<char> buf, TYPE
const &value,
ctx c) {
375 template<
typename TYPE>
385 template<
typename TYPE>
397 template<
typename... TYPE>
398 [[nodiscard]]
inline constexpr std::size_t
416 template<
typename TYPE>
417 requires(pqxx::internal::to_buf_7<TYPE> or pqxx::internal::to_buf_8<TYPE>)
418 consteval
bool supports_to_buf_8()
428 template<
typename TYPE>
429 [[nodiscard]]
inline std::string_view
430 to_buf(std::span<char> buf, TYPE
const &value,
ctx c = {})
432 using traits = string_traits<TYPE>;
433 if constexpr (supports_to_buf_8<TYPE>())
439 auto const begin{std::data(buf)}, end{begin + std::size(buf)};
452 template<
typename TYPE>
453 [[nodiscard]]
inline std::size_t
456 auto const data{std::data(buf)};
457 std::string_view out{
to_buf(buf, value, c)};
458 auto const sz{std::size(out)};
462 if (not std::empty(out)) [[likely]]
464 if (std::cmp_greater(std::size(out), std::size(buf)))
465 throw conversion_overrun{
467 "Buffer too small to convert {} value to string (needs a {}-byte "
469 name_type<TYPE>(), std::size(out)),
471 std::memmove(data, std::data(out), sz);
482 template<
typename TYPE>
484 pqxx::internal::from_string_7<TYPE> or pqxx::internal::from_string_8<TYPE>)
485 consteval
bool supports_from_string_8()
504 template<
typename TYPE>
507 if constexpr (supports_from_string_8<TYPE>())
543 value = from_string<T>(text, c);
553 template<
typename TYPE>
554 inline std::string
to_string(TYPE
const &value,
ctx c = {});
578 [[nodiscard]]
static constexpr std::string_view
579 to_buf(std::span<char> buf, ENUM
const &value,
ctx c = {})
586 return static_cast<ENUM
>(pqxx::from_string<impl_type>(text, c));
589 [[nodiscard]]
static constexpr std::size_t
597 static constexpr
impl_type to_underlying(ENUM
const &value) noexcept
617 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
619 [[maybe_unused, deprecated("Use name_type() instead of type_name.")]] \
620 constexpr inline std::string_view type_name<ENUM>{#ENUM}; \
621 template<> inline constexpr std::string_view name_type<ENUM>() noexcept \
626 struct string_traits<ENUM> final : pqxx::internal::enum_traits<ENUM> \
640 template<
typename... TYPE>
641 [[nodiscard, deprecated(
"Pass span and string_view.")]]
642 inline std::vector<std::string_view>
643 to_buf(
char *begin,
char const *end, TYPE... value)
645 assert(begin <= end);
653 begin, check_cast<std::size_t>(
654 end - begin,
"string_view too large.", sl::current())};
655 std::size_t here{0u};
656 return {[&here, buf](
auto v) {
659 assert(start < here);
660 assert(here <= std::size(buf));
662 assert(buf[here - 1] ==
'\0');
664 auto len{here - start - 1};
665 return std::string_view{std::data(buf) + start, len};
678 template<
typename... TYPE>
679 inline std::vector<std::string_view>
683 std::size_t here{0u};
684 return {[&here, buf, &c](
auto v) {
687 assert(start < here);
688 assert(here <= std::size(buf));
690 auto const len{here - start};
691 return std::string_view{std::data(buf) + start, len};
709 template<
typename... TYPE>
710 inline std::vector<std::string_view>
714 std::size_t here{0u};
716 return {[&here, buf, &c](
auto v) {
719 assert(start < here);
720 assert(here <= std::size(buf));
722 auto const len{here - start};
723 return std::string_view{std::data(buf) + start, len};
732 template<
typename TYPE>
744 template<
typename TYPE> [[nodiscard]]
inline constexpr
bool has_null() noexcept
746 using base_type = std::remove_cvref_t<TYPE>;
754 template<
typename TYPE>
757 using base_type = std::remove_cvref_t<TYPE>;
763 template<
typename TYPE>
764 [[nodiscard]]
inline constexpr
bool is_null(TYPE
const &value) noexcept
766 using base_type = std::remove_cvref_t<TYPE>;
767 if constexpr (always_null<TYPE>())
780 template<
typename TYPE>
784 using base_type = std::remove_cvref_t<TYPE>;
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
Private namespace for libpqxx's internal use; do not access.
Definition: connection.cxx:333
concept to_buf_7
Signature for string_traits<TYPE>::to_buf() in libpqxx 7.
Definition: strconv.hxx:360
concept from_string_8
Signature for string_traits<TYPE>::from_string() in libpqxx 8.
Definition: strconv.hxx:376
concept from_string_7
Signature for string_traits<TYPE>::from_string() in libpqxx 7.
Definition: strconv.hxx:386
concept to_buf_8
Signature for string_traits<TYPE>::to_buf() in libpqxx 8.
Definition: strconv.hxx:367
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::string_view to_buf(std::span< char > buf, TYPE const &value, ctx c={})
Represent value as SQL text, optionally using buf as storage.
Definition: strconv.hxx:430
constexpr bool has_null() noexcept
Does TYPE have one or more inherent null values?
Definition: strconv.hxx:744
constexpr TYPE make_null() requires(pqxx
Return a null value of TYPE.
Definition: strconv.hxx:781
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition: strconv.hxx:816
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
constexpr format param_format(std::optional< T > const &value)
Definition: conversions.hxx:313
T from_string(field const &value, ctx c={})
Convert a field's value to type T.
Definition: field.hxx:831
PQXX_LIBEXPORT std::string to_string(field_ref const &value, ctx)
Convert a field_ref to a string.
Definition: field.hxx:891
encoding_group
Definition: encoding_group.hxx:40
@ unknown
Default: indeterminate encoding. All we know is it supports ASCII.
constexpr std::size_t size_buffer(TYPE const &...value) noexcept
Estimate how much buffer space is needed to represent values as a string.
Definition: strconv.hxx:399
constexpr bool is_null(TYPE const &value) noexcept
Is value a null?
Definition: strconv.hxx:764
std::vector< std::string_view > to_buf(char *begin, char const *end, TYPE... value)
Convert multiple values to strings inside a single buffer.
Definition: strconv.hxx:643
requires(pqxx::internal::to_buf_7< TYPE > or pqxx::internal::to_buf_8< TYPE >) const eval bool supports_to_buf_8()
Is the libpqxx 8 version of to_buf() supported for TYPE?
Definition: strconv.hxx:417
std::size_t into_buf(std::span< char > buf, TYPE const &value, ctx c={})
Write an SQL representation of value into buf.
Definition: strconv.hxx:454
constexpr bool is_sql_array
Does this type translate to an SQL array?
Definition: strconv.hxx:796
std::vector< std::string_view > to_buf_multi(ctx c, std::span< char > buf, TYPE... value)
Convert multiple values to strings inside a single buffer.
Definition: strconv.hxx:680
conversion_context const & ctx
Convenience alias: const reference to a pqxx::conversion_context.
Definition: strconv.hxx:201
constexpr bool always_null() noexcept
Is a value of TYPE always null?
Definition: strconv.hxx:755
constexpr bool is_unquoted_safe
Can we use this type in arrays and composite types without quoting them?
Definition: strconv.hxx:812
format
Format code: is data text or binary?
Definition: types.hxx:121
void into_string(T const &value, std::string &out, ctx c={})
Definition: conversions.hxx:1024
Nullness traits describing a type whose values are always null.
Definition: strconv.hxx:133
static constexpr bool has_null
Does TYPE have a null value?
Definition: strconv.hxx:135
static constexpr bool always_null
Is TYPE always null?
Definition: strconv.hxx:138
static constexpr PQXX_PURE bool is_null(TYPE const &) noexcept
Is the given TYPE value a null?
Definition: strconv.hxx:141
Contextual parameters for string conversions implementations.
Definition: strconv.hxx:163
sl loc
A std::source_location for the call.
Definition: strconv.hxx:183
constexpr conversion_context(encoding_group e, sl l=sl::current())
Definition: strconv.hxx:190
encoding_group enc
Encoding group describing the client text encoding.
Definition: strconv.hxx:172
constexpr conversion_context(sl lc=sl::current())
Definition: strconv.hxx:188
String traits for a forbidden type conversion.
Definition: strconv.hxx:274
static std::string_view to_buf(std::span< char >, TYPE const &, ctx={})=delete
static TYPE from_string(std::string_view, ctx={})=delete
static std::size_t size_buffer(TYPE const &) noexcept=delete
Helper class for defining enum conversions.
Definition: strconv.hxx:575
static constexpr std::size_t size_buffer(ENUM const &value) noexcept
Definition: strconv.hxx:590
static constexpr std::string_view to_buf(std::span< char > buf, ENUM const &value, ctx c={})
Definition: strconv.hxx:579
static ENUM from_string(std::string_view text, ctx c={})
Definition: strconv.hxx:584
std::underlying_type_t< ENUM > impl_type
Definition: strconv.hxx:576
Nullness traits describing a type which does not have a null value.
Definition: strconv.hxx:93
static constexpr PQXX_PURE bool is_null(TYPE const &) noexcept
Does a given value correspond to an SQL null value?
Definition: strconv.hxx:118
static constexpr bool always_null
Are all values of this type null?
Definition: strconv.hxx:111
static constexpr bool has_null
Does TYPE have a "built-in null value"?
Definition: strconv.hxx:105
Traits describing a type's "null value," if any.
Definition: strconv.hxx:70
static constexpr bool always_null
Is this type always null?
Definition: strconv.hxx:76
static PQXX_PURE bool is_null(TYPE const &value)
Is value a null?
static bool has_null
Does this type have a null value?
Definition: strconv.hxx:72
static PQXX_PURE TYPE null()
Return a null value.
Traits class for use in string conversions.
Definition: strconv.hxx:213
static std::size_t size_buffer(TYPE const &value) noexcept
Estimate how much buffer space is needed to represent value as SQL text.
static TYPE from_string(std::string_view text, ctx={})
Parse a string representation of a TYPE value.
Definition: time.cxx:198
static std::string_view to_buf(std::span< char > buf, TYPE const &value, ctx={})
Return a string_view representing value in SQL text.
Definition: time.cxx:180