userver: userver/storages/postgres/parameter_store.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
27 public:
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 data_.Write(kNoUserTypes, param);
42 return *this;
43 }
44
45 /// Returns whether the parameter list is empty.
46 bool IsEmpty() const { return data_.Size() == 0; }
47
48 /// Returns current size of the list.
49 size_t Size() const { return data_.Size(); }
50
51 /// @cond
52 const detail::DynamicQueryParameters& GetInternalData() const {
53 return data_;
54 }
55 /// @endcond
56
57 private:
58 static UserTypes kNoUserTypes;
59
60 detail::DynamicQueryParameters data_;
61};
62
63} // namespace storages::postgres
64
65USERVER_NAMESPACE_END