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

Added DISTINCT flag to avg and sum

This commit is contained in:
rbock 2014-01-22 09:31:36 +01:00
parent 34f6c7a2eb
commit abce8592e6
3 changed files with 38 additions and 12 deletions

View File

@ -34,9 +34,10 @@ namespace sqlpp
{ {
namespace vendor namespace vendor
{ {
template<typename Expr> template<typename Flag, typename Expr>
struct avg_t: public floating_point::template operators<avg_t<Expr>> struct avg_t: public floating_point::template operators<avg_t<Flag, Expr>>
{ {
static_assert(is_noop<Flag>::value or std::is_same<sqlpp::distinct_t, Flag>::value, "avg() used with flag other than 'distinct'");
static_assert(is_numeric_t<Expr>::value, "avg() requires a value expression as argument"); static_assert(is_numeric_t<Expr>::value, "avg() requires a value expression as argument");
struct _value_type: public floating_point struct _value_type: public floating_point
@ -76,14 +77,19 @@ namespace sqlpp
namespace vendor namespace vendor
{ {
template<typename Context, typename Expr> template<typename Context, typename Flag, typename Expr>
struct interpreter_t<Context, vendor::avg_t<Expr>> struct interpreter_t<Context, vendor::avg_t<Flag, Expr>>
{ {
using T = vendor::avg_t<Expr>; using T = vendor::avg_t<Flag, Expr>;
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "AVG("; context << "AVG(";
if (std::is_same<sqlpp::distinct_t, Flag>::value)
{
interpret(Flag(), context);
context << ' ';
}
interpret(t._expr, context); interpret(t._expr, context);
context << ")"; context << ")";
return context; return context;
@ -92,7 +98,13 @@ namespace sqlpp
} }
template<typename T> template<typename T>
auto avg(T&& t) -> typename vendor::avg_t<typename operand_t<T, is_value_t>::type> auto avg(T&& t) -> typename vendor::avg_t<vendor::noop, typename operand_t<T, is_value_t>::type>
{
return { std::forward<T>(t) };
}
template<typename T>
auto avg(const sqlpp::distinct_t&, T&& t) -> typename vendor::avg_t<sqlpp::distinct_t, typename operand_t<T, is_value_t>::type>
{ {
return { std::forward<T>(t) }; return { std::forward<T>(t) };
} }

View File

@ -34,9 +34,10 @@ namespace sqlpp
{ {
namespace vendor namespace vendor
{ {
template<typename Expr> template<typename Flag, typename Expr>
struct sum_t: public boolean::template operators<sum_t<Expr>> struct sum_t: public boolean::template operators<sum_t<Flag, Expr>>
{ {
static_assert(is_noop<Flag>::value or std::is_same<sqlpp::distinct_t, Flag>::value, "sum() used with flag other than 'distinct'");
static_assert(is_numeric_t<Expr>::value, "sum() requires a numeric expression as argument"); static_assert(is_numeric_t<Expr>::value, "sum() requires a numeric expression as argument");
struct _value_type: public Expr::_value_type::_base_value_type struct _value_type: public Expr::_value_type::_base_value_type
@ -76,14 +77,19 @@ namespace sqlpp
namespace vendor namespace vendor
{ {
template<typename Context, typename Expr> template<typename Context, typename Flag, typename Expr>
struct interpreter_t<Context, vendor::sum_t<Expr>> struct interpreter_t<Context, vendor::sum_t<Flag, Expr>>
{ {
using T = vendor::sum_t<Expr>; using T = vendor::sum_t<Flag, Expr>;
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "SUM("; context << "SUM(";
if (std::is_same<sqlpp::distinct_t, Flag>::value)
{
interpret(Flag(), context);
context << ' ';
}
interpret(t._expr, context); interpret(t._expr, context);
context << ")"; context << ")";
return context; return context;
@ -92,7 +98,13 @@ namespace sqlpp
} }
template<typename T> template<typename T>
auto sum(T&& t) -> typename vendor::sum_t<typename operand_t<T, is_value_t>::type> auto sum(T&& t) -> typename vendor::sum_t<vendor::noop, typename operand_t<T, is_value_t>::type>
{
return { std::forward<T>(t) };
}
template<typename T>
auto sum(const sqlpp::distinct_t&, T&& t) -> typename vendor::sum_t<sqlpp::distinct_t, typename operand_t<T, is_value_t>::type>
{ {
return { std::forward<T>(t) }; return { std::forward<T>(t) };
} }

View File

@ -116,6 +116,8 @@ int main()
// distinct aggregate // distinct aggregate
interpret(count(sqlpp::distinct, t.alpha % 7), printer).flush(); interpret(count(sqlpp::distinct, t.alpha % 7), printer).flush();
interpret(avg(sqlpp::distinct, t.alpha - 7), printer).flush();
interpret(sum(sqlpp::distinct, t.alpha + 7), printer).flush();
return 0; return 0;
} }