diff --git a/orm_lib/inc/drogon/orm/Mapper.h b/orm_lib/inc/drogon/orm/Mapper.h index 2a85697b..e430f924 100644 --- a/orm_lib/inc/drogon/orm/Mapper.h +++ b/orm_lib/inc/drogon/orm/Mapper.h @@ -91,6 +91,12 @@ class Mapper const ExceptionCallback &ecb) noexcept; std::future 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 deleteFutureBy(const Criteria &criteria) noexcept; + private: DbClientPtr _client; std::string _limitString; @@ -784,8 +790,8 @@ inline size_t Mapper::deleteOne(const T &obj) noexcept(false) } template inline void Mapper::deleteOne(const T &obj, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept + const CountCallback &rcb, + const ExceptionCallback &ecb) noexcept { clear(); static_assert(!std::is_same::value, "No primary key in the table!"); @@ -829,6 +835,93 @@ inline std::future Mapper::deleteFutureOne(const T &obj) noexcept return prom->get_future(); } +template +inline size_t Mapper::deleteBy(const Criteria &criteria) noexcept(false) +{ + clear(); + static_assert(!std::is_same::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 +inline void Mapper::deleteBy(const Criteria &criteria, + const CountCallback &rcb, + const ExceptionCallback &ecb) noexcept +{ + clear(); + static_assert(!std::is_same::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 +inline std::future Mapper::deleteFutureBy(const Criteria &criteria) noexcept +{ + clear(); + static_assert(!std::is_same::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> prom = std::make_shared>(); + 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 inline Mapper &Mapper::limit(size_t limit) {