userver: userver/storages/postgres/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/postgres/parameter_store.hpp
4/// @brief @copybrief storages::postgres::ParameterStore
5
6#include <userver/storages/postgres/detail/query_parameters.hpp>
7#include <userver/storages/postgres/io/type_traits.hpp>
8#include <userver/storages/postgres/io/user_types.hpp>
9#include <userver/utils/strong_typedef.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace storages::postgres {
14
15/// @ingroup userver_containers
16///
17/// @brief Class for dynamic PostgreSQL parameter list construction.
18///
19/// Typical use case for this container is to keep parameters around while the
20/// query is being constructed on the fly:
21/// @snippet storages/postgres/tests/interval_pgtest.cpp Parameters store sample
22///
23/// Note that storages::postgres::Cluster::Execute with explicitly provided
24/// arguments works slightly faster:
25/// @snippet storages/postgres/tests/landing_test.cpp Exec sample
27public:
28 ParameterStore() = default;
29 ParameterStore(const ParameterStore&) = delete;
30 ParameterStore(ParameterStore&&) = default;
31 ParameterStore& operator=(const ParameterStore&) = delete;
32 ParameterStore& operator=(ParameterStore&&) = default;
33
34 /// @brief Adds a parameter to the end of the parameter list.
35 /// @note Currently only built-in/system types are supported.
36 template <typename T>
37 ParameterStore& PushBack(const T& param) {
38 static_assert(
39 io::IsTypeMappedToSystem<T>() || io::IsTypeMappedToSystemArray<T>(),
40 "Currently only built-in types can be used in ParameterStore"
41 );
42 data_.Write(kNoUserTypes, param);
43 return *this;
44 }
45
46 /// Returns whether the parameter list is empty.
47 bool IsEmpty() const { return data_.Size() == 0; }
48
49 /// Returns current size of the list.
50 size_t Size() const { return data_.Size(); }
51
52 /// @cond
53 const detail::DynamicQueryParameters& GetInternalData() const { return data_; }
54 /// @endcond
55
56private:
57 static UserTypes kNoUserTypes;
58
59 detail::DynamicQueryParameters data_;
60};
61
62} // namespace storages::postgres
63
64USERVER_NAMESPACE_END