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

interpret works for group_by(), having() und .like()

This commit is contained in:
rbock 2014-01-12 16:19:09 +01:00
parent d56d1422cd
commit 7fc5c34190
5 changed files with 57 additions and 49 deletions

View File

@ -62,29 +62,29 @@ namespace sqlpp
_dynamic_expressions.emplace_back(std::forward<E>(expr)); _dynamic_expressions.emplace_back(std::forward<E>(expr));
} }
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
static_assert(Db::_supports_group_by, "group_by() not supported by current database");
if (sizeof...(Expr) == 0 and _dynamic_expressions.empty())
return;
os << " GROUP 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; _parameter_tuple_t _expressions;
detail::serializable_list<Database> _dynamic_expressions; detail::serializable_list<Database> _dynamic_expressions;
}; };
template<typename Context, typename Database, typename... Expr>
struct interpreter_t<Context, group_by_t<Database, Expr...>>
{
using T = group_by_t<Database, Expr...>;
static Context& _(const T& t, Context& context)
{
if (sizeof...(Expr) == 0 and t._dynamic_expressions.empty())
return context;
context << " GROUP 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 #endif

View File

@ -57,28 +57,29 @@ namespace sqlpp
_dynamic_expressions.emplace_back(std::forward<E>(expr)); _dynamic_expressions.emplace_back(std::forward<E>(expr));
} }
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
static_assert(Db::_supports_having, "having() not supported by current database");
if (sizeof...(Expr) == 0 and _dynamic_expressions.empty())
return;
os << " HAVING ";
detail::serialize_tuple(os, db, _expressions, " AND ");
_dynamic_expressions.serialize(os, db, " AND ", sizeof...(Expr) == 0);
}
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_expressions, index);
return index;
}
_parameter_tuple_t _expressions; _parameter_tuple_t _expressions;
detail::serializable_list<Database> _dynamic_expressions; detail::serializable_list<Database> _dynamic_expressions;
}; };
template<typename Context, typename Database, typename... Expr>
struct interpreter_t<Context, having_t<Database, Expr...>>
{
using T = having_t<Database, Expr...>;
static Context& _(const T& t, Context& context)
{
if (sizeof...(Expr) == 0 and t._dynamic_expressions.empty())
return context;
context << " HAVING ";
interpret_tuple(t._expressions, " AND ", context);
if (sizeof...(Expr) and not t._dynamic_expressions.empty())
context << " AND ";
interpret_serializable_list(t._dynamic_expressions, " AND ", context);
return context;
}
};
} }
#endif #endif

View File

@ -33,8 +33,6 @@
namespace sqlpp namespace sqlpp
{ {
namespace detail
{
// The ValueType should be boolean, this is a hack because boolean is not fully defined when the compiler first gets here... // The ValueType should be boolean, this is a hack because boolean is not fully defined when the compiler first gets here...
template<typename ValueType, typename Operand, typename Pattern> template<typename ValueType, typename Operand, typename Pattern>
struct like_t: public ValueType::_base_value_type::template operators<like_t<ValueType, Operand, Pattern>> struct like_t: public ValueType::_base_value_type::template operators<like_t<ValueType, Operand, Pattern>>
@ -74,13 +72,6 @@ namespace sqlpp
like_t& operator=(like_t&&) = default; like_t& operator=(like_t&&) = default;
~like_t() = default; ~like_t() = default;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_operand, index);
index = set_parameter_index(_pattern, index);
return index;
}
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
@ -91,11 +82,25 @@ namespace sqlpp
os << ")"; os << ")";
} }
private:
Operand _operand; Operand _operand;
Pattern _pattern; Pattern _pattern;
}; };
}
template<typename Context, typename ValueType, typename Operand, typename Pattern>
struct interpreter_t<Context, like_t<ValueType, Operand, Pattern>>
{
using T = like_t<ValueType, Operand, Pattern>;
static Context& _(const T& t, Context& context)
{
interpret(t._operand, context);
context << " LIKE(";
interpret(t._pattern, context);
context << ")";
return context;
}
};
} }
#endif #endif

View File

@ -203,10 +203,10 @@ namespace sqlpp
} }
template<typename T> template<typename T>
detail::like_t<boolean, Base, typename _constraint<T>::type> like(T&& t) const like_t<boolean, Base, typename _constraint<T>::type> like(T&& t) const
{ {
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand"); static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), std::forward<T>(t) }; return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
} }
}; };

View File

@ -46,6 +46,8 @@ int main()
interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush();
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), 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();
return 0; return 0;
} }