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
6 changes: 0 additions & 6 deletions .github/workflows/linux-cxx20-conan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ jobs:
- compiler: gcc
compiler-version: 12
link: "static"
- compiler: gcc
compiler-version: 14
link: "static"
- compiler: llvm
compiler-version: 16
link: "shared"
Expand All @@ -37,9 +34,6 @@ jobs:
- compiler: gcc
compiler-version: 12
link: "shared"
- compiler: gcc
compiler-version: 14
link: "shared"
name: "${{ github.job }} (${{ matrix.compiler }}-${{ matrix.compiler-version }}-${{ matrix.link }})"
runs-on: ubuntu-latest
steps:
Expand Down
13 changes: 13 additions & 0 deletions include/sqlgen/internal/random.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <random>
#include <string>

namespace sqlgen::internal {

/// Generates a random number to be used in the names of iterators and such.
inline std::string random() {
std::random_device rd;
std::mt19937 gen(rd());
return std::to_string(gen());
}

} // namespace sqlgen::internal
33 changes: 20 additions & 13 deletions src/sqlgen/postgres/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdexcept>

#include "sqlgen/internal/collect/vector.hpp"
#include "sqlgen/internal/random.hpp"
#include "sqlgen/internal/strings/strings.hpp"
#include "sqlgen/postgres/Iterator.hpp"

Expand Down Expand Up @@ -36,12 +37,18 @@ Result<Nothing> Connection::insert_impl(
return Nothing{};
}

const auto name = "sqlgen_insert_into_table_" + internal::random();

const auto sql = to_sql_impl(_stmt);

const auto res = execute("PREPARE \"sqlgen_insert_into_table\" AS " + sql);
const auto res = PQprepare(conn_.get(), name.c_str(), sql.c_str(),
_data.at(0).size(), nullptr);

const auto status = PQresultStatus(res);

if (!res) {
return res;
if (status != PGRES_COMMAND_OK) {
return error("Generating prepared statement for '" + sql +
"' failed: " + PQresultErrorMessage(res));
}

std::vector<const char*> current_row(_data[0].size());
Expand All @@ -52,7 +59,7 @@ Result<Nothing> Connection::insert_impl(
const auto& d = _data[i];

if (d.size() != current_row.size()) {
execute("DEALLOCATE sqlgen_insert_into_table;");
execute("DEALLOCATE " + name + ";");
return error("Error in entry " + std::to_string(i) + ": Expected " +
std::to_string(current_row.size()) + " entries, got " +
std::to_string(d.size()));
Expand All @@ -62,26 +69,26 @@ Result<Nothing> Connection::insert_impl(
current_row[j] = d[j] ? d[j]->c_str() : nullptr;
}

const auto res = PQexecPrepared(conn_.get(), // conn
"sqlgen_insert_into_table", // stmtName
n_params, // nParams
current_row.data(), // paramValues
nullptr, // paramLengths
nullptr, // paramFormats
0 // resultFormat
const auto res = PQexecPrepared(conn_.get(), // conn
name.c_str(), // stmtName
n_params, // nParams
current_row.data(), // paramValues
nullptr, // paramLengths
nullptr, // paramFormats
0 // resultFormat
);

const auto status = PQresultStatus(res);

if (status != PGRES_COMMAND_OK) {
const auto err = error(std::string("Executing INSERT failed: ") +
PQresultErrorMessage(res));
execute("DEALLOCATE sqlgen_insert_into_table;");
execute("DEALLOCATE " + name + ";");
return err;
}
}

return execute("DEALLOCATE sqlgen_insert_into_table;");
return execute("DEALLOCATE " + name + ";");
}

rfl::Result<Ref<Connection>> Connection::make(
Expand Down
64 changes: 64 additions & 0 deletions tests/mysql/test_insert_fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY

#include <gtest/gtest.h>

#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/mysql.hpp>
#include <vector>

namespace test_insert_fail {

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

TEST(mysql, test_insert_fail) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});

const auto credentials = sqlgen::mysql::Credentials{.host = "localhost",
.user = "sqlgen",
.password = "password",
.dbname = "mysql"};

using namespace sqlgen;
using namespace sqlgen::literals;

const auto conn = mysql::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(write(people1.at(0)));

const auto res = conn.and_then(begin_transaction)
.and_then(insert(people1.at(0)))
.and_then(commit);

// Should fail - duplicate key violation.
EXPECT_FALSE(res && true);

const auto people2 =
conn.and_then(begin_transaction)
.and_then(insert(people1 | std::ranges::views::drop(1)))
.and_then(commit)
.and_then(sqlgen::read<std::vector<Person>>)
.value();

const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);

EXPECT_EQ(json1, json2);
}

} // namespace test_insert_fail

#endif
64 changes: 64 additions & 0 deletions tests/postgres/test_insert_fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY

#include <gtest/gtest.h>

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

namespace test_insert_fail {

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

TEST(postgres, test_insert_fail) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});

const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};

using namespace sqlgen;
using namespace sqlgen::literals;

const auto conn = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(write(people1.at(0)));

const auto res = conn.and_then(begin_transaction)
.and_then(insert(people1.at(0)))
.and_then(commit);

// Should fail - duplicate key violation.
EXPECT_FALSE(res && true);

const auto people2 =
conn.and_then(begin_transaction)
.and_then(insert(people1 | std::ranges::views::drop(1)))
.and_then(commit)
.and_then(sqlgen::read<std::vector<Person>>)
.value();

const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);

EXPECT_EQ(json1, json2);
}

} // namespace test_insert_fail

#endif
59 changes: 59 additions & 0 deletions tests/sqlite/test_insert_fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY

#include <gtest/gtest.h>

#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/sqlite.hpp>
#include <vector>

namespace test_insert_fail {

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

TEST(sqlite, test_insert_fail) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});

using namespace sqlgen;
using namespace sqlgen::literals;

const auto conn = sqlite::connect()
.and_then(drop<Person> | if_exists)
.and_then(write(people1.at(0)));

const auto res = conn.and_then(begin_transaction)
.and_then(insert(people1.at(0)))
.and_then(commit);

// Should fail - duplicate key violation.
EXPECT_FALSE(res && true);

const auto people2 =
conn.and_then(begin_transaction)
.and_then(insert(people1 | std::ranges::views::drop(1)))
.and_then(commit)
.and_then(sqlgen::read<std::vector<Person>>)
.value();

const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);

EXPECT_EQ(json1, json2);
}

} // namespace test_insert_fail

#endif
Loading