moved query macro generated class to namespace matador::query::meta

This commit is contained in:
Sascha Kühl 2025-11-21 10:12:54 +01:00
parent a146dce321
commit e235c111a1
4 changed files with 32 additions and 23 deletions

View File

@ -3,6 +3,7 @@
#include "matador/sql/query_macro.hpp"
#include "matador/query/criteria.hpp"
#include "matador/query/query.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/object/repository.hpp"
@ -143,20 +144,21 @@ struct matador::utils::data_type_traits<job::job_mode, void> {
}
};
QUERY_HELPER( authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
QUERY_HELPER( authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
QUERY_HELPER( books, id, author_id, title, published_in )
QUERY_HELPER( books, BOOK, id, author_id, title, published_in )
QUERY_HELPER( job, id, payload, type, description, state, mode )
QUERY_HELPER( job, JOB, id, payload, type, description, state, mode )
QUERY_HELPER( payload, id )
QUERY_HELPER( payload, PAYLOAD, id )
QUERY_HELPER( temporary_table, id );
QUERY_HELPER( temporary_table, TEMPORARY_TABLE, id );
int main() {
using namespace matador::sql;
using namespace matador::object;
using namespace matador::utils;
using namespace matador::query::meta;
const std::string env_var{"MATADOR_BACKENDS_PATH"};
@ -233,19 +235,20 @@ int main() {
// .values( {3, "Misery", mc.id, 1984} )
// .execute();
//
// auto select_books_sql = c.query( s )
// .select( qh::books.columns, {qh::authors.last_name} )
// .from( qh::books )
// .join_left( qh::authors )
// .on( qh::books.author_id == qh::authors.id )
// .where( qh::books.published_in < 2008 && qh::authors.last_name == "King" )
// .group_by( qh::books.published_in )
// .order_by( qh::books.title ).asc()
// .limit( 5 )
// .offset( 2 )
// .fetch_all();
//
// for (const auto& r: select_books_sql) { std::cout << "R: " << r.at( qh::books.title ) << ", " << r.at( qh::authors.last_name ) << "\n"; }
auto select_books_sql = matador::query::query::select( BOOK, {AUTHOR.last_name} )
.from( BOOK )
.join_left( AUTHOR )
.on( BOOK.author_id == AUTHOR.id )
.where( BOOK.published_in < 2008 && AUTHOR.last_name == "King" )
.group_by( BOOK.published_in )
.order_by( BOOK.title ).asc()
.limit( 5 )
.offset( 2 )
.fetch_all(c);
for (const auto& r: *select_books_sql) {
std::cout << "R: " << r.at( BOOK.title ) << ", " << r.at( AUTHOR.last_name ) << "\n";
}
// // SELECT book.title, book.id, book.author_id, book.published_in, author.name
// // FROM book
// // INNER JOIN author ON book.author_id = author.id

View File

@ -30,6 +30,8 @@ public:
[[nodiscard]] const std::string& name() const;
[[nodiscard]] const std::string& alias() const;
[[nodiscard]] const std::vector<column>& columns() const;
operator const std::vector<column>&() const;
private:
friend class column;

View File

@ -9,16 +9,16 @@
#include <string>
#include <ostream>
#define FIELD(x) const query::column x{*this, #x, ""};
#define FIELD(x) const column x{*this, #x, ""};
#define QUERY_HELPER(C, ...) \
namespace matador::qh { \
#define QUERY_HELPER(C, V, ...) \
namespace matador::query::meta { \
namespace internal { \
struct C##_query : query::table { \
struct C##_query : table { \
C##_query() : table(#C) {} \
MAP(FIELD, __VA_ARGS__) \
}; } \
static const internal:: C##_query C; \
static const internal:: C##_query V; \
}
#endif //QUERY_QUERY_HELPER_HPP

View File

@ -54,6 +54,10 @@ const std::vector<column>& table::columns() const {
return columns_;
}
table::operator const std::vector<column>&() const {
return columns_;
}
table operator ""_tab(const char *name, size_t len) {
return {{name, len}};
}