xref: /PHP-8.1/ext/pgsql/tests/05large_object.phpt (revision 1f427779)
1--TEST--
2PostgreSQL large object
3--EXTENSIONS--
4pgsql
5--SKIPIF--
6<?php include("skipif.inc"); ?>
7--FILE--
8<?php
9
10include('config.inc');
11
12$db = pg_connect($conn_str);
13
14echo "create/write/close LO\n";
15pg_exec ($db, "begin");
16$oid = pg_lo_create ($db);
17if (!$oid) echo ("pg_lo_create() error\n");
18$handle = pg_lo_open ($db, $oid, "w");
19if (!$handle) echo ("pg_lo_open() error\n");
20pg_lo_write ($handle, "large object data");
21pg_lo_close ($handle);
22pg_exec ($db, "commit");
23
24echo "open/read/tell/seek/close LO\n";
25pg_exec ($db, "begin");
26$handle = pg_lo_open ($db, $oid, "w");
27var_dump(pg_lo_read($handle, 5));
28var_dump(pg_lo_tell($handle));
29var_dump(pg_lo_seek($handle, 2, /* PGSQL_SEEK_CUR */)); // This is the default so move cursor from 5
30var_dump(pg_lo_read($handle, 100)); // Read to the end because chunk is larger then remaining content
31var_dump(pg_lo_tell($handle));
32var_dump(pg_lo_seek($handle, 0, PGSQL_SEEK_SET)); /* Reset cursor to beginning */
33var_dump(pg_lo_read($handle));
34var_dump(pg_lo_seek($handle, -4, PGSQL_SEEK_END)); /* Seek from the end */
35var_dump(pg_lo_read($handle));
36pg_lo_close($handle);
37pg_exec ($db, "commit");
38
39echo "open/read_all/close LO\n";
40pg_exec ($db, "begin");
41$handle = pg_lo_open ($db, $oid, "w");
42/* Will write to stdout */
43$bytesWritten = pg_lo_read_all($handle);
44echo "\n";
45var_dump($bytesWritten);
46if (pg_last_error($db)) echo "pg_lo_read_all() error\n".pg_last_error();
47pg_lo_close($handle);
48pg_exec ($db, "commit");
49
50echo "unlink LO\n";
51pg_exec ($db, "begin");
52pg_lo_unlink($db, $oid) or print("pg_lo_unlink() error 1\n");
53pg_exec ($db, "commit");
54
55// more pg_lo_unlink() tests
56echo "Test without connection\n";
57pg_exec ($db, "begin");
58$oid = pg_lo_create ($db) or print("pg_lo_create() error\n");
59pg_lo_unlink($oid) or print("pg_lo_unlink() error 2\n");
60pg_exec ($db, "commit");
61
62echo "Test with string oid value\n";
63pg_exec ($db, "begin");
64$oid = pg_lo_create ($db) or print("pg_lo_create() error\n");
65pg_lo_unlink($db, (string)$oid) or print("pg_lo_unlink() error 3\n");
66pg_exec ($db, "commit");
67
68echo "import/export LO\n";
69$path = __DIR__ . '/';
70pg_query($db, 'begin');
71$oid = pg_lo_import($db, $path . 'php.gif');
72pg_query($db, 'commit');
73pg_query($db, 'begin');
74@unlink($path . 'php.gif.exported');
75pg_lo_export($db, $oid, $path . 'php.gif.exported');
76if (!file_exists($path . 'php.gif.exported')) {
77    echo "Export failed\n";
78}
79@unlink($path . 'php.gif.exported');
80pg_query($db, 'commit');
81
82/* invalid OID values */
83try {
84    pg_lo_create(-15);
85} catch (\ValueError $e) {
86    echo $e->getMessage(), \PHP_EOL;
87}
88try {
89    pg_lo_create($db, -15);
90} catch (\ValueError $e) {
91    echo $e->getMessage(), \PHP_EOL;
92}
93try {
94    pg_lo_create('giberrish');
95} catch (\ValueError $e) {
96    echo $e->getMessage(), \PHP_EOL;
97}
98try {
99    pg_lo_create($db, 'giberrish');
100} catch (\ValueError $e) {
101    echo $e->getMessage(), \PHP_EOL;
102}
103
104echo "OK";
105?>
106--EXPECTF--
107create/write/close LO
108open/read/tell/seek/close LO
109string(5) "large"
110int(5)
111bool(true)
112string(10) "bject data"
113int(17)
114bool(true)
115string(17) "large object data"
116bool(true)
117string(4) "data"
118open/read_all/close LO
119large object data
120int(17)
121unlink LO
122Test without connection
123
124Deprecated: pg_lo_unlink(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
125Test with string oid value
126import/export LO
127
128Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
129Invalid OID value passed
130Invalid OID value passed
131
132Deprecated: pg_lo_create(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
133Invalid OID value passed
134Invalid OID value passed
135OK
136