summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_stmt_bind_result_format.phpt
blob: 8a1e9b16c04ca9673f05e8f1545bcde59d5aba37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
--TEST--
Playing with SELECT FORMAT(...) AS _format - see also bugs.php.net/42378
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');
?>
--INI--
memory_limit=83886080
--FILE--
<?php
	require_once("connect.inc");

	function create_table($link, $column, $min, $max, $engine, $offset) {

		if (!mysqli_query($link, 'DROP TABLE IF EXISTS test')) {
			printf("[%03d] Cannot drop table test, [%d] %s\n",
				$offset,
				mysqli_errno($link), mysqli_error($link));
			return array();
		}

		$sql = sprintf("CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, col1 %s) ENGINE=%s",
			$column, $engine);
		if (!mysqli_query($link, $sql)) {
			printf("[%03d] Cannot create table test, [%d] %s\n",
				$offset + 1,
				mysqli_errno($link), mysqli_error($link));
			return array();
		}

		$values = array();
		for ($i = 1; $i <= 100; $i++) {
			$col1 = mt_rand($min, $max);
			$values[$i] = $col1;
			$sql = sprintf("INSERT INTO test(id, col1) VALUES (%d, %f)",
				$i, $col1);
			if (!mysqli_query($link, $sql)) {
				printf("[%03d] Cannot insert data, [%d] %s\n",
					$offset + 2,
					mysqli_errno($link), mysqli_error($link));
				return array();
			}
		}

		return $values;
	}

	function test_format($link, $format, $from, $order_by, $expected, $offset) {

		if (!$stmt = mysqli_stmt_init($link)) {
			printf("[%03d] Cannot create PS, [%d] %s\n",
				$offset,
				mysqli_errno($link), mysqli_error($link));
			return false;
		}

		if ($order_by)
			$sql = sprintf('SELECT %s AS _format FROM %s ORDER BY %s', $format, $from, $order_by);
		else
			$sql = sprintf('SELECT %s AS _format FROM %s', $format, $from);

		if (!mysqli_stmt_prepare($stmt, $sql)) {
			printf("[%03d] Cannot prepare PS, [%d] %s\n",
				$offset + 1,
				mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			return false;
		}

		if (!mysqli_stmt_execute($stmt)) {
			printf("[%03d] Cannot execute PS, [%d] %s\n",
				$offset + 2,
				mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			return false;
		}

		if (!mysqli_stmt_store_result($stmt)) {
			printf("[%03d] Cannot store result set, [%d] %s\n",
				$offset + 3,
				mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			return false;
		}

		if (!is_array($expected)) {

			$result = null;
			if (!mysqli_stmt_bind_result($stmt, $result)) {
				printf("[%03d] Cannot bind result, [%d] %s\n",
					$offset + 4,
					mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
				return false;
			}

			if (!mysqli_stmt_fetch($stmt)) {
				printf("[%03d] Cannot fetch result,, [%d] %s\n",
					$offset + 5,
					mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
				return false;
			}

			if ($result !== $expected) {
				printf("[%03d] Expecting %s/%s got %s/%s with %s - %s.\n",
					$offset + 6,
					gettype($expected), $expected,
					gettype($result), $result,
					$format, $sql);
			}

		} else {

			$order_by_col = $result = null;
			if (!mysqli_stmt_bind_result($stmt, $order_by_col, $result)) {
				printf("[%03d] Cannot bind result, [%d] %s\n",
					$offset + 7,
					mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
				return false;
			}

			reset($expected);
			while ((list($k, $v) = each($expected)) && mysqli_stmt_fetch($stmt)) {
				if ($result !== $v) {
					printf("[%03d] Row %d - expecting %s/%s got %s/%s [%s] with %s - %s.\n",
						$offset + 8,
						$k,
						gettype($v), $v,
						gettype($result), $result,
						$order_by_col,
						$format, $sql);
				}
			}

		}

		mysqli_stmt_free_result($stmt);
		mysqli_stmt_close($stmt);

		return true;
	}

	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
		printf("[001] Cannot connect - [%d] %s\n",
			mysqli_connect_errno(),
			mysqli_connect_error());

	/* select from dual - pseudo table */
	test_format($link, 'FORMAT(1.01, 0)', 'DUAL', null, '1', 10);
	test_format($link, 'FORMAT(1.23, 1)', 'DUAL', null, '1.2', 20);
	test_format($link, 'FORMAT(1.23, 2)', 'DUAL', null, '1.23', 30);
	test_format($link, 'FORMAT(1234.567, 3)', 'DUAL', null, '1,234.567', 40);
	/* no typo! */
	test_format($link, 'FORMAT(1234.567, 4)', 'DUAL', null, '1,234.5670', 50);

	mysqli_close($link);
	require_once('table.inc');

	/* select from existing table */
	test_format($link, 'FORMAT(id, 0)', 'test', null, '1', 60);
	test_format($link, 'FORMAT(id + 0.1, 1)', 'test', null, '1.1', 70);
	test_format($link, 'FORMAT(id + 0.01, 2)', 'test', null, '1.01', 80);

	/* create new table and select from it */
	$expected = create_table($link, 'FLOAT', -10000, 10000, $engine, 90);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 100);

	$expected = create_table($link, 'FLOAT UNSIGNED', 0, 10000, $engine, 110);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 120);

	$expected = create_table($link, 'TINYINT', -128, 127, $engine, 130);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 140);

	$expected = create_table($link, 'SMALLINT UNSIGNED', 0, 65535, $engine, 150);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 160);

	$expected = create_table($link, 'MEDIUMINT', 0, 8388607, $engine, 170);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 180);

	$expected = create_table($link, 'INT UNSIGNED', 0, 1000, $engine, 190);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 200);

	$expected = create_table($link, 'BIGINT', -1000, 1000, $engine, 210);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 220);

	$expected = create_table($link, 'DECIMAL(5,0)', -1000, 1000, $engine, 230);
	foreach ($expected as $k => $v)
		$expected[$k] = number_format(round($v), 0, '.', ',');
	test_format($link, 'id AS order_by_col, FORMAT(col1, 0)', 'test', 'id', $expected, 240);

	// http://bugs.php.net/bug.php?id=42378
	if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
		printf("[300] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
	}

	if (mysqli_query($link, "CREATE TABLE `test` (
  `targetport` int(11) NOT NULL default '0',
  `sources` double(17,4) default NULL,
  `current_sources` double(17,4) default NULL,
  `reports` double(17,4) default NULL,
  `current_reports` double(17,4) default NULL,
  `targets` double(17,4) default NULL,
  `current_targets` double(17,4) default NULL,
  `maxsources` int(11) default NULL,
  `maxtargets` int(11) default NULL,
  `maxreports` int(11) default NULL,
  `trend` float default NULL,
  PRIMARY KEY  (`targetport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1")) {

		do {
			$values = array();
			for ($i = 0; $i < 200; $i++) {
				$current_targets = mt_rand(-100000, 100000) / 10;
				do {
					$trend = (mt_rand(0, 3) > 1) ? (mt_rand(-10000, 10000) / 100) : 'NULL';
				} while (isset($values[$trend]));

				$sql = sprintf('INSERT INTO test(targetport, current_targets, maxreports, trend) VALUES (%d, %f, %s, %s)',
					$i,
					$current_targets,
					(mt_rand(0, 1) > 0) ? mt_rand(0, 1000) : 'NULL',
					$trend);
				if (!mysqli_query($link, $sql)) {
					printf("[301] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
					break 2;
				}
				if ($current_targets > 0 && $trend !== 'NULL')
					$values[$trend] = $i;
			}
			krsort($values);

			if (!$stmt = mysqli_stmt_init($link)) {
				printf("[302] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
				break;
			}

			if (!mysqli_stmt_prepare($stmt, 'SELECT trend, targetport, FORMAT(trend, 2) FROM test WHERE current_targets > 0 AND trend IS NOT NULL ORDER BY trend DESC LIMIT 100')) {
				printf("[303] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			if (!mysqli_stmt_execute($stmt)) {
				printf("[304] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			if (!mysqli_stmt_store_result($stmt)) {
				printf("[305] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			$trend = $targetport = $format = null;
			if (!mysqli_stmt_bind_result($stmt, $trend, $targetport, $format)) {

				printf("[305] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			reset($values);
			while (mysqli_stmt_fetch($stmt)) {
				list($exp_trend, $exp_targetport) = each($values);
				if ($targetport != $exp_targetport) {
					printf("[306] Values fetched from MySQL seem to be wrong, check manually\n");
					printf("%s/%s - %s/%s - '%s'\n", $trend, $exp_trend, $targetport, $exp_targetport, $format);
				}
			}
			mysqli_stmt_free_result($stmt);
			mysqli_stmt_close($stmt);

			// same but OO interface
			if (!$stmt = mysqli_stmt_init($link)) {
				printf("[307] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
				break;
			}

			if (!$stmt->prepare('SELECT trend, targetport, FORMAT(trend, 2) FROM test WHERE current_targets > 0 AND trend IS NOT NULL ORDER BY trend DESC LIMIT 100')) {
				printf("[308] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			if (!$stmt->execute()) {
				printf("[309] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			if (!$stmt->store_result()) {
				printf("[310] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			$trend = $targetport = $format = null;
			if (!$stmt->bind_result($trend, $targetport, $format)) {

				printf("[311] [%d] %s\n", mysqli_stmt_errno($link), mysqli_stmt_error($link));
				break;
			}

			reset($values);
			while ($stmt->fetch()) {
				list($exp_trend, $exp_targetport) = each($values);
				if ($targetport != $exp_targetport) {
					printf("[312] Values fetched from MySQL seem to be wrong, check manually\n");
					printf("%s/%s - %s/%s - '%s'\n", $trend, $exp_trend, $targetport, $exp_targetport, $format);
				}
			}
			$stmt->free_result();
			$stmt->close();

		} while (false);

	} else {
		var_dump(mysqli_error($link));
	}


	mysqli_close($link);
	print "done!";
?>
--CLEAN--
<?php
	require_once("clean_table.inc");
?>
--EXPECTF--
done!