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
29 changes: 9 additions & 20 deletions include/sqlgen/insert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "internal/to_str_vec.hpp"
#include "is_connection.hpp"
#include "transpilation/to_insert_or_write.hpp"
#include "transpilation/value_t.hpp"

namespace sqlgen {

Expand Down Expand Up @@ -84,31 +85,19 @@ auto insert(const Args&... args) {

template <class ContainerType>
auto insert_or_replace(const auto& _conn, const ContainerType& _data) {
if constexpr (std::ranges::input_range<std::remove_cvref_t<ContainerType>>) {
static_assert(
internal::has_constraint_v<typename ContainerType::value_type>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
} else {
static_assert(internal::has_constraint_v<ContainerType>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
}
static_assert(
internal::has_constraint_v<transpilation::value_t<ContainerType>>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
return insert_impl(_conn, _data, true);
}

template <class ContainerType>
auto insert_or_replace(const ContainerType& _data) {
if constexpr (std::ranges::input_range<std::remove_cvref_t<ContainerType>>) {
static_assert(
internal::has_constraint_v<typename ContainerType::value_type>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
} else {
static_assert(internal::has_constraint_v<ContainerType>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
}
static_assert(
internal::has_constraint_v<transpilation::value_t<ContainerType>>,
"The table must have a primary key or unique column for "
"insert_or_replace(...) to work.");
return insert_impl(_data, true);
}

Expand Down
24 changes: 20 additions & 4 deletions include/sqlgen/transpilation/value_t.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
#ifndef SQLGEN_TRANSPILATION_VALUET_HPP_
#define SQLGEN_TRANSPILATION_VALUET_HPP_

#include <functional>
#include <ranges>
#include <type_traits>

#include "../Range.hpp"

namespace sqlgen::transpilation {

template <class ContainerType>
struct ValueType;

template <class T>
requires(!std::ranges::input_range<T>)
struct ValueType<T> {
using Type = std::remove_cvref_t<T>;
};

template <class ContainerType>
struct ValueType {
using Type = typename ContainerType::value_type;
requires std::ranges::input_range<ContainerType>
struct ValueType<ContainerType> {
using Type = std::remove_cvref_t<typename ContainerType::value_type>;
};

template <class T>
struct ValueType<Range<T>> {
using Type = typename Range<T>::value_type::value_type;
using Type = std::remove_cvref_t<typename Range<T>::value_type::value_type>;
};

template <class T>
struct ValueType<std::reference_wrapper<T>> {
using Type = typename ValueType<std::remove_cvref_t<T>>::Type;
};

template <class T>
using value_t = typename ValueType<T>::Type;
using value_t = typename ValueType<std::remove_cvref_t<T>>::Type;

} // namespace sqlgen::transpilation

Expand Down
31 changes: 31 additions & 0 deletions tests/postgres/test_read_to_sql.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <gtest/gtest.h>

#include <iostream>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>

namespace test_write_and_read_dry {

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

TEST(postgres, test_write_and_read_dry) {
using namespace sqlgen;
using namespace sqlgen::literals;

const auto query1 =
postgres::to_sql(sqlgen::read<std::vector<Person>> | where("id"_c == 1));
const auto query2 =
postgres::to_sql(sqlgen::read<Person> | where("id"_c == 1));

std::cout << query1 << std::endl;
std::cout << query2 << std::endl;
}

} // namespace test_write_and_read_dry

44 changes: 27 additions & 17 deletions tests/sqlite/test_insert_or_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,42 @@ TEST(sqlite, test_insert_or_replace) {
Person{
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});

const auto people2 = std::vector<Person>(
{Person{.id = 1, .first_name = "Bartholomew", .last_name = "Simpson", .age = 10},
Person{
.id = 3, .first_name = "Margaret", .last_name = "Simpson", .age = 1}});
const auto people2 = std::vector<Person>({Person{.id = 1,
.first_name = "Bartholomew",
.last_name = "Simpson",
.age = 10},
Person{.id = 3,
.first_name = "Margaret",
.last_name = "Simpson",
.age = 1}});

const auto people3 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bartholomew", .last_name = "Simpson", .age = 10},
Person{.id = 1,
.first_name = "Bartholomew",
.last_name = "Simpson",
.age = 10},
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 3, .first_name = "Margaret", .last_name = "Simpson", .age = 1}});
Person{.id = 3,
.first_name = "Margaret",
.last_name = "Simpson",
.age = 1}});

using namespace sqlgen;
using namespace sqlgen::literals;

const auto people4 = sqlite::connect()
.and_then(begin_transaction)
.and_then(create_table<Person> | if_not_exists)
.and_then(insert(people1))
.and_then(commit)
.and_then(begin_transaction)
.and_then(insert_or_replace(people2))
.and_then(commit)
.and_then(sqlgen::read<std::vector<Person>> | order_by("id"_c))
.value();
const auto people4 =
sqlite::connect()
.and_then(begin_transaction)
.and_then(create_table<Person> | if_not_exists)
.and_then(insert(people1))
.and_then(commit)
.and_then(begin_transaction)
.and_then(insert_or_replace(std::ref(people2)))
.and_then(commit)
.and_then(sqlgen::read<std::vector<Person>> | order_by("id"_c))
.value();

const auto json3 = rfl::json::write(people3);
const auto json4 = rfl::json::write(people4);
Expand Down
Loading