#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_result.h"
#include "mysqlnd_result_meta.h"
#include "mysqlnd_statistics.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_block_alloc.h"
#include "mysqlnd_ext_plugin.h"
#define MYSQLND_SILENT
const char * const mysqlnd_not_bound_as_blob = "Can't send long data for non-string/non-binary data types";
const char * const mysqlnd_stmt_not_prepared = "Statement not prepared";
enum_func_status mysqlnd_stmt_execute_generate_request(MYSQLND_STMT * const s, zend_uchar ** request, size_t *request_len, zend_bool * free_buffer TSRMLS_DC);
static void mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const stmt TSRMLS_DC);
static void mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const stmt, unsigned int param_no TSRMLS_DC);
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret;
MYSQLND_CONN_DATA * conn;
MYSQLND_RES * result;
DBG_ENTER("mysqlnd_stmt::store_result");
if (!stmt || !stmt->conn || !stmt->result) {
DBG_RETURN(NULL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
conn = stmt->conn;
if (!stmt->field_count) {
DBG_RETURN(NULL);
}
if (stmt->cursor_exists) {
DBG_RETURN(s->m->use_result(s TSRMLS_CC));
}
if (CONN_GET_STATE(conn) != CONN_FETCHING_DATA ||
stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE)
{
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_RETURN(NULL);
}
stmt->default_rset_handler = s->m->store_result;
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*conn->error_info);
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_PS_BUFFERED_SETS);
result = stmt->result;
result->type = MYSQLND_RES_PS_BUF;
result->stored_data = (MYSQLND_RES_BUFFERED *) mysqlnd_result_buffered_zval_init(result->field_count, TRUE, result->persistent TSRMLS_CC);
if (!result->stored_data) {
SET_OOM_ERROR(*conn->error_info);
DBG_RETURN(NULL);
}
ret = result->m.store_result_fetch_data(conn, result, result->meta, &result->stored_data->row_buffers, TRUE TSRMLS_CC);
result->stored_data->m.fetch_row = mysqlnd_stmt_fetch_row_buffered;
if (PASS == ret) {
if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
MYSQLND_RES_BUFFERED_ZVAL * set = (MYSQLND_RES_BUFFERED_ZVAL *) result->stored_data;
if (result->stored_data->row_count) {
if (result->stored_data->row_count * result->meta->field_count * sizeof(zval *) > SIZE_MAX) {
SET_OOM_ERROR(*conn->error_info);
DBG_RETURN(NULL);
}
set->data = mnd_emalloc((size_t)(result->stored_data->row_count * result->meta->field_count * sizeof(zval *)));
if (!set->data) {
SET_OOM_ERROR(*conn->error_info);
DBG_RETURN(NULL);
}
memset(set->data, 0, (size_t)(result->stored_data->row_count * result->meta->field_count * sizeof(zval *)));
}
set->data_cursor = set->data;
} else if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
}
stmt->upsert_status->affected_rows = stmt->result->stored_data->row_count;
stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;
} else {
COPY_CLIENT_ERROR(*conn->error_info, result->stored_data->error_info);
stmt->result->m.free_result_contents(stmt->result TSRMLS_CC);
mnd_efree(stmt->result);
stmt->result = NULL;
stmt->state = MYSQLND_STMT_PREPARED;
}
DBG_RETURN(result);
}
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_CONN_DATA * conn;
MYSQLND_RES *result;
DBG_ENTER("mysqlnd_stmt::get_result");
if (!stmt || !stmt->conn || !stmt->result) {
DBG_RETURN(NULL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
conn = stmt->conn;
if (!stmt->field_count) {
DBG_RETURN(NULL);
}
if (stmt->cursor_exists) {
DBG_RETURN(s->m->use_result(s TSRMLS_CC));
}
if (CONN_GET_STATE(conn) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_RETURN(NULL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*conn->error_info);
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
do {
result = conn->m->result_init(stmt->result->field_count, stmt->persistent TSRMLS_CC);
if (!result) {
SET_OOM_ERROR(*conn->error_info);
break;
}
result->meta = stmt->result->meta->m->clone_metadata(stmt->result->meta, FALSE TSRMLS_CC);
if (!result->meta) {
SET_OOM_ERROR(*conn->error_info);
break;
}
if ((result = result->m.store_result(result, conn, MYSQLND_STORE_PS | MYSQLND_STORE_NO_COPY TSRMLS_CC))) {
stmt->upsert_status->affected_rows = result->stored_data->row_count;
stmt->state = MYSQLND_STMT_PREPARED;
result->type = MYSQLND_RES_PS_BUF;
} else {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
stmt->state = MYSQLND_STMT_PREPARED;
break;
}
DBG_RETURN(result);
} while (0);
if (result) {
result->m.free_result(result, TRUE TSRMLS_CC);
}
DBG_RETURN(NULL);
}
static zend_bool
MYSQLND_METHOD(mysqlnd_stmt, more_results)(const MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::more_results");
DBG_RETURN((stmt && stmt->conn && (stmt->conn->m->get_server_status(stmt->conn TSRMLS_CC) & SERVER_MORE_RESULTS_EXISTS))?
TRUE:
FALSE);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, next_result)(MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_CONN_DATA * conn;
DBG_ENTER("mysqlnd_stmt::next_result");
if (!stmt || !stmt->conn || !stmt->result) {
DBG_RETURN(FAIL);
}
conn = stmt->conn;
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
if (CONN_GET_STATE(conn) != CONN_NEXT_RESULT_PENDING || !(conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS)) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status, stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);
s->m->free_stmt_result(s TSRMLS_CC);
{
enum_func_status ret = s->m->parse_execute_response(s TSRMLS_CC);
DBG_RETURN(ret);
}
}
static enum_func_status
mysqlnd_stmt_skip_metadata(MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
unsigned int i = 0;
enum_func_status ret = FAIL;
MYSQLND_PACKET_RES_FIELD * field_packet;
DBG_ENTER("mysqlnd_stmt_skip_metadata");
if (!stmt || !stmt->conn || !stmt->conn->protocol) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
field_packet = stmt->conn->protocol->m.get_result_field_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
if (!field_packet) {
SET_OOM_ERROR(*stmt->error_info);
SET_OOM_ERROR(*stmt->conn->error_info);
} else {
ret = PASS;
field_packet->skip_parsing = TRUE;
for (;i < stmt->param_count; i++) {
if (FAIL == PACKET_READ(field_packet, stmt->conn)) {
ret = FAIL;
break;
}
}
PACKET_FREE(field_packet);
}
DBG_RETURN(ret);
}
static enum_func_status
mysqlnd_stmt_read_prepare_response(MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_PACKET_PREPARE_RESPONSE * prepare_resp;
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_stmt_read_prepare_response");
if (!stmt || !stmt->conn || !stmt->conn->protocol) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
prepare_resp = stmt->conn->protocol->m.get_prepare_response_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
if (!prepare_resp) {
SET_OOM_ERROR(*stmt->error_info);
SET_OOM_ERROR(*stmt->conn->error_info);
goto done;
}
if (FAIL == PACKET_READ(prepare_resp, stmt->conn)) {
goto done;
}
if (0xFF == prepare_resp->error_code) {
COPY_CLIENT_ERROR(*stmt->error_info, prepare_resp->error_info);
COPY_CLIENT_ERROR(*stmt->conn->error_info, prepare_resp->error_info);
goto done;
}
ret = PASS;
stmt->stmt_id = prepare_resp->stmt_id;
stmt->warning_count = stmt->conn->upsert_status->warning_count = prepare_resp->warning_count;
stmt->field_count = stmt->conn->field_count = prepare_resp->field_count;
stmt->param_count = prepare_resp->param_count;
stmt->upsert_status->affected_rows = 0;
done:
PACKET_FREE(prepare_resp);
DBG_RETURN(ret);
}
static enum_func_status
mysqlnd_stmt_prepare_read_eof(MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_PACKET_EOF * fields_eof;
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_stmt_prepare_read_eof");
if (!stmt || !stmt->conn || !stmt->conn->protocol) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
fields_eof = stmt->conn->protocol->m.get_eof_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
if (!fields_eof) {
SET_OOM_ERROR(*stmt->error_info);
SET_OOM_ERROR(*stmt->conn->error_info);
} else {
if (FAIL == (ret = PACKET_READ(fields_eof, stmt->conn))) {
if (stmt->result) {
stmt->result->m.free_result_contents(stmt->result TSRMLS_CC);
mnd_efree(stmt->result);
memset(stmt, 0, sizeof(MYSQLND_STMT_DATA));
stmt->state = MYSQLND_STMT_INITTED;
}
} else {
stmt->upsert_status->server_status = fields_eof->server_status;
stmt->upsert_status->warning_count = fields_eof->warning_count;
stmt->state = MYSQLND_STMT_PREPARED;
}
PACKET_FREE(fields_eof);
}
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, prepare)(MYSQLND_STMT * const s, const char * const query, unsigned int query_len TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_STMT * s_to_prepare = s;
MYSQLND_STMT_DATA * stmt_to_prepare = stmt;
DBG_ENTER("mysqlnd_stmt::prepare");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
DBG_INF_FMT("query=%s", query);
SET_ERROR_AFF_ROWS(stmt);
SET_ERROR_AFF_ROWS(stmt->conn);
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->state > MYSQLND_STMT_INITTED) {
if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
stmt->default_rset_handler = s->m->use_result;
stmt->default_rset_handler(s TSRMLS_CC);
}
if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE && stmt->result) {
stmt->result->m.skip_result(stmt->result TSRMLS_CC);
}
s_to_prepare = stmt->conn->m->stmt_init(stmt->conn TSRMLS_CC);
if (!s_to_prepare) {
goto fail;
}
stmt_to_prepare = s_to_prepare->data;
}
if (FAIL == stmt_to_prepare->conn->m->simple_command(stmt_to_prepare->conn, COM_STMT_PREPARE, (const zend_uchar *) query, query_len, PROT_LAST, FALSE, TRUE TSRMLS_CC) ||
FAIL == mysqlnd_stmt_read_prepare_response(s_to_prepare TSRMLS_CC))
{
goto fail;
}
if (stmt_to_prepare->param_count) {
if (FAIL == mysqlnd_stmt_skip_metadata(s_to_prepare TSRMLS_CC) ||
FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare TSRMLS_CC))
{
goto fail;
}
}
if (stmt_to_prepare->field_count) {
MYSQLND_RES * result = stmt->conn->m->result_init(stmt_to_prepare->field_count, stmt_to_prepare->persistent TSRMLS_CC);
if (!result) {
SET_OOM_ERROR(*stmt->conn->error_info);
goto fail;
}
stmt_to_prepare->result = result;
result->conn = stmt_to_prepare->conn->m->get_reference(stmt_to_prepare->conn TSRMLS_CC);
result->type = MYSQLND_RES_PS_BUF;
if (FAIL == result->m.read_result_metadata(result, stmt_to_prepare->conn TSRMLS_CC) ||
FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare TSRMLS_CC))
{
goto fail;
}
}
if (stmt_to_prepare != stmt) {
size_t real_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
char * tmp_swap = mnd_malloc(real_size);
memcpy(tmp_swap, s, real_size);
memcpy(s, s_to_prepare, real_size);
memcpy(s_to_prepare, tmp_swap, real_size);
mnd_free(tmp_swap);
{
MYSQLND_STMT_DATA * tmp_swap_data = stmt_to_prepare;
stmt_to_prepare = stmt;
stmt = tmp_swap_data;
}
s_to_prepare->m->dtor(s_to_prepare, TRUE TSRMLS_CC);
}
stmt->state = MYSQLND_STMT_PREPARED;
DBG_INF("PASS");
DBG_RETURN(PASS);
fail:
if (stmt_to_prepare != stmt && s_to_prepare) {
s_to_prepare->m->dtor(s_to_prepare, TRUE TSRMLS_CC);
}
stmt->state = MYSQLND_STMT_INITTED;
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
static enum_func_status
mysqlnd_stmt_execute_parse_response(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret;
MYSQLND_CONN_DATA * conn;
DBG_ENTER("mysqlnd_stmt_execute_parse_response");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
conn = stmt->conn;
CONN_SET_STATE(conn, CONN_QUERY_SENT);
ret = mysqlnd_query_read_result_set_header(stmt->conn, s TSRMLS_CC);
if (ret == FAIL) {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
memset(stmt->upsert_status, 0, sizeof(*stmt->upsert_status));
stmt->upsert_status->affected_rows = conn->upsert_status->affected_rows;
if (CONN_GET_STATE(conn) == CONN_QUIT_SENT) {
}
stmt->state = MYSQLND_STMT_PREPARED;
stmt->send_types_to_server = 1;
} else {
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
*stmt->upsert_status = *conn->upsert_status;
stmt->state = MYSQLND_STMT_EXECUTED;
if (conn->last_query_type == QUERY_UPSERT || conn->last_query_type == QUERY_LOAD_LOCAL) {
DBG_INF("PASS");
DBG_RETURN(PASS);
}
stmt->result->type = MYSQLND_RES_PS_BUF;
if (!stmt->result->conn) {
stmt->result->conn = stmt->conn->m->get_reference(stmt->conn TSRMLS_CC);
}
stmt->field_count = stmt->result->field_count = conn->field_count;
if (stmt->result->stored_data) {
stmt->result->stored_data->lengths = NULL;
} else if (stmt->result->unbuf) {
stmt->result->unbuf->lengths = NULL;
}
if (stmt->field_count) {
stmt->state = MYSQLND_STMT_WAITING_USE_OR_STORE;
DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status,
stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);
if (stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS) {
DBG_INF("cursor exists");
stmt->cursor_exists = TRUE;
CONN_SET_STATE(conn, CONN_READY);
stmt->default_rset_handler = s->m->use_result;
DBG_INF("use_result");
} else if (stmt->flags & CURSOR_TYPE_READ_ONLY) {
DBG_INF("asked for cursor but got none");
stmt->default_rset_handler = s->m->store_result;
DBG_INF("store_result");
} else {
DBG_INF("no cursor");
stmt->default_rset_handler = s->m->use_result;
DBG_INF("use_result");
}
}
}
#ifndef MYSQLND_DONT_SKIP_OUT_PARAMS_RESULTSET
if (stmt->upsert_status->server_status & SERVER_PS_OUT_PARAMS) {
s->m->free_stmt_content(s TSRMLS_CC);
DBG_INF("PS OUT Variable RSet, skipping");
ret = mysqlnd_stmt_execute_parse_response(s TSRMLS_CC);
}
#endif
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, execute)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret;
MYSQLND_CONN_DATA * conn;
zend_uchar *request = NULL;
size_t request_len;
zend_bool free_request;
DBG_ENTER("mysqlnd_stmt::execute");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
conn = stmt->conn;
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
SET_ERROR_AFF_ROWS(stmt);
SET_ERROR_AFF_ROWS(stmt->conn);
if (stmt->result && stmt->state >= MYSQLND_STMT_PREPARED && stmt->field_count) {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
if (stmt->result_bind &&
stmt->result_zvals_separated_once == TRUE &&
stmt->state >= MYSQLND_STMT_USER_FETCHING)
{
unsigned int i;
for (i = 0; i < stmt->field_count; i++) {
if (stmt->result_bind[i].bound == TRUE) {
zval_copy_ctor(stmt->result_bind[i].zv);
}
}
}
#endif
s->m->flush(s TSRMLS_CC);
stmt->result->m.free_result_buffers(stmt->result TSRMLS_CC);
stmt->state = MYSQLND_STMT_PREPARED;
} else if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
if (stmt->param_count) {
unsigned int i, not_bound = 0;
if (!stmt->param_bind) {
SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE,
"No data supplied for parameters in prepared statement");
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
for (i = 0; i < stmt->param_count; i++) {
if (stmt->param_bind[i].zv == NULL) {
not_bound++;
}
}
if (not_bound) {
char * msg;
mnd_sprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
not_bound, not_bound>1 ?"s":"");
SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, msg);
if (msg) {
mnd_sprintf_free(msg);
}
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
}
ret = s->m->generate_execute_request(s, &request, &request_len, &free_request TSRMLS_CC);
if (ret == PASS) {
ret = stmt->conn->m->simple_command(stmt->conn, COM_STMT_EXECUTE, request, request_len,
PROT_LAST ,
FALSE, FALSE TSRMLS_CC);
} else {
SET_STMT_ERROR(stmt, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Couldn't generate the request. Possibly OOM.");
}
if (free_request) {
mnd_efree(request);
}
if (ret == FAIL) {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
stmt->execute_count++;
ret = s->m->parse_execute_response(s TSRMLS_CC);
DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status, stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);
if (ret == PASS && conn->last_query_type == QUERY_UPSERT && stmt->upsert_status->affected_rows) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_PS, stmt->upsert_status->affected_rows);
}
DBG_RETURN(ret);
}
enum_func_status
mysqlnd_stmt_fetch_row_buffered(MYSQLND_RES * result, void * param, unsigned int flags, zend_bool * fetched_anything TSRMLS_DC)
{
MYSQLND_STMT * s = (MYSQLND_STMT *) param;
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
const MYSQLND_RES_METADATA * const meta = result->meta;
unsigned int field_count = meta->field_count;
DBG_ENTER("mysqlnd_stmt_fetch_row_buffered");
*fetched_anything = FALSE;
DBG_INF_FMT("stmt=%lu", stmt != NULL ? stmt->stmt_id : 0L);
if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
MYSQLND_RES_BUFFERED_ZVAL * set = (MYSQLND_RES_BUFFERED_ZVAL *) result->stored_data;
if (set->data_cursor &&
(set->data_cursor - set->data) < (result->stored_data->row_count * field_count))
{
if (stmt->result_bind) {
unsigned int i;
zval **current_row = set->data_cursor;
if (NULL == current_row[0]) {
uint64_t row_num = (set->data_cursor - set->data) / field_count;
enum_func_status rc = result->stored_data->m.row_decoder(result->stored_data->row_buffers[row_num],
current_row,
meta->field_count,
meta->fields,
result->conn->options->int_and_float_native,
result->conn->stats TSRMLS_CC);
if (PASS != rc) {
DBG_RETURN(FAIL);
}
result->stored_data->initialized_rows++;
if (stmt->update_max_length) {
for (i = 0; i < result->field_count; i++) {
if (Z_TYPE_P(current_row[i]) >= IS_STRING) {
unsigned long len = Z_STRLEN_P(current_row[i]);
if (meta->fields[i].max_length < len) {
meta->fields[i].max_length = len;
}
}
}
}
}
for (i = 0; i < result->field_count; i++) {
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
if (stmt->result_bind[i].zv) {
zval_dtor(stmt->result_bind[i].zv);
}
#endif
if (stmt->result_bind[i].bound == TRUE) {
DBG_INF_FMT("i=%u type=%u", i, Z_TYPE_P(current_row[i]));
if (Z_TYPE_P(current_row[i]) != IS_NULL) {
Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(current_row[i]);
stmt->result_bind[i].zv->value = current_row[i]->value;
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
zval_copy_ctor(stmt->result_bind[i].zv);
#endif
} else {
ZVAL_NULL(stmt->result_bind[i].zv);
}
}
}
}
set->data_cursor += field_count;
*fetched_anything = TRUE;
MYSQLND_INC_GLOBAL_STATISTIC(STAT_ROWS_FETCHED_FROM_CLIENT_PS_BUF);
DBG_INF("row fetched");
} else {
set->data_cursor = NULL;
DBG_INF("no more data");
}
} else if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_C) {
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
enum_func_status
mysqlnd_stmt_fetch_row_unbuffered(MYSQLND_RES * result, void * param, unsigned int flags, zend_bool * fetched_anything TSRMLS_DC)
{
enum_func_status ret;
MYSQLND_STMT * s = (MYSQLND_STMT *) param;
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_PACKET_ROW * row_packet;
const MYSQLND_RES_METADATA * const meta = result->meta;
DBG_ENTER("mysqlnd_stmt_fetch_row_unbuffered");
*fetched_anything = FALSE;
if (result->unbuf->eof_reached) {
DBG_INF("EOF already reached");
DBG_RETURN(PASS);
}
if (CONN_GET_STATE(result->conn) != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(*result->conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("command out of sync");
DBG_RETURN(FAIL);
}
if (!(row_packet = result->unbuf->row_packet)) {
DBG_RETURN(FAIL);
}
row_packet->skip_extraction = stmt && stmt->result_bind? FALSE:TRUE;
if (PASS == (ret = PACKET_READ(row_packet, result->conn)) && !row_packet->eof) {
unsigned int i, field_count = result->field_count;
if (!row_packet->skip_extraction) {
result->unbuf->m.free_last_data(result->unbuf, result->conn? result->conn->stats : NULL TSRMLS_CC);
result->unbuf->last_row_data = row_packet->fields;
result->unbuf->last_row_buffer = row_packet->row_buffer;
row_packet->fields = NULL;
row_packet->row_buffer = NULL;
if (PASS != result->unbuf->m.row_decoder(result->unbuf->last_row_buffer,
result->unbuf->last_row_data,
row_packet->field_count,
row_packet->fields_metadata,
result->conn->options->int_and_float_native,
result->conn->stats TSRMLS_CC))
{
DBG_RETURN(FAIL);
}
for (i = 0; i < field_count; i++) {
if (stmt->result_bind[i].bound == TRUE) {
zval *data = result->unbuf->last_row_data[i];
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
zval_dtor(stmt->result_bind[i].zv);
#endif
if (IS_NULL != (Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(data)) ) {
if ((Z_TYPE_P(data) == IS_STRING) && (meta->fields[i].max_length < (unsigned long) Z_STRLEN_P(data))) {
meta->fields[i].max_length = Z_STRLEN_P(data);
}
stmt->result_bind[i].zv->value = data->value;
ZVAL_NULL(data);
}
}
}
MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_UNBUF);
} else {
DBG_INF("skipping extraction");
row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
row_packet->row_buffer = NULL;
}
result->unbuf->row_count++;
*fetched_anything = TRUE;
} else if (ret == FAIL) {
if (row_packet->error_info.error_no) {
COPY_CLIENT_ERROR(*stmt->conn->error_info, row_packet->error_info);
COPY_CLIENT_ERROR(*stmt->error_info, row_packet->error_info);
}
CONN_SET_STATE(result->conn, CONN_READY);
result->unbuf->eof_reached = TRUE;
} else if (row_packet->eof) {
DBG_INF("EOF");
result->unbuf->eof_reached = TRUE;
memset(result->conn->upsert_status, 0, sizeof(*result->conn->upsert_status));
result->conn->upsert_status->warning_count = row_packet->warning_count;
result->conn->upsert_status->server_status = row_packet->server_status;
if (result->conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS) {
CONN_SET_STATE(result->conn, CONN_NEXT_RESULT_PENDING);
} else {
CONN_SET_STATE(result->conn, CONN_READY);
}
}
DBG_INF_FMT("ret=%s fetched_anything=%u", ret == PASS? "PASS":"FAIL", *fetched_anything);
DBG_RETURN(ret);
}
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, use_result)(MYSQLND_STMT * s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_RES * result;
MYSQLND_CONN_DATA * conn;
DBG_ENTER("mysqlnd_stmt::use_result");
if (!stmt || !stmt->conn || !stmt->result) {
DBG_RETURN(NULL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
conn = stmt->conn;
if (!stmt->field_count ||
(!stmt->cursor_exists && CONN_GET_STATE(conn) != CONN_FETCHING_DATA) ||
(stmt->cursor_exists && CONN_GET_STATE(conn) != CONN_READY) ||
(stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE))
{
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("command out of sync");
DBG_RETURN(NULL);
}
SET_EMPTY_ERROR(*stmt->error_info);
MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_PS_UNBUFFERED_SETS);
result = stmt->result;
result->m.use_result(stmt->result, TRUE TSRMLS_CC);
result->unbuf->m.fetch_row = stmt->cursor_exists? mysqlnd_fetch_stmt_row_cursor:
mysqlnd_stmt_fetch_row_unbuffered;
stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;
DBG_INF_FMT("%p", result);
DBG_RETURN(result);
}
#define STMT_ID_LENGTH 4
enum_func_status
mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, void * param, unsigned int flags, zend_bool * fetched_anything TSRMLS_DC)
{
enum_func_status ret;
MYSQLND_STMT * s = (MYSQLND_STMT *) param;
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
zend_uchar buf[STMT_ID_LENGTH + 4 ];
MYSQLND_PACKET_ROW * row_packet;
DBG_ENTER("mysqlnd_fetch_stmt_row_cursor");
if (!stmt || !stmt->conn || !result || !result->conn || !result->unbuf) {
DBG_ERR("no statement");
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu flags=%u", stmt->stmt_id, flags);
if (stmt->state < MYSQLND_STMT_USER_FETCHING) {
SET_CLIENT_ERROR(*stmt->conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
DBG_ERR("command out of sync");
DBG_RETURN(FAIL);
}
if (!(row_packet = result->unbuf->row_packet)) {
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
int4store(buf, stmt->stmt_id);
int4store(buf + STMT_ID_LENGTH, 1);
if (FAIL == stmt->conn->m->simple_command(stmt->conn, COM_STMT_FETCH, buf, sizeof(buf),
PROT_LAST ,
FALSE, TRUE TSRMLS_CC)) {
COPY_CLIENT_ERROR(*stmt->error_info, *stmt->conn->error_info);
DBG_RETURN(FAIL);
}
row_packet->skip_extraction = stmt->result_bind? FALSE:TRUE;
memset(stmt->upsert_status, 0, sizeof(*stmt->upsert_status));
if (PASS == (ret = PACKET_READ(row_packet, result->conn)) && !row_packet->eof) {
const MYSQLND_RES_METADATA * const meta = result->meta;
unsigned int i, field_count = result->field_count;
if (!row_packet->skip_extraction) {
result->unbuf->m.free_last_data(result->unbuf, result->conn? result->conn->stats : NULL TSRMLS_CC);
result->unbuf->last_row_data = row_packet->fields;
result->unbuf->last_row_buffer = row_packet->row_buffer;
row_packet->fields = NULL;
row_packet->row_buffer = NULL;
if (PASS != result->unbuf->m.row_decoder(result->unbuf->last_row_buffer,
result->unbuf->last_row_data,
row_packet->field_count,
row_packet->fields_metadata,
result->conn->options->int_and_float_native,
result->conn->stats TSRMLS_CC))
{
DBG_RETURN(FAIL);
}
for (i = 0; i < field_count; i++) {
if (stmt->result_bind[i].bound == TRUE) {
zval *data = result->unbuf->last_row_data[i];
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
zval_dtor(stmt->result_bind[i].zv);
#endif
DBG_INF_FMT("i=%u bound_var=%p type=%u refc=%u", i, stmt->result_bind[i].zv,
Z_TYPE_P(data), Z_REFCOUNT_P(stmt->result_bind[i].zv));
if (IS_NULL != (Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(data))) {
if ((Z_TYPE_P(data) == IS_STRING) && (meta->fields[i].max_length < (unsigned long) Z_STRLEN_P(data))) {
meta->fields[i].max_length = Z_STRLEN_P(data);
}
stmt->result_bind[i].zv->value = data->value;
ZVAL_NULL(data);
}
}
}
} else {
DBG_INF("skipping extraction");
row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
row_packet->row_buffer = NULL;
}
ret = PACKET_READ(row_packet, result->conn);
if (row_packet->row_buffer) {
row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
row_packet->row_buffer = NULL;
}
MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_CURSOR);
result->unbuf->row_count++;
*fetched_anything = TRUE;
} else {
*fetched_anything = FALSE;
stmt->upsert_status->warning_count =
stmt->conn->upsert_status->warning_count =
row_packet->warning_count;
stmt->upsert_status->server_status =
stmt->conn->upsert_status->server_status =
row_packet->server_status;
result->unbuf->eof_reached = row_packet->eof;
}
stmt->upsert_status->warning_count =
stmt->conn->upsert_status->warning_count =
row_packet->warning_count;
stmt->upsert_status->server_status =
stmt->conn->upsert_status->server_status =
row_packet->server_status;
DBG_INF_FMT("ret=%s fetched=%u server_status=%u warnings=%u eof=%u",
ret == PASS? "PASS":"FAIL", *fetched_anything,
row_packet->server_status, row_packet->warning_count,
result->unbuf->eof_reached);
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, fetch)(MYSQLND_STMT * const s, zend_bool * const fetched_anything TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret;
DBG_ENTER("mysqlnd_stmt::fetch");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
if (!stmt->result ||
stmt->state < MYSQLND_STMT_WAITING_USE_OR_STORE) {
SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("command out of sync");
DBG_RETURN(FAIL);
} else if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
stmt->default_rset_handler(s TSRMLS_CC);
}
stmt->state = MYSQLND_STMT_USER_FETCHING;
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
DBG_INF_FMT("result_bind=%p separated_once=%u", stmt->result_bind, stmt->result_zvals_separated_once);
if (stmt->result_bind && !stmt->result_zvals_separated_once) {
unsigned int i;
for (i = 0; i < stmt->result->field_count; i++) {
if (stmt->result_bind[i].bound == TRUE) {
zval_dtor(stmt->result_bind[i].zv);
ZVAL_NULL(stmt->result_bind[i].zv);
}
}
stmt->result_zvals_separated_once = TRUE;
}
ret = stmt->result->m.fetch_row(stmt->result, (void*)s, 0, fetched_anything TSRMLS_CC);
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, reset)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret = PASS;
zend_uchar cmd_buf[STMT_ID_LENGTH ];
DBG_ENTER("mysqlnd_stmt::reset");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->stmt_id) {
MYSQLND_CONN_DATA * conn = stmt->conn;
if (stmt->param_bind) {
unsigned int i;
DBG_INF("resetting long data");
for (i = 0; i < stmt->param_count; i++) {
if (stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED) {
stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
}
}
}
s->m->flush(s TSRMLS_CC);
int4store(cmd_buf, stmt->stmt_id);
if (CONN_GET_STATE(conn) == CONN_READY &&
FAIL == (ret = conn->m->simple_command(conn, COM_STMT_RESET, cmd_buf,
sizeof(cmd_buf), PROT_OK_PACKET,
FALSE, TRUE TSRMLS_CC))) {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
}
*stmt->upsert_status = *conn->upsert_status;
}
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, flush)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_stmt::flush");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
if (stmt->stmt_id) {
do {
if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
DBG_INF("fetching result set header");
stmt->default_rset_handler(s TSRMLS_CC);
stmt->state = MYSQLND_STMT_USER_FETCHING;
}
if (stmt->result) {
DBG_INF("skipping result");
stmt->result->m.skip_result(stmt->result TSRMLS_CC);
}
} while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
}
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, send_long_data)(MYSQLND_STMT * const s, unsigned int param_no,
const char * const data, unsigned long length TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
enum_func_status ret = FAIL;
MYSQLND_CONN_DATA * conn;
zend_uchar * cmd_buf;
enum php_mysqlnd_server_command cmd = COM_STMT_SEND_LONG_DATA;
DBG_ENTER("mysqlnd_stmt::send_long_data");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu param_no=%u data_len=%lu", stmt->stmt_id, param_no, length);
conn = stmt->conn;
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
DBG_ERR("not prepared");
DBG_RETURN(FAIL);
}
if (!stmt->param_bind) {
SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("command out of sync");
DBG_RETURN(FAIL);
}
if (param_no >= stmt->param_count) {
SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
DBG_ERR("invalid param_no");
DBG_RETURN(FAIL);
}
if (stmt->param_bind[param_no].type != MYSQL_TYPE_LONG_BLOB) {
SET_STMT_ERROR(stmt, CR_INVALID_BUFFER_USE, UNKNOWN_SQLSTATE, mysqlnd_not_bound_as_blob);
DBG_ERR("param_no is not of a blob type");
DBG_RETURN(FAIL);
}
if (CONN_GET_STATE(conn) == CONN_READY) {
size_t packet_len;
cmd_buf = mnd_emalloc(packet_len = STMT_ID_LENGTH + 2 + length);
if (cmd_buf) {
stmt->param_bind[param_no].flags |= MYSQLND_PARAM_BIND_BLOB_USED;
int4store(cmd_buf, stmt->stmt_id);
int2store(cmd_buf + STMT_ID_LENGTH, param_no);
memcpy(cmd_buf + STMT_ID_LENGTH + 2, data, length);
ret = conn->m->simple_command(conn, cmd, cmd_buf, packet_len, PROT_LAST , FALSE, TRUE TSRMLS_CC);
mnd_efree(cmd_buf);
if (FAIL == ret) {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
}
} else {
ret = FAIL;
SET_OOM_ERROR(*stmt->error_info);
SET_OOM_ERROR(*conn->error_info);
}
#ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
#if HAVE_USLEEP && !defined(PHP_WIN32)
usleep(120000);
#endif
if ((packet_len = conn->net->m.consume_uneaten_data(conn->net, cmd TSRMLS_CC))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "There was an error "
"while sending long data. Probably max_allowed_packet_size "
"is smaller than the data. You have to increase it or send "
"smaller chunks of data. Answer was "MYSQLND_SZ_T_SPEC" bytes long.", packet_len);
SET_STMT_ERROR(stmt, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE,
"Server responded to COM_STMT_SEND_LONG_DATA.");
ret = FAIL;
}
#endif
}
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_parameters)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * const param_bind TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::bind_param");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
DBG_ERR("not prepared");
if (param_bind) {
s->m->free_parameter_bind(s, param_bind TSRMLS_CC);
}
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->param_count) {
unsigned int i = 0;
if (!param_bind) {
SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, "Re-binding (still) not supported");
DBG_ERR("Re-binding (still) not supported");
DBG_RETURN(FAIL);
} else if (stmt->param_bind) {
DBG_INF("Binding");
for (i = 0; i < stmt->param_count; i++) {
if (stmt->param_bind[i].zv) {
zval_ptr_dtor(&stmt->param_bind[i].zv);
}
}
if (stmt->param_bind != param_bind) {
s->m->free_parameter_bind(s, stmt->param_bind TSRMLS_CC);
}
}
stmt->param_bind = param_bind;
for (i = 0; i < stmt->param_count; i++) {
DBG_INF_FMT("%u is of type %u", i, stmt->param_bind[i].type);
Z_ADDREF_P(stmt->param_bind[i].zv);
stmt->param_bind[i].flags = 0;
if (stmt->param_bind[i].type == MYSQL_TYPE_LONG_BLOB) {
stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
}
}
stmt->send_types_to_server = 1;
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter)(MYSQLND_STMT * const s, unsigned int param_no,
zval * const zv, zend_uchar type TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::bind_one_parameter");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu param_no=%u param_count=%u type=%u", stmt->stmt_id, param_no, stmt->param_count, type);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
DBG_ERR("not prepared");
DBG_RETURN(FAIL);
}
if (param_no >= stmt->param_count) {
SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
DBG_ERR("invalid param_no");
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->param_count) {
if (!stmt->param_bind) {
stmt->param_bind = mnd_pecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND), stmt->persistent);
if (!stmt->param_bind) {
DBG_RETURN(FAIL);
}
}
Z_ADDREF_P(zv);
DBG_INF("Binding");
if (stmt->param_bind[param_no].zv) {
zval_ptr_dtor(&stmt->param_bind[param_no].zv);
}
if (type == MYSQL_TYPE_LONG_BLOB) {
stmt->param_bind[param_no].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
}
stmt->param_bind[param_no].zv = zv;
stmt->param_bind[param_no].type = type;
stmt->send_types_to_server = 1;
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::refresh_bind_param");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
DBG_ERR("not prepared");
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->param_count) {
stmt->send_types_to_server = 1;
}
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_result)(MYSQLND_STMT * const s,
MYSQLND_RESULT_BIND * const result_bind TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::bind_result");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
if (result_bind) {
s->m->free_result_bind(s, result_bind TSRMLS_CC);
}
DBG_ERR("not prepared");
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->field_count) {
unsigned int i = 0;
if (!result_bind) {
DBG_ERR("no result bind passed");
DBG_RETURN(FAIL);
}
mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);
stmt->result_zvals_separated_once = FALSE;
stmt->result_bind = result_bind;
for (i = 0; i < stmt->field_count; i++) {
Z_ADDREF_P(stmt->result_bind[i].zv);
DBG_INF_FMT("ref of %p = %u", stmt->result_bind[i].zv, Z_REFCOUNT_P(stmt->result_bind[i].zv));
stmt->result_bind[i].bound = TRUE;
}
} else if (result_bind) {
s->m->free_result_bind(s, result_bind TSRMLS_CC);
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_one_result)(MYSQLND_STMT * const s, unsigned int param_no TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::bind_result");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);
if (stmt->state < MYSQLND_STMT_PREPARED) {
SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
DBG_ERR("not prepared");
DBG_RETURN(FAIL);
}
if (param_no >= stmt->field_count) {
SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
DBG_ERR("invalid param_no");
DBG_RETURN(FAIL);
}
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
if (stmt->field_count) {
mysqlnd_stmt_separate_one_result_bind(s, param_no TSRMLS_CC);
if (!stmt->result_bind) {
stmt->result_bind = mnd_pecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND), stmt->persistent);
} else {
stmt->result_bind = mnd_perealloc(stmt->result_bind, stmt->field_count * sizeof(MYSQLND_RESULT_BIND), stmt->persistent);
}
if (!stmt->result_bind) {
DBG_RETURN(FAIL);
}
ALLOC_INIT_ZVAL(stmt->result_bind[param_no].zv);
stmt->result_bind[param_no].bound = TRUE;
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, insert_id)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->upsert_status->last_insert_id : 0;
}
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, affected_rows)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->upsert_status->affected_rows : 0;
}
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, num_rows)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt && stmt->result? mysqlnd_num_rows(stmt->result):0;
}
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, warning_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->upsert_status->warning_count : 0;
}
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, server_status)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->upsert_status->server_status : 0;
}
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, field_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->field_count : 0;
}
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, param_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->param_count : 0;
}
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, errno)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->error_info->error_no : 0;
}
static const char *
MYSQLND_METHOD(mysqlnd_stmt, error)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt? stmt->error_info->error : 0;
}
static const char *
MYSQLND_METHOD(mysqlnd_stmt, sqlstate)(const MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt && stmt->error_info->sqlstate[0] ? stmt->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, data_seek)(const MYSQLND_STMT * const s, uint64_t row TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
return stmt && stmt->result? stmt->result->m.seek_data(stmt->result, row TSRMLS_CC) : FAIL;
}
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, param_metadata)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
if (!stmt || !stmt->param_count) {
return NULL;
}
return NULL;
}
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, result_metadata)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_RES *result;
DBG_ENTER("mysqlnd_stmt::result_metadata");
if (!stmt) {
DBG_RETURN(NULL);
}
DBG_INF_FMT("stmt=%u field_count=%u", stmt->stmt_id, stmt->field_count);
if (!stmt->field_count || !stmt->conn || !stmt->result || !stmt->result->meta) {
DBG_INF("NULL");
DBG_RETURN(NULL);
}
if (stmt->update_max_length && stmt->result->stored_data) {
stmt->result->stored_data->m.initialize_result_set_rest(stmt->result->stored_data, stmt->result->meta, stmt->conn->stats,
stmt->conn->options->int_and_float_native TSRMLS_CC);
}
do {
result = stmt->conn->m->result_init(stmt->field_count, stmt->persistent TSRMLS_CC);
if (!result) {
break;
}
result->type = MYSQLND_RES_NORMAL;
result->unbuf = mysqlnd_result_unbuffered_init(stmt->field_count, TRUE, result->persistent TSRMLS_CC);
if (!result->unbuf) {
break;
}
result->unbuf->eof_reached = TRUE;
result->meta = stmt->result->meta->m->clone_metadata(stmt->result->meta, FALSE TSRMLS_CC);
if (!result->meta) {
break;
}
DBG_INF_FMT("result=%p", result);
DBG_RETURN(result);
} while (0);
SET_OOM_ERROR(*stmt->conn->error_info);
if (result) {
result->m.free_result(result, TRUE TSRMLS_CC);
}
DBG_RETURN(NULL);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, attr_set)(MYSQLND_STMT * const s,
enum mysqlnd_stmt_attr attr_type,
const void * const value TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::attr_set");
if (!stmt) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);
switch (attr_type) {
case STMT_ATTR_UPDATE_MAX_LENGTH:{
zend_uchar bval = *(zend_uchar *) value;
stmt->update_max_length = bval? TRUE:FALSE;
break;
}
case STMT_ATTR_CURSOR_TYPE: {
unsigned int ival = *(unsigned int *) value;
if (ival > (unsigned long) CURSOR_TYPE_READ_ONLY) {
SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
stmt->flags = ival;
break;
}
case STMT_ATTR_PREFETCH_ROWS: {
unsigned int ival = *(unsigned int *) value;
if (ival == 0) {
ival = MYSQLND_DEFAULT_PREFETCH_ROWS;
} else if (ival > 1) {
SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
DBG_INF("FAIL");
DBG_RETURN(FAIL);
}
stmt->prefetch_rows = ival;
break;
}
default:
SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
DBG_RETURN(FAIL);
}
DBG_INF("PASS");
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, attr_get)(const MYSQLND_STMT * const s,
enum mysqlnd_stmt_attr attr_type,
void * const value TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::attr_set");
if (!stmt) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);
switch (attr_type) {
case STMT_ATTR_UPDATE_MAX_LENGTH:
*(zend_bool *) value= stmt->update_max_length;
break;
case STMT_ATTR_CURSOR_TYPE:
*(unsigned long *) value= stmt->flags;
break;
case STMT_ATTR_PREFETCH_ROWS:
*(unsigned long *) value= stmt->prefetch_rows;
break;
default:
DBG_RETURN(FAIL);
}
DBG_INF_FMT("value=%lu", value);
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, free_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::free_result");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
if (!stmt->result) {
DBG_INF("no result");
DBG_RETURN(PASS);
}
if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
DBG_INF("fetching result set header");
stmt->default_rset_handler = s->m->use_result;
stmt->default_rset_handler(s TSRMLS_CC);
}
if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE) {
DBG_INF("skipping result");
stmt->result->m.skip_result(stmt->result TSRMLS_CC);
mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);
stmt->result->m.free_result_buffers(stmt->result TSRMLS_CC);
}
if (stmt->state > MYSQLND_STMT_PREPARED) {
stmt->state = MYSQLND_STMT_PREPARED;
}
CONN_SET_STATE(stmt->conn, CONN_READY);
DBG_RETURN(PASS);
}
static void
mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
unsigned int i;
DBG_ENTER("mysqlnd_stmt_separate_result_bind");
if (!stmt) {
DBG_VOID_RETURN;
}
DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count);
if (!stmt->result_bind) {
DBG_VOID_RETURN;
}
for (i = 0; i < stmt->field_count; i++) {
if (stmt->result_bind[i].bound == TRUE) {
DBG_INF_FMT("%u has refcount=%u", i, Z_REFCOUNT_P(stmt->result_bind[i].zv));
if (Z_REFCOUNT_P(stmt->result_bind[i].zv) > 1) {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
zval_copy_ctor(stmt->result_bind[i].zv);
#endif
zval_ptr_dtor(&stmt->result_bind[i].zv);
} else {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
ZVAL_NULL(stmt->result_bind[i].zv);
#endif
zval_ptr_dtor(&stmt->result_bind[i].zv);
}
}
}
s->m->free_result_bind(s, stmt->result_bind TSRMLS_CC);
stmt->result_bind = NULL;
DBG_VOID_RETURN;
}
static void
mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const s, unsigned int param_no TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt_separate_one_result_bind");
if (!stmt) {
DBG_VOID_RETURN;
}
DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u param_no=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count, param_no);
if (!stmt->result_bind) {
DBG_VOID_RETURN;
}
if (stmt->result_bind[param_no].bound == TRUE) {
DBG_INF_FMT("%u has refcount=%u", param_no, Z_REFCOUNT_P(stmt->result_bind[param_no].zv));
if (Z_REFCOUNT_P(stmt->result_bind[param_no].zv) > 1) {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
zval_copy_ctor(stmt->result_bind[param_no].zv);
#endif
zval_ptr_dtor(&stmt->result_bind[param_no].zv);
} else {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
ZVAL_NULL(stmt->result_bind[param_no].zv);
#endif
zval_ptr_dtor(&stmt->result_bind[param_no].zv);
}
}
DBG_VOID_RETURN;
}
static void
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::free_stmt_result");
if (!stmt) {
DBG_VOID_RETURN;
}
mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);
if (stmt->result) {
stmt->result->m.free_result_internal(stmt->result TSRMLS_CC);
stmt->result = NULL;
}
if (stmt->error_info->error_list) {
zend_llist_clean(stmt->error_info->error_list);
mnd_pefree(stmt->error_info->error_list, s->persistent);
stmt->error_info->error_list = NULL;
}
DBG_VOID_RETURN;
}
static void
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::free_stmt_content");
if (!stmt) {
DBG_VOID_RETURN;
}
DBG_INF_FMT("stmt=%lu param_bind=%p param_count=%u", stmt->stmt_id, stmt->param_bind, stmt->param_count);
if (stmt->param_bind) {
unsigned int i;
for (i = 0; i < stmt->param_count; i++) {
if (stmt->param_bind[i].zv) {
zval_ptr_dtor(&stmt->param_bind[i].zv);
}
}
s->m->free_parameter_bind(s, stmt->param_bind TSRMLS_CC);
stmt->param_bind = NULL;
}
s->m->free_stmt_result(s TSRMLS_CC);
DBG_VOID_RETURN;
}
static enum_func_status
MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, net_close)(MYSQLND_STMT * const s, zend_bool implicit TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
MYSQLND_CONN_DATA * conn;
zend_uchar cmd_buf[STMT_ID_LENGTH ];
enum_mysqlnd_collected_stats statistic = STAT_LAST;
DBG_ENTER("mysqlnd_stmt::net_close");
if (!stmt || !stmt->conn) {
DBG_RETURN(FAIL);
}
DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
conn = stmt->conn;
SET_EMPTY_ERROR(*stmt->error_info);
SET_EMPTY_ERROR(*stmt->conn->error_info);
do {
if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
DBG_INF("fetching result set header");
stmt->default_rset_handler(s TSRMLS_CC);
stmt->state = MYSQLND_STMT_USER_FETCHING;
}
if (stmt->result) {
DBG_INF("skipping result");
stmt->result->m.skip_result(stmt->result TSRMLS_CC);
}
} while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
if (stmt->stmt_id) {
MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE? STAT_FREE_RESULT_IMPLICIT:
STAT_FREE_RESULT_EXPLICIT);
int4store(cmd_buf, stmt->stmt_id);
if (CONN_GET_STATE(conn) == CONN_READY &&
FAIL == conn->m->simple_command(conn, COM_STMT_CLOSE, cmd_buf, sizeof(cmd_buf),
PROT_LAST ,
FALSE, TRUE TSRMLS_CC)) {
COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
DBG_RETURN(FAIL);
}
}
switch (stmt->execute_count) {
case 0:
statistic = STAT_PS_PREPARED_NEVER_EXECUTED;
break;
case 1:
statistic = STAT_PS_PREPARED_ONCE_USED;
break;
default:
break;
}
if (statistic != STAT_LAST) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, statistic);
}
if (stmt->execute_cmd_buffer.buffer) {
mnd_pefree(stmt->execute_cmd_buffer.buffer, stmt->persistent);
stmt->execute_cmd_buffer.buffer = NULL;
}
s->m->free_stmt_content(s TSRMLS_CC);
if (stmt->conn) {
stmt->conn->m->free_reference(stmt->conn TSRMLS_CC);
stmt->conn = NULL;
}
DBG_RETURN(PASS);
}
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, dtor)(MYSQLND_STMT * const s, zend_bool implicit TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = (s != NULL) ? s->data:NULL;
enum_func_status ret = FAIL;
zend_bool persistent = (s != NULL) ? s->persistent : 0;
DBG_ENTER("mysqlnd_stmt::dtor");
if (stmt) {
DBG_INF_FMT("stmt=%p", stmt);
MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE? STAT_STMT_CLOSE_IMPLICIT:
STAT_STMT_CLOSE_EXPLICIT);
ret = s->m->net_close(s, implicit TSRMLS_CC);
mnd_pefree(stmt, persistent);
}
mnd_pefree(s, persistent);
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
static MYSQLND_PARAM_BIND *
MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::alloc_param_bind");
if (!stmt) {
DBG_RETURN(NULL);
}
DBG_RETURN(mnd_pecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND), stmt->persistent));
}
static MYSQLND_RESULT_BIND *
MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind)(MYSQLND_STMT * const s TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
DBG_ENTER("mysqlnd_stmt::alloc_result_bind");
if (!stmt) {
DBG_RETURN(NULL);
}
DBG_RETURN(mnd_pecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND), stmt->persistent));
}
PHPAPI void
MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * param_bind TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
if (stmt) {
mnd_pefree(param_bind, stmt->persistent);
}
}
PHPAPI void
MYSQLND_METHOD(mysqlnd_stmt, free_result_bind)(MYSQLND_STMT * const s, MYSQLND_RESULT_BIND * result_bind TSRMLS_DC)
{
MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
if (stmt) {
mnd_pefree(result_bind, stmt->persistent);
}
}
MYSQLND_CLASS_METHODS_START(mysqlnd_stmt)
MYSQLND_METHOD(mysqlnd_stmt, prepare),
MYSQLND_METHOD(mysqlnd_stmt, execute),
MYSQLND_METHOD(mysqlnd_stmt, use_result),
MYSQLND_METHOD(mysqlnd_stmt, store_result),
MYSQLND_METHOD(mysqlnd_stmt, get_result),
MYSQLND_METHOD(mysqlnd_stmt, more_results),
MYSQLND_METHOD(mysqlnd_stmt, next_result),
MYSQLND_METHOD(mysqlnd_stmt, free_result),
MYSQLND_METHOD(mysqlnd_stmt, data_seek),
MYSQLND_METHOD(mysqlnd_stmt, reset),
MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, net_close),
MYSQLND_METHOD(mysqlnd_stmt, dtor),
MYSQLND_METHOD(mysqlnd_stmt, fetch),
MYSQLND_METHOD(mysqlnd_stmt, bind_parameters),
MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter),
MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param),
MYSQLND_METHOD(mysqlnd_stmt, bind_result),
MYSQLND_METHOD(mysqlnd_stmt, bind_one_result),
MYSQLND_METHOD(mysqlnd_stmt, send_long_data),
MYSQLND_METHOD(mysqlnd_stmt, param_metadata),
MYSQLND_METHOD(mysqlnd_stmt, result_metadata),
MYSQLND_METHOD(mysqlnd_stmt, insert_id),
MYSQLND_METHOD(mysqlnd_stmt, affected_rows),
MYSQLND_METHOD(mysqlnd_stmt, num_rows),
MYSQLND_METHOD(mysqlnd_stmt, param_count),
MYSQLND_METHOD(mysqlnd_stmt, field_count),
MYSQLND_METHOD(mysqlnd_stmt, warning_count),
MYSQLND_METHOD(mysqlnd_stmt, errno),
MYSQLND_METHOD(mysqlnd_stmt, error),
MYSQLND_METHOD(mysqlnd_stmt, sqlstate),
MYSQLND_METHOD(mysqlnd_stmt, attr_get),
MYSQLND_METHOD(mysqlnd_stmt, attr_set),
MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind),
MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind),
MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind),
MYSQLND_METHOD(mysqlnd_stmt, free_result_bind),
MYSQLND_METHOD(mysqlnd_stmt, server_status),
mysqlnd_stmt_execute_generate_request,
mysqlnd_stmt_execute_parse_response,
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content),
MYSQLND_METHOD(mysqlnd_stmt, flush),
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)
MYSQLND_CLASS_METHODS_END;
MYSQLND_STMT *
_mysqlnd_stmt_init(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
{
MYSQLND_STMT * ret;
DBG_ENTER("_mysqlnd_stmt_init");
ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_prepared_statement(conn TSRMLS_CC);
DBG_RETURN(ret);
}
void _mysqlnd_init_ps_subsystem()
{
mysqlnd_stmt_set_methods(&MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_stmt));
_mysqlnd_init_ps_fetch_subsystem();
}