Aiolibpq simple example version for c++20 co_await with boost asio
website: https://www.gamefunc.top:9029
github: https://github.com/gamefunc
qq: 32686647
weixin: gamefunc
mail: [email protected]
/* full in Aiolibpq_simple_test_unit.cpp */
awaitable<void> query_test_task(int i, std::string cmd){
Aiolibpq aiopg_conn(
"db_name", "gamefunc", "password",
"127.0.0.1", 5432);
int err = co_await aiopg_conn.connect();
if(err){ throw 0; }
err = co_await aiopg_conn.execute(cmd);
if(err){ throw 0; }
auto [order_key_list, rows_dict] = co_await aiopg_conn.fetchall();
std::cout << std::format(
"task_{}: "
"num_of_cols: {}; "
"num_of_rows: {}; "
"rows_dict[\"cover_name\"][{}]: {}\n",
i,
rows_dict.size(),
rows_dict["cover_name"].size(),
i,
rows_dict["cover_name"][i]
);
// aiopg_conn.close_conn();
}// query_test()
- need modify postgresql source code to add get unresolv domain,
run "Modify_Libpq_Source_Code.py" (need set PG_SOURCE_CODE_ROOT_PATH); - then rebuild libpq c lib;
- include and link libpq;
- include and link boost asio;
- include Aiolibpq_simple.hpp;
- set(CMAKE_CXX_STANDARD_REQUIRED 20);
- make with Aiolibpq_simple.cpp;
-
if you don't want to modify pg source code and rebuild libpq;
you can parse conn_params string:
if ("host=" in conn_params) and ("hostaddr=" not in conn_params):
get_domain_name_and_resolv_and_add_hostaddr() -
see "Aiolibpq_simple.hpp" coro::gen<PGresult*> fetchone();
// want like: for(auto p: co_await fetchone()){ if(!p){break;} dodo(p); }
// but co_yield coro task is not yet supported: so impl yourself;
// see "Aiolibpq_simple.hpp" coro::gen<PGresult*> fetchone();
while(...){
...some code...
co_await asyncio_wait_fd_rw(fd, tcp::socket::wait_read);
...some code...
while(!PQisBusy(conn)){
co_yield PQgetResult(conn);
}// while(!PQisBusy(conn))
}// while(...)
3: default use std::format, need gcc13, clang14, msvc19.32,
https://en.cppreference.com/w/cpp/23
if not, use fmt::format() or delete that;
"Aiolibpq_simple_test_unit.cpp"
GNU General Public License v3.0
free to use and modify, but need keep the above information.