From 1231e84a45bdb927118fd4ad66643d63d7f611cc Mon Sep 17 00:00:00 2001 From: Yevgeniy Zakharov Date: Fri, 8 Nov 2024 20:01:55 +0500 Subject: [PATCH 1/2] added column_pointer support to fk --- dev/constraints.h | 6 ++ include/sqlite_orm/sqlite_orm.h | 7 +++ tests/constraints/foreign_key.cpp | 95 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/dev/constraints.h b/dev/constraints.h index 9a6a4188..e9cf651c 100644 --- a/dev/constraints.h +++ b/dev/constraints.h @@ -16,6 +16,7 @@ #include "error_code.h" #include "table_type_of.h" #include "type_printer.h" +#include "column_pointer.h" namespace sqlite_orm { @@ -375,6 +376,11 @@ namespace sqlite_orm { foreign_key_t, std::tuple> references(Rs... refs) { return {std::move(this->columns), {std::forward(refs)...}}; } + + template + foreign_key_t, std::tuple...>> references(Rs... refs) { + return {std::move(this->columns), {sqlite_orm::column(refs)...}}; + } }; #endif diff --git a/include/sqlite_orm/sqlite_orm.h b/include/sqlite_orm/sqlite_orm.h index 410b2c0a..ff679345 100644 --- a/include/sqlite_orm/sqlite_orm.h +++ b/include/sqlite_orm/sqlite_orm.h @@ -3191,6 +3191,8 @@ namespace sqlite_orm { // #include "type_printer.h" +// #include "column_pointer.h" + namespace sqlite_orm { namespace internal { @@ -3549,6 +3551,11 @@ namespace sqlite_orm { foreign_key_t, std::tuple> references(Rs... refs) { return {std::move(this->columns), {std::forward(refs)...}}; } + + template + foreign_key_t, std::tuple...>> references(Rs... refs) { + return {std::move(this->columns), {sqlite_orm::column(refs)...}}; + } }; #endif diff --git a/tests/constraints/foreign_key.cpp b/tests/constraints/foreign_key.cpp index 5163baea..416d1a0d 100644 --- a/tests/constraints/foreign_key.cpp +++ b/tests/constraints/foreign_key.cpp @@ -128,4 +128,99 @@ TEST_CASE("Foreign key 2") { storage.update(t2); } + +TEST_CASE("Foreign key with inheritance") { + struct Person { + int id; + std::string name; + int age; + }; + + // Define derived class Student + struct Student : public Person { + std::string school_name; + + Student(int id, std::string name, int age, std::string school_name) : + Person{id, std::move(name), age}, school_name(std::move(school_name)) {} + }; + + // Define derived class Teacher + struct Teacher : public Person { + std::string subject; + double salary; + + Teacher(int id, std::string name, int age, std::string subject, double salary) : + Person{id, std::move(name), age}, subject(subject), salary(salary) {} + }; + + // Define Classroom class referencing Teacher and Student + struct Classroom { + int id; + int teacher_id; // Foreign key referencing Teacher + int student_id; // Foreign key referencing Student + std::string room_name; + }; + + auto storage = + make_storage("", + + // Define the Person table as a base, though it is not used directly + make_table("persons", + make_column("id", &Person::id, primary_key().autoincrement()), + make_column("name", &Person::name), + make_column("age", &Person::age)), + + // Define the Student table with foreign key inheritance + make_table("students", + make_column("id", &Student::id, primary_key()), + make_column("name", &Student::name), + make_column("age", &Student::age), + make_column("school_name", &Student::school_name)), + + // Define the Teacher table with foreign key inheritance + make_table("teachers", + make_column("id", &Teacher::id, primary_key()), + make_column("name", &Teacher::name), + make_column("age", &Teacher::age), + make_column("subject", &Teacher::subject), + make_column("salary", &Teacher::salary)), + + // Define the Classroom table with foreign keys to Teacher and Student + make_table("classrooms", + make_column("id", &Classroom::id, primary_key().autoincrement()), + make_column("teacher_id", &Classroom::teacher_id), + make_column("student_id", &Classroom::student_id), + make_column("room_name", &Classroom::room_name), + foreign_key(&Classroom::teacher_id).references(&Teacher::id), + foreign_key(&Classroom::student_id).references(&Student::id))); + // Sync schema (create tables) + storage.sync_schema(); + + // Create and insert a Teacher record + Teacher teacher{0, "Mr. Smith", 45, "Mathematics", 55000.00}; + int teacher_id = storage.insert(teacher); + + // Create and insert a Student record + Student student = {0, "Alice", 20, "High School"}; + int student_id = storage.insert(student); + + // try + // { + + // Create and insert a Classroom record with foreign keys + Classroom classroom = {0, teacher_id, student_id, "Room A"}; + storage.insert(classroom); + // } + // catch (std::exception& ex) + // { + // auto s = ex.what(); + // } + + // Fetch and display inserted Classroom with foreign key references + auto classrooms = storage.get_all(); + for(const auto& c: classrooms) { + // std::cout << "Classroom: " << c.room_name << ", Teacher ID: " << c.teacher_id + // << ", Student ID: " << c.student_id << "\n"; + } +} #endif From 9a7a7600e156403b8339112dca1c65d8fa32d634 Mon Sep 17 00:00:00 2001 From: Yevgeniy Zakharov Date: Fri, 8 Nov 2024 20:03:52 +0500 Subject: [PATCH 2/2] test simplified --- tests/constraints/foreign_key.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/tests/constraints/foreign_key.cpp b/tests/constraints/foreign_key.cpp index 416d1a0d..203f6ea1 100644 --- a/tests/constraints/foreign_key.cpp +++ b/tests/constraints/foreign_key.cpp @@ -204,23 +204,8 @@ TEST_CASE("Foreign key with inheritance") { Student student = {0, "Alice", 20, "High School"}; int student_id = storage.insert(student); - // try - // { - // Create and insert a Classroom record with foreign keys Classroom classroom = {0, teacher_id, student_id, "Room A"}; storage.insert(classroom); - // } - // catch (std::exception& ex) - // { - // auto s = ex.what(); - // } - - // Fetch and display inserted Classroom with foreign key references - auto classrooms = storage.get_all(); - for(const auto& c: classrooms) { - // std::cout << "Classroom: " << c.room_name << ", Teacher ID: " << c.teacher_id - // << ", Student ID: " << c.student_id << "\n"; - } } #endif