Skip to content

Commit 48202a7

Browse files
committed
MySQLnd: Support cursors in store/get result
1 parent 7b8a985 commit 48202a7

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; */
@@ -127,19 +157,8 @@ MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s)
127157
DBG_RETURN(NULL);
128158
}
129159

130-
if (stmt->cursor_exists) {
131-
/* Prepared statement cursors are not supported as of yet */
132-
char * msg;
133-
mnd_sprintf(&msg, 0, "%s() cannot be used with cursors", get_active_function_name());
134-
SET_CLIENT_ERROR(stmt->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, msg);
135-
if (msg) {
136-
mnd_sprintf_free(msg);
137-
}
138-
DBG_RETURN(NULL);
139-
}
140-
141160
/* Nothing to store for UPSERT/LOAD DATA*/
142-
if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
161+
if (!mysqlnd_stmt_check_state(stmt)) {
143162
SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
144163
DBG_RETURN(NULL);
145164
}
@@ -148,6 +167,12 @@ MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s)
148167
SET_EMPTY_ERROR(conn->error_info);
149168
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
150169

170+
if (stmt->cursor_exists) {
171+
if (mysqlnd_stmt_send_cursor_fetch_command(stmt, -1) == FAIL) {
172+
DBG_RETURN(NULL);
173+
}
174+
}
175+
151176
do {
152177
result = conn->m->result_init(stmt->result->field_count);
153178
if (!result) {
@@ -718,11 +743,7 @@ MYSQLND_METHOD(mysqlnd_stmt, use_result)(MYSQLND_STMT * s)
718743
}
719744
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
720745

721-
if (!stmt->field_count ||
722-
(!stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) ||
723-
(stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_READY) ||
724-
(stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE))
725-
{
746+
if (!stmt->field_count || !mysqlnd_stmt_check_state(stmt)) {
726747
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
727748
DBG_ERR("command out of sync");
728749
DBG_RETURN(NULL);
@@ -752,7 +773,6 @@ mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, zval **row_ptr, const unsign
752773
enum_func_status ret;
753774
MYSQLND_STMT_DATA * stmt = result->unbuf->stmt;
754775
MYSQLND_CONN_DATA * conn = stmt->conn;
755-
zend_uchar buf[MYSQLND_STMT_ID_LENGTH /* statement id */ + 4 /* number of rows to fetch */];
756776
MYSQLND_PACKET_ROW * row_packet;
757777

758778
DBG_ENTER("mysqlnd_fetch_stmt_row_cursor");
@@ -776,18 +796,9 @@ mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, zval **row_ptr, const unsign
776796
SET_EMPTY_ERROR(stmt->error_info);
777797
SET_EMPTY_ERROR(conn->error_info);
778798

779-
int4store(buf, stmt->stmt_id);
780-
int4store(buf + MYSQLND_STMT_ID_LENGTH, 1); /* for now fetch only one row */
781-
782-
{
783-
const MYSQLND_CSTRING payload = {(const char*) buf, sizeof(buf)};
784-
785-
ret = conn->command->stmt_fetch(conn, payload);
786-
if (ret == FAIL) {
787-
COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
788-
DBG_RETURN(FAIL);
789-
}
790-
799+
/* for now fetch only one row */
800+
if (mysqlnd_stmt_send_cursor_fetch_command(stmt, 1) == FAIL) {
801+
DBG_RETURN(FAIL);
791802
}
792803

793804
UPSERT_STATUS_RESET(stmt->upsert_status);

0 commit comments

Comments
 (0)