0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

interpret handles order_by(), limit() and offset()

This commit is contained in:
rbock 2014-01-13 06:24:27 +01:00
parent f21860e5c5
commit c03ef1b0b1
5 changed files with 67 additions and 61 deletions

View File

@ -37,26 +37,24 @@ namespace sqlpp
struct limit_t
{
using _is_limit = std::true_type;
using _parameter_tuple_t = std::tuple<Limit>;
static_assert(std::is_integral<Limit>::value
or (is_parameter_t<Limit>::value and is_numeric_t<Limit>::value), "limit requires an integral value or integral parameter");
template<typename Db>
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<typename Context, typename Limit>
struct interpreter_t<Context, limit_t<Limit>>
{
using T = limit_t<Limit>;
static Context& _(const T& t, Context& context)
{
context << "LIMIT " << t._limit;
return context;
}
};
struct dynamic_limit_t
{
using _is_limit = std::true_type;

View File

@ -37,26 +37,24 @@ namespace sqlpp
struct offset_t
{
using _is_offset = std::true_type;
using _parameter_tuple_t = std::tuple<Offset>;
static_assert(std::is_integral<Offset>::value
or (is_parameter_t<Offset>::value and is_numeric_t<Offset>::value), "offset requires an integral value or integral parameter");
template<typename Db>
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<typename Context, typename Offset>
struct interpreter_t<Context, offset_t<Offset>>
{
using T = offset_t<Offset>;
static Context& _(const T& t, Context& context)
{
context << "OFFSET " << t._offset;
return context;
}
};
struct dynamic_offset_t
{
using _is_offset = std::true_type;

View File

@ -60,28 +60,29 @@ namespace sqlpp
_dynamic_expressions.push_back(std::forward<E>(expr));
}
template<typename Db>
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<Database> _dynamic_expressions;
};
template<typename Context, typename Database, typename... Expr>
struct interpreter_t<Context, order_by_t<Database, Expr...>>
{
using T = order_by_t<Database, Expr...>;
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

View File

@ -40,23 +40,30 @@ namespace sqlpp
{
using _is_sort_order = std::true_type;
template<typename Db>
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<typename Context, typename Expression, sort_type SortType>
struct interpreter_t<Context, sort_order_t<Expression, SortType>>
{
using T = sort_order_t<Expression, SortType>;
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

View File

@ -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;
}