diff --git a/demo/main.cpp b/demo/main.cpp index dad6b7d..36b7a88 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -1,9 +1,9 @@ #include "matador/sql/connection.hpp" -#include "matador/sql/query_macro.hpp" #include "matador/query/criteria.hpp" #include "matador/query/query.hpp" #include "matador/query/table_column.hpp" +#include "matador/query/meta_table_macro.hpp" #include "matador/object/object_ptr.hpp" #include "matador/object/repository.hpp" @@ -140,20 +140,20 @@ struct matador::utils::data_type_traits { } }; -QUERY_HELPER(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished) +META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished) -QUERY_HELPER(books, BOOK, id, author_id, title, published_in) +META_TABLE(books, BOOK, id, author_id, title, published_in) -QUERY_HELPER(job, JOB, id, payload, type, description, state, mode) +META_TABLE(job, JOB, id, payload, type, description, state, mode) -QUERY_HELPER(payload, PAYLOAD, id ) +META_TABLE(payload, PAYLOAD, id ) -QUERY_HELPER(temporary_table, TEMPORARY_TABLE, id ); +META_TABLE(temporary_table, TEMPORARY_TABLE, id ); -QUERY_HELPER(customer, CUSTOMER, id, name, email, address) -QUERY_HELPER(product, PRODUCT, id, title, description, price, category) -QUERY_HELPER(category, CATEGORY, id, title, description) -QUERY_HELPER(cart, CART, id, items, owner) +META_TABLE(customer, CUSTOMER, id, name, email, address) +META_TABLE(product, PRODUCT, id, title, description, price, category) +META_TABLE(category, CATEGORY, id, title, description) +META_TABLE(cart, CART, id, items, owner) int main() { using namespace matador::sql; diff --git a/include/matador/query/meta_table_macro.hpp b/include/matador/query/meta_table_macro.hpp new file mode 100644 index 0000000..f828a61 --- /dev/null +++ b/include/matador/query/meta_table_macro.hpp @@ -0,0 +1,27 @@ +#ifndef MATADOR_META_TABLE_MACRO_HPP +#define MATADOR_META_TABLE_MACRO_HPP + +#include "matador/utils/macro_map.hpp" + +#include "matador/query/table_column.hpp" +#include "matador/query/table.hpp" + +#define FIELD(FIELD_NAME) const matador::query::table_column& FIELD_NAME; +#define INIT_FIELD(FIELD_NAME) , FIELD_NAME(*column_by_name(*this, #FIELD_NAME)) +#define FIELD_STRING(FIELD_NAME) #FIELD_NAME, + +#define META_TABLE(TABLE_NAME, VARIABLE_NAME, ...) \ +namespace matador::query::meta { \ +namespace internal { \ +class TABLE_NAME##_table : public table { \ +public: \ +TABLE_NAME##_table() \ +: table(#TABLE_NAME, {MAP(FIELD_STRING, __VA_ARGS__)}) \ +MAP(INIT_FIELD, __VA_ARGS__) \ +{} \ +MAP(FIELD, __VA_ARGS__) \ +}; } \ +static const internal:: TABLE_NAME##_table VARIABLE_NAME; \ +} + +#endif //MATADOR_META_TABLE_MACRO_HPP \ No newline at end of file diff --git a/include/matador/sql/query_macro.hpp b/include/matador/sql/query_macro.hpp deleted file mode 100644 index 8396dc2..0000000 --- a/include/matador/sql/query_macro.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef QUERY_QUERY_HELPER_HPP -#define QUERY_QUERY_HELPER_HPP - -#include "matador/utils/macro_map.hpp" - -#include "matador/query/table.hpp" -#include "matador/query/table_column.hpp" - -#include -#include - -#define FIELD(x) const matador::query::table_column x = create_column(*this, #x); - -#define QUERY_HELPER(C, V, ...) \ -namespace matador::query::meta { \ -namespace internal { \ -class C##_table : public table { \ -public: \ -C##_table() : table(#C) {} \ -MAP(FIELD, __VA_ARGS__) \ -}; } \ -static const internal:: C##_table V; \ -} - -#endif //QUERY_QUERY_HELPER_HPP diff --git a/source/orm/CMakeLists.txt b/source/orm/CMakeLists.txt index d7c7e45..13d414e 100644 --- a/source/orm/CMakeLists.txt +++ b/source/orm/CMakeLists.txt @@ -49,6 +49,7 @@ add_library(matador-orm STATIC ../../include/matador/query/internal/query_parts.hpp ../../include/matador/query/join_data.hpp ../../include/matador/query/key_value_generator.hpp + ../../include/matador/query/meta_table_macro.hpp ../../include/matador/query/query.hpp ../../include/matador/query/query_compiler.hpp ../../include/matador/query/query_data.hpp @@ -79,7 +80,6 @@ add_library(matador-orm STATIC ../../include/matador/sql/internal/query_result_impl.hpp ../../include/matador/sql/internal/query_result_pk_resolver.hpp ../../include/matador/sql/query_context.hpp - ../../include/matador/sql/query_macro.hpp ../../include/matador/sql/query_result.hpp ../../include/matador/sql/record.hpp ../../include/matador/sql/sql_functions.hpp @@ -160,6 +160,7 @@ add_library(matador-orm STATIC sql/record.cpp sql/statement.cpp sql/statement_cache.cpp + ../../include/matador/query/meta_table_macro.hpp ) target_include_directories(matador-orm diff --git a/test/orm/CMakeLists.txt b/test/orm/CMakeLists.txt index bea6a93..9d6090f 100644 --- a/test/orm/CMakeLists.txt +++ b/test/orm/CMakeLists.txt @@ -19,7 +19,7 @@ add_executable(OrmTests query/QueryFixture.cpp query/QueryFixture.hpp query/QueryTest.cpp - sql/ColumnGeneratorTest.cpp + query/ColumnGeneratorTest.cpp sql/ColumnTest.cpp sql/ConnectionPoolFixture.hpp sql/ConnectionPoolTest.cpp diff --git a/test/orm/orm/SessionQueryBuilderTest.cpp b/test/orm/orm/SessionQueryBuilderTest.cpp index 1e0d670..4785a3e 100644 --- a/test/orm/orm/SessionQueryBuilderTest.cpp +++ b/test/orm/orm/SessionQueryBuilderTest.cpp @@ -35,39 +35,6 @@ using namespace matador::utils; using namespace matador::sql; using namespace matador::test; -// #define FIELD(FIELD_NAME) const matador::query::column& FIELD_NAME = create_column(*this, #FIELD_NAME); -// -// #define META_TABLE(TABLE_NAME, VARIABLE_NAME, ...) \ -// namespace matador::query::meta { \ -// namespace internal { \ -// class TABLE_NAME##_table : public table { \ -// public: \ -// TABLE_NAME##_table() : table(#TABLE_NAME) {} \ -// MAP(FIELD, __VA_ARGS__) \ -// }; } \ -// static const internal:: TABLE_NAME##_table VARIABLE_NAME; \ -// } -// -// META_TABLE( books, BOOK, id, author_id, title, published_in ) - -class book_table : public table { -public: - book_table() - : table("books", {"id", "title", "author_id"}) - , id(*column_by_name(*this, "id")) - , title(*column_by_name(*this, "id")) - , author_id(*column_by_name(*this, "author_id")) { - } - - const table_column& id; - const table_column& title; - const table_column& author_id; -}; - -book_table BOOK; - -using namespace matador::test; - TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") { using namespace matador::test; backend_provider::instance().register_backend("noop", std::make_unique()); diff --git a/test/orm/sql/ColumnGeneratorTest.cpp b/test/orm/query/ColumnGeneratorTest.cpp similarity index 100% rename from test/orm/sql/ColumnGeneratorTest.cpp rename to test/orm/query/ColumnGeneratorTest.cpp diff --git a/test/orm/sql/StatementCacheTest.cpp b/test/orm/sql/StatementCacheTest.cpp index 901f8e0..c8e8bd2 100644 --- a/test/orm/sql/StatementCacheTest.cpp +++ b/test/orm/sql/StatementCacheTest.cpp @@ -1,8 +1,6 @@ #include #include -#include - #include "matador/sql/connection_pool.hpp" #include "matador/sql/error_code.hpp" #include "matador/sql/statement_cache.hpp" @@ -19,7 +17,6 @@ using namespace matador::test; using namespace matador::sql; -using namespace matador::query; using namespace matador::utils; class MetricsObserver {