Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
PostgresCoro.hpp
Go to the documentation of this file.
1#pragma once
3#include <coroutine>
4#include <memory>
5#include <string>
6
7namespace database {
8
13 std::shared_ptr<PostgresClient> client;
14 bool success = false;
15
16 bool await_ready() const noexcept { return false; }
17 void await_suspend(std::coroutine_handle<> h) {
18 client->connect([this, h](bool s) {
19 success = s;
20 h.resume();
21 });
22 }
23 bool await_resume() { return success; }
24};
25
32inline ConnectAwaiter connect_async(std::shared_ptr<PostgresClient> client) {
33 return ConnectAwaiter{std::move(client)};
34}
35
40 std::shared_ptr<PostgresClient> client;
41 std::string sql;
43
44 bool await_ready() const noexcept { return false; }
45 void await_suspend(std::coroutine_handle<> h) {
46 client->query(sql, [this, h](const ResultSet& r) {
47 result = r;
48 h.resume();
49 });
50 }
52};
53
65inline QueryAwaiter query_async(std::shared_ptr<PostgresClient> client, const std::string& sql) {
66 return QueryAwaiter{std::move(client), sql, ResultSet{}};
67}
68
69} // namespace database
Represents a generic, unified result set from a database query.
Definition ResultSet.hpp:111
Definition ConnectionPool.hpp:10
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:65
ConnectAwaiter connect_async(std::shared_ptr< PostgresClient > client)
Creates an awaiter for connecting a PostgresClient.
Definition PostgresCoro.hpp:32
A coroutine awaiter for connecting a PostgresClient asynchronously.
Definition PostgresCoro.hpp:12
bool await_resume()
Definition PostgresCoro.hpp:23
bool await_ready() const noexcept
Definition PostgresCoro.hpp:16
bool success
Definition PostgresCoro.hpp:14
void await_suspend(std::coroutine_handle<> h)
Definition PostgresCoro.hpp:17
std::shared_ptr< PostgresClient > client
Definition PostgresCoro.hpp:13
A coroutine awaiter for executing a query on a PostgresClient asynchronously.
Definition PostgresCoro.hpp:39
std::shared_ptr< PostgresClient > client
Definition PostgresCoro.hpp:40
bool await_ready() const noexcept
Definition PostgresCoro.hpp:44
std::string sql
Definition PostgresCoro.hpp:41
ResultSet await_resume()
Definition PostgresCoro.hpp:51
ResultSet result
Definition PostgresCoro.hpp:42
void await_suspend(std::coroutine_handle<> h)
Definition PostgresCoro.hpp:45