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, sprintf('CREATE TABLE test_prepare_native_anonymous_placeholder(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
105        prepex(5, $db, "INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES(1, '?')");
106        $stmt = prepex(6, $db, 'SELECT label FROM test_prepare_native_anonymous_placeholder');
107        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
108
109        prepex(7, $db, 'DELETE FROM test_prepare_native_anonymous_placeholder');
110        prepex(8, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES(1, ?)',
111            array('first row'));
112        prepex(9, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES(2, ?)',
113            array('second row'));
114        $stmt = prepex(10, $db, 'SELECT label FROM test_prepare_native_anonymous_placeholder');
115        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
116
117        // Is PDO fun?
118        prepex(10, $db, 'SELECT label FROM test_prepare_native_anonymous_placeholder WHERE ? > 1',
119            array('id'));
120        prepex(11, $db, 'SELECT ? FROM test_prepare_native_anonymous_placeholder WHERE id > 1',
121            array('id'));
122        prepex(12, $db, 'SELECT ? FROM test_prepare_native_anonymous_placeholder WHERE ? > ?',
123            array('id', 'label', 'value'));
124
125        for ($num_params = 2; $num_params < 100; $num_params++) {
126            $params = array('a');
127            for ($i = 1; $i < $num_params; $i++) {
128                $params[] = 'some data';
129            }
130            prepex(13, $db, 'SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE label > ?',
131                $params, array('execute' => array('sqlstate' => 'HY093')));
132        }
133
134        prepex(14, $db, 'DELETE FROM test_prepare_native_anonymous_placeholder');
135        prepex(15, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES (1, ?), (2, ?)',
136            array('row', 'row'));
137        $stmt = prepex(16, $db, 'SELECT id, label FROM test_prepare_native_anonymous_placeholder ORDER BY id');
138        $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
139        $exp = array(
140            0 => array(
141                "id"  => "1",
142                "label" => "row"
143            ),
144            1 => array(
145                "id" => "2",
146                "label" => "row"
147            ),
148        );
149
150        if (MySQLPDOTest::isPDOMySQLnd()) {
151            // mysqlnd returns native types
152            $exp[0]['id'] = 1;
153            $exp[1]['id'] = 2;
154        }
155        if ($tmp !== $exp) {
156            printf("[064] Results seem wrong. Please check dumps manually.\n");
157            var_dump($exp);
158            var_dump($tmp);
159        }
160
161        $stmt = prepex(17, $db, 'SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE ? IS NOT NULL',
162            array(1));
163        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
164            printf("[048] '1' IS NOT NULL evaluates to true, expecting two rows, got %d rows\n", $tmp);
165
166        $stmt = prepex(19, $db, 'SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE ? IS NULL',
167            array(1));
168        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 0)
169            printf("[050] '1' IS NOT NULL evaluates to true, expecting zero rows, got %d rows\n", $tmp);
170
171        prepex(21, $db, 'DROP TABLE IF EXISTS test_prepare_native_anonymous_placeholder');
172        prepex(22, $db, 'CREATE TABLE test_prepare_native_anonymous_placeholder(id INT, label CHAR(255)) ENGINE=MyISAM');
173        if (is_object(prepex(23, $db, 'CREATE FULLTEXT INDEX idx1 ON test_prepare_native_anonymous_placeholder(label)', null, null, true))) {
174            prepex(24, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES (1, ?)',
175                array('MySQL is the best database in the world!'));
176            prepex(25, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES (1, ?)',
177                array('If I have the freedom to choose, I would always go again for the MySQL Server'));
178            $stmt = prepex(26, $db, 'SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE MATCH label AGAINST (?)',
179                array('mysql'), null, true);
180            /*
181            Lets ignore that
182            if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
183                printf("[074] Expecting two rows, got %d rows\n", $tmp);
184            */
185        }
186
187        prepex(27, $db, 'DELETE FROM test_prepare_native_anonymous_placeholder');
188        prepex(28, $db, 'INSERT INTO test_prepare_native_anonymous_placeholder(id, label) VALUES (1, ?), (2, ?)',
189            array('row1', 'row2'));
190
191        /*
192        TODO enable after fix
193        $stmt = prepex(27, $db, 'SELECT id, label FROM \'test_prepare_native_anonymous_placeholder WHERE MATCH label AGAINST (:placeholder)',
194            array(':placeholder' => 'row'),
195            array('execute' => array('sqlstate' => '42000', 'mysql' => 1064)));
196        */
197
198        $stmt = prepex(29, $db, 'SELECT id, label AS "label" FROM test_prepare_native_anonymous_placeholder WHERE label = ?',
199            array('row1'));
200        $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
201        $exp = array(
202            0 => array("id" => "1", "label" => "row1")
203        );
204
205        if (MySQLPDOTest::isPDOMySQLnd()) {
206            // mysqlnd returns native types
207            $exp[0]['id'] = 1;
208        }
209        if ($tmp !== $exp) {
210            printf("[065] Results seem wrong. Please check dumps manually.\n");
211            var_dump($exp);
212            var_dump($tmp);
213        }
214
215        $sql = sprintf("SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE (label LIKE %s) AND (id = ?)",
216            $db->quote('%ro%'));
217        $stmt = prepex(30, $db, $sql,	array(-1));
218        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 0)
219                printf("[061] Expecting zero rows, got %d rows\n", $tmp);
220
221        $sql = sprintf("SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE  (id = ?) OR (label LIKE %s)",
222            $db->quote('%ro%'));
223        $stmt = prepex(31, $db, $sql,	array(1));
224        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
225                printf("[062] Expecting two rows, got %d rows\n", $tmp);
226
227        $sql = "SELECT id, label FROM test_prepare_native_anonymous_placeholder WHERE id = ? AND label = (SELECT label AS 'SELECT' FROM test_prepare_native_anonymous_placeholder WHERE id = ?)";
228        $stmt = prepex(33, $db, $sql,	array(1, 1));
229        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 1)
230                printf("[064] Expecting one row, got %d rows\n", $tmp);
231
232    } catch (PDOException $e) {
233        printf("[001] %s [%s] %s\n",
234            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
235    }
236
237    print "done!";
238?>
239--CLEAN--
240<?php
241require_once __DIR__ . '/inc/mysql_pdo_test.inc';
242$db = MySQLPDOTest::factory();
243$db->exec('DROP TABLE IF EXISTS test_prepare_native_anonymous_placeholder');
244?>
245--EXPECT--
246PDO::prepare(): Argument #1 ($query) must not be empty
247array(1) {
248  [0]=>
249  array(1) {
250    ["label"]=>
251    string(1) "?"
252  }
253}
254array(2) {
255  [0]=>
256  array(1) {
257    ["label"]=>
258    string(9) "first row"
259  }
260  [1]=>
261  array(1) {
262    ["label"]=>
263    string(10) "second row"
264  }
265}
266done!
267