Skip to content

Commit 8a69cad

Browse files
Began developing the autoincr primary key
1 parent 1db68cd commit 8a69cad

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

include/sqlgen/duckdb/Connection.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@
1818
#include "../Transaction.hpp"
1919
#include "../dynamic/Write.hpp"
2020
#include "../internal/iterator_t.hpp"
21+
#include "../internal/remove_auto_incr_primary_t.hpp"
2122
#include "../internal/to_container.hpp"
2223
#include "../is_connection.hpp"
23-
#include "../transpilation/get_tablename.hpp"
24-
#include "../transpilation/has_reflection_method.hpp"
25-
#include "../transpilation/is_nullable.hpp"
2624
#include "./parsing/Parser_default.hpp"
2725
#include "DuckDBConnection.hpp"
2826
#include "Iterator.hpp"
29-
#include "parsing/Parser.hpp"
3027
#include "sqlgen/dynamic/Operation.hpp"
3128
#include "sqlgen/dynamic/SelectFrom.hpp"
32-
#include "sqlgen/transpilation/get_schema.hpp"
33-
#include "sqlgen/transpilation/to_table_or_query.hpp"
3429
#include "to_sql.hpp"
3530

3631
namespace sqlgen::duckdb {
@@ -199,8 +194,10 @@ class Connection {
199194
template <class StructT>
200195
Result<Nothing> write_row(const StructT &_struct,
201196
duckdb_appender _appender) noexcept {
197+
using ViewType =
198+
internal::remove_auto_incr_primary_t<rfl::view_t<const StructT>>;
202199
Result<Nothing> res = Nothing{};
203-
rfl::to_view(_struct).apply([&](const auto &_field) {
200+
ViewType(rfl::to_view(_struct)).apply([&](const auto &_field) {
204201
using ValueType = std::remove_cvref_t<std::remove_pointer_t<
205202
typename std::remove_cvref_t<decltype(_field)>::Type>>;
206203
if (res) {

src/sqlgen/duckdb/to_sql.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ std::string operation_to_sql(const dynamic::Operation& _stmt) noexcept {
673673

674674
std::string properties_to_sql(const dynamic::types::Properties& _p) noexcept {
675675
return [&]() -> std::string {
676-
return std::string(_p.auto_incr ? " GENERATED ALWAYS AS IDENTITY" : "") +
677-
std::string(_p.nullable ? "" : " NOT NULL") +
676+
return std::string(_p.nullable ? "" : " NOT NULL") +
677+
std::string(_p.auto_incr ? " GENERATED ALWAYS" : "") +
678678
std::string(_p.unique ? " UNIQUE" : "");
679679
}() + [&]() -> std::string {
680680
if (!_p.foreign_key_reference) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <rfl.hpp>
4+
#include <rfl/json.hpp>
5+
#include <sqlgen.hpp>
6+
#include <sqlgen/duckdb.hpp>
7+
#include <vector>
8+
9+
namespace test_auto_incr_primary_key {
10+
11+
struct Person {
12+
sqlgen::PrimaryKey<uint32_t, sqlgen::auto_incr> id;
13+
std::string first_name;
14+
std::string last_name;
15+
int age;
16+
};
17+
18+
TEST(duckdb, test_auto_incr_primary_key) {
19+
auto people1 = std::vector<Person>(
20+
{Person{.first_name = "Homer", .last_name = "Simpson", .age = 45},
21+
Person{.first_name = "Bart", .last_name = "Simpson", .age = 10},
22+
Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8},
23+
Person{.first_name = "Maggie", .last_name = "Simpson", .age = 0}});
24+
25+
using namespace sqlgen;
26+
using namespace sqlgen::literals;
27+
28+
const auto people2 = duckdb::connect()
29+
.and_then(write(std::ref(people1)))
30+
.and_then(sqlgen::read<std::vector<Person>> |
31+
order_by("age"_c.desc()))
32+
.value();
33+
34+
people1.at(0).id = 1;
35+
people1.at(1).id = 2;
36+
people1.at(2).id = 3;
37+
people1.at(3).id = 4;
38+
39+
const auto json1 = rfl::json::write(people1);
40+
const auto json2 = rfl::json::write(people2);
41+
42+
EXPECT_EQ(json1, json2);
43+
}
44+
45+
} // namespace test_auto_incr_primary_key

0 commit comments

Comments
 (0)