From c03ef1b0b115679c1758511ee9274a042fc89f23 Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 13 Jan 2014 06:24:27 +0100 Subject: [PATCH] interpret handles order_by(), limit() and offset() --- include/sqlpp11/limit.h | 26 ++++++++++++------------- include/sqlpp11/offset.h | 26 ++++++++++++------------- include/sqlpp11/order_by.h | 37 ++++++++++++++++++------------------ include/sqlpp11/sort_order.h | 37 +++++++++++++++++++++--------------- tests/InterpretTest.cpp | 2 ++ 5 files changed, 67 insertions(+), 61 deletions(-) diff --git a/include/sqlpp11/limit.h b/include/sqlpp11/limit.h index 15ec9f08..a88b281e 100644 --- a/include/sqlpp11/limit.h +++ b/include/sqlpp11/limit.h @@ -37,26 +37,24 @@ namespace sqlpp struct limit_t { using _is_limit = std::true_type; - using _parameter_tuple_t = std::tuple; static_assert(std::is_integral::value or (is_parameter_t::value and is_numeric_t::value), "limit requires an integral value or integral parameter"); - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_limit, "limit not supported by current database"); - os << " LIMIT " << _limit; - } - - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_limit, index); - return index; - } - Limit _limit; }; + template + struct interpreter_t> + { + using T = limit_t; + + static Context& _(const T& t, Context& context) + { + context << "LIMIT " << t._limit; + return context; + } + }; + struct dynamic_limit_t { using _is_limit = std::true_type; diff --git a/include/sqlpp11/offset.h b/include/sqlpp11/offset.h index 18593d43..24b2be3c 100644 --- a/include/sqlpp11/offset.h +++ b/include/sqlpp11/offset.h @@ -37,26 +37,24 @@ namespace sqlpp struct offset_t { using _is_offset = std::true_type; - using _parameter_tuple_t = std::tuple; static_assert(std::is_integral::value or (is_parameter_t::value and is_numeric_t::value), "offset requires an integral value or integral parameter"); - - template - void serialize(std::ostream& os, Db& db) const - { - os << " OFFSET " << _offset; - } - - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_offset, index); - return index; - } - Offset _offset; }; + template + struct interpreter_t> + { + using T = offset_t; + + static Context& _(const T& t, Context& context) + { + context << "OFFSET " << t._offset; + return context; + } + }; + struct dynamic_offset_t { using _is_offset = std::true_type; diff --git a/include/sqlpp11/order_by.h b/include/sqlpp11/order_by.h index 8f04cf0f..7591d4e8 100644 --- a/include/sqlpp11/order_by.h +++ b/include/sqlpp11/order_by.h @@ -60,28 +60,29 @@ namespace sqlpp _dynamic_expressions.push_back(std::forward(expr)); } - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_order_by, "order by not supported by current database"); - if (sizeof...(Expr) == 0 and _dynamic_expressions.empty()) - return; - - os << " ORDER BY "; - detail::serialize_tuple(os, db, _expressions, ','); - _dynamic_expressions.serialize(os, db, sizeof...(Expr) == 0); - } - - size_t _set_parameter_index(size_t index) - { - index = set_parameter_index(_expressions, index); - return index; - } - _parameter_tuple_t _expressions; detail::serializable_list _dynamic_expressions; }; + template + struct interpreter_t> + { + using T = order_by_t; + + static Context& _(const T& t, Context& context) + { + if (sizeof...(Expr) == 0 and t._dynamic_expressions.empty()) + return context; + context << " ORDER BY "; + interpret_tuple(t._expressions, ',', context); + if (sizeof...(Expr) and not t._dynamic_expressions.empty()) + context << ','; + interpret_serializable_list(t._dynamic_expressions, ',', context); + return context; + } + }; + + } #endif diff --git a/include/sqlpp11/sort_order.h b/include/sqlpp11/sort_order.h index f85199d7..30c85d3b 100644 --- a/include/sqlpp11/sort_order.h +++ b/include/sqlpp11/sort_order.h @@ -40,23 +40,30 @@ namespace sqlpp { using _is_sort_order = std::true_type; - template - void serialize(std::ostream& os, Db& db) const - { - _expression.serialize(os, db); - switch(SortType) - { - case sort_type::asc: - os << " ASC"; - break; - default: - os << " DESC"; - break; - } - } - Expression _expression; }; + + template + struct interpreter_t> + { + using T = sort_order_t; + + static Context& _(const T& t, Context& context) + { + interpret(t._expression, context); + switch(SortType) + { + case sort_type::asc: + context << " ASC"; + break; + default: + context << " DESC"; + break; + } + return context; + } + }; + } #endif diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index c3f3d8b0..2897be57 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -48,6 +48,8 @@ int main() interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")), printer).flush(); + interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")).order_by(t.beta.asc()), printer).flush(); + interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3).group_by(t.gamma).having(t.beta.like("%kuchen")).order_by(t.beta.asc()).limit(17).offset(3), printer).flush(); return 0; }