Loading...
Searching...
No Matches
binarystring.hxx
/* Deprecated representation for raw, binary data.
*
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/binarystring instead.
*
* Copyright (c) 2000-2024, Jeroen T. Vermeulen.
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this
* mistake, or contact the author.
*/
#ifndef PQXX_H_BINARYSTRING
#define PQXX_H_BINARYSTRING
#if !defined(PQXX_HEADER_PRE)
# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
#endif
#include <memory>
#include <string>
#include <string_view>
#include "pqxx/result.hxx"
#include "pqxx/strconv.hxx"
namespace pqxx
{
class binarystring;
template<> struct string_traits<binarystring>;
class PQXX_LIBEXPORT binarystring
{
public:
using char_type = unsigned char;
using value_type = char_type;
using size_type = std::size_t;
using difference_type = long;
using const_reference = value_type const &;
using const_pointer = value_type const *;
using const_iterator = const_pointer;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
[[deprecated("Use std::byte for binary data.")]] binarystring(
binarystring const &) = default;
[[deprecated("Use std::byte for binary data.")]] explicit binarystring(
field const &);
[[deprecated("Use std::byte for binary data.")]] explicit binarystring(
std::string_view);
[[deprecated("Use std::byte for binary data.")]] binarystring(
void const *, std::size_t);
[[deprecated("Use std::byte for binary data.")]] binarystring(
std::shared_ptr<value_type> ptr, size_type size) :
m_buf{std::move(ptr)}, m_size{size}
{}
[[nodiscard]] size_type size() const noexcept { return m_size; }
[[nodiscard]] size_type length() const noexcept { return size(); }
[[nodiscard]] bool empty() const noexcept { return size() == 0; }
[[nodiscard]] const_iterator begin() const noexcept { return data(); }
[[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
[[nodiscard]] const_iterator end() const noexcept { return data() + m_size; }
[[nodiscard]] const_iterator cend() const noexcept { return end(); }
[[nodiscard]] const_reference front() const noexcept { return *begin(); }
[[nodiscard]] const_reference back() const noexcept
{
return *(data() + m_size - 1);
}
[[nodiscard]] const_reverse_iterator rbegin() const
{
return const_reverse_iterator{end()};
}
[[nodiscard]] const_reverse_iterator crbegin() const { return rbegin(); }
[[nodiscard]] const_reverse_iterator rend() const
{
return const_reverse_iterator{begin()};
}
[[nodiscard]] const_reverse_iterator crend() const { return rend(); }
[[nodiscard]] value_type const *data() const noexcept { return m_buf.get(); }
[[nodiscard]] const_reference operator[](size_type i) const noexcept
{
return data()[i];
}
[[nodiscard]] PQXX_PURE bool operator==(binarystring const &) const noexcept;
[[nodiscard]] bool operator!=(binarystring const &rhs) const noexcept
{
return not operator==(rhs);
}
binarystring &operator=(binarystring const &);
const_reference at(size_type) const;
void swap(binarystring &);
[[nodiscard]] char const *get() const noexcept
{
return reinterpret_cast<char const *>(m_buf.get());
}
[[nodiscard]] std::string_view view() const noexcept
{
return std::string_view(get(), size());
}
[[nodiscard]] std::string str() const;
[[nodiscard]] std::byte const *bytes() const
{
return reinterpret_cast<std::byte const *>(get());
}
[[nodiscard]] pqxx::bytes_view bytes_view() const
{
return pqxx::bytes_view{bytes(), size()};
}
private:
std::shared_ptr<value_type> m_buf;
size_type m_size{0};
};
template<> struct nullness<binarystring> : no_null<binarystring>
{};
template<> struct string_traits<binarystring>
{
static std::size_t size_buffer(binarystring const &value) noexcept
{
return internal::size_esc_bin(std::size(value));
}
static zview to_buf(char *begin, char *end, binarystring const &value)
{
return generic_to_buf(begin, end, value);
}
static char *into_buf(char *begin, char *end, binarystring const &value)
{
auto const budget{size_buffer(value)};
if (internal::cmp_less(end - begin, budget))
throw conversion_overrun{
"Not enough buffer space to escape binary data."};
std::string_view text{value.view()};
internal::esc_bin(binary_cast(text), begin);
return begin + budget;
}
static binarystring from_string(std::string_view text)
{
auto const size{pqxx::internal::size_unesc_bin(std::size(text))};
std::shared_ptr<unsigned char> buf{
new unsigned char[size], [](unsigned char const *x) { delete[] x; }};
pqxx::internal::unesc_bin(text, reinterpret_cast<std::byte *>(buf.get()));
#include "pqxx/internal/ignore-deprecated-pre.hxx"
return binarystring{std::move(buf), size};
#include "pqxx/internal/ignore-deprecated-post.hxx"
}
};
} // namespace pqxx
#endif
pqxx::field
Reference to a field in a result set.
Definition field.hxx:35
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
pqxx::binarystring::get
char const * get() const noexcept
Raw character buffer (no terminating zero is added).
Definition binarystring.hxx:151
pqxx::binarystring::binarystring
binarystring(std::shared_ptr< value_type > ptr, size_type size)
Efficiently wrap a buffer of binary data in a binarystring.
Definition binarystring.hxx:92
pqxx::binarystring::view
std::string_view view() const noexcept
Read contents as a std::string_view.
Definition binarystring.hxx:157
pqxx::binarystring::bytes_view
pqxx::bytes_view bytes_view() const
Read data as a bytes_view.
Definition binarystring.hxx:177
pqxx::binarystring::bytes
std::byte const * bytes() const
Access data as a pointer to std::byte.
Definition binarystring.hxx:171
pqxx::binarystring::data
value_type const * data() const noexcept
Unescaped field contents.
Definition binarystring.hxx:126
pqxx::binarystring::length
size_type length() const noexcept
Size of converted string in bytes.
Definition binarystring.hxx:100
pqxx::binarystring::size
size_type size() const noexcept
Size of converted string in bytes.
Definition binarystring.hxx:98
pqxx::binarystring
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition binarystring.hxx:58
pqxx::conversion_overrun
Could not convert value to string: not enough buffer space.
Definition except.hxx:313
pqxx::internal::unesc_bin
void PQXX_LIBEXPORT unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition util.cxx:166
pqxx::internal::size_esc_bin
constexpr std::size_t size_esc_bin(std::size_t binary_bytes) noexcept
Compute buffer size needed to escape binary data for use as a BYTEA.
Definition util.hxx:516
pqxx::internal::cmp_less
constexpr bool cmp_less(LEFT lhs, RIGHT rhs) noexcept
Same as std::cmp_less, or a workaround where that's not available.
Definition util.hxx:65
pqxx::internal::esc_bin
void PQXX_LIBEXPORT esc_bin(bytes_view binary_data, char buffer[]) noexcept
Hex-escape binary data into a buffer.
Definition util.cxx:134
pqxx::internal::size_unesc_bin
constexpr std::size_t size_unesc_bin(std::size_t escaped_bytes) noexcept
Compute binary size from the size of its escaped version.
Definition util.hxx:525
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
pqxx::binary_cast
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition util.hxx:409
pqxx::bytes
std::conditional< has_generic_bytes_char_traits, std::basic_string< std::byte >, std::basic_string< std::byte, byte_char_traits > >::type bytes
Type alias for a container containing bytes.
Definition util.hxx:375
pqxx::bytes_view
std::conditional< has_generic_bytes_char_traits, std::basic_string_view< std::byte >, std::basic_string_view< std::byte, byte_char_traits > >::type bytes_view
Type alias for a view of bytes.
Definition util.hxx:385
pqxx::no_null
Nullness traits describing a type which does not have a null value.
Definition strconv.hxx:113
pqxx::nullness
Traits describing a type's "null value," if any.
Definition strconv.hxx:91
pqxx::string_traits
Traits class for use in string conversions.
Definition strconv.hxx:154
pqxx::string_traits::from_string
static TYPE from_string(std::string_view text)
Parse a string representation of a TYPE value.
pqxx::string_traits::size_buffer
static std::size_t size_buffer(TYPE const &value) noexcept
Estimate how much buffer space is needed to represent value.
pqxx::string_traits::to_buf
static zview to_buf(char *begin, char *end, TYPE const &value)
Return a string_view representing value, plus terminating zero.
pqxx::string_traits::into_buf
static char * into_buf(char *begin, char *end, TYPE const &value)
Write value's string representation into buffer at begin.