summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_stmt_attr_set.phpt
blob: c2ddd211984f96e3712ecfd3b006741acca2ccfc (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
--TEST--
mysqli_stmt_attr_set() - mysqlnd does not check for invalid codes
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
	require_once("connect.inc");

	$tmp    = NULL;
	$link   = NULL;

	if (!is_null($tmp = @mysqli_stmt_attr_set()))
		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	if (!is_null($tmp = @mysqli_stmt_attr_set($link)))
		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	if (!is_null($tmp = @mysqli_stmt_attr_set($link, $link)))
		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	if (!is_null($tmp = @mysqli_stmt_attr_set($link, $link, $link)))
		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	require('table.inc');

	$valid_attr = array(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
	if ((mysqli_get_client_version() > 50003) || $IS_MYSQLND) {
		$valid_attr[] = MYSQLI_STMT_ATTR_CURSOR_TYPE;
		$valid_attr[] =	MYSQLI_CURSOR_TYPE_NO_CURSOR;
		$valid_attr[] =	MYSQLI_CURSOR_TYPE_READ_ONLY;
		$valid_attr[] =	MYSQLI_CURSOR_TYPE_FOR_UPDATE;
		$valid_attr[] =	MYSQLI_CURSOR_TYPE_SCROLLABLE;
	}

	if ((mysqli_get_client_version() > 50007) || $IS_MYSQLND)
		$valid_attr[] = MYSQLI_STMT_ATTR_PREFETCH_ROWS;


	$stmt = mysqli_stmt_init($link);
	if (!is_null($tmp = @mysqli_stmt_attr_set($stmt, 0, 0)))
		printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);

	$stmt->prepare("SELECT * FROM test");

	mt_srand(microtime(true));

	for ($i = -100; $i < 1000; $i++) {
		if (in_array($i, $valid_attr))
			continue;
		$invalid_attr = $i;
		if (false !== ($tmp = @mysqli_stmt_attr_set($stmt, $invalid_attr, 0))) {
			printf("[006a] Expecting boolean/false for attribute %d, got %s/%s\n", $invalid_attr, gettype($tmp), $tmp);
		}
	}

	for ($i = 0; $i < 2; $i++) {
		do {
			$invalid_attr = mt_rand(-1 * (min(4294967296, PHP_INT_MAX) + 1), min(4294967296, PHP_INT_MAX));
		} while (in_array($invalid_attr, $valid_attr));
		if (false !== ($tmp = @mysqli_stmt_attr_set($stmt, $invalid_attr, 0))) {
			printf("[006b] Expecting boolean/false for attribute %d, got %s/%s\n", $invalid_attr, gettype($tmp), $tmp);
		}
	}
	$stmt->close();

	//
	// MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH
	//


	// expecting max_length not to be set and be 0 in all cases
	$stmt = mysqli_stmt_init($link);
	$stmt->prepare("SELECT label FROM test");
	$stmt->execute();
	$stmt->store_result();
	$res = $stmt->result_metadata();
	$fields = $res->fetch_fields();
	$max_lengths = array();
	foreach ($fields as $k => $meta) {
		$max_lengths[$meta->name] = $meta->max_length;
		if ($meta->max_length !== 0)
			printf("[007] max_length should be not set (= 0), got %s for field %s\n", $meta->max_length, $meta->name);
	}
	$res->close();
	$stmt->close();

	// expecting max_length to _be_ set
	$stmt = mysqli_stmt_init($link);
	$stmt->prepare("SELECT label FROM test");
	$stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, 1);
	$res = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
	if ($res !== 1)
		printf("[007.1] max_length should be 1, got %s\n", $res);
	$stmt->execute();
	$stmt->store_result();
	$res = $stmt->result_metadata();
	$fields = $res->fetch_fields();
	$max_lengths = array();
	foreach ($fields as $k => $meta) {
		$max_lengths[$meta->name] = $meta->max_length;
		if ($meta->max_length === 0)
			printf("[008] max_length should be set (!= 0), got %s for field %s\n", $meta->max_length, $meta->name);
	}
	$res->close();
	$stmt->close();

	// expecting max_length not to be set
	$stmt = mysqli_stmt_init($link);
	$stmt->prepare("SELECT label FROM test");
	$stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, 0);
	$res = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
	if ($res !== 0)
		printf("[008.1] max_length should be 0, got %s\n", $res);
	$stmt->execute();
	$stmt->store_result();
	$res = $stmt->result_metadata();
	$fields = $res->fetch_fields();
	$max_lengths = array();
	foreach ($fields as $k => $meta) {
		$max_lengths[$meta->name] = $meta->max_length;
		if ($meta->max_length !== 0)
			printf("[009] max_length should not be set (= 0), got %s for field %s\n", $meta->max_length, $meta->name);
	}
	$res->close();
	$stmt->close();

	//
	// Cursors
	//

	if (mysqli_get_client_version() > 50003) {

		$cursor_types = array(
			MYSQLI_CURSOR_TYPE_NO_CURSOR,
			MYSQLI_CURSOR_TYPE_READ_ONLY,
			MYSQLI_CURSOR_TYPE_FOR_UPDATE,
			MYSQLI_CURSOR_TYPE_SCROLLABLE
		);
		do {
			$invalid_cursor_type = mt_rand(-1000, 1000);
		} while (in_array($invalid_cursor_type, $cursor_types));

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");

		if (false !== ($tmp = @$stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, $invalid_cursor_type)))
			printf("[010] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);

		if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_FOR_UPDATE)))
			printf("[011] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);

		if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_SCROLLABLE)))
			printf("[012] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);

		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_NO_CURSOR)))
			printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);

		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY)))
			printf("[014] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);

		$stmt->close();

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");
		$stmt->execute();
		$id = $label = NULL;
		$stmt->bind_result($id, $label);
		$results = array();
		while ($stmt->fetch())
			$results[$id] = $label;
		$stmt->close();
		if (empty($results))
			printf("[015] Results should not be empty, subsequent tests will probably fail!\n");

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");
		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_NO_CURSOR)))
			printf("[016] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->execute();
		$id = $label = NULL;
		$stmt->bind_result($id, $label);
		$results2 = array();
		while ($stmt->fetch())
			$results2[$id] = $label;
		$stmt->close();
		if ($results != $results2) {
			printf("[017] Results should not differ. Dumping both result sets.\n");
			var_dump($results);
			var_dump($results2);
		}

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");
		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY)))
			printf("[018] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->execute();
		$id = $label = NULL;
		$stmt->bind_result($id, $label);
		$results2 = array();
		while ($stmt->fetch())
			$results2[$id] = $label;
		$stmt->close();
		if ($results != $results2) {
			printf("[019] Results should not differ. Dumping both result sets.\n");
			var_dump($results);
			var_dump($results2);
		}

	}


	//
	// MYSQLI_STMT_ATTR_PREFETCH_ROWS
	//

	if (mysqli_get_client_version() > 50007) {

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");
		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, 1)))
			printf("[020] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->execute();
		$id = $label = NULL;
		$stmt->bind_result($id, $label);
		$results = array();
		while ($stmt->fetch())
			$results[$id] = $label;
		$stmt->close();
		if (empty($results))
			printf("[021] Results should not be empty, subsequent tests will probably fail!\n");

		/* prefetch is not supported
		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT label FROM test");
		if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, -1)))
			printf("[022] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->close();

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT label FROM test");
		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, PHP_INT_MAX)))
				printf("[023] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->close();

		$stmt = mysqli_stmt_init($link);
		$stmt->prepare("SELECT id, label FROM test");
		if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, 2)))
			printf("[024] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
		$stmt->execute();
		$id = $label = NULL;
		$stmt->bind_result($id, $label);
		$results2 = array();
		while ($stmt->fetch())
			$results2[$id] = $label;
		$stmt->close();
		if ($results != $results2) {
			printf("[025] Results should not differ. Dumping both result sets.\n");
			var_dump($results);
			var_dump($results2);
		}
		*/

	}

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