69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
#ifndef QUERY_STUDENT_HPP
|
|
#define QUERY_STUDENT_HPP
|
|
|
|
#include "matador/utils/access.hpp"
|
|
#include "matador/utils/foreign_attributes.hpp"
|
|
|
|
#include "matador/sql/entity.hpp"
|
|
#include "matador/sql/has_many_to_many_relation.hpp"
|
|
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace matador::test {
|
|
|
|
class course;
|
|
|
|
struct student
|
|
{
|
|
unsigned long id{};
|
|
std::string name;
|
|
std::vector<matador::sql::entity<course>> courses;
|
|
|
|
student() = default;
|
|
explicit student(unsigned long id, std::string name)
|
|
: id(id)
|
|
, name(std::move(name)) {}
|
|
|
|
template < class Operator >
|
|
void process(Operator &op)
|
|
{
|
|
namespace field = matador::utils::access;
|
|
field::primary_key(op, "id", id);
|
|
field::attribute(op, "name", name, 255);
|
|
field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::fetch_type::LAZY);
|
|
}
|
|
|
|
};
|
|
|
|
struct course
|
|
{
|
|
unsigned long id{};
|
|
std::string title;
|
|
std::vector<matador::sql::entity<student>> students;
|
|
|
|
course() = default;
|
|
explicit course(unsigned long id, std::string title)
|
|
: id(id)
|
|
, title(std::move(title)) {}
|
|
|
|
template < class Operator >
|
|
void process(Operator &op)
|
|
{
|
|
namespace field = matador::utils::access;
|
|
field::primary_key(op, "id", id);
|
|
field::attribute(op, "title", title, 255);
|
|
field::has_many_to_many(op, "student_courses", students, utils::fetch_type::EAGER);
|
|
}
|
|
};
|
|
|
|
class student_course : public sql::has_many_to_many_relation<student, course>
|
|
{
|
|
public:
|
|
student_course()
|
|
: has_many_to_many_relation("student_id", "course_id") {}
|
|
};
|
|
|
|
}
|
|
#endif //QUERY_STUDENT_HPP
|