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