diff --git a/orm_lib/inc/drogon/orm/Mapper.h b/orm_lib/inc/drogon/orm/Mapper.h index 94c488db..ac7dbda8 100644 --- a/orm_lib/inc/drogon/orm/Mapper.h +++ b/orm_lib/inc/drogon/orm/Mapper.h @@ -29,6 +29,20 @@ enum class SortOrder ASC, DESC }; +namespace internal +{ +template +struct Traits +{ + typedef typename T::PrimaryKeyType type; +}; +template +struct Traits +{ + typedef int type; +}; +} // namespace internal + template class Mapper { @@ -45,11 +59,15 @@ class Mapper Mapper(const DbClientPtr &client) : _client(client) {} - T findByPrimaryKey(const typename T::PrimaryKeyType &key) noexcept(false); - void findByPrimaryKey(const typename T::PrimaryKeyType &key, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept; - std::future findFutureByPrimaryKey(const typename T::PrimaryKeyType &key) noexcept; + typedef typename internal::Traits::value>::type TraitsPKType; + + T findByPrimaryKey(const TraitsPKType &key) noexcept(false); + + void findByPrimaryKey(const TraitsPKType &key, + const SingleRowCallback &rcb, + const ExceptionCallback &ecb) noexcept; + + std::future findFutureByPrimaryKey(const TraitsPKType &key) noexcept; std::vector findAll() noexcept(false); void findAll(const MultipleRowsCallback &rcb, @@ -133,12 +151,14 @@ class Mapper } } template - typename std::enable_if::value, void>::type outputPrimeryKeyToBinder(const typename T::PrimaryKeyType &pk, internal::SqlBinder &binder) + typename std::enable_if::value, void>::type + outputPrimeryKeyToBinder(const TraitsPKType &pk, internal::SqlBinder &binder) { binder << pk; } template - typename std::enable_if, PKType>::value, void>::type outputPrimeryKeyToBinder(const typename T::PrimaryKeyType &pk, internal::SqlBinder &binder) + typename std::enable_if, PKType>::value, void>::type + outputPrimeryKeyToBinder(const TraitsPKType &pk, internal::SqlBinder &binder) { tupleToBinder(pk, binder); } @@ -157,14 +177,14 @@ class Mapper }; template -inline T Mapper::findByPrimaryKey(const typename T::PrimaryKeyType &key) noexcept(false) +inline T Mapper::findByPrimaryKey(const typename Mapper::TraitsPKType &key) noexcept(false) { static_assert(!std::is_same::value, "No primary key in the table!"); // return findOne(Criteria(T::primaryKeyName, key)); std::string sql = "select * from "; sql += T::tableName; makePrimaryKeyCriteria(sql); - if(_forUpdate) + if (_forUpdate) { sql += " for update"; } @@ -193,7 +213,7 @@ inline T Mapper::findByPrimaryKey(const typename T::PrimaryKeyType &key) noex } template -inline void Mapper::findByPrimaryKey(const typename T::PrimaryKeyType &key, +inline void Mapper::findByPrimaryKey(const typename Mapper::TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept { @@ -228,7 +248,8 @@ inline void Mapper::findByPrimaryKey(const typename T::PrimaryKeyType &key, } template -inline std::future Mapper::findFutureByPrimaryKey(const typename T::PrimaryKeyType &key) noexcept +inline std::future +Mapper::findFutureByPrimaryKey(const typename Mapper::TraitsPKType &key) noexcept { static_assert(!std::is_same::value, "No primary key in the table!"); //return findFutureOne(Criteria(T::primaryKeyName, key));