Skip to content

Commit 7ae0273

Browse files
committed
Make the $row param of pg_fetch_result(), pg_field_prtlen() and pg_field_is_null() nullable
1 parent b3bd55f commit 7ae0273

8 files changed

+62
-23
lines changed

ext/pgsql/pgsql.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,7 @@ PHP_FUNCTION(pg_fetch_result)
16941694
zval *result;
16951695
zend_string *field_name;
16961696
zend_long row, field_offset;
1697+
bool row_is_null = false;
16971698
PGresult *pgsql_result;
16981699
pgsql_result_handle *pg_result;
16991700
int pgsql_row;
@@ -1706,7 +1707,11 @@ PHP_FUNCTION(pg_fetch_result)
17061707
} else {
17071708
ZEND_PARSE_PARAMETERS_START(3, 3)
17081709
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
1709-
Z_PARAM_LONG(row)
1710+
if (zend_string_equals_literal(EG(current_execute_data)->func->common.function_name, "pg_result")) {
1711+
Z_PARAM_LONG(row)
1712+
} else {
1713+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
1714+
}
17101715
Z_PARAM_STR_OR_LONG(field_name, field_offset)
17111716
ZEND_PARSE_PARAMETERS_END();
17121717
}
@@ -1715,7 +1720,7 @@ PHP_FUNCTION(pg_fetch_result)
17151720
CHECK_PGSQL_RESULT(pg_result);
17161721
pgsql_result = pg_result->result;
17171722

1718-
if (ZEND_NUM_ARGS() == 2) {
1723+
if (ZEND_NUM_ARGS() == 2 || row_is_null) {
17191724
if (pg_result->row < 0) {
17201725
pg_result->row = 0;
17211726
}
@@ -1995,11 +2000,12 @@ PHP_FUNCTION(pg_result_seek)
19952000
#define PHP_PG_DATA_ISNULL 2
19962001

19972002
/* {{{ php_pgsql_data_info */
1998-
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
2003+
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bool nullable_row)
19992004
{
20002005
zval *result;
20012006
zend_string *field_name;
20022007
zend_long row, field_offset;
2008+
bool row_is_null = false;
20032009
PGresult *pgsql_result;
20042010
pgsql_result_handle *pg_result;
20052011
int pgsql_row;
@@ -2012,7 +2018,11 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
20122018
} else {
20132019
ZEND_PARSE_PARAMETERS_START(3, 3)
20142020
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2015-
Z_PARAM_LONG(row)
2021+
if (nullable_row) {
2022+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2023+
} else {
2024+
Z_PARAM_LONG(row)
2025+
}
20162026
Z_PARAM_STR_OR_LONG(field_name, field_offset)
20172027
ZEND_PARSE_PARAMETERS_END();
20182028
}
@@ -2021,7 +2031,7 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
20212031
CHECK_PGSQL_RESULT(pg_result);
20222032
pgsql_result = pg_result->result;
20232033

2024-
if (ZEND_NUM_ARGS() == 2) {
2034+
if (ZEND_NUM_ARGS() == 2 || row_is_null) {
20252035
if (pg_result->row < 0) {
20262036
pg_result->row = 0;
20272037
}
@@ -2062,14 +2072,28 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
20622072
/* {{{ Returns the printed length */
20632073
PHP_FUNCTION(pg_field_prtlen)
20642074
{
2065-
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
2075+
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH, true);
2076+
}
2077+
/* }}} */
2078+
2079+
/* {{{ Returns the printed length */
2080+
PHP_FUNCTION(pg_fieldprtlen)
2081+
{
2082+
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH, false);
20662083
}
20672084
/* }}} */
20682085

20692086
/* {{{ Test if a field is NULL */
20702087
PHP_FUNCTION(pg_field_is_null)
20712088
{
2072-
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
2089+
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL, true);
2090+
}
2091+
/* }}} */
2092+
2093+
/* {{{ Test if a field is NULL */
2094+
PHP_FUNCTION(pg_fieldisnull)
2095+
{
2096+
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL, false);
20732097
}
20742098
/* }}} */
20752099

ext/pgsql/pgsql.stub.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ function pg_field_num(PgSql\Result $result, string $field): int {}
625625
function pg_fieldnum(PgSql\Result $result, string $field): int {}
626626

627627
/**
628-
* @param string|int $row
628+
* @param string|int|null $row
629629
* @refcount 1
630630
*/
631631
function pg_fetch_result(PgSql\Result $result, $row, string|int $field = UNKNOWN): string|false|null {}
@@ -672,22 +672,20 @@ function pg_fetch_all_columns(PgSql\Result $result, int $field = 0): array {}
672672

673673
function pg_result_seek(PgSql\Result $result, int $row): bool {}
674674

675-
/** @param string|int $row */
675+
/** @param string|int|null $row */
676676
function pg_field_prtlen(PgSql\Result $result, $row, string|int $field = UNKNOWN): int|false {}
677677

678678
/**
679679
* @param string|int $row
680-
* @alias pg_field_prtlen
681680
* @deprecated
682681
*/
683682
function pg_fieldprtlen(PgSql\Result $result, $row, string|int $field = UNKNOWN): int|false {}
684683

685-
/** @param string|int $row */
684+
/** @param string|int|null $row */
686685
function pg_field_is_null(PgSql\Result $result, $row, string|int $field = UNKNOWN): int|false {}
687686

688687
/**
689688
* @param string|int $row
690-
* @alias pg_field_is_null
691689
* @deprecated
692690
*/
693691
function pg_fieldisnull(PgSql\Result $result, $row, string|int $field = UNKNOWN): int|false {}

ext/pgsql/pgsql_arginfo.h

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);
8787
static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
8888
static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
8989
static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
90-
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
90+
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bool nullable_row);
9191
static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type);
9292

9393
static ssize_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count);

ext/pgsql/tests/23sync_query_params.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ if ($version['protocol'] >= 3) {
4545
pg_field_num($result, $field_name);
4646
pg_field_size($result, 0);
4747
pg_field_type($result, 0);
48-
pg_field_prtlen($result, 0);
49-
pg_field_is_null($result, 0);
48+
pg_field_prtlen($result, null, 0);
49+
pg_field_is_null($result, null, 0);
5050

5151
$result = pg_query_params($db, "INSERT INTO ".$table_name." VALUES (\$1, \$2);", array(9999, "A'BC"));
5252
pg_last_oid($result);

ext/pgsql/tests/32nb_async_query.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pg_field_name($result, 0);
5959
pg_field_num($result, $field_name);
6060
pg_field_size($result, 0);
6161
pg_field_type($result, 0);
62-
pg_field_prtlen($result, 0);
63-
pg_field_is_null($result, 0);
62+
pg_field_prtlen($result, null, 0);
63+
pg_field_is_null($result, null, 0);
6464

6565
$nb_send = pg_send_query($db, "INSERT INTO ".$table_name." VALUES (8888, 'GGG');");
6666
if ($nb_send === FALSE) {

ext/pgsql/tests/98old_api.phpt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ pg_fieldname($result, 0);
1717
pg_fieldsize($result, 0);
1818
pg_fieldtype($result, 0);
1919
pg_fieldprtlen($result, 0);
20+
pg_fieldprtlen($result, null, 0);
2021
pg_fieldisnull($result, 0);
21-
22+
pg_fieldisnull($result, null, 0);
2223
pg_result($result,0,0);
24+
pg_result($result,null,0);
25+
2326
$result = pg_exec($db, "INSERT INTO ".$table_name." VALUES (7777, 'KKK')");
2427
$oid = pg_getlastoid($result);
2528
pg_freeresult($result);
@@ -44,10 +47,22 @@ Deprecated: Function pg_fieldtype() is deprecated in %s on line %d
4447

4548
Deprecated: Function pg_fieldprtlen() is deprecated in %s on line %d
4649

50+
Deprecated: Function pg_fieldprtlen() is deprecated in %s on line %d
51+
52+
Deprecated: pg_fieldprtlen(): Passing null to parameter #2 ($row) of type int is deprecated in %s on line %d
53+
4754
Deprecated: Function pg_fieldisnull() is deprecated in %s on line %d
4855

56+
Deprecated: Function pg_fieldisnull() is deprecated in %s on line %d
57+
58+
Deprecated: pg_fieldisnull(): Passing null to parameter #2 ($row) of type int is deprecated in %s on line %d
59+
4960
Deprecated: Function pg_result() is deprecated in %s on line %d
5061

62+
Deprecated: Function pg_result() is deprecated in %s on line %d
63+
64+
Deprecated: pg_result(): Passing null to parameter #2 ($row) of type int is deprecated in %s on line %d
65+
5166
Deprecated: Function pg_getlastoid() is deprecated in %s on line %d
5267

5368
Deprecated: Function pg_freeresult() is deprecated in %s on line %d

ext/pgsql/tests/bug37100_9.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ pg_query($db, "INSERT INTO test_bug VALUES (decode('0103AA000812','hex'))");
2121

2222

2323
$data = pg_query($db, "SELECT binfield FROM test_bug");
24-
$res = pg_fetch_result($data,0);
24+
$res = pg_fetch_result($data, null, 0);
2525
var_dump($res);
2626
var_dump(bin2hex(pg_unescape_bytea($res)));
2727

2828
$sql = "BEGIN; DECLARE mycursor BINARY CURSOR FOR SELECT binfield FROM test_bug; FETCH ALL IN mycursor;";
2929

3030
$data = pg_query($db, $sql);
31-
$res = pg_fetch_result($data,0);
31+
$res = pg_fetch_result($data, null, 0);
3232

3333
var_dump(strlen($res));
3434
var_dump(bin2hex($res));

0 commit comments

Comments
 (0)