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 enums and 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 (std::is_enum_v<T> && io::traits::kIsMappedToPg<T> &&
41 std::is_same_v<typename io::CppToPg<T>::Mapping, io::CppToUserPg<typename io::CppToPg<T>::Type>>),
42 "Currently only built-in and enum types can be used in ParameterStore"
43 );
44 data_.Write(kNoUserTypes, param);
45 return *this;
46 }
47
48 /// Returns whether the parameter list is empty.
49 bool IsEmpty() const { return data_.Size() == 0; }
50
51 /// Returns current size of the list.
52 size_t Size() const { return data_.Size(); }
53
54 /// @cond
55 const detail::DynamicQueryParameters& GetInternalData() const { return data_; }
56 /// @endcond
57
58private:
59 static UserTypes kNoUserTypes;
60
61 detail::DynamicQueryParameters data_;
62};
63
64} // namespace storages::postgres
65
66USERVER_NAMESPACE_END