1--TEST--
2mysqli_stmt_result_metadata() - non SELECT statements
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8die("skip Check again when the Klingons visit earth - http://bugs.mysql.com/bug.php?id=42490");
9?>
10--FILE--
11<?php
12    require('table.inc');
13
14    function testStatement($offset, $link, $sql, $expected_lib, $expected_mysqlnd, $check_mysqlnd, $compare) {
15
16        if (!$stmt = mysqli_stmt_init($link)) {
17            printf("[%04d - %s] [%d] %s\n",
18                $offset, $sql,
19                mysqli_errno($link), mysqli_error($link));
20            return false;
21        }
22
23        if (!@mysqli_stmt_prepare($stmt, $sql)) {
24            /* Not all server versions will support all statements */
25            /* Failing to prepare is OK */
26            return true;
27        }
28
29        if (empty($expected_lib) && (false !== $res)) {
30            printf("[%04d - %s] No metadata expected\n",
31                $offset + 1, $sql);
32            return false;
33        } else if (!empty($expected_lib) && (false == $res)) {
34            printf("[%04d - %s] Metadata expected\n",
35                $offset + 2, $sql);
36            return false;
37        }
38        if (!empty($expected_lib)) {
39            if (!is_object($res)) {
40                printf("[%04d - %s] [%d] %s\n",
41                    $offset + 3, $sql,
42                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43                return false;
44            }
45            if (get_class($res) != 'mysqli_result') {
46                printf("[%04d - %s] Expecting object/mysqli_result got object/%s\n",
47                    $offset + 4, $sql, get_class($res));
48                return false;
49            }
50
51            $meta = array(
52                'num_fields'		=> mysqli_num_fields($res),
53                'fetch_field'		=> mysqli_fetch_field($res),
54                'fetch_field_direct0'	=> mysqli_fetch_field_direct($res, 0),
55                'fetch_field_direct1'	=> @mysqli_fetch_field_direct($res, 1),
56                'fetch_fields'		=> count(mysqli_fetch_fields($res)),
57                'field_count'		=> $res->field_count,
58                'field_seek-1'		=> @mysqli_field_seek($res, -1),
59                'field_seek0'		=> mysqli_field_seek($res, 0),
60                'field_tell'		=> mysqli_field_tell($res),
61            );
62            if (is_object($meta['fetch_field'])) {
63                $meta['fetch_field']->charsetnr	= 'ignore';
64                $meta['fetch_field']->flags	= 'ignore';
65            }
66            if (is_object($meta['fetch_field_direct0'])) {
67                $meta['fetch_field_direct0']->charsetnr	= 'ignore';
68                $meta['fetch_field_direct0']->flags	= 'ignore';
69            }
70            if (is_object($meta['fetch_field_direct1'])) {
71                $meta['fetch_field_direct1']->charsetnr	= 'ignore';
72                $meta['fetch_field_direct1']->flags	= 'ignore';
73            }
74            mysqli_free_result($res);
75
76            if ($meta != $expected_lib) {
77                printf("[%04d - %s] Metadata differs from expected values\n",
78                    $offset + 5, $sql);
79                var_dump($meta);
80                var_dump($expected_lib);
81                return false;
82            }
83        }
84
85        if (function_exists('mysqli_stmt_get_result')) {
86            /* mysqlnd only */
87            if (!mysqli_stmt_execute($stmt)) {
88                printf("[%04d - %s] [%d] %s\n",
89                    $offset + 6, $sql,
90                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
91                return false;
92            }
93
94            $res = mysqli_stmt_get_result($stmt);
95            if (false === $res && !empty($expected_mysqlnd)) {
96                printf("[%04d - %s] Expecting resultset [%d] %s\n",
97                    $offset + 7, $sql,
98                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
99                return false;
100            } else if (empty($expected_mysqlnd) && false !== $res) {
101                printf("[%04d - %s] Unexpected resultset [%d] %s\n",
102                    $offset + 8, $sql,
103                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
104                return false;
105            }
106
107            if (!is_object($res)) {
108                printf("[%04d - %s] [%d] %s\n",
109                    $offset + 9, $sql,
110                    mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
111                return false;
112            }
113            if ('mysqli_result' != get_class($res)) {
114                printf("[%04d - %s] Expecting object/mysqli_result got object/%s\n",
115                    $offset + 10, $sql,
116                    get_class($res));
117                return false;
118            }
119
120            $meta_res = array(
121                'num_fields'		=> mysqli_num_fields($res),
122                'fetch_field'		=> mysqli_fetch_field($res),
123                'fetch_field_direct0'	=> mysqli_fetch_field_direct($res, 0),
124                'fetch_field_direct1'	=> @mysqli_fetch_field_direct($res, 1),
125                'fetch_fields'		=> count(mysqli_fetch_fields($res)),
126                'field_count'		=> mysqli_field_count($link),
127                'field_seek-1'		=> @mysqli_field_seek($res, -1),
128                'field_seek0'		=> mysqli_field_seek($res, 0),
129                'field_tell'		=> mysqli_field_tell($res),
130            );
131            if (is_object($meta_res['fetch_field'])) {
132                $meta_res['fetch_field']->charsetnr	= 'ignore';
133                $meta_res['fetch_field']->flags	= 'ignore';
134            }
135            if (is_object($meta_res['fetch_field_direct0'])) {
136                $meta_res['fetch_field_direct0']->charsetnr	= 'ignore';
137                $meta_res['fetch_field_direct0']->flags	= 'ignore';
138            }
139            if (is_object($meta_res['fetch_field_direct1'])) {
140                $meta_res['fetch_field_direct1']->charsetnr	= 'ignore';
141                $meta_res['fetch_field_direct1']->flags	= 'ignore';
142            }
143            mysqli_free_result($res);
144            if ($check_mysqlnd && $meta_res != $expected_mysqlnd) {
145                printf("[%04d - %s] Metadata differs from expected\n",
146                    $offset + 11, $sql);
147                var_dump($meta_res);
148                var_dump($expected_mysqlnd);
149            } else {
150                if ($meta_res['field_count'] < 1) {
151                    printf("[%04d - %s] Metadata seems wrong, no fields?\n",
152                    $offset + 12, $sql);
153                    var_dump($meta_res);
154                    var_dump(mysqli_fetch_assoc($res));
155                }
156            }
157
158            if ($compare && $meta_res != $meta) {
159                printf("[%04d - %s] Metadata returned by mysqli_stmt_result_metadata() and mysqli_stmt_get_result() differ\n",
160                    $offset + 13, $sql);
161                var_dump($meta_res);
162                var_dump($meta);
163            }
164
165        }
166
167        mysqli_stmt_close($stmt);
168        return true;
169    }
170
171    /* Note: very weak testing, we accept almost any result */
172
173    testStatement(100, $link, 'ANALYZE TABLE test', array(), array(1), false, false);
174    testStatement(120, $link, 'OPTIMIZE TABLE test', array(), array(1), false, false);
175    testStatement(140, $link, 'REPAIR TABLE test', array(), array(1), false, false);
176
177    testStatement(160, $link, 'SHOW AUTHORS', array(), array(1), false, false);
178    testStatement(180, $link, 'SHOW CHARACTER SET', array(), array(1), false, false);
179    testStatement(200, $link, 'SHOW COLLATION', array(), array(1), false, false);
180    testStatement(220, $link, 'SHOW CONTRIBUTORS', array(), array(1), false, false);
181    testStatement(240, $link, 'SHOW CREATE DATABASE ' . $db, array(), array(1), false, false);
182    testStatement(260, $link, 'SHOW DATABASES', array(), array(1), false, false);
183    testStatement(280, $link, 'SHOW ENGINE InnoDB STATUS', array(), array(1), false, false);
184    testStatement(300, $link, 'SHOW ENGINES', array(), array(1), false, false);
185    testStatement(320, $link, 'SHOW PLUGINS', array(), array(1), false, false);
186    testStatement(340, $link, 'SHOW PROCESSLIST', array(), array(1), false, false);
187    testStatement(360, $link, 'SHOW FULL PROCESSLIST', array(), array(1), false, false);
188    testStatement(380, $link, 'SHOW STATUS', array(), array(1), false, false);
189    testStatement(400, $link, 'SHOW TABLE STATUS', array(), array(1), false, false);
190    testStatement(420, $link, 'SHOW TABLE STATUS', array(), array(1), false, false);
191    testStatement(440, $link, 'SHOW TABLES', array(), array(1), false, false);
192    testStatement(460, $link, 'SHOW OPEN TABLES', array(), array(1), false, false);
193    testStatement(460, $link, 'SHOW VARIABLES', array(), array(1), false, false);
194
195    $field0 = new stdClass();
196    $field0->name 		= 'id';
197    $field0->orgname 	= 'id';
198    $field0->table		= 'test';
199    $field0->orgtable	= 'test';
200    $field0->def 		= '';
201    $field0->max_length	= 0;
202    $field0->length		= 11;
203    $field0->charsetnr	= 'ignore';
204    $field0->flags		= 'ignore';
205    $field0->type		= MYSQLI_TYPE_LONG;
206    $field0->decimals	= 0;
207    $meta_lib = array(
208        'num_fields'		=> 1,
209        'fetch_field'		=> $field0,
210        'fetch_field_direct0'	=> $field0,
211        'fetch_field_direct1'	=> false,
212        'fetch_fields'		=> 1,
213        'field_count'		=> 1,
214        'field_seek-1'		=> false,
215        'field_seek0'		=> true,
216        'field_tell'		=> 0,
217    );
218    $meta_mysqlnd = $meta_lib;
219    testStatement(480, $link, 'SELECT id FROM test', $meta_lib, $meta_mysqlnd, true, true);
220
221    testStatement(500, $link, 'CHECKSUM TABLE test', array(), array(1), false, false);
222
223    mysqli_close($link);
224    print "done!";
225?>
226--CLEAN--
227<?php
228    require_once("clean_table.inc");
229?>
230--EXPECT--
231done!
232