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