0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00
sqlpp11/include/sqlpp11/custom_query.h

142 lines
5.1 KiB
C
Raw Normal View History

2014-10-28 00:36:33 +08:00
/*
2015-02-16 02:00:21 +08:00
* Copyright (c) 2013-2015, Roland Bock
2014-10-28 00:36:33 +08:00
* All rights reserved.
2015-08-05 20:43:21 +08:00
*
2014-10-28 00:36:33 +08:00
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
2015-08-05 20:43:21 +08:00
*
2014-10-28 00:36:33 +08:00
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
2015-08-05 20:43:21 +08:00
*
2014-10-28 00:36:33 +08:00
* 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.
2015-08-05 20:43:21 +08:00
*
2014-10-28 00:36:33 +08:00
* 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_CUSTOM_QUERY_H
#define SQLPP_CUSTOM_QUERY_H
#include <sqlpp11/connection.h>
2014-10-31 15:28:51 +08:00
#include <sqlpp11/interpret_tuple.h>
#include <sqlpp11/hidden.h>
2014-10-31 15:28:51 +08:00
#include <sqlpp11/detail/get_first.h>
2014-10-28 00:36:33 +08:00
namespace sqlpp
{
template <typename Database, typename... Parts>
struct custom_query_t;
namespace detail
{
template <typename Db, typename... Parts>
struct custom_parts_t
{
using _custom_query_t = custom_query_t<Db, Parts...>;
using _result_type_provider = detail::get_first_if<is_return_value_t, noop, Parts...>;
using _result_methods_t = typename _result_type_provider::template _result_methods_t<_result_type_provider>;
};
}
template <typename Database, typename... Parts>
struct custom_query_t : private detail::custom_parts_t<Database, Parts...>::_result_methods_t
{
using _methods_t = typename detail::custom_parts_t<Database, Parts...>::_result_methods_t;
using _traits = make_traits<no_value_t, tag::is_statement>;
using _nodes = detail::type_vector<Parts...>;
using _parameter_check =
typename std::conditional<detail::type_vector_size<parameters_of<custom_query_t>>::value == 0,
consistent_t,
assert_no_parameters_t>::type;
using _run_check = detail::get_first_if<is_inconsistent_t, consistent_t, _parameter_check>;
using _prepare_check = consistent_t;
custom_query_t(Parts... parts) : _parts(parts...)
{
}
custom_query_t(std::tuple<Parts...> parts) : _parts(parts)
{
}
custom_query_t(const custom_query_t&) = default;
custom_query_t(custom_query_t&&) = default;
custom_query_t& operator=(const custom_query_t&) = default;
custom_query_t& operator=(custom_query_t&&) = default;
~custom_query_t() = default;
template <typename Db>
auto _run(Db& db) const -> decltype(std::declval<_methods_t>()._run(db, *this))
{
_run_check::_();
return _methods_t::_run(db, *this);
}
template <typename Db>
auto _prepare(Db& db) const -> decltype(std::declval<_methods_t>()._prepare(db, *this))
{
_prepare_check::_();
return _methods_t::_prepare(db, *this);
}
static constexpr size_t _get_static_no_of_parameters()
{
return std::tuple_size<parameters_of<custom_query_t>>::value;
}
size_t _get_no_of_parameters() const
{
return _get_static_no_of_parameters();
}
template <typename Part>
auto with_result_type_of(Part part) -> custom_query_t<Database, Part, Parts...>
{
return {tuple_cat(std::make_tuple(part), _parts)};
}
std::tuple<Parts...> _parts;
};
template <typename Context, typename Database, typename... Parts>
struct serializer_t<Context, custom_query_t<Database, Parts...>>
{
using _serialize_check = serialize_check_of<Context, Parts...>;
using T = custom_query_t<Database, Parts...>;
static Context& _(const T& t, Context& context)
{
interpret_tuple_without_braces(t._parts, " ", context);
return context;
}
};
template <typename... Parts>
auto custom_query(Parts... parts) -> custom_query_t<void, wrap_operand_t<Parts>...>
{
static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument");
return custom_query_t<void, wrap_operand_t<Parts>...>(parts...);
}
template <typename Database, typename... Parts>
auto dynamic_custom_query(const Database&, Parts... parts) -> custom_query_t<Database, wrap_operand_t<Parts>...>
{
static_assert(sizeof...(Parts) > 0, "custom query requires at least one query argument");
static_assert(std::is_base_of<connection, Database>::value, "Invalid database parameter");
return custom_query_t<Database, wrap_operand_t<Parts>...>(parts...);
}
2014-10-28 00:36:33 +08:00
}
#endif