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

Added a few checks to insert, update and remove

This commit is contained in:
rbock 2014-04-22 22:21:44 +02:00
parent d342973ef4
commit f47ef75c24
4 changed files with 53 additions and 3 deletions

View File

@ -126,10 +126,22 @@ namespace sqlpp
return _parameter_list_t::size::value;
}
template<typename A>
struct is_table_subset_of_table
{
static constexpr bool value = ::sqlpp::detail::is_subset_of<typename A::_table_set, typename _table_t::_table_set>::value;
};
void _check_consistency() const
{
// FIXME: Read up on what is allowed/prohibited in INSERT
}
template<typename Database>
std::size_t _run(Database& db) const
{
#warning add _check_consistency here and for remove and update
_check_consistency();
static_assert(_get_static_no_of_parameters() == 0, "cannot run insert directly with parameters, use prepare instead");
return db.insert(*this);
}
@ -138,6 +150,8 @@ namespace sqlpp
auto _prepare(Database& db) const
-> prepared_insert_t<Database, insert_t>
{
_check_consistency();
return {{}, db.prepare_insert(*this)};
}

View File

@ -132,11 +132,26 @@ namespace sqlpp
return _parameter_list_t::size::value;
}
template<typename A>
struct is_table_subset_of_table
{
static constexpr bool value = ::sqlpp::detail::is_subset_of<typename A::_table_set, typename _table_t::_table_set>::value;
};
void _check_consistency() const
{
static_assert(is_where_t<_where_t>::value, "cannot run update without having a where condition, use .where(true) to update all rows");
// FIXME: Read more details about what is allowed and what not in SQL DELETE
static_assert(is_table_subset_of_table<_where_t>::value, "where requires additional tables");
}
template<typename Database>
std::size_t _run(Database& db) const
{
_check_consistency();
static_assert(_get_static_no_of_parameters() == 0, "cannot run remove directly with parameters, use prepare instead");
static_assert(is_where_t<_where_t>::value, "cannot run remove without having a where condition, use .where(true) to remove all rows");
return db.remove(*this);
}
@ -144,7 +159,8 @@ namespace sqlpp
auto _prepare(Database& db) const
-> prepared_remove_t<Database, remove_t>
{
static_assert(is_where_t<_where_t>::value, "cannot run remove without having a where condition, use .where(true) to remove all rows");
_check_consistency();
return {{}, db.prepare_remove(*this)};
}

View File

@ -256,6 +256,8 @@ namespace sqlpp
void _check_consistency() const
{
static_assert(is_select_column_list_t<_column_list_t>::value, "no columns selected");
static_assert(is_table_subset_of_from<_flag_list_t>::value, "flags require additional tables in from()");
static_assert(is_table_subset_of_from<_column_list_t>::value, "selected columns require additional tables in from()");
static_assert(is_table_subset_of_from<_where_t>::value, "where() expression requires additional tables in from()");

View File

@ -130,9 +130,25 @@ namespace sqlpp
return _parameter_list_t::size::value;
}
template<typename A>
struct is_table_subset_of_table
{
static constexpr bool value = ::sqlpp::detail::is_subset_of<typename A::_table_set, typename _table_t::_table_set>::value;
};
void _check_consistency() const
{
static_assert(is_where_t<_where_t>::value, "cannot run update without having a where condition, use .where(true) to update all rows");
static_assert(is_table_subset_of_table<_update_list_t>::value, "updates require additional tables");
static_assert(is_table_subset_of_table<_where_t>::value, "where requires additional tables");
}
template<typename Database>
std::size_t _run(Database& db) const
{
_check_consistency();
static_assert(_get_static_no_of_parameters() == 0, "cannot run update directly with parameters, use prepare instead");
return db.update(*this);
}
@ -141,6 +157,8 @@ namespace sqlpp
auto _prepare(Database& db) const
-> prepared_update_t<Database, update_t>
{
_check_consistency();
return {{}, db.prepare_update(*this)};
}