1--TEST--
2mysqli_stmt_get_result - geometry / spatial types
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7    require_once('skipifconnectfailure.inc');
8
9    if (!function_exists('mysqli_stmt_get_result'))
10        die("skip mysqli_stmt_get_result() not available");
11
12    if (!defined("MYSQLI_TYPE_GEOMETRY"))
13        die("skip MYSQLI_TYPE_GEOMETRY not defined");
14?>
15--FILE--
16<?php
17    require('connect.inc');
18    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
19        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
20
21    function func_mysqli_stmt_get_result_geom($link, $engine, $sql_type, $bind_value, $offset) {
22
23        if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
24            printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
25            return false;
26        }
27
28        if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
29            // don't bail - column type might not be supported by the server, ignore this
30            return false;
31        }
32
33        for ($id = 1; $id < 4; $id++) {
34            $sql = sprintf("INSERT INTO test(id, label) VALUES (%d, %s)", $id, $bind_value);
35            if (!mysqli_query($link, $sql)) {
36                printf("[%04d] [%d] %s\n", $offset + 2 + $id, mysqli_errno($link), mysqli_error($link));
37            }
38        }
39
40        if (!$stmt = mysqli_stmt_init($link)) {
41            printf("[%04d] [%d] %s\n", $offset + 6, mysqli_errno($link), mysqli_error($link));
42            return false;
43        }
44
45        if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) {
46            printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
47            mysqli_stmt_close($stmt);
48            return false;
49        }
50
51        if (!mysqli_stmt_execute($stmt)) {
52            printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
53            mysqli_stmt_close($stmt);
54            return false;
55        }
56        if (!$res = mysqli_stmt_get_result($stmt)) {
57            printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
58            mysqli_stmt_close($stmt);
59            return false;
60        }
61
62        $result = mysqli_stmt_result_metadata($stmt);
63        $fields = mysqli_fetch_fields($result);
64        if ($fields[1]->type != MYSQLI_TYPE_GEOMETRY) {
65            printf("[%04d] [%d] %s wrong type %d\n", $offset + 10, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $fields[1]->type);
66        }
67
68        $num = 0;
69        while ($row = mysqli_fetch_assoc($res)) {
70            $bind_res = &$row['label'];
71
72            if (!$stmt2 = mysqli_stmt_init($link)) {
73                printf("[%04d] [%d] %s\n", $offset + 11, mysqli_errno($link), mysqli_error($link));
74                return false;
75            }
76
77            if (!mysqli_stmt_prepare($stmt2, "INSERT INTO test(id, label) VALUES (?, ?)")) {
78                printf("[%04d] [%d] %s\n", $offset + 12, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
79                return false;
80            }
81
82            $id = $row['id'] + 10;
83            if (!mysqli_stmt_bind_param($stmt2, "is", $id, $bind_res)) {
84                printf("[%04d] [%d] %s\n", $offset + 13, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
85                return false;
86            }
87
88            if (!mysqli_stmt_execute($stmt2)) {
89                printf("[%04d] [%d] %s\n", $offset + 14, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
90                return false;
91            }
92            mysqli_stmt_close($stmt2);
93
94            if (!$res_normal = mysqli_query($link, sprintf("SELECT id, label FROM test WHERE id = %d",
95                    $row['id'] + 10))) {
96                printf("[%04d] [%d] %s\n", $offset + 15, mysqli_errno($link), mysqli_error($link));
97                return false;
98            }
99
100            if (!$row_normal = mysqli_fetch_assoc($res_normal)) {
101                printf("[%04d] [%d] %s\n", $offset + 16, mysqli_errno($link), mysqli_error($link));
102                return false;
103            }
104
105            if ($row_normal['label'] != $bind_res) {
106                printf("[%04d] PS and non-PS return different data.\n", $offset + 17);
107                return false;
108            }
109            mysqli_free_result($res_normal);
110            $num++;
111        }
112
113        if ($num != 3) {
114            printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
115                $offset + 18, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
116            mysqli_free_result($res);
117            mysqli_stmt_close($stmt);
118            return false;
119        }
120        mysqli_free_result($res);
121        mysqli_stmt_close($stmt);
122
123        return true;
124    }
125
126    $geomFromText = $link->server_version >= 80000 ? "ST_GeomFromText" : "GeomFromText";
127    func_mysqli_stmt_get_result_geom($link, $engine, "GEOMETRY", "$geomFromText('POINT(2 2)')", 20);
128    func_mysqli_stmt_get_result_geom($link, $engine, "POINT", "$geomFromText('POINT(1 1)')", 40);
129    func_mysqli_stmt_get_result_geom($link, $engine, "LINESTRING", "$geomFromText('LINESTRING(0 0,1 1,2 2)')", 60);
130    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);
131    func_mysqli_stmt_get_result_geom($link, $engine, "MULTIPOINT", "$geomFromText('MULTIPOINT(1 1, 2 2)')", 100);
132    func_mysqli_stmt_get_result_geom($link, $engine, "MULTILINESTRING", "$geomFromText('MULTILINESTRING((0 0,1 1,2 2),(0 0,1 1,3 3))')", 120);
133    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);
134    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);
135
136    mysqli_close($link);
137    print "done!";
138?>
139--CLEAN--
140<?php
141    require_once("clean_table.inc");
142?>
143--EXPECT--
144done!
145