Skip to content

Commit 8ff2f2f

Browse files
committed
Return empty array for no rows in pg_fetch_all()
This makes it line up with pg_fetch_all_columns(), as well as similar functions in other exts, such as mysqli_fetch_all().
1 parent fb4554e commit 8ff2f2f

File tree

7 files changed

+19
-16
lines changed

7 files changed

+19
-16
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ PHP 8.0 UPGRADE NOTES
431431
. The deprecated pg_lo_import() and pg_lo_export() signature that passes the
432432
connection as the last argument is no longer supported. The connection
433433
should be passed as first argument instead.
434+
. pg_fetch_all() will now return an empty array instead of false for result
435+
sets with zero rows.
434436

435437
- Phar:
436438
. Metadata associated with a phar will no longer be automatically unserialized,

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ static const func_info_t func_infos[] = {
710710
F1("pg_fetch_assoc", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_STRING | MAY_BE_ARRAY_OF_NULL | MAY_BE_ARRAY_OF_STRING),
711711
F1("pg_fetch_array", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_NULL | MAY_BE_ARRAY_OF_STRING),
712712
F1("pg_fetch_object", MAY_BE_FALSE | MAY_BE_OBJECT),
713-
F1("pg_fetch_all", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_ARRAY),
713+
F1("pg_fetch_all", MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_ARRAY),
714714
F1("pg_fetch_all_columns", MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_NULL | MAY_BE_ARRAY_OF_STRING),
715715
F1("pg_last_oid", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING),
716716
F1("pg_lo_create", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING),

ext/pgsql/pgsql.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,10 +2015,7 @@ PHP_FUNCTION(pg_fetch_all)
20152015

20162016
pgsql_result = pg_result->result;
20172017
array_init(return_value);
2018-
if (php_pgsql_result2array(pgsql_result, return_value, result_type) == FAILURE) {
2019-
zend_array_destroy(Z_ARR_P(return_value));
2020-
RETURN_FALSE;
2021-
}
2018+
php_pgsql_result2array(pgsql_result, return_value, result_type);
20222019
}
20232020
/* }}} */
20242021

@@ -5796,7 +5793,7 @@ PHP_FUNCTION(pg_delete)
57965793
/* }}} */
57975794

57985795
/* {{{ php_pgsql_result2array */
5799-
PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type)
5796+
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type)
58005797
{
58015798
zval row;
58025799
char *field_name;
@@ -5805,9 +5802,7 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, l
58055802
uint32_t i;
58065803
assert(Z_TYPE_P(ret_array) == IS_ARRAY);
58075804

5808-
if ((pg_numrows = PQntuples(pg_result)) <= 0) {
5809-
return FAILURE;
5810-
}
5805+
pg_numrows = PQntuples(pg_result);
58115806
for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
58125807
array_init(&row);
58135808
for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
@@ -5834,7 +5829,6 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, l
58345829
}
58355830
add_index_zval(ret_array, pg_row, &row);
58365831
}
5837-
return SUCCESS;
58385832
}
58395833
/* }}} */
58405834

@@ -5877,7 +5871,8 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, l
58775871

58785872
pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
58795873
if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) {
5880-
ret = php_pgsql_result2array(pg_result, ret_array, result_type);
5874+
php_pgsql_result2array(pg_result, ret_array, result_type);
5875+
ret = SUCCESS;
58815876
} else {
58825877
php_error_docref(NULL, E_NOTICE, "Failed to execute '%s'", ZSTR_VAL(querystr.s));
58835878
}

ext/pgsql/pgsql.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function pg_fetch_array($result, ?int $row_number = null, int $result_type = PGS
193193
function pg_fetch_object($result, ?int $row_number = null, string $class_name = "stdClass", ?array $ctor_params = null): object|false {}
194194

195195
/** @param resource $result */
196-
function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array|false {}
196+
function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array {}
197197

198198
/** @param resource $result */
199199
function pg_fetch_all_columns($result, int $field_number = 0): array {}

ext/pgsql/pgsql_arginfo.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 3f5e097d572721b42f2ad438c2af8c0d1f9c9086 */
2+
* Stub hash: 9735dbc8a4ca642ee825ae8942470ac2dec89f50 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, connection_string, IS_STRING, 0)
@@ -155,7 +155,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_object, 0, 1, MAY_BE_OB
155155
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctor_params, IS_ARRAY, 1, "null")
156156
ZEND_END_ARG_INFO()
157157

158-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_all, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
158+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pg_fetch_all, 0, 1, IS_ARRAY, 0)
159159
ZEND_ARG_INFO(0, result)
160160
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_ASSOC")
161161
ZEND_END_ARG_INFO()
@@ -385,7 +385,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_result_status, 0, 1, MAY_BE_S
385385
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_STATUS_LONG")
386386
ZEND_END_ARG_INFO()
387387

388-
#define arginfo_pg_get_notify arginfo_pg_fetch_all
388+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_get_notify, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
389+
ZEND_ARG_INFO(0, result)
390+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_ASSOC")
391+
ZEND_END_ARG_INFO()
389392

390393
#define arginfo_pg_get_pid arginfo_pg_connect_poll
391394

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *val
182182
PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
183183
PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql);
184184
PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
185-
PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
185+
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
186186

187187
/* internal functions */
188188
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);

ext/pgsql/tests/03sync_query.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ try {
125125

126126
$result = pg_query($db, "INSERT INTO ".$table_name." VALUES (9999, 'ABC');");
127127
pg_last_oid($result);
128+
var_dump(pg_fetch_all($result));
128129

129130
pg_free_result($result);
130131
pg_close($db);
@@ -144,4 +145,6 @@ pg_field_name(): Argument #2 ($field_number) must be greater than or equal to 0
144145
pg_field_name(): Argument #2 ($field_number) must be less than the number of fields for this result set
145146
pg_field_table(): Argument #2 ($field_number) must be greater than or equal to 0
146147
pg_field_table(): Argument #2 ($field_number) must be less than the number of fields for this result set
148+
array(0) {
149+
}
147150
OK

0 commit comments

Comments
 (0)