diff --git a/include/sqlpp11/avg.h b/include/sqlpp11/avg.h index 9ca975e6..0606ac5d 100644 --- a/include/sqlpp11/avg.h +++ b/include/sqlpp11/avg.h @@ -34,9 +34,10 @@ namespace sqlpp { namespace vendor { - template - struct avg_t: public floating_point::template operators> + template + struct avg_t: public floating_point::template operators> { + static_assert(is_noop::value or std::is_same::value, "avg() used with flag other than 'distinct'"); static_assert(is_numeric_t::value, "avg() requires a value expression as argument"); struct _value_type: public floating_point @@ -76,14 +77,19 @@ namespace sqlpp namespace vendor { - template - struct interpreter_t> + template + struct interpreter_t> { - using T = vendor::avg_t; + using T = vendor::avg_t; static Context& _(const T& t, Context& context) { context << "AVG("; + if (std::is_same::value) + { + interpret(Flag(), context); + context << ' '; + } interpret(t._expr, context); context << ")"; return context; @@ -92,7 +98,13 @@ namespace sqlpp } template - auto avg(T&& t) -> typename vendor::avg_t::type> + auto avg(T&& t) -> typename vendor::avg_t::type> + { + return { std::forward(t) }; + } + + template + auto avg(const sqlpp::distinct_t&, T&& t) -> typename vendor::avg_t::type> { return { std::forward(t) }; } diff --git a/include/sqlpp11/count.h b/include/sqlpp11/count.h index 98c24cba..167b3f37 100644 --- a/include/sqlpp11/count.h +++ b/include/sqlpp11/count.h @@ -27,16 +27,17 @@ #ifndef SQLPP_COUNT_H #define SQLPP_COUNT_H -#include +#include #include namespace sqlpp { namespace vendor { - template - struct count_t: public sqlpp::detail::integral::template operators> + template + struct count_t: public sqlpp::detail::integral::template operators> { + static_assert(is_noop::value or std::is_same::value, "count() used with flag other than 'distinct'"); static_assert(is_value_t::value, "count() requires a sql value as argument"); struct _value_type: public sqlpp::detail::integral @@ -76,14 +77,19 @@ namespace sqlpp namespace vendor { - template - struct interpreter_t> + template + struct interpreter_t> { - using T = vendor::count_t; + using T = vendor::count_t; static Context& _(const T& t, Context& context) { context << "COUNT("; + if (std::is_same::value) + { + interpret(Flag(), context); + context << ' '; + } interpret(t._expr, context); context << ")"; return context; @@ -91,9 +97,14 @@ namespace sqlpp }; } -#warning: Add optional distinct flag to aggregate functions template - auto count(T&& t) -> typename vendor::count_t::type> + auto count(T&& t) -> typename vendor::count_t::type> + { + return { std::forward(t) }; + } + + template + auto count(const sqlpp::distinct_t&, T&& t) -> typename vendor::count_t::type> { return { std::forward(t) }; } diff --git a/include/sqlpp11/select_flags.h b/include/sqlpp11/select_flags.h new file mode 100644 index 00000000..ffd15b30 --- /dev/null +++ b/include/sqlpp11/select_flags.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2013, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SQLPP_SELECT_FLAGS_H +#define SQLPP_SELECT_FLAGS_H + +#include +#include +#include +#include +#include + +namespace sqlpp +{ + // standard select flags + struct all_t + { + struct _value_type + { + using _is_select_flag = std::true_type; + }; + }; + static constexpr all_t all = {}; + + namespace vendor + { + template + struct interpreter_t + { + static Context& _(const all_t&, Context& context) + { + context << "ALL"; + return context; + } + }; + } + + struct distinct_t + { + struct _value_type + { + using _is_select_flag = std::true_type; + }; + }; + static constexpr distinct_t distinct = {}; + + namespace vendor + { + template + struct interpreter_t + { + static Context& _(const distinct_t&, Context& context) + { + context << "DISTINCT"; + return context; + } + }; + } + + struct straight_join_t + { + struct _value_type + { + using _is_select_flag = std::true_type; + }; + }; + static constexpr straight_join_t straight_join = {}; + + namespace vendor + { + template + struct interpreter_t + { + static Context& _(const straight_join_t&, Context& context) + { + context << "STRAIGHT_JOIN"; + return context; + } + }; + } + +} + +#endif diff --git a/include/sqlpp11/sum.h b/include/sqlpp11/sum.h index e3d72ea1..0ce6fe07 100644 --- a/include/sqlpp11/sum.h +++ b/include/sqlpp11/sum.h @@ -34,9 +34,10 @@ namespace sqlpp { namespace vendor { - template - struct sum_t: public boolean::template operators> + template + struct sum_t: public boolean::template operators> { + static_assert(is_noop::value or std::is_same::value, "sum() used with flag other than 'distinct'"); static_assert(is_numeric_t::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 - struct interpreter_t> + template + struct interpreter_t> { - using T = vendor::sum_t; + using T = vendor::sum_t; static Context& _(const T& t, Context& context) { context << "SUM("; + if (std::is_same::value) + { + interpret(Flag(), context); + context << ' '; + } interpret(t._expr, context); context << ")"; return context; @@ -92,7 +98,13 @@ namespace sqlpp } template - auto sum(T&& t) -> typename vendor::sum_t::type> + auto sum(T&& t) -> typename vendor::sum_t::type> + { + return { std::forward(t) }; + } + + template + auto sum(const sqlpp::distinct_t&, T&& t) -> typename vendor::sum_t::type> { return { std::forward(t) }; } diff --git a/include/sqlpp11/vendor/select_flag_list.h b/include/sqlpp11/vendor/select_flag_list.h index d0aaaedf..c58d3f39 100644 --- a/include/sqlpp11/vendor/select_flag_list.h +++ b/include/sqlpp11/vendor/select_flag_list.h @@ -24,84 +24,18 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SQLPP_SELECT_FLAGS_H -#define SQLPP_SELECT_FLAGS_H +#ifndef SQLPP_VENDOR_SELECT_FLAG_LIST_H +#define SQLPP_VENDOR_SELECT_FLAG_LIST_H #include #include +#include #include #include #include namespace sqlpp { - // standard select flags - struct all_t - { - struct _value_type - { - using _is_select_flag = std::true_type; - }; - }; - static constexpr all_t all = {}; - - namespace vendor - { - template - struct interpreter_t - { - static Context& _(const all_t&, Context& context) - { - context << "ALL"; - return context; - } - }; - } - - struct distinct_t - { - struct _value_type - { - using _is_select_flag = std::true_type; - }; - }; - static constexpr distinct_t distinct = {}; - - namespace vendor - { - template - struct interpreter_t - { - static Context& _(const distinct_t&, Context& context) - { - context << "DISTINCT"; - return context; - } - }; - } - - struct straight_join_t - { - struct _value_type - { - using _is_select_flag = std::true_type; - }; - }; - static constexpr straight_join_t straight_join = {}; - - namespace vendor - { - template - struct interpreter_t - { - static Context& _(const straight_join_t&, Context& context) - { - context << "STRAIGHT_JOIN"; - return context; - } - }; - } - namespace vendor { template diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index d8fff6bc..7a90267b 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -114,5 +114,10 @@ int main() interpret(dynamic_select(db).dynamic_flags().add_flag(sqlpp::distinct).dynamic_columns().add_column(t.gamma).add_column(t.beta), printer).flush(); interpret(dynamic_select(db).dynamic_flags(sqlpp::distinct).add_flag(sqlpp::all).dynamic_columns(t.alpha).add_column(t.beta), printer).flush(); + // 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; }