userver: userver/storages/postgres/detail/non_transaction.hpp Source File
Loading...
Searching...
No Matches
non_transaction.hpp
1#pragma once
2
3#include <string>
4
5#include <userver/storages/postgres/options.hpp>
6#include <userver/storages/postgres/parameter_store.hpp>
7#include <userver/storages/postgres/postgres_fwd.hpp>
8#include <userver/storages/postgres/query.hpp>
9#include <userver/storages/postgres/result_set.hpp>
10
11#include <userver/storages/postgres/detail/connection_ptr.hpp>
12#include <userver/storages/postgres/detail/query_parameters.hpp>
13#include <userver/storages/postgres/detail/time_types.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace storages::postgres::detail {
18
19class NonTransaction {
20 public:
21 explicit NonTransaction(
22 ConnectionPtr&& conn,
23 SteadyClock::time_point start_time = detail::SteadyClock::now());
24
25 NonTransaction(NonTransaction&&) noexcept;
26 NonTransaction& operator=(NonTransaction&&) noexcept;
27
28 NonTransaction(const NonTransaction&) = delete;
29 NonTransaction& operator=(const NonTransaction&) = delete;
30
31 ~NonTransaction();
32
33 /// @name Query execution
34 /// @{
35 /// Execute statement with arbitrary parameters.
36 ///
37 /// Suspends coroutine for execution.
38 template <typename... Args>
39 ResultSet Execute(const Query& query, const Args&... args) {
40 return Execute(OptionalCommandControl{}, query, args...);
41 }
42
43 /// Execute statement with arbitrary parameters and per-statement command
44 /// control.
45 ///
46 /// Suspends coroutine for execution.
47 template <typename... Args>
48 ResultSet Execute(OptionalCommandControl statement_cmd_ctl,
49 const Query& query, const Args&... args) {
50 detail::StaticQueryParameters<sizeof...(args)> params;
51 params.Write(GetConnectionUserTypes(), args...);
52 return DoExecute(query, detail::QueryParameters{params}, statement_cmd_ctl);
53 }
54
55 /// Execute statement with stored arguments.
56 ///
57 /// Suspends coroutine for execution.
58 ResultSet Execute(const std::string& statement, const ParameterStore& store) {
59 return Execute(OptionalCommandControl{}, statement, store);
60 }
61
62 /// Execute statement with stored arguments and per-statement command control.
63 ///
64 /// Suspends coroutine for execution.
65 ResultSet Execute(OptionalCommandControl statement_cmd_ctl,
66 const std::string& statement, const ParameterStore& store);
67 /// @}
68 private:
69 ResultSet DoExecute(const Query& query, const detail::QueryParameters& params,
70 OptionalCommandControl statement_cmd_ctl);
71 const UserTypes& GetConnectionUserTypes() const;
72
73 detail::ConnectionPtr conn_;
74};
75
76} // namespace storages::postgres::detail
77
78USERVER_NAMESPACE_END