Support CoroMapper method chaining (#1103)

This commit is contained in:
NitroMelon 2021-12-04 20:54:25 +08:00 committed by GitHub
parent 1c44cf7e4c
commit e9fafc643d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 0 deletions

View File

@ -119,6 +119,74 @@ class CoroMapper : public Mapper<T>
abort();
}
}
// Query condition overrides
/**
* @brief Add a limit to the query.
*
* @param limit The limit
* @return CoroMapper<T>& The CoroMapper itself.
*/
CoroMapper<T> &limit(size_t limit)
{
Mapper<T>::limit(limit);
return *this;
}
/**
* @brief Add a offset to the query.
*
* @param offset The offset.
* @return CoroMapper<T>& The CoroMapper itself.
*/
CoroMapper<T> &offset(size_t offset)
{
Mapper<T>::offset(offset);
return *this;
}
/**
* @brief Set the order of the results.
*
* @param colName the column name, the results are sorted by that column
* @param order Ascending or descending order
* @return CoroMapper<T>& The CoroMapper itself.
*/
CoroMapper<T> &orderBy(const std::string &colName,
const SortOrder &order = SortOrder::ASC)
{
Mapper<T>::orderBy(colName, order);
return *this;
}
/**
* @brief Set the order of the results.
*
* @param colIndex the column index, the results are sorted by that column
* @param order Ascending or descending order
* @return CoroMapper<T>& The CoroMapper itself.
*/
CoroMapper<T> &orderBy(size_t colIndex,
const SortOrder &order = SortOrder::ASC)
{
Mapper<T>::orderBy(colIndex, order);
return *this;
}
/**
* @brief Lock the result for updating.
*
* @return CoroMapper<T>& The CoroMapper itself.
*/
CoroMapper<T> &forUpdate()
{
Mapper<T>::forUpdate();
return *this;
}
// Read api for coroutines
inline internal::MapperAwaiter<std::vector<T>> findAll()
{
return findBy(Criteria());

View File

@ -666,6 +666,20 @@ DROGON_TEST(PostgreTest)
{
SUCCESS();
}
try
{
CoroMapper<Users> mapper(clientPtr);
auto users = co_await mapper.orderBy(Users::Cols::_user_id)
.limit(2)
.findAll();
MANDATE(users.size() == 2);
SUCCESS();
}
catch (const DrogonDbException &e)
{
FAULT("postgresql - ORM mapper coroutine interface(2) what():" +
std::string(e.base().what()));
}
/// 7.4 Transactions
try
{