11 #ifndef PQXX_PARAMS_HXX
12 #define PQXX_PARAMS_HXX
14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
82 template<
typename First,
typename... Args>
83 params(First &&first, Args &&...args)
97 append_pack(loc, std::forward<Args>(args)...);
103 loc, std::forward<First>(first), std::forward<Args>(args)...);
116 void reserve(std::size_t n) &;
119 [[nodiscard]] constexpr
auto size() const noexcept
121 return m_params.size();
130 [[nodiscard]] constexpr
auto ssize()
const {
return std::ssize(m_params); }
133 void append(
sl = sl::current()) &;
139 void append(
zview,
sl = sl::current()) &;
145 void append(std::string
const &,
sl = sl::current()) &;
148 void append(std::string &&,
sl = sl::current()) &;
160 template<binary DATA>
void append(DATA
const &data,
sl loc = sl::current()) &
166 void append(
bytes &&,
sl = sl::current()) &;
169 void append(
params const &value,
sl = sl::current()) &;
172 void append(
params &&value,
sl = sl::current()) &;
176 template<
typename TYPE>
177 void append([[maybe_unused]] TYPE
const &value,
sl loc = sl::current()) &
180 if constexpr (pqxx::always_null<TYPE>())
182 m_params.emplace_back();
186 m_params.emplace_back();
191 m_params.emplace_back(
197 template<std::ranges::range RANGE>
200 if constexpr (std::ranges::sized_range<RANGE>)
201 reserve(std::size(*
this) + std::size(
range));
202 for (
auto &value :
range) append(value, loc);
219 template<
typename... Args>
void append_pack(
sl loc, Args &&...args)
221 reserve(size() +
sizeof...(args));
222 ((this->append(std::forward<Args>(args), loc)), ...);
228 void append_pack(
sl)
const noexcept {}
234 std::variant<std::nullptr_t, zview, std::string, bytes_view, bytes>;
235 std::vector<entry> m_params;
239 static constexpr std::string_view s_overflow{
240 "Statement parameter length overflow."sv};
260 (std::numeric_limits<COUNTER>::max)()};
264 static constexpr
auto initial{
"$1\0"sv};
265 initial.copy(std::data(m_buf), std::size(initial));
274 return zview{std::data(m_buf), m_len};
283 [[nodiscard]] std::string
get()
const
285 return std::string(std::data(m_buf), m_len);
295 "Too many parameters in one statement: limit is {}.",
max_params),
299 if (m_current % 10 == 0)
304 char *
const data{std::data(m_buf)};
306 auto const written{pqxx::into_buf<COUNTER>(
307 {data + 1, data + std::size(m_buf) - 1}, m_current, c)};
308 std::size_t
const end{1 + written};
309 assert(end < std::size(m_buf));
311 m_len = check_cast<COUNTER>(end,
"placeholders counter", loc);
316 ++m_buf.at(m_len - 1);
321 [[nodiscard]] COUNTER
count() const noexcept {
return m_current; }
325 COUNTER m_current = 1;
338 std::array<char, std::numeric_limits<COUNTER>::digits10 + 3> m_buf;
Connection to a database.
Definition: connection.hxx:273
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:65
constexpr auto size() const noexcept
Get the number of parameters currently in this params.
Definition: params.hxx:119
params(First &&first, Args &&...args)
Pre-populate a params with args. Feel free to add more later.
Definition: params.hxx:83
constexpr auto ssize() const
Get the number of parameters (signed).
Definition: params.hxx:130
void append([[maybe_unused]] TYPE const &value, sl loc=sl::current()) &
Definition: params.hxx:177
void append(DATA const &data, sl loc=sl::current()) &
Append a non-null binary parameter.
Definition: params.hxx:160
void append_multi(RANGE const &range, sl loc=sl::current()) &
Append all elements of range as parameters.
Definition: params.hxx:198
Generate parameter placeholders for use in an SQL statement.
Definition: params.hxx:256
static constexpr unsigned int max_params
Maximum number of parameters we support.
Definition: params.hxx:259
COUNTER count() const noexcept
Return the current placeholder number. The initial placeholder is 1.
Definition: params.hxx:321
std::string get() const
Read the current placeholder text, as a std::string.
Definition: params.hxx:283
void next(sl loc=sl::current()) &
Move on to the next parameter.
Definition: params.hxx:289
placeholders()
Definition: params.hxx:262
constexpr zview view() const &noexcept
Read an ephemeral version of the current placeholder text.
Definition: params.hxx:272
A C++ equivalent to PostgreSQL's range types.
Definition: range.hxx:268
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:651
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:151
Private namespace for libpqxx's internal use; do not access.
Definition: connection.cxx:333
constexpr PQXX_PURE pqxx::encoding_group get_encoding_group(encoding_group const &enc, sl=sl::current()) noexcept
Identity function for encoding_group, for regularity.
Definition: params.hxx:29
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::span< std::byte const > bytes_view
Type alias for a view of bytes.
Definition: types.hxx:188
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
PQXX_LIBEXPORT std::string to_string(field_ref const &value, ctx)
Convert a field_ref to a string.
Definition: field.hxx:891
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:260
encoding_group
Definition: encoding_group.hxx:40
@ unknown
Default: indeterminate encoding. All we know is it supports ASCII.
constexpr bool is_null(TYPE const &value) noexcept
Is value a null?
Definition: strconv.hxx:764
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
conversion_context const & ctx
Convenience alias: const reference to a pqxx::conversion_context.
Definition: strconv.hxx:201
std::vector< std::byte > bytes
Type alias for a container containing bytes.
Definition: util.hxx:240
format
Format code: is data text or binary?
Definition: types.hxx:121
Contextual parameters for string conversions implementations.
Definition: strconv.hxx:163
Internal type: encode statement parameters.
Definition: statement_parameters.hxx:44