Skip to content

Commit

Permalink
Fix Swoole\Coroutine\PostgreSQL bugs (#5172)
Browse files Browse the repository at this point in the history
* Fix $row parameter of pgsql fetch method does not take effect

* Fix Statement's row count

* Fix test
  • Loading branch information
Yurunsoft authored Nov 1, 2023
1 parent 2cf0afd commit 8a2dfcc
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ext-src/swoole_postgresql_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Object {
Statement *statement;
std::list<Statement *> statements;
enum QueryType request_type;
int row;
bool connected;
bool ignore_notices;
bool log_notices;
Expand All @@ -70,6 +69,7 @@ class Statement {
PGresult *result;
char *name;
char *query;
int row;
};
} // namespace postgresql
} // namespace swoole
Expand Down Expand Up @@ -856,12 +856,12 @@ static int query_result_parse(PGObject *object) {
case PGRES_COMMAND_OK: /* successful command that did not return rows */
default:
object->result = pgsql_result;
object->row = 0;
/* Wait to finish sending buffer */
res = PQflush(object->conn);
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"));
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultDiag"));
if (object->statement) {
object->statement->row = 0;
zend_update_property_null(
swoole_postgresql_coro_statement_ce, SW_Z8_OBJ_P(object->statement->object), ZEND_STRL("error"));
zend_update_property_null(
Expand Down Expand Up @@ -1656,7 +1656,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
RETURN_FALSE;
}
}
use_row = ZEND_NUM_ARGS() > 1 && row != -1;
use_row = ZEND_NUM_ARGS() > 0 && row != -1;

if (!(result_type & PGSQL_BOTH)) {
php_swoole_fatal_error(E_WARNING, "Invalid result type");
Expand All @@ -1679,14 +1679,14 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
RETURN_FALSE;
}
pgsql_row = (int) row;
pg_result->row = pgsql_row;
statement->row = pgsql_row;
} else {
/* If 2nd param is nullptr, use internal row counter to access next row */
pgsql_row = pg_result->row;
pgsql_row = statement->row;
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
RETURN_FALSE;
}
pg_result->row++;
statement->row++;
}

array_init(return_value);
Expand Down
1 change: 1 addition & 0 deletions tests/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE weather (
prcp real,
date date);
INSERT INTO weather(city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27') RETURNING id;
INSERT INTO weather(city, temp_lo, temp_hi, prcp, date) VALUES ('Test2', 11, 22, 0.3, '1994-11-28') RETURNING id;

DROP TABLE IF EXISTS oid;
CREATE TABLE oid (
Expand Down
158 changes: 158 additions & 0 deletions tests/swoole_pgsql_coro/fetch.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
--TEST--
swoole_pgsql_coro: fetch
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
Swoole\Coroutine\run(function () {
$pgsql = new Swoole\Coroutine\PostgreSQL();
$connected = $pgsql->connect(PGSQL_CONNECTION_STRING);
Assert::true($connected, (string) $pgsql->error);

$stmt = $pgsql->query('SELECT * FROM weather;');
Assert::true(false !== $stmt, (string) $pgsql->error);

var_dump($stmt->fetchObject(0), $stmt->fetchObject(1));
var_dump($stmt->fetchAssoc(0), $stmt->fetchAssoc(1));
var_dump($stmt->fetchArray(0), $stmt->fetchArray(1));
var_dump($stmt->fetchRow(0), $stmt->fetchRow(1));
});
?>
--EXPECTF--
object(stdClass)#%d (6) {
["id"]=>
int(1)
["city"]=>
string(13) "San Francisco"
["temp_lo"]=>
int(46)
["temp_hi"]=>
int(50)
["prcp"]=>
float(0.25)
["date"]=>
string(10) "1994-11-27"
}
object(stdClass)#%d (6) {
["id"]=>
int(2)
["city"]=>
string(5) "Test2"
["temp_lo"]=>
int(11)
["temp_hi"]=>
int(22)
["prcp"]=>
float(0.3)
["date"]=>
string(10) "1994-11-28"
}
array(6) {
["id"]=>
int(1)
["city"]=>
string(13) "San Francisco"
["temp_lo"]=>
int(46)
["temp_hi"]=>
int(50)
["prcp"]=>
float(0.25)
["date"]=>
string(10) "1994-11-27"
}
array(6) {
["id"]=>
int(2)
["city"]=>
string(5) "Test2"
["temp_lo"]=>
int(11)
["temp_hi"]=>
int(22)
["prcp"]=>
float(0.3)
["date"]=>
string(10) "1994-11-28"
}
array(12) {
[0]=>
int(1)
["id"]=>
int(1)
[1]=>
string(13) "San Francisco"
["city"]=>
string(13) "San Francisco"
[2]=>
int(46)
["temp_lo"]=>
int(46)
[3]=>
int(50)
["temp_hi"]=>
int(50)
[4]=>
float(0.25)
["prcp"]=>
float(0.25)
[5]=>
string(10) "1994-11-27"
["date"]=>
string(10) "1994-11-27"
}
array(12) {
[0]=>
int(2)
["id"]=>
int(2)
[1]=>
string(5) "Test2"
["city"]=>
string(5) "Test2"
[2]=>
int(11)
["temp_lo"]=>
int(11)
[3]=>
int(22)
["temp_hi"]=>
int(22)
[4]=>
float(0.3)
["prcp"]=>
float(0.3)
[5]=>
string(10) "1994-11-28"
["date"]=>
string(10) "1994-11-28"
}
array(6) {
[0]=>
int(1)
[1]=>
string(13) "San Francisco"
[2]=>
int(46)
[3]=>
int(50)
[4]=>
float(0.25)
[5]=>
string(10) "1994-11-27"
}
array(6) {
[0]=>
int(2)
[1]=>
string(5) "Test2"
[2]=>
int(11)
[3]=>
int(22)
[4]=>
float(0.3)
[5]=>
string(10) "1994-11-28"
}

0 comments on commit 8a2dfcc

Please sign in to comment.