From 6737bf901df3bbf7c8ac71f6a9c6f57d5d0107aa Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 7 Apr 2014 22:02:08 +0200 Subject: [PATCH] Added a bunch of table tests to select And some hints regarding additional tests and how to turn them of for subselects --- include/sqlpp11/alias.h | 1 + include/sqlpp11/select.h | 21 ++++++++++++++------- include/sqlpp11/vendor/from.h | 2 +- include/sqlpp11/vendor/group_by.h | 1 + include/sqlpp11/vendor/having.h | 1 + include/sqlpp11/vendor/limit.h | 3 +++ include/sqlpp11/vendor/offset.h | 2 ++ include/sqlpp11/vendor/order_by.h | 2 ++ tests/SelectTest.cpp | 4 +--- 9 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index 0d781a7a..39f3bb52 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -44,6 +44,7 @@ namespace sqlpp }; using _name_t = typename AliasProvider::_name_t; + using _table_set = typename Expression::_table_set; Expression _expression; }; diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 2da1832b..6888db64 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -391,9 +391,6 @@ namespace sqlpp return _column_list_t::static_size() + get_dynamic_names().size(); } - template - struct can_run_t - { /* static_assert(column_list::_table_set::template is_subset_t<_from_t::_table_set>::value static_assert(detail::is_subset_of::value @@ -406,8 +403,10 @@ namespace sqlpp //static_assert(is_where_t::value, "cannot run select without having a where condition, use .where(true) to select all rows"); // FIXME: Check for missing aliases (if references are used) // FIXME: Check for missing tables, well, actually, check for missing tables at the where(), order_by(), etc. - - static constexpr bool value = true; + template + struct is_table_subset_of_from + { + static constexpr bool value = ::sqlpp::detail::is_subset_of::value; }; // Execute @@ -415,7 +414,16 @@ namespace sqlpp auto _run(Db& db) const -> result_t> { - static_assert(can_run_t::value, "Cannot execute select statement"); +#warning: need to check in add_xy method as well +#warning: need add_wxy_without_table_check +#warning: might want to add an .extra_tables() method to say which tables might also be used here, say via dynamic_from or because this is a subselect + static_assert(is_table_subset_of_from::value, "selected columns require additional tables in from()"); + static_assert(is_table_subset_of_from::value, "where() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from::value, "group_by() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from::value, "having() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from::value, "order_by() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from::value, "limit() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from::value, "offset() expression requires additional tables in from()"); static_assert(_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead"); return {db.select(*this), get_dynamic_names()}; } @@ -425,7 +433,6 @@ namespace sqlpp auto _prepare(Db& db) const -> prepared_select_t { - static_assert(can_run_t::value, "Cannot prepare select statement"); return {{}, get_dynamic_names(), db.prepare_select(*this)}; } diff --git a/include/sqlpp11/vendor/from.h b/include/sqlpp11/vendor/from.h index 0f991384..e703b2b3 100644 --- a/include/sqlpp11/vendor/from.h +++ b/include/sqlpp11/vendor/from.h @@ -51,7 +51,7 @@ namespace sqlpp static_assert(::sqlpp::detail::all_t::value, "at least one argument is not a table or join in from()"); - using _table_set = typename ::sqlpp::detail::make_joined_set; + using _table_set = ::sqlpp::detail::make_joined_set_t; from_t(Tables... tables): diff --git a/include/sqlpp11/vendor/group_by.h b/include/sqlpp11/vendor/group_by.h index a4e59bca..5491a6e8 100644 --- a/include/sqlpp11/vendor/group_by.h +++ b/include/sqlpp11/vendor/group_by.h @@ -78,6 +78,7 @@ namespace sqlpp struct no_group_by_t { using _is_noop = std::true_type; + using _table_set = ::sqlpp::detail::type_set<>; }; // Interpreters diff --git a/include/sqlpp11/vendor/having.h b/include/sqlpp11/vendor/having.h index 33a89044..7572b205 100644 --- a/include/sqlpp11/vendor/having.h +++ b/include/sqlpp11/vendor/having.h @@ -75,6 +75,7 @@ namespace sqlpp struct no_having_t { using _is_noop = std::true_type; + using _table_set = ::sqlpp::detail::type_set<>; }; // Interpreters diff --git a/include/sqlpp11/vendor/limit.h b/include/sqlpp11/vendor/limit.h index 98ff44b8..4e91e312 100644 --- a/include/sqlpp11/vendor/limit.h +++ b/include/sqlpp11/vendor/limit.h @@ -29,6 +29,7 @@ #include #include +#include namespace sqlpp { @@ -40,6 +41,7 @@ namespace sqlpp { using _is_limit = std::true_type; static_assert(is_integral_t::value, "limit requires an integral value or integral parameter"); + using _table_set = ::sqlpp::detail::type_set<>; limit_t(Limit value): _value(value) @@ -93,6 +95,7 @@ namespace sqlpp struct no_limit_t { using _is_noop = std::true_type; + using _table_set = ::sqlpp::detail::type_set<>; }; // Interpreters diff --git a/include/sqlpp11/vendor/offset.h b/include/sqlpp11/vendor/offset.h index 80a9ac36..35f43bfc 100644 --- a/include/sqlpp11/vendor/offset.h +++ b/include/sqlpp11/vendor/offset.h @@ -29,6 +29,7 @@ #include #include +#include namespace sqlpp { @@ -93,6 +94,7 @@ namespace sqlpp struct no_offset_t { using _is_noop = std::true_type; + using _table_set = ::sqlpp::detail::type_set<>; }; // Interpreters diff --git a/include/sqlpp11/vendor/order_by.h b/include/sqlpp11/vendor/order_by.h index 093ba2e3..4a424eaa 100644 --- a/include/sqlpp11/vendor/order_by.h +++ b/include/sqlpp11/vendor/order_by.h @@ -33,6 +33,7 @@ #include #include #include +#include namespace sqlpp { @@ -75,6 +76,7 @@ namespace sqlpp struct no_order_by_t { using _is_noop = std::true_type; + using _table_set = ::sqlpp::detail::type_set<>; }; // Interpreters diff --git a/tests/SelectTest.cpp b/tests/SelectTest.cpp index bc636b2e..423ff488 100644 --- a/tests/SelectTest.cpp +++ b/tests/SelectTest.cpp @@ -61,13 +61,11 @@ int main() const std::string b = row.tabBar.beta; } -#warning this should fail because f is not in from() - for (const auto& row : db(select(f.omega, all_of(t).as(t), t.gamma).from(t).where(true))) + for (const auto& row : db(select(all_of(t).as(t), t.gamma).from(t).where(t.alpha > 7))) { int64_t a = row.tabBar.alpha; const std::string b = row.tabBar.beta; const bool g = row.gamma; - const float o = row.omega; } return 0;