summaryrefslogtreecommitdiff
path: root/ext/pdo_mysql/tests/pdo_mysql_stmt_blobfromsteam.phpt
blob: 382ed0ea56be226ba42e1903fd0165119eb25ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--TEST--
MySQL PDOStatement - inserting BLOB from stream
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();

$tmp = MySQLPDOTest::getTempDir();
if (!$tmp)
	die("skip Can't create temporary file");

$file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst';
$fp = fopen($file, 'w');
if (!$fp)
	die("skip Can't create temporary file");

if (4 != fwrite($fp, 'test')) {
	die("skip Can't create temporary file");
}
fclose($fp);
clearstatcache();

if (!file_exists($file))
	die("skip Can't create temporary file");

unlink($file);
?>
--FILE--
<?php
	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');

	function blob_from_stream($offset, $db, $file, $blob) {

			@unlink($file);
			clearstatcache();
			if (file_exists($file)) {
				printf("[%03d + 1] Cannot remove old test file\n", $offset);
				return false;
			}

			$fp = fopen($file, 'w');
			if (!$fp || !fwrite($fp, $blob)) {
				printf("[%03d + 2] Cannot create test file '%s'\n", $offset, $file);
				return false;
			}

			fclose($fp);
			clearstatcache();
			if (!file_exists($file)) {
				printf("[%03d + 3] Failed to create test file '%s'\n", $offset, $file);
				return false;
			}

			$db->exec('DROP TABLE IF EXISTS test');
			$sql = sprintf('CREATE TABLE test(id INT, label BLOB) ENGINE=%s', PDO_MYSQL_TEST_ENGINE);
			$db->exec($sql);

			if (!$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)')) {
				printf("[%03d + 4] %s\n", $offset, var_export($db->errorInfo(), true));
				return false;
			}

			$fp = fopen($file, 'r');
			if (!$fp) {
				printf("[%03d + 5] Cannot create test file '%s'\n", $offset, $file);
				return false;
			}


			$id = 1;
			$stmt->bindParam(1, $id);
			if (true !== ($tmp = $stmt->bindParam(2, $fp, PDO::PARAM_LOB))) {
				printf("[%03d + 6] Expecting true, got %s. %s\n",
					$offset,
					var_export($tmp, true),
					var_export($db->errorInfo(), true));
				return false;
			}

			if (true !== $stmt->execute()) {
				printf("[%03d + 7] Failed to INSERT data, %s\n", $offset, var_export($stmt->errorInfo(), true));
				return false;
			}

			$stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1');
			$row = $stmt2->fetch(PDO::FETCH_ASSOC);
			if ($row['label'] != $blob) {
				printf("[%03d + 8] INSERT and/or SELECT has failed, dumping data.\n", $offset);
				var_dump($row);
				var_dump($blob);
				return false;
			}

			// Lets test the chr(0) handling in case the streaming has failed:
			// is the bug about chr(0) or the streaming...
			$db->exec('DELETE FROM test');
			$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
			$stmt->bindParam(1, $id);
			$stmt->bindParam(2, $blob);
			if (true !== $stmt->execute())
				printf("[%03d + 9] %s\n", $offset, var_export($stmt->errorInfo(), true));

			$stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1');
			$row = $stmt2->fetch(PDO::FETCH_ASSOC);
			if ($row['label'] != $blob) {
				printf("[%03d + 10] INSERT and/or SELECT has failed, dumping data.\n", $offset);
				var_dump($row);
				var_dump($blob);
				return false;
			}

			return true;
	}

	$db = MySQLPDOTest::factory();
	$blob = 'I am a mighty BLOB!' . chr(0) . "I am a binary thingie!";
	$tmp = MySQLPDOTest::getTempDir();
	$file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst';

	try {

		printf("Emulated PS...\n");
		$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
		blob_from_stream(10, $db, $file, $blob);

		printf("Native PS...\n");
		$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
		blob_from_stream(30, $db, $file, $blob);

	} catch (PDOException $e) {
		printf("[001] %s [%s] %s\n",
			$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
	}

	print "done!";
?>
--CLEAN--
<?php
require dirname(__FILE__) . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test');
@unlink(MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR . 'pdoblob.tst');
?>
--EXPECTF--
Emulated PS...
Native PS...
done!