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

Split union data from union, to be used in ctes, as well

This commit is contained in:
rbock 2015-02-08 18:26:00 +01:00
parent 3cef1a1217
commit b60df812e1
2 changed files with 79 additions and 42 deletions

View File

@ -27,6 +27,7 @@
#ifndef SQLPP_UNION_H
#define SQLPP_UNION_H
#include <sqlpp11/union_data.h>
#include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/parameter_list.h>
@ -38,7 +39,6 @@
namespace sqlpp
{
struct no_union_t;
using blank_union_t = statement_t<void,
@ -60,26 +60,6 @@ namespace sqlpp
template<bool Check, typename Union>
using union_statement_t = typename union_statement_impl<Check, Union>::type;
// UNION DATA
template<typename Database, typename Flag, typename Lhs, typename Rhs>
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<typename Database, typename Flag, typename Lhs, typename Rhs>
struct union_t
@ -164,7 +144,7 @@ namespace sqlpp
using _database_t = typename Policies::_database_t;
template<typename... T>
using _check = logic::all_t<is_statement_t<T>::value...>; // FIXME and consistent/runnable
using _check = logic::all_t<is_statement_t<T>::value...>;
template<typename Check, typename T>
using _new_statement_t = union_statement_t<Check::value, T>;
@ -192,10 +172,13 @@ namespace sqlpp
-> _new_statement_t<_check<Rhs>, union_t<void, all_t, derived_statement_t<Policies>, Rhs>>
{
static_assert(is_statement_t<Rhs>::value, "argument of union call has to be a statement");
static_assert(has_policy_t<Rhs, is_select_t>::value, "argument of union call has to be a select");
static_assert(has_result_row_t<Rhs>::value, "argument of a union has to be a (complete) select statement");
static_assert(has_result_row_t<derived_statement_t<Policies>>::value, "left hand side argument of a union has to be a (complete) select statement");
static_assert(std::is_same<get_result_row_t<derived_statement_t<Policies>>, get_result_row_t<Rhs>>::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<Rhs>;
static_assert(is_static_result_row_t<_result_row_t>::value, "unions must not have dynamically added columns");
return _union_impl<void, all_t>(_check<derived_statement_t<Policies>, Rhs>{}, rhs);
}
@ -216,26 +199,6 @@ namespace sqlpp
};
};
// Interpreters
template<typename Context, typename Database, typename Flag, typename Lhs, typename Rhs>
struct serializer_t<Context, union_data_t<Database, Flag, Lhs, Rhs>>
{
using _serialize_check = serialize_check_of<Context, Lhs, Rhs>;
using T = union_data_t<Database, Flag, Lhs, Rhs>;
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

View File

@ -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 <sqlpp11/serializer.h>
namespace sqlpp
{
template<typename Database, typename Flag, typename Lhs, typename Rhs>
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<typename Context, typename Database, typename Flag, typename Lhs, typename Rhs>
struct serializer_t<Context, union_data_t<Database, Flag, Lhs, Rhs>>
{
using _serialize_check = serialize_check_of<Context, Lhs, Rhs>;
using T = union_data_t<Database, Flag, Lhs, Rhs>;
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