Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux-cxx20-vcpkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/macos-cxx20-vcpkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: macos-cxx20-vcpkg
on: [push, pull_request]


env:
env:
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"

jobs:
Expand All @@ -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 }})"
Expand Down Expand Up @@ -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++
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -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++
Expand Down
28 changes: 14 additions & 14 deletions .github/workflows/windows-cxx20-vcpkg.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 }}
Expand All @@ -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
3 changes: 3 additions & 0 deletions include/sqlgen/postgres/Iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion include/sqlgen/postgres/PostgresV2Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class SQLGEN_API PostgresV2Connection {

PostgresV2Connection(PGconn* _ptr)
: ptr_(Ref<PGconn>::make(std::shared_ptr<PGconn>(_ptr, &PQfinish))
.value()) {}
.value()),
caller_in_transaction_(std::make_shared<bool>(false)) {}

PostgresV2Connection(PGconn* _ptr, NoticeHandler _notice_handler);

Expand All @@ -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<PGconn> ptr_;
std::shared_ptr<NoticeHandler> notice_handler_;
std::shared_ptr<bool> caller_in_transaction_;
};

} // namespace sqlgen::postgres
Expand Down
11 changes: 9 additions & 2 deletions src/sqlgen/postgres/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
.value()) {}

Result<Nothing> Connection::begin_transaction() noexcept {
conn_.set_in_caller_transaction(true);
return execute("BEGIN TRANSACTION;");
}

Result<Nothing> Connection::commit() noexcept { return execute("COMMIT;"); }
Result<Nothing> Connection::commit() noexcept {
conn_.set_in_caller_transaction(false);
return execute("COMMIT;");
}

Result<Nothing> Connection::execute(const std::string& _sql) noexcept {
return PostgresV2Result::make(_sql, conn_).transform([](auto&&) {
Expand Down Expand Up @@ -203,7 +207,10 @@
return Iterator::make(sql, conn_);
}

Result<Nothing> Connection::rollback() noexcept { return execute("ROLLBACK;"); }
Result<Nothing> Connection::rollback() noexcept {
conn_.set_in_caller_transaction(false);
return execute("ROLLBACK;");
}

std::string Connection::to_buffer(
const std::vector<std::optional<std::string>>& _line) const noexcept {
Expand All @@ -212,7 +219,7 @@
const auto edit_field =
[](const std::optional<std::string>& _field) -> std::string {
if (!_field) {
return "\e";

Check warning on line 222 in src/sqlgen/postgres/Connection.cpp

View workflow job for this annotation

GitHub Actions / (windows-postgres)

'e': unrecognized character escape sequence [D:\a\sqlgen\sqlgen\build\sqlgen.vcxproj]

Check warning on line 222 in src/sqlgen/postgres/Connection.cpp

View workflow job for this annotation

GitHub Actions / (windows-postgres)

'e': unrecognized character escape sequence [D:\a\sqlgen\sqlgen\build\sqlgen.vcxproj]
}
if (_field->find("\t") != std::string::npos) {
return "\a" + *_field + "\a";
Expand Down
20 changes: 16 additions & 4 deletions src/sqlgen/postgres/Iterator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "sqlgen/postgres/Iterator.hpp"

#include <cstdio>
#include <ranges>
#include <rfl.hpp>
#include <sstream>
Expand All @@ -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(); }
Expand Down Expand Up @@ -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;
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/sqlgen/postgres/PostgresV2Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace sqlgen::postgres {

namespace {
void notice_trampoline(void* arg, const char* message) {
auto* handler =
static_cast<PostgresV2Connection::NoticeHandler*>(arg);
auto* handler = static_cast<PostgresV2Connection::NoticeHandler*>(arg);
if (handler && *handler) {
(*handler)(message);
}
Expand All @@ -15,10 +14,10 @@ void notice_trampoline(void* arg, const char* message) {
PostgresV2Connection::PostgresV2Connection(PGconn* _ptr,
NoticeHandler _notice_handler)
: ptr_(Ref<PGconn>::make(std::shared_ptr<PGconn>(_ptr, &PQfinish)).value()),
notice_handler_(_notice_handler
? std::make_shared<NoticeHandler>(
std::move(_notice_handler))
: nullptr) {
notice_handler_(_notice_handler ? std::make_shared<NoticeHandler>(
std::move(_notice_handler))
: nullptr),
caller_in_transaction_(std::make_shared<bool>(false)) {
if (notice_handler_) {
PQsetNoticeProcessor(ptr_.get(), notice_trampoline, notice_handler_.get());
}
Expand Down
70 changes: 70 additions & 0 deletions tests/postgres/test_read_within_transaction_rollback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY

#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
#include "test_helpers.hpp"

namespace test_read_within_transaction_rollback {

struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
int age;
};

TEST(postgres, test_read_within_transaction_rollback) {
const auto existing = std::vector<Person>(
{Person{.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45}});

const auto to_insert = std::vector<Person>(
{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<Person> | if_exists);

sqlgen::write(conn, existing).value();

const auto read_all = sqlgen::read<std::vector<Person>> | 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
Loading