query parts progress

This commit is contained in:
2024-02-22 22:25:29 +01:00
parent 16d7fd7e76
commit 0822332669
11 changed files with 51 additions and 58 deletions
-42
View File
@@ -1,42 +0,0 @@
#ifndef QUERY_MACRO_MAP_HPP
#define QUERY_MACRO_MAP_HPP
#define EVAL0(...) __VA_ARGS__
#define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
#define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
#define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
#define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
#define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
#define MAP_END(...)
#define MAP_OUT
#define MAP_COMMA ,
#define MAP_GET_END2() 0, MAP_END
#define MAP_GET_END1(...) MAP_GET_END2
#define MAP_GET_END(...) MAP_GET_END1
#define MAP_NEXT0(test, next, ...) next MAP_OUT
#define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
#define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
#define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
#define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
#define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
#define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
#define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
/**
* Applies the function macro `f` to each of the remaining parameters.
*/
#define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
/**
* Applies the function macro `f` to each of the remaining parameters and
* inserts commas between the results.
*/
#define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
#endif //QUERY_MACRO_MAP_HPP
+9 -8
View File
@@ -44,17 +44,18 @@ struct book
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "title", title, 511);
field::has_one(op, "authors", book_author, matador::utils::default_foreign_attributes);
field::has_one(op, "author_id", book_author, matador::utils::default_foreign_attributes);
field::attribute(op, "published_in", published_in);
}
};
QUERY_HELPER(authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
QUERY_HELPER(books, id, book_author, title, published_in)
QUERY_HELPER(books, id, author_id, title, published_in)
int main()
{
using namespace matador::sql;
using namespace matador;
const std::string env_var{"MATADOR_BACKENDS_PATH"};
@@ -67,12 +68,12 @@ int main()
connection c(dns, s);
auto books = c.query()
.select<book>({matador::qh::books.id.name})
.from({"book"})
.join_left({"authors"})
.on("book.author_id"_col == "author.id"_col)
.where("book.published_in"_col < 2008 && "author.name"_col == "Michael Crichton")
.order_by("book.title").asc()
.select<book>({qh::authors.first_name})
.from(qh::books)
.join_left(qh::authors)
.on(qh::books.author_id == qh::authors.id)
.where(qh::books.published_in < 2008 && qh::authors.first_name == "Michael Crichton")
.order_by(qh::books.title).asc()
.offset(2)
.limit(5)
.str();
+7 -35
View File
@@ -1,49 +1,21 @@
#ifndef QUERY_QUERY_HELPER_HPP
#define QUERY_QUERY_HELPER_HPP
#include "macro_map.hpp"
#include "matador/utils/macro_map.hpp"
#include "matador/sql/table.hpp"
#include "matador/sql/column.hpp"
#include <string>
#include <ostream>
template<typename QueryClass>
struct basic_query;
struct table {
template<typename QueryClass>
table(const basic_query<QueryClass> &qc);
const std::string name;
};
struct field {
const std::string name;
friend std::ostream& operator<<(std::ostream &out, const field &f) {
out << f.name;
return out;
}
};
template<typename QueryClass>
struct basic_query {
const table& operator()() const {
return table_;
}
const table table_;
};
template<typename QueryClass>
table::table(const basic_query<QueryClass>&)
: name(QueryClass::internal_table_name_) {
}
#define FIELD(x) const field x{#x};
#define FIELD(x) const sql::column x{this->name, #x, ""};
#define QUERY_HELPER(C, ...) \
namespace matador::qh { \
namespace internal { \
struct C##_query { \
struct C##_query : sql::table { \
C##_query() : table(#C) {} \
MAP(FIELD, __VA_ARGS__) \
}; } \
static const internal:: C##_query C; \