Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
PostgresCoro.hpp
Go to the documentation of this file.
1#pragma once
4#include <coroutine>
5#include <memory>
6#include <string>
7#include <optional>
8#include <vector>
9
10namespace orbit::database {
11
16 std::shared_ptr<PostgresClient> client;
17 bool success = false;
18
19 bool await_ready() const noexcept { return false; }
20 void await_suspend(std::coroutine_handle<> h) {
21 client->connect([this, h](bool s) {
22 success = s;
23 h.resume();
24 });
25 }
26 bool await_resume() { return success; }
27};
28
35inline ConnectAwaiter connect_async(std::shared_ptr<PostgresClient> client) {
36 return ConnectAwaiter{std::move(client)};
37}
38
43 std::shared_ptr<PostgresClient> client;
44 std::string sql;
46 std::vector<std::optional<std::string>> params;
47 bool parameterized = false;
48
49 bool await_ready() const noexcept { return false; }
50 void await_suspend(std::coroutine_handle<> h) {
51 auto done = [this, h](const ResultSet& r) {
52 result = r;
53 h.resume();
54 };
55 if (parameterized) {
56 client->query(sql, params, done);
57 } else {
58 client->query(sql, done);
59 }
60 }
62};
63
75inline QueryAwaiter query_async(std::shared_ptr<PostgresClient> client, const std::string& sql) {
76 return QueryAwaiter{std::move(client), sql, ResultSet{}, {}, false};
77}
78
86inline QueryAwaiter query_async(std::shared_ptr<PostgresClient> client, const std::string& sql,
87 std::vector<std::optional<std::string>> params) {
88 return QueryAwaiter{std::move(client), sql, ResultSet{}, std::move(params), true};
89}
90
95 std::function<void(std::function<void(const ResultSet&)>)> start;
97
98 bool await_ready() const noexcept { return false; }
99 void await_suspend(std::coroutine_handle<> h) {
100 start([this, h](const ResultSet& r) {
101 result = r;
102 h.resume();
103 });
104 }
105 ResultSet await_resume() { return std::move(result); }
106};
107
109inline ResultAwaiter execute_async(std::shared_ptr<PostgresClient> client, std::string sql,
110 std::vector<std::optional<std::string>> params = {}) {
111 return ResultAwaiter{[client = std::move(client), sql = std::move(sql), params = std::move(params)](auto done) {
112 client->execute(sql, params, std::move(done));
113 }, ResultSet{}};
114}
115
127inline ResultAwaiter begin_async(std::shared_ptr<PostgresClient> client) {
128 return ResultAwaiter{[client = std::move(client)](auto done) { client->begin(std::move(done)); }, ResultSet{}};
129}
130
131inline ResultAwaiter commit_async(std::shared_ptr<PostgresClient> client) {
132 return ResultAwaiter{[client = std::move(client)](auto done) { client->commit(std::move(done)); }, ResultSet{}};
133}
134
135inline ResultAwaiter rollback_async(std::shared_ptr<PostgresClient> client) {
136 return ResultAwaiter{[client = std::move(client)](auto done) { client->rollback(std::move(done)); }, ResultSet{}};
137}
138
139} // namespace database
Represents a generic, unified result set from a database query.
Definition ResultSet.hpp:197
Definition ConnectionPool.hpp:14
ConnectAwaiter connect_async(std::shared_ptr< PostgresClient > client)
Creates an awaiter for connecting a PostgresClient.
Definition PostgresCoro.hpp:35
ResultAwaiter execute_async(std::shared_ptr< PostgresClient > client, std::string sql, std::vector< std::optional< std::string > > params={})
Runs a statement through the prepared-statement cache (PostgresClient::execute).
Definition PostgresCoro.hpp:109
ResultAwaiter begin_async(std::shared_ptr< PostgresClient > client)
Transaction control for coroutines.
Definition PostgresCoro.hpp:127
ResultAwaiter commit_async(std::shared_ptr< PostgresClient > client)
Definition PostgresCoro.hpp:131
QueryAwaiter query_async(std::shared_ptr< PostgresClient > client, const std::string &sql)
Creates an awaiter for executing a query on a PostgresClient.
Definition PostgresCoro.hpp:75
ResultAwaiter rollback_async(std::shared_ptr< PostgresClient > client)
Definition PostgresCoro.hpp:135
A coroutine awaiter for connecting a PostgresClient asynchronously.
Definition PostgresCoro.hpp:15
bool await_resume()
Definition PostgresCoro.hpp:26
std::shared_ptr< PostgresClient > client
Definition PostgresCoro.hpp:16
void await_suspend(std::coroutine_handle<> h)
Definition PostgresCoro.hpp:20
bool await_ready() const noexcept
Definition PostgresCoro.hpp:19
bool success
Definition PostgresCoro.hpp:17
A coroutine awaiter for executing a query on a PostgresClient asynchronously.
Definition PostgresCoro.hpp:42
bool await_ready() const noexcept
Definition PostgresCoro.hpp:49
bool parameterized
Definition PostgresCoro.hpp:47
std::string sql
Definition PostgresCoro.hpp:44
std::shared_ptr< PostgresClient > client
Definition PostgresCoro.hpp:43
void await_suspend(std::coroutine_handle<> h)
Definition PostgresCoro.hpp:50
ResultSet await_resume()
Definition PostgresCoro.hpp:61
std::vector< std::optional< std::string > > params
Definition PostgresCoro.hpp:46
ResultSet result
Definition PostgresCoro.hpp:45
Awaits any PostgresClient operation that reports a ResultSet.
Definition PostgresCoro.hpp:94
void await_suspend(std::coroutine_handle<> h)
Definition PostgresCoro.hpp:99
ResultSet result
Definition PostgresCoro.hpp:96
bool await_ready() const noexcept
Definition PostgresCoro.hpp:98
ResultSet await_resume()
Definition PostgresCoro.hpp:105
std::function< void(std::function< void(const ResultSet &)>)> start
Definition PostgresCoro.hpp:95