forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05large_object.phpt
141 lines (127 loc) · 3.73 KB
/
05large_object.phpt
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
--TEST--
PostgreSQL large object
--EXTENSIONS--
pgsql
--SKIPIF--
<?php include("inc/skipif.inc"); ?>
--FILE--
<?php
include('inc/config.inc');
$db = pg_connect($conn_str);
echo "create/write/close LO\n";
pg_exec ($db, "BEGIN");
$oid = pg_lo_create ($db);
if (!$oid) echo ("pg_lo_create() error\n");
$handle = pg_lo_open ($db, $oid, "w");
if (!$handle) echo ("pg_lo_open() error\n");
try {
pg_lo_write ($handle, "large\0object data");
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
pg_lo_write ($handle, "large object data");
pg_lo_close ($handle);
pg_exec ($db, "COMMIT");
echo "open/read/tell/seek/close LO\n";
pg_exec ($db, "BEGIN");
$handle = pg_lo_open ($db, $oid, "w");
var_dump(pg_lo_read($handle, 5));
var_dump(pg_lo_tell($handle));
var_dump(pg_lo_seek($handle, 2, /* PGSQL_SEEK_CUR */)); // This is the default so move cursor from 5
var_dump(pg_lo_read($handle, 100)); // Read to the end because chunk is larger then remaining content
var_dump(pg_lo_tell($handle));
var_dump(pg_lo_seek($handle, 0, PGSQL_SEEK_SET)); /* Reset cursor to beginning */
var_dump(pg_lo_read($handle));
var_dump(pg_lo_seek($handle, -4, PGSQL_SEEK_END)); /* Seek from the end */
var_dump(pg_lo_read($handle));
pg_lo_close($handle);
pg_exec ($db, "COMMIT");
echo "open/read_all/close LO\n";
pg_exec ($db, "BEGIN");
$handle = pg_lo_open ($db, $oid, "w");
/* Will write to stdout */
$bytesWritten = pg_lo_read_all($handle);
echo "\n";
var_dump($bytesWritten);
if (pg_last_error($db)) echo "pg_lo_read_all() error\n".pg_last_error();
pg_lo_close($handle);
pg_exec ($db, "COMMIT");
echo "unlink LO\n";
pg_exec ($db, "BEGIN");
pg_lo_unlink($db, $oid) or print("pg_lo_unlink() error 1\n");
pg_exec ($db, "COMMIT");
// more pg_lo_unlink() tests
echo "Test without connection\n";
pg_exec ($db, "BEGIN");
$oid = pg_lo_create ($db) or print("pg_lo_create() error\n");
pg_lo_unlink($oid) or print("pg_lo_unlink() error 2\n");
pg_exec ($db, "COMMIT");
echo "Test with string oid value\n";
pg_exec ($db, "BEGIN");
$oid = pg_lo_create ($db) or print("pg_lo_create() error\n");
pg_lo_unlink($db, (string)$oid) or print("pg_lo_unlink() error 3\n");
pg_exec ($db, "COMMIT");
echo "import/export LO\n";
$path = __DIR__ . '/';
pg_query($db, 'BEGIN');
$oid = pg_lo_import($db, $path . 'php.gif');
pg_query($db, 'COMMIT');
pg_query($db, 'BEGIN');
@unlink($path . 'php.gif.exported');
pg_lo_export($db, $oid, $path . 'php.gif.exported');
if (!file_exists($path . 'php.gif.exported')) {
echo "Export failed\n";
}
@unlink($path . 'php.gif.exported');
pg_query($db, 'COMMIT');
/* invalid OID values */
try {
pg_lo_create(-15);
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
pg_lo_create($db, -15);
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
pg_lo_create('giberrish');
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
pg_lo_create($db, 'giberrish');
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
echo "OK";
?>
--EXPECTF--
create/write/close LO
pg_lo_write(): Argument #2 ($data) must not contain any null bytes
open/read/tell/seek/close LO
string(5) "large"
int(5)
bool(true)
string(10) "bject data"
int(17)
bool(true)
string(17) "large object data"
bool(true)
string(4) "data"
open/read_all/close LO
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