summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_store_result_copy.phpt
blob: 3f82b8a8925a281e8e903619449736b903de6f0e (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
--TEST--
mysqli_store_result()
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');
if (!$IS_MYSQLND) {
    die("SKIP mysqlnd only test");
}
?>
--INI--
mysqlnd.debug=d:t:O,/tmp/mysqlnd.trace
mysqlnd.net_read_buffer_size=1
mysqlnd.mempool_default_size=1
mysqlnd.fetch_data_copy=0
--FILE--
<?php
	require_once("connect.inc");

	$tmp    = NULL;
	$link   = NULL;

	require('table.inc');

	if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id"))
		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[004] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	if (true !== ($tmp = mysqli_data_seek($res, 2)))
		printf("[005] Expecting boolean/true, got %s/%s. [%d] %s\n",
			gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));

	var_dump($res->fetch_assoc());

	if (true !== ($tmp = mysqli_data_seek($res, 0)))
		printf("[006] Expecting boolean/true, got %s/%s. [%d] %s\n",
			gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));

	while ($row = $res->fetch_assoc()) {
		printf("id = %d, label = %s\n", $row['id'], $row['label']);
	}

	mysqli_free_result($res);
	mysqli_close($link);

	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
		printf("[007] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
			$host, $user, $db, $port, $socket);
	}


	if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id"))
		printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[009] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	$no_result = 0;
	for ($i = 0; $i < 1000; $i++) {
		$idx = mt_rand(-100, 100);
		if (true === @mysqli_data_seek($res, $idx)) {
			$row = $res->fetch_assoc();
			if (!isset($row['id']) || !isset($row['label'])) {
				printf("[010] Brute force seek %d returned %d\n", $idx, var_export($row, true));
			}
		} else {
			$no_result++;
		}
	}
	printf("No result: %d\n", $no_result);

	/* implicit free, implicit store */
	/* meta and fetch lengths code */
	if (!$res = mysqli_query($link, "SELECT CONCAT(id, id) AS _c, label FROM test ORDER BY id DESC LIMIT 2"))
		printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	printf("Default\n");
	var_dump(mysqli_fetch_lengths($res));
	$fields = $res->fetch_fields();
	while ($row = $res->fetch_assoc()) {
		var_dump(mysqli_fetch_lengths($res));
	}
	var_dump(mysqli_fetch_lengths($res));

	if (!$res = mysqli_real_query($link, "SELECT CONCAT(id, id) AS _c, label FROM test ORDER BY id DESC LIMIT 2"))
		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[013] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	printf("Copy\n");
	var_dump(mysqli_fetch_lengths($res));
	$fields_copy = $res->fetch_fields();
	while ($row = $res->fetch_assoc()) {
		var_dump(mysqli_fetch_lengths($res));
	}
	var_dump(mysqli_fetch_lengths($res));

	/* There's no need for in-depth testing here. If you want in-depth switch mysqlnd
	globally to copy mode and run all the tests */
	foreach ($fields as $k => $field_info) {
		if ($fields_copy[$k] != $field_info) {
			printf("[014] Metadata seems to differ, dumping\n");
			var_dump($field_info);
			var_dump($fields_copy[$k]);
		}
	}

	/* fetch all */

	if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
		printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[016] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	foreach (mysqli_fetch_all($res, MYSQLI_ASSOC) as $row) {
		printf("id = %d label = %s\n", $row['id'], $row['label']);
	}

	if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
		printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[018] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	/* provoke out of sync */
	if (!mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
		printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	var_dump($res->fetch_assoc());

	if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2"))
		printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
		printf("[021] Expecting object, got %s/%s. [%d] %s\n",
			gettype($res), $res, mysqli_errno($link), mysqli_error($link));

	/* user conn killed, res associated with conn, fetch from res */
	unset($link);
	var_dump($res->fetch_assoc());


	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
		printf("[022] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
			$host, $user, $db, $port, $socket);
	}

	if (!$res = mysqli_real_query($link, "INSERT INTO test(id, label) VALUES (100, 'z')"))
		printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

	mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA);

	if (mysqli_get_server_version($link) > 50000) {
		// let's try to play with stored procedures
		mysqli_real_query($link, 'DROP PROCEDURE IF EXISTS p');
		if (mysqli_real_query($link, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) READS SQL DATA BEGIN SELECT id FROM test WHERE id >= 100 ORDER BY id; SELECT id + 1, label FROM test WHERE id > 0 AND id < 3 ORDER BY id; SELECT VERSION() INTO ver_param;
END;')) {
			mysqli_multi_query($link, "CALL p(@version)");
			do {
				if ($res = $link->store_result(MYSQLI_STORE_RESULT_COPY_DATA)) {
					printf("---\n");
					var_dump($res->fetch_all());
					$res->free();
				} else {
					if ($link->errno) {
						echo "Store failed: (" . $link->errno . ") " . $link->error;
					}
				}
			} while ($link->more_results() && $link->next_result());
			mysqli_real_query($link, 'SELECT @version AS p_version');
			$res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA);

			$tmp = mysqli_fetch_assoc($res);
			if (!is_array($tmp) || empty($tmp) || !isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
				printf("[024] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
				var_dump($tmp);
			}

			mysqli_free_result($res);
		} else {
				printf("[025] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
		}
	}

	print "done!";
?>
--CLEAN--
<?php
	require_once("clean_table.inc");
?>
--EXPECTF--
array(2) {
  ["id"]=>
  string(1) "3"
  ["label"]=>
  string(1) "c"
}
id = 1, label = a
id = 2, label = b
id = 3, label = c
id = 4, label = d
id = 5, label = e
id = 6, label = f
No result: %d
Default
bool(false)
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(1)
}
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(1)
}
bool(false)
Copy
bool(false)
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(1)
}
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(1)
}
bool(false)
id = 6 label = f
id = 5 label = e
array(2) {
  ["id"]=>
  string(1) "6"
  ["label"]=>
  string(1) "f"
}
[020] [2014] %s
array(2) {
  ["id"]=>
  string(1) "6"
  ["label"]=>
  string(1) "f"
}
---
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(3) "100"
  }
}
---
array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "a"
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "b"
  }
}
done!