1--TEST--
2PDO PgSQL Large Objects
3--EXTENSIONS--
4pdo_pgsql
5--SKIPIF--
6<?php
7require __DIR__ . '/config.inc';
8require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
9PDOTest::skip();
10?>
11--FILE--
12<?php
13require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
14$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
15$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
16$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
17
18$db->exec('CREATE TABLE test_large_objects (blobid integer not null primary key, bloboid OID)');
19
20$db->beginTransaction();
21$oid = $db->pgsqlLOBCreate();
22try {
23$stm = $db->pgsqlLOBOpen($oid, 'w+b');
24fwrite($stm, "Hello dude\n");
25
26$stmt = $db->prepare("INSERT INTO test_large_objects (blobid, bloboid) values (?, ?)");
27$stmt->bindValue(1, 1);
28/* bind as LOB; the oid from the pgsql stream will be inserted instead
29 * of the stream contents. Binding other streams will attempt to bind
30 * as bytea, and will most likely lead to an error.
31 * You can also just bind the $oid in as a string. */
32$stmt->bindParam(2, $stm, PDO::PARAM_LOB);
33$stmt->execute();
34$stm = null;
35
36/* Pull it out */
37$stmt = $db->prepare("SELECT * from test_large_objects");
38$stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
39$stmt->execute();
40echo "Fetching:\n";
41while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
42    var_dump($row['blobid']);
43    var_dump(stream_get_contents($lob));
44}
45echo "Fetched!\n";
46
47/* Try again, with late bind */
48$stmt = $db->prepare("SELECT * from test_large_objects");
49$stmt->execute();
50$stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
51echo "Fetching late bind:\n";
52while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
53    var_dump($row['blobid']);
54    var_dump(is_int($row['bloboid']));
55}
56echo "Fetched!\n";
57
58/* Try again, with NO  bind */
59$stmt = $db->prepare("SELECT * from test_large_objects");
60$stmt->execute();
61$stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
62echo "Fetching NO bind:\n";
63while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
64    var_dump($row['blobid']);
65    var_dump(is_int($row['bloboid']));
66}
67echo "Fetched!\n";
68
69} catch (Exception $e) {
70    /* catch exceptions so that we can guarantee to clean
71     * up the LOB */
72    echo "Exception! at line ", $e->getLine(), "\n";
73    var_dump($e->getMessage());
74}
75
76/* Now to remove the large object from the database, so it doesn't
77 * linger and clutter up the storage */
78$db->pgsqlLOBUnlink($oid);
79?>
80--CLEAN--
81<?php
82require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
83$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
84$db->exec('DROP TABLE test_large_objects');
85?>
86--EXPECT--
87Fetching:
88int(1)
89string(11) "Hello dude
90"
91Fetched!
92Fetching late bind:
93int(1)
94bool(true)
95Fetched!
96Fetching NO bind:
97int(1)
98bool(true)
99Fetched!
100