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

Turned remove into a generic statement

This commit is contained in:
rbock 2014-06-03 09:12:27 +02:00
parent 8d16b6ab2d
commit c866f31ea5
7 changed files with 100 additions and 289 deletions

View File

@ -38,108 +38,6 @@
namespace sqlpp namespace sqlpp
{ {
#if 0
template<typename Db, typename... Policies>
struct insert_t;
namespace detail
{
template<typename Db, typename... Policies>
struct insert_policies_t
{
using _database_t = Db;
using _statement_t = insert_t<Db, Policies...>;
struct _methods_t: public Policies::template _methods_t<insert_policies_t>...
{};
template<typename Needle, typename Replacement>
struct _policies_update_t
{
static_assert(detail::is_element_of<Needle, make_type_set_t<Policies...>>::value, "policies update for non-policy class detected");
using type = insert_t<Db, vendor::policy_update_t<Policies, Needle, Replacement>...>;
};
template<typename Needle, typename Replacement>
using _new_statement_t = typename _policies_update_t<Needle, Replacement>::type;
using _all_required_tables = detail::make_joined_set_t<required_tables_of<Policies>...>;
using _all_provided_tables = detail::make_joined_set_t<provided_tables_of<Policies>...>;
using _all_extra_tables = detail::make_joined_set_t<extra_tables_of<Policies>...>;
using _known_tables = detail::make_joined_set_t<_all_provided_tables, _all_extra_tables>;
template<typename Expression>
using _no_unknown_tables = detail::is_subset_of<required_tables_of<Expression>, _known_tables>;
// The tables not covered by the from.
using _required_tables = detail::make_difference_set_t<
_all_required_tables,
_all_provided_tables // Hint: extra_tables are not used here because they are just a helper for dynamic .add_*()
>;
using _traits = make_traits<no_value_t>; // FIXME
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<>;
};
};
}
// INSERT
template<typename Db, typename... Policies>
struct insert_t:
public Policies...,
public detail::insert_policies_t<Db, Policies...>::_methods_t
{
using _policies_t = typename detail::insert_policies_t<Db, Policies...>;
using _database_t = typename _policies_t::_database_t;
using _is_dynamic = typename std::conditional<std::is_same<_database_t, void>::value, std::false_type, std::true_type>::type;
using _parameter_list_t = typename make_parameter_list_t<insert_t>::type;
// Constructors
constexpr insert_t()
{}
template<typename Statement, typename Term>
insert_t(Statement statement, Term term):
Policies(detail::pick_arg<Policies>(statement, term))...
{}
insert_t(const insert_t&) = default;
insert_t(insert_t&&) = default;
insert_t& operator=(const insert_t&) = default;
insert_t& operator=(insert_t&&) = default;
~insert_t() = default;
};
namespace vendor
{
template<typename Context, typename Database, typename... Policies>
struct serializer_t<Context, insert_t<Database, Policies...>>
{
using T = insert_t<Database, Policies...>;
static Context& _(const T& t, Context& context)
{
context << "INSERT INTO ";
using swallow = int[];
(void) swallow{(serialize(static_cast<const Policies&>(t), context), 0)...};
return context;
}
};
}
#endif
template<typename Database> template<typename Database>
using blank_insert_t = statement_t<Database, using blank_insert_t = statement_t<Database,
vendor::no_single_table_t, vendor::no_single_table_t,

View File

@ -28,6 +28,7 @@
#define SQLPP_PARAMETER_LIST_H #define SQLPP_PARAMETER_LIST_H
#include <tuple> #include <tuple>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/vendor/wrong.h> #include <sqlpp11/vendor/wrong.h>
#include <sqlpp11/detail/index_sequence.h> #include <sqlpp11/detail/index_sequence.h>

View File

@ -27,6 +27,7 @@
#ifndef SQLPP_REMOVE_H #ifndef SQLPP_REMOVE_H
#define SQLPP_REMOVE_H #define SQLPP_REMOVE_H
#include <sqlpp11/statement.h>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/parameter_list.h> #include <sqlpp11/parameter_list.h>
#include <sqlpp11/prepared_remove.h> #include <sqlpp11/prepared_remove.h>
@ -36,179 +37,39 @@
#include <sqlpp11/vendor/extra_tables.h> #include <sqlpp11/vendor/extra_tables.h>
#include <sqlpp11/vendor/using.h> #include <sqlpp11/vendor/using.h>
#include <sqlpp11/vendor/where.h> #include <sqlpp11/vendor/where.h>
#include <sqlpp11/vendor/policy_update.h>
#include <sqlpp11/detail/get_last.h>
#include <sqlpp11/detail/pick_arg.h>
namespace sqlpp namespace sqlpp
{ {
template<typename Db, typename... Policies>
struct remove_t;
namespace detail
{
template<typename Db = void, typename... Policies>
struct remove_policies_t
{
using _database_t = Db;
using _statement_t = remove_t<Db, Policies...>;
struct _methods_t: public Policies::template _methods_t<remove_policies_t>...
{};
template<typename Needle, typename Replacement>
struct _policies_update_t
{
static_assert(detail::is_element_of<Needle, make_type_set_t<Policies...>>::value, "policies update for non-policy class detected");
using type = remove_t<Db, vendor::policy_update_t<Policies, Needle, Replacement>...>;
};
template<typename Needle, typename Replacement>
using _new_statement_t = typename _policies_update_t<Needle, Replacement>::type;
using _all_required_tables = detail::make_joined_set_t<required_tables_of<Policies>...>;
using _all_provided_tables = detail::make_joined_set_t<provided_tables_of<Policies>...>;
using _all_extra_tables = detail::make_joined_set_t<extra_tables_of<Policies>...>;
using _known_tables = detail::make_joined_set_t<_all_provided_tables, _all_extra_tables>;
template<typename Expression>
using _no_unknown_tables = detail::is_subset_of<required_tables_of<Expression>, _known_tables>;
// The tables not covered by the from.
using _required_tables = detail::make_difference_set_t<
_all_required_tables,
_all_provided_tables // Hint: extra_tables are not used here because they are just a helper for dynamic .add_*()
>;
using _traits = make_traits<no_value_t>; // FIXME
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<>;
};
};
}
// REMOVE
template<typename Db, typename... Policies>
struct remove_t:
public Policies...,
public detail::remove_policies_t<Db, Policies...>::_methods_t
{
using _policies_t = typename detail::remove_policies_t<Db, Policies...>;
using _database_t = typename _policies_t::_database_t;
using _is_dynamic = typename std::conditional<std::is_same<_database_t, void>::value, std::false_type, std::true_type>::type;
using _parameter_list_t = typename make_parameter_list_t<remove_t>::type;
// Constructors
constexpr remove_t()
{}
template<typename Statement, typename Term>
remove_t(Statement statement, Term term):
Policies(detail::pick_arg<Policies>(statement, term))...
{}
remove_t(const remove_t&) = default;
remove_t(remove_t&&) = default;
remove_t& operator=(const remove_t&) = default;
remove_t& operator=(remove_t&&) = default;
~remove_t() = default;
// run and prepare
static constexpr size_t _get_static_no_of_parameters()
{
return _parameter_list_t::size::value;
}
size_t _get_no_of_parameters() const
{
return _parameter_list_t::size::value;
}
void _check_consistency() const
{
#warning reactivate checks
#if 0
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");
#endif
}
template<typename Database> template<typename Database>
std::size_t _run(Database& db) const using blank_remove_t = statement_t<Database,
{ #warning Make this another policy that can take several table (cannot be select's from, since it has to be a result provider)
_check_consistency();
static_assert(_get_static_no_of_parameters() == 0, "cannot run remove directly with parameters, use prepare instead");
return db.remove(*this);
}
template<typename Database>
auto _prepare(Database& db) const
-> prepared_remove_t<Database, remove_t>
{
_check_consistency();
return {{}, db.prepare_remove(*this)};
}
};
namespace vendor
{
template<typename Context, typename Database, typename... Policies>
struct serializer_t<Context, remove_t<Database, Policies...>>
{
using T = remove_t<Database, Policies...>;
static Context& _(const T& t, Context& context)
{
context << "DELETE FROM ";
using swallow = int[];
(void) swallow{(serialize(static_cast<const Policies&>(t), context), 0)...};
return context;
}
};
}
template<typename Database>
using blank_remove_t = remove_t<Database,
vendor::no_single_table_t, vendor::no_single_table_t,
vendor::no_using_t, vendor::no_using_t,
vendor::no_extra_tables_t, vendor::no_extra_tables_t,
vendor::no_where_t>; vendor::no_where_t>;
constexpr auto remove() auto remove()
-> blank_remove_t<void> -> blank_remove_t<void>
{ {
return { blank_remove_t<void>() }; return { blank_remove_t<void>() };
} }
template<typename Table> template<typename Table>
constexpr auto remove_from(Table table) auto remove_from(Table table)
-> decltype(blank_remove_t<void>().from(table)) -> decltype(blank_remove_t<void>().from(table))
{ {
return { blank_remove_t<void>().from(table) }; return { blank_remove_t<void>().from(table) };
} }
template<typename Database> template<typename Database>
constexpr auto dynamic_remove(const Database&) auto dynamic_remove(const Database&)
-> decltype(blank_remove_t<Database>()) -> decltype(blank_remove_t<Database>())
{ {
return { blank_remove_t<Database>() }; return { blank_remove_t<Database>() };
} }
template<typename Database, typename Table> template<typename Database, typename Table>
constexpr auto dynamic_remove_from(const Database&, Table table) auto dynamic_remove_from(const Database&, Table table)
-> decltype(blank_remove_t<Database>().from(table)) -> decltype(blank_remove_t<Database>().from(table))
{ {
return { blank_remove_t<Database>().from(table) }; return { blank_remove_t<Database>().from(table) };

View File

@ -30,6 +30,8 @@
#include <sqlpp11/result.h> #include <sqlpp11/result.h>
#include <sqlpp11/parameter_list.h> #include <sqlpp11/parameter_list.h>
#include <sqlpp11/prepared_select.h> #include <sqlpp11/prepared_select.h>
#include <sqlpp11/serialize.h>
#include <sqlpp11/vendor/noop.h>
#include <sqlpp11/vendor/policy_update.h> #include <sqlpp11/vendor/policy_update.h>
#include <sqlpp11/vendor/serializer.h> #include <sqlpp11/vendor/serializer.h>

View File

@ -37,6 +37,24 @@ namespace sqlpp
{ {
namespace vendor namespace vendor
{ {
// USING DATA
template<typename Database, typename... Tables>
struct using_data_t
{
using_data_t(Tables... tables):
_tables(tables...)
{}
using_data_t(const using_data_t&) = default;
using_data_t(using_data_t&&) = default;
using_data_t& operator=(const using_data_t&) = default;
using_data_t& operator=(using_data_t&&) = default;
~using_data_t() = default;
std::tuple<Tables...> _tables;
vendor::interpretable_list_t<Database> _dynamic_tables;
};
// USING // USING
template<typename Database, typename... Tables> template<typename Database, typename... Tables>
struct using_t struct using_t
@ -52,53 +70,95 @@ namespace sqlpp
static_assert(::sqlpp::detail::all_t<is_table_t<Tables>::value...>::value, "at least one argument is not an table in using()"); static_assert(::sqlpp::detail::all_t<is_table_t<Tables>::value...>::value, "at least one argument is not an table in using()");
using_t& _using() { return *this; } // Data
using _data_t = using_data_t<Database, Tables...>;
using_t(Tables... tables): // Member implementation with data and methods
_tables(tables...) template <typename Policies>
{} struct _impl_t
using_t(const using_t&) = default;
using_t(using_t&&) = default;
using_t& operator=(const using_t&) = default;
using_t& operator=(using_t&&) = default;
~using_t() = default;
template<typename Policies>
struct _methods_t
{ {
template<typename Table> template<typename Table>
void add_using(Table table) void add(Table table)
{ {
static_assert(_is_dynamic::value, "add_using must not be called for static using()"); static_assert(_is_dynamic::value, "add must not be called for static using()");
static_assert(is_table_t<Table>::value, "invalid table argument in add_using()"); static_assert(is_table_t<Table>::value, "invalid table argument in add()");
using ok = ::sqlpp::detail::all_t<_is_dynamic::value, is_table_t<Table>::value>; using ok = ::sqlpp::detail::all_t<_is_dynamic::value, is_table_t<Table>::value>;
_add_using_impl(table, ok()); // dispatch to prevent compile messages after the static_assert _add_impl(table, ok()); // dispatch to prevent compile messages after the static_assert
} }
private: private:
template<typename Table> template<typename Table>
void _add_using_impl(Table table, const std::true_type&) void _add_impl(Table table, const std::true_type&)
{ {
return static_cast<typename Policies::_statement_t*>(this)->_using()._dynamic_tables.emplace_back(table); return _data._dynamic_tables.emplace_back(table);
} }
template<typename Table> template<typename Table>
void _add_using_impl(Table table, const std::false_type&); void _add_impl(Table table, const std::false_type&);
public:
_data_t _data;
}; };
// Member template for adding the named member to a statement
template<typename Policies>
struct _member_t
{
using _data_t = using_data_t<Database, Tables...>;
std::tuple<Tables...> _tables; _impl_t<Policies> using_;
vendor::interpretable_list_t<Database> _dynamic_tables; _impl_t<Policies>& operator()() { return using_; }
const _impl_t<Policies>& operator()() const { return using_; }
template<typename T>
static auto _get_member(T t) -> decltype(t.using_)
{
return t.using_;
}
}; };
// Additional methods for the statement
template<typename Policies>
struct _methods_t
{
};
};
// NO USING YET
struct no_using_t struct no_using_t
{ {
using _traits = make_traits<no_value_t, ::sqlpp::tag::where>; using _traits = make_traits<no_value_t, ::sqlpp::tag::where>;
using _recursive_traits = make_recursive_traits<>; using _recursive_traits = make_recursive_traits<>;
// Data
using _data_t = no_data_t;
// Member implementation with data and methods
template<typename Policies>
struct _impl_t
{
_data_t _data;
};
// Member template for adding the named member to a statement
template<typename Policies>
struct _member_t
{
using _data_t = no_data_t;
_impl_t<Policies> no_using;
_impl_t<Policies>& operator()() { return no_using; }
const _impl_t<Policies>& operator()() const { return no_using; }
template<typename T>
static auto _get_member(T t) -> decltype(t.no_using)
{
return t.no_using;
}
};
template<typename Policies> template<typename Policies>
struct _methods_t struct _methods_t
{ {
@ -110,7 +170,7 @@ namespace sqlpp
auto using_(Args... args) auto using_(Args... args)
-> _new_statement_t<using_t<void, Args...>> -> _new_statement_t<using_t<void, Args...>>
{ {
return { *static_cast<typename Policies::_statement_t*>(this), using_t<void, Args...>{args...} }; return { *static_cast<typename Policies::_statement_t*>(this), using_data_t<void, Args...>{args...} };
} }
template<typename... Args> template<typename... Args>
@ -118,16 +178,16 @@ namespace sqlpp
-> _new_statement_t<using_t<_database_t, Args...>> -> _new_statement_t<using_t<_database_t, Args...>>
{ {
static_assert(not std::is_same<_database_t, void>::value, "dynamic_using must not be called in a static statement"); static_assert(not std::is_same<_database_t, void>::value, "dynamic_using must not be called in a static statement");
return { *static_cast<typename Policies::_statement_t*>(this), vendor::using_t<_database_t, Args...>{args...} }; return { *static_cast<typename Policies::_statement_t*>(this), using_data_t<_database_t, Args...>{args...} };
} }
}; };
}; };
// Interpreters // Interpreters
template<typename Context, typename Database, typename... Tables> template<typename Context, typename Database, typename... Tables>
struct serializer_t<Context, using_t<Database, Tables...>> struct serializer_t<Context, using_data_t<Database, Tables...>>
{ {
using T = using_t<Database, Tables...>; using T = using_data_t<Database, Tables...>;
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
@ -141,17 +201,6 @@ namespace sqlpp
return context; return context;
} }
}; };
template<typename Context>
struct serializer_t<Context, no_using_t>
{
using T = no_using_t;
static Context& _(const T& t, Context& context)
{
return context;
}
};
} }
} }

View File

@ -7,8 +7,8 @@ macro (build_and_run arg)
endmacro () endmacro ()
#build_and_run(InterpretTest) #build_and_run(InterpretTest)
build_and_run(InsertTest) #build_and_run(InsertTest)
#build_and_run(RemoveTest) build_and_run(RemoveTest)
#build_and_run(UpdateTest) #build_and_run(UpdateTest)
#build_and_run(SelectTest) #build_and_run(SelectTest)
#build_and_run(SelectTypeTest) #build_and_run(SelectTypeTest)

View File

@ -60,8 +60,8 @@ int main()
serialize(remove_from(t).where(t.beta != "transparent"), printer).str(); serialize(remove_from(t).where(t.beta != "transparent"), printer).str();
serialize(remove_from(t).using_(t), printer).str(); serialize(remove_from(t).using_(t), printer).str();
auto r = dynamic_remove_from(db, t).dynamic_using().dynamic_where(); auto r = dynamic_remove_from(db, t).dynamic_using().dynamic_where();
r.add_using(t); r.using_.add(t);
r.add_where(t.beta != "transparent"); r.where.add(t.beta != "transparent");
serialize(r, printer).str(); serialize(r, printer).str();
db(r); db(r);