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