27
27
#include " thirdparty/php80/pdo_pgsql/php_pdo_pgsql_int.h"
28
28
#endif
29
29
30
+ using swoole::Reactor;
30
31
using swoole::coroutine::Socket;
31
32
32
33
static bool swoole_pgsql_blocking = true ;
33
34
34
35
static int swoole_pgsql_socket_poll (PGconn *conn, swEventType event, double timeout = -1 ) {
35
36
if (swoole_pgsql_blocking) {
36
- return 1 ;
37
+ struct pollfd fds[1 ] = {};
38
+ fds[0 ].fd = PQsocket (conn);
39
+ fds[0 ].events = 0 ;
40
+
41
+ if (Reactor::isset_read_event (event)) {
42
+ fds[0 ].events |= POLLIN;
43
+ }
44
+
45
+ if (Reactor::isset_write_event (event)) {
46
+ fds[0 ].events |= POLLOUT;
47
+ }
48
+
49
+ if (Reactor::isset_error_event (event)) {
50
+ fds[0 ].events |= POLLHUP;
51
+ }
52
+
53
+ int result = 0 ;
54
+ do {
55
+ result = poll (fds, 1 , timeout);
56
+ } while (result < 0 && errno == EINTR);
57
+
58
+ return result > 0 ? 1 : errno == ETIMEDOUT ? 0 : -1 ;
37
59
}
60
+
38
61
Socket sock (PQsocket (conn), SW_SOCK_RAW);
39
62
sock.get_socket ()->nonblock = 1 ;
40
63
bool retval = sock.poll (event, timeout);
@@ -74,10 +97,6 @@ static PGresult *swoole_pgsql_get_result(PGconn *conn) {
74
97
}
75
98
76
99
PGconn *swoole_pgsql_connectdb (const char *conninfo) {
77
- if (swoole_pgsql_blocking) {
78
- return PQconnectdb (conninfo);
79
- }
80
-
81
100
PGconn *conn = PQconnectStart (conninfo);
82
101
if (conn == nullptr ) {
83
102
return nullptr ;
@@ -120,10 +139,6 @@ PGconn *swoole_pgsql_connectdb(const char *conninfo) {
120
139
121
140
PGresult *swoole_pgsql_prepare (
122
141
PGconn *conn, const char *stmt_name, const char *query, int n_params, const Oid *param_types) {
123
- if (swoole_pgsql_blocking) {
124
- return PQprepare (conn, stmt_name, query, n_params, param_types);
125
- }
126
-
127
142
swoole_trace_log (SW_TRACE_CO_PGSQL, " PQsendPrepare(conn=%p, stmt_name='%s')" , conn, stmt_name);
128
143
int ret = PQsendPrepare (conn, stmt_name, query, n_params, param_types);
129
144
if (ret == 0 ) {
@@ -144,9 +159,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
144
159
const int *param_lengths,
145
160
const int *param_formats,
146
161
int result_format) {
147
- if (swoole_pgsql_blocking) {
148
- return PQexecPrepared (conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
149
- }
150
162
swoole_trace_log (SW_TRACE_CO_PGSQL, " PQsendQueryPrepared(conn=%p, stmt_name='%s')" , conn, stmt_name);
151
163
int ret = PQsendQueryPrepared (conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
152
164
if (ret == 0 ) {
@@ -161,10 +173,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
161
173
}
162
174
163
175
PGresult *swoole_pgsql_exec (PGconn *conn, const char *query) {
164
- if (swoole_pgsql_blocking) {
165
- return PQexec (conn, query);
166
- }
167
-
168
176
swoole_trace_log (SW_TRACE_CO_PGSQL, " PQsendQuery(conn=%p, query='%s')" , conn, query);
169
177
int ret = PQsendQuery (conn, query);
170
178
if (ret == 0 ) {
@@ -186,10 +194,6 @@ PGresult *swoole_pgsql_exec_params(PGconn *conn,
186
194
const int *param_lengths,
187
195
const int *param_formats,
188
196
int result_format) {
189
- if (swoole_pgsql_blocking) {
190
- return PQexecParams (conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);
191
- }
192
-
193
197
swoole_trace_log (SW_TRACE_CO_PGSQL, " PQsendQueryParams(conn=%p, command='%s')" , conn, command);
194
198
int ret = PQsendQueryParams (
195
199
conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);
0 commit comments