summaryrefslogtreecommitdiff
path: root/ext/pgsql/tests/escape.inc
blob: a39e1c16211326a16b2f065a49199fa35d0182ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

include 'config.inc';
define('FILE_NAME', './php.gif');

// pg_escape_string() test
$before = "ABC\\ABC\'";
$expect  = "ABC\\\\ABC\\'";
$after = pg_escape_string($before);
if ($expect === $after) {
	echo "pg_escape_string() is Ok\n";
}
else {
	echo "pg_escape_string() is NOT Ok\n";
	var_dump($before);
	var_dump($after);
	var_dump($expect);
}

// pg_escape_bytea() test
$before = "ABC\\ABC";
$expect  = "ABC\\\\\\\\ABC";
$after  = pg_escape_bytea($before);
if ($expect === $after) {
	echo "pg_escape_bytea() is Ok\n";
}
else {
	echo "pg_escape_byte() is NOT Ok\n";
	var_dump($before);
	var_dump($after);
	var_dump($expect);
}

// Test using database
$fp   = fopen(FILE_NAME,'r');
$data = fread($fp, filesize(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 = -9999;");
$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (-9999, CAST ('".$escaped_data."' AS BYTEA));";
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);

// Compare
// Need to wait PostgreSQL 7.3.x for PQunescapeBytea()
// if ($data === pg_unescape_bytea($row['bin'])) {
// 	echo "pg_escape_bytea() actually works with databse\n";
// }
// else {
// 	echo "pg_escape_bytea() is broken\n";
// }

?>