/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: George Schlossnagle <george@omniti.com> |
| Wez Furlong <wez@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id: mysql_statement.c,v 1.48.2.14 2006/04/22 16:35:18 wez Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "pdo/php_pdo.h"
#include "pdo/php_pdo_driver.h"
#include "php_pdo_mysql.h"
#include "php_pdo_mysql_int.h"
static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
if (S->result) {
/* free the resource */
mysql_free_result(S->result);
S->result = NULL;
}
if (S->einfo.errmsg) {
pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
S->einfo.errmsg = NULL;
}
#if HAVE_MYSQL_STMT_PREPARE
if (S->stmt) {
mysql_stmt_close(S->stmt);
S->stmt = NULL;
}
if (S->params) {
efree(S->params);
efree(S->in_null);
efree(S->in_length);
}
if (S->bound_result)
{
int i;
for (i = 0; i < stmt->column_count; i++) {
efree(S->bound_result[i].buffer);
}
efree(S->bound_result);
efree(S->out_null);
efree(S->out_length);
}
#endif
#if HAVE_MYSQL_NEXT_RESULT
while (mysql_more_results(S->H->server)) {
MYSQL_RES *res;
if (mysql_next_result(S->H->server) != 0) {
break;
}
res = mysql_store_result(S->H->server);
if (res) {
mysql_free_result(res);
}
}
#endif
efree(S);
return 1;
}
static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
pdo_mysql_db_handle *H = S->H;
my_ulonglong row_count;
#if HAVE_MYSQL_STMT_PREPARE
int i;
if (S->stmt) {
/* (re)bind the parameters */
if (mysql_stmt_bind_param(S->stmt, S->params)) {
pdo_mysql_error_stmt(stmt);
return 0;
}
if (mysql_stmt_execute(S->stmt)) {
pdo_mysql_error_stmt(stmt);
return 0;
}
if (!S->result) {
/* figure out the result set format, if any */
S->result = mysql_stmt_result_metadata(S->stmt);
if (S->result) {
int calc_max_length = H->buffered && S->max_length == 1;
S->fields = mysql_fetch_fields(S->result);
if (S->bound_result) {
int i;
for (i = 0; i < stmt->column_count; i++) {
efree(S->bound_result[i].buffer);
}
efree(S->bound_result);
efree(S->out_null);
efree(S->out_length);
}
stmt->column_count = (int)mysql_num_fields(S->result);
S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
S->out_length = ecalloc(stmt->column_count, sizeof(unsigned long));
/* summon memory to hold the row */
for (i = 0; i < stmt->column_count; i++) {
if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
my_bool on = 1;
mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
calc_max_length = 0;
}
switch (S->fields[i].type) {
case FIELD_TYPE_INT24:
S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH;
break;
case FIELD_TYPE_LONG:
S->bound_result[i].buffer_length = MAX_INT_WIDTH;
break;
case FIELD_TYPE_LONGLONG:
S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH;
break;
case FIELD_TYPE_TINY:
S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH;
break;
case FIELD_TYPE_SHORT:
S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH;
break;
default:
S->bound_result[i].buffer_length =
S->fields[i].max_length? S->fields[i].max_length:
S->fields[i].length;
/* work-around for longtext and alike */
if (S->bound_result[i].buffer_length > H->max_buffer_size) {
S->bound_result[i].buffer_length = H->max_buffer_size;
}
}
#if 0
printf("%d: max_length=%d length=%d buffer_length=%d type=%d\n",
i,
S->fields[i].max_length,
S->fields[i].length,
S->bound_result[i].buffer_length,
S->fields[i].type
);
#endif
/* there are cases where the length reported by mysql is too short.
* eg: when describing a table that contains an enum column. Since
* we have no way of knowing the true length either, we'll bump up
* our buffer size to a reasonable size, just in case */
if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
S->bound_result[i].buffer_length = 128;
}
S->out_length[i] = 0;
S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
S->bound_result[i].is_null = &S->out_null[i];
S->bound_result[i].length = &S->out_length[i];
S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
}
if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
pdo_mysql_error_stmt(stmt);
return 0;
}
/* if buffered, pre-fetch all the data */
if (H->buffered) {
mysql_stmt_store_result(S->stmt);
}
}
}
row_count = mysql_stmt_affected_rows(S->stmt);
if (row_count != (my_ulonglong)-1) {
stmt->row_count = row_count;
}
return 1;
}
#endif
/* ensure that we free any previous unfetched results */
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}
if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
pdo_mysql_error_stmt(stmt);
return 0;
}
row_count = mysql_affected_rows(H->server);
if (row_count == (my_ulonglong)-1) {
/* we either have a query that returned a result set or an error occured
lets see if we have access to a result set */
if (!H->buffered) {
S->result = mysql_use_result(H->server);
} else {
S->result = mysql_store_result(H->server);
}
if (NULL == S->result) {
pdo_mysql_error_stmt(stmt);
return 0;
}
stmt->row_count = 0;
if (!stmt->executed) {
stmt->column_count = (int) mysql_num_fields(S->result);
S->fields = mysql_fetch_fields(S->result);
}
} else {
/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
stmt->row_count = row_count;
}
return 1;
}
static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC)
{
#if HAVE_MYSQL_NEXT_RESULT
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
pdo_mysql_db_handle *H = S->H;
my_ulonglong row_count;
int ret;
/* ensure that we free any previous unfetched results */
#if HAVE_MYSQL_STMT_PREPARE
if (S->stmt) {
mysql_stmt_free_result(S->stmt);
}
#endif
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}
ret = mysql_next_result(H->server);
if (ret > 0) {
pdo_mysql_error_stmt(stmt);
return 0;
} else if (ret < 0) {
/* No more results */
return 0;
} else {
if ((my_ulonglong)-1 == (row_count = mysql_affected_rows(H->server))) {
pdo_mysql_error_stmt(stmt);
return 0;
}
if (!H->buffered) {
S->result = mysql_use_result(H->server);
} else {
S->result = mysql_store_resu