Skip to content

Deprecate implicit default PGSQL connection #6790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ PHP 8.1 UPGRADE NOTES
. The PgSQL functions now accept and return, respectively, \PgSql\Lob
objects instead of "pgsql large object" resources. Return value checks
using is_resource() should be replaced with checks for `false`.
. Not passing the connection argument to PgSQL functions and using the
default connection is deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_1

- PSpell:
. The PSpell functions now accept and return, respectively, PSpell\Dictionary objects
Expand Down
11 changes: 9 additions & 2 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,15 @@
zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
RETURN_THROWS(); \
}

/* This is a bit hacky as the macro usage is "link = FETCH_DEFAULT_LINK();" */
#define FETCH_DEFAULT_LINK() \
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL); \
php_error_docref(NULL, E_DEPRECATED, "Automatic fetching of PostgreSQL connection is deprecated")

/* Used only when creating a connection */
#define FETCH_DEFAULT_LINK_NO_WARNING() \
(PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)

#define CHECK_PGSQL_LINK(link_handle) \
if (link_handle->conn == NULL) { \
Expand Down Expand Up @@ -850,7 +857,7 @@ PHP_FUNCTION(pg_close)
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);

if (link == FETCH_DEFAULT_LINK()) {
if (link == FETCH_DEFAULT_LINK_NO_WARNING()) {
GC_DELREF(PGG(default_link));
PGG(default_link) = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/tests/01createdb.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ else {
echo pg_last_error()."\n";
}

$v = pg_version();
$v = pg_version($db);
if (version_compare($v['server'], '9.2', '>=') && (!($q = @pg_query($db, "SELECT * FROM ".$table_name_92)) || !@pg_num_rows($q)))
{
pg_query($db,$table_def_92); // Create table here
Expand Down
10 changes: 8 additions & 2 deletions ext/pgsql/tests/05large_object.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $handle = pg_lo_open ($db, $oid, "w");
$bytesWritten = pg_lo_read_all($handle);
echo "\n";
var_dump($bytesWritten);
if (pg_last_error()) echo "pg_lo_read_all() error\n".pg_last_error();
if (pg_last_error($db)) echo "pg_lo_read_all() error\n".pg_last_error();
pg_lo_close($handle);
pg_exec ($db, "commit");

Expand Down Expand Up @@ -103,7 +103,7 @@ try {

echo "OK";
?>
--EXPECT--
--EXPECTF--
create/write/close LO
open/read/tell/seek/close LO
string(5) "large"
Expand All @@ -120,10 +120,16 @@ large object data
int(17)
unlink LO
Test without connection

Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
Test with string oid value
import/export LO

Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
Invalid OID value passed
Invalid OID value passed

Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
Invalid OID value passed
Invalid OID value passed
OK
6 changes: 3 additions & 3 deletions ext/pgsql/tests/07optional.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ $enc = pg_client_encoding($db);
pg_set_client_encoding($db, $enc);

if (function_exists('pg_set_error_verbosity')) {
pg_set_error_verbosity(PGSQL_ERRORS_TERSE);
pg_set_error_verbosity(PGSQL_ERRORS_DEFAULT);
pg_set_error_verbosity(PGSQL_ERRORS_VERBOSE);
pg_set_error_verbosity($db, PGSQL_ERRORS_TERSE);
pg_set_error_verbosity($db, PGSQL_ERRORS_DEFAULT);
pg_set_error_verbosity($db, PGSQL_ERRORS_VERBOSE);
}
echo "OK";
?>
Expand Down
13 changes: 8 additions & 5 deletions ext/pgsql/tests/08escape.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ $data = file_get_contents(FILE_NAME);
$db = pg_connect($conn_str);

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

Expand Down Expand Up @@ -73,7 +73,7 @@ for ($i = 0; $i < 2; $i++) {
// pg_escape_literal/pg_escape_identifier
$before = "ABC\\ABC\'";
$expect = " E'ABC\\\\ABC\\\\'''";
$after = pg_escape_literal($before);
$after = pg_escape_literal($db, $before);
if ($expect === $after) {
echo "pg_escape_literal() is Ok\n";
}
Expand All @@ -86,7 +86,7 @@ else {

$before = "ABC\\ABC\'";
$expect = "\"ABC\ABC\'\"";
$after = pg_escape_identifier($before);
$after = pg_escape_identifier($db, $before);
if ($expect === $after) {
echo "pg_escape_identifier() is Ok\n";
}
Expand All @@ -98,8 +98,11 @@ else {
}

?>
--EXPECT--
--EXPECTF--
Deprecated: pg_escape_string(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
pg_escape_string() is Ok

Deprecated: pg_escape_bytea(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
pg_escape_bytea() is Ok
pg_escape_bytea() actually works with database
pg_escape_literal() is Ok
Expand Down
4 changes: 2 additions & 2 deletions ext/pgsql/tests/09notice.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pgsql

include("skipif.inc");

_skip_lc_messages();
_skip_lc_messages($conn);

?>
--FILE--
Expand All @@ -20,7 +20,7 @@ ini_set('pgsql.ignore_notice', FALSE);

$db = pg_connect($conn_str);

_set_lc_messages();
_set_lc_messages($db);

$res = pg_query($db, 'SET client_min_messages TO NOTICE;');
var_dump($res);
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/tests/13pg_select_9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ error_reporting(E_ALL);
include 'config.inc';

$db = pg_connect($conn_str);
pg_query("SET bytea_output = 'hex'");
pg_query($db, "SET bytea_output = 'hex'");

$fields = array('num'=>'1234', 'str'=>'ABC', 'bin'=>'XYZ');
$ids = array('num'=>'1234');
Expand Down
3 changes: 2 additions & 1 deletion ext/pgsql/tests/18pg_escape_bytea_before.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ else {
echo "OK";
}
?>
--EXPECT--
--EXPECTF--
Deprecated: pg_escape_bytea(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
OK
2 changes: 1 addition & 1 deletion ext/pgsql/tests/18pg_escape_bytea_esc.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $db = pg_connect($conn_str);
@pg_query($db, "SET bytea_output = 'escape'");

$image = file_get_contents(__DIR__ . '/php.gif');
$esc_image = pg_escape_bytea($image);
$esc_image = pg_escape_bytea($db, $image);

pg_query($db, 'INSERT INTO '.$table_name.' (num, bin) VALUES (9876, \''.$esc_image.'\');');
$result = pg_query($db, 'SELECT * FROM '.$table_name.' WHERE num = 9876');
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/tests/18pg_escape_bytea_hex.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $db = pg_connect($conn_str);
@pg_query($db, "SET bytea_output = 'hex'");

$image = file_get_contents(__DIR__ . '/php.gif');
$esc_image = pg_escape_bytea($image);
$esc_image = pg_escape_bytea($db, $image);

pg_query($db, 'INSERT INTO '.$table_name.' (num, bin) VALUES (9876, \''.$esc_image.'\');');
$result = pg_query($db, 'SELECT * FROM '.$table_name.' WHERE num = 9876');
Expand Down
10 changes: 9 additions & 1 deletion ext/pgsql/tests/27large_object_oid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ pg_exec ("commit");

echo "OK";
?>
--EXPECT--
--EXPECTF--
create LO from int
create LO from string
create LO using default connection

Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
OK
14 changes: 13 additions & 1 deletion ext/pgsql/tests/28large_object_import_oid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,26 @@ try {

echo "OK";
?>
--EXPECT--
--EXPECTF--
import LO from int
import LO from string
import LO using default connection

Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_exec(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d

Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
Invalid OID value passed
Invalid OID value passed

Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
Invalid OID value passed
Invalid OID value passed

Deprecated: pg_lo_import(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
OID value must be of type string|int, bool given
OID value must be of type string|int, array given
OID value must be of type string|int, stdClass given
Expand Down
8 changes: 4 additions & 4 deletions ext/pgsql/tests/80_bug24499.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ if (!$dbh) {
die ("Could not connect to the server");
}

@pg_query("DROP SEQUENCE id_id_seq");
@pg_query("DROP TABLE id");
pg_query("CREATE TABLE id (id SERIAL, t INT)");
@pg_query($dbh, "DROP SEQUENCE id_id_seq");
@pg_query($dbh, "DROP TABLE id");
pg_query($dbh, "CREATE TABLE id (id SERIAL, t INT)");

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

class Id
Expand Down
8 changes: 4 additions & 4 deletions ext/pgsql/tests/80_bug27597.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ if (!$dbh) {
die ("Could not connect to the server");
}

@pg_query("DROP TABLE id");
pg_query("CREATE TABLE id (id INT)");
@pg_query($dbh, "DROP TABLE id");
pg_query($dbh, "CREATE TABLE id (id INT)");

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

function xi_fetch_array($res, $type = PGSQL_ASSOC) {
$a = pg_fetch_array($res, NULL, $type) ;
return $a ;
}

$res = pg_query("SELECT * FROM id");
$res = pg_query($dbh, "SELECT * FROM id");
$i = 0; // endless-loop protection
while($row = xi_fetch_array($res)) {
print_r($row);
Expand Down
4 changes: 2 additions & 2 deletions ext/pgsql/tests/80_bug32223.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pgsql
<?php
require_once('skipif.inc');

_skip_lc_messages();
_skip_lc_messages($conn);

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

_set_lc_messages();
_set_lc_messages($dbh);

$res = pg_query($dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
begin
Expand Down
4 changes: 2 additions & 2 deletions ext/pgsql/tests/80_bug32223b.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pgsql
<?php
require_once('skipif.inc');

_skip_lc_messages();
_skip_lc_messages($conn);

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

_set_lc_messages();
_set_lc_messages($dbh);

$res = pg_query($dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
begin
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/tests/80_bug39971.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (!$dbh) {
die ("Could not connect to the server");
}

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

$values = array('tm' => 'now()');
pg_insert($dbh, 'php_test', $values);
Expand Down
4 changes: 2 additions & 2 deletions ext/pgsql/tests/80_bug42783.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ if (!$dbh) {
die ("Could not connect to the server");
}

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

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

var_dump(pg_fetch_assoc(pg_query("SELECT * FROM php_test")));
var_dump(pg_fetch_assoc(pg_query($dbh, "SELECT * FROM php_test")));

pg_query($dbh, "DROP TABLE php_test");
pg_close($dbh);
Expand Down
8 changes: 4 additions & 4 deletions ext/pgsql/tests/98old_api.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pgsql
include('config.inc');

$db = pg_connect($conn_str);
$result = pg_exec("SELECT * FROM ".$table_name);
$result = pg_exec($db, "SELECT * FROM ".$table_name);
pg_numrows($result);
pg_numfields($result);
pg_fieldname($result, 0);
Expand All @@ -20,11 +20,11 @@ pg_fieldprtlen($result, 0);
pg_fieldisnull($result, 0);

pg_result($result,0,0);
$result = pg_exec("INSERT INTO ".$table_name." VALUES (7777, 'KKK')");
$result = pg_exec($db, "INSERT INTO ".$table_name." VALUES (7777, 'KKK')");
$oid = pg_getlastoid($result);
pg_freeresult($result);
pg_errormessage();
$result = pg_exec("UPDATE ".$table_name." SET str = 'QQQ' WHERE str like 'RGD';");
pg_errormessage($db);
$result = pg_exec($db, "UPDATE ".$table_name." SET str = 'QQQ' WHERE str like 'RGD';");
pg_cmdtuples($result);


Expand Down
12 changes: 6 additions & 6 deletions ext/pgsql/tests/bug37100_9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ include 'config.inc';

$db = pg_connect($conn_str);

@pg_query('DROP TABLE test_bug');
@pg_query($db, 'DROP TABLE test_bug');

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


$data = pg_query("SELECT binfield FROM test_bug");
$data = pg_query($db, "SELECT binfield FROM test_bug");
$res = pg_fetch_result($data,0);
var_dump($res);
var_dump(bin2hex(pg_unescape_bytea($res)));

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

$data = pg_query($sql);
$data = pg_query($db, $sql);
$res = pg_fetch_result($data,0);

var_dump(strlen($res));
Expand All @@ -36,7 +36,7 @@ var_dump(bin2hex($res));
pg_close($db);

$db = pg_connect($conn_str);
pg_query('DROP TABLE test_bug');
pg_query($db, 'DROP TABLE test_bug');
pg_close($db);


Expand Down
Loading