mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Re-animated prepared statements
This commit is contained in:
parent
e775e6165e
commit
3d26f9a686
@ -114,16 +114,14 @@ namespace sqlpp
|
||||
return db.insert(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
template<typename Db>
|
||||
auto _prepare(Db& db) const
|
||||
-> prepared_insert_t<Db, insert_t>
|
||||
-> prepared_insert_t<Db, _statement_t>
|
||||
{
|
||||
_statement_t::_check_consistency();
|
||||
|
||||
return {{}, db.prepare_insert(*this)};
|
||||
}
|
||||
*/
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -64,40 +64,8 @@ namespace sqlpp
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename Exp, typename Enable = void>
|
||||
struct get_parameter_tuple
|
||||
{
|
||||
using type = std::tuple<>;
|
||||
};
|
||||
|
||||
template<typename Exp>
|
||||
struct get_parameter_tuple<Exp, typename std::enable_if<is_parameter_t<Exp>::value, void>::type>
|
||||
{
|
||||
using type = std::tuple<Exp>;
|
||||
};
|
||||
|
||||
template<typename... Param>
|
||||
struct get_parameter_tuple<std::tuple<Param...>, void>
|
||||
{
|
||||
// cat together parameter tuples
|
||||
using type = decltype(std::tuple_cat(std::declval<typename get_parameter_tuple<Param>::type>()...));
|
||||
};
|
||||
|
||||
template<typename Exp>
|
||||
struct get_parameter_tuple<Exp, typename std::enable_if<not std::is_same<typename Exp::_parameter_tuple_t, void>::value, void>::type>
|
||||
{
|
||||
using type = typename get_parameter_tuple<typename Exp::_parameter_tuple_t>::type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename Exp>
|
||||
struct make_parameter_list_t
|
||||
{
|
||||
using type = parameter_list_t<typename detail::get_parameter_tuple<Exp>::type>;
|
||||
};
|
||||
using make_parameter_list_t = parameter_list_t<parameters_of<Exp>>;
|
||||
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ namespace sqlpp
|
||||
struct prepared_select_t
|
||||
{
|
||||
using _result_row_t = typename Select::template _result_row_t<Database>;
|
||||
using _parameter_list_t = typename Select::_parameter_list_t;
|
||||
using _parameter_list_t = parameters_of<Select>;
|
||||
using _dynamic_names_t = typename Select::_dynamic_names_t;
|
||||
using _prepared_statement_t = typename Database::_prepared_statement_t;
|
||||
|
||||
|
@ -65,16 +65,14 @@ namespace sqlpp
|
||||
return db.remove(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
template<typename Db>
|
||||
auto _prepare(Db& db) const
|
||||
-> prepared_remove_t<Db, remove_t>
|
||||
-> prepared_remove_t<Db, _statement_t>
|
||||
{
|
||||
_check_consistency();
|
||||
|
||||
return {{}, db.prepare_remove(*this)};
|
||||
}
|
||||
*/
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -294,18 +294,16 @@ namespace sqlpp
|
||||
|
||||
return {db.select(_get_statement()), get_dynamic_names()};
|
||||
}
|
||||
#if 0
|
||||
|
||||
// Prepare
|
||||
template<typename Db>
|
||||
auto _prepare(Db& db) const
|
||||
-> prepared_select_t<Db, select_t>
|
||||
-> prepared_select_t<Db, _statement_t>
|
||||
{
|
||||
_statement_t::_check_consistency();
|
||||
|
||||
return {{}, get_dynamic_names(), db.prepare_select(*this)};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
};
|
||||
|
@ -133,16 +133,14 @@ namespace sqlpp
|
||||
return db.insert(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
template<typename Db>
|
||||
auto _prepare(Db& db) const
|
||||
-> prepared_insert_t<Db, insert_t>
|
||||
-> prepared_insert_t<Db, _statement_t>
|
||||
{
|
||||
_check_consistency();
|
||||
|
||||
return {{}, db.prepare_insert(*this)};
|
||||
}
|
||||
*/
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -112,10 +112,10 @@ namespace sqlpp
|
||||
|
||||
struct _recursive_traits
|
||||
{
|
||||
using _parameters = std::tuple<>; // FIXME
|
||||
using _required_tables = _required_tables;
|
||||
using _provided_tables = detail::type_set<>;
|
||||
using _extra_tables = detail::type_set<>;
|
||||
using _parameters = detail::make_parameter_list_t<parameters_of<Policies>...>;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
@ -178,6 +178,22 @@ namespace sqlpp
|
||||
{
|
||||
using type = typename T::_recursive_traits::_extra_tables;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct parameters_of_impl
|
||||
{
|
||||
using type = typename T::_recursive_traits::_parameters;
|
||||
};
|
||||
|
||||
template<typename... T>
|
||||
struct make_parameter_list_impl
|
||||
{
|
||||
using type = decltype(std::tuple_cat(std::declval<T>()...));
|
||||
};
|
||||
|
||||
#warning this will lead to confusion with ::sqlpp::make_parameter_list_t in parameter_list.h
|
||||
template<typename... T>
|
||||
using make_parameter_list_t = typename make_parameter_list_impl<T...>::type;
|
||||
}
|
||||
template<typename T>
|
||||
using value_type_of = typename detail::value_type_of_impl<T>::type;
|
||||
@ -191,6 +207,9 @@ namespace sqlpp
|
||||
template<typename T>
|
||||
using extra_tables_of = typename detail::extra_table_of_impl<T>::type;
|
||||
|
||||
template<typename T>
|
||||
using parameters_of = typename detail::parameters_of_impl<T>::type;
|
||||
|
||||
template<typename ValueType, typename... Tags>
|
||||
struct make_traits
|
||||
{
|
||||
@ -203,6 +222,7 @@ namespace sqlpp
|
||||
using _required_tables = detail::make_joined_set_t<required_tables_of<Arguments>...>;
|
||||
using _provided_tables = detail::make_joined_set_t<provided_tables_of<Arguments>...>;
|
||||
using _extra_tables = detail::make_joined_set_t<extra_tables_of<Arguments>...>;
|
||||
using _parameters = detail::make_parameter_list_t<parameters_of<Arguments>...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -65,16 +65,14 @@ namespace sqlpp
|
||||
return db.update(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
template<typename Db>
|
||||
auto _prepare(Db& db) const
|
||||
-> prepared_update_t<Db, update_t>
|
||||
-> prepared_update_t<Db, _statement_t>
|
||||
{
|
||||
_statement_t::_check_consistency();
|
||||
|
||||
return {{}, db.prepare_update(*this)};
|
||||
}
|
||||
*/
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -7,13 +7,13 @@ macro (build_and_run arg)
|
||||
endmacro ()
|
||||
|
||||
#build_and_run(InterpretTest)
|
||||
build_and_run(InsertTest)
|
||||
build_and_run(RemoveTest)
|
||||
build_and_run(UpdateTest)
|
||||
build_and_run(SelectTest)
|
||||
#build_and_run(InsertTest)
|
||||
#build_and_run(RemoveTest)
|
||||
#build_and_run(UpdateTest)
|
||||
#build_and_run(SelectTest)
|
||||
#build_and_run(SelectTypeTest)
|
||||
#build_and_run(FunctionTest)
|
||||
#build_and_run(PreparedTest)
|
||||
build_and_run(PreparedTest)
|
||||
|
||||
find_package(PythonInterp REQUIRED)
|
||||
|
||||
|
@ -40,37 +40,38 @@ int main()
|
||||
|
||||
// empty parameter lists
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(t.alpha)>::type;
|
||||
using T = sqlpp::parameters_of<decltype(t.alpha)>;
|
||||
static_assert(std::is_same<T, std::tuple<>>::value, "type requirement");
|
||||
}
|
||||
|
||||
// single parameter
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(parameter(t.alpha))>::type;
|
||||
using T = sqlpp::parameters_of<decltype(parameter(t.alpha))>;
|
||||
static_assert(std::is_same<T, std::tuple<decltype(parameter(t.alpha))>>::value, "type requirement");
|
||||
}
|
||||
|
||||
// single parameter
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(parameter(t.alpha))>::type;
|
||||
using T = sqlpp::parameters_of<decltype(parameter(t.alpha))>;
|
||||
static_assert(std::is_same<T, std::tuple<decltype(parameter(t.alpha))>>::value, "type requirement");
|
||||
}
|
||||
|
||||
// single parameter in expression
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(t.alpha == parameter(t.alpha))>::type;
|
||||
using T = sqlpp::parameters_of<decltype(t.alpha == parameter(t.alpha))>;
|
||||
static_assert(std::is_same<T, std::tuple<decltype(parameter(t.alpha))>>::value, "type requirement");
|
||||
}
|
||||
|
||||
|
||||
// single parameter in larger expression
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(t.beta.like("%") and t.alpha == parameter(t.alpha) or t.gamma != false)>::type;
|
||||
using T = sqlpp::parameters_of<decltype(t.beta.like("%") and t.alpha == parameter(t.alpha) or t.gamma != false)>;
|
||||
static_assert(std::is_same<T, std::tuple<decltype(parameter(t.alpha))>>::value, "type requirement");
|
||||
}
|
||||
|
||||
// three parameters in expression
|
||||
{
|
||||
using T = typename sqlpp::detail::get_parameter_tuple<decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma))>::type;
|
||||
using T = sqlpp::parameters_of<decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma))>;
|
||||
static_assert(std::tuple_size<T>::value == 3, "type requirement");
|
||||
static_assert(std::is_same<T, std::tuple<decltype(parameter(t.beta)), decltype(parameter(t.alpha)),decltype(parameter(t.gamma))>>::value, "type requirement");
|
||||
}
|
||||
@ -78,11 +79,11 @@ int main()
|
||||
// OK, fine, now create a named parameter list from an expression
|
||||
{
|
||||
using Exp = decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma));
|
||||
using T = sqlpp::make_parameter_list_t<Exp>::type;
|
||||
using T = sqlpp::make_parameter_list_t<Exp>;
|
||||
T npl;
|
||||
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t, decltype(npl.beta)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.alpha)>::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.beta)>::_parameter_t, decltype(npl.beta)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.gamma)>::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
|
||||
}
|
||||
|
||||
// Wonderful, now take a look at the parameter list of a select
|
||||
@ -90,12 +91,12 @@ int main()
|
||||
auto s = select(all_of(t)).from(t).where(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma));
|
||||
auto p = db.prepare(s);
|
||||
using S = decltype(s);
|
||||
using T = sqlpp::make_parameter_list_t<S>::type;
|
||||
using T = sqlpp::make_parameter_list_t<S>;
|
||||
T npl;
|
||||
|
||||
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t, decltype(npl.beta)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.alpha)>::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.beta)>::_parameter_t, decltype(npl.beta)>::value, "type requirement");
|
||||
static_assert(std::is_same<typename sqlpp::value_type_of<decltype(t.gamma)>::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
|
||||
npl.alpha = 7;
|
||||
auto x = npl;
|
||||
x = npl;
|
||||
@ -104,6 +105,5 @@ int main()
|
||||
std::cerr << x.alpha << std::endl;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user