mirror of
https://github.com/getml/sqlgen.git
synced 2026-04-28 19:19:16 -05:00
Added support for foreign key constraints (#37)
This commit is contained in:
committed by
GitHub
parent
7d9cd0d38f
commit
098deb9477
@@ -0,0 +1,58 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_foreign_key {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Relationship {
|
||||
sqlgen::ForeignKey<uint32_t, Person, "id"> parent_id;
|
||||
uint32_t child_id;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_foreign_key) {
|
||||
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 relationships =
|
||||
std::vector<Relationship>({Relationship{.parent_id = 0, .child_id = 2},
|
||||
Relationship{.parent_id = 0, .child_id = 3},
|
||||
Relationship{.parent_id = 0, .child_id = 4},
|
||||
Relationship{.parent_id = 1, .child_id = 2},
|
||||
Relationship{.parent_id = 1, .child_id = 3},
|
||||
Relationship{.parent_id = 1, .child_id = 4}});
|
||||
|
||||
using namespace sqlgen;
|
||||
using namespace sqlgen::literals;
|
||||
|
||||
const auto people = sqlite::connect()
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(drop<Relationship> | if_exists)
|
||||
.and_then(begin_transaction)
|
||||
.and_then(create_table<Person>)
|
||||
.and_then(create_table<Relationship>)
|
||||
.and_then(insert(std::ref(people1)))
|
||||
.and_then(insert(std::ref(relationships)))
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(drop<Relationship> | if_exists)
|
||||
.and_then(commit)
|
||||
.value();
|
||||
}
|
||||
|
||||
} // namespace test_foreign_key
|
||||
|
||||
Reference in New Issue
Block a user