2013-08-14 04:43:10 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Roland Bock
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SQLPP_REMOVE_H
|
|
|
|
#define SQLPP_REMOVE_H
|
|
|
|
|
|
|
|
#include <sqlpp11/type_traits.h>
|
2014-01-09 05:14:16 +08:00
|
|
|
#include <sqlpp11/parameter_list.h>
|
|
|
|
#include <sqlpp11/prepared_remove.h>
|
2014-01-18 22:50:16 +08:00
|
|
|
#include <sqlpp11/vendor/noop.h>
|
2014-02-09 04:06:23 +08:00
|
|
|
#include <sqlpp11/vendor/single_table.h>
|
2014-01-18 22:50:16 +08:00
|
|
|
#include <sqlpp11/vendor/using.h>
|
|
|
|
#include <sqlpp11/vendor/where.h>
|
2014-02-08 04:25:23 +08:00
|
|
|
#include <sqlpp11/vendor/policy_update.h>
|
2014-03-27 15:37:05 +08:00
|
|
|
#include <sqlpp11/detail/arg_selector.h>
|
2013-08-14 04:43:10 +08:00
|
|
|
|
|
|
|
namespace sqlpp
|
|
|
|
{
|
2014-02-08 04:25:23 +08:00
|
|
|
namespace detail
|
|
|
|
{
|
2014-02-09 04:06:23 +08:00
|
|
|
template<typename Table, typename Using, typename Where>
|
2014-02-08 04:25:23 +08:00
|
|
|
struct check_remove_t
|
|
|
|
{
|
|
|
|
static_assert(is_where_t<Where>::value, "cannot run remove without having a where condition, use .where(true) to remove all rows");
|
|
|
|
static constexpr bool value = true;
|
|
|
|
};
|
|
|
|
}
|
2013-08-14 04:43:10 +08:00
|
|
|
|
2014-02-10 01:14:52 +08:00
|
|
|
// REMOVE
|
|
|
|
template<typename Database,
|
|
|
|
typename Table = vendor::no_single_table_t,
|
|
|
|
typename Using = vendor::no_using_t,
|
|
|
|
typename Where = vendor::no_where_t
|
|
|
|
>
|
|
|
|
struct remove_t
|
2014-02-08 04:25:23 +08:00
|
|
|
{
|
2014-02-10 01:14:52 +08:00
|
|
|
using _database_t = Database;
|
|
|
|
using _is_dynamic = typename std::conditional<std::is_same<Database, void>::value, std::false_type, std::true_type>::type;
|
|
|
|
|
|
|
|
template<typename Needle, typename Replacement, typename... Policies>
|
|
|
|
struct _policies_update_impl
|
|
|
|
{
|
|
|
|
using type = remove_t<Database, vendor::policy_update_t<Policies, Needle, Replacement>...>;
|
|
|
|
};
|
|
|
|
|
2014-02-08 04:25:23 +08:00
|
|
|
template<typename Needle, typename Replacement>
|
2014-02-10 01:14:52 +08:00
|
|
|
using _policies_update_t = typename _policies_update_impl<Needle, Replacement, Table, Using, Where>::type;
|
2013-08-14 04:43:10 +08:00
|
|
|
|
2014-02-10 01:14:52 +08:00
|
|
|
using _parameter_tuple_t = std::tuple<Table, Using, Where>;
|
2014-01-09 05:14:16 +08:00
|
|
|
using _parameter_list_t = typename make_parameter_list_t<remove_t>::type;
|
|
|
|
|
2014-02-10 01:14:52 +08:00
|
|
|
// Constructors
|
2014-02-08 04:25:23 +08:00
|
|
|
remove_t()
|
|
|
|
{}
|
2013-08-14 04:43:10 +08:00
|
|
|
|
2014-03-27 15:37:05 +08:00
|
|
|
template<typename Statement, typename T>
|
|
|
|
remove_t(Statement s, T t):
|
|
|
|
_table(detail::arg_selector<Table>::_(s._table, t)),
|
|
|
|
_using(detail::arg_selector<Using>::_(s._using, t)),
|
|
|
|
_where(detail::arg_selector<Where>::_(s._where, t))
|
2014-02-08 04:25:23 +08:00
|
|
|
{}
|
2013-10-04 02:53:55 +08:00
|
|
|
|
2014-02-09 04:06:23 +08:00
|
|
|
remove_t(const remove_t&) = default;
|
|
|
|
remove_t(remove_t&&) = default;
|
|
|
|
remove_t& operator=(const remove_t&) = default;
|
|
|
|
remove_t& operator=(remove_t&&) = default;
|
2014-02-08 04:25:23 +08:00
|
|
|
~remove_t() = default;
|
2013-09-30 13:46:50 +08:00
|
|
|
|
2014-02-10 01:14:52 +08:00
|
|
|
// type update functions
|
|
|
|
template<typename... Args>
|
|
|
|
auto using_(Args... args)
|
|
|
|
-> _policies_update_t<vendor::no_using_t, vendor::using_t<void, Args...>>
|
|
|
|
{
|
2014-02-10 06:30:30 +08:00
|
|
|
static_assert(is_noop_t<Using>::value, "cannot call using_()/dynamic_using() twice");
|
2014-02-11 00:56:16 +08:00
|
|
|
return { *this, vendor::using_t<void, Args...>{args...} };
|
2014-02-10 01:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
auto dynamic_using(Args... args)
|
|
|
|
-> _policies_update_t<vendor::no_using_t, vendor::using_t<_database_t, Args...>>
|
|
|
|
{
|
2014-02-10 06:30:30 +08:00
|
|
|
static_assert(is_noop_t<Using>::value, "cannot call using_()/dynamic_using() twice");
|
2014-02-10 01:14:52 +08:00
|
|
|
static_assert(not std::is_same<_database_t, void>::value, "dynamic_using must not be called in a static statement");
|
2014-02-11 00:56:16 +08:00
|
|
|
return { *this, vendor::using_t<_database_t, Args...>{args...} };
|
2014-02-10 01:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
auto where(Args... args)
|
|
|
|
-> _policies_update_t<vendor::no_where_t, vendor::where_t<void, Args...>>
|
|
|
|
{
|
2014-02-10 06:30:30 +08:00
|
|
|
static_assert(is_noop_t<Where>::value, "cannot call where()/dynamic_where() twice");
|
2014-02-11 00:56:16 +08:00
|
|
|
return { *this, vendor::where_t<void, Args...>{args...} };
|
2014-02-10 01:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
auto dynamic_where(Args... args)
|
|
|
|
-> _policies_update_t<vendor::no_where_t, vendor::where_t<_database_t, Args...>>
|
|
|
|
{
|
2014-02-10 06:30:30 +08:00
|
|
|
static_assert(is_noop_t<Where>::value, "cannot call where()/dynamic_where() twice");
|
2014-02-10 01:14:52 +08:00
|
|
|
static_assert(not std::is_same<_database_t, void>::value, "dynamic_where must not be called in a static statement");
|
2014-02-11 00:56:16 +08:00
|
|
|
return { *this, vendor::where_t<_database_t, Args...>{args...} };
|
2014-02-10 01:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// value adding methods
|
|
|
|
template<typename... Args>
|
|
|
|
void add_using(Args... args)
|
|
|
|
{
|
|
|
|
static_assert(is_using_t<Using>::value, "cannot call add_using() before dynamic_using()");
|
|
|
|
static_assert(is_dynamic_t<Using>::value, "cannot call add_using() before dynamic_using()");
|
|
|
|
return _using.add_using(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void add_where(Args... args)
|
|
|
|
{
|
|
|
|
static_assert(is_where_t<Where>::value, "cannot call add_where() before dynamic_where()");
|
|
|
|
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() before dynamic_where()");
|
|
|
|
return _where.add_where(*this, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
// run and prepare
|
2014-01-09 05:14:16 +08:00
|
|
|
static constexpr size_t _get_static_no_of_parameters()
|
|
|
|
{
|
|
|
|
return _parameter_list_t::size::value;
|
|
|
|
}
|
|
|
|
|
2014-01-19 05:40:13 +08:00
|
|
|
size_t _get_no_of_parameters() const
|
2014-01-10 05:33:23 +08:00
|
|
|
{
|
2014-01-29 01:03:14 +08:00
|
|
|
return _parameter_list_t::size::value;
|
2014-01-10 05:33:23 +08:00
|
|
|
}
|
|
|
|
|
2013-08-14 04:43:10 +08:00
|
|
|
template<typename Db>
|
2014-01-29 20:11:11 +08:00
|
|
|
std::size_t _run(Db& db) const
|
2013-08-14 04:43:10 +08:00
|
|
|
{
|
2014-01-09 05:14:16 +08:00
|
|
|
static_assert(_get_static_no_of_parameters() == 0, "cannot run remove directly with parameters, use prepare instead");
|
2014-02-10 01:14:52 +08:00
|
|
|
//static_assert(detail::check_remove_t<Policies...>::value, "Cannot run this remove expression");
|
2014-01-09 05:14:16 +08:00
|
|
|
return db.remove(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Db>
|
2014-01-29 20:11:11 +08:00
|
|
|
auto _prepare(Db& db) const
|
2014-02-16 20:16:09 +08:00
|
|
|
-> prepared_remove_t<Db, remove_t>
|
2014-01-09 05:14:16 +08:00
|
|
|
{
|
2014-02-10 01:14:52 +08:00
|
|
|
//static_assert(detail::check_remove_t<Policies...>::value, "Cannot run this remove expression");
|
2014-01-09 05:14:16 +08:00
|
|
|
return {{}, db.prepare_remove(*this)};
|
2013-08-14 04:43:10 +08:00
|
|
|
}
|
2014-02-10 01:14:52 +08:00
|
|
|
|
|
|
|
Table _table;
|
|
|
|
Using _using;
|
|
|
|
Where _where;
|
2013-08-14 04:43:10 +08:00
|
|
|
};
|
|
|
|
|
2014-01-18 23:10:46 +08:00
|
|
|
namespace vendor
|
|
|
|
{
|
2014-02-08 04:25:23 +08:00
|
|
|
template<typename Context, typename Database, typename... Policies>
|
2014-02-19 00:45:08 +08:00
|
|
|
struct serializer_t<Context, remove_t<Database, Policies...>>
|
2014-01-15 15:24:42 +08:00
|
|
|
{
|
2014-02-08 04:25:23 +08:00
|
|
|
using T = remove_t<Database, Policies...>;
|
2014-01-18 23:10:46 +08:00
|
|
|
|
|
|
|
static Context& _(const T& t, Context& context)
|
|
|
|
{
|
2014-02-16 20:16:09 +08:00
|
|
|
context << "DELETE FROM ";
|
2014-02-19 00:45:08 +08:00
|
|
|
serialize(t._table, context);
|
|
|
|
serialize(t._using, context);
|
|
|
|
serialize(t._where, context);
|
2014-01-18 23:10:46 +08:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-01-15 15:24:42 +08:00
|
|
|
|
2013-08-14 04:43:10 +08:00
|
|
|
template<typename Table>
|
2014-02-09 04:06:23 +08:00
|
|
|
constexpr auto remove_from(Table table)
|
2014-02-10 01:14:52 +08:00
|
|
|
-> remove_t<void, vendor::single_table_t<void, Table>>
|
2013-09-30 13:46:50 +08:00
|
|
|
{
|
2014-02-10 01:14:52 +08:00
|
|
|
return { remove_t<void>(), vendor::single_table_t<void, Table>{table} };
|
2013-09-30 13:46:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-08 04:25:23 +08:00
|
|
|
template<typename Database, typename Table>
|
2014-02-09 04:06:23 +08:00
|
|
|
constexpr auto dynamic_remove_from(const Database&, Table table)
|
2014-02-10 01:14:52 +08:00
|
|
|
-> remove_t<Database, vendor::single_table_t<void, Table>>
|
2013-08-14 04:43:10 +08:00
|
|
|
{
|
2014-02-10 01:14:52 +08:00
|
|
|
return { remove_t<Database>(), vendor::single_table_t<void, Table>{table} };
|
2013-08-14 04:43:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|