Can compile (very simple) custom queries and selects

This commit is contained in:
rbock
2014-11-03 17:52:02 +01:00
parent 514ed4139c
commit e79585b165
8 changed files with 123 additions and 32 deletions

View File

@@ -52,6 +52,8 @@ namespace sqlpp
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>;
using _recursive_traits = make_recursive_traits<Parts...>;
static void _check_consistency() {};
@@ -61,6 +63,12 @@ namespace sqlpp
return _methods_t::_run(db, *this);
}
template<typename Db>
auto _prepare(Db& db) const -> decltype(_methods_t::_prepare(db, *this))
{
return _methods_t::_prepare(db, *this);
}
custom_query_t(Parts... parts):
_parts(parts...)
{}

View File

@@ -56,6 +56,17 @@ namespace sqlpp
return static_cast<const _statement_t&>(*this);
}
// Execute
template<typename Db, typename Composite>
auto _run(Db& db, const Composite& composite) const
-> decltype(db.insert(composite))
{
Composite::_check_consistency();
static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run insert directly with parameters, use prepare instead");
return db.insert(composite);
}
template<typename Db>
auto _run(Db& db) const -> decltype(db.insert(this->_get_statement()))
{
@@ -65,14 +76,24 @@ namespace sqlpp
return db.insert(_get_statement());
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_insert_t<Db, _statement_t>
{
_statement_t::_check_consistency();
// Prepare
template<typename Db, typename Composite>
auto _prepare(Db& db, const Composite& composite) const
-> prepared_insert_t<Db, Composite>
{
Composite::_check_consistency();
return {{}, db.prepare_insert(_get_statement())};
}
return {{}, db.prepare_insert(composite)};
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_insert_t<Db, _statement_t>
{
_statement_t::_check_consistency();
return {{}, db.prepare_insert(_get_statement())};
}
};
};

View File

@@ -32,12 +32,12 @@
namespace sqlpp
{
template<typename Database, typename Select>
template<typename Database, typename Statement, typename Composite = Statement>
struct prepared_select_t
{
using _result_row_t = typename Select::template _result_row_t<Database>;
using _parameter_list_t = make_parameter_list_t<Select>;
using _dynamic_names_t = typename Select::_dynamic_names_t;
using _result_row_t = typename Statement::template _result_row_t<Database>;
using _parameter_list_t = make_parameter_list_t<Composite>;
using _dynamic_names_t = typename Statement::_dynamic_names_t;
using _prepared_statement_t = typename Database::_prepared_statement_t;
auto _run(Database& db) const

View File

@@ -56,6 +56,17 @@ namespace sqlpp
return static_cast<const _statement_t&>(*this);
}
// Execute
template<typename Db, typename Composite>
auto _run(Db& db, const Composite& composite) const
-> decltype(db.remove(composite))
{
Composite::_check_consistency();
static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run remove directly with parameters, use prepare instead");
return db.remove(composite);
}
template<typename Db>
auto _run(Db& db) const -> decltype(db.remove(this->_get_statement()))
{
@@ -65,14 +76,24 @@ namespace sqlpp
return db.remove(_get_statement());
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_remove_t<Db, _statement_t>
{
_statement_t::_check_consistency();
// Prepare
template<typename Db, typename Composite>
auto _prepare(Db& db, const Composite& composite) const
-> prepared_remove_t<Db, Composite>
{
Composite::_check_consistency();
return {{}, db.prepare_remove(_get_statement())};
}
return {{}, db.prepare_remove(composite)};
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_remove_t<Db, _statement_t>
{
_statement_t::_check_consistency();
return {{}, db.prepare_remove(_get_statement())};
}
};
};

View File

@@ -293,17 +293,36 @@ namespace sqlpp
}
// Execute
template<typename Db, typename S>
auto _run(Db& db, const S& s) const
-> result_t<decltype(db.select(s)), _result_row_t<Db>>
template<typename Db, typename Composite>
auto _run(Db& db, const Composite& composite) const
-> result_t<decltype(db.select(composite)), _result_row_t<Db>>
{
S::_check_consistency();
Composite::_check_consistency();
static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead");
return {db.select(s), get_dynamic_names()};
return {db.select(composite), get_dynamic_names()};
}
template<typename Db>
auto _run(Db& db) const
-> result_t<decltype(db.select(_get_statement())), _result_row_t<Db>>
{
_statement_t::_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()};
}
// Prepare
template<typename Db, typename Composite>
auto _prepare(Db& db, const Composite& composite) const
-> prepared_select_t<Db, _statement_t, Composite>
{
Composite::_check_consistency();
return {make_parameter_list_t<Composite>{}, get_dynamic_names(), db.prepare_select(composite)};
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_select_t<Db, _statement_t>

View File

@@ -56,6 +56,17 @@ namespace sqlpp
return static_cast<const _statement_t&>(*this);
}
// Execute
template<typename Db, typename Composite>
auto _run(Db& db, const Composite& composite) const
-> decltype(db.update(composite))
{
Composite::_check_consistency();
static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run update directly with parameters, use prepare instead");
return db.update(composite);
}
template<typename Db>
auto _run(Db& db) const -> decltype(db.update(this->_get_statement()))
{
@@ -65,14 +76,24 @@ namespace sqlpp
return db.update(_get_statement());
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_update_t<Db, _statement_t>
{
_statement_t::_check_consistency();
// Prepare
template<typename Db, typename Composite>
auto _prepare(Db& db, const Composite& composite) const
-> prepared_update_t<Db, Composite>
{
Composite::_check_consistency();
return {{}, db.prepare_update(_get_statement())};
}
return {{}, db.prepare_update(composite)};
}
template<typename Db>
auto _prepare(Db& db) const
-> prepared_update_t<Db, _statement_t>
{
_statement_t::_check_consistency();
return {{}, db.prepare_update(_get_statement())};
}
};
};

View File

@@ -12,7 +12,7 @@ build_and_run(CustomQueryTest)
#build_and_run(InsertTest)
#build_and_run(RemoveTest)
#build_and_run(UpdateTest)
#build_and_run(SelectTest)
build_and_run(SelectTest)
#build_and_run(SelectTypeTest)
#build_and_run(FunctionTest)
#build_and_run(PreparedTest)

View File

@@ -38,8 +38,9 @@ int main()
test::TabBar t;
auto c = custom_query(select(all_of(t)).from(t));
//std::cerr << serialize(c, printer).str() << std::endl;
db(c);
auto p = db.prepare(custom_query(select(all_of(t)).from(t).where(t.alpha > sqlpp::parameter(t.alpha))));
return 0;
}