From 028af83fe78feb000331877d1b025262396e3910 Mon Sep 17 00:00:00 2001 From: rbock Date: Thu, 5 Mar 2015 18:01:59 +0100 Subject: [PATCH] Fixed UNION DISTINCT to not serialize DISTINCT sqlite3 does not support the keyword in UNION, and for others DISTINCT is the default anywy --- include/sqlpp11/type_traits.h | 1 + include/sqlpp11/union.h | 10 ++--- include/sqlpp11/union_data.h | 6 +-- include/sqlpp11/union_flags.h | 75 +++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 include/sqlpp11/union_flags.h diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index 74cad854..8610532a 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -83,6 +83,7 @@ namespace sqlpp SQLPP_VALUE_TRAIT_GENERATOR(is_multi_expression) SQLPP_VALUE_TRAIT_GENERATOR(is_alias) SQLPP_VALUE_TRAIT_GENERATOR(is_select_flag) + SQLPP_VALUE_TRAIT_GENERATOR(is_union_flag) SQLPP_VALUE_TRAIT_GENERATOR(is_result_field) SQLPP_VALUE_TRAIT_GENERATOR(must_not_insert) diff --git a/include/sqlpp11/union.h b/include/sqlpp11/union.h index 199c0aa8..f8daf2f2 100644 --- a/include/sqlpp11/union.h +++ b/include/sqlpp11/union.h @@ -28,7 +28,7 @@ #define SQLPP_UNION_H #include -#include +#include #include #include #include @@ -154,7 +154,7 @@ namespace sqlpp template auto union_distinct(Rhs rhs) const - -> _new_statement_t<_check, union_t, Rhs>> + -> _new_statement_t<_check, union_t, Rhs>> { static_assert(is_statement_t::value, "argument of union call has to be a statement"); static_assert(has_policy_t::value, "argument of union call has to be a select"); @@ -165,12 +165,12 @@ namespace sqlpp static_assert(std::is_same>, _result_row_t>::value, "both arguments in a union have to have the same result columns (type and name)"); static_assert(is_static_result_row_t<_result_row_t>::value, "unions must not have dynamically added columns"); - return _union_impl(_check, Rhs>{}, rhs); + return _union_impl(_check, Rhs>{}, rhs); } template auto union_all(Rhs rhs) const - -> _new_statement_t<_check, union_t, Rhs>> + -> _new_statement_t<_check, union_t, Rhs>> { static_assert(is_statement_t::value, "argument of union call has to be a statement"); static_assert(has_policy_t::value, "argument of union call has to be a select"); @@ -181,7 +181,7 @@ namespace sqlpp static_assert(std::is_same>, _result_row_t>::value, "both arguments in a union have to have the same result columns (type and name)"); static_assert(is_static_result_row_t<_result_row_t>::value, "unions must not have dynamically added columns"); - return _union_impl(_check, Rhs>{}, rhs); + return _union_impl(_check, Rhs>{}, rhs); } private: diff --git a/include/sqlpp11/union_data.h b/include/sqlpp11/union_data.h index 55379046..f1c02f4a 100644 --- a/include/sqlpp11/union_data.h +++ b/include/sqlpp11/union_data.h @@ -58,13 +58,11 @@ namespace sqlpp static Context& _(const T& t, Context& context) { - context << '('; serialize(t._lhs, context); - context << ") UNION "; + context << " UNION "; serialize(Flag{}, context); - context << " ("; + context << " "; serialize(t._rhs, context); - context << ')'; return context; } }; diff --git a/include/sqlpp11/union_flags.h b/include/sqlpp11/union_flags.h new file mode 100644 index 00000000..9d2ded02 --- /dev/null +++ b/include/sqlpp11/union_flags.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2013-2015, 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_UNION_FLAGS_H +#define SQLPP_UNION_FLAGS_H + +#include +#include +#include +#include + +namespace sqlpp +{ + // standard select flags + struct union_all_t + { + using _traits = make_traits; + using _nodes = detail::type_vector<>; + }; + + template + struct serializer_t + { + using _serialize_check = consistent_t; + + static Context& _(const union_all_t&, Context& context) + { + context << "ALL"; + return context; + } + }; + + struct union_distinct_t + { + using _traits = make_traits; + using _nodes = detail::type_vector<>; + }; + + template + struct serializer_t + { + using _serialize_check = consistent_t; + + static Context& _(const union_distinct_t&, Context& context) + { + return context; + } + }; + +} + +#endif