~ubuntu-branches/ubuntu/quantal/php5/quantal

« back to all changes in this revision

Viewing changes to ext/pgsql/tests/08escape.phpt

  • Committer: Bazaar Package Importer
  • Author(s): Sean Finney
  • Date: 2009-07-01 09:12:10 UTC
  • mto: (0.9.1) (1.1.17 upstream)
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: james.westby@ubuntu.com-20090701091210-go0h6506p62on17r
Tags: upstream-5.3.0
ImportĀ upstreamĀ versionĀ 5.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--TEST--
 
2
PostgreSQL escape functions
 
3
--SKIPIF--
 
4
<?php include("skipif.inc"); ?>
 
5
--FILE--
 
6
<?php
 
7
 
 
8
include 'config.inc';
 
9
define('FILE_NAME', dirname(__FILE__) . '/php.gif');
 
10
 
 
11
// pg_escape_string() test
 
12
$before = "ABC\\ABC\'";
 
13
$expect  = "ABC\\\\ABC\\'";
 
14
$after = pg_escape_string($before);
 
15
if ($expect === $after) {
 
16
        echo "pg_escape_string() is Ok\n";
 
17
}
 
18
else {
 
19
        echo "pg_escape_string() is NOT Ok\n";
 
20
        var_dump($before);
 
21
        var_dump($after);
 
22
        var_dump($expect);
 
23
}
 
24
 
 
25
// pg_escape_bytea() test
 
26
$before = "ABC\\ABC";
 
27
$expect  = "ABC\\\\\\\\ABC";
 
28
$after  = pg_escape_bytea($before);
 
29
if ($expect === $after) {
 
30
        echo "pg_escape_bytea() is Ok\n";
 
31
}
 
32
else {
 
33
        echo "pg_escape_byte() is NOT Ok\n";
 
34
        var_dump($before);
 
35
        var_dump($after);
 
36
        var_dump($expect);
 
37
}
 
38
 
 
39
// Test using database
 
40
$data = file_get_contents(FILE_NAME);
 
41
$db   = pg_connect($conn_str);
 
42
 
 
43
// Insert binary to DB
 
44
$escaped_data = pg_escape_bytea($data);
 
45
pg_query("DELETE FROM ".$table_name." WHERE num = -9999;");
 
46
$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (-9999, CAST ('".$escaped_data."' AS BYTEA));";
 
47
pg_query($db, $sql);
 
48
 
 
49
// Retrieve binary from DB
 
50
$sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
 
51
$result = pg_query($db, $sql);
 
52
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
 
53
 
 
54
if ($data === pg_unescape_bytea($row['bin'])) {
 
55
        echo "pg_escape_bytea() actually works with database\n";
 
56
}
 
57
else {
 
58
        echo "pg_escape_bytea() is broken\n";
 
59
}
 
60
 
 
61
?>
 
62
--EXPECT--
 
63
pg_escape_string() is NOT Ok
 
64
string(9) "ABC\ABC\'"
 
65
string(12) "ABC\\ABC\\''"
 
66
string(10) "ABC\\ABC\'"
 
67
pg_escape_bytea() is Ok
 
68
pg_escape_bytea() actually works with database