libpqxx  7.6.1
params.hxx
1 /* Helpers for prepared statements and parameterised statements.
2  *
3  * See the connection class for more about such statements.
4  *
5  * Copyright (c) 2000-2022, Jeroen T. Vermeulen.
6  *
7  * See COPYING for copyright license. If you did not receive a file called
8  * COPYING with this source code, please notify the distributor of this
9  * mistake, or contact the author.
10  */
11 #ifndef PQXX_H_PARAMS
12 #define PQXX_H_PARAMS
13 
14 #include "pqxx/compiler-public.hxx"
15 #include "pqxx/internal/compiler-internal-pre.hxx"
16 
17 #include "pqxx/internal/concat.hxx"
18 #include "pqxx/internal/statement_parameters.hxx"
19 #include "pqxx/types.hxx"
20 
21 
23 namespace pqxx::prepare
24 {
26 
43 template<typename IT>
44 [[deprecated("Use the params class instead.")]] constexpr inline auto
45 make_dynamic_params(IT begin, IT end)
46 {
47  return pqxx::internal::dynamic_params(begin, end);
48 }
49 
50 
52 
68 template<typename C>
69 [[deprecated("Use the params class instead.")]] constexpr inline auto
70 make_dynamic_params(C const &container)
71 {
72  using IT = typename C::const_iterator;
73 #include "pqxx/internal/ignore-deprecated-pre.hxx"
74  return pqxx::internal::dynamic_params<IT>{container};
75 #include "pqxx/internal/ignore-deprecated-post.hxx"
76 }
77 
78 
80 
97 template<typename C, typename ACCESSOR>
98 [[deprecated("Use the params class instead.")]] constexpr inline auto
99 make_dynamic_params(C &container, ACCESSOR accessor)
100 {
101  using IT = decltype(std::begin(container));
102 #include "pqxx/internal/ignore-deprecated-pre.hxx"
103  return pqxx::internal::dynamic_params<IT, ACCESSOR>{container, accessor};
104 #include "pqxx/internal/ignore-deprecated-post.hxx"
105 }
106 } // namespace pqxx::prepare
107 
108 
109 namespace pqxx
110 {
112 
122 template<typename COUNTER = unsigned int> class placeholders
123 {
124 public:
126  static inline constexpr unsigned int max_params{
127  std::numeric_limits<COUNTER>::max()};
128 
130  {
131  static constexpr auto initial{"$1\0"sv};
132  initial.copy(std::data(m_buf), std::size(initial));
133  }
134 
136 
139  zview view() const noexcept { return zview{std::data(m_buf), m_len}; }
140 
142 
147  std::string get() const { return std::string(std::data(m_buf), m_len); }
148 
150  void next()
151  {
152  if (m_current >= max_params)
153  throw range_error{pqxx::internal::concat(
154  "Too many parameters in one statement: limit is ", max_params, ".")};
155  ++m_current;
156  if (m_current % 10 == 0)
157  {
158  // Carry the 1. Don't get too clever for this relatively rare
159  // case, just rewrite the entire number. Leave the $ in place
160  // though.
161  char *const data{std::data(m_buf)};
162  char *const end{string_traits<COUNTER>::into_buf(
163  data + 1, data + std::size(m_buf), m_current)};
164  // (Subtract because we don't include the trailing zero.)
165  m_len = check_cast<COUNTER>(end - data, "placeholders counter") - 1;
166  }
167  else
168  {
169  PQXX_LIKELY
170  // Shortcut for the common case: just increment that last digit.
171  ++m_buf[m_len - 1];
172  }
173  }
174 
176  COUNTER count() const noexcept { return m_current; }
177 
178 private:
180  COUNTER m_current = 1;
181 
183  COUNTER m_len = 2;
184 
186 
193  std::array<char, std::numeric_limits<COUNTER>::digits10 + 3> m_buf;
194 };
195 
196 
198 
213 class PQXX_LIBEXPORT params
214 {
215 public:
216  params() = default;
217 
220  template<typename... Args> constexpr params(Args &&...args)
221  {
222  reserve(sizeof...(args));
223  append_pack(std::forward<Args>(args)...);
224  }
225 
227 
233  void reserve(std::size_t n);
234 
236  auto size() const noexcept { return m_params.size(); }
237 
238  // TODO: In C++20, use the vector's ssize() directly and go noexcept.
240 
245  auto ssize() const { return pqxx::internal::ssize(m_params); }
246 
248  void append();
249 
251 
254  void append(zview);
255 
257 
260  void append(std::string const &);
261 
263  void append(std::string &&);
264 
266 
269  void append(std::basic_string_view<std::byte>);
270 
272 
276  void append(std::basic_string<std::byte> const &);
277 
278 #if defined(PQXX_HAVE_CONCEPTS)
280 
283  template<binary DATA> void append(DATA const &data)
284  {
285  append(
286  std::basic_string_view<std::byte>{std::data(data), std::size(data)});
287  }
288 #endif // PQXX_HAVE_CONCEPTS
289 
291  void append(std::basic_string<std::byte> &&);
292 
294 
297  void append(binarystring const &value);
298 
300  template<typename IT, typename ACCESSOR>
301  void append(pqxx::internal::dynamic_params<IT, ACCESSOR> const &value)
302  {
303  for (auto &param : value) append(value.access(param));
304  }
305 
306  void append(params const &value);
307 
308  void append(params &&value);
309 
312  template<typename TYPE> void append(TYPE const &value)
313  {
314  // TODO: Pool storage for multiple string conversions in one buffer?
315  if constexpr (nullness<strip_t<TYPE>>::always_null)
316  {
317  ignore_unused(value);
318  m_params.emplace_back();
319  }
320  else if (is_null(value))
321  {
322  m_params.emplace_back();
323  }
324  else
325  {
326  m_params.emplace_back(entry{to_string(value)});
327  }
328  }
329 
331  template<PQXX_RANGE_ARG RANGE> void append_multi(RANGE const &range)
332  {
333 #if defined(PQXX_HAVE_CONCEPTS)
334  if constexpr (std::ranges::sized_range<RANGE>)
335  reserve(std::size(*this) + std::size(range));
336 #endif
337  for (auto &value : range) append(value);
338  }
339 
341 
350  pqxx::internal::c_params make_c_params() const;
351 
352 private:
354  template<typename Arg, typename... More>
355  void append_pack(Arg &&arg, More &&...args)
356  {
357  this->append(std::forward<Arg>(arg));
358  // Recurse for remaining args.
359  append_pack(std::forward<More>(args)...);
360  }
361 
363  void append_pack() {}
364 
365  // The way we store a parameter depends on whether it's binary or text
366  // (most types are text), and whether we're responsible for storing the
367  // contents.
368  using entry = std::variant<
369  std::nullptr_t, zview, std::string, std::basic_string_view<std::byte>,
370  std::basic_string<std::byte>>;
371  std::vector<entry> m_params;
372 
373  static constexpr std::string_view s_overflow{
374  "Statement parameter length overflow."sv};
375 };
376 } // namespace pqxx
377 
378 #include "pqxx/internal/compiler-internal-post.hxx"
379 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
void ignore_unused(T &&...)
Suppress compiler warning about an unused item.
Definition: util.hxx:71
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:87
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:507
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:357
auto ssize(T const &c)
Transitional: std::ssize(), or custom implementation if not available.
Definition: util.hxx:368
Definition: params.hxx:24
constexpr auto make_dynamic_params(IT begin, IT end)
Pass a number of statement parameters only known at runtime.
Definition: params.hxx:45
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:193
Generate parameter placeholders for use in an SQL statement.
Definition: params.hxx:123
static constexpr unsigned int max_params
Maximum number of parameters we support.
Definition: params.hxx:126
zview view() const noexcept
Read an ephemeral version of the current placeholder text.
Definition: params.hxx:139
void next()
Move on to the next parameter.
Definition: params.hxx:150
COUNTER count() const noexcept
Return the current placeholder number. The initial placeholder is 1.
Definition: params.hxx:176
std::string get() const
Read the current placeholder text, as a std::string.
Definition: params.hxx:147
placeholders()
Definition: params.hxx:129
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:214
auto size() const noexcept
Get the number of parameters currently in this params.
Definition: params.hxx:236
void append(TYPE const &value)
Definition: params.hxx:312
void append_multi(RANGE const &range)
Append all elements of range as parameters.
Definition: params.hxx:331
params()=default
auto ssize() const
Get the number of parameters (signed).
Definition: params.hxx:245
constexpr params(Args &&...args)
Definition: params.hxx:220
void append(pqxx::internal::dynamic_params< IT, ACCESSOR > const &value)
Append all parameters from value.
Definition: params.hxx:301
Traits describing a type's "null value," if any.
Definition: strconv.hxx:91
static char * into_buf(char *begin, char *end, TYPE const &value)
Write value's string representation into buffer at begin.
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:40