From b60df812e1a86381f67f0900b4a3b2a93b4abe5c Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 8 Feb 2015 18:26:00 +0100 Subject: [PATCH] Split union data from union, to be used in ctes, as well --- include/sqlpp11/union.h | 47 +++-------------------- include/sqlpp11/union_data.h | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 include/sqlpp11/union_data.h diff --git a/include/sqlpp11/union.h b/include/sqlpp11/union.h index 575f2b08..b45d1930 100644 --- a/include/sqlpp11/union.h +++ b/include/sqlpp11/union.h @@ -27,6 +27,7 @@ #ifndef SQLPP_UNION_H #define SQLPP_UNION_H +#include #include #include #include @@ -38,7 +39,6 @@ namespace sqlpp { - struct no_union_t; using blank_union_t = statement_t using union_statement_t = typename union_statement_impl::type; - - // UNION DATA - template - struct union_data_t - { - union_data_t(Lhs lhs, Rhs rhs): - _lhs(lhs), - _rhs(rhs) - {} - - union_data_t(const union_data_t&) = default; - union_data_t(union_data_t&&) = default; - union_data_t& operator=(const union_data_t&) = default; - union_data_t& operator=(union_data_t&&) = default; - ~union_data_t() = default; - - Lhs _lhs; - Rhs _rhs; - }; - // UNION(EXPR) template struct union_t @@ -164,7 +144,7 @@ namespace sqlpp using _database_t = typename Policies::_database_t; template - using _check = logic::all_t::value...>; // FIXME and consistent/runnable + using _check = logic::all_t::value...>; template using _new_statement_t = union_statement_t; @@ -192,10 +172,13 @@ namespace sqlpp -> _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"); static_assert(has_result_row_t::value, "argument of a union has to be a (complete) select statement"); static_assert(has_result_row_t>::value, "left hand side argument of a union has to be a (complete) select statement"); static_assert(std::is_same>, get_result_row_t>::value, "both select statements in a union have to have the same result columns (type and name)"); + using _result_row_t = get_result_row_t; + static_assert(is_static_result_row_t<_result_row_t>::value, "unions must not have dynamically added columns"); return _union_impl(_check, Rhs>{}, rhs); } @@ -216,26 +199,6 @@ namespace sqlpp }; }; - // Interpreters - template - struct serializer_t> - { - using _serialize_check = serialize_check_of; - using T = union_data_t; - - static Context& _(const T& t, Context& context) - { - context << '('; - serialize(t._lhs, context); - context << ") UNION "; - serialize(Flag{}, context); - context << " ("; - serialize(t._rhs, context); - context << ')'; - return context; - } - }; - } #endif diff --git a/include/sqlpp11/union_data.h b/include/sqlpp11/union_data.h new file mode 100644 index 00000000..fd9bba32 --- /dev/null +++ b/include/sqlpp11/union_data.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2013-2014, 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_DATA_H +#define SQLPP_UNION_DATA_H + +#include + +namespace sqlpp +{ + template + struct union_data_t + { + union_data_t(Lhs lhs, Rhs rhs): + _lhs(lhs), + _rhs(rhs) + {} + + union_data_t(const union_data_t&) = default; + union_data_t(union_data_t&&) = default; + union_data_t& operator=(const union_data_t&) = default; + union_data_t& operator=(union_data_t&&) = default; + ~union_data_t() = default; + + Lhs _lhs; + Rhs _rhs; + }; + + // Interpreters + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = union_data_t; + + static Context& _(const T& t, Context& context) + { + context << '('; + serialize(t._lhs, context); + context << ") UNION "; + serialize(Flag{}, context); + context << " ("; + serialize(t._rhs, context); + context << ')'; + return context; + } + }; + +} + +#endif