diff --git a/include/sqlpp11/remove.h b/include/sqlpp11/remove.h index 4b787352..3b9677c2 100644 --- a/include/sqlpp11/remove.h +++ b/include/sqlpp11/remove.h @@ -150,6 +150,7 @@ namespace sqlpp std::size_t run(Db& db) const { static_assert(_get_static_no_of_parameters() == 0, "cannot run remove directly with parameters, use prepare instead"); + static_assert(is_where_t::value, "cannot run update without having a where condition, use .where(true) to remove all rows"); return db.remove(*this); } diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index d41bd9fd..ec83b748 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -620,6 +620,7 @@ namespace sqlpp { static_assert(not vendor::is_noop::value, "cannot run select without having selected anything"); static_assert(is_from_t::value, "cannot run select without a from()"); + static_assert(is_where_t::value, "cannot run select without having a where condition, use .where(true) to select all rows"); static_assert(_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead"); // 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. diff --git a/include/sqlpp11/update.h b/include/sqlpp11/update.h index 63a55599..73dfabd1 100644 --- a/include/sqlpp11/update.h +++ b/include/sqlpp11/update.h @@ -149,6 +149,7 @@ namespace sqlpp std::size_t run(Db& db) const { static_assert(not vendor::is_noop::value, "calling set() required before running update"); + static_assert(is_where_t::value, "cannot run update without having a where condition, use .where(true) to update all rows"); static_assert(_get_static_no_of_parameters() == 0, "cannot run update directly with parameters, use prepare instead"); return db.update(*this); } diff --git a/include/sqlpp11/vendor/where.h b/include/sqlpp11/vendor/where.h index f9251f2b..b354918b 100644 --- a/include/sqlpp11/vendor/where.h +++ b/include/sqlpp11/vendor/where.h @@ -82,6 +82,29 @@ namespace sqlpp } }; + template<> + struct where_t + { + using _is_where = std::true_type; + using _is_dynamic = std::false_type; + using _parameter_tuple_t = std::tuple<>; + + std::tuple _condition; + }; + + template + struct interpreter_t> + { + using T = where_t; + + static Context& _(const T& t, Context& context) + { + if (not std::get<0>(t._condition)) + context << " WHERE NULL"; + return context; + } + }; + } } diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index 7a90267b..9aa6b521 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -41,6 +41,7 @@ SQLPP_ALIAS_PROVIDER(kaesekuchen); int main() { TabSample t; + TabFoo f; interpret(t.alpha, printer).flush(); interpret(-t.alpha, printer).flush(); @@ -68,7 +69,7 @@ int main() interpret(t.alpha == parameter(t.alpha) and (t.beta + "gimmick").like(parameter(t.beta)), printer).flush(); interpret(insert_into(t), printer).flush(); - interpret(insert_into(t).default_values(), printer).flush(); + interpret(insert_into(f).default_values(), printer).flush(); interpret(insert_into(t).set(t.gamma = true), printer).flush(); interpret(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).flush(); @@ -119,5 +120,8 @@ int main() interpret(avg(sqlpp::distinct, t.alpha - 7), printer).flush(); interpret(sum(sqlpp::distinct, t.alpha + 7), printer).flush(); + interpret(select(all_of(t)).from(t).where(true), printer).flush(); + interpret(select(all_of(t)).from(t).where(false), printer).flush(); + return 0; }