$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
+try {
+ $db->query("SET bytea_output = 'escape'");
+} catch (Exception $e) {
+}
+
$db->query('CREATE TABLE test_one_blob (id SERIAL NOT NULL, blob1 BYTEA)');
$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES (:foo)");
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+try {
+ $db->query("SET bytea_output = 'escape'");
+} catch (Exception $e) {
+}
+
$db->query('CREATE TABLE test_one_blob (id SERIAL NOT NULL, blob1 BYTEA)');
$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES (:foo)");
pg_query($db, $sql);
// Retrieve binary from DB
-$sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
-$result = pg_query($db, $sql);
-$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
+for ($i = 0; $i < 2; $i++) {
+ $sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
+ $result = pg_query($db, $sql);
+ $row = pg_fetch_array($result, 0, PGSQL_ASSOC);
-if ($data === pg_unescape_bytea($row['bin'])) {
- echo "pg_escape_bytea() actually works with database\n";
-}
-else {
- echo "pg_escape_bytea() is broken\n";
+ if ($data === pg_unescape_bytea($row['bin'])) {
+ echo "pg_escape_bytea() actually works with database\n";
+ break;
+ }
+ elseif (!$i) {
+ // Force bytea escaping and retry
+ @pg_query($db, "SET bytea_output = 'escape'");
+ }
+ else {
+ $result = pg_query($db, $sql);
+ echo "pg_escape_bytea() is broken\n";
+ break;
+ }
}
// pg_escape_literal/pg_escape_identifier
pg_escape_bytea() is Ok
pg_escape_bytea() actually works with database
pg_escape_literal() is Ok
-pg_escape_identifier() is Ok
\ No newline at end of file
+pg_escape_identifier() is Ok
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '>=');
+skip_bytea_not_escape();
?>
--FILE--
<?php
--TEST--
-PostgreSQL pg_convert() (8.5+)
+PostgreSQL pg_convert() (9.0+)
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '<');
+skip_bytea_not_hex();
?>
--FILE--
<?php
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '>=');
+skip_bytea_not_escape();
?>
--FILE--
<?php
--TEST--
-PostgreSQL pg_insert() (8.5+)
+PostgreSQL pg_insert() (9.0+)
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '<');
+skip_bytea_not_hex();
?>
--FILE--
<?php
--TEST--
-PostgreSQL pg_select() (8.5+)
+PostgreSQL pg_select() (9.0+)
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '<');
+skip_server_version('9.0', '<');
?>
--FILE--
<?php
include 'config.inc';
$db = pg_connect($conn_str);
+pg_query("SET bytea_output = 'hex'");
+
$fields = array('num'=>'1234', 'str'=>'ABC', 'bin'=>'XYZ');
$ids = array('num'=>'1234');
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '>=');
+skip_bytea_not_escape();
?>
--FILE--
<?php
--TEST--
-PostgreSQL pg_update() (8.5+)
+PostgreSQL pg_update() (9.0)
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '<');
+skip_bytea_not_hex();
?>
--FILE--
<?php
--- /dev/null
+--TEST--
+PostgreSQL pg_escape_bytea() functions (before connection)
+--SKIPIF--
+<?php include("skipif.inc"); ?>
+--FILE--
+<?php
+// optional functions
+
+include('config.inc');
+
+$image = file_get_contents(dirname(__FILE__) . '/php.gif');
+$esc_image = pg_escape_bytea($image);
+
+$db = pg_connect($conn_str);
+@pg_query($db, "SET bytea_output = 'escape'");
+
+pg_query($db, 'INSERT INTO '.$table_name.' (num, bin) VALUES (9876, E\''.$esc_image.'\');');
+$result = pg_query($db, 'SELECT * FROM '.$table_name.' WHERE num = 9876');
+$rows = pg_fetch_all($result);
+$unesc_image = pg_unescape_bytea($rows[0]['bin']);
+
+if ($unesc_image !== $image) {
+ echo "NG";
+}
+else {
+ echo "OK";
+}
+?>
+--EXPECT--
+OK
--TEST--
-PostgreSQL pg_escape_bytea() functions
+PostgreSQL pg_escape_bytea() functions (escape format)
--SKIPIF--
<?php include("skipif.inc"); ?>
--FILE--
include('config.inc');
$db = pg_connect($conn_str);
+@pg_query($db, "SET bytea_output = 'escape'");
$image = file_get_contents(dirname(__FILE__) . '/php.gif');
$esc_image = pg_escape_bytea($image);
--- /dev/null
+--TEST--
+PostgreSQL pg_escape_bytea() functions (hex format)
+--SKIPIF--
+<?php
+include("skipif.inc");
+skip_bytea_not_hex();
+?>
+--FILE--
+<?php
+// optional functions
+
+include('config.inc');
+
+$db = pg_connect($conn_str);
+@pg_query($db, "SET bytea_output = 'hex'");
+
+$image = file_get_contents(dirname(__FILE__) . '/php.gif');
+$esc_image = pg_escape_bytea($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');
+$rows = pg_fetch_all($result);
+$unesc_image = pg_unescape_bytea($rows[0]['bin']);
+
+if ($unesc_image !== $image) {
+ echo "NG";
+}
+else {
+ echo "OK";
+}
+?>
+--EXPECT--
+OK
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '>=');
+skip_bytea_not_escape();
?>
--FILE--
<?php
include 'config.inc';
$db = pg_connect($conn_str);
+@pg_query("SET bytea_output = 'escape'");
@pg_query('DROP TABLE test_bug');
--TEST--
-Bug #37100 (data is returned truncated with BINARY CURSOR) (8.5+)
+Bug #37100 (data is returned truncated with BINARY CURSOR) (9.0+)
--SKIPIF--
<?php
include("skipif.inc");
-skip_server_version('8.5dev', '<');
+skip_bytea_not_hex();
?>
--FILE--
<?php
if (version_compare($pg, $version, $op)) {
die("skip Server version {$pg} is {$op} {$version}\n");
}
+ return $pg;
+}
+
+function skip_bytea_not_hex()
+{
+ $out = pg_escape_bytea("\xFF");
+ if (strpos($out, '377') !== false) {
+ die("skip libpq or backend < 9.0\n");
+ }
+}
+
+function skip_bytea_not_escape()
+{
+ $out = pg_escape_bytea("\xFF");
+ if (strpos($out, '377') === false) {
+ die("skip libpq or backend >= 9.0\n");
+ }
}
?>