userver: /data/code/userver/odbc/include/userver/storages/odbc/parameter_store.hpp Source File
Loading...
Searching...
No Matches
parameter_store.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/odbc/parameter_store.hpp
4/// @brief @copybrief storages::odbc::ParameterStore
5
6#include <algorithm>
7#include <concepts>
8#include <cstddef>
9#include <iterator>
10#include <optional>
11#include <stdexcept>
12#include <type_traits>
13
14#include <userver/storages/odbc/impl/parameter.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace storages::odbc {
19
20class Cluster;
21class Transaction;
22class BulkParameterStore;
23
24/// @ingroup userver_containers
25///
26/// @brief Owning, ordered list of dynamically assembled ODBC parameters.
27///
28/// Values are copied into the store and remain valid independently of the
29/// source objects. Use an empty `std::optional<T>` for SQL NULL: `T` determines
30/// the parameter type used for ODBC binding. Raw `nullptr` and `std::nullopt`
31/// remain untyped, just like in the variadic API, and should only be used when
32/// the driver can infer the type from the statement. A null `const char*` is a
33/// typed string NULL.
34///
35/// @warning Parameters are always values for existing `?` placeholders. Never
36/// interpolate them into the SQL query text.
37class ParameterStore final {
38public:
39 ParameterStore() = default;
40 ParameterStore(const ParameterStore&) = delete;
41 ParameterStore(ParameterStore&&) noexcept = default;
42 ParameterStore& operator=(const ParameterStore&) = delete;
43 ParameterStore& operator=(ParameterStore&&) noexcept = default;
44
45 /// @brief Copies a scalar parameter or the declaration-order fields of a
46 /// supported aggregate to the end of the ordered list.
47 /// @returns `*this` for chained construction.
48 template <typename T>
49 requires impl::kIsParameterArgument<T>
50 ParameterStore& PushBack(const T& parameter) {
51 auto appended = impl::MakeParameterList(parameter);
52 static_assert(std::is_nothrow_move_constructible_v<impl::Parameter>);
53 if (appended.size() > parameters_.max_size() - parameters_.size()) {
54 throw std::length_error("ODBC ParameterStore size exceeds its maximum");
55 }
56 parameters_.reserve(parameters_.size() + appended.size());
57 std::move(appended.begin(), appended.end(), std::back_inserter(parameters_));
58 return *this;
59 }
60
61 /// Returns whether the parameter list is empty.
62 bool IsEmpty() const noexcept { return parameters_.empty(); }
63
64 /// Returns the number of stored parameters.
65 std::size_t Size() const noexcept { return parameters_.size(); }
66
67private:
68 friend class Cluster;
69 friend class Transaction;
70 friend class BulkParameterStore;
71
72 const impl::ParameterList& GetParameters() const noexcept { return parameters_; }
73
74 impl::ParameterList parameters_;
75};
76
77} // namespace storages::odbc
78
79USERVER_NAMESPACE_END