Modify the interface of the DbClient class
This commit is contained in:
parent
136ab1d460
commit
38163a26a6
|
@ -75,19 +75,22 @@ class DbClient : public trantor::NonCopyable
|
||||||
static std::shared_ptr<DbClient> newSqlite3Client(const std::string &connInfo, const size_t connNum);
|
static std::shared_ptr<DbClient> newSqlite3Client(const std::string &connInfo, const size_t connNum);
|
||||||
#endif
|
#endif
|
||||||
/// Async and nonblocking method
|
/// Async and nonblocking method
|
||||||
template <
|
/**
|
||||||
typename FUNCTION1,
|
* FUNCTION1 is usually the ResultCallback type;
|
||||||
typename FUNCTION2,
|
* FUNCTION2 is usually the ExceptionCallback type;
|
||||||
typename... Arguments>
|
*/
|
||||||
|
template <typename FUNCTION1,
|
||||||
|
typename FUNCTION2,
|
||||||
|
typename... Arguments>
|
||||||
void execSqlAsync(const std::string &sql,
|
void execSqlAsync(const std::string &sql,
|
||||||
FUNCTION1 rCallback,
|
FUNCTION1 &&rCallback,
|
||||||
FUNCTION2 exceptCallback,
|
FUNCTION2 &&exceptCallback,
|
||||||
Arguments &&... args) noexcept
|
Arguments &&... args) noexcept
|
||||||
{
|
{
|
||||||
auto binder = *this << sql;
|
auto binder = *this << sql;
|
||||||
std::vector<int> v = {(binder << std::forward<Arguments>(args), 0)...};
|
std::vector<int> v = {(binder << std::forward<Arguments>(args), 0)...};
|
||||||
binder >> rCallback;
|
binder >> std::forward<FUNCTION1>(rCallback);
|
||||||
binder >> exceptCallback;
|
binder >> std::forward<FUNCTION2>(exceptCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Async and nonblocking method
|
/// Async and nonblocking method
|
||||||
|
|
|
@ -107,5 +107,18 @@ int main()
|
||||||
LOG_DEBUG << "async nonblocking except callback:" << e.base().what();
|
LOG_DEBUG << "async nonblocking except callback:" << e.base().what();
|
||||||
},
|
},
|
||||||
1);
|
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();
|
getchar();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue