1--TEST--
2MySQL Prepared Statements and BLOBs
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13    $db = MySQLPDOTest::factory();
14
15    $blobs = array(
16        'TINYBLOB'		=> 255,
17        'TINYTEXT'		=> 255,
18        'BLOB'				=> 32767,
19        'TEXT'				=> 32767,
20        'MEDIUMBLOB'	=> 100000,
21        'MEDIUMTEXT'	=> 100000,
22        'LONGBLOB'		=> 100000,
23        'LONGTEXT'		=> 100000,
24    );
25
26    function test_blob($db, $offset, $sql_type, $test_len) {
27
28        $db->exec('DROP TABLE IF EXISTS test_stmt_blobs');
29        $db->exec(sprintf('CREATE TABLE test_stmt_blobs(id INT, label %s) ENGINE=%s', $sql_type, PDO_MYSQL_TEST_ENGINE));
30
31        $value = str_repeat('a', $test_len);
32        $stmt = $db->prepare('INSERT INTO test_stmt_blobs(id, label) VALUES (?, ?)');
33        $stmt->bindValue(1, 1);
34        $stmt->bindValue(2, $value);
35        if (!$stmt->execute()) {
36            printf("[%03d + 1] %d %s\n",
37                $offset, $stmt->errorCode(), var_export($stmt->errorInfo(), true));
38            return false;
39        }
40
41        $stmt = $db->query('SELECT id, label FROM test_stmt_blobs');
42        $id = $label = NULL;
43        $stmt->bindColumn(1, $id, PDO::PARAM_INT);
44        $stmt->bindColumn(2, $label, PDO::PARAM_LOB);
45
46        if (!$stmt->fetch(PDO::FETCH_BOUND)) {
47            printf("[%03d + 2] %d %s\n",
48                $offset, $stmt->errorCode(), var_export($stmt->errorInfo(), true));
49            return false;
50        }
51
52        if (!is_resource($label)) {
53            printf("[%03d + 3] Returned value is not a stream resource\n", $offset);
54            return false;
55        }
56
57        $contents = stream_get_contents($label);
58        if ($contents !== $value) {
59            printf("[%03d + 3] Returned value seems to be wrong (%d vs. %d characters). Check manually\n",
60                $offset, strlen($contents), strlen($value));
61            return false;
62        }
63
64        if (1 != $id) {
65            printf("[%03d + 3] Returned id column value seems wrong, expecting 1 got %s.\n",
66                $offset, var_export($id, true));
67            return false;
68        }
69
70        $stmt = $db->query('SELECT id, label FROM test_stmt_blobs');
71        $ret = $stmt->fetch(PDO::FETCH_ASSOC);
72
73        if ($ret['label'] !== $value) {
74            printf("[%03d + 3] Returned value seems to be wrong (%d vs. %d characters). Check manually\n",
75                $offset, strlen($ret['label']), strlen($value));
76            return false;
77        }
78
79        if (1 != $ret['id']) {
80            printf("[%03d + 3] Returned id column value seems wrong, expecting 1 got %s.\n",
81                $offset, var_export($ret['id'], true));
82            return false;
83        }
84
85        return true;
86    }
87
88    $offset = 0;
89    foreach ($blobs as $sql_type => $test_len) {
90        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
91        test_blob($db, ++$offset, $sql_type, $test_len);
92        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
93        test_blob($db, ++$offset, $sql_type, $test_len);
94    }
95
96    print "done!";
97?>
98--CLEAN--
99<?php
100require_once __DIR__ . '/inc/mysql_pdo_test.inc';
101$db = MySQLPDOTest::factory();
102$db->exec('DROP TABLE IF EXISTS test_stmt_blobs');
103?>
104--EXPECT--
105done!
106