add code for the C++Now “postgres” presentation#61
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds/updates PostgreSQL-related example programs intended to support a C++Now “postgres” talk, including demo code for async libpq usage integrated with beman::execution / beman::net.
Changes:
- Reworks
examples/postgres.cppinto a more elaborate demo (connection limiting, polling/flush loops, multiple concurrent requests). - Updates
examples/postgres-talk.cppwith additional talk/demo code (mutex/serialization wrapper, multiple queries). - Adjusts example build configuration and fixes a file header comment.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| examples/postgres.cpp | New async libpq query flow + connection limiting + demo “timer / two requests” orchestration. |
| examples/postgres-talk.cpp | Talk-focused variant of the postgres demo with a mutex-like wrapper and two query flows. |
| examples/populate-postgres.cpp | Corrects the file header comment to match the filename. |
| examples/CMakeLists.txt | Changes which examples are built/iterated, including postgres-related targets. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
4
to
+8
| #include <beman/net/net.hpp> | ||
| #include <beman/net/detail/repeat_effect_until.hpp> | ||
| #include <beman/execution/execution.hpp> | ||
| #include <libpq-fe.h> | ||
| #include "demo_algorithm.hpp" |
Comment on lines
+51
to
+54
| std::unique_ptr<PGconn, decltype([](auto c) { PQfinish(c); })> conn; | ||
| net::ip::tcp::socket socket; | ||
| connection(net::io_context& io, PGconn* c) | ||
| : conn(c ? c : throw std::runtime_error("connection failed")), |
| res.push_back(pg::result(PQgetResult(conn))); | ||
| }), | ||
| [&res]() noexcept { return !res.empty() && !res.back(); }) | | ||
| ex::then([&res] noexcept { return std::move(res); }); |
| [&conn] noexcept { return !PQisBusy(conn); }); | ||
| } | ||
| auto exec1(pg::connection& conn, const char* query) { | ||
| return ex::just() | ex::then([&conn, query] noexcept { PQsendQuery(conn, query); }) | |
Comment on lines
+169
to
+172
| return net::repeat_effect_until(net::async_poll(conn.socket, net::event_type::in) | | ||
| ex::upon_error([](auto&&) noexcept {}) | | ||
| ex::then([&conn](auto&&...) noexcept { PQconsumeInput(conn); }), | ||
| [&conn] noexcept { return !PQisBusy(conn); }); |
Comment on lines
+53
to
+57
| struct conn { | ||
| std::unique_ptr<PGconn, decltype([](auto c) { PQfinish(c); })> cn; | ||
| net::ip::tcp::socket socket; | ||
| connection(net::io_context& io, PGconn* c) | ||
| : conn(c ? c : throw std::runtime_error("connection failed")), | ||
| socket(io, io.make_socket(PQsocket(conn.get()))) {} | ||
| operator PGconn*() const { return conn.get(); } | ||
| conn(net::io_context& io, PGconn* c) : cn(c), socket(io, io.make_socket(PQsocket(c))) {} | ||
| operator PGconn*() const { return cn.get(); } |
Comment on lines
10
to
+14
| #include <iostream> | ||
| #include <optional> | ||
| #include <ranges> | ||
| #include <string> | ||
| #include <thread> |
Comment on lines
40
to
43
| set(xEXAMPLES taps) | ||
| set(xEXAMPLES memory-stream) | ||
| set(xEXAMPLES postgres-talk) | ||
| set(EXAMPLES postgres-talk) | ||
|
|
| }); | ||
| } | ||
| ex::task<pg::results, pg::env> exec2(pg::connection& conn, const char* query) { | ||
| PQsendQuery(conn, query); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.