Annotation of embedaddon/php/ext/mysqli/tests/mysqli_fetch_field_flags.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: mysqli_fetch_field() - flags/field->flags
3: --SKIPIF--
4: <?php
5: require_once('skipif.inc');
6: require_once('skipifemb.inc');
7: require_once('skipifconnectfailure.inc');
8:
9: require_once('connect.inc');
10: if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
11: die(printf("skip: [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
12:
13: if (mysqli_get_server_version($link) < 50041)
14: die("skip: Due to many MySQL Server differences, the test requires 5.0.41+");
15:
16: mysqli_close($link);
17: ?>
18: --FILE--
19: <?php
20: require_once("connect.inc");
21:
22: /* TODO: mysqli.c needs to export a few more constants - see all the defined() calls! */
23:
24: $flags = array(
25: MYSQLI_NOT_NULL_FLAG => 'NOT_NULL',
26: MYSQLI_PRI_KEY_FLAG => 'PRI_KEY',
27: MYSQLI_UNIQUE_KEY_FLAG => 'UNIQUE_KEY',
28: MYSQLI_MULTIPLE_KEY_FLAG => 'MULTIPLE_KEY',
29: MYSQLI_BLOB_FLAG => 'BLOB',
30: MYSQLI_UNSIGNED_FLAG => 'UNSIGNED',
31: MYSQLI_ZEROFILL_FLAG => 'ZEROFILL',
32: MYSQLI_AUTO_INCREMENT_FLAG => 'AUTO_INCREMENT',
33: MYSQLI_TIMESTAMP_FLAG => 'TIMESTAMP',
34: MYSQLI_SET_FLAG => 'SET',
35: MYSQLI_NUM_FLAG => 'NUM',
36: MYSQLI_PART_KEY_FLAG => 'PART_KEY',
37: // MYSQLI_GROUP_FLAG => 'MYSQLI_GROUP_FLAG' - internal usage only
38: (defined('MYSQLI_NO_DEFAULT_VALUE_FLAG') ? MYSQLI_NO_DEFAULT_VALUE_FLAG : 4096) => 'NO_DEFAULT_VALUE',
39: (defined('MYSQLI_BINARY_FLAG') ? MYSQLI_BINARY_FLAG : 128) => 'BINARY',
40: (defined('MYSQLI_ENUM_FLAG') ? MYSQLI_ENUM_FLAG : 256) => 'ENUM',
41: // MYSQLI_BINCMP_FLAG
42: );
43:
44: // 5.1.24 / 6.0.4+
45: if (defined('MYSQLI_ON_UPDATE_NOW'))
46: $flags[MYSQLI_ON_UPDATE_NOW] = 'ON_UPDATE_NOW';
47: else
48: $flags[8192] = 'ON_UPDATE_NOW';
49:
50: krsort($flags);
51:
52: $columns = array(
53: 'INT DEFAULT NULL' => 'NUM',
54: 'INT NOT NULL' => 'NOT_NULL NO_DEFAULT_VALUE NUM',
55: 'INT NOT NULL DEFAULT 1' => 'NOT_NULL NUM',
56: 'INT UNSIGNED DEFAULT NULL' => 'UNSIGNED NUM',
57: 'INT UNSIGNED NOT NULL' => 'NOT_NULL UNSIGNED NO_DEFAULT_VALUE NUM',
58: 'INT UNSIGNED NOT NULL DEFAULT 1' => 'NOT_NULL UNSIGNED NUM',
59: 'INT UNSIGNED ZEROFILL DEFAULT NULL' => 'UNSIGNED ZEROFILL NUM',
60: 'INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY' => 'NOT_NULL PRI_KEY UNSIGNED AUTO_INCREMENT NUM PART_KEY',
61: 'CHAR(1) DEFAULT NULL' => '',
62: 'CHAR(1) NOT NULL' => 'NOT_NULL NO_DEFAULT_VALUE',
63: 'TIMESTAMP NOT NULL' => 'NOT_NULL UNSIGNED ZEROFILL BINARY TIMESTAMP',
64: 'VARBINARY(127) DEFAULT NULL' => 'BINARY',
65: 'BLOB' => 'BLOB BINARY',
66: 'TINYBLOB' => 'BLOB BINARY',
67: 'MEDIUMBLOB' => 'BLOB BINARY',
68: 'LONGBLOB' => 'BLOB BINARY',
69: 'TEXT' => 'BLOB',
70: 'TINYTEXT' => 'BLOB',
71: 'MEDIUMTEXT' => 'BLOB',
72: 'LONGTEXT' => 'BLOB',
73: 'SET("one", "two")' => 'SET',
74: 'SET("one", "two") NOT NULL' => 'NOT_NULL SET NO_DEFAULT_VALUE',
75: 'SET("one", "two") NOT NULL DEFAULT "one"' => 'NOT_NULL SET',
76: 'ENUM("one", "two")' => 'ENUM',
77: 'ENUM("one", "two") NOT NULL' => 'NOT_NULL ENUM NO_DEFAULT_VALUE',
78: 'ENUM("one", "two") NOT NULL DEFAULT "one"' => 'NOT_NULL ENUM',
79: 'TINYINT UNIQUE' => 'UNIQUE_KEY NUM PART_KEY',
80: 'SMALLINT UNIQUE' => 'UNIQUE_KEY NUM PART_KEY',
81: 'MEDIUMINT UNIQUE DEFAULT 1' => 'UNIQUE_KEY NUM PART_KEY',
82: 'BIGINT UNSIGNED UNIQUE DEFAULT 100' => 'UNIQUE_KEY UNSIGNED NUM PART_KEY',
83: 'BIT' => 'UNSIGNED',
84: 'VARCHAR(2) NOT NULL PRIMARY KEY' => 'NOT_NULL PRI_KEY NO_DEFAULT_VALUE PART_KEY'
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 'TIMESTAMP NOT NULL':
145: // http://bugs.mysql.com/bug.php?id=30081 - new flag introduced in 5.1.24/6.0.4
146: $version = mysqli_get_server_version($link);
147: if ((($version > 50122) && ($version < 60000) && ($version != 50200)) ||
148: ($version >= 60004)) {
149: // new flag ON_UPDATE_NOW_FLAG (8192)
150: $expected_flags .= ' ON_UPDATE_NOW';
151: }
152: break;
153:
154: case 'INT UNSIGNED NOT NULL':
155: case 'INT NOT NULL':
156: case 'CHAR(1) NOT NULL':
157: case 'SET("one", "two") NOT NULL':
158: case 'ENUM("one", "two") NOT NULL':
159: $version = mysqli_get_server_version($link);
160: if ($version < 50000) {
161: // TODO - check exact version!
162: $expected_flags = trim(str_replace('NO_DEFAULT_VALUE', '', $expected_flags));
163: }
164: break;
165:
166: case 'BIT':
167: $version = mysqli_get_server_version($link);
168: if (($version <= 50114 && $version > 50100) || ($version == 50200)) {
169: // TODO - check exact version!
170: $expected_flags = trim(str_replace('UNSIGNED', '', $expected_flags));
171: }
172:
173: default:
174: break;
175: }
176:
177: list($missing_flags, $unexpected_flags, $flags_found) = checkFlags($field->flags, $expected_flags, $flags);
178: if ($unexpected_flags) {
179: printf("[006] Found unexpected flags '%s' for %s, found '%s' with MySQL %s'\n",
180: $unexpected_flags, $column_def, $flags_found, mysqli_get_server_version($link));
181: }
182: if ($missing_flags) {
183: printf("[007] The flags '%s' have not been reported for %s, found '%s'\n",
184: $missing_flags, $column_def, $flags_found);
185: var_dump($create);
186: var_dump(mysqli_get_server_version($link));
187: die($missing_flags);
188: }
189:
190: mysqli_free_result($res);
191: }
192:
193: if (!mysqli_query($link, 'DROP TABLE IF EXISTS test')) {
194: printf("[008] %s [%d] %s\n", $column_def,
195: mysqli_errno($link), mysqli_error($link));
196: continue;
197: }
198:
199: $column_def = array('col1 CHAR(1)', 'col2 CHAR(2)','INDEX idx_col1_col2(col1, col2)');
200: $expected_flags = array('col1' => 'MULTIPLE_KEY PART_KEY', 'col2' => 'PART_KEY');
201: $create = 'CREATE TABLE test(id INT, ';
202: foreach ($column_def as $k => $v) {
203: $create .= sprintf('%s, ', $v);
204: }
205: $create = sprintf('%s)', substr($create, 0, -2));
206:
207: if (mysqli_query($link, $create)) {
208: if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
209: printf("[009] Cannot run SELECT, [%d] %s\n",
210: mysqli_errno($link), mysqli_error($link));
211: }
212: // id column - skip it
213: $field = mysqli_fetch_field($res);
214: while ($field = mysqli_fetch_field($res)) {
215: if (!isset($expected_flags[$field->name])) {
216: printf("[010] Found unexpected field '%s'\n", $field->name);
217: continue;
218: }
219: list($missing_flags, $unexpected_flags, $flags_found) = checkFlags($field->flags, $expected_flags[$field->name], $flags);
220: if ($unexpected_flags)
221: printf("[011] Found unexpected flags '%s' for %s, found '%s'\n",
222: $unexpected_flags, $field->name, $flags_found);
223: if ($missing_flags)
224: printf("[012] The flags '%s' have not been reported for %s, found '%s'\n",
225: $missing_flags, $field->name, $flags_found);
226: }
227: }
228:
229: mysqli_close($link);
230: print "done!";
231: ?>
232: --CLEAN--
233: <?php
234: require_once("clean_table.inc");
235: ?>
236: --EXPECTF--
237: done!
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>