Skip to content

Commit ce9c879

Browse files
committed
MySQLnd: Support cursors in store/get result
1 parent 1c29db1 commit ce9c879

File tree

2 files changed

+64
-56
lines changed

2 files changed

+64
-56
lines changed

ext/mysqli/tests/mysqli_stmt_get_result.phpt

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ if (!function_exists('mysqli_stmt_get_result'))
109109

110110
mysqli_stmt_close($stmt);
111111

112-
// get_result cannot be used in PS cursor mode
112+
// get_result can be used in PS cursor mode
113113
if (!$stmt = mysqli_stmt_init($link))
114114
printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
115115

@@ -122,23 +122,10 @@ if (!function_exists('mysqli_stmt_get_result'))
122122
if (!mysqli_stmt_execute($stmt))
123123
printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
124124

125-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
126-
try {
127-
$res = mysqli_stmt_get_result($stmt);
128-
// we expect no segfault if we try to fetch a row because get_result should throw an error or return false
129-
mysqli_fetch_assoc($res);
130-
} catch (\mysqli_sql_exception $e) {
131-
echo $e->getMessage() . "\n";
132-
}
133-
134-
try {
135-
$res = $stmt->get_result();
136-
// we expect no segfault if we try to fetch a row because get_result should throw an error or return false
137-
$res->fetch_assoc();
138-
} catch (\mysqli_sql_exception $e) {
139-
echo $e->getMessage() . "\n";
125+
$result = mysqli_stmt_get_result($stmt);
126+
while ($row = mysqli_fetch_assoc($result)) {
127+
var_dump($row);
140128
}
141-
mysqli_report(MYSQLI_REPORT_OFF);
142129

143130
if (!$stmt = mysqli_stmt_init($link))
144131
printf("[034] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@@ -196,8 +183,18 @@ if (!function_exists('mysqli_stmt_get_result'))
196183
mysqli_stmt object is not fully initialized
197184
mysqli_stmt object is not fully initialized
198185
mysqli_stmt object is not fully initialized
199-
mysqli_stmt_get_result() cannot be used with cursors
200-
get_result() cannot be used with cursors
186+
array(2) {
187+
["id"]=>
188+
int(1)
189+
["label"]=>
190+
string(1) "a"
191+
}
192+
array(2) {
193+
["id"]=>
194+
int(2)
195+
["label"]=>
196+
string(1) "b"
197+
}
201198
[040] [2014] [Commands out of sync; you can't run this command now]
202199
[041] [0] []
203200
array(2) {

ext/mysqlnd/mysqlnd_ps.c

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ enum_func_status mysqlnd_stmt_execute_batch_generate_request(MYSQLND_STMT * cons
3737

3838
static void mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const stmt);
3939

40+
static enum_func_status mysqlnd_stmt_send_cursor_fetch_command(
41+
const MYSQLND_STMT_DATA *stmt, unsigned max_rows)
42+
{
43+
MYSQLND_CONN_DATA *conn = stmt->conn;
44+
zend_uchar buf[MYSQLND_STMT_ID_LENGTH /* statement id */ + 4 /* number of rows to fetch */];
45+
const MYSQLND_CSTRING payload = {(const char*) buf, sizeof(buf)};
46+
47+
int4store(buf, stmt->stmt_id);
48+
int4store(buf + MYSQLND_STMT_ID_LENGTH, max_rows);
49+
50+
if (conn->command->stmt_fetch(conn, payload) == FAIL) {
51+
COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
52+
return FAIL;
53+
}
54+
return PASS;
55+
}
56+
57+
static zend_bool mysqlnd_stmt_check_state(const MYSQLND_STMT_DATA *stmt)
58+
{
59+
const MYSQLND_CONN_DATA *conn = stmt->conn;
60+
if (stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
61+
return 0;
62+
}
63+
if (stmt->cursor_exists) {
64+
return GET_CONNECTION_STATE(&conn->state) == CONN_READY;
65+
} else {
66+
return GET_CONNECTION_STATE(&conn->state) == CONN_FETCHING_DATA;
67+
}
68+
}
69+
4070
/* {{{ mysqlnd_stmt::store_result */
4171
static MYSQLND_RES *
4272
MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s)
@@ -57,14 +87,8 @@ MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s)
5787
DBG_RETURN(NULL);
5888
}
5989

60-
if (stmt->cursor_exists) {
61-
/* Silently convert buffered to unbuffered, for now */
62-
DBG_RETURN(s->m->use_result(s));
63-
}
64-
6590
/* Nothing to store for UPSERT/LOAD DATA*/
66-
if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE)
67-
{
91+
if (!mysqlnd_stmt_check_state(stmt)) {
6892
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
6993
DBG_RETURN(NULL);
7094
}
@@ -75,6 +99,12 @@ MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s)
7599
SET_EMPTY_ERROR(conn->error_info);
76100
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_PS_BUFFERED_SETS);
77101

102+
if (stmt->cursor_exists) {
103+
if (mysqlnd_stmt_send_cursor_fetch_command(stmt, -1) == FAIL) {
104+
DBG_RETURN(NULL);
105+
}
106+
}
107+
78108
result = stmt->result;
79109
result->type = MYSQLND_RES_PS_BUF;
80110
/* result->m.row_decoder = php_mysqlnd_rowp_read_binary_protocol; */
@@ -149,19 +179,8 @@ MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s)
149179
DBG_RETURN(NULL);
150180
}
151181

152-
if (stmt->cursor_exists) {
153-
/* Prepared statement cursors are not supported as of yet */
154-
char * msg;
155-
mnd_sprintf(&msg, 0, "%s() cannot be used with cursors", get_active_function_name());
156-
SET_CLIENT_ERROR(stmt->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, msg);
157-
if (msg) {
158-
mnd_sprintf_free(msg);
159-
}
160-
DBG_RETURN(NULL);
161-
}
162-
163182
/* Nothing to store for UPSERT/LOAD DATA*/
164-
if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
183+
if (!mysqlnd_stmt_check_state(stmt)) {
165184
SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
166185
DBG_RETURN(NULL);
167186
}
@@ -170,6 +189,12 @@ MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s)
170189
SET_EMPTY_ERROR(conn->error_info);
171190
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
172191

192+
if (stmt->cursor_exists) {
193+
if (mysqlnd_stmt_send_cursor_fetch_command(stmt, -1) == FAIL) {
194+
DBG_RETURN(NULL);
195+
}
196+
}
197+
173198
do {
174199
result = conn->m->result_init(stmt->result->field_count);
175200
if (!result) {
@@ -952,11 +977,7 @@ MYSQLND_METHOD(mysqlnd_stmt, use_result)(MYSQLND_STMT * s)
952977
}
953978
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
954979

955-
if (!stmt->field_count ||
956-
(!stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) ||
957-
(stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_READY) ||
958-
(stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE))
959-
{
980+
if (!stmt->field_count || !mysqlnd_stmt_check_state(stmt)) {
960981
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
961982
DBG_ERR("command out of sync");
962983
DBG_RETURN(NULL);
@@ -986,7 +1007,6 @@ mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, void * param, const unsigned
9861007
MYSQLND_STMT * s = (MYSQLND_STMT *) param;
9871008
MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
9881009
MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
989-
zend_uchar buf[MYSQLND_STMT_ID_LENGTH /* statement id */ + 4 /* number of rows to fetch */];
9901010
MYSQLND_PACKET_ROW * row_packet;
9911011

9921012
DBG_ENTER("mysqlnd_fetch_stmt_row_cursor");
@@ -1010,18 +1030,9 @@ mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, void * param, const unsigned
10101030
SET_EMPTY_ERROR(stmt->error_info);
10111031
SET_EMPTY_ERROR(conn->error_info);
10121032

1013-
int4store(buf, stmt->stmt_id);
1014-
int4store(buf + MYSQLND_STMT_ID_LENGTH, 1); /* for now fetch only one row */
1015-
1016-
{
1017-
const MYSQLND_CSTRING payload = {(const char*) buf, sizeof(buf)};
1018-
1019-
ret = conn->command->stmt_fetch(conn, payload);
1020-
if (ret == FAIL) {
1021-
COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
1022-
DBG_RETURN(FAIL);
1023-
}
1024-
1033+
/* for now fetch only one row */
1034+
if (mysqlnd_stmt_send_cursor_fetch_command(stmt, 1) == FAIL) {
1035+
DBG_RETURN(FAIL);
10251036
}
10261037

10271038
UPSERT_STATUS_RESET(stmt->upsert_status);

0 commit comments

Comments
 (0)