From f5db34642024c5a619c333897ab2c792a9ab576c Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 15 Nov 2026 15:11:37 +0100 Subject: [PATCH 1/2] Make sure we can call read within transactions --- include/sqlgen/postgres/Iterator.hpp | 3 + .../sqlgen/postgres/PostgresV2Connection.hpp | 10 ++- src/sqlgen/postgres/Connection.cpp | 11 ++- src/sqlgen/postgres/Iterator.cpp | 20 ++++-- src/sqlgen/postgres/PostgresV2Connection.cpp | 11 ++- .../test_read_within_transaction_rollback.cpp | 70 +++++++++++++++++++ 6 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 tests/postgres/test_read_within_transaction_rollback.cpp diff --git a/include/sqlgen/postgres/Iterator.hpp b/include/sqlgen/postgres/Iterator.hpp index 598599c..2430f47 100644 --- a/include/sqlgen/postgres/Iterator.hpp +++ b/include/sqlgen/postgres/Iterator.hpp @@ -69,6 +69,9 @@ class SQLGEN_API Iterator { /// Whether the end is reached. bool end_; + + /// Whether the iterator opened its own transaction around the cursor. + bool started_transaction_; }; } // namespace sqlgen::postgres diff --git a/include/sqlgen/postgres/PostgresV2Connection.hpp b/include/sqlgen/postgres/PostgresV2Connection.hpp index 5c37514..a129b83 100644 --- a/include/sqlgen/postgres/PostgresV2Connection.hpp +++ b/include/sqlgen/postgres/PostgresV2Connection.hpp @@ -20,7 +20,8 @@ class SQLGEN_API PostgresV2Connection { PostgresV2Connection(PGconn* _ptr) : ptr_(Ref::make(std::shared_ptr(_ptr, &PQfinish)) - .value()) {} + .value()), + caller_in_transaction_(std::make_shared(false)) {} PostgresV2Connection(PGconn* _ptr, NoticeHandler _notice_handler); @@ -43,9 +44,16 @@ class SQLGEN_API PostgresV2Connection { PGconn* ptr() const { return ptr_.get(); } + bool in_caller_transaction() const { return *caller_in_transaction_; } + + void set_in_caller_transaction(bool _value) { + *caller_in_transaction_ = _value; + } + private: Ref ptr_; std::shared_ptr notice_handler_; + std::shared_ptr caller_in_transaction_; }; } // namespace sqlgen::postgres diff --git a/src/sqlgen/postgres/Connection.cpp b/src/sqlgen/postgres/Connection.cpp index 07abab2..8d5205b 100644 --- a/src/sqlgen/postgres/Connection.cpp +++ b/src/sqlgen/postgres/Connection.cpp @@ -21,10 +21,14 @@ Connection::Connection(const Credentials& _credentials) .value()) {} Result Connection::begin_transaction() noexcept { + conn_.set_in_caller_transaction(true); return execute("BEGIN TRANSACTION;"); } -Result Connection::commit() noexcept { return execute("COMMIT;"); } +Result Connection::commit() noexcept { + conn_.set_in_caller_transaction(false); + return execute("COMMIT;"); +} Result Connection::execute(const std::string& _sql) noexcept { return PostgresV2Result::make(_sql, conn_).transform([](auto&&) { @@ -203,7 +207,10 @@ Result> Connection::read_impl( return Iterator::make(sql, conn_); } -Result Connection::rollback() noexcept { return execute("ROLLBACK;"); } +Result Connection::rollback() noexcept { + conn_.set_in_caller_transaction(false); + return execute("ROLLBACK;"); +} std::string Connection::to_buffer( const std::vector>& _line) const noexcept { diff --git a/src/sqlgen/postgres/Iterator.cpp b/src/sqlgen/postgres/Iterator.cpp index d99ea30..f7ae222 100644 --- a/src/sqlgen/postgres/Iterator.cpp +++ b/src/sqlgen/postgres/Iterator.cpp @@ -1,5 +1,6 @@ #include "sqlgen/postgres/Iterator.hpp" +#include #include #include #include @@ -11,16 +12,23 @@ namespace sqlgen::postgres { Iterator::Iterator(const std::string& _sql, const Conn& _conn) - : cursor_name_(make_cursor_name()), conn_(_conn), end_(false) { - exec(conn_, "BEGIN").value(); + : cursor_name_(make_cursor_name()), + conn_(_conn), + end_(false), + started_transaction_(!conn_.in_caller_transaction()) { + if (started_transaction_) { + exec(conn_, "BEGIN").value(); + } exec(conn_, "DECLARE " + cursor_name_ + " CURSOR FOR " + _sql).value(); } Iterator::Iterator(Iterator&& _other) noexcept : cursor_name_(std::move(_other.cursor_name_)), conn_(std::move(_other.conn_)), - end_(_other.end_) { + end_(_other.end_), + started_transaction_(_other.started_transaction_) { _other.end_ = true; + _other.started_transaction_ = false; } Iterator::~Iterator() { shutdown(); } @@ -77,14 +85,18 @@ Iterator& Iterator::operator=(Iterator&& _other) noexcept { cursor_name_ = std::move(_other.cursor_name_); conn_ = std::move(_other.conn_); end_ = _other.end_; + started_transaction_ = _other.started_transaction_; _other.end_ = true; + _other.started_transaction_ = false; return *this; } void Iterator::shutdown() { if (!end_) { exec(conn_, "CLOSE " + cursor_name_); - exec(conn_, "END"); + if (started_transaction_) { + exec(conn_, "END"); + } end_ = true; } } diff --git a/src/sqlgen/postgres/PostgresV2Connection.cpp b/src/sqlgen/postgres/PostgresV2Connection.cpp index 48f5bfe..9dfaf2b 100644 --- a/src/sqlgen/postgres/PostgresV2Connection.cpp +++ b/src/sqlgen/postgres/PostgresV2Connection.cpp @@ -4,8 +4,7 @@ namespace sqlgen::postgres { namespace { void notice_trampoline(void* arg, const char* message) { - auto* handler = - static_cast(arg); + auto* handler = static_cast(arg); if (handler && *handler) { (*handler)(message); } @@ -15,10 +14,10 @@ void notice_trampoline(void* arg, const char* message) { PostgresV2Connection::PostgresV2Connection(PGconn* _ptr, NoticeHandler _notice_handler) : ptr_(Ref::make(std::shared_ptr(_ptr, &PQfinish)).value()), - notice_handler_(_notice_handler - ? std::make_shared( - std::move(_notice_handler)) - : nullptr) { + notice_handler_(_notice_handler ? std::make_shared( + std::move(_notice_handler)) + : nullptr), + caller_in_transaction_(std::make_shared(false)) { if (notice_handler_) { PQsetNoticeProcessor(ptr_.get(), notice_trampoline, notice_handler_.get()); } diff --git a/tests/postgres/test_read_within_transaction_rollback.cpp b/tests/postgres/test_read_within_transaction_rollback.cpp new file mode 100644 index 0000000..1b83627 --- /dev/null +++ b/tests/postgres/test_read_within_transaction_rollback.cpp @@ -0,0 +1,70 @@ +#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY + +#include + +#include +#include +#include +#include +#include +#include "test_helpers.hpp" + +namespace test_read_within_transaction_rollback { + +struct Person { + sqlgen::PrimaryKey id; + std::string first_name; + std::string last_name; + int age; +}; + +TEST(postgres, test_read_within_transaction_rollback) { + const auto existing = std::vector( + {Person{.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45}}); + + const auto to_insert = std::vector( + {Person{.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 44}}); + + const auto credentials = sqlgen::postgres::test::make_credentials(); + + using namespace sqlgen; + using namespace sqlgen::literals; + + const auto conn = + sqlgen::postgres::connect(credentials).and_then(drop | if_exists); + + sqlgen::write(conn, existing).value(); + + const auto read_all = sqlgen::read> | order_by("id"_c); + + const auto read_before = begin_transaction(sqlgen::postgres::connect(credentials)) + .and_then(commit) + .and_then(read_all) + .value(); + + // Within a transaction: write, then read (which goes through a cursor + // iterator). The read must not commit the transaction, so a subsequent + // rollback undoes the write. If the read committed the transaction, the + // inserted row would survive the rollback and read_after would differ from + // read_before. + const auto txn = + begin_transaction(sqlgen::postgres::connect(credentials)).value(); + + insert(std::ref(to_insert))(txn->conn()).value(); + + const auto read_in_txn = read_all(txn->conn()).value(); + EXPECT_EQ(read_in_txn.size(), 2); + + txn->rollback().value(); + + const auto read_after = begin_transaction(sqlgen::postgres::connect(credentials)) + .and_then(commit) + .and_then(read_all) + .value(); + + EXPECT_EQ(rfl::json::write(read_before), rfl::json::write(read_after)); +} + +} // namespace test_read_within_transaction_rollback + +#endif From 13ea0e87c0f197bbbaa07c2acfd801be1fade449 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 15 Nov 2026 15:13:38 +0100 Subject: [PATCH 2/2] Make sure we actually build the postgres --- .github/workflows/linux-cxx20-vcpkg.yaml | 2 +- .github/workflows/macos-cxx20-vcpkg.yaml | 18 +++++++------- .github/workflows/windows-cxx20-vcpkg.yaml | 28 +++++++++++----------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.github/workflows/linux-cxx20-vcpkg.yaml b/.github/workflows/linux-cxx20-vcpkg.yaml index 72f6358..2f1b3b8 100644 --- a/.github/workflows/linux-cxx20-vcpkg.yaml +++ b/.github/workflows/linux-cxx20-vcpkg.yaml @@ -117,7 +117,7 @@ jobs: sudo ln -s $(which ccache) /usr/local/bin/$CC sudo ln -s $(which ccache) /usr/local/bin/$CXX $CXX --version - cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF + cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=ON -DSQLGEN_SQLITE3=OFF cmake --build build - name: Compile if: matrix.db == 'duckdb' diff --git a/.github/workflows/macos-cxx20-vcpkg.yaml b/.github/workflows/macos-cxx20-vcpkg.yaml index 31b2a93..560f8cf 100644 --- a/.github/workflows/macos-cxx20-vcpkg.yaml +++ b/.github/workflows/macos-cxx20-vcpkg.yaml @@ -3,7 +3,7 @@ name: macos-cxx20-vcpkg on: [push, pull_request] -env: +env: VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite" jobs: @@ -17,7 +17,7 @@ jobs: - os: "macos-latest" db: sqlite - os: "macos-latest" - db: mysql + db: mysql - os: "macos-latest" db: duckdb name: "${{ github.job }} (${{ matrix.os }}-${{ matrix.db }})" @@ -45,11 +45,11 @@ jobs: create-symlink: true - name: Run vcpkg uses: lukka/run-vcpkg@v11 - - name: Install ninja + - name: Install ninja run: brew install ninja autoconf bison flex pkg-config icu4c if: matrix.os == 'macos-latest' - name: Compile - if: matrix.db == 'duckdb' + if: matrix.db == 'duckdb' env: CC: clang CXX: clang++ @@ -59,10 +59,10 @@ jobs: export CMAKE_GENERATOR=Ninja fi $CXX --version - cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF + cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF cmake --build build -j 4 - name: Compile - if: matrix.db == 'postgres' + if: matrix.db == 'postgres' env: CC: clang CXX: clang++ @@ -73,10 +73,10 @@ jobs: export MACOSX_DEPLOYMENT_TARGET="$(sw_vers -productVersion)" fi $CXX --version - cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON + cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON cmake --build build -j 4 - name: Compile - if: matrix.db == 'sqlite' + if: matrix.db == 'sqlite' env: CC: clang CXX: clang++ @@ -89,7 +89,7 @@ jobs: cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF cmake --build build -j 4 - name: Compile - if: matrix.db == 'mysql' + if: matrix.db == 'mysql' env: CC: clang CXX: clang++ diff --git a/.github/workflows/windows-cxx20-vcpkg.yaml b/.github/workflows/windows-cxx20-vcpkg.yaml index 0bf3043..1a3366f 100644 --- a/.github/workflows/windows-cxx20-vcpkg.yaml +++ b/.github/workflows/windows-cxx20-vcpkg.yaml @@ -1,8 +1,8 @@ -name: windows-cxx20-vcpkg +name: windows-cxx20-vcpkg on: [push, pull_request] -env: +env: VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite" jobs: @@ -11,10 +11,10 @@ jobs: fail-fast: false matrix: include: - - db: duckdb + - db: duckdb - db: postgres - db: sqlite - - db: mysql + - db: mysql name: "(windows-${{ matrix.db }})" concurrency: group: ci-${{ github.ref }}-windows-${{ matrix.db }} @@ -35,25 +35,25 @@ jobs: - uses: ilammy/msvc-dev-cmd@v1 - uses: lukka/run-vcpkg@v11 - name: Compile - if: matrix.db == 'duckdb' + if: matrix.db == 'duckdb' run: | - cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF - cmake --build build --config Release -j4 + cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_DUCKDB=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF + cmake --build build --config Release -j4 - name: Compile - if: matrix.db == 'postgres' + if: matrix.db == 'postgres' run: | - cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-windows-release - cmake --build build --config Release -j4 + cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_SQLITE3=OFF -DSQLGEN_POSTGRES=ON -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-windows-release + cmake --build build --config Release -j4 - name: Compile - if: matrix.db == 'sqlite' + if: matrix.db == 'sqlite' run: | cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_POSTGRES=OFF -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-windows-release - cmake --build build --config Release -j4 + cmake --build build --config Release -j4 - name: Compile - if: matrix.db == 'mysql' + if: matrix.db == 'mysql' run: | cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DSQLGEN_BUILD_TESTS=ON -DSQLGEN_MYSQL=ON -DSQLGEN_POSTGRES=OFF -DSQLGEN_SQLITE3=OFF -DSQLGEN_BUILD_DRY_TESTS_ONLY=ON -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-windows-release - cmake --build build --config Release -j4 + cmake --build build --config Release -j4 - name: Run tests run: | ctest --test-dir build --output-on-failure