From dd7d41e93835046c0f0c81a5ff21856c1ae8321b Mon Sep 17 00:00:00 2001 From: Hartmut Holzgraefe Date: Sun, 27 Feb 2005 12:43:23 +0000 Subject: [PATCH] use fetch_fields only once and remember the result --- ext/pdo_mysql/mysql_statement.c | 41 +++++++++++++++---------------- ext/pdo_mysql/php_pdo_mysql_int.h | 1 + 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ext/pdo_mysql/mysql_statement.c b/ext/pdo_mysql/mysql_statement.c index 7095cb8ec5..b7d423173d 100755 --- a/ext/pdo_mysql/mysql_statement.c +++ b/ext/pdo_mysql/mysql_statement.c @@ -75,6 +75,7 @@ static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) 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, ... @@ -98,8 +99,9 @@ static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, return 0; } if ((S->current_data = mysql_fetch_row(S->result)) == NULL) { - /* there seems to be no way of distinguishing 'no data' from 'error' */ - pdo_mysql_error_stmt(stmt); + if (mysql_errno(S->H->server)) { + pdo_mysql_error_stmt(stmt); + } return 0; } S->current_lengths = mysql_fetch_lengths(S->result); @@ -112,27 +114,30 @@ static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) MYSQL_RES *R = S->result; MYSQL_FIELD *F; struct pdo_column_data *cols = stmt->columns; - unsigned int num_fields, i; + unsigned int i; if (!S->result) { return 0; } + + if (colno >= stmt->column_count) { + /* error invalid column */ + return 0; + } + /* fetch all on demand, this seems easiest ** if we've been here before bail out */ if (cols[0].name) { return 1; } - num_fields = mysql_num_fields(R); - F = mysql_fetch_fields(R); - for (i=0; i < num_fields; i++) { + for(i=0; i < stmt->column_count; i++) { int namelen; - namelen = strlen(F[i].name); - cols[i].precision = F[i].decimals; - cols[i].maxlen = F[i].length; + namelen = strlen(S->fields[i].name); + cols[i].precision = S->fields[i].decimals; + cols[i].maxlen = S->fields[i].length; cols[i].namelen = namelen; - /* FIXME where does this get freed? */ - cols[i].name = estrndup(F[i].name, namelen + 1); + cols[i].name = estrndup(S->fields[i].name, namelen + 1); cols[i].param_type = PDO_PARAM_STR; } return 1; @@ -145,9 +150,8 @@ static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsig if (S->current_data == NULL || !S->result) { return 0; } - if (colno >= mysql_num_fields(S->result)) { + if (colno >= stmt->column_count) { /* error invalid column */ - pdo_mysql_error_stmt(stmt); return 0; } *ptr = S->current_data[colno]; @@ -199,15 +203,8 @@ static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_va if (!S->result) { return FAILURE; } - if (colno >= mysql_num_fields(S->result)) { + if (colno >= stmt->column_count) { /* error invalid column */ - pdo_mysql_error_stmt(stmt); - return FAILURE; - } - mysql_field_seek(S->result, colno); - F = mysql_fetch_field(S->result); - if (!F) { - pdo_mysql_error_stmt(stmt); return FAILURE; } @@ -215,6 +212,8 @@ static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_va MAKE_STD_ZVAL(flags); array_init(flags); + F = S->fields + colno; + if (F->def) { add_assoc_string(return_value, "mysql:def", F->def, 1); } diff --git a/ext/pdo_mysql/php_pdo_mysql_int.h b/ext/pdo_mysql/php_pdo_mysql_int.h index eacaa66f63..888a605591 100755 --- a/ext/pdo_mysql/php_pdo_mysql_int.h +++ b/ext/pdo_mysql/php_pdo_mysql_int.h @@ -47,6 +47,7 @@ typedef struct { typedef struct { pdo_mysql_db_handle *H; MYSQL_RES *result; + MYSQL_FIELD *fields; MYSQL_ROW current_data; long *current_lengths; } pdo_mysql_stmt; -- 2.40.0