Add deleteBy methods in the class Mapper
This commit is contained in:
parent
a45bc0c0a7
commit
1ce440b7f3
|
@ -91,6 +91,12 @@ class Mapper
|
|||
const ExceptionCallback &ecb) noexcept;
|
||||
std::future<size_t> deleteFutureOne(const T &obj) noexcept;
|
||||
|
||||
size_t deleteBy(const Criteria &criteria) noexcept(false);
|
||||
void deleteBy(const Criteria &criteria,
|
||||
const CountCallback &rcb,
|
||||
const ExceptionCallback &ecb) noexcept;
|
||||
std::future<size_t> deleteFutureBy(const Criteria &criteria) noexcept;
|
||||
|
||||
private:
|
||||
DbClientPtr _client;
|
||||
std::string _limitString;
|
||||
|
@ -829,6 +835,93 @@ inline std::future<size_t> Mapper<T>::deleteFutureOne(const T &obj) noexcept
|
|||
return prom->get_future();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline size_t Mapper<T>::deleteBy(const Criteria &criteria) noexcept(false)
|
||||
{
|
||||
clear();
|
||||
static_assert(!std::is_same<typename T::PrimaryKeyType, void>::value, "No primary key in the table!");
|
||||
std::string sql = "delete from ";
|
||||
sql += T::tableName;
|
||||
|
||||
if (criteria)
|
||||
{
|
||||
sql += " where ";
|
||||
sql += criteria.criteriaString();
|
||||
sql = _client->replaceSqlPlaceHolder(sql, "$?");
|
||||
}
|
||||
|
||||
Result r(nullptr);
|
||||
{
|
||||
auto binder = *_client << sql;
|
||||
if (criteria)
|
||||
{
|
||||
criteria.outputArgs(binder);
|
||||
}
|
||||
binder << Mode::Blocking;
|
||||
binder >> [&r](const Result &result) {
|
||||
r = result;
|
||||
};
|
||||
binder.exec(); //Maybe throw exception;
|
||||
}
|
||||
return r.affectedRows();
|
||||
}
|
||||
template <typename T>
|
||||
inline void Mapper<T>::deleteBy(const Criteria &criteria,
|
||||
const CountCallback &rcb,
|
||||
const ExceptionCallback &ecb) noexcept
|
||||
{
|
||||
clear();
|
||||
static_assert(!std::is_same<typename T::PrimaryKeyType, void>::value, "No primary key in the table!");
|
||||
std::string sql = "delete from ";
|
||||
sql += T::tableName;
|
||||
|
||||
if (criteria)
|
||||
{
|
||||
sql += " where ";
|
||||
sql += criteria.criteriaString();
|
||||
sql = _client->replaceSqlPlaceHolder(sql, "$?");
|
||||
}
|
||||
|
||||
auto binder = *_client << sql;
|
||||
if (criteria)
|
||||
{
|
||||
criteria.outputArgs(binder);
|
||||
}
|
||||
binder >> [=](const Result &r) {
|
||||
rcb(r.affectedRows());
|
||||
};
|
||||
binder >> ecb;
|
||||
}
|
||||
template <typename T>
|
||||
inline std::future<size_t> Mapper<T>::deleteFutureBy(const Criteria &criteria) noexcept
|
||||
{
|
||||
clear();
|
||||
static_assert(!std::is_same<typename T::PrimaryKeyType, void>::value, "No primary key in the table!");
|
||||
std::string sql = "delete from ";
|
||||
sql += T::tableName;
|
||||
if (criteria)
|
||||
{
|
||||
sql += " where ";
|
||||
sql += criteria.criteriaString();
|
||||
sql = _client->replaceSqlPlaceHolder(sql, "$?");
|
||||
}
|
||||
auto binder = *_client << sql;
|
||||
if (criteria)
|
||||
{
|
||||
criteria.outputArgs(binder);
|
||||
}
|
||||
|
||||
std::shared_ptr<std::promise<size_t>> prom = std::make_shared<std::promise<size_t>>();
|
||||
binder >> [=](const Result &r) {
|
||||
prom->set_value(r.affectedRows());
|
||||
};
|
||||
binder >> [=](const std::exception_ptr &e) {
|
||||
prom->set_exception(e);
|
||||
};
|
||||
binder.exec();
|
||||
return prom->get_future();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Mapper<T> &Mapper<T>::limit(size_t limit)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue