From e235c111a15ea7a7c51a6fac25a1c9604884af4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Fri, 21 Nov 2025 10:12:54 +0100 Subject: [PATCH] moved query macro generated class to namespace matador::query::meta --- demo/main.cpp | 39 ++++++++++++++++------------- include/matador/query/table.hpp | 2 ++ include/matador/sql/query_macro.hpp | 10 ++++---- source/orm/query/table.cpp | 4 +++ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/demo/main.cpp b/demo/main.cpp index cd66de9..7adeb42 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -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 { } }; -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 diff --git a/include/matador/query/table.hpp b/include/matador/query/table.hpp index aa02d72..9d7fde5 100644 --- a/include/matador/query/table.hpp +++ b/include/matador/query/table.hpp @@ -30,6 +30,8 @@ public: [[nodiscard]] const std::string& name() const; [[nodiscard]] const std::string& alias() const; [[nodiscard]] const std::vector& columns() const; + + operator const std::vector&() const; private: friend class column; diff --git a/include/matador/sql/query_macro.hpp b/include/matador/sql/query_macro.hpp index 940c5bb..437c906 100644 --- a/include/matador/sql/query_macro.hpp +++ b/include/matador/sql/query_macro.hpp @@ -9,16 +9,16 @@ #include #include -#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 diff --git a/source/orm/query/table.cpp b/source/orm/query/table.cpp index 2ed5c54..d01dc08 100644 --- a/source/orm/query/table.cpp +++ b/source/orm/query/table.cpp @@ -54,6 +54,10 @@ const std::vector& table::columns() const { return columns_; } +table::operator const std::vector&() const { + return columns_; +} + table operator ""_tab(const char *name, size_t len) { return {{name, len}}; }