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