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().
This commit is contained in:
Nikita Popov 2020-09-21 17:20:28 +02:00
parent fb4554e431
commit 8ff2f2f84b
7 changed files with 19 additions and 16 deletions

View File

@ -431,6 +431,8 @@ PHP 8.0 UPGRADE NOTES
. The deprecated pg_lo_import() and pg_lo_export() signature that passes the
connection as the last argument is no longer supported. The connection
should be passed as first argument instead.
. pg_fetch_all() will now return an empty array instead of false for result
sets with zero rows.
- Phar:
. Metadata associated with a phar will no longer be automatically unserialized,

View File

@ -710,7 +710,7 @@ static const func_info_t func_infos[] = {
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),
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),
F1("pg_fetch_object", MAY_BE_FALSE | MAY_BE_OBJECT),
F1("pg_fetch_all", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_ARRAY),
F1("pg_fetch_all", MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_ARRAY),
F1("pg_fetch_all_columns", MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_NULL | MAY_BE_ARRAY_OF_STRING),
F1("pg_last_oid", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING),
F1("pg_lo_create", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING),

View File

@ -2015,10 +2015,7 @@ PHP_FUNCTION(pg_fetch_all)
pgsql_result = pg_result->result;
array_init(return_value);
if (php_pgsql_result2array(pgsql_result, return_value, result_type) == FAILURE) {
zend_array_destroy(Z_ARR_P(return_value));
RETURN_FALSE;
}
php_pgsql_result2array(pgsql_result, return_value, result_type);
}
/* }}} */
@ -5796,7 +5793,7 @@ PHP_FUNCTION(pg_delete)
/* }}} */
/* {{{ php_pgsql_result2array */
PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type)
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type)
{
zval row;
char *field_name;
@ -5805,9 +5802,7 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, l
uint32_t i;
assert(Z_TYPE_P(ret_array) == IS_ARRAY);
if ((pg_numrows = PQntuples(pg_result)) <= 0) {
return FAILURE;
}
pg_numrows = PQntuples(pg_result);
for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
array_init(&row);
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
}
add_index_zval(ret_array, pg_row, &row);
}
return SUCCESS;
}
/* }}} */
@ -5877,7 +5871,8 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, l
pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) {
ret = php_pgsql_result2array(pg_result, ret_array, result_type);
php_pgsql_result2array(pg_result, ret_array, result_type);
ret = SUCCESS;
} else {
php_error_docref(NULL, E_NOTICE, "Failed to execute '%s'", ZSTR_VAL(querystr.s));
}

View File

@ -193,7 +193,7 @@ function pg_fetch_array($result, ?int $row_number = null, int $result_type = PGS
function pg_fetch_object($result, ?int $row_number = null, string $class_name = "stdClass", ?array $ctor_params = null): object|false {}
/** @param resource $result */
function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array|false {}
function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array {}
/** @param resource $result */
function pg_fetch_all_columns($result, int $field_number = 0): array {}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 3f5e097d572721b42f2ad438c2af8c0d1f9c9086 */
* Stub hash: 9735dbc8a4ca642ee825ae8942470ac2dec89f50 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
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
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctor_params, IS_ARRAY, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_all, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pg_fetch_all, 0, 1, IS_ARRAY, 0)
ZEND_ARG_INFO(0, result)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_ASSOC")
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
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_STATUS_LONG")
ZEND_END_ARG_INFO()
#define arginfo_pg_get_notify arginfo_pg_fetch_all
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_get_notify, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_INFO(0, result)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_ASSOC")
ZEND_END_ARG_INFO()
#define arginfo_pg_get_pid arginfo_pg_connect_poll

View File

@ -182,7 +182,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *val
PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql);
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 );
PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
/* internal functions */
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);

View File

@ -125,6 +125,7 @@ try {
$result = pg_query($db, "INSERT INTO ".$table_name." VALUES (9999, 'ABC');");
pg_last_oid($result);
var_dump(pg_fetch_all($result));
pg_free_result($result);
pg_close($db);
@ -144,4 +145,6 @@ pg_field_name(): Argument #2 ($field_number) must be greater than or equal to 0
pg_field_name(): Argument #2 ($field_number) must be less than the number of fields for this result set
pg_field_table(): Argument #2 ($field_number) must be greater than or equal to 0
pg_field_table(): Argument #2 ($field_number) must be less than the number of fields for this result set
array(0) {
}
OK