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

First custom query sent to MockDb

This commit is contained in:
rbock 2014-10-31 08:28:51 +01:00
parent 6684ac9ea8
commit 514ed4139c
4 changed files with 51 additions and 20 deletions

View File

@ -28,22 +28,48 @@
#define SQLPP_CUSTOM_QUERY_H
#include <sqlpp11/connection.h>
#include <sqlpp11/interpret_tuple.h>
#include <sqlpp11/detail/get_first.h>
namespace sqlpp
{
template<typename Database, typename... Parts>
struct custom_query_t/*:
public FirstPart::_result_type_provider::template _result_methods_t<custom_query_t>*/
{
custom_query_t(Parts... parts):
_parts(parts...)
{}
struct custom_query_t;
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;
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;
static void _check_consistency() {};
template<typename Db>
auto _run(Db& db) const -> decltype(_methods_t::_run(db, *this))
{
return _methods_t::_run(db, *this);
}
custom_query_t(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;
std::tuple<Parts...> _parts;
};
@ -64,6 +90,7 @@ namespace sqlpp
auto custom_query(Parts... parts)
-> custom_query_t<void, Parts...>
{
static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument");
return custom_query_t<void, Parts...>(parts...);
}
@ -71,6 +98,7 @@ namespace sqlpp
auto dynamic_custom_query(const Database&, Parts...)
-> custom_query_t<Database, 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 { };
}

View File

@ -293,14 +293,14 @@ namespace sqlpp
}
// Execute
template<typename Db>
auto _run(Db& db) const
-> result_t<decltype(db.select(this->_get_statement())), _result_row_t<Db>>
template<typename Db, typename S>
auto _run(Db& db, const S& s) const
-> result_t<decltype(db.select(s)), _result_row_t<Db>>
{
_statement_t::_check_consistency();
S::_check_consistency();
static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead");
return {db.select(_get_statement()), get_dynamic_names()};
return {db.select(s), get_dynamic_names()};
}
// Prepare

View File

@ -132,17 +132,19 @@ namespace sqlpp
public Policies::template _methods_t<detail::statement_policies_t<Db, Policies...>>...
{
using _policies_t = typename detail::statement_policies_t<Db, Policies...>;
using _result_type_provider = typename _policies_t::_result_type_provider;
template<typename Composite>
using _result_methods_t = typename _result_type_provider::template _result_methods_t<Composite>;
using _traits = make_traits<value_type_of<_policies_t>,
tag::is_select,
tag_if<tag::is_expression, is_expression_t<_policies_t>::value>,
tag_if<tag::is_selectable, is_expression_t<_policies_t>::value>,
tag_if<tag::is_return_value, detail::none_t<is_noop_t<_result_type_provider>::value>::value>,
tag::requires_braces>;
using _recursive_traits = typename _policies_t::_recursive_traits;
using _used_outer_tables = typename _policies_t::_all_provided_outer_tables;
using _result_type_provider = typename _policies_t::_result_type_provider;
using _name_t = typename _result_type_provider::_name_t;
// Constructors

View File

@ -37,8 +37,9 @@ int main()
test::TabFoo f;
test::TabBar t;
auto c = custom_query(select(all_of(t)).from(t), insert_into(t));
std::cerr << serialize(c, printer).str() << std::endl;
auto c = custom_query(select(all_of(t)).from(t));
//std::cerr << serialize(c, printer).str() << std::endl;
db(c);
return 0;
}