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        // lets be fair and do the most simple SELECT first
105        $stmt = prepex(4, $db, 'SELECT 1 as "one"');
106        if (MySQLPDOTest::isPDOMySQLnd())
107            // native types - int
108            $expected = array('one' => 1);
109        else
110            // always strings, like STRINGIFY flag
111            $expected = array('one' => '1');
112
113        $row = $stmt->fetch(PDO::FETCH_ASSOC);
114        if ($row !== $expected) {
115            printf("[004a] Expecting %s got %s\n", var_export($expected, true), var_export($row, true));
116        }
117
118        prepex(6, $db, sprintf('CREATE TABLE test_prepare_native(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
119        prepex(7, $db, "INSERT INTO test_prepare_native(id, label) VALUES(1, ':placeholder')");
120        $stmt = prepex(8, $db, 'SELECT label FROM test_prepare_native ORDER BY id ASC');
121        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
122
123        prepex(9, $db, 'DELETE FROM test_prepare_native');
124        prepex(10, $db, 'INSERT INTO test_prepare_native(id, label) VALUES(1, :placeholder)',
125            array(':placeholder' => 'first row'));
126        prepex(11, $db, 'INSERT INTO test_prepare_native(id, label) VALUES(2, :placeholder)',
127            array(':placeholder' => 'second row'));
128        $stmt = prepex(12, $db, 'SELECT label FROM test_prepare_native ORDER BY id ASC');
129        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
130
131        // Is PDO fun?
132        $stmt = prepex(13, $db, 'SELECT label FROM test_prepare_native WHERE :placeholder > 1',
133            array(':placeholder' => 'id'));
134        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
135
136        for ($num_params = 2; $num_params < 100; $num_params++) {
137            $params = array(':placeholder' => 'a');
138            for ($i = 1; $i < $num_params; $i++) {
139                $params[str_repeat('a', $i)] = 'some data';
140            }
141            prepex(16, $db, 'SELECT id, label FROM test_prepare_native WHERE label > :placeholder',
142                $params, array('execute' => array('sqlstate' => 'HY093')));
143        }
144
145        $stmt = prepex(16, $db, 'SELECT id, label FROM test_prepare_native WHERE :placeholder IS NOT NULL',
146            array(':placeholder' => 1));
147        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2)
148            printf("[017] '1' IS NOT NULL evaluates to true, expecting two rows, got %d rows\n", $tmp);
149
150        $stmt = prepex(18, $db, 'SELECT id, label FROM test_prepare_native WHERE :placeholder IS NULL',
151            array(':placeholder' => 1));
152        if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 0)
153            printf("[019] '1' IS NOT NULL evaluates to true, expecting zero rows, got %d rows\n", $tmp);
154    } catch (PDOException $e) {
155        printf("[001] %s [%s] %s\n",
156            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
157    }
158
159    print "done!";
160?>
161--CLEAN--
162<?php
163require_once __DIR__ . '/inc/mysql_pdo_test.inc';
164$db = MySQLPDOTest::factory();
165$db->exec('DROP TABLE IF EXISTS test_prepare_native');
166?>
167--EXPECT--
168PDO::prepare(): Argument #1 ($query) cannot be empty
169array(1) {
170  [0]=>
171  array(1) {
172    ["label"]=>
173    string(12) ":placeholder"
174  }
175}
176array(2) {
177  [0]=>
178  array(1) {
179    ["label"]=>
180    string(9) "first row"
181  }
182  [1]=>
183  array(1) {
184    ["label"]=>
185    string(10) "second row"
186  }
187}
188array(0) {
189}
190done!
191