-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aiolibpq_simple.hpp
166 lines (127 loc) · 4.89 KB
/
Aiolibpq_simple.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* GNU General Public License v3.0
Aiolibpq simple version
author: gamefunc
website: https://www.gamefunc.top:9029
github: https://github.com/gamefunc
qq: 32686647
weixin: gamefunc
mail: fevefun@hotmail.com
free to use and modify, but need keep the above information.
test env:
boost version >= 1.78
win_10: cl: 19.34.31937;
debian_11: gcc: 10.2.1(include.replace("std::format", "fmt::format"));
*/
#ifndef __GAMEFUNC_AIOLIBPQ_FOR_CPP_SIMPLE__
#define __GAMEFUNC_AIOLIBPQ_FOR_CPP_SIMPLE__
// boost asio:
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
// cpp std:
#include <iostream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <tuple>
// https://en.cppreference.com/w/cpp/23 gcc13, clang14, msvc19.32:
#include <format>
// libpq:
#include <libpq-fe.h>
// boost asio:
using boost::asio::awaitable;
using boost::asio::co_spawn;
using boost::asio::detached;
using boost::asio::use_awaitable;
using boost::asio::ip::tcp;
namespace this_coro = boost::asio::this_coro;
namespace ssl = boost::asio::ssl;
class Aiolibpq{
/* Aiolibpq simple version by gamefunc qq 32686647:
need modify postgresql source code,
use Modify_Libpq_Source_Code.py,
then build libpq c lib;
if you don't want to modify pg source code,
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()
*/
public:
PGconn *conn = nullptr;
/* @brief Constructor 0:
@param conn_params
Aiolibpq aio_pg("dbname=hanime "
" user=gamefunc password=gamefunc "
" hostaddr=127.0.0.1 port=5432 ");
https://www.gamefunc.top:9029/public/knowledge/postgresql_x_cn/libpq-connect.html */
Aiolibpq(std::string conn_params);
/* @brief Constructor 1:
@param https://www.gamefunc.top:9029/public/knowledge/postgresql_x_cn/libpq-connect.html */
Aiolibpq(std::string dbname = "hanime",
std::string user = "gamefunc",
std::string password = "gamefunc",
std::string host = "www.gamefunc.top || 127.0.0.1",
uint16_t port = 5432,
std::string extra_params = "");
~Aiolibpq();
Aiolibpq() = delete;
Aiolibpq(const Aiolibpq &src) = delete;
Aiolibpq(Aiolibpq &&src);
void close_conn();
/* @brief do async connect db;
@return int: success ? 0 : !0; */
awaitable<int> connect();
/* @brief send sql cmd(auto commit):
@param cmd: "select * from hanime_collection;"
@return int success ? 0 : !0; */
awaitable<int> execute(std::string_view cmd);
/* @brief get all query result; (after co_await execute(cmd));
@return [
vector<string>: order_key_list = [
"col0_name", "col1_name", ...
],
umap<string, vector<string>> rows_dict = {
"col0_name": ["row0_col0_v", "row1_col0_v", ...],
"col1_name": ["row0_col1_v", "row1_col1_v", ...],
...
}
]; */
awaitable<std::tuple<
std::vector<std::string>,
std::unordered_map<std::string, std::vector<std::string>>
> > fetchall();
/* @brief setup fetchall() result dict;
@param rows_dict
@param order_key_list
@param rows
@return int return int success ? 0 : !0; */
int setup_result_rows_dict_key(
std::unordered_map<std::string, std::vector<std::string>> &rows_dict,
std::vector<std::string> &order_key_list,
PGresult *rows);
// /* @brief not yet support co_yield coro task:
// want like:
// for(auto p: co_await fetchone()){ if(!p){break;} dodo(p); }
// @return PGresult* */
// coro::gen<PGresult*> fetchone(){
// // https://www.gamefunc.top:9029/public/knowledge/postgresql_x_cn/libpq-single-row-mode.html
// int ok = PQsetSingleRowMode(conn);
// auto fd = PQsocket(conn);
// while(PQisBusy(conn)){
// co_await asyncio_wait_fd_rw(fd, tcp::socket::wait_read);
// if(PQconsumeInput(conn)){
// while(!PQisBusy(conn)){
// PGresult *one_row = PQgetResult(conn);
// co_yield one_row;
// }// while(!PQisBusy(conn))
// }// if(PQconsumeInput(conn))
// }// while(PQisBusy(conn))
// }// fetchone()
private:
/* @brief await fd can write || read;
@param fd: socket fd;
@param t: tcp::socket:: wait_read || wait_write; */
awaitable<void> asyncio_wait_fd_rw(
auto fd, auto t);
};// class Aiolibpq{}
#endif// __GAMEFUNC_AIOLIBPQ_FOR_CPP_SIMPLE__