/* * 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 #include #include #include namespace sqlpp { template struct custom_query_t; namespace detail { template struct custom_parts_t { using _custom_query_t = custom_query_t; using _result_type_provider = detail::get_first_if; using _result_methods_t = typename _result_type_provider::template _result_methods_t<_result_type_provider>; }; } template struct custom_query_t: private detail::custom_parts_t::_result_methods_t { using _methods_t = typename detail::custom_parts_t::_result_methods_t; using _traits = make_traits; using _recursive_traits = make_recursive_traits; using _parameter_check = typename std::conditional::value == 0, consistent_t, assert_no_parameters_t>::type; using _run_check = detail::get_first_if; using _prepare_check = consistent_t; custom_query_t(Parts... parts): _parts(parts...) {} custom_query_t(std::tuple 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 auto _run(Db& db) const -> decltype(std::declval<_methods_t>()._run(db, *this)) { _run_check::_(); return _methods_t::_run(db, *this); } template 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>::value; } size_t _get_no_of_parameters() const { return _get_static_no_of_parameters(); } template auto with_result_type_of(Part part) -> custom_query_t { return {tuple_cat(std::make_tuple(part), _parts)}; } std::tuple _parts; }; template struct serializer_t> { using T = custom_query_t; static Context& _(const T& t, Context& context) { interpret_tuple_without_braces(t._parts, " ", context); return context; } }; template auto custom_query(Parts... parts) -> custom_query_t...> { static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument"); return custom_query_t...>(parts...); } template auto dynamic_custom_query(const Database&, Parts... parts) -> custom_query_t...> { static_assert(sizeof...(Parts) > 0, "custom query requires at least one query argument"); static_assert(std::is_base_of::value, "Invalid database parameter"); return custom_query_t...>(parts...); } } #endif