1--TEST--
2mysqli_stmt_fetch - geometry / spatial types
3--SKIPIF--
4<?php
5    require_once('skipif.inc');
6    require_once('skipifemb.inc');
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_fetch_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) || !mysqli_stmt_store_result($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
54        if (!mysqli_stmt_bind_result($stmt, $id, $bind_res)) {
55            printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
56            mysqli_stmt_close($stmt);
57            return false;
58        }
59
60        $result = mysqli_stmt_result_metadata($stmt);
61        $fields = mysqli_fetch_fields($result);
62        if ($fields[1]->type != MYSQLI_TYPE_GEOMETRY) {
63            printf("[%04d] [%d] %s wrong type %d\n", $offset + 10, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $fields[1]->type);
64        }
65
66        $num = 0;
67        $rows = array();
68        while (true === @mysqli_stmt_fetch($stmt)) {
69            $rows[] = array('id' => $id, 'label' => $bind_res);
70            $num++;
71        }
72
73        if ($num != 3) {
74            printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
75                $offset + 17, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
76            return false;
77        }
78        mysqli_stmt_close($stmt);
79
80        foreach ($rows as $row) {
81            if (!$stmt = mysqli_stmt_init($link)) {
82                printf("[%04d] [%d] %s\n", $offset + 10, mysqli_errno($link), mysqli_error($link));
83                return false;
84            }
85
86            if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)")) {
87                printf("[%04d] [%d] %s\n", $offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
88                return false;
89            }
90
91            $new_id = $row['id'] + 10;
92            if (!mysqli_stmt_bind_param($stmt, "is", $new_id, $row['label'])) {
93                printf("[%04d] [%d] %s\n", $offset + 12, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
94                return false;
95            }
96
97            if (!mysqli_stmt_execute($stmt)) {
98                printf("[%04d] [%d] %s\n", $offset + 13, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
99                return false;
100            }
101            mysqli_stmt_close($stmt);
102
103            if (!$res_normal = mysqli_query($link, sprintf("SELECT id, label FROM test WHERE id = %d",
104                    $new_id))) {
105                printf("[%04d] [%d] %s\n", $offset + 14, mysqli_errno($link), mysqli_error($link));
106                return false;
107            }
108
109            if (!$row_normal = mysqli_fetch_assoc($res_normal)) {
110                printf("[%04d] [%d] %s\n", $offset + 15, mysqli_errno($link), mysqli_error($link));
111                return false;
112            }
113
114            if ($row_normal['label'] != $row['label']) {
115                printf("[%04d] PS and non-PS return different data.\n", $offset + 16);
116                return false;
117            }
118            mysqli_free_result($res_normal);
119        }
120
121        return true;
122    }
123
124    $geomFromText = $link->server_version >= 80000 ? "ST_GeomFromText" : "GeomFromText";
125    func_mysqli_stmt_fetch_geom($link, $engine, "GEOMETRY", "$geomFromText('POINT(2 2)')", 20);
126    func_mysqli_stmt_fetch_geom($link, $engine, "POINT", "$geomFromText('POINT(1 1)')", 40);
127    func_mysqli_stmt_fetch_geom($link, $engine, "LINESTRING", "$geomFromText('LINESTRING(0 0,1 1,2 2)')", 60);
128    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);
129    func_mysqli_stmt_fetch_geom($link, $engine, "MULTIPOINT", "$geomFromText('MULTIPOINT(1 1, 2 2)')", 100);
130    func_mysqli_stmt_fetch_geom($link, $engine, "MULTILINESTRING", "$geomFromText('MULTILINESTRING((0 0,1 1,2 2),(0 0,1 1,3 3))')", 120);
131    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);
132    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);
133
134    mysqli_close($link);
135    print "done!";
136?>
137--CLEAN--
138<?php
139    require_once("clean_table.inc");
140?>
141--EXPECT--
142done!
143