From 602f33726f13b59514730cce60673407714451bf Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 15 Jan 2014 07:22:54 +0100 Subject: [PATCH] interpreter added for update() --- include/sqlpp11/assignment_list.h | 22 ++++++++++++-- include/sqlpp11/insert.h | 12 ++++---- include/sqlpp11/insert_list.h | 12 ++++---- include/sqlpp11/remove.h | 19 +----------- include/sqlpp11/select.h | 15 +--------- include/sqlpp11/update.h | 49 ++++++++++++++----------------- tests/InterpretTest.cpp | 4 +++ 7 files changed, 58 insertions(+), 75 deletions(-) diff --git a/include/sqlpp11/assignment_list.h b/include/sqlpp11/assignment_list.h index e55e8ab5..f301dd0c 100644 --- a/include/sqlpp11/assignment_list.h +++ b/include/sqlpp11/assignment_list.h @@ -34,7 +34,7 @@ namespace sqlpp { - template class ProhibitPredicate, typename... Assignments> + template struct assignment_list_t { using _is_assignment_list = std::true_type; @@ -52,14 +52,14 @@ namespace sqlpp static_assert(_assignment_set::size::value == sizeof...(Assignments), "at least one argument is not an assignment in set()"); // check for prohibited assignments - using _prohibited_assignment_set = typename detail::make_set_if::type; + using _prohibited_assignment_set = typename detail::make_set_if::type; static_assert(_prohibited_assignment_set::size::value == 0, "at least one assignment is prohibited by its column definition in set()"); template void add(Assignment&& assignment) { static_assert(is_assignment_t::type>::value, "set() arguments require to be assigments"); - static_assert(not ProhibitPredicate::type::column_type>::value, "set() argument must not be updated"); + static_assert(not must_not_update_t::type::column_type>::value, "set() argument must not be updated"); _dynamic_assignments.emplace_back(std::forward(assignment)); } @@ -81,6 +81,22 @@ namespace sqlpp typename detail::serializable_list _dynamic_assignments; }; + template + struct interpreter_t> + { + using T = assignment_list_t; + + static Context& _(const T& t, Context& context) + { + context << " SET "; + interpret_tuple(t._assignments, ",", context); + if (sizeof...(Assignments) and not t._dynamic_assignments.empty()) + context << ','; + interpret_serializable_list(t._dynamic_assignments, ',', context); + return context; + } + }; + } #endif diff --git a/include/sqlpp11/insert.h b/include/sqlpp11/insert.h index 45dca769..b3ac108b 100644 --- a/include/sqlpp11/insert.h +++ b/include/sqlpp11/insert.h @@ -35,8 +35,6 @@ #include #include -#include - namespace sqlpp { @@ -77,24 +75,24 @@ namespace sqlpp template auto set(Assignment&&... assignment) - -> set_insert_list_t::type...>> + -> set_insert_list_t::type...>> { static_assert(std::is_same::value, "cannot call set() after set() or default_values()"); // FIXME: Need to check if all required columns are set return { _table, - insert_list_t::type...>{std::forward(assignment)...}, + insert_list_t::type...>{std::forward(assignment)...}, }; } template auto dynamic_set(Assignment&&... assignment) - -> set_insert_list_t::type...>> + -> set_insert_list_t::type...>> { static_assert(std::is_same::value, "cannot call set() after set() or default_values()"); return { _table, - insert_list_t::type...>{std::forward(assignment)...}, + insert_list_t::type...>{std::forward(assignment)...}, }; } @@ -130,7 +128,7 @@ namespace sqlpp } template - auto prepare(Db& db) + auto prepare(Db& db) const -> prepared_insert_t::type, insert_t> { constexpr bool calledSet = not is_noop::value; diff --git a/include/sqlpp11/insert_list.h b/include/sqlpp11/insert_list.h index 2078ec6b..082a7d98 100644 --- a/include/sqlpp11/insert_list.h +++ b/include/sqlpp11/insert_list.h @@ -71,7 +71,7 @@ namespace sqlpp }; - template class ProhibitPredicate, typename... Assignments> + template struct insert_list_t { using _is_insert_list = std::true_type; @@ -89,7 +89,7 @@ namespace sqlpp static_assert(_assignment_set::size::value == sizeof...(Assignments), "at least one argument is not an assignment in set()"); // check for prohibited assignments - using _prohibited_assignment_set = typename detail::make_set_if::type; + using _prohibited_assignment_set = typename detail::make_set_if::type; static_assert(_prohibited_assignment_set::size::value == 0, "at least one assignment is prohibited by its column definition in set()"); insert_list_t(Assignments... assignment): @@ -107,7 +107,7 @@ namespace sqlpp void add(Assignment&& assignment) { static_assert(is_assignment_t::type>::value, "set() arguments require to be assigments"); - static_assert(not ProhibitPredicate::type>::value, "set() argument must not be used in insert"); + static_assert(not must_not_insert_t::type>::value, "set() argument must not be used in insert"); _dynamic_columns.emplace_back(insert_column_t{std::forward(assignment._lhs)}); _dynamic_values.emplace_back(std::forward(assignment._rhs)); } @@ -119,10 +119,10 @@ namespace sqlpp typename detail::serializable_list _dynamic_values; }; - template class ProhibitPredicate, typename... Assignments> - struct interpreter_t> + template + struct interpreter_t> { - using T = insert_list_t; + using T = insert_list_t; static Context& _(const T& t, Context& context) { diff --git a/include/sqlpp11/remove.h b/include/sqlpp11/remove.h index 91b3ce2f..d84b0d31 100644 --- a/include/sqlpp11/remove.h +++ b/include/sqlpp11/remove.h @@ -145,13 +145,6 @@ namespace sqlpp return *this; } - template - remove_t& serialize(std::ostream& os, Db& db) - { - static_cast(this)->serialize(os, db); - return *this; - } - static constexpr size_t _get_static_no_of_parameters() { return _parameter_list_t::size::value; @@ -170,22 +163,12 @@ namespace sqlpp } template - auto prepare(Db& db) + auto prepare(Db& db) const -> prepared_remove_t::type, remove_t> { - _set_parameter_index(0); return {{}, db.prepare_remove(*this)}; } - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_table, index); - index = set_parameter_index(_using, index); - index = set_parameter_index(_where, index); - return index; - } - - Table _table; Using _using; Where _where; diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 99d4710c..506d6229 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -584,29 +584,16 @@ namespace sqlpp // Prepare template auto prepare(Db& db) - -> prepared_select_t::type, select_t> + -> prepared_select_t::type, select_t> const { static_assert(not is_noop::value, "cannot run select without having selected anything"); static_assert(is_from_t::value, "cannot run select without a from()"); // 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. - _set_parameter_index(0); return {{}, get_dynamic_names(), db.prepare_select(*this)}; } - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_expression_list, index); - index = set_parameter_index(_where, index); - index = set_parameter_index(_group_by, index); - index = set_parameter_index(_having, index); - index = set_parameter_index(_order_by, index); - index = set_parameter_index(_limit, index); - index = set_parameter_index(_offset, index); - return index; - } - Flags _flags; ExpressionList _expression_list; From _from; diff --git a/include/sqlpp11/update.h b/include/sqlpp11/update.h index ac21cfd4..5384b741 100644 --- a/include/sqlpp11/update.h +++ b/include/sqlpp11/update.h @@ -67,7 +67,7 @@ namespace sqlpp template auto set(Assignment&&... assignment) - -> set_assignments_t::type...>> + -> set_assignments_t::type...>> { static_assert(std::is_same::value, "cannot call set() twice"); return { @@ -79,7 +79,7 @@ namespace sqlpp template auto dynamic_set(Assignment&&... assignment) - -> set_assignments_t::type...>> + -> set_assignments_t::type...>> { static_assert(std::is_same::value, "cannot call set() twice"); return { @@ -135,23 +135,6 @@ namespace sqlpp return *this; } - template - const update_t& serialize(std::ostream& os, Db& db) const - { - os << "UPDATE "; - _table.serialize(os, db); - _assignments.serialize(os, db); - _where.serialize(os, db); - return *this; - } - - template - update_t& serialize(std::ostream& os, Db& db) - { - static_cast(this)->serialize(os, db); - return *this; - } - static constexpr size_t _get_static_no_of_parameters() { return _parameter_list_t::size::value; @@ -176,22 +159,34 @@ namespace sqlpp { static_assert(not is_noop::value, "calling set() required before running update"); - _set_parameter_index(0); return {{}, db.prepare_update(*this)}; } - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_table, index); - index = set_parameter_index(_assignments, index); - return index; - } - Table _table; Assignments _assignments; Where _where; }; + template + struct interpreter_t> + { + using T = update_t; + + static Context& _(const T& t, Context& context) + { + context << "UPDATE "; + interpret(t._table, context); + interpret(t._assignments, context); + interpret(t._where, context); + return context; + } + }; + template constexpr update_t::type> update(Table&& table) { diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index b1ea8bef..e695b1d9 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -27,6 +27,7 @@ #include "MockDb.h" #include #include +#include #include #include @@ -65,5 +66,8 @@ int main() interpret(insert_into(t).set(t.gamma = true), printer).flush(); interpret(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).flush(); + interpret(update(t), printer).flush(); + interpret(update(t).set(t.gamma = true), printer).flush(); + return 0; }