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