1--TEST--
2MySQL PDO->prepare(), native PS
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    function prepex($offset, &$db, $query, $input_params = null, $error_info = null, $suppress_warning = false) {
16        try {
17            if ($suppress_warning || (is_array($error_info) && isset($error_info['prepare'])))
18                $stmt = @$db->prepare($query);
19            else
20                $stmt = $db->prepare($query);
21
22            if (is_array($error_info) && isset($error_info['prepare'])) {
23                $tmp = $db->errorInfo();
24
25                if (isset($error_info['prepare']['sqlstate']) &&
26                    ($error_info['prepare']['sqlstate'] !== $tmp[0])) {
27                    printf("[%03d] prepare() - expecting SQLSTATE '%s' got '%s'\n",
28                        $offset, $error_info['prepare']['sqlstate'], $tmp[0]);
29                    return false;
30                }
31
32                if (isset($error_info['prepare']['mysql']) &&
33                    ($error_info['prepare']['mysql'] !== $tmp[1])) {
34                    printf("[%03d] prepare() - expecting MySQL Code '%s' got '%s'\n",
35                        $offset, $error_info['prepare']['mysql'], $tmp[0]);
36                    return false;
37                }
38
39                return false;
40            }
41
42            if (!is_object($stmt))
43                return false;
44
45            if (is_null($input_params))
46                $input_params = array();
47// 5.0.18, 5.1.14 @ 15
48// printf("[%03d]\n", $offset);
49            if ($suppress_warning || (is_array($error_info) && isset($error_info['execute'])))
50                $ret = @$stmt->execute($input_params);
51            else
52                $ret = $stmt->execute($input_params);
53
54            if (!is_bool($ret))
55                printf("[%03d] PDO::execute() should return a boolean value, got %s/%s\n",
56                    var_export($ret, true), $ret);
57
58            $tmp = $stmt->errorInfo();
59            if (isset($tmp[1]) && ($tmp[1] == 2030)) {
60                // Trying to hack around MySQL Server version dependent features
61                // 2030 This command is not supported in the prepared statement protocol yet
62                return false;
63            }
64
65            if (is_array($error_info) && isset($error_info['execute'])) {
66
67                if (isset($error_info['execute']['sqlstate']) &&
68                    ($error_info['execute']['sqlstate'] !== $tmp[0])) {
69                    printf("[%03d] execute() - expecting SQLSTATE '%s' got '%s'\n",
70                        $offset, $error_info['execute']['sqlstate'], $tmp[0]);
71                    return false;
72                }
73
74                if (isset($error_info['execute']['mysql']) &&
75                    ($error_info['execute']['mysql'] !== $tmp[1])) {
76                    printf("[%03d] execute() - expecting MySQL Code '%s' got '%s'\n",
77                        $offset, $error_info['execute']['mysql'], $tmp[0]);
78                    return false;
79                }
80
81                return false;
82            }
83        } catch (PDOException $e) {
84            printf("[%03d] %s, [%s} %s\n",
85                $offset, $e->getMessage(),
86                $db->errorCode(), implode(' ', $db->errorInfo()));
87            return false;
88        }
89
90        return $stmt;
91    }
92
93    try {
94        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
95        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
96            printf("[002] Unable to turn off emulated prepared statements\n");
97
98        try {
99            prepex(3, $db, '', [], ['prepare' => ['sqlstate' => '42000']]);
100        } catch (\ValueError $e) {
101            echo $e->getMessage(), \PHP_EOL;
102        }
103
104        prepex(4, $db, 'CREATE TABLE test_prepare_native_myisam_index(id INT, label CHAR(255)) ENGINE=MyISAM');
105        if (is_object(prepex(5, $db, 'CREATE FULLTEXT INDEX idx1 ON test_prepare_native_myisam_index(label)', null, null, true))) {
106            prepex(6, $db, 'INSERT INTO test_prepare_native_myisam_index(id, label) VALUES (1, ?)',
107                array('MySQL is the best database in the world!'));
108            prepex(7, $db, 'INSERT INTO test_prepare_native_myisam_index(id, label) VALUES (1, ?)',
109                array('If I have the freedom to choose, I would always go again for the MySQL Server'));
110            $stmt = prepex(8, $db, 'SELECT id, label FROM test_prepare_native_myisam_index WHERE MATCH label AGAINST (?)',
111                array('mysql'), null, true);
112            /*
113            Lets ignore that
114            if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
115                printf("[074] Expecting two rows, got %d rows\n", $tmp);
116            */
117        }
118
119        prepex(9, $db, 'DELETE FROM test_prepare_native_myisam_index');
120        prepex(10, $db, 'INSERT INTO test_prepare_native_myisam_index(id, label) VALUES (1, ?), (2, ?)',
121            array('row1', 'row2'));
122
123        /*
124        TODO enable after fix
125        $stmt = prepex(11, $db, 'SELECT id, label FROM \'test_prepare_native_myisam_index WHERE MATCH label AGAINST (:placeholder)',
126            array(':placeholder' => 'row'),
127            array('execute' => array('sqlstate' => '42000', 'mysql' => 1064)));
128        */
129
130        $stmt = prepex(12, $db, 'SELECT id, label AS "label" FROM test_prepare_native_myisam_index WHERE label = ?',
131            array('row1'));
132        $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
133        $exp = array(
134            0 => array("id" => "1", "label" => "row1")
135        );
136
137        if (MySQLPDOTest::isPDOMySQLnd()) {
138            // mysqlnd returns native types
139            $exp[0]['id'] = 1;
140        }
141        if ($tmp !== $exp) {
142            printf("[065] Results seem wrong. Please check dumps manually.\n");
143            var_dump($exp);
144            var_dump($tmp);
145        }
146
147        $sql = sprintf("SELECT id, label FROM test_prepare_native_myisam_index WHERE (label LIKE %s) AND (id = ?)",
148            $db->quote('%ro%'));
149        $stmt = prepex(13, $db, $sql,	array(-1));
150        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 0)
151                printf("[061] Expecting zero rows, got %d rows\n", $tmp);
152
153        $sql = sprintf("SELECT id, label FROM test_prepare_native_myisam_index WHERE  (id = ?) OR (label LIKE %s)",
154            $db->quote('%ro%'));
155        $stmt = prepex(14, $db, $sql,	array(1));
156        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
157                printf("[062] Expecting two rows, got %d rows\n", $tmp);
158
159        $sql = "SELECT id, label FROM test_prepare_native_myisam_index WHERE id = ? AND label = (SELECT label AS 'SELECT' FROM test_prepare_native_myisam_index WHERE id = ?)";
160        $stmt = prepex(15, $db, $sql,	array(1, 1));
161        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 1)
162                printf("[064] Expecting one row, got %d rows\n", $tmp);
163
164    } catch (PDOException $e) {
165        printf("[001] %s [%s] %s\n",
166            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
167    }
168
169    print "done!";
170?>
171--CLEAN--
172<?php
173require_once __DIR__ . '/inc/mysql_pdo_test.inc';
174$db = MySQLPDOTest::factory();
175$db->exec('DROP TABLE IF EXISTS test_prepare_native_myisam_index');
176?>
177--EXPECT--
178PDO::prepare(): Argument #1 ($query) cannot be empty
179done!
180