2014-10-28 00:36:33 +08:00
|
|
|
/*
|
|
|
|
* 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_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>
|
2014-11-09 23:26:36 +08:00
|
|
|
#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
|
|
|
|
{
|
2014-10-29 14:34:42 +08:00
|
|
|
template<typename Database, typename... Parts>
|
2014-10-31 15:28:51 +08:00
|
|
|
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
|
2014-10-28 00:36:33 +08:00
|
|
|
{
|
2014-10-31 15:28:51 +08:00
|
|
|
using _methods_t = typename detail::custom_parts_t<Database, Parts...>::_result_methods_t;
|
2014-11-26 00:43:55 +08:00
|
|
|
using _traits = make_traits<no_value_t, tag::is_statement>;
|
2014-11-04 00:52:02 +08:00
|
|
|
using _recursive_traits = make_recursive_traits<Parts...>;
|
2014-10-31 15:28:51 +08:00
|
|
|
|
2014-11-18 02:20:55 +08:00
|
|
|
using _parameter_check = typename std::conditional<std::tuple_size<typename _recursive_traits::_parameters>::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;
|
|
|
|
|
2014-11-06 00:50:46 +08:00
|
|
|
custom_query_t(Parts... parts):
|
|
|
|
_parts(parts...)
|
|
|
|
{}
|
|
|
|
|
2014-11-09 23:26:36 +08:00
|
|
|
custom_query_t(std::tuple<Parts...> parts):
|
|
|
|
_parts(parts)
|
|
|
|
{}
|
|
|
|
|
2014-11-06 00:50:46 +08:00
|
|
|
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;
|
|
|
|
|
2014-10-31 15:28:51 +08:00
|
|
|
template<typename Db>
|
2014-11-10 00:03:32 +08:00
|
|
|
auto _run(Db& db) const -> decltype(std::declval<_methods_t>()._run(db, *this))
|
2014-10-31 15:28:51 +08:00
|
|
|
{
|
2014-11-18 02:20:55 +08:00
|
|
|
_run_check::_();
|
2014-10-31 15:28:51 +08:00
|
|
|
return _methods_t::_run(db, *this);
|
|
|
|
}
|
|
|
|
|
2014-11-04 00:52:02 +08:00
|
|
|
template<typename Db>
|
2014-11-10 00:03:32 +08:00
|
|
|
auto _prepare(Db& db) const -> decltype(std::declval<_methods_t>()._prepare(db, *this))
|
2014-11-04 00:52:02 +08:00
|
|
|
{
|
2014-11-18 02:20:55 +08:00
|
|
|
_prepare_check::_();
|
2014-11-04 00:52:02 +08:00
|
|
|
return _methods_t::_prepare(db, *this);
|
|
|
|
}
|
|
|
|
|
2014-11-06 00:50:46 +08:00
|
|
|
static constexpr size_t _get_static_no_of_parameters()
|
|
|
|
{
|
|
|
|
return std::tuple_size<parameters_of<custom_query_t>>::value;
|
|
|
|
}
|
2014-10-28 00:36:33 +08:00
|
|
|
|
2014-11-06 00:50:46 +08:00
|
|
|
size_t _get_no_of_parameters() const
|
|
|
|
{
|
|
|
|
return _get_static_no_of_parameters();
|
|
|
|
}
|
2014-10-28 00:36:33 +08:00
|
|
|
|
2014-11-09 23:26:36 +08:00
|
|
|
template<typename Part>
|
|
|
|
auto with_result_type_of(Part part)
|
|
|
|
-> custom_query_t<Database, Part, Parts...>
|
|
|
|
{
|
|
|
|
return {tuple_cat(std::make_tuple(part), _parts)};
|
|
|
|
}
|
|
|
|
|
2014-10-29 14:34:42 +08:00
|
|
|
std::tuple<Parts...> _parts;
|
2014-10-28 00:36:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Context, typename Database, typename... Parts>
|
|
|
|
struct serializer_t<Context, custom_query_t<Database, Parts...>>
|
|
|
|
{
|
2014-11-26 00:43:55 +08:00
|
|
|
using _serialize_check = serialize_check_of<Context, Parts...>;
|
2014-10-28 00:36:33 +08:00
|
|
|
using T = custom_query_t<Database, Parts...>;
|
|
|
|
|
|
|
|
static Context& _(const T& t, Context& context)
|
|
|
|
{
|
2014-10-29 14:34:42 +08:00
|
|
|
interpret_tuple_without_braces(t._parts, " ", context);
|
2014-10-28 00:36:33 +08:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Parts>
|
|
|
|
auto custom_query(Parts... parts)
|
2014-11-05 14:27:19 +08:00
|
|
|
-> custom_query_t<void, wrap_operand_t<Parts>...>
|
2014-10-28 00:36:33 +08:00
|
|
|
{
|
2014-10-31 15:28:51 +08:00
|
|
|
static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument");
|
2014-11-05 14:27:19 +08:00
|
|
|
return custom_query_t<void, wrap_operand_t<Parts>...>(parts...);
|
2014-10-28 00:36:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Database, typename... Parts>
|
2014-11-05 14:27:19 +08:00
|
|
|
auto dynamic_custom_query(const Database&, Parts... parts)
|
|
|
|
-> custom_query_t<Database, wrap_operand_t<Parts>...>
|
2014-10-28 00:36:33 +08:00
|
|
|
{
|
2014-10-31 15:28:51 +08:00
|
|
|
static_assert(sizeof...(Parts) > 0, "custom query requires at least one query argument");
|
2014-10-28 00:36:33 +08:00
|
|
|
static_assert(std::is_base_of<connection, Database>::value, "Invalid database parameter");
|
2014-11-05 14:27:19 +08:00
|
|
|
|
|
|
|
return custom_query_t<Database, wrap_operand_t<Parts>...>(parts...);
|
2014-10-28 00:36:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|