diff --git a/orm_lib/inc/drogon/orm/DbClient.h b/orm_lib/inc/drogon/orm/DbClient.h index bf51da78..3d1f48ca 100644 --- a/orm_lib/inc/drogon/orm/DbClient.h +++ b/orm_lib/inc/drogon/orm/DbClient.h @@ -75,19 +75,22 @@ class DbClient : public trantor::NonCopyable static std::shared_ptr newSqlite3Client(const std::string &connInfo, const size_t connNum); #endif /// Async and nonblocking method - template < - typename FUNCTION1, - typename FUNCTION2, - typename... Arguments> + /** + * FUNCTION1 is usually the ResultCallback type; + * FUNCTION2 is usually the ExceptionCallback type; + */ + template void execSqlAsync(const std::string &sql, - FUNCTION1 rCallback, - FUNCTION2 exceptCallback, + FUNCTION1 &&rCallback, + FUNCTION2 &&exceptCallback, Arguments &&... args) noexcept { auto binder = *this << sql; std::vector v = {(binder << std::forward(args), 0)...}; - binder >> rCallback; - binder >> exceptCallback; + binder >> std::forward(rCallback); + binder >> std::forward(exceptCallback); } /// Async and nonblocking method diff --git a/orm_lib/src/postgresql_impl/test/test1.cc b/orm_lib/src/postgresql_impl/test/test1.cc index 065ea605..faa419ee 100644 --- a/orm_lib/src/postgresql_impl/test/test1.cc +++ b/orm_lib/src/postgresql_impl/test/test1.cc @@ -107,5 +107,18 @@ int main() LOG_DEBUG << "async nonblocking except callback:" << e.base().what(); }, 1); + clientPtr->execSqlAsync("select * from users where org_name=$1", + [](const Result &r) { + std::cout << r.size() << " rows selected!" << std::endl; + int i = 0; + for (auto row : r) + { + std::cout << i++ << ": user name is " << row["user_name"].as() << std::endl; + } + }, + [](const DrogonDbException &e) { + std::cerr << "error:" << e.base().what() << std::endl; + }, + "default"); getchar(); }