summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_stmt_get_result_geom.phpt
blob: b6794ad41e40f075054d9f935a050829d11c6714 (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
--TEST--
mysqli_stmt_get_result - geometry / spatial types
--SKIPIF--
<?php
	require_once('skipif.inc');
	require_once('skipifemb.inc');
	require_once('skipifconnectfailure.inc');

	if (!function_exists('mysqli_stmt_get_result'))
		die("skip mysqli_stmt_get_result() not available");

	if (!defined("MYSQLI_TYPE_GEOMETRY"))
		die("skip MYSQLI_TYPE_GEOMETRY not defined");
?>
--FILE--
<?php
	require('connect.inc');
	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
		printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());

	function func_mysqli_stmt_get_result_geom($link, $engine, $sql_type, $bind_value, $offset) {

		if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
			printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
			return false;
		}

		if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
			// don't bail - column type might not be supported by the server, ignore this
			return false;
		}

		for ($id = 1; $id < 4; $id++) {
			$sql = sprintf("INSERT INTO test(id, label) VALUES (%d, %s)", $id, $bind_value);
			if (!mysqli_query($link, $sql)) {
				printf("[%04d] [%d] %s\n", $offset + 2 + $id, mysqli_errno($link), mysqli_error($link));
			}
		}

		if (!$stmt = mysqli_stmt_init($link)) {
			printf("[%04d] [%d] %s\n", $offset + 6, mysqli_errno($link), mysqli_error($link));
			return false;
		}

		if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) {
			printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			mysqli_stmt_close($stmt);
			return false;
		}

		if (!mysqli_stmt_execute($stmt)) {
			printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			mysqli_stmt_close($stmt);
			return false;
		}
		if (!$res = mysqli_stmt_get_result($stmt)) {
			printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
			mysqli_stmt_close($stmt);
			return false;
		}

		$result = mysqli_stmt_result_metadata($stmt);
		$fields = mysqli_fetch_fields($result);
		if ($fields[1]->type != MYSQLI_TYPE_GEOMETRY) {
			printf("[%04d] [%d] %s wrong type %d\n", $offset + 10, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $fields[1]->type);
		}

		$num = 0;
		while ($row = mysqli_fetch_assoc($res)) {
			$bind_res = &$row['label'];

			if (!$stmt2 = mysqli_stmt_init($link)) {
				printf("[%04d] [%d] %s\n", $offset + 11, mysqli_errno($link), mysqli_error($link));
				return false;
			}

			if (!mysqli_stmt_prepare($stmt2, "INSERT INTO test(id, label) VALUES (?, ?)")) {
				printf("[%04d] [%d] %s\n", $offset + 12, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
				return false;
			}

			$id = $row['id'] + 10;
			if (!mysqli_stmt_bind_param($stmt2, "is", $id, $bind_res)) {
				printf("[%04d] [%d] %s\n", $offset + 13, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
				return false;
			}

			if (!mysqli_stmt_execute($stmt2)) {
				printf("[%04d] [%d] %s\n", $offset + 14, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
				return false;
			}
			mysqli_stmt_close($stmt2);

			if (!$res_normal = mysqli_query($link, sprintf("SELECT id, label FROM test WHERE id = %d",
					$row['id'] + 10))) {
				printf("[%04d] [%d] %s\n", $offset + 15, mysqli_errno($link), mysqli_error($link));
				return false;
			}

			if (!$row_normal = mysqli_fetch_assoc($res_normal)) {
				printf("[%04d] [%d] %s\n", $offset + 16, mysqli_errno($link), mysqli_error($link));
				return false;
			}

			if ($row_normal['label'] != $bind_res) {
				printf("[%04d] PS and non-PS return different data.\n", $offset + 17);
				return false;
			}
			mysqli_free_result($res_normal);
			$num++;
		}

		if ($num != 3) {
			printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
				$offset + 18, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
			mysqli_free_result($res);
			mysqli_stmt_close($stmt);
			return false;
		}
		mysqli_free_result($res);
		mysqli_stmt_close($stmt);

		return true;
	}

	func_mysqli_stmt_get_result_geom($link, $engine, "GEOMETRY", "GeomFromText('POINT(2 2)')", 20);
	func_mysqli_stmt_get_result_geom($link, $engine, "POINT", "GeomFromText('POINT(1 1)')", 40);
	func_mysqli_stmt_get_result_geom($link, $engine, "LINESTRING", "GeomFromText('LINESTRING(0 0,1 1,2 2)')", 60);
	func_mysqli_stmt_get_result_geom($link, $engine, "POLYGON", "GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))')", 80);
	func_mysqli_stmt_get_result_geom($link, $engine, "MULTIPOINT", "GeomFromText('MULTIPOINT(1 1, 2 2)')", 100);
	func_mysqli_stmt_get_result_geom($link, $engine, "MULTILINESTRING", "GeomFromText('MULTILINESTRING((0 0,1 1,2 2),(0 0,1 1,3 3))')", 120);
	func_mysqli_stmt_get_result_geom($link, $engine, "MULTIPOLYGON", "GeomFromText('MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5)),((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5)))')", 140);
	func_mysqli_stmt_get_result_geom($link, $engine, "GEOMETRYCOLLECTION", "GeomFromText('GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))')", 160);

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