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:
parent
34f6c7a2eb
commit
abce8592e6
@ -34,9 +34,10 @@ namespace sqlpp
|
||||
{
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Expr>
|
||||
struct avg_t: public floating_point::template operators<avg_t<Expr>>
|
||||
template<typename Flag, typename 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");
|
||||
|
||||
struct _value_type: public floating_point
|
||||
@ -76,14 +77,19 @@ namespace sqlpp
|
||||
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Context, typename Expr>
|
||||
struct interpreter_t<Context, vendor::avg_t<Expr>>
|
||||
template<typename Context, typename Flag, typename 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)
|
||||
{
|
||||
context << "AVG(";
|
||||
if (std::is_same<sqlpp::distinct_t, Flag>::value)
|
||||
{
|
||||
interpret(Flag(), context);
|
||||
context << ' ';
|
||||
}
|
||||
interpret(t._expr, context);
|
||||
context << ")";
|
||||
return context;
|
||||
@ -92,7 +98,13 @@ namespace sqlpp
|
||||
}
|
||||
|
||||
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) };
|
||||
}
|
||||
|
@ -34,9 +34,10 @@ namespace sqlpp
|
||||
{
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Expr>
|
||||
struct sum_t: public boolean::template operators<sum_t<Expr>>
|
||||
template<typename Flag, typename 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");
|
||||
|
||||
struct _value_type: public Expr::_value_type::_base_value_type
|
||||
@ -76,14 +77,19 @@ namespace sqlpp
|
||||
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Context, typename Expr>
|
||||
struct interpreter_t<Context, vendor::sum_t<Expr>>
|
||||
template<typename Context, typename Flag, typename 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)
|
||||
{
|
||||
context << "SUM(";
|
||||
if (std::is_same<sqlpp::distinct_t, Flag>::value)
|
||||
{
|
||||
interpret(Flag(), context);
|
||||
context << ' ';
|
||||
}
|
||||
interpret(t._expr, context);
|
||||
context << ")";
|
||||
return context;
|
||||
@ -92,7 +98,13 @@ namespace sqlpp
|
||||
}
|
||||
|
||||
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) };
|
||||
}
|
||||
|
@ -116,6 +116,8 @@ int main()
|
||||
|
||||
// distinct aggregate
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user