From 514ed4139cc229db4664f2e15fad20df0bcdbe66 Mon Sep 17 00:00:00 2001 From: rbock Date: Fri, 31 Oct 2014 08:28:51 +0100 Subject: [PATCH] First custom query sent to MockDb --- include/sqlpp11/custom_query.h | 50 ++++++++++++++++++++++------ include/sqlpp11/select_column_list.h | 10 +++--- include/sqlpp11/statement.h | 6 ++-- tests/CustomQueryTest.cpp | 5 +-- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/include/sqlpp11/custom_query.h b/include/sqlpp11/custom_query.h index 4acf12d1..e2492eee 100644 --- a/include/sqlpp11/custom_query.h +++ b/include/sqlpp11/custom_query.h @@ -28,22 +28,48 @@ #define SQLPP_CUSTOM_QUERY_H #include +#include +#include namespace sqlpp { template - struct custom_query_t/*: - public FirstPart::_result_type_provider::template _result_methods_t*/ - { - custom_query_t(Parts... parts): - _parts(parts...) - {} + struct custom_query_t; - custom_query_t(const custom_query_t&) = default; - custom_query_t(custom_query_t&&) = default; - custom_query_t& operator=(const custom_query_t&) = default; - custom_query_t& operator=(custom_query_t&&) = default; - ~custom_query_t() = default; + namespace detail + { + template + struct custom_parts_t + { + using _custom_query_t = custom_query_t; + using _result_type_provider = detail::get_first_if; + using _result_methods_t = typename _result_type_provider::template _result_methods_t<_result_type_provider>; + }; + } + + template + struct custom_query_t: + private detail::custom_parts_t::_result_methods_t + { + using _methods_t = typename detail::custom_parts_t::_result_methods_t; + + static void _check_consistency() {}; + + template + auto _run(Db& db) const -> decltype(_methods_t::_run(db, *this)) + { + return _methods_t::_run(db, *this); + } + + custom_query_t(Parts... parts): + _parts(parts...) + {} + + custom_query_t(const custom_query_t&) = default; + custom_query_t(custom_query_t&&) = default; + custom_query_t& operator=(const custom_query_t&) = default; + custom_query_t& operator=(custom_query_t&&) = default; + ~custom_query_t() = default; std::tuple _parts; }; @@ -64,6 +90,7 @@ namespace sqlpp auto custom_query(Parts... parts) -> custom_query_t { + static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument"); return custom_query_t(parts...); } @@ -71,6 +98,7 @@ namespace sqlpp auto dynamic_custom_query(const Database&, Parts...) -> custom_query_t { + static_assert(sizeof...(Parts) > 0, "custom query requires at least one query argument"); static_assert(std::is_base_of::value, "Invalid database parameter"); return { }; } diff --git a/include/sqlpp11/select_column_list.h b/include/sqlpp11/select_column_list.h index abfe6860..8af3d38a 100644 --- a/include/sqlpp11/select_column_list.h +++ b/include/sqlpp11/select_column_list.h @@ -293,14 +293,14 @@ namespace sqlpp } // Execute - template - auto _run(Db& db) const - -> result_t_get_statement())), _result_row_t> + template + auto _run(Db& db, const S& s) const + -> result_t> { - _statement_t::_check_consistency(); + S::_check_consistency(); static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead"); - return {db.select(_get_statement()), get_dynamic_names()}; + return {db.select(s), get_dynamic_names()}; } // Prepare diff --git a/include/sqlpp11/statement.h b/include/sqlpp11/statement.h index 4092638d..e0d48437 100644 --- a/include/sqlpp11/statement.h +++ b/include/sqlpp11/statement.h @@ -132,17 +132,19 @@ namespace sqlpp public Policies::template _methods_t>... { using _policies_t = typename detail::statement_policies_t; + using _result_type_provider = typename _policies_t::_result_type_provider; + template + using _result_methods_t = typename _result_type_provider::template _result_methods_t; using _traits = make_traits, tag::is_select, tag_if::value>, tag_if::value>, + tag_if::value>::value>, tag::requires_braces>; using _recursive_traits = typename _policies_t::_recursive_traits; using _used_outer_tables = typename _policies_t::_all_provided_outer_tables; - using _result_type_provider = typename _policies_t::_result_type_provider; - using _name_t = typename _result_type_provider::_name_t; // Constructors diff --git a/tests/CustomQueryTest.cpp b/tests/CustomQueryTest.cpp index 64654367..67347e3d 100644 --- a/tests/CustomQueryTest.cpp +++ b/tests/CustomQueryTest.cpp @@ -37,8 +37,9 @@ int main() test::TabFoo f; test::TabBar t; - auto c = custom_query(select(all_of(t)).from(t), insert_into(t)); - std::cerr << serialize(c, printer).str() << std::endl; + auto c = custom_query(select(all_of(t)).from(t)); + //std::cerr << serialize(c, printer).str() << std::endl; + db(c); return 0; }