Modify the interface of the DbClient class

This commit is contained in:
antao 2019-02-16 02:11:30 +08:00
parent 136ab1d460
commit 38163a26a6
2 changed files with 24 additions and 8 deletions

View File

@ -75,19 +75,22 @@ class DbClient : public trantor::NonCopyable
static std::shared_ptr<DbClient> 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 <typename FUNCTION1,
typename FUNCTION2,
typename... Arguments>
void execSqlAsync(const std::string &sql,
FUNCTION1 rCallback,
FUNCTION2 exceptCallback,
FUNCTION1 &&rCallback,
FUNCTION2 &&exceptCallback,
Arguments &&... args) noexcept
{
auto binder = *this << sql;
std::vector<int> v = {(binder << std::forward<Arguments>(args), 0)...};
binder >> rCallback;
binder >> exceptCallback;
binder >> std::forward<FUNCTION1>(rCallback);
binder >> std::forward<FUNCTION2>(exceptCallback);
}
/// Async and nonblocking method

View File

@ -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::string>() << std::endl;
}
},
[](const DrogonDbException &e) {
std::cerr << "error:" << e.base().what() << std::endl;
},
"default");
getchar();
}