Skip to content

Commit

Permalink
[6.0]Fix bug #5313 (#5396)
Browse files Browse the repository at this point in the history
* Fix bug #5313

* optimize code
  • Loading branch information
NathanFreeman committed Jul 5, 2024
1 parent 6e9966c commit 61a86d1
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions ext-src/swoole_pgsql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "php_swoole_pgsql.h"
#include "php_swoole_private.h"
#include "swoole_coroutine_socket.h"
#include "swoole_coroutine_system.h"

#ifdef SW_USE_PGSQL
#if PHP_VERSION_ID > 80100
Expand All @@ -27,14 +28,26 @@
#include "thirdparty/php80/pdo_pgsql/php_pdo_pgsql_int.h"
#endif

using swoole::Reactor;
using swoole::coroutine::Socket;
using swoole::coroutine::translate_events_to_poll;

static bool swoole_pgsql_blocking = true;

static int swoole_pgsql_socket_poll(PGconn *conn, swEventType event, double timeout = -1) {
if (swoole_pgsql_blocking) {
return 1;
struct pollfd fds[1];
fds[0].fd = PQsocket(conn);
fds[0].events |= translate_events_to_poll(event);

int result = 0;
do {
result = poll(fds, 1, timeout);
} while (result < 0 && errno == EINTR);

return result > 0 ? 1 : errno == ETIMEDOUT ? 0 : -1;
}

Socket sock(PQsocket(conn), SW_SOCK_RAW);
sock.get_socket()->nonblock = 1;
bool retval = sock.poll(event, timeout);
Expand Down Expand Up @@ -74,10 +87,6 @@ static PGresult *swoole_pgsql_get_result(PGconn *conn) {
}

PGconn *swoole_pgsql_connectdb(const char *conninfo) {
if (swoole_pgsql_blocking) {
return PQconnectdb(conninfo);
}

PGconn *conn = PQconnectStart(conninfo);
if (conn == nullptr) {
return nullptr;
Expand Down Expand Up @@ -120,10 +129,6 @@ PGconn *swoole_pgsql_connectdb(const char *conninfo) {

PGresult *swoole_pgsql_prepare(
PGconn *conn, const char *stmt_name, const char *query, int n_params, const Oid *param_types) {
if (swoole_pgsql_blocking) {
return PQprepare(conn, stmt_name, query, n_params, param_types);
}

swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendPrepare(conn=%p, stmt_name='%s')", conn, stmt_name);
int ret = PQsendPrepare(conn, stmt_name, query, n_params, param_types);
if (ret == 0) {
Expand All @@ -144,9 +149,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
const int *param_lengths,
const int *param_formats,
int result_format) {
if (swoole_pgsql_blocking) {
return PQexecPrepared(conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
}
swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQueryPrepared(conn=%p, stmt_name='%s')", conn, stmt_name);
int ret = PQsendQueryPrepared(conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
if (ret == 0) {
Expand All @@ -161,10 +163,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
}

PGresult *swoole_pgsql_exec(PGconn *conn, const char *query) {
if (swoole_pgsql_blocking) {
return PQexec(conn, query);
}

swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQuery(conn=%p, query='%s')", conn, query);
int ret = PQsendQuery(conn, query);
if (ret == 0) {
Expand All @@ -186,10 +184,6 @@ PGresult *swoole_pgsql_exec_params(PGconn *conn,
const int *param_lengths,
const int *param_formats,
int result_format) {
if (swoole_pgsql_blocking) {
return PQexecParams(conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);
}

swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQueryParams(conn=%p, command='%s')", conn, command);
int ret = PQsendQueryParams(
conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);
Expand Down

0 comments on commit 61a86d1

Please sign in to comment.