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