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