cvs: pecl /pdo_pgsql pgsql_driver.c pgsql_statement.c php_pdo_pgsql_int.h
| From: | Edin Kadribasic | Date: | Sun, 13 Feb 2005 01:02:20 +0000 |
| Subject: | cvs: pecl /pdo_pgsql pgsql_driver.c pgsql_statement.c php_pdo_pgsql_int.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-2525@lists.php.net to get a copy of this message | ||
edink Sat Feb 12 20:02:20 2005 EDT
Modified files:
/pecl/pdo_pgsql pgsql_driver.c pgsql_statement.c
php_pdo_pgsql_int.h
Log:
Unix line endings
http://cvs.php.net/diff.php/pecl/pdo_pgsql/pgsql_driver.c?r1=1.34&r2=1.35&ty=u Index: pecl/pdo_pgsql/pgsql_driver.c diff -u pecl/pdo_pgsql/pgsql_driver.c:1.34 pecl/pdo_pgsql/pgsql_driver.c:1.35 --- pecl/pdo_pgsql/pgsql_driver.c:1.34 Sat Feb 12 15:55:28 2005 +++ pecl/pdo_pgsql/pgsql_driver.c Sat Feb 12 20:02:18 2005 @@ -1,412 +1,412 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: pgsql_driver.c,v 1.34 2005/02/12 20:55:28 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 "pg_config.h" /* needed for PG_VERSION */ -#include "php_pdo_pgsql.h" -#include "php_pdo_pgsql_int.h" -#include "zend_exceptions.h" - -static char * _pdo_pgsql_trim_message(const char *message, int persistent) -{ - register int i = strlen(message)-1; - char *tmp; - - if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { - --i; - } - while (i>0 && (message[i] == '\r' || message[i] == '\n')) { - --i; - } - ++i; - tmp = pemalloc(i + 1, persistent); - memcpy(tmp, message, i); - tmp[i] = '\0'; - - return tmp; -} - -int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; - pdo_pgsql_error_info *einfo = &H->einfo; - char *errmsg = PQerrorMessage(H->server); - - einfo->errcode = errcode; - einfo->file = file; - einfo->line = line; - - if (einfo->errmsg) { - pefree(einfo->errmsg, dbh->is_persistent); - einfo->errmsg = NULL; - } - - switch (errcode) { - default: - strcpy(*pdo_err, "HY000"); - break; - } - - if (errmsg) { - einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent); - } - - 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 errcode; -} -/* }}} */ - -static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_pgsql_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 pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - if (H) { - if (H->server) { - PQfinish(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 pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt)); - int scrollable; - - S->H = H; - stmt->driver_data = S; - stmt->methods = &pgsql_stmt_methods; - stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; - - scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, - PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL; - - if (scrollable) { - spprintf(&S->cursor_name, 0, "pdo_pgsql_cursor_%08x", (unsigned int) stmt); - } - - return 1; -} - -static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - PGresult *res; - - if (!(res = PQexec(H->server, sql))) { - /* fatal error */ - pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); - return 0; - } else { - ExecStatusType qs = PQresultStatus(res); - if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) { - pdo_pgsql_error(dbh, qs); - PQclear(res); - return 0; - } - H->pgoid = PQoidValue(res); - PQclear(res); - } - - return 1; -} - -static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) -{ - unsigned char *escaped; - - switch (paramtype) { - case PDO_PARAM_LOB: - /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */ - escaped = PQescapeBytea(unquoted, unquotedlen, quotedlen); - *quotedlen += 1; - *quoted = emalloc(*quotedlen + 1); - memcpy((*quoted)+1, escaped, *quotedlen-2); - (*quoted)[0] = '\''; - (*quoted)[*quotedlen-1] = '\''; - (*quoted)[*quotedlen] = '\0'; - free(escaped); - break; - default: - *quoted = emalloc(2*unquotedlen + 3); - (*quoted)[0] = '\''; - *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen); - (*quoted)[*quotedlen + 1] = '\''; - (*quoted)[*quotedlen + 2] = '\0'; - *quotedlen += 2; - } - return 1; -} - -static long pdo_pgsql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - - if (H->pgoid == InvalidOid) { - return -1; - } - - return (long) H->pgoid; -} - -static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - - switch (attr) { - case PDO_ATTR_CLIENT_VERSION: - ZVAL_STRING(return_value, PG_VERSION, 1); - break; - - case PDO_ATTR_SERVER_VERSION: -#ifdef HAVE_PQPROTOCOLVERSION - if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */ - ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1); - } else /* emulate above via a query */ -#endif - { - PGresult *res = PQexec(H->server, "SELECT VERSION()"); - if (res && PQresultStatus(res) == PGRES_TUPLES_OK) { - ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1); - } - - if (res) { - PQclear(res); - } - } - break; - - case PDO_ATTR_CONNECTION_STATUS: - switch (PQstatus(H->server)) { - case CONNECTION_STARTED: - ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1); - break; - - case CONNECTION_MADE: - case CONNECTION_OK: - ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1); - break; - - case CONNECTION_AWAITING_RESPONSE: - ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1); - break; - - case CONNECTION_AUTH_OK: - ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1); - break; -#ifdef CONNECTION_SSL_STARTUP - case CONNECTION_SSL_STARTUP: - ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1); - break; -#endif - case CONNECTION_SETENV: - ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1); - break; - - case CONNECTION_BAD: - default: - ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1); - break; - } - break; - - case PDO_ATTR_SERVER_INFO: { - int spid = PQbackendPID(H->server); - char *tmp; -#ifdef HAVE_PQPROTOCOLVERSION - spprintf(&tmp, 0, - "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s", - spid, - (char*)PQparameterStatus(H->server, "client_encoding"), - (char*)PQparameterStatus(H->server, "is_superuser"), - (char*)PQparameterStatus(H->server, "session_authorization"), - (char*)PQparameterStatus(H->server, "DateStyle")); -#else - spprintf(&tmp, 0, "PID: %d", spid); -#endif - ZVAL_STRING(return_value, tmp, 0); - } - break; - - default: - return 0; - } - - return 1; -} - -static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - PGresult *res; - int ret = 1; - - res = PQexec(H->server, cmd); - - if (PQresultStatus(res) != PGRES_COMMAND_OK) { - ret = 0; - } - - PQclear(res); - return ret; -} - -static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC); -} - -static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC); -} - -static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC); -} - -static struct pdo_dbh_methods pgsql_methods = { - pgsql_handle_closer, - pgsql_handle_preparer, - pgsql_handle_doer, - pgsql_handle_quoter, - pgsql_handle_begin, - pgsql_handle_commit, - pgsql_handle_rollback, - NULL, /* set_attr */ - pdo_pgsql_last_insert_id, - pdo_pgsql_fetch_error_func, - pdo_pgsql_get_attribute, - NULL, /* check_liveness */ - NULL /* get_driver_methods */ -}; - -static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H; - int ret = 0; - char *conn_str, *p, *e; - - H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent); - dbh->driver_data = H; - - H->einfo.errcode = 0; - H->einfo.errmsg = NULL; - - /* PostgreSQL wants params in the connect string to be separated by spaces, - * if the PDO standard semicolons are used, we convert them to spaces - */ - e = (char *) dbh->data_source + strlen(dbh->data_source); - p = (char *) dbh->data_source; - while ((p = memchr(p, ';', (e - p)))) { - *p = ' '; - } - - /* support both full connection string & connection string + login and/or password */ - if (!dbh->username || !dbh->password) { - conn_str = (char *) dbh->data_source; - } else if (dbh->username && dbh->password) { - spprintf(&conn_str, 0, "%s user=%s password=%s", dbh->data_source, dbh->username, dbh->password); - } else if (dbh->username) { - spprintf(&conn_str, 0, "%s user=%s", dbh->data_source, dbh->username); - } else { - spprintf(&conn_str, 0, "%s password=%s", dbh->data_source, dbh->password); - } - - H->server = PQconnectdb(conn_str); - - if (conn_str != dbh->data_source) { - efree(conn_str); - } - - if (PQstatus(H->server) != CONNECTION_OK) { - pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); - goto cleanup; - } - - H->attached = 1; - H->pgoid = -1; - - dbh->methods = &pgsql_methods; - dbh->alloc_own_columns = 1; - dbh->max_escaped_char_length = 2; - - ret = 1; - -cleanup: - dbh->methods = &pgsql_methods; - if (!ret) { - pgsql_handle_closer(dbh TSRMLS_CC); - } - - return ret; -} -/* }}} */ - -pdo_driver_t pdo_pgsql_driver = { - PDO_DRIVER_HEADER(pgsql), - pdo_pgsql_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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: pgsql_driver.c,v 1.35 2005/02/13 01:02:18 edink 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 "pg_config.h" /* needed for PG_VERSION */ +#include "php_pdo_pgsql.h" +#include "php_pdo_pgsql_int.h" +#include "zend_exceptions.h" + +static char * _pdo_pgsql_trim_message(const char *message, int persistent) +{ + register int i = strlen(message)-1; + char *tmp; + + if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { + --i; + } + while (i>0 && (message[i] == '\r' || message[i] == '\n')) { + --i; + } + ++i; + tmp = pemalloc(i + 1, persistent); + memcpy(tmp, message, i); + tmp[i] = '\0'; + + return tmp; +} + +int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; + pdo_pgsql_error_info *einfo = &H->einfo; + char *errmsg = PQerrorMessage(H->server); + + einfo->errcode = errcode; + einfo->file = file; + einfo->line = line; + + if (einfo->errmsg) { + pefree(einfo->errmsg, dbh->is_persistent); + einfo->errmsg = NULL; + } + + switch (errcode) { + default: + strcpy(*pdo_err, "HY000"); + break; + } + + if (errmsg) { + einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent); + } + + 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 errcode; +} +/* }}} */ + +static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_pgsql_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 pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + if (H) { + if (H->server) { + PQfinish(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 pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt)); + int scrollable; + + S->H = H; + stmt->driver_data = S; + stmt->methods = &pgsql_stmt_methods; + stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; + + scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, + PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL; + + if (scrollable) { + spprintf(&S->cursor_name, 0, "pdo_pgsql_cursor_%08x", (unsigned int) stmt); + } + + return 1; +} + +static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + PGresult *res; + + if (!(res = PQexec(H->server, sql))) { + /* fatal error */ + pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); + return 0; + } else { + ExecStatusType qs = PQresultStatus(res); + if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) { + pdo_pgsql_error(dbh, qs); + PQclear(res); + return 0; + } + H->pgoid = PQoidValue(res); + PQclear(res); + } + + return 1; +} + +static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) +{ + unsigned char *escaped; + + switch (paramtype) { + case PDO_PARAM_LOB: + /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */ + escaped = PQescapeBytea(unquoted, unquotedlen, quotedlen); + *quotedlen += 1; + *quoted = emalloc(*quotedlen + 1); + memcpy((*quoted)+1, escaped, *quotedlen-2); + (*quoted)[0] = '\''; + (*quoted)[*quotedlen-1] = '\''; + (*quoted)[*quotedlen] = '\0'; + free(escaped); + break; + default: + *quoted = emalloc(2*unquotedlen + 3); + (*quoted)[0] = '\''; + *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen); + (*quoted)[*quotedlen + 1] = '\''; + (*quoted)[*quotedlen + 2] = '\0'; + *quotedlen += 2; + } + return 1; +} + +static long pdo_pgsql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + + if (H->pgoid == InvalidOid) { + return -1; + } + + return (long) H->pgoid; +} + +static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + + switch (attr) { + case PDO_ATTR_CLIENT_VERSION: + ZVAL_STRING(return_value, PG_VERSION, 1); + break; + + case PDO_ATTR_SERVER_VERSION: +#ifdef HAVE_PQPROTOCOLVERSION + if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */ + ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1); + } else /* emulate above via a query */ +#endif + { + PGresult *res = PQexec(H->server, "SELECT VERSION()"); + if (res && PQresultStatus(res) == PGRES_TUPLES_OK) { + ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1); + } + + if (res) { + PQclear(res); + } + } + break; + + case PDO_ATTR_CONNECTION_STATUS: + switch (PQstatus(H->server)) { + case CONNECTION_STARTED: + ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1); + break; + + case CONNECTION_MADE: + case CONNECTION_OK: + ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1); + break; + + case CONNECTION_AWAITING_RESPONSE: + ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1); + break; + + case CONNECTION_AUTH_OK: + ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1); + break; +#ifdef CONNECTION_SSL_STARTUP + case CONNECTION_SSL_STARTUP: + ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1); + break; +#endif + case CONNECTION_SETENV: + ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1); + break; + + case CONNECTION_BAD: + default: + ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1); + break; + } + break; + + case PDO_ATTR_SERVER_INFO: { + int spid = PQbackendPID(H->server); + char *tmp; +#ifdef HAVE_PQPROTOCOLVERSION + spprintf(&tmp, 0, + "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s", + spid, + (char*)PQparameterStatus(H->server, "client_encoding"), + (char*)PQparameterStatus(H->server, "is_superuser"), + (char*)PQparameterStatus(H->server, "session_authorization"), + (char*)PQparameterStatus(H->server, "DateStyle")); +#else + spprintf(&tmp, 0, "PID: %d", spid); +#endif + ZVAL_STRING(return_value, tmp, 0); + } + break; + + default: + return 0; + } + + return 1; +} + +static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + PGresult *res; + int ret = 1; + + res = PQexec(H->server, cmd); + + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + ret = 0; + } + + PQclear(res); + return ret; +} + +static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC); +} + +static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC); +} + +static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC); +} + +static struct pdo_dbh_methods pgsql_methods = { + pgsql_handle_closer, + pgsql_handle_preparer, + pgsql_handle_doer, + pgsql_handle_quoter, + pgsql_handle_begin, + pgsql_handle_commit, + pgsql_handle_rollback, + NULL, /* set_attr */ + pdo_pgsql_last_insert_id, + pdo_pgsql_fetch_error_func, + pdo_pgsql_get_attribute, + NULL, /* check_liveness */ + NULL /* get_driver_methods */ +}; + +static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H; + int ret = 0; + char *conn_str, *p, *e; + + H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent); + dbh->driver_data = H; + + H->einfo.errcode = 0; + H->einfo.errmsg = NULL; + + /* PostgreSQL wants params in the connect string to be separated by spaces, + * if the PDO standard semicolons are used, we convert them to spaces + */ + e = (char *) dbh->data_source + strlen(dbh->data_source); + p = (char *) dbh->data_source; + while ((p = memchr(p, ';', (e - p)))) { + *p = ' '; + } + + /* support both full connection string & connection string + login and/or password */ + if (!dbh->username || !dbh->password) { + conn_str = (char *) dbh->data_source; + } else if (dbh->username && dbh->password) { + spprintf(&conn_str, 0, "%s user=%s password=%s", dbh->data_source, dbh->username, dbh->password); + } else if (dbh->username) { + spprintf(&conn_str, 0, "%s user=%s", dbh->data_source, dbh->username); + } else { + spprintf(&conn_str, 0, "%s password=%s", dbh->data_source, dbh->password); + } + + H->server = PQconnectdb(conn_str); + + if (conn_str != dbh->data_source) { + efree(conn_str); + } + + if (PQstatus(H->server) != CONNECTION_OK) { + pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); + goto cleanup; + } + + H->attached = 1; + H->pgoid = -1; + + dbh->methods = &pgsql_methods; + dbh->alloc_own_columns = 1; + dbh->max_escaped_char_length = 2; + + ret = 1; + +cleanup: + dbh->methods = &pgsql_methods; + if (!ret) { + pgsql_handle_closer(dbh TSRMLS_CC); + } + + return ret; +} +/* }}} */ + +pdo_driver_t pdo_pgsql_driver = { + PDO_DRIVER_HEADER(pgsql), + pdo_pgsql_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_pgsql/pgsql_statement.c?r1=1.16&r2=1.17&ty=u Index: pecl/pdo_pgsql/pgsql_statement.c diff -u pecl/pdo_pgsql/pgsql_statement.c:1.16 pecl/pdo_pgsql/pgsql_statement.c:1.17 --- pecl/pdo_pgsql/pgsql_statement.c:1.16 Sun Feb 6 19:04:20 2005 +++ pecl/pdo_pgsql/pgsql_statement.c Sat Feb 12 20:02:18 2005 @@ -1,320 +1,320 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: pgsql_statement.c,v 1.16 2005/02/07 00:04:20 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_pgsql.h" -#include "php_pdo_pgsql_int.h" - -/* from postgresql/src/include/catalog/pg_type.h */ -#define BOOLOID 16 -#define BYTEAOID 17 -#define INT8OID 20 -#define INT2OID 21 -#define INT4OID 23 -#define OIDOID 26 - - -static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - - if (S->result) { - /* free the resource */ - PQclear(S->result); - S->result = NULL; - } - - if (S->cursor_name) { - pdo_pgsql_db_handle *H = S->H; - char *q = NULL; - PGresult *res; - - spprintf(&q, 0, "CLOSE %s", S->cursor_name); - res = PQexec(H->server, q); - efree(q); - if (res) PQclear(res); - efree(S->cursor_name); - S->cursor_name = NULL; - } - - if(S->cols) { - efree(S->cols); - S->cols = NULL; - } - efree(S); - return 1; -} - -static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) -{ - pdo_dbh_t *dbh = stmt->dbh; - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - pdo_pgsql_db_handle *H = S->H; - ExecStatusType status; - - if (stmt->executed) { - /* ensure that we free any previous unfetched results */ - if(S->result) { - PQclear(S->result); - S->result = NULL; - } - } - - if (S->cursor_name) { - char *q = NULL; - spprintf(&q, 0, "DECLARE %s FOR %s", S->cursor_name, stmt->active_query_string); - S->result = PQexec(H->server, q); - efree(q); - } else { - S->result = PQexec(H->server, stmt->active_query_string); - } - status = PQresultStatus(S->result); - - if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { - pdo_pgsql_error_stmt(stmt, status); - return 0; - } - - if(!stmt->executed) { - stmt->column_count = (int) PQnfields(S->result); - S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column)); - } - - if (status == PGRES_COMMAND_OK) { - stmt->row_count = (long)atoi(PQcmdTuples(S->result)); - H->pgoid = PQoidValue(S->result); - } else { - stmt->row_count = (long)PQntuples(S->result); - } - - return 1; -} - -static int pgsql_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 pgsql_stmt_fetch(pdo_stmt_t *stmt, - enum pdo_fetch_orientation ori, long offset TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - - if (S->cursor_name) { - char *ori_str = NULL; - char *q = NULL; - ExecStatusType status; - - switch (ori) { - case PDO_FETCH_ORI_NEXT: ori_str = "NEXT"; break; - case PDO_FETCH_ORI_PRIOR: ori_str = "PRIOR"; break; - case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break; - } - if (!ori_str) { - return 0; - } - - spprintf(&q, 0, "FETCH %s %d FROM %s", ori_str, offset, S->cursor_name); - S->result = PQexec(S->H->server, q); - status = PQresultStatus(S->result); - - if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { - pdo_pgsql_error_stmt(stmt, status); - return 0; - } - - S->current_row = 1; - return 1; - - } else { - if (S->current_row < stmt->row_count) { - S->current_row++; - return 1; - } else { - return 0; - } - } -} - -static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - struct pdo_column_data *cols = stmt->columns; - - if (!S->result) { - return 0; - } - - cols[colno].name = estrdup(PQfname(S->result, colno)); - cols[colno].namelen = strlen(cols[colno].name); - cols[colno].maxlen = PQfsize(S->result, colno); - cols[colno].precision = PQfmod(S->result, colno); - S->cols[colno].pgsql_type = PQftype(S->result, colno); - - switch(S->cols[colno].pgsql_type) { - - case BOOLOID: - cols[colno].param_type = PDO_PARAM_BOOL; - break; - - case INT2OID: - case INT4OID: - case OIDOID: - cols[colno].param_type = PDO_PARAM_INT; - break; - - case INT8OID: - if (sizeof(long)>=8) { - cols[colno].param_type = PDO_PARAM_INT; - } - break; - - case BYTEAOID: - cols[colno].param_type = PDO_PARAM_LOB; - break; - - default: - cols[colno].param_type = PDO_PARAM_STR; - } - - return 1; -} - -static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - struct pdo_column_data *cols = stmt->columns; - char *tmp_ptr; - size_t tmp_len; - - if (!S->result) { - return 0; - } - - /* We have already increased count by 1 in pgsql_stmt_fetch() */ - if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */ - *ptr = NULL; - *len = 0; - } else { - *ptr = PQgetvalue(S->result, S->current_row - 1, colno); - *len = PQgetlength(S->result, S->current_row - 1, colno); - - switch(cols[colno].param_type) { - - case PDO_PARAM_INT: - S->cols[colno].intval = atol(*ptr); - *ptr = (char *) &(S->cols[colno].intval); - *len = sizeof(long); - break; - - case PDO_PARAM_BOOL: - S->cols[colno].boolval = **ptr == 't' ? 1: 0; - *ptr = (char *) &(S->cols[colno].boolval); - *len = sizeof(zend_bool); - break; - - case PDO_PARAM_LOB: - tmp_ptr = PQunescapeBytea(*ptr, &tmp_len); - *ptr = estrndup(tmp_ptr, tmp_len); - *len = tmp_len; - *caller_frees = 1; - free(tmp_ptr); - break; - } - } - - return 1; -} - -static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - PGresult *res; - char *q=NULL; - ExecStatusType status; - - if (!S->result) { - return FAILURE; - } - - if (colno >= stmt->column_count) { - return FAILURE; - } - - array_init(return_value); - add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type); - - /* Fetch metadata from Postgres system catalogue */ - spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type); - res = PQexec(S->H->server, q); - efree(q); - - status = PQresultStatus(res); - - if (status != PGRES_TUPLES_OK) { - /* Failed to get system catalogue, but return success - * with the data we have collected so far - */ - PQclear(res); - return 1; - } - - /* We want exactly one row returned */ - if (1 != PQntuples(res)) { - PQclear(res); - return 1; - } - - add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1); - - PQclear(res); - return 1; -} - -struct pdo_stmt_methods pgsql_stmt_methods = { - pgsql_stmt_dtor, - pgsql_stmt_execute, - pgsql_stmt_fetch, - pgsql_stmt_describe, - pgsql_stmt_get_col, - pgsql_stmt_param_hook, - NULL, /* set_attr */ - NULL, /* get_attr */ - pgsql_stmt_get_column_meta, - NULL /* next_rowset */ -}; - -/* - * 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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: pgsql_statement.c,v 1.17 2005/02/13 01:02:18 edink 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_pgsql.h" +#include "php_pdo_pgsql_int.h" + +/* from postgresql/src/include/catalog/pg_type.h */ +#define BOOLOID 16 +#define BYTEAOID 17 +#define INT8OID 20 +#define INT2OID 21 +#define INT4OID 23 +#define OIDOID 26 + + +static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + + if (S->result) { + /* free the resource */ + PQclear(S->result); + S->result = NULL; + } + + if (S->cursor_name) { + pdo_pgsql_db_handle *H = S->H; + char *q = NULL; + PGresult *res; + + spprintf(&q, 0, "CLOSE %s", S->cursor_name); + res = PQexec(H->server, q); + efree(q); + if (res) PQclear(res); + efree(S->cursor_name); + S->cursor_name = NULL; + } + + if(S->cols) { + efree(S->cols); + S->cols = NULL; + } + efree(S); + return 1; +} + +static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_dbh_t *dbh = stmt->dbh; + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + pdo_pgsql_db_handle *H = S->H; + ExecStatusType status; + + if (stmt->executed) { + /* ensure that we free any previous unfetched results */ + if(S->result) { + PQclear(S->result); + S->result = NULL; + } + } + + if (S->cursor_name) { + char *q = NULL; + spprintf(&q, 0, "DECLARE %s FOR %s", S->cursor_name, stmt->active_query_string); + S->result = PQexec(H->server, q); + efree(q); + } else { + S->result = PQexec(H->server, stmt->active_query_string); + } + status = PQresultStatus(S->result); + + if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { + pdo_pgsql_error_stmt(stmt, status); + return 0; + } + + if(!stmt->executed) { + stmt->column_count = (int) PQnfields(S->result); + S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column)); + } + + if (status == PGRES_COMMAND_OK) { + stmt->row_count = (long)atoi(PQcmdTuples(S->result)); + H->pgoid = PQoidValue(S->result); + } else { + stmt->row_count = (long)PQntuples(S->result); + } + + return 1; +} + +static int pgsql_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 pgsql_stmt_fetch(pdo_stmt_t *stmt, + enum pdo_fetch_orientation ori, long offset TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + + if (S->cursor_name) { + char *ori_str = NULL; + char *q = NULL; + ExecStatusType status; + + switch (ori) { + case PDO_FETCH_ORI_NEXT: ori_str = "NEXT"; break; + case PDO_FETCH_ORI_PRIOR: ori_str = "PRIOR"; break; + case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break; + } + if (!ori_str) { + return 0; + } + + spprintf(&q, 0, "FETCH %s %d FROM %s", ori_str, offset, S->cursor_name); + S->result = PQexec(S->H->server, q); + status = PQresultStatus(S->result); + + if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { + pdo_pgsql_error_stmt(stmt, status); + return 0; + } + + S->current_row = 1; + return 1; + + } else { + if (S->current_row < stmt->row_count) { + S->current_row++; + return 1; + } else { + return 0; + } + } +} + +static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + struct pdo_column_data *cols = stmt->columns; + + if (!S->result) { + return 0; + } + + cols[colno].name = estrdup(PQfname(S->result, colno)); + cols[colno].namelen = strlen(cols[colno].name); + cols[colno].maxlen = PQfsize(S->result, colno); + cols[colno].precision = PQfmod(S->result, colno); + S->cols[colno].pgsql_type = PQftype(S->result, colno); + + switch(S->cols[colno].pgsql_type) { + + case BOOLOID: + cols[colno].param_type = PDO_PARAM_BOOL; + break; + + case INT2OID: + case INT4OID: + case OIDOID: + cols[colno].param_type = PDO_PARAM_INT; + break; + + case INT8OID: + if (sizeof(long)>=8) { + cols[colno].param_type = PDO_PARAM_INT; + } + break; + + case BYTEAOID: + cols[colno].param_type = PDO_PARAM_LOB; + break; + + default: + cols[colno].param_type = PDO_PARAM_STR; + } + + return 1; +} + +static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + struct pdo_column_data *cols = stmt->columns; + char *tmp_ptr; + size_t tmp_len; + + if (!S->result) { + return 0; + } + + /* We have already increased count by 1 in pgsql_stmt_fetch() */ + if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */ + *ptr = NULL; + *len = 0; + } else { + *ptr = PQgetvalue(S->result, S->current_row - 1, colno); + *len = PQgetlength(S->result, S->current_row - 1, colno); + + switch(cols[colno].param_type) { + + case PDO_PARAM_INT: + S->cols[colno].intval = atol(*ptr); + *ptr = (char *) &(S->cols[colno].intval); + *len = sizeof(long); + break; + + case PDO_PARAM_BOOL: + S->cols[colno].boolval = **ptr == 't' ? 1: 0; + *ptr = (char *) &(S->cols[colno].boolval); + *len = sizeof(zend_bool); + break; + + case PDO_PARAM_LOB: + tmp_ptr = PQunescapeBytea(*ptr, &tmp_len); + *ptr = estrndup(tmp_ptr, tmp_len); + *len = tmp_len; + *caller_frees = 1; + free(tmp_ptr); + break; + } + } + + return 1; +} + +static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + PGresult *res; + char *q=NULL; + ExecStatusType status; + + if (!S->result) { + return FAILURE; + } + + if (colno >= stmt->column_count) { + return FAILURE; + } + + array_init(return_value); + add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type); + + /* Fetch metadata from Postgres system catalogue */ + spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type); + res = PQexec(S->H->server, q); + efree(q); + + status = PQresultStatus(res); + + if (status != PGRES_TUPLES_OK) { + /* Failed to get system catalogue, but return success + * with the data we have collected so far + */ + PQclear(res); + return 1; + } + + /* We want exactly one row returned */ + if (1 != PQntuples(res)) { + PQclear(res); + return 1; + } + + add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1); + + PQclear(res); + return 1; +} + +struct pdo_stmt_methods pgsql_stmt_methods = { + pgsql_stmt_dtor, + pgsql_stmt_execute, + pgsql_stmt_fetch, + pgsql_stmt_describe, + pgsql_stmt_get_col, + pgsql_stmt_param_hook, + NULL, /* set_attr */ + NULL, /* get_attr */ + pgsql_stmt_get_column_meta, + NULL /* next_rowset */ +}; + +/* + * 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_pgsql/php_pdo_pgsql_int.h?r1=1.7&r2=1.8&ty=u Index: pecl/pdo_pgsql/php_pdo_pgsql_int.h diff -u pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.7 pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.8 --- pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.7 Sun Feb 6 17:48:35 2005 +++ pecl/pdo_pgsql/php_pdo_pgsql_int.h Sat Feb 12 20:02:18 2005 @@ -1,84 +1,84 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: php_pdo_pgsql_int.h,v 1.7 2005/02/06 22:48:35 edink Exp $ */ - -#ifndef PHP_PDO_PGSQL_INT_H -#define PHP_PDO_PGSQL_INT_H - -#if HAVE_PDO_PGSQL - -#include <libpq-fe.h> - -typedef struct { - const char *file; - int line; - unsigned int errcode; - char *errmsg; -} pdo_pgsql_error_info; - -/* stuff we use in a pgsql database handle */ -typedef struct { - PGconn *server; - unsigned attached:1; - unsigned _reserved:31; - pdo_pgsql_error_info einfo; - Oid pgoid; -} pdo_pgsql_db_handle; - -typedef struct { - char *def; - Oid pgsql_type; - long intval; - zend_bool boolval; -} pdo_pgsql_column; - -typedef struct { - pdo_pgsql_db_handle *H; - PGresult *result; - int current_row; - pdo_pgsql_column *cols; - char *cursor_name; -} pdo_pgsql_stmt; - -typedef struct { - char *repr; - long repr_len; - int pgsql_type; - void *thing; /* for LOBS, REFCURSORS etc. */ -} pdo_pgsql_bound_param; - -extern pdo_driver_t pdo_pgsql_driver; - -extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC); -#define pdo_pgsql_error(d,e) _pdo_pgsql_error(d, NULL, e, __FILE__, __LINE__ TSRMLS_CC) -#define pdo_pgsql_error_stmt(s,e) _pdo_pgsql_error(s->dbh, s, e, __FILE__, __LINE__ TSRMLS_CC) - -extern struct pdo_stmt_methods pgsql_stmt_methods; - -#endif /* HAVE_PDO_PGSQL */ -#endif /* PHP_PDO_PGSQL_INT_H */ - -/* - * 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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: php_pdo_pgsql_int.h,v 1.8 2005/02/13 01:02:18 edink Exp $ */ + +#ifndef PHP_PDO_PGSQL_INT_H +#define PHP_PDO_PGSQL_INT_H + +#if HAVE_PDO_PGSQL + +#include <libpq-fe.h> + +typedef struct { + const char *file; + int line; + unsigned int errcode; + char *errmsg; +} pdo_pgsql_error_info; + +/* stuff we use in a pgsql database handle */ +typedef struct { + PGconn *server; + unsigned attached:1; + unsigned _reserved:31; + pdo_pgsql_error_info einfo; + Oid pgoid; +} pdo_pgsql_db_handle; + +typedef struct { + char *def; + Oid pgsql_type; + long intval; + zend_bool boolval; +} pdo_pgsql_column; + +typedef struct { + pdo_pgsql_db_handle *H; + PGresult *result; + int current_row; + pdo_pgsql_column *cols; + char *cursor_name; +} pdo_pgsql_stmt; + +typedef struct { + char *repr; + long repr_len; + int pgsql_type; + void *thing; /* for LOBS, REFCURSORS etc. */ +} pdo_pgsql_bound_param; + +extern pdo_driver_t pdo_pgsql_driver; + +extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC); +#define pdo_pgsql_error(d,e) _pdo_pgsql_error(d, NULL, e, __FILE__, __LINE__ TSRMLS_CC) +#define pdo_pgsql_error_stmt(s,e) _pdo_pgsql_error(s->dbh, s, e, __FILE__, __LINE__ TSRMLS_CC) + +extern struct pdo_stmt_methods pgsql_stmt_methods; + +#endif /* HAVE_PDO_PGSQL */ +#endif /* PHP_PDO_PGSQL_INT_H */ + +/* + * 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_pgsql/pgsql_driver.c?r1=1.34&r2=1.35&ty=u Index: pecl/pdo_pgsql/pgsql_driver.c diff -u pecl/pdo_pgsql/pgsql_driver.c:1.34 pecl/pdo_pgsql/pgsql_driver.c:1.35 --- pecl/pdo_pgsql/pgsql_driver.c:1.34 Sat Feb 12 15:55:28 2005 +++ pecl/pdo_pgsql/pgsql_driver.c Sat Feb 12 20:02:18 2005 @@ -1,412 +1,412 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: pgsql_driver.c,v 1.34 2005/02/12 20:55:28 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 "pg_config.h" /* needed for PG_VERSION */ -#include "php_pdo_pgsql.h" -#include "php_pdo_pgsql_int.h" -#include "zend_exceptions.h" - -static char * _pdo_pgsql_trim_message(const char *message, int persistent) -{ - register int i = strlen(message)-1; - char *tmp; - - if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { - --i; - } - while (i>0 && (message[i] == '\r' || message[i] == '\n')) { - --i; - } - ++i; - tmp = pemalloc(i + 1, persistent); - memcpy(tmp, message, i); - tmp[i] = '\0'; - - return tmp; -} - -int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; - pdo_pgsql_error_info *einfo = &H->einfo; - char *errmsg = PQerrorMessage(H->server); - - einfo->errcode = errcode; - einfo->file = file; - einfo->line = line; - - if (einfo->errmsg) { - pefree(einfo->errmsg, dbh->is_persistent); - einfo->errmsg = NULL; - } - - switch (errcode) { - default: - strcpy(*pdo_err, "HY000"); - break; - } - - if (errmsg) { - einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent); - } - - 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 errcode; -} -/* }}} */ - -static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_pgsql_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 pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - if (H) { - if (H->server) { - PQfinish(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 pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt)); - int scrollable; - - S->H = H; - stmt->driver_data = S; - stmt->methods = &pgsql_stmt_methods; - stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; - - scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, - PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL; - - if (scrollable) { - spprintf(&S->cursor_name, 0, "pdo_pgsql_cursor_%08x", (unsigned int) stmt); - } - - return 1; -} - -static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - PGresult *res; - - if (!(res = PQexec(H->server, sql))) { - /* fatal error */ - pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); - return 0; - } else { - ExecStatusType qs = PQresultStatus(res); - if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) { - pdo_pgsql_error(dbh, qs); - PQclear(res); - return 0; - } - H->pgoid = PQoidValue(res); - PQclear(res); - } - - return 1; -} - -static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) -{ - unsigned char *escaped; - - switch (paramtype) { - case PDO_PARAM_LOB: - /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */ - escaped = PQescapeBytea(unquoted, unquotedlen, quotedlen); - *quotedlen += 1; - *quoted = emalloc(*quotedlen + 1); - memcpy((*quoted)+1, escaped, *quotedlen-2); - (*quoted)[0] = '\''; - (*quoted)[*quotedlen-1] = '\''; - (*quoted)[*quotedlen] = '\0'; - free(escaped); - break; - default: - *quoted = emalloc(2*unquotedlen + 3); - (*quoted)[0] = '\''; - *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen); - (*quoted)[*quotedlen + 1] = '\''; - (*quoted)[*quotedlen + 2] = '\0'; - *quotedlen += 2; - } - return 1; -} - -static long pdo_pgsql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - - if (H->pgoid == InvalidOid) { - return -1; - } - - return (long) H->pgoid; -} - -static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - - switch (attr) { - case PDO_ATTR_CLIENT_VERSION: - ZVAL_STRING(return_value, PG_VERSION, 1); - break; - - case PDO_ATTR_SERVER_VERSION: -#ifdef HAVE_PQPROTOCOLVERSION - if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */ - ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1); - } else /* emulate above via a query */ -#endif - { - PGresult *res = PQexec(H->server, "SELECT VERSION()"); - if (res && PQresultStatus(res) == PGRES_TUPLES_OK) { - ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1); - } - - if (res) { - PQclear(res); - } - } - break; - - case PDO_ATTR_CONNECTION_STATUS: - switch (PQstatus(H->server)) { - case CONNECTION_STARTED: - ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1); - break; - - case CONNECTION_MADE: - case CONNECTION_OK: - ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1); - break; - - case CONNECTION_AWAITING_RESPONSE: - ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1); - break; - - case CONNECTION_AUTH_OK: - ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1); - break; -#ifdef CONNECTION_SSL_STARTUP - case CONNECTION_SSL_STARTUP: - ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1); - break; -#endif - case CONNECTION_SETENV: - ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1); - break; - - case CONNECTION_BAD: - default: - ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1); - break; - } - break; - - case PDO_ATTR_SERVER_INFO: { - int spid = PQbackendPID(H->server); - char *tmp; -#ifdef HAVE_PQPROTOCOLVERSION - spprintf(&tmp, 0, - "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s", - spid, - (char*)PQparameterStatus(H->server, "client_encoding"), - (char*)PQparameterStatus(H->server, "is_superuser"), - (char*)PQparameterStatus(H->server, "session_authorization"), - (char*)PQparameterStatus(H->server, "DateStyle")); -#else - spprintf(&tmp, 0, "PID: %d", spid); -#endif - ZVAL_STRING(return_value, tmp, 0); - } - break; - - default: - return 0; - } - - return 1; -} - -static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC) -{ - pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; - PGresult *res; - int ret = 1; - - res = PQexec(H->server, cmd); - - if (PQresultStatus(res) != PGRES_COMMAND_OK) { - ret = 0; - } - - PQclear(res); - return ret; -} - -static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC); -} - -static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC); -} - -static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) -{ - return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC); -} - -static struct pdo_dbh_methods pgsql_methods = { - pgsql_handle_closer, - pgsql_handle_preparer, - pgsql_handle_doer, - pgsql_handle_quoter, - pgsql_handle_begin, - pgsql_handle_commit, - pgsql_handle_rollback, - NULL, /* set_attr */ - pdo_pgsql_last_insert_id, - pdo_pgsql_fetch_error_func, - pdo_pgsql_get_attribute, - NULL, /* check_liveness */ - NULL /* get_driver_methods */ -}; - -static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ -{ - pdo_pgsql_db_handle *H; - int ret = 0; - char *conn_str, *p, *e; - - H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent); - dbh->driver_data = H; - - H->einfo.errcode = 0; - H->einfo.errmsg = NULL; - - /* PostgreSQL wants params in the connect string to be separated by spaces, - * if the PDO standard semicolons are used, we convert them to spaces - */ - e = (char *) dbh->data_source + strlen(dbh->data_source); - p = (char *) dbh->data_source; - while ((p = memchr(p, ';', (e - p)))) { - *p = ' '; - } - - /* support both full connection string & connection string + login and/or password */ - if (!dbh->username || !dbh->password) { - conn_str = (char *) dbh->data_source; - } else if (dbh->username && dbh->password) { - spprintf(&conn_str, 0, "%s user=%s password=%s", dbh->data_source, dbh->username, dbh->password); - } else if (dbh->username) { - spprintf(&conn_str, 0, "%s user=%s", dbh->data_source, dbh->username); - } else { - spprintf(&conn_str, 0, "%s password=%s", dbh->data_source, dbh->password); - } - - H->server = PQconnectdb(conn_str); - - if (conn_str != dbh->data_source) { - efree(conn_str); - } - - if (PQstatus(H->server) != CONNECTION_OK) { - pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); - goto cleanup; - } - - H->attached = 1; - H->pgoid = -1; - - dbh->methods = &pgsql_methods; - dbh->alloc_own_columns = 1; - dbh->max_escaped_char_length = 2; - - ret = 1; - -cleanup: - dbh->methods = &pgsql_methods; - if (!ret) { - pgsql_handle_closer(dbh TSRMLS_CC); - } - - return ret; -} -/* }}} */ - -pdo_driver_t pdo_pgsql_driver = { - PDO_DRIVER_HEADER(pgsql), - pdo_pgsql_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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: pgsql_driver.c,v 1.35 2005/02/13 01:02:18 edink 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 "pg_config.h" /* needed for PG_VERSION */ +#include "php_pdo_pgsql.h" +#include "php_pdo_pgsql_int.h" +#include "zend_exceptions.h" + +static char * _pdo_pgsql_trim_message(const char *message, int persistent) +{ + register int i = strlen(message)-1; + char *tmp; + + if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { + --i; + } + while (i>0 && (message[i] == '\r' || message[i] == '\n')) { + --i; + } + ++i; + tmp = pemalloc(i + 1, persistent); + memcpy(tmp, message, i); + tmp[i] = '\0'; + + return tmp; +} + +int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code; + pdo_pgsql_error_info *einfo = &H->einfo; + char *errmsg = PQerrorMessage(H->server); + + einfo->errcode = errcode; + einfo->file = file; + einfo->line = line; + + if (einfo->errmsg) { + pefree(einfo->errmsg, dbh->is_persistent); + einfo->errmsg = NULL; + } + + switch (errcode) { + default: + strcpy(*pdo_err, "HY000"); + break; + } + + if (errmsg) { + einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent); + } + + 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 errcode; +} +/* }}} */ + +static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_pgsql_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 pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + if (H) { + if (H->server) { + PQfinish(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 pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt)); + int scrollable; + + S->H = H; + stmt->driver_data = S; + stmt->methods = &pgsql_stmt_methods; + stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; + + scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, + PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL; + + if (scrollable) { + spprintf(&S->cursor_name, 0, "pdo_pgsql_cursor_%08x", (unsigned int) stmt); + } + + return 1; +} + +static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + PGresult *res; + + if (!(res = PQexec(H->server, sql))) { + /* fatal error */ + pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); + return 0; + } else { + ExecStatusType qs = PQresultStatus(res); + if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) { + pdo_pgsql_error(dbh, qs); + PQclear(res); + return 0; + } + H->pgoid = PQoidValue(res); + PQclear(res); + } + + return 1; +} + +static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC) +{ + unsigned char *escaped; + + switch (paramtype) { + case PDO_PARAM_LOB: + /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */ + escaped = PQescapeBytea(unquoted, unquotedlen, quotedlen); + *quotedlen += 1; + *quoted = emalloc(*quotedlen + 1); + memcpy((*quoted)+1, escaped, *quotedlen-2); + (*quoted)[0] = '\''; + (*quoted)[*quotedlen-1] = '\''; + (*quoted)[*quotedlen] = '\0'; + free(escaped); + break; + default: + *quoted = emalloc(2*unquotedlen + 3); + (*quoted)[0] = '\''; + *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen); + (*quoted)[*quotedlen + 1] = '\''; + (*quoted)[*quotedlen + 2] = '\0'; + *quotedlen += 2; + } + return 1; +} + +static long pdo_pgsql_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + + if (H->pgoid == InvalidOid) { + return -1; + } + + return (long) H->pgoid; +} + +static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + + switch (attr) { + case PDO_ATTR_CLIENT_VERSION: + ZVAL_STRING(return_value, PG_VERSION, 1); + break; + + case PDO_ATTR_SERVER_VERSION: +#ifdef HAVE_PQPROTOCOLVERSION + if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */ + ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1); + } else /* emulate above via a query */ +#endif + { + PGresult *res = PQexec(H->server, "SELECT VERSION()"); + if (res && PQresultStatus(res) == PGRES_TUPLES_OK) { + ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1); + } + + if (res) { + PQclear(res); + } + } + break; + + case PDO_ATTR_CONNECTION_STATUS: + switch (PQstatus(H->server)) { + case CONNECTION_STARTED: + ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1); + break; + + case CONNECTION_MADE: + case CONNECTION_OK: + ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1); + break; + + case CONNECTION_AWAITING_RESPONSE: + ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1); + break; + + case CONNECTION_AUTH_OK: + ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1); + break; +#ifdef CONNECTION_SSL_STARTUP + case CONNECTION_SSL_STARTUP: + ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1); + break; +#endif + case CONNECTION_SETENV: + ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1); + break; + + case CONNECTION_BAD: + default: + ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1); + break; + } + break; + + case PDO_ATTR_SERVER_INFO: { + int spid = PQbackendPID(H->server); + char *tmp; +#ifdef HAVE_PQPROTOCOLVERSION + spprintf(&tmp, 0, + "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s", + spid, + (char*)PQparameterStatus(H->server, "client_encoding"), + (char*)PQparameterStatus(H->server, "is_superuser"), + (char*)PQparameterStatus(H->server, "session_authorization"), + (char*)PQparameterStatus(H->server, "DateStyle")); +#else + spprintf(&tmp, 0, "PID: %d", spid); +#endif + ZVAL_STRING(return_value, tmp, 0); + } + break; + + default: + return 0; + } + + return 1; +} + +static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; + PGresult *res; + int ret = 1; + + res = PQexec(H->server, cmd); + + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + ret = 0; + } + + PQclear(res); + return ret; +} + +static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC); +} + +static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC); +} + +static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) +{ + return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC); +} + +static struct pdo_dbh_methods pgsql_methods = { + pgsql_handle_closer, + pgsql_handle_preparer, + pgsql_handle_doer, + pgsql_handle_quoter, + pgsql_handle_begin, + pgsql_handle_commit, + pgsql_handle_rollback, + NULL, /* set_attr */ + pdo_pgsql_last_insert_id, + pdo_pgsql_fetch_error_func, + pdo_pgsql_get_attribute, + NULL, /* check_liveness */ + NULL /* get_driver_methods */ +}; + +static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */ +{ + pdo_pgsql_db_handle *H; + int ret = 0; + char *conn_str, *p, *e; + + H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent); + dbh->driver_data = H; + + H->einfo.errcode = 0; + H->einfo.errmsg = NULL; + + /* PostgreSQL wants params in the connect string to be separated by spaces, + * if the PDO standard semicolons are used, we convert them to spaces + */ + e = (char *) dbh->data_source + strlen(dbh->data_source); + p = (char *) dbh->data_source; + while ((p = memchr(p, ';', (e - p)))) { + *p = ' '; + } + + /* support both full connection string & connection string + login and/or password */ + if (!dbh->username || !dbh->password) { + conn_str = (char *) dbh->data_source; + } else if (dbh->username && dbh->password) { + spprintf(&conn_str, 0, "%s user=%s password=%s", dbh->data_source, dbh->username, dbh->password); + } else if (dbh->username) { + spprintf(&conn_str, 0, "%s user=%s", dbh->data_source, dbh->username); + } else { + spprintf(&conn_str, 0, "%s password=%s", dbh->data_source, dbh->password); + } + + H->server = PQconnectdb(conn_str); + + if (conn_str != dbh->data_source) { + efree(conn_str); + } + + if (PQstatus(H->server) != CONNECTION_OK) { + pdo_pgsql_error(dbh, PGRES_FATAL_ERROR); + goto cleanup; + } + + H->attached = 1; + H->pgoid = -1; + + dbh->methods = &pgsql_methods; + dbh->alloc_own_columns = 1; + dbh->max_escaped_char_length = 2; + + ret = 1; + +cleanup: + dbh->methods = &pgsql_methods; + if (!ret) { + pgsql_handle_closer(dbh TSRMLS_CC); + } + + return ret; +} +/* }}} */ + +pdo_driver_t pdo_pgsql_driver = { + PDO_DRIVER_HEADER(pgsql), + pdo_pgsql_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_pgsql/pgsql_statement.c?r1=1.16&r2=1.17&ty=u Index: pecl/pdo_pgsql/pgsql_statement.c diff -u pecl/pdo_pgsql/pgsql_statement.c:1.16 pecl/pdo_pgsql/pgsql_statement.c:1.17 --- pecl/pdo_pgsql/pgsql_statement.c:1.16 Sun Feb 6 19:04:20 2005 +++ pecl/pdo_pgsql/pgsql_statement.c Sat Feb 12 20:02:18 2005 @@ -1,320 +1,320 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: pgsql_statement.c,v 1.16 2005/02/07 00:04:20 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_pgsql.h" -#include "php_pdo_pgsql_int.h" - -/* from postgresql/src/include/catalog/pg_type.h */ -#define BOOLOID 16 -#define BYTEAOID 17 -#define INT8OID 20 -#define INT2OID 21 -#define INT4OID 23 -#define OIDOID 26 - - -static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - - if (S->result) { - /* free the resource */ - PQclear(S->result); - S->result = NULL; - } - - if (S->cursor_name) { - pdo_pgsql_db_handle *H = S->H; - char *q = NULL; - PGresult *res; - - spprintf(&q, 0, "CLOSE %s", S->cursor_name); - res = PQexec(H->server, q); - efree(q); - if (res) PQclear(res); - efree(S->cursor_name); - S->cursor_name = NULL; - } - - if(S->cols) { - efree(S->cols); - S->cols = NULL; - } - efree(S); - return 1; -} - -static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) -{ - pdo_dbh_t *dbh = stmt->dbh; - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - pdo_pgsql_db_handle *H = S->H; - ExecStatusType status; - - if (stmt->executed) { - /* ensure that we free any previous unfetched results */ - if(S->result) { - PQclear(S->result); - S->result = NULL; - } - } - - if (S->cursor_name) { - char *q = NULL; - spprintf(&q, 0, "DECLARE %s FOR %s", S->cursor_name, stmt->active_query_string); - S->result = PQexec(H->server, q); - efree(q); - } else { - S->result = PQexec(H->server, stmt->active_query_string); - } - status = PQresultStatus(S->result); - - if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { - pdo_pgsql_error_stmt(stmt, status); - return 0; - } - - if(!stmt->executed) { - stmt->column_count = (int) PQnfields(S->result); - S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column)); - } - - if (status == PGRES_COMMAND_OK) { - stmt->row_count = (long)atoi(PQcmdTuples(S->result)); - H->pgoid = PQoidValue(S->result); - } else { - stmt->row_count = (long)PQntuples(S->result); - } - - return 1; -} - -static int pgsql_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 pgsql_stmt_fetch(pdo_stmt_t *stmt, - enum pdo_fetch_orientation ori, long offset TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - - if (S->cursor_name) { - char *ori_str = NULL; - char *q = NULL; - ExecStatusType status; - - switch (ori) { - case PDO_FETCH_ORI_NEXT: ori_str = "NEXT"; break; - case PDO_FETCH_ORI_PRIOR: ori_str = "PRIOR"; break; - case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break; - } - if (!ori_str) { - return 0; - } - - spprintf(&q, 0, "FETCH %s %d FROM %s", ori_str, offset, S->cursor_name); - S->result = PQexec(S->H->server, q); - status = PQresultStatus(S->result); - - if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { - pdo_pgsql_error_stmt(stmt, status); - return 0; - } - - S->current_row = 1; - return 1; - - } else { - if (S->current_row < stmt->row_count) { - S->current_row++; - return 1; - } else { - return 0; - } - } -} - -static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - struct pdo_column_data *cols = stmt->columns; - - if (!S->result) { - return 0; - } - - cols[colno].name = estrdup(PQfname(S->result, colno)); - cols[colno].namelen = strlen(cols[colno].name); - cols[colno].maxlen = PQfsize(S->result, colno); - cols[colno].precision = PQfmod(S->result, colno); - S->cols[colno].pgsql_type = PQftype(S->result, colno); - - switch(S->cols[colno].pgsql_type) { - - case BOOLOID: - cols[colno].param_type = PDO_PARAM_BOOL; - break; - - case INT2OID: - case INT4OID: - case OIDOID: - cols[colno].param_type = PDO_PARAM_INT; - break; - - case INT8OID: - if (sizeof(long)>=8) { - cols[colno].param_type = PDO_PARAM_INT; - } - break; - - case BYTEAOID: - cols[colno].param_type = PDO_PARAM_LOB; - break; - - default: - cols[colno].param_type = PDO_PARAM_STR; - } - - return 1; -} - -static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - struct pdo_column_data *cols = stmt->columns; - char *tmp_ptr; - size_t tmp_len; - - if (!S->result) { - return 0; - } - - /* We have already increased count by 1 in pgsql_stmt_fetch() */ - if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */ - *ptr = NULL; - *len = 0; - } else { - *ptr = PQgetvalue(S->result, S->current_row - 1, colno); - *len = PQgetlength(S->result, S->current_row - 1, colno); - - switch(cols[colno].param_type) { - - case PDO_PARAM_INT: - S->cols[colno].intval = atol(*ptr); - *ptr = (char *) &(S->cols[colno].intval); - *len = sizeof(long); - break; - - case PDO_PARAM_BOOL: - S->cols[colno].boolval = **ptr == 't' ? 1: 0; - *ptr = (char *) &(S->cols[colno].boolval); - *len = sizeof(zend_bool); - break; - - case PDO_PARAM_LOB: - tmp_ptr = PQunescapeBytea(*ptr, &tmp_len); - *ptr = estrndup(tmp_ptr, tmp_len); - *len = tmp_len; - *caller_frees = 1; - free(tmp_ptr); - break; - } - } - - return 1; -} - -static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) -{ - pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; - PGresult *res; - char *q=NULL; - ExecStatusType status; - - if (!S->result) { - return FAILURE; - } - - if (colno >= stmt->column_count) { - return FAILURE; - } - - array_init(return_value); - add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type); - - /* Fetch metadata from Postgres system catalogue */ - spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type); - res = PQexec(S->H->server, q); - efree(q); - - status = PQresultStatus(res); - - if (status != PGRES_TUPLES_OK) { - /* Failed to get system catalogue, but return success - * with the data we have collected so far - */ - PQclear(res); - return 1; - } - - /* We want exactly one row returned */ - if (1 != PQntuples(res)) { - PQclear(res); - return 1; - } - - add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1); - - PQclear(res); - return 1; -} - -struct pdo_stmt_methods pgsql_stmt_methods = { - pgsql_stmt_dtor, - pgsql_stmt_execute, - pgsql_stmt_fetch, - pgsql_stmt_describe, - pgsql_stmt_get_col, - pgsql_stmt_param_hook, - NULL, /* set_attr */ - NULL, /* get_attr */ - pgsql_stmt_get_column_meta, - NULL /* next_rowset */ -}; - -/* - * 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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: pgsql_statement.c,v 1.17 2005/02/13 01:02:18 edink 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_pgsql.h" +#include "php_pdo_pgsql_int.h" + +/* from postgresql/src/include/catalog/pg_type.h */ +#define BOOLOID 16 +#define BYTEAOID 17 +#define INT8OID 20 +#define INT2OID 21 +#define INT4OID 23 +#define OIDOID 26 + + +static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + + if (S->result) { + /* free the resource */ + PQclear(S->result); + S->result = NULL; + } + + if (S->cursor_name) { + pdo_pgsql_db_handle *H = S->H; + char *q = NULL; + PGresult *res; + + spprintf(&q, 0, "CLOSE %s", S->cursor_name); + res = PQexec(H->server, q); + efree(q); + if (res) PQclear(res); + efree(S->cursor_name); + S->cursor_name = NULL; + } + + if(S->cols) { + efree(S->cols); + S->cols = NULL; + } + efree(S); + return 1; +} + +static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_dbh_t *dbh = stmt->dbh; + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + pdo_pgsql_db_handle *H = S->H; + ExecStatusType status; + + if (stmt->executed) { + /* ensure that we free any previous unfetched results */ + if(S->result) { + PQclear(S->result); + S->result = NULL; + } + } + + if (S->cursor_name) { + char *q = NULL; + spprintf(&q, 0, "DECLARE %s FOR %s", S->cursor_name, stmt->active_query_string); + S->result = PQexec(H->server, q); + efree(q); + } else { + S->result = PQexec(H->server, stmt->active_query_string); + } + status = PQresultStatus(S->result); + + if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { + pdo_pgsql_error_stmt(stmt, status); + return 0; + } + + if(!stmt->executed) { + stmt->column_count = (int) PQnfields(S->result); + S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column)); + } + + if (status == PGRES_COMMAND_OK) { + stmt->row_count = (long)atoi(PQcmdTuples(S->result)); + H->pgoid = PQoidValue(S->result); + } else { + stmt->row_count = (long)PQntuples(S->result); + } + + return 1; +} + +static int pgsql_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 pgsql_stmt_fetch(pdo_stmt_t *stmt, + enum pdo_fetch_orientation ori, long offset TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + + if (S->cursor_name) { + char *ori_str = NULL; + char *q = NULL; + ExecStatusType status; + + switch (ori) { + case PDO_FETCH_ORI_NEXT: ori_str = "NEXT"; break; + case PDO_FETCH_ORI_PRIOR: ori_str = "PRIOR"; break; + case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break; + } + if (!ori_str) { + return 0; + } + + spprintf(&q, 0, "FETCH %s %d FROM %s", ori_str, offset, S->cursor_name); + S->result = PQexec(S->H->server, q); + status = PQresultStatus(S->result); + + if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { + pdo_pgsql_error_stmt(stmt, status); + return 0; + } + + S->current_row = 1; + return 1; + + } else { + if (S->current_row < stmt->row_count) { + S->current_row++; + return 1; + } else { + return 0; + } + } +} + +static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + struct pdo_column_data *cols = stmt->columns; + + if (!S->result) { + return 0; + } + + cols[colno].name = estrdup(PQfname(S->result, colno)); + cols[colno].namelen = strlen(cols[colno].name); + cols[colno].maxlen = PQfsize(S->result, colno); + cols[colno].precision = PQfmod(S->result, colno); + S->cols[colno].pgsql_type = PQftype(S->result, colno); + + switch(S->cols[colno].pgsql_type) { + + case BOOLOID: + cols[colno].param_type = PDO_PARAM_BOOL; + break; + + case INT2OID: + case INT4OID: + case OIDOID: + cols[colno].param_type = PDO_PARAM_INT; + break; + + case INT8OID: + if (sizeof(long)>=8) { + cols[colno].param_type = PDO_PARAM_INT; + } + break; + + case BYTEAOID: + cols[colno].param_type = PDO_PARAM_LOB; + break; + + default: + cols[colno].param_type = PDO_PARAM_STR; + } + + return 1; +} + +static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + struct pdo_column_data *cols = stmt->columns; + char *tmp_ptr; + size_t tmp_len; + + if (!S->result) { + return 0; + } + + /* We have already increased count by 1 in pgsql_stmt_fetch() */ + if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */ + *ptr = NULL; + *len = 0; + } else { + *ptr = PQgetvalue(S->result, S->current_row - 1, colno); + *len = PQgetlength(S->result, S->current_row - 1, colno); + + switch(cols[colno].param_type) { + + case PDO_PARAM_INT: + S->cols[colno].intval = atol(*ptr); + *ptr = (char *) &(S->cols[colno].intval); + *len = sizeof(long); + break; + + case PDO_PARAM_BOOL: + S->cols[colno].boolval = **ptr == 't' ? 1: 0; + *ptr = (char *) &(S->cols[colno].boolval); + *len = sizeof(zend_bool); + break; + + case PDO_PARAM_LOB: + tmp_ptr = PQunescapeBytea(*ptr, &tmp_len); + *ptr = estrndup(tmp_ptr, tmp_len); + *len = tmp_len; + *caller_frees = 1; + free(tmp_ptr); + break; + } + } + + return 1; +} + +static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) +{ + pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data; + PGresult *res; + char *q=NULL; + ExecStatusType status; + + if (!S->result) { + return FAILURE; + } + + if (colno >= stmt->column_count) { + return FAILURE; + } + + array_init(return_value); + add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type); + + /* Fetch metadata from Postgres system catalogue */ + spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type); + res = PQexec(S->H->server, q); + efree(q); + + status = PQresultStatus(res); + + if (status != PGRES_TUPLES_OK) { + /* Failed to get system catalogue, but return success + * with the data we have collected so far + */ + PQclear(res); + return 1; + } + + /* We want exactly one row returned */ + if (1 != PQntuples(res)) { + PQclear(res); + return 1; + } + + add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1); + + PQclear(res); + return 1; +} + +struct pdo_stmt_methods pgsql_stmt_methods = { + pgsql_stmt_dtor, + pgsql_stmt_execute, + pgsql_stmt_fetch, + pgsql_stmt_describe, + pgsql_stmt_get_col, + pgsql_stmt_param_hook, + NULL, /* set_attr */ + NULL, /* get_attr */ + pgsql_stmt_get_column_meta, + NULL /* next_rowset */ +}; + +/* + * 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_pgsql/php_pdo_pgsql_int.h?r1=1.7&r2=1.8&ty=u Index: pecl/pdo_pgsql/php_pdo_pgsql_int.h diff -u pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.7 pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.8 --- pecl/pdo_pgsql/php_pdo_pgsql_int.h:1.7 Sun Feb 6 17:48:35 2005 +++ pecl/pdo_pgsql/php_pdo_pgsql_int.h Sat Feb 12 20:02:18 2005 @@ -1,84 +1,84 @@ -/* - +----------------------------------------------------------------------+ - | 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: Edin Kadribasic <edink@emini.dk> | - +----------------------------------------------------------------------+ -*/ - -/* $Id: php_pdo_pgsql_int.h,v 1.7 2005/02/06 22:48:35 edink Exp $ */ - -#ifndef PHP_PDO_PGSQL_INT_H -#define PHP_PDO_PGSQL_INT_H - -#if HAVE_PDO_PGSQL - -#include <libpq-fe.h> - -typedef struct { - const char *file; - int line; - unsigned int errcode; - char *errmsg; -} pdo_pgsql_error_info; - -/* stuff we use in a pgsql database handle */ -typedef struct { - PGconn *server; - unsigned attached:1; - unsigned _reserved:31; - pdo_pgsql_error_info einfo; - Oid pgoid; -} pdo_pgsql_db_handle; - -typedef struct { - char *def; - Oid pgsql_type; - long intval; - zend_bool boolval; -} pdo_pgsql_column; - -typedef struct { - pdo_pgsql_db_handle *H; - PGresult *result; - int current_row; - pdo_pgsql_column *cols; - char *cursor_name; -} pdo_pgsql_stmt; - -typedef struct { - char *repr; - long repr_len; - int pgsql_type; - void *thing; /* for LOBS, REFCURSORS etc. */ -} pdo_pgsql_bound_param; - -extern pdo_driver_t pdo_pgsql_driver; - -extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC); -#define pdo_pgsql_error(d,e) _pdo_pgsql_error(d, NULL, e, __FILE__, __LINE__ TSRMLS_CC) -#define pdo_pgsql_error_stmt(s,e) _pdo_pgsql_error(s->dbh, s, e, __FILE__, __LINE__ TSRMLS_CC) - -extern struct pdo_stmt_methods pgsql_stmt_methods; - -#endif /* HAVE_PDO_PGSQL */ -#endif /* PHP_PDO_PGSQL_INT_H */ - -/* - * 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: Edin Kadribasic <edink@emini.dk> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: php_pdo_pgsql_int.h,v 1.8 2005/02/13 01:02:18 edink Exp $ */ + +#ifndef PHP_PDO_PGSQL_INT_H +#define PHP_PDO_PGSQL_INT_H + +#if HAVE_PDO_PGSQL + +#include <libpq-fe.h> + +typedef struct { + const char *file; + int line; + unsigned int errcode; + char *errmsg; +} pdo_pgsql_error_info; + +/* stuff we use in a pgsql database handle */ +typedef struct { + PGconn *server; + unsigned attached:1; + unsigned _reserved:31; + pdo_pgsql_error_info einfo; + Oid pgoid; +} pdo_pgsql_db_handle; + +typedef struct { + char *def; + Oid pgsql_type; + long intval; + zend_bool boolval; +} pdo_pgsql_column; + +typedef struct { + pdo_pgsql_db_handle *H; + PGresult *result; + int current_row; + pdo_pgsql_column *cols; + char *cursor_name; +} pdo_pgsql_stmt; + +typedef struct { + char *repr; + long repr_len; + int pgsql_type; + void *thing; /* for LOBS, REFCURSORS etc. */ +} pdo_pgsql_bound_param; + +extern pdo_driver_t pdo_pgsql_driver; + +extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC); +#define pdo_pgsql_error(d,e) _pdo_pgsql_error(d, NULL, e, __FILE__, __LINE__ TSRMLS_CC) +#define pdo_pgsql_error_stmt(s,e) _pdo_pgsql_error(s->dbh, s, e, __FILE__, __LINE__ TSRMLS_CC) + +extern struct pdo_stmt_methods pgsql_stmt_methods; + +#endif /* HAVE_PDO_PGSQL */ +#endif /* PHP_PDO_PGSQL_INT_H */ + +/* + * 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 + */