1--TEST--
2mysqli_fetch_field() - flags/field->flags
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8require_once('connect.inc');
9if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
10        die(printf("skip: [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
11
12if (mysqli_get_server_version($link) < 50041)
13    die("skip: Due to many MySQL Server differences, the test requires 5.0.41+");
14
15mysqli_close($link);
16?>
17--FILE--
18<?php
19    require_once("connect.inc");
20
21/* TODO: mysqli.c needs to export a few more constants - see all the defined() calls! */
22
23    $flags = array(
24        MYSQLI_NOT_NULL_FLAG => 'NOT_NULL',
25        MYSQLI_PRI_KEY_FLAG => 'PRI_KEY',
26        MYSQLI_UNIQUE_KEY_FLAG => 'UNIQUE_KEY',
27        MYSQLI_MULTIPLE_KEY_FLAG => 'MULTIPLE_KEY',
28        MYSQLI_BLOB_FLAG => 'BLOB',
29        MYSQLI_UNSIGNED_FLAG	=> 'UNSIGNED',
30        MYSQLI_ZEROFILL_FLAG => 'ZEROFILL',
31        MYSQLI_AUTO_INCREMENT_FLAG => 'AUTO_INCREMENT',
32        MYSQLI_TIMESTAMP_FLAG	=> 'TIMESTAMP',
33        MYSQLI_SET_FLAG	=> 'SET',
34        MYSQLI_NUM_FLAG => 'NUM',
35        MYSQLI_PART_KEY_FLAG => 'PART_KEY',
36        // MYSQLI_GROUP_FLAG => 'MYSQLI_GROUP_FLAG' - internal usage only
37        (defined('MYSQLI_NO_DEFAULT_VALUE_FLAG') ? MYSQLI_NO_DEFAULT_VALUE_FLAG : 4096) => 'NO_DEFAULT_VALUE',
38        (defined('MYSQLI_BINARY_FLAG') ? MYSQLI_BINARY_FLAG : 128) => 'BINARY',
39        (defined('MYSQLI_ENUM_FLAG') ? MYSQLI_ENUM_FLAG : 256) => 'ENUM',
40        // MYSQLI_BINCMP_FLAG
41    );
42
43    // 5.1.24 / 6.0.4+
44    if (defined('MYSQLI_ON_UPDATE_NOW'))
45        $flags[MYSQLI_ON_UPDATE_NOW] = 'ON_UPDATE_NOW';
46    else
47        $flags[8192] = 'ON_UPDATE_NOW';
48
49    krsort($flags);
50
51    $columns = array(
52        'INT DEFAULT NULL' => 'NUM',
53        'INT NOT NULL' => 'NOT_NULL NO_DEFAULT_VALUE NUM',
54        'INT NOT NULL DEFAULT 1' => 'NOT_NULL NUM',
55        'INT UNSIGNED DEFAULT NULL' => 'UNSIGNED NUM',
56        'INT UNSIGNED NOT NULL'	=> 'NOT_NULL UNSIGNED NO_DEFAULT_VALUE NUM',
57        'INT UNSIGNED NOT NULL DEFAULT 1' => 'NOT_NULL UNSIGNED NUM',
58        'INT UNSIGNED ZEROFILL DEFAULT NULL' => 'UNSIGNED ZEROFILL NUM',
59        'INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY' => 'NOT_NULL PRI_KEY UNSIGNED AUTO_INCREMENT NUM PART_KEY',
60        'CHAR(1) DEFAULT NULL'	=> '',
61        'CHAR(1) NOT NULL' => 'NOT_NULL NO_DEFAULT_VALUE',
62        'VARBINARY(127) DEFAULT NULL' => 'BINARY',
63        'BLOB'	=> 'BLOB BINARY',
64        'TINYBLOB'	=> 'BLOB BINARY',
65        'MEDIUMBLOB'	=> 'BLOB BINARY',
66        'LONGBLOB'	=> 'BLOB BINARY',
67        'TEXT'	=> 'BLOB',
68        'TINYTEXT'	=> 'BLOB',
69        'MEDIUMTEXT'	=> 'BLOB',
70        'LONGTEXT'	=> 'BLOB',
71        'SET("one", "two")'	=> 'SET',
72        'SET("one", "two") NOT NULL'	=> 'NOT_NULL SET NO_DEFAULT_VALUE',
73        'SET("one", "two") NOT NULL DEFAULT "one"'	=> 'NOT_NULL SET',
74        'ENUM("one", "two")'	=> 'ENUM',
75        'ENUM("one", "two") NOT NULL' => 'NOT_NULL ENUM NO_DEFAULT_VALUE',
76        'ENUM("one", "two") NOT NULL DEFAULT "one"' => 'NOT_NULL ENUM',
77        'TINYINT UNIQUE' => 'UNIQUE_KEY NUM PART_KEY',
78        'SMALLINT UNIQUE' => 'UNIQUE_KEY NUM PART_KEY',
79        'MEDIUMINT UNIQUE DEFAULT 1' => 'UNIQUE_KEY NUM PART_KEY',
80        'BIGINT UNSIGNED UNIQUE DEFAULT 100' => 'UNIQUE_KEY UNSIGNED NUM PART_KEY',
81        'BIT' => 'UNSIGNED',
82        'VARCHAR(2) NOT NULL PRIMARY KEY' => 'NOT_NULL PRI_KEY NO_DEFAULT_VALUE PART_KEY'
83    );
84
85
86
87    function checkFlags($reported_flags, $expected_flags, $flags) {
88        $found_flags = $unexpected_flags = '';
89        foreach ($flags as $code => $name) {
90            if ($reported_flags >= $code) {
91                $reported_flags -= $code;
92                $found_flags .= $name . ' ';
93                if (stristr($expected_flags, $name)) {
94                    $expected_flags = trim(str_ireplace($name, '', $expected_flags));
95                } else {
96                    $unexpected_flags .= $name . ' ';
97                }
98            }
99        }
100
101        return array($expected_flags, $unexpected_flags, $found_flags);
102    }
103
104    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
105        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
106
107    foreach ($columns as $column_def => $expected_flags) {
108        if (!mysqli_query($link, 'DROP TABLE IF EXISTS test')) {
109            printf("[002] %s [%d] %s\n", $column_def,
110                mysqli_errno($link), mysqli_error($link));
111            continue;
112        }
113
114        $create = sprintf('CREATE TABLE test(id INT, col1 %s)', $column_def);
115        if (!mysqli_query($link, $create)) {
116            // Server might not support it - skip
117            continue;
118        }
119
120        if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
121            printf("[003] Can't select from table, %s [%d] %s\n", $column_def,
122                mysqli_errno($link), mysqli_error($link));
123            continue;
124        }
125
126        $field = mysqli_fetch_field_direct($res, 1);
127        if (!is_object($field)) {
128            printf("[004] Fetching the meta data failed, %s [%d] %s\n", $column_def,
129                mysqli_errno($link), mysqli_error($link));
130            continue;
131        }
132        if ($field->name != 'col1') {
133            printf("[005] Field information seems wrong, %s [%d] %s\n", $column_def,
134                mysqli_errno($link), mysqli_error($link));
135            continue;
136        }
137
138        /*
139        TODO
140        Unfortunately different server versions give you slightly different
141        results.The test does not yet fully reflect all server changes/bugs etc.
142        */
143        switch ($column_def) {
144            case 'INT UNSIGNED NOT NULL':
145            case 'INT NOT NULL':
146            case 'CHAR(1) NOT NULL':
147            case 'SET("one", "two") NOT NULL':
148            case 'ENUM("one", "two") NOT NULL':
149                $version = mysqli_get_server_version($link);
150                if ($version < 50000) {
151                    // TODO - check exact version!
152                    $expected_flags = trim(str_replace('NO_DEFAULT_VALUE', '', $expected_flags));
153                }
154                break;
155
156            case 'BIT':
157                $version = mysqli_get_server_version($link);
158                if (($version <= 50114 && $version > 50100) || ($version == 50200)) {
159                    // TODO - check exact version!
160                    $expected_flags = trim(str_replace('UNSIGNED', '', $expected_flags));
161                }
162
163            default:
164                break;
165        }
166
167        list($missing_flags, $unexpected_flags, $flags_found) = checkFlags($field->flags, $expected_flags, $flags);
168        if ($unexpected_flags) {
169            printf("[006] Found unexpected flags '%s' for %s, found '%s' with MySQL %s'\n",
170                $unexpected_flags, $column_def, $flags_found, mysqli_get_server_version($link));
171        }
172        if ($missing_flags) {
173            printf("[007] The flags '%s' have not been reported for %s, found '%s'\n",
174                $missing_flags, $column_def, $flags_found);
175            var_dump($create);
176            var_dump(mysqli_get_server_version($link));
177            die($missing_flags);
178        }
179
180        mysqli_free_result($res);
181    }
182
183    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test')) {
184        printf("[008] %s [%d] %s\n", $column_def,
185            mysqli_errno($link), mysqli_error($link));
186    }
187
188    $column_def = array('col1 CHAR(1)', 'col2 CHAR(2)','INDEX idx_col1_col2(col1, col2)');
189    $expected_flags = array('col1' => 'MULTIPLE_KEY PART_KEY', 'col2' => 'PART_KEY');
190    $create = 'CREATE TABLE test(id INT, ';
191    foreach ($column_def as $k => $v) {
192        $create .= sprintf('%s, ', $v);
193    }
194    $create = sprintf('%s)', substr($create, 0, -2));
195
196    if (mysqli_query($link, $create)) {
197        if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
198            printf("[009] Cannot run SELECT, [%d] %s\n",
199                mysqli_errno($link), mysqli_error($link));
200        }
201        // id column - skip it
202        $field = mysqli_fetch_field($res);
203        while ($field = mysqli_fetch_field($res)) {
204            if (!isset($expected_flags[$field->name])) {
205                printf("[010] Found unexpected field '%s'\n", $field->name);
206            }
207            list($missing_flags, $unexpected_flags, $flags_found) = checkFlags($field->flags, $expected_flags[$field->name], $flags);
208            if ($unexpected_flags)
209                printf("[011] Found unexpected flags '%s' for %s, found '%s'\n",
210                    $unexpected_flags, $field->name, $flags_found);
211            if ($missing_flags)
212                printf("[012] The flags '%s' have not been reported for %s, found '%s'\n",
213                    $missing_flags, $field->name, $flags_found);
214        }
215    }
216
217    mysqli_close($link);
218    print "done!";
219?>
220--CLEAN--
221<?php
222    require_once("clean_table.inc");
223?>
224--EXPECT--
225done!
226