Skip to content

Commit 1f42777

Browse files
committed
Deprecate using the implicit default PgSQL connection
The DB connection should be provided in all cases as the first argument. The overloaded function signatures will be removed in the future. Warn about this change. Part of https://wiki.php.net/rfc/deprecations_php_8_1.
1 parent 1607207 commit 1f42777

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+311
-115
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ PHP 8.1 UPGRADE NOTES
143143
. The PgSQL functions now accept and return, respectively, \PgSql\Lob
144144
objects instead of "pgsql large object" resources. Return value checks
145145
using is_resource() should be replaced with checks for `false`.
146+
. Not passing the connection argument to PgSQL functions and using the
147+
default connection is deprecated.
148+
RFC: https://wiki.php.net/rfc/deprecations_php_8_1
146149

147150
- PSpell:
148151
. The PSpell functions now accept and return, respectively, PSpell\Dictionary objects

ext/pgsql/pgsql.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@
7878
zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
7979
RETURN_THROWS(); \
8080
}
81+
82+
/* This is a bit hacky as the macro usage is "link = FETCH_DEFAULT_LINK();" */
8183
#define FETCH_DEFAULT_LINK() \
82-
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
84+
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL); \
85+
php_error_docref(NULL, E_DEPRECATED, "Automatic fetching of PostgreSQL connection is deprecated")
86+
87+
/* Used only when creating a connection */
88+
#define FETCH_DEFAULT_LINK_NO_WARNING() \
89+
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
8390

8491
#define CHECK_PGSQL_LINK(link_handle) \
8592
if (link_handle->conn == NULL) { \
@@ -850,7 +857,7 @@ PHP_FUNCTION(pg_close)
850857
link = Z_PGSQL_LINK_P(pgsql_link);
851858
CHECK_PGSQL_LINK(link);
852859

853-
if (link == FETCH_DEFAULT_LINK()) {
860+
if (link == FETCH_DEFAULT_LINK_NO_WARNING()) {
854861
GC_DELREF(PGG(default_link));
855862
PGG(default_link) = NULL;
856863
}

ext/pgsql/tests/01createdb.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ else {
2222
echo pg_last_error()."\n";
2323
}
2424

25-
$v = pg_version();
25+
$v = pg_version($db);
2626
if (version_compare($v['server'], '9.2', '>=') && (!($q = @pg_query($db, "SELECT * FROM ".$table_name_92)) || !@pg_num_rows($q)))
2727
{
2828
pg_query($db,$table_def_92); // Create table here

ext/pgsql/tests/05large_object.phpt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $handle = pg_lo_open ($db, $oid, "w");
4343
$bytesWritten = pg_lo_read_all($handle);
4444
echo "\n";
4545
var_dump($bytesWritten);
46-
if (pg_last_error()) echo "pg_lo_read_all() error\n".pg_last_error();
46+
if (pg_last_error($db)) echo "pg_lo_read_all() error\n".pg_last_error();
4747
pg_lo_close($handle);
4848
pg_exec ($db, "commit");
4949

@@ -103,7 +103,7 @@ try {
103103

104104
echo "OK";
105105
?>
106-
--EXPECT--
106+
--EXPECTF--
107107
create/write/close LO
108108
open/read/tell/seek/close LO
109109
string(5) "large"
@@ -120,10 +120,16 @@ large object data
120120
int(17)
121121
unlink LO
122122
Test without connection
123+
124+
Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
123125
Test with string oid value
124126
import/export LO
127+
128+
Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
125129
Invalid OID value passed
126130
Invalid OID value passed
131+
132+
Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
127133
Invalid OID value passed
128134
Invalid OID value passed
129135
OK

ext/pgsql/tests/07optional.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ $enc = pg_client_encoding($db);
1616
pg_set_client_encoding($db, $enc);
1717

1818
if (function_exists('pg_set_error_verbosity')) {
19-
pg_set_error_verbosity(PGSQL_ERRORS_TERSE);
20-
pg_set_error_verbosity(PGSQL_ERRORS_DEFAULT);
21-
pg_set_error_verbosity(PGSQL_ERRORS_VERBOSE);
19+
pg_set_error_verbosity($db, PGSQL_ERRORS_TERSE);
20+
pg_set_error_verbosity($db, PGSQL_ERRORS_DEFAULT);
21+
pg_set_error_verbosity($db, PGSQL_ERRORS_VERBOSE);
2222
}
2323
echo "OK";
2424
?>

ext/pgsql/tests/08escape.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ $data = file_get_contents(FILE_NAME);
4444
$db = pg_connect($conn_str);
4545

4646
// Insert binary to DB
47-
$escaped_data = pg_escape_bytea($data);
48-
pg_query("DELETE FROM ".$table_name." WHERE num = 10000;");
47+
$escaped_data = pg_escape_bytea($db, $data);
48+
pg_query($db, "DELETE FROM ".$table_name." WHERE num = 10000;");
4949
$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (10000, CAST ('".$escaped_data."' AS BYTEA));";
5050
pg_query($db, $sql);
5151

@@ -73,7 +73,7 @@ for ($i = 0; $i < 2; $i++) {
7373
// pg_escape_literal/pg_escape_identifier
7474
$before = "ABC\\ABC\'";
7575
$expect = " E'ABC\\\\ABC\\\\'''";
76-
$after = pg_escape_literal($before);
76+
$after = pg_escape_literal($db, $before);
7777
if ($expect === $after) {
7878
echo "pg_escape_literal() is Ok\n";
7979
}
@@ -86,7 +86,7 @@ else {
8686

8787
$before = "ABC\\ABC\'";
8888
$expect = "\"ABC\ABC\'\"";
89-
$after = pg_escape_identifier($before);
89+
$after = pg_escape_identifier($db, $before);
9090
if ($expect === $after) {
9191
echo "pg_escape_identifier() is Ok\n";
9292
}
@@ -98,8 +98,11 @@ else {
9898
}
9999

100100
?>
101-
--EXPECT--
101+
--EXPECTF--
102+
Deprecated: pg_escape_string(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
102103
pg_escape_string() is Ok
104+
105+
Deprecated: pg_escape_bytea(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
103106
pg_escape_bytea() is Ok
104107
pg_escape_bytea() actually works with database
105108
pg_escape_literal() is Ok

ext/pgsql/tests/09notice.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pgsql
77

88
include("skipif.inc");
99

10-
_skip_lc_messages();
10+
_skip_lc_messages($conn);
1111

1212
?>
1313
--FILE--
@@ -20,7 +20,7 @@ ini_set('pgsql.ignore_notice', FALSE);
2020

2121
$db = pg_connect($conn_str);
2222

23-
_set_lc_messages();
23+
_set_lc_messages($db);
2424

2525
$res = pg_query($db, 'SET client_min_messages TO NOTICE;');
2626
var_dump($res);

ext/pgsql/tests/13pg_select_9.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error_reporting(E_ALL);
1414
include 'config.inc';
1515

1616
$db = pg_connect($conn_str);
17-
pg_query("SET bytea_output = 'hex'");
17+
pg_query($db, "SET bytea_output = 'hex'");
1818

1919
$fields = array('num'=>'1234', 'str'=>'ABC', 'bin'=>'XYZ');
2020
$ids = array('num'=>'1234');

ext/pgsql/tests/18pg_escape_bytea_before.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ else {
2828
echo "OK";
2929
}
3030
?>
31-
--EXPECT--
31+
--EXPECTF--
32+
Deprecated: pg_escape_bytea(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
3233
OK

ext/pgsql/tests/18pg_escape_bytea_esc.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $db = pg_connect($conn_str);
1414
@pg_query($db, "SET bytea_output = 'escape'");
1515

1616
$image = file_get_contents(__DIR__ . '/php.gif');
17-
$esc_image = pg_escape_bytea($image);
17+
$esc_image = pg_escape_bytea($db, $image);
1818

1919
pg_query($db, 'INSERT INTO '.$table_name.' (num, bin) VALUES (9876, \''.$esc_image.'\');');
2020
$result = pg_query($db, 'SELECT * FROM '.$table_name.' WHERE num = 9876');

0 commit comments

Comments
 (0)