1--TEST--
2MySQL PDOStatement - inserting BLOB from stream
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8
9$tmp = MySQLPDOTest::getTempDir();
10if (!$tmp)
11	die("skip Can't create temporary file");
12
13$file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst';
14$fp = fopen($file, 'w');
15if (!$fp)
16	die("skip Can't create temporary file");
17
18if (4 != fwrite($fp, 'test')) {
19	die("skip Can't create temporary file");
20}
21fclose($fp);
22clearstatcache();
23
24if (!file_exists($file))
25	die("skip Can't create temporary file");
26
27unlink($file);
28?>
29--FILE--
30<?php
31	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
32
33	function blob_from_stream($offset, $db, $file, $blob) {
34
35			@unlink($file);
36			clearstatcache();
37			if (file_exists($file)) {
38				printf("[%03d + 1] Cannot remove old test file\n", $offset);
39				return false;
40			}
41
42			$fp = fopen($file, 'w');
43			if (!$fp || !fwrite($fp, $blob)) {
44				printf("[%03d + 2] Cannot create test file '%s'\n", $offset, $file);
45				return false;
46			}
47
48			fclose($fp);
49			clearstatcache();
50			if (!file_exists($file)) {
51				printf("[%03d + 3] Failed to create test file '%s'\n", $offset, $file);
52				return false;
53			}
54
55			$db->exec('DROP TABLE IF EXISTS test');
56			$sql = sprintf('CREATE TABLE test(id INT, label BLOB) ENGINE=%s', PDO_MYSQL_TEST_ENGINE);
57			$db->exec($sql);
58
59			if (!$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)')) {
60				printf("[%03d + 4] %s\n", $offset, var_export($db->errorInfo(), true));
61				return false;
62			}
63
64			$fp = fopen($file, 'r');
65			if (!$fp) {
66				printf("[%03d + 5] Cannot create test file '%s'\n", $offset, $file);
67				return false;
68			}
69
70
71			$id = 1;
72			$stmt->bindParam(1, $id);
73			if (true !== ($tmp = $stmt->bindParam(2, $fp, PDO::PARAM_LOB))) {
74				printf("[%03d + 6] Expecting true, got %s. %s\n",
75					$offset,
76					var_export($tmp, true),
77					var_export($db->errorInfo(), true));
78				return false;
79			}
80
81			if (true !== $stmt->execute()) {
82				printf("[%03d + 7] Failed to INSERT data, %s\n", $offset, var_export($stmt->errorInfo(), true));
83				return false;
84			}
85
86			$stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1');
87			$row = $stmt2->fetch(PDO::FETCH_ASSOC);
88			if ($row['label'] != $blob) {
89				printf("[%03d + 8] INSERT and/or SELECT has failed, dumping data.\n", $offset);
90				var_dump($row);
91				var_dump($blob);
92				return false;
93			}
94
95			// Lets test the chr(0) handling in case the streaming has failed:
96			// is the bug about chr(0) or the streaming...
97			$db->exec('DELETE FROM test');
98			$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
99			$stmt->bindParam(1, $id);
100			$stmt->bindParam(2, $blob);
101			if (true !== $stmt->execute())
102				printf("[%03d + 9] %s\n", $offset, var_export($stmt->errorInfo(), true));
103
104			$stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1');
105			$row = $stmt2->fetch(PDO::FETCH_ASSOC);
106			if ($row['label'] != $blob) {
107				printf("[%03d + 10] INSERT and/or SELECT has failed, dumping data.\n", $offset);
108				var_dump($row);
109				var_dump($blob);
110				return false;
111			}
112
113			return true;
114	}
115
116	$db = MySQLPDOTest::factory();
117	$blob = 'I am a mighty BLOB!' . chr(0) . "I am a binary thingie!";
118	$tmp = MySQLPDOTest::getTempDir();
119	$file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst';
120
121	try {
122
123		printf("Emulated PS...\n");
124		$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
125		blob_from_stream(10, $db, $file, $blob);
126
127		printf("Native PS...\n");
128		$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
129		blob_from_stream(30, $db, $file, $blob);
130
131	} catch (PDOException $e) {
132		printf("[001] %s [%s] %s\n",
133			$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
134	}
135
136	print "done!";
137?>
138--CLEAN--
139<?php
140require dirname(__FILE__) . '/mysql_pdo_test.inc';
141$db = MySQLPDOTest::factory();
142$db->exec('DROP TABLE IF EXISTS test');
143@unlink(MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR . 'pdoblob.tst');
144?>
145--EXPECTF--
146Emulated PS...
147Native PS...
148done!
149