8000 Deprecate using the implicit default PgSQL connection · nikic/php-src@1f42777 · GitHub
[go: up one dir, main page]

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');

ext/pgsql/tests/18pg_escape_bytea_hex.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $db = pg_connect($conn_str);
1717
@pg_query($db, "SET bytea_output = 'hex'");
1818

1919
$image = file_get_contents(__DIR__ . '/php.gif');
20-
$esc_image = pg_escape_bytea($image);
20+
$esc_image = pg_escape_bytea($db, $image);
2121

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

ext/pgsql/tests/27large_object_oid.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ pg_exec ("commit");
4242

4343
echo "OK";
4444
?>
45-
--EXPECT--
45+
--EXPECTF--
4646
create LO from int
4747
create LO from string
4848
create LO using default connection
49+
50+
Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
51+
52+
Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
53+
54+
Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
55+
56+
Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
4957
OK

ext/pgsql/tests/28large_object_import_oid.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,26 @@ try {
8484

8585
echo "OK";
8686
?>
87-
--EXPECT--
87+
--EXPECTF--
8888
import LO from int
8989
import LO from string
9090
import LO using default connection
91+
92+
Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
93+
94+
Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
95+
96+
Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
97+
98+
Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
9199
Invalid OID value passed
92100
Invalid OID value passed
101+
102+
Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
93103
Invalid OID value passed
94104
Invalid OID value passed
105+
106+
Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
95107
OID value must be of type string|int, bool given
96108
OID value must be of type string|int, array given
97109
OID value must be of type string|int, stdClass given

ext/pgsql/tests/80_bug24499.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ if (!$dbh) {
1616
die ("Could not connect to the server");
1717
}
1818

19-
@pg_query("DROP SEQUENCE id_id_seq");
20-
@pg_query("DROP TABLE id");
21-
pg_query("CREATE TABLE id (id SERIAL, t INT)");
19+
@pg_query($dbh, "DROP SEQUENCE id_id_seq");
20+
@pg_query($dbh, "DROP TABLE id");
21+
pg_query($dbh, "CREATE TABLE id (id SERIAL, t INT)");
2222

2323
for ($i=0; $i<4; $i++) {
24-
pg_query("INSERT INTO id (t) VALUES ($i)");
24+
pg_query($dbh, "INSERT INTO id (t) VALUES ($i)");
2525
}
2626

2727
class Id

ext/pgsql/tests/80_bug27597.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ if (!$dbh) {
1616
die ("Could not connect to the server");
1717
}
1818

19-
@pg_query("DROP TABLE id");
20-
pg_query("CREATE TABLE id (id INT)");
19+
@pg_query($dbh, "DROP TABLE id");
20+
pg_query($dbh, "CREATE TABLE id (id INT)");
2121

2222
for ($i=0; $i<4; $i++) {
23-
pg_query("INSERT INTO id (id) VALUES ($i)");
23+
pg_query($dbh, "INSERT INTO id (id) VALUES ($i)");
2424
}
2525

2626
function xi_fetch_array($res, $type = PGSQL_ASSOC) {
2727
$a = pg_fetch_array($res, NULL, $type) ;
2828
return $a ;
2929
}
3030

31-
$res = pg_query("SELECT * FROM id");
31+
$res = pg_query($dbh, "SELECT * FROM id");
3232
$i = 0; // endless-loop protection
3333
while($row = xi_fetch_array($res)) {
3434
print_r($row);
10000

ext/pgsql/tests/80_bug32223.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pgsql
66
<?php
77
require_once('skipif.inc');
88

9-
_skip_lc_messages();
9+
_skip_lc_messages($conn);
1010

1111
@pg_query($conn, "CREATE LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler LANCOMPILER 'PL/pgSQL'");
1212
$res = @pg_query($conn, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
@@ -30,7 +30,7 @@ if (!$dbh) {
3030
die ("Could not connect to the server");
3131
}
3232

33-
_set_lc_messages();
33+
_set_lc_messages($dbh);
3434

3535
$res = pg_query($dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
3636
begin

ext/pgsql/tests/80_bug32223b.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pgsql
66
<?php
77
require_once('skipif.inc');
88

9-
_skip_lc_messages();
9+
_skip_lc_messages($conn);
1010

1111
@pg_query($conn, "CREATE LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler LANCOMPILER 'PL/pgSQL'");
1212
$res = @pg_query($conn, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
@@ -30,7 +30,7 @@ if (!$dbh) {
3030
die ("Could not connect to the server");
3131
}
3232

33-
_set_lc_messages();
33+
_set_lc_messages($dbh);
3434

3535
$res = pg_query($dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
3636
begin

ext/pgsql/tests/80_bug39971.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (!$dbh) {
1616
die ("Could not connect to the server");
1717
}
1818

19-
pg_query("CREATE TABLE php_test (id SERIAL, tm timestamp NOT NULL)");
19+
pg_query($dbh, "CREATE TABLE php_test (id SERIAL, tm timestamp NOT NULL)");
2020

2121
$values = array('tm' => 'now()');
2222
pg_insert($dbh, 'php_test', $values);

ext/pgsql/tests/80_bug42783.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ if (!$dbh) {
1616
die ("Could not connect to the server");
1717
}
1818

19-
pg_query("CREATE TABLE php_test (id SERIAL PRIMARY KEY, time TIMESTAMP NOT NULL DEFAULT now())");
19+
pg_query($dbh, "CREATE TABLE php_test (id SERIAL PRIMARY KEY, time TIMESTAMP NOT NULL DEFAULT now())");
2020

2121
pg_insert($dbh, 'php_test', array());
2222

23-
var_dump(pg_fetch_assoc(pg_query("SELECT * FROM php_test")));
23+
var_dump(pg_fetch_assoc(pg_query($dbh, "SELECT * FROM php_test")));
2424

2525
pg_query($dbh, "DROP TABLE php_test");
2626
pg_close($dbh);

ext/pgsql/tests/98old_api.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pgsql
1010
include('config.inc');
1111

1212
$db = pg_connect($conn_str);
13-
$result = pg_exec("SELECT * FROM ".$table_name);
13+
$result = pg_exec($db, "SELECT * FROM ".$table_name);
1414
pg_numrows($result);
1515
pg_numfields($result);
1616
pg_fieldname($result, 0);
@@ -20,11 +20,11 @@ pg_fieldprtlen($result, 0);
2020
pg_fieldisnull($result, 0);
2121

2222
pg_result($result,0,0);
23-
$result = pg_exec("INSERT INTO ".$table_name." VALUES (7777, 'KKK')");
23+
$result = pg_exec($db, "INSERT INTO ".$table_name." VALUES (7777, 'KKK')");
2424
$oid = pg_getlastoid($result);
2525
pg_freeresult($result);
26-
pg_errormessage();
27-
$result = pg_exec("UPDATE ".$table_name." SET str = 'QQQ' WHERE str like 'RGD';");
26+
pg_errormessage($db);
27+
$result = pg_exec($db, "UPDATE ".$table_name." SET str = 'QQQ' WHERE str like 'RGD';");
2828
pg_cmdtuples($result);
2929

3030

ext/pgsql/tests/bug37100_9.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ include 'config.inc';
1414

1515
$db = pg_connect($conn_str);
1616

17-
@pg_query('DROP TABLE test_bug');
17+
@pg_query($db, 'DROP TABLE test_bug');
1818

19-
pg_query('CREATE TABLE test_bug (binfield byteA) ;');
20-
pg_query("INSERT INTO test_bug VALUES (decode('0103AA000812','hex'))");
19+
pg_query($db, 'CREATE TABLE test_bug (binfield byteA) ;');
20+
pg_query($db, "INSERT INTO test_bug VALUES (decode('0103AA000812','hex'))");
2121

2222

23-
$data = pg_query("SELECT binfield FROM test_bug");
23+
$data = pg_query($db, "SELECT binfield FROM test_bug");
2424
$res = pg_fetch_result($data,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

30-
$data = pg_query($sql);
30+
$data = pg_query($db, $sql);
3131
$res = pg_fetch_result($data,0);
3232

3333
var_dump(strlen($res));
@@ -36,7 +36,7 @@ var_dump(bin2hex($res));
3636
pg_close($db);
3737

3838
$db = pg_connect($conn_str);
39-
pg_query('DROP TABLE test_bug');
39+
pg_query($db, 'DROP TABLE test_bug');
4040
pg_close($db);
4141

4242

0 commit comments

Comments
 (0)
0