cvs: pecl /pdo_mysql mysql_driver.c mysql_statement.c php_pdo_mysql_int.h

From: Date: Sun, 13 Feb 2005 00:48:01 +0000
Subject: cvs: pecl /pdo_mysql mysql_driver.c mysql_statement.c php_pdo_mysql_int.h
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-2521@lists.php.net to get a copy of this message
hholzgra Sat Feb 12 19:48:01 2005 EDT Modified files: /pecl/pdo_mysql mysql_driver.c mysql_statement.c php_pdo_mysql_int.h Log: DOS -> Unix line endings

http://cvs.php.net/diff.php/pecl/pdo_mysql/mysql_driver.c?r1=1.29&r2=1.30&ty=u Index: pecl/pdo_mysql/mysql_driver.c diff -u pecl/pdo_mysql/mysql_driver.c:1.29 pecl/pdo_mysql/mysql_driver.c:1.30 --- pecl/pdo_mysql/mysql_driver.c:1.29 Sat Feb 12 15:58:29 2005 +++ pecl/pdo_mysql/mysql_driver.c Sat Feb 12 19:48:00 2005 @@ -1,324 +1,324 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2004 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 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_0.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> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: mysql_driver.c,v 1.29 2005/02/12 20:58:29 iliaa 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" -#include "zend_exceptions.h" - -int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC) /* {{{ */ -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; - pdo_mysql_error_info *einfo = &H->einfo; - - einfo->errcode = mysql_errno(H->server); - einfo->file = file; - einfo->line = line; - - if (einfo->errmsg) { - pefree(einfo->errmsg, dbh->is_persistent); - einfo->errmsg = NULL; - } - - if (einfo->errcode) { - einfo->errmsg = pestrdup(mysql_error(H->server), dbh->is_persistent); - } else { /* no error */ - strcpy(*pdo_err, PDO_ERR_NONE); - return 0; - } - - /* mapping taken from: - http://dev.mysql.com/doc/mysql/en/Error-handling.html - */ - switch (einfo->errcode) { - default: - case 1000: case 1001: case 1002: case 1003: - case 1004: case 1005: case 1006: case 1007: - case 1008: case 1009: case 1010: case 1011: - case 1012: case 1013: case 1014: case 1015: - case 1016: case 1017: case 1018: case 1019: - case 1020: case 1021: case 1023: case 1024: - case 1025: case 1026: case 1027: case 1028: - case 1029: case 1030: case 1031: case 1032: - case 1034: case 1035: case 1036: case 1039: - case 1041: - strcpy(*pdo_err, "HY000"); - break; - case 1022: - strcpy(*pdo_err, "23000"); - break; - case 1037: case 1038: - strcpy(*pdo_err, "HY001"); - break; - case 1040: - strcpy(*pdo_err, "08004"); - break; - case 1042: case 1043: - strcpy(*pdo_err, "08S01"); - break; - case 1044: - strcpy(*pdo_err, "42000"); - break; - case 1045: - strcpy(*pdo_err, "28000"); - break; - - /* TODO: someone with more time on their hands - * needs to complete this list */ - } - - if (!dbh->methods) { - zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s", - *pdo_err, einfo->errcode, einfo->errmsg); - } - - return einfo->errcode; -} -/* }}} */ - -static int pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - pdo_mysql_error_info *einfo = &H->einfo; - - if (einfo->errcode) { - add_next_index_long(info, einfo->errcode); - add_next_index_string(info, einfo->errmsg, 1); - } - - return 1; -} - -static int mysql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - - if (H) { - if (H->server) { - mysql_close(H->server); - H->server = NULL; - } - if (H->einfo.errmsg) { - pefree(H->einfo.errmsg, dbh->is_persistent); - H->einfo.errmsg = NULL; - } - pefree(H, dbh->is_persistent); - dbh->driver_data = NULL; - } - return 0; -} -/* }}} */ - -static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt)); - - S->H = H; - stmt->driver_data = S; - stmt->methods = &mysql_stmt_methods; - stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; - - return 1; -} - -static long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - - if (mysql_real_query(H->server, sql, sql_len)) { - pdo_mysql_error(dbh); - return -1; - } else { - return mysql_affected_rows(H->server); - } -} - -static long pdo_mysql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - - return (long) mysql_insert_id(H->server); -} - -static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - *quoted = emalloc(2*unquotedlen + 3); - (*quoted)[0] = '"'; - *quotedlen = mysql_real_escape_string(H->server, *quoted + 1, unquoted, unquotedlen); - (*quoted)[*quotedlen + 1] = '"'; - (*quoted)[*quotedlen + 2] = '\0'; - *quotedlen += 2; - return 1; -} - -static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) -{ - pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; - - switch (attr) { - case PDO_ATTR_CLIENT_VERSION: - ZVAL_STRING(return_value, (char *)mysql_get_client_info(), 1); - break; - - case PDO_ATTR_SERVER_VERSION: - ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server), 1); - break; - - case PDO_ATTR_CONNECTION_STATUS: - ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server), 1); - break; - - case PDO_ATTR_SERVER_INFO: { - char *tmp; - - if ((tmp = (char *)mysql_stat(H->server))) { - ZVAL_STRING(return_value, tmp, 1); - } else { - pdo_mysql_error(dbh); - return -1; - } - } - break; - - default: - return 0; - } - - return 1; -} - -static struct pdo_dbh_methods mysql_methods = { - mysql_handle_closer, - mysql_handle_preparer, - mysql_handle_doer, - mysql_handle_quoter, - NULL, - NULL, - NULL, - NULL, - pdo_mysql_last_insert_id, - pdo_mysql_fetch_error_func, - pdo_mysql_get_attribute, - NULL /* check_liveness: TODO: ping */ -}; - -#ifndef PDO_MYSQL_UNIX_ADDR -# define PDO_MYSQL_UNIX_ADDR "MySQL" -#endif - -static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ -{ - pdo_mysql_db_handle *H; - int i, ret = 0; - char *host = NULL, *unix_socket = NULL; - unsigned int port = 3306; - char *dbname; - struct pdo_data_src_parser vars[] = { - { "charset", NULL, 0 }, - { "dbname", "", 0 }, - { "host", "localhost", 0 }, - { "port", "3306", 0 }, - { "unix_socket", PDO_MYSQL_UNIX_ADDR, 0 }, - }; - - php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 4); - - H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent); - - H->einfo.errcode = 0; - H->einfo.errmsg = NULL; - - /* allocate an environment */ - - /* handle for the server */ - H->server = mysql_init(NULL); - dbh->driver_data = H; - - /* handle MySQL options */ - if (driver_options) { - long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC); - - if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) { - pdo_mysql_error(dbh); - goto cleanup; - } - } - -#ifndef PHP_WIN32 - if (vars[2].optval && !strcmp("localhost", vars[2].optval)) { - host = "."; - unix_socket = vars[4].optval; - } else -#endif - { - host = vars[2].optval; - port = atoi(vars[3].optval); - } - dbname = vars[1].optval; - if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, 0) == NULL) { - pdo_mysql_error(dbh); - goto cleanup; - } - - H->attached = 1; - - dbh->alloc_own_columns = 1; - dbh->max_escaped_char_length = 2; - dbh->methods = &mysql_methods; - - ret = 1; - -cleanup: - for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) { - if (vars[i].freeme) { - efree(vars[i].optval); - } - } - - dbh->methods = &mysql_methods; - - return ret; -} -/* }}} */ - -pdo_driver_t pdo_mysql_driver = { - PDO_DRIVER_HEADER(mysql), - pdo_mysql_handle_factory -}; - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2004 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 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_0.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> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: mysql_driver.c,v 1.30 2005/02/13 00:48:00 hholzgra 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" +#include "zend_exceptions.h" + +int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC) /* {{{ */ +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; + pdo_mysql_error_info *einfo = &H->einfo; + + einfo->errcode = mysql_errno(H->server); + einfo->file = file; + einfo->line = line; + + if (einfo->errmsg) { + pefree(einfo->errmsg, dbh->is_persistent); + einfo->errmsg = NULL; + } + + if (einfo->errcode) { + einfo->errmsg = pestrdup(mysql_error(H->server), dbh->is_persistent); + } else { /* no error */ + strcpy(*pdo_err, PDO_ERR_NONE); + return 0; + } + + /* mapping taken from: + http://dev.mysql.com/doc/mysql/en/Error-handling.html + */ + switch (einfo->errcode) { + default: + case 1000: case 1001: case 1002: case 1003: + case 1004: case 1005: case 1006: case 1007: + case 1008: case 1009: case 1010: case 1011: + case 1012: case 1013: case 1014: case 1015: + case 1016: case 1017: case 1018: case 1019: + case 1020: case 1021: case 1023: case 1024: + case 1025: case 1026: case 1027: case 1028: + case 1029: case 1030: case 1031: case 1032: + case 1034: case 1035: case 1036: case 1039: + case 1041: + strcpy(*pdo_err, "HY000"); + break; + case 1022: + strcpy(*pdo_err, "23000"); + break; + case 1037: case 1038: + strcpy(*pdo_err, "HY001"); + break; + case 1040: + strcpy(*pdo_err, "08004"); + break; + case 1042: case 1043: + strcpy(*pdo_err, "08S01"); + break; + case 1044: + strcpy(*pdo_err, "42000"); + break; + case 1045: + strcpy(*pdo_err, "28000"); + break; + + /* TODO: someone with more time on their hands + * needs to complete this list */ + } + + if (!dbh->methods) { + zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s", + *pdo_err, einfo->errcode, einfo->errmsg); + } + + return einfo->errcode; +} +/* }}} */ + +static int pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + pdo_mysql_error_info *einfo = &H->einfo; + + if (einfo->errcode) { + add_next_index_long(info, einfo->errcode); + add_next_index_string(info, einfo->errmsg, 1); + } + + return 1; +} + +static int mysql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + + if (H) { + if (H->server) { + mysql_close(H->server); + H->server = NULL; + } + if (H->einfo.errmsg) { + pefree(H->einfo.errmsg, dbh->is_persistent); + H->einfo.errmsg = NULL; + } + pefree(H, dbh->is_persistent); + dbh->driver_data = NULL; + } + return 0; +} +/* }}} */ + +static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt)); + + S->H = H; + stmt->driver_data = S; + stmt->methods = &mysql_stmt_methods; + stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; + + return 1; +} + +static long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + + if (mysql_real_query(H->server, sql, sql_len)) { + pdo_mysql_error(dbh); + return -1; + } else { + return mysql_affected_rows(H->server); + } +} + +static long pdo_mysql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + + return (long) mysql_insert_id(H->server); +} + +static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + *quoted = emalloc(2*unquotedlen + 3); + (*quoted)[0] = '"'; + *quotedlen = mysql_real_escape_string(H->server, *quoted + 1, unquoted, unquotedlen); + (*quoted)[*quotedlen + 1] = '"'; + (*quoted)[*quotedlen + 2] = '\0'; + *quotedlen += 2; + return 1; +} + +static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) +{ + pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; + + switch (attr) { + case PDO_ATTR_CLIENT_VERSION: + ZVAL_STRING(return_value, (char *)mysql_get_client_info(), 1); + break; + + case PDO_ATTR_SERVER_VERSION: + ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server), 1); + break; + + case PDO_ATTR_CONNECTION_STATUS: + ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server), 1); + break; + + case PDO_ATTR_SERVER_INFO: { + char *tmp; + + if ((tmp = (char *)mysql_stat(H->server))) { + ZVAL_STRING(return_value, tmp, 1); + } else { + pdo_mysql_error(dbh); + return -1; + } + } + break; + + default: + return 0; + } + + return 1; +} + +static struct pdo_dbh_methods mysql_methods = { + mysql_handle_closer, + mysql_handle_preparer, + mysql_handle_doer, + mysql_handle_quoter, + NULL, + NULL, + NULL, + NULL, + pdo_mysql_last_insert_id, + pdo_mysql_fetch_error_func, + pdo_mysql_get_attribute, + NULL /* check_liveness: TODO: ping */ +}; + +#ifndef PDO_MYSQL_UNIX_ADDR +# define PDO_MYSQL_UNIX_ADDR "MySQL" +#endif + +static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ +{ + pdo_mysql_db_handle *H; + int i, ret = 0; + char *host = NULL, *unix_socket = NULL; + unsigned int port = 3306; + char *dbname; + struct pdo_data_src_parser vars[] = { + { "charset", NULL, 0 }, + { "dbname", "", 0 }, + { "host", "localhost", 0 }, + { "port", "3306", 0 }, + { "unix_socket", PDO_MYSQL_UNIX_ADDR, 0 }, + }; + + php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 4); + + H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent); + + H->einfo.errcode = 0; + H->einfo.errmsg = NULL; + + /* allocate an environment */ + + /* handle for the server */ + H->server = mysql_init(NULL); + dbh->driver_data = H; + + /* handle MySQL options */ + if (driver_options) { + long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC); + + if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) { + pdo_mysql_error(dbh); + goto cleanup; + } + } + +#ifndef PHP_WIN32 + if (vars[2].optval && !strcmp("localhost", vars[2].optval)) { + host = "."; + unix_socket = vars[4].optval; + } else +#endif + { + host = vars[2].optval; + port = atoi(vars[3].optval); + } + dbname = vars[1].optval; + if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, 0) == NULL) { + pdo_mysql_error(dbh); + goto cleanup; + } + + H->attached = 1; + + dbh->alloc_own_columns = 1; + dbh->max_escaped_char_length = 2; + dbh->methods = &mysql_methods; + + ret = 1; + +cleanup: + for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) { + if (vars[i].freeme) { + efree(vars[i].optval); + } + } + + dbh->methods = &mysql_methods; + + return ret; +} +/* }}} */ + +pdo_driver_t pdo_mysql_driver = { + PDO_DRIVER_HEADER(mysql), + pdo_mysql_handle_factory +}; + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ http://cvs.php.net/diff.php/pecl/pdo_mysql/mysql_statement.c?r1=1.13&r2=1.14&ty=u Index: pecl/pdo_mysql/mysql_statement.c diff -u pecl/pdo_mysql/mysql_statement.c:1.13 pecl/pdo_mysql/mysql_statement.c:1.14 --- pecl/pdo_mysql/mysql_statement.c:1.13 Sat Feb 12 15:58:29 2005 +++ pecl/pdo_mysql/mysql_statement.c Sat Feb 12 19:48:00 2005 @@ -1,259 +1,259 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2004 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 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_0.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> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: mysql_statement.c,v 1.13 2005/02/12 20:58:29 iliaa 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->cols) { - efree(S->cols); - S->cols = NULL; - } - 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; - - if (stmt->executed) { - /* 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; - } - if ((S->result = mysql_use_result(H->server)) == NULL) { - /* could've been INSERT/UPDATE/DELETE query */ - if (!mysql_affected_rows(H->server)) { - pdo_mysql_error_stmt(stmt); - return 0; - } - return 1; - } - if (!stmt->executed) { - stmt->column_count = (int) mysql_num_fields(S->result); - S->cols = ecalloc(stmt->column_count, sizeof(pdo_mysql_column)); - } - return 1; -} - -static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, - enum pdo_param_event event_type TSRMLS_DC) -{ - return 1; -} - -static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, - enum pdo_fetch_orientation ori, long offset TSRMLS_DC) -{ - pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; - if (!S->result) { - 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); - return 0; - } - S->current_lengths = mysql_fetch_lengths(S->result); - return 1; -} - -static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) -{ - pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; - MYSQL_RES *R = S->result; - MYSQL_FIELD *F; - struct pdo_column_data *cols = stmt->columns; - unsigned int num_fields, i; - - if (!S->result) { - 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++) { - int namelen; - namelen = strlen(F[i].name); - cols[i].precision = F[i].decimals; - cols[i].maxlen = F[i].length; - cols[i].namelen = namelen; - /* FIXME where does this get freed? */ - cols[i].name = estrndup(F[i].name, namelen + 1); - cols[i].param_type = PDO_PARAM_STR; - } - return 1; -} - -static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) -{ - pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; - - if(S->current_data == NULL || !S->result) { - return 0; - } - if(colno >= mysql_num_fields(S->result)) { - /* error invalid column */ - pdo_mysql_error_stmt(stmt); - return 0; - } - *ptr = S->current_data[colno]; - *len = S->current_lengths[colno]; - return 1; -} - -static char *type_to_name_native(int type) -{ -#define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x; - - switch (type) { - PDO_MYSQL_NATIVE_TYPE_NAME(STRING) - PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING) -#ifdef MYSQL_HAS_TINY - PDO_MYSQL_NATIVE_TYPE_NAME(TINY) -#endif - PDO_MYSQL_NATIVE_TYPE_NAME(SHORT) - PDO_MYSQL_NATIVE_TYPE_NAME(LONG) - PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG) - PDO_MYSQL_NATIVE_TYPE_NAME(INT24) - PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT) - PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE) - PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL) - PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP) -#ifdef MYSQL_HAS_YEAR - PDO_MYSQL_NATIVE_TYPE_NAME(YEAR) -#endif - PDO_MYSQL_NATIVE_TYPE_NAME(DATE) - PDO_MYSQL_NATIVE_TYPE_NAME(TIME) - PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME) - PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB) - PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB) - PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB) - PDO_MYSQL_NATIVE_TYPE_NAME(BLOB) - PDO_MYSQL_NATIVE_TYPE_NAME(NULL) - default: - return NULL; - } -} - -static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) -{ - pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; - MYSQL_FIELD *F; - zval *flags; - char *str; - - if(S->current_data == NULL || !S->result) { - return FAILURE; - } - if(colno >= mysql_num_fields(S->result)) { - /* 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; - } - - array_init(return_value); - MAKE_STD_ZVAL(flags); - array_init(flags); - - if (F->def) { - add_assoc_string(return_value, "mysql:def", F->def, 1); - } - if (IS_NOT_NULL(F->flags)) { - add_next_index_string(flags, "not_null", 1); - } - if (IS_PRI_KEY(F->flags)) { - add_next_index_string(flags, "primary_key", 1); - } - if (F->flags & MULTIPLE_KEY_FLAG) { - add_next_index_string(flags, "multiple_key", 1); - } - if (F->flags & UNIQUE_KEY_FLAG) { - add_next_index_string(flags, "unique_key", 1); - } - if (IS_BLOB(F->flags)) { - add_next_index_string(flags, "blob", 1); - } - str = type_to_name_native(F->flags); - if (str) { - add_assoc_string(return_value, "native_type", str, 1); - } - - add_assoc_zval(return_value, "flags", flags); - return SUCCESS; -} - -struct pdo_stmt_methods mysql_stmt_methods = { - pdo_mysql_stmt_dtor, - pdo_mysql_stmt_execute, - pdo_mysql_stmt_fetch, - pdo_mysql_stmt_describe, - pdo_mysql_stmt_get_col, - pdo_mysql_stmt_param_hook, - NULL, /* set_attr */ - NULL, /* get_attr */ - pdo_mysql_stmt_col_meta -}; - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2004 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 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_0.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> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: mysql_statement.c,v 1.14 2005/02/13 00:48:00 hholzgra 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->cols) { + efree(S->cols); + S->cols = NULL; + } + 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; + + if (stmt->executed) { + /* 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; + } + if ((S->result = mysql_use_result(H->server)) == NULL) { + /* could've been INSERT/UPDATE/DELETE query */ + if (!mysql_affected_rows(H->server)) { + pdo_mysql_error_stmt(stmt); + return 0; + } + return 1; + } + if (!stmt->executed) { + stmt->column_count = (int) mysql_num_fields(S->result); + S->cols = ecalloc(stmt->column_count, sizeof(pdo_mysql_column)); + } + return 1; +} + +static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, + enum pdo_param_event event_type TSRMLS_DC) +{ + return 1; +} + +static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, + enum pdo_fetch_orientation ori, long offset TSRMLS_DC) +{ + pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; + if (!S->result) { + 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); + return 0; + } + S->current_lengths = mysql_fetch_lengths(S->result); + return 1; +} + +static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) +{ + pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; + MYSQL_RES *R = S->result; + MYSQL_FIELD *F; + struct pdo_column_data *cols = stmt->columns; + unsigned int num_fields, i; + + if (!S->result) { + 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++) { + int namelen; + namelen = strlen(F[i].name); + cols[i].precision = F[i].decimals; + cols[i].maxlen = F[i].length; + cols[i].namelen = namelen; + /* FIXME where does this get freed? */ + cols[i].name = estrndup(F[i].name, namelen + 1); + cols[i].param_type = PDO_PARAM_STR; + } + return 1; +} + +static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) +{ + pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; + + if(S->current_data == NULL || !S->result) { + return 0; + } + if(colno >= mysql_num_fields(S->result)) { + /* error invalid column */ + pdo_mysql_error_stmt(stmt); + return 0; + } + *ptr = S->current_data[colno]; + *len = S->current_lengths[colno]; + return 1; +} + +static char *type_to_name_native(int type) +{ +#define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x; + + switch (type) { + PDO_MYSQL_NATIVE_TYPE_NAME(STRING) + PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING) +#ifdef MYSQL_HAS_TINY + PDO_MYSQL_NATIVE_TYPE_NAME(TINY) +#endif + PDO_MYSQL_NATIVE_TYPE_NAME(SHORT) + PDO_MYSQL_NATIVE_TYPE_NAME(LONG) + PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG) + PDO_MYSQL_NATIVE_TYPE_NAME(INT24) + PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT) + PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE) + PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL) + PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP) +#ifdef MYSQL_HAS_YEAR + PDO_MYSQL_NATIVE_TYPE_NAME(YEAR) +#endif + PDO_MYSQL_NATIVE_TYPE_NAME(DATE) + PDO_MYSQL_NATIVE_TYPE_NAME(TIME) + PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME) + PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB) + PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB) + PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB) + PDO_MYSQL_NATIVE_TYPE_NAME(BLOB) + PDO_MYSQL_NATIVE_TYPE_NAME(NULL) + default: + return NULL; + } +} + +static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) +{ + pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data; + MYSQL_FIELD *F; + zval *flags; + char *str; + + if(S->current_data == NULL || !S->result) { + return FAILURE; + } + if(colno >= mysql_num_fields(S->result)) { + /* 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; + } + + array_init(return_value); + MAKE_STD_ZVAL(flags); + array_init(flags); + + if (F->def) { + add_assoc_string(return_value, "mysql:def", F->def, 1); + } + if (IS_NOT_NULL(F->flags)) { + add_next_index_string(flags, "not_null", 1); + } + if (IS_PRI_KEY(F->flags)) { + add_next_index_string(flags, "primary_key", 1); + } + if (F->flags & MULTIPLE_KEY_FLAG) { + add_next_index_string(flags, "multiple_key", 1); + } + if (F->flags & UNIQUE_KEY_FLAG) { + add_next_index_string(flags, "unique_key", 1); + } + if (IS_BLOB(F->flags)) { + add_next_index_string(flags, "blob", 1); + } + str = type_to_name_native(F->flags); + if (str) { + add_assoc_string(return_value, "native_type", str, 1); + } + + add_assoc_zval(return_value, "flags", flags); + return SUCCESS; +} + +struct pdo_stmt_methods mysql_stmt_methods = { + pdo_mysql_stmt_dtor, + pdo_mysql_stmt_execute, + pdo_mysql_stmt_fetch, + pdo_mysql_stmt_describe, + pdo_mysql_stmt_get_col, + pdo_mysql_stmt_param_hook, + NULL, /* set_attr */ + NULL, /* get_attr */ + pdo_mysql_stmt_col_meta +}; + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ http://cvs.php.net/diff.php/pecl/pdo_mysql/php_pdo_mysql_int.h?r1=1.9&r2=1.10&ty=u Index: pecl/pdo_mysql/php_pdo_mysql_int.h diff -u pecl/pdo_mysql/php_pdo_mysql_int.h:1.9 pecl/pdo_mysql/php_pdo_mysql_int.h:1.10 --- pecl/pdo_mysql/php_pdo_mysql_int.h:1.9 Thu May 20 12:17:37 2004 +++ pecl/pdo_mysql/php_pdo_mysql_int.h Sat Feb 12 19:48:00 2005 @@ -1,69 +1,69 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2004 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 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_0.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> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: php_pdo_mysql_int.h,v 1.9 2004/05/20 16:17:37 wez Exp $ */ - -#ifndef PHP_PDO_MYSQL_INT_H -#define PHP_PDO_MYSQL_INT_H - -#include <mysql.h> - -typedef struct { - const char *file; - int line; - unsigned int errcode; - char *errmsg; -} pdo_mysql_error_info; - -/* stuff we use in a mySQL database handle */ -typedef struct { - MYSQL *server; - - unsigned attached:1; - unsigned _reserved:31; - - pdo_mysql_error_info einfo; -} pdo_mysql_db_handle; - -typedef struct { - MYSQL_FIELD *def; -} pdo_mysql_column; - -typedef struct { - pdo_mysql_db_handle *H; - MYSQL_RES *result; - MYSQL_ROW current_data; - long *current_lengths; - pdo_mysql_column *cols; -} pdo_mysql_stmt; - -typedef struct { - char *repr; - long repr_len; - int mysql_type; - void *thing; /* for LOBS, REFCURSORS etc. */ -} pdo_mysql_bound_param; - -extern pdo_driver_t pdo_mysql_driver; - -extern int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC); -#define pdo_mysql_error(s) _pdo_mysql_error(s, NULL, __FILE__, __LINE__ TSRMLS_CC) -#define pdo_mysql_error_stmt(s) _pdo_mysql_error(stmt->dbh, stmt, __FILE__, __LINE__ TSRMLS_CC) - -extern struct pdo_stmt_methods mysql_stmt_methods; -#endif +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2004 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 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_0.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> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: php_pdo_mysql_int.h,v 1.10 2005/02/13 00:48:00 hholzgra Exp $ */ + +#ifndef PHP_PDO_MYSQL_INT_H +#define PHP_PDO_MYSQL_INT_H + +#include <mysql.h> + +typedef struct { + const char *file; + int line; + unsigned int errcode; + char *errmsg; +} pdo_mysql_error_info; + +/* stuff we use in a mySQL database handle */ +typedef struct { + MYSQL *server; + + unsigned attached:1; + unsigned _reserved:31; + + pdo_mysql_error_info einfo; +} pdo_mysql_db_handle; + +typedef struct { + MYSQL_FIELD *def; +} pdo_mysql_column; + +typedef struct { + pdo_mysql_db_handle *H; + MYSQL_RES *result; + MYSQL_ROW current_data; + long *current_lengths; + pdo_mysql_column *cols; +} pdo_mysql_stmt; + +typedef struct { + char *repr; + long repr_len; + int mysql_type; + void *thing; /* for LOBS, REFCURSORS etc. */ +} pdo_mysql_bound_param; + +extern pdo_driver_t pdo_mysql_driver; + +extern int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC); +#define pdo_mysql_error(s) _pdo_mysql_error(s, NULL, __FILE__, __LINE__ TSRMLS_CC) +#define pdo_mysql_error_stmt(s) _pdo_mysql_error(stmt->dbh, stmt, __FILE__, __LINE__ TSRMLS_CC) + +extern struct pdo_stmt_methods mysql_stmt_methods; +#endif
« previous php.pecl.cvs (#2521) next »