added query helper macro and join query parts
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#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
|
||||
+17
-4
@@ -6,6 +6,9 @@
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "query_helper.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
struct author
|
||||
@@ -46,26 +49,36 @@ struct book
|
||||
}
|
||||
};
|
||||
|
||||
QUERY_HELPER(author, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||
QUERY_HELPER(book, id, book_author, title, published_in)
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace matador::sql;
|
||||
|
||||
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
||||
|
||||
std::string dns{"sqlite://demo.db"};
|
||||
// std::string dns{"memory://test"};
|
||||
auto s = std::make_shared<schema>();
|
||||
s->attach<author>("authors");
|
||||
s->attach<book>("books");
|
||||
|
||||
connection c(dns, s);
|
||||
|
||||
auto books = c.query()
|
||||
.select<book>({"author.name"})
|
||||
.from("book")
|
||||
.join_left("author")
|
||||
.select<book>({qh::book.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()
|
||||
.offset(2)
|
||||
.limit(5)
|
||||
.fetch_all<book>();
|
||||
.str();
|
||||
// .fetch_all<book>();
|
||||
|
||||
std::cout << "SQL: " << books << "\n";
|
||||
|
||||
// SELECT book.title, book.id, book.author_id, book.published_in, author.name
|
||||
// FROM book
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef QUERY_QUERY_HELPER_HPP
|
||||
#define QUERY_QUERY_HELPER_HPP
|
||||
|
||||
#include "macro_map.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 QUERY_HELPER(C, ...) \
|
||||
namespace qh { \
|
||||
namespace internal { \
|
||||
struct C##_query { \
|
||||
MAP(FIELD, __VA_ARGS__) \
|
||||
}; } \
|
||||
static const internal:: C##_query C; \
|
||||
}
|
||||
|
||||
#endif //QUERY_QUERY_HELPER_HPP
|
||||
Reference in New Issue
Block a user