php-src/ext/pdo/pdo_dbh.c

1117 lines
28 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
2005-01-12 04:49:12 +00:00
| Copyright (c) 1997-2005 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: Wez Furlong <wez@php.net> |
| Marcus Boerger <helly@php.net> |
| Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* The PDO Database Handle Class */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_pdo.h"
#include "php_pdo_driver.h"
#include "php_pdo_int.h"
#include "zend_exceptions.h"
#include "zend_object_handlers.h"
#include "zend_hash.h"
2005-02-06 21:05:59 +00:00
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp TSRMLS_DC)
{
pdo_error_type *pdo_err = &dbh->error_code;
char *message = NULL;
const char *msg;
zval *info = NULL;
if (dbh->error_mode == PDO_ERRMODE_SILENT) {
#if 0
/* BUG: if user is running in silent mode and hits an error at the driver level
* when they use the PDO methods to call up the error information, they may
* get bogus information */
return;
#endif
}
if (stmt) {
pdo_err = &stmt->error_code;
}
strcpy(*pdo_err, sqlstate);
/* hash sqlstate to error messages */
msg = pdo_sqlstate_state_to_description(*pdo_err);
if (!msg) {
msg = "<<Unknown error>>";
}
MAKE_STD_ZVAL(info);
array_init(info);
add_next_index_string(info, *pdo_err, 1);
add_next_index_long(info, 0);
if (supp) {
2005-02-09 15:12:41 +00:00
spprintf(&message, 0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
2005-02-06 21:05:59 +00:00
} else {
spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
}
if (dbh->error_mode != PDO_ERRMODE_EXCEPTION) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
if (info) {
zval_ptr_dtor(&info);
}
} else {
zval *ex;
zend_class_entry *def_ex = zend_exception_get_default(), *pdo_ex = php_pdo_get_exception();
MAKE_STD_ZVAL(ex);
object_init_ex(ex, pdo_ex);
zend_update_property_string(def_ex, ex, "message", sizeof("message")-1, message TSRMLS_CC);
zend_update_property_string(def_ex, ex, "code", sizeof("code")-1, *pdo_err TSRMLS_CC);
if (info) {
zend_update_property(pdo_ex, ex, "errorInfo", sizeof("errorInfo")-1, info TSRMLS_CC);
}
zend_throw_exception_object(ex TSRMLS_CC);
}
if (message) {
efree(message);
}
}
void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_error_type *pdo_err = &dbh->error_code;
const char *msg = "<<Unknown>>";
char *supp = NULL;
long native_code = 0;
char *message = NULL;
zval *info = NULL;
if (dbh->error_mode == PDO_ERRMODE_SILENT) {
return;
}
if (stmt) {
pdo_err = &stmt->error_code;
}
/* hash sqlstate to error messages */
msg = pdo_sqlstate_state_to_description(*pdo_err);
if (!msg) {
msg = "<<Unknown error>>";
}
if (dbh->methods->fetch_err) {
MAKE_STD_ZVAL(info);
array_init(info);
add_next_index_string(info, *pdo_err, 1);
if (dbh->methods->fetch_err(dbh, stmt, info TSRMLS_CC)) {
zval **item;
if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(info), 1, (void**)&item)) {
native_code = Z_LVAL_PP(item);
}
if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(info), 2, (void**)&item)) {
supp = estrndup(Z_STRVAL_PP(item), Z_STRLEN_PP(item));
}
}
2005-01-19 03:44:49 +00:00
zval_ptr_dtor(&info);
info = NULL;
}
if (supp) {
spprintf(&message, 0, "SQLSTATE[%s]: %s: %ld %s", *pdo_err, msg, native_code, supp);
} else {
spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
}
if (dbh->error_mode == PDO_ERRMODE_WARNING) {
2004-05-21 14:27:48 +00:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
if (info) {
zval_ptr_dtor(&info);
}
} else {
zval *ex;
zend_class_entry *def_ex = zend_exception_get_default(), *pdo_ex = php_pdo_get_exception();
MAKE_STD_ZVAL(ex);
object_init_ex(ex, pdo_ex);
zend_update_property_string(def_ex, ex, "message", sizeof("message")-1, message TSRMLS_CC);
zend_update_property_string(def_ex, ex, "code", sizeof("code")-1, *pdo_err TSRMLS_CC);
if (info) {
zend_update_property(pdo_ex, ex, "errorInfo", sizeof("errorInfo")-1, info TSRMLS_CC);
}
zend_throw_exception_object(ex TSRMLS_CC);
}
if (message) {
efree(message);
}
if (supp) {
efree(supp);
}
}
2004-09-19 18:11:27 +00:00
static char *dsn_from_uri(char *uri, char *buf, size_t buflen TSRMLS_DC)
{
php_stream *stream;
char *dsn = NULL;
stream = php_stream_open_wrapper(uri, "rb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
if (stream) {
2004-09-19 18:11:27 +00:00
dsn = php_stream_get_line(stream, buf, buflen, NULL);
php_stream_close(stream);
}
return dsn;
}
/* {{{ proto object PDO::__construct(string dsn, string username, string passwd [, array driver_opts])
*/
static PHP_FUNCTION(dbh_constructor)
{
zval *object = getThis();
pdo_dbh_t *dbh = NULL;
zend_bool is_persistent = FALSE;
char *data_source;
2004-06-23 13:20:54 +00:00
int data_source_len;
char *colon;
char *username=NULL, *password=NULL;
2004-06-23 13:20:54 +00:00
int usernamelen, passwordlen;
pdo_driver_t *driver = NULL;
zval *driver_options = NULL;
2004-09-19 18:11:27 +00:00
char alt_dsn[512];
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssa!", &data_source, &data_source_len,
&username, &usernamelen, &password, &passwordlen, &driver_options)) {
ZVAL_NULL(object);
return;
}
/* parse the data source name */
colon = strchr(data_source, ':');
if (!colon) {
/* let's see if this string has a matching dsn in the php.ini */
char *ini_dsn = NULL;
snprintf(alt_dsn, sizeof(alt_dsn), "pdo.dsn.%s", data_source);
if (FAILURE == cfg_get_string(alt_dsn, &ini_dsn)) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "invalid data source name");
ZVAL_NULL(object);
return;
}
data_source = ini_dsn;
colon = strchr(data_source, ':');
if (!colon) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "invalid data source name (via INI: %s)", alt_dsn);
ZVAL_NULL(object);
return;
}
}
if (!strncmp(data_source, "uri:", sizeof("uri:")-1)) {
/* the specified URI holds connection details */
2004-09-19 18:11:27 +00:00
data_source = dsn_from_uri(data_source, alt_dsn, sizeof(alt_dsn) TSRMLS_CC);
if (!data_source) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "invalid data source URI");
ZVAL_NULL(object);
return;
2004-09-19 18:11:27 +00:00
}
colon = strchr(data_source, ':');
if (!colon) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "invalid data source name (via URI)");
2004-09-19 18:11:27 +00:00
ZVAL_NULL(object);
return;
}
}
driver = pdo_find_driver(data_source, colon - data_source);
if (!driver) {
/* NB: don't want to include the data_source in the error message as
* it might contain a password */
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "could not find driver");
ZVAL_NULL(object);
return;
}
2004-06-23 18:06:00 +00:00
dbh = (pdo_dbh_t *) zend_object_store_get_object(object TSRMLS_CC);
/* is this supposed to be a persistent connection ? */
if (driver_options) {
zval **v;
int plen;
char *hashkey = NULL;
list_entry *le;
pdo_dbh_t *pdbh = NULL;
if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(driver_options), PDO_ATTR_PERSISTENT, (void**)&v)) {
if (Z_TYPE_PP(v) == IS_STRING) {
/* user specified key */
plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
username ? username : "",
password ? password : "",
Z_STRVAL_PP(v));
is_persistent = 1;
} else {
convert_to_long_ex(v);
is_persistent = Z_LVAL_PP(v) ? 1 : 0;
plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s", data_source,
username ? username : "",
password ? password : "");
}
}
/* let's see if we have one cached.... */
if (is_persistent && SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, plen+1, (void*)&le)) {
if (Z_TYPE_P(le) == php_pdo_list_entry()) {
pdbh = (pdo_dbh_t*)le->ptr;
/* is the connection still alive ? */
if (pdbh->methods->check_liveness && FAILURE == (pdbh->methods->check_liveness)(pdbh TSRMLS_CC)) {
/* nope... need to kill it */
pdbh = NULL;
}
}
}
if (is_persistent && !pdbh) {
/* need a brand new pdbh */
pdbh = pecalloc(1, sizeof(*pdbh), 1);
if (!pdbh) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "out of memory while allocating PDO handle");
/* NOTREACHED */
}
pdbh->is_persistent = 1;
pdbh->persistent_id = pemalloc(plen + 1, 1);
memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
pdbh->persistent_id_len = plen+1;
pdbh->refcount = 1;
}
if (pdbh) {
/* let's copy the emalloc bits over from the other handle */
pdbh->ce = dbh->ce;
pdbh->properties = dbh->properties;
/* kill the non-persistent thingamy */
efree(dbh);
/* switch over to the persistent one */
dbh = pdbh;
zend_object_store_set_object(object, dbh TSRMLS_CC);
dbh->refcount++;
}
if (hashkey) {
efree(hashkey);
}
}
dbh->data_source_len = strlen(colon + 1);
dbh->data_source = (const char*)pestrdup(colon + 1, is_persistent);
dbh->username = username ? pestrdup(username, is_persistent) : NULL;
dbh->password = password ? pestrdup(password, is_persistent) : NULL;
dbh->auto_commit = pdo_attr_lval(driver_options, PDO_ATTR_AUTOCOMMIT, 1 TSRMLS_CC);
2004-09-26 20:45:44 +00:00
if (!dbh->data_source || (username && !dbh->username) || (password && !dbh->password)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "out of memory");
}
if (driver->db_handle_factory(dbh, driver_options TSRMLS_CC)) {
/* all set */
if (is_persistent) {
list_entry le;
/* register in the persistent list etc. */
2004-06-23 18:06:00 +00:00
/* we should also need to replace the object store entry,
since it was created with emalloc */
le.type = php_pdo_list_entry();
le.ptr = dbh;
if (FAILURE == zend_hash_update(&EG(persistent_list),
(char*)dbh->persistent_id, dbh->persistent_id_len, (void*)&le,
sizeof(le), NULL)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to register persistent entry");
}
}
return;
}
2004-06-23 18:06:00 +00:00
/* the connection failed; things will tidy up in free_storage */
/* XXX raise exception */
ZVAL_NULL(object);
}
/* }}} */
2005-01-21 00:38:09 +00:00
/* {{{ proto object PDO::prepare(string statment [, array driver_options])
Prepares a statement for execution and returns a statement object */
static PHP_METHOD(PDO, prepare)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
pdo_stmt_t *stmt;
char *statement;
2004-06-23 13:20:54 +00:00
int statement_len;
zval *driver_options = NULL;
2005-01-21 00:38:09 +00:00
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &statement,
&statement_len, &driver_options)) {
RETURN_FALSE;
}
PDO_DBH_CLEAR_ERR();
stmt = ecalloc(1, sizeof(*stmt));
/* unconditionally keep this for later reference */
stmt->query_string = estrndup(statement, statement_len);
stmt->query_stringlen = statement_len;
Synopsis: PDOStatement::setFetchMode() reset default fetch() mode for a statement to PDO_FETCH_BOTH PDOStatement::setFetchMode(PDO_FETCH_NUM) PDOStatement::setFetchMode(PDO_FETCH_ASSOC) PDOStatement::setFetchMode(PDO_FETCH_BOTH) PDOStatement::setFetchMode(PDO_FETCH_OBJ) set default fetch() mode for a statement. PDOStatement::setFetchMode(PDO_FETCH_COLUMN, int colno) set default fetch() mode to retrieve colno-th column on each fetch() call. PDOStatement::setFetchMode(PDO_FETCH_CLASS, string classname [, array ctor args]) set default fetch() mode to create an instance of classname, calling it's ctor, passing the optional ctor args. The names of the columns in the result set will be used as property names on the object instance. PPP rules apply. [NOTE: calling ctor is not yet implemented] [TODO: this might crash PHP for persistent PDO handles] PDOStatement::setFetchMode(PDO_FETCH_INTO, object obj) Similar to PDO_FETCH_CLASS, except that each iteration will update the supplied object properties. [TODO: this might crash PHP for persistent PDO handles] The default fetch() mode is used when no parameters are passed to PDOStatement::fetch(). When using a statement in an iterator context, PDOStatement::fetch() is called implicitly on each iteration. object PDO::queryAndIterate(string sql, <PDOStatement::setFetchMode args>) This is semantically equivalent to: $stmt = $pdo->prepare($sql); $stmt->execute(); $stmt->setFetchMode($args); return $stmt; Example/Intended usage: /* fetch an array with numeric and string keys */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test") as $row) { debug_zval_dump($row); } /* fetch the value of column 1 into $row on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_COLUMN, 1) as $row) { debug_zval_dump($row); // string(3) "foo" } /* create a new instance of class Foo on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_CLASS, 'Foo') as $row) { debug_zval_dump($row); /* Object(Foo)#4 (2) refcount(2){ ["NAME"]=> string(12) "foo220051429" refcount(2) ["VALUE"]=> string(12) "bar789825748" refcount(2) } */ } etc.
2004-10-27 10:26:27 +00:00
stmt->default_fetch_type = PDO_FETCH_BOTH;
2005-01-21 00:38:09 +00:00
if (dbh->methods->preparer(dbh, statement, statement_len, stmt, driver_options TSRMLS_CC)) {
/* prepared; create a statement object for PHP land to access it */
Z_TYPE_P(return_value) = IS_OBJECT;
Z_OBJ_HANDLE_P(return_value) = zend_objects_store_put(stmt, NULL, pdo_dbstmt_free_storage, NULL TSRMLS_CC);
Z_OBJ_HT_P(return_value) = &pdo_dbstmt_object_handlers;
/* give it a reference to me */
stmt->database_object_handle = *getThis();
zend_objects_store_add_ref(getThis() TSRMLS_CC);
stmt->dbh = dbh;
2004-05-21 13:26:58 +00:00
/* we haven't created a lazy object yet */
ZVAL_NULL(&stmt->lazy_object_ref);
stmt->refcount = 1;
return;
}
2005-01-21 04:41:52 +00:00
efree(stmt->query_string);
efree(stmt);
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
2004-06-23 18:06:00 +00:00
/* }}} */
/* {{{ proto bool PDO::beginTransaction()
Initiates a transaction */
static PHP_METHOD(PDO, beginTransaction)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (dbh->in_txn) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is already an active transaction");
RETURN_FALSE;
}
if (!dbh->methods->begin) {
/* TODO: this should be an exception; see the auto-commit mode
* comments below */
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "This driver doesn't support transactions");
RETURN_FALSE;
}
if (dbh->methods->begin(dbh TSRMLS_CC)) {
dbh->in_txn = 1;
RETURN_TRUE;
}
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool PDO::commit()
Commit a transaction */
static PHP_METHOD(PDO, commit)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (!dbh->in_txn) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
RETURN_FALSE;
}
if (dbh->methods->commit(dbh TSRMLS_CC)) {
dbh->in_txn = 0;
RETURN_TRUE;
}
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool PDO::rollBack()
roll back a transaction */
static PHP_METHOD(PDO, rollBack)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (!dbh->in_txn) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
RETURN_FALSE;
}
if (dbh->methods->rollback(dbh TSRMLS_CC)) {
dbh->in_txn = 0;
RETURN_TRUE;
}
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool PDO::setAttribute(long attribute, mixed value)
Set an attribute */
static PHP_METHOD(PDO, setAttribute)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
long attr;
zval *value = NULL;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz!", &attr, &value)) {
RETURN_FALSE;
}
switch (attr) {
case PDO_ATTR_ERRMODE:
convert_to_long(value);
switch (Z_LVAL_P(value)) {
case PDO_ERRMODE_SILENT:
case PDO_ERRMODE_WARNING:
case PDO_ERRMODE_EXCEPTION:
dbh->error_mode = Z_LVAL_P(value);
RETURN_TRUE;
default:
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "Error mode %d is invalid", Z_LVAL_P(value));
}
RETURN_FALSE;
case PDO_ATTR_CASE:
convert_to_long(value);
switch (Z_LVAL_P(value)) {
case PDO_CASE_NATURAL:
case PDO_CASE_UPPER:
case PDO_CASE_LOWER:
dbh->desired_case = Z_LVAL_P(value);
RETURN_TRUE;
default:
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "Case folding mode %d is invalid", Z_LVAL_P(value));
}
RETURN_FALSE;
case PDO_ATTR_ORACLE_NULLS:
convert_to_long(value);
dbh->oracle_nulls = Z_LVAL_P(value) ? 1 : 0;
RETURN_TRUE;
default:
;
}
if (!dbh->methods->set_attribute) {
goto fail;
}
PDO_DBH_CLEAR_ERR();
if (dbh->methods->set_attribute(dbh, attr, value TSRMLS_CC)) {
RETURN_TRUE;
}
fail:
if (attr == PDO_ATTR_AUTOCOMMIT) {
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "The auto-commit mode cannot be changed for this driver");
} else if (!dbh->methods->set_attribute) {
2005-02-06 21:05:59 +00:00
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support setting attributes" TSRMLS_CC);
2004-05-25 18:32:48 +00:00
} else {
PDO_HANDLE_DBH_ERR();
}
RETURN_FALSE;
}
/* }}} */
2004-05-25 18:32:48 +00:00
/* {{{ proto mixed PDO::getAttribute(long attribute)
Get an attribute */
2004-05-20 19:09:35 +00:00
static PHP_METHOD(PDO, getAttribute)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
long attr;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &attr)) {
RETURN_FALSE;
}
2005-02-06 17:25:09 +00:00
PDO_DBH_CLEAR_ERR();
/* handle generic PDO-level atributes */
switch (attr) {
case PDO_ATTR_PERSISTENT:
RETURN_BOOL(dbh->is_persistent);
case PDO_ATTR_CASE:
RETURN_LONG(dbh->desired_case);
case PDO_ATTR_ORACLE_NULLS:
RETURN_BOOL(dbh->oracle_nulls);
2005-02-06 17:33:12 +00:00
case PDO_ATTR_ERRMODE:
RETURN_LONG(dbh->error_mode);
2005-02-06 17:25:09 +00:00
}
2004-05-20 19:14:44 +00:00
if (!dbh->methods->get_attribute) {
2005-02-06 21:05:59 +00:00
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support getting attributes" TSRMLS_CC);
2004-05-20 19:09:35 +00:00
RETURN_FALSE;
}
switch (dbh->methods->get_attribute(dbh, attr, return_value TSRMLS_CC)) {
case -1:
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
case 0:
2005-02-06 21:05:59 +00:00
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support that attribute" TSRMLS_CC);
RETURN_FALSE;
2004-05-20 19:09:35 +00:00
default:
return;
}
}
/* }}} */
/* {{{ proto long PDO::exec(string query)
2004-05-19 19:27:53 +00:00
Execute a query that does not return a row set, returning the number of affected rows */
static PHP_METHOD(PDO, exec)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
char *statement;
2004-06-23 13:20:54 +00:00
int statement_len;
2004-05-19 19:27:53 +00:00
long ret;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &statement, &statement_len)) {
RETURN_FALSE;
}
if (!statement_len) {
RETURN_FALSE;
}
PDO_DBH_CLEAR_ERR();
2004-05-19 19:27:53 +00:00
ret = dbh->methods->doer(dbh, statement, statement_len TSRMLS_CC);
if(ret == -1) {
PDO_HANDLE_DBH_ERR();
RETURN_FALSE;
} else {
2004-05-19 19:27:53 +00:00
RETURN_LONG(ret);
}
}
/* }}} */
2004-05-19 19:27:53 +00:00
/* {{{ proto int PDO::lastInsertId()
Returns the number id of rows that we affected by the last call to PDO::exec(). Not always meaningful. */
static PHP_METHOD(PDO, lastInsertId)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
PDO_DBH_CLEAR_ERR();
if (!dbh->methods->last_id) {
2005-02-06 21:05:59 +00:00
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()" TSRMLS_CC);
RETURN_FALSE;
} else {
RETURN_LONG(dbh->methods->last_id(dbh TSRMLS_CC));
}
}
/* }}} */
/* {{{ proto string PDO::errorCode()
2004-05-20 17:22:13 +00:00
Fetch the error code associated with the last operation on the database handle */
static PHP_METHOD(PDO, errorCode)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
RETURN_STRING(dbh->error_code, 1);
2004-05-20 17:22:13 +00:00
}
/* }}} */
/* {{{ proto int PDO::errorInfo()
Fetch extended error information associated with the last operation on the database handle */
static PHP_METHOD(PDO, errorInfo)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
array_init(return_value);
add_next_index_string(return_value, dbh->error_code, 1);
2004-05-20 17:22:13 +00:00
if (dbh->methods->fetch_err) {
dbh->methods->fetch_err(dbh, NULL, return_value TSRMLS_CC);
}
}
/* }}} */
/* {{{ proto object PDO::query(string sql [, PDOStatement::setFetchMode() args])
Synopsis: PDOStatement::setFetchMode() reset default fetch() mode for a statement to PDO_FETCH_BOTH PDOStatement::setFetchMode(PDO_FETCH_NUM) PDOStatement::setFetchMode(PDO_FETCH_ASSOC) PDOStatement::setFetchMode(PDO_FETCH_BOTH) PDOStatement::setFetchMode(PDO_FETCH_OBJ) set default fetch() mode for a statement. PDOStatement::setFetchMode(PDO_FETCH_COLUMN, int colno) set default fetch() mode to retrieve colno-th column on each fetch() call. PDOStatement::setFetchMode(PDO_FETCH_CLASS, string classname [, array ctor args]) set default fetch() mode to create an instance of classname, calling it's ctor, passing the optional ctor args. The names of the columns in the result set will be used as property names on the object instance. PPP rules apply. [NOTE: calling ctor is not yet implemented] [TODO: this might crash PHP for persistent PDO handles] PDOStatement::setFetchMode(PDO_FETCH_INTO, object obj) Similar to PDO_FETCH_CLASS, except that each iteration will update the supplied object properties. [TODO: this might crash PHP for persistent PDO handles] The default fetch() mode is used when no parameters are passed to PDOStatement::fetch(). When using a statement in an iterator context, PDOStatement::fetch() is called implicitly on each iteration. object PDO::queryAndIterate(string sql, <PDOStatement::setFetchMode args>) This is semantically equivalent to: $stmt = $pdo->prepare($sql); $stmt->execute(); $stmt->setFetchMode($args); return $stmt; Example/Intended usage: /* fetch an array with numeric and string keys */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test") as $row) { debug_zval_dump($row); } /* fetch the value of column 1 into $row on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_COLUMN, 1) as $row) { debug_zval_dump($row); // string(3) "foo" } /* create a new instance of class Foo on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_CLASS, 'Foo') as $row) { debug_zval_dump($row); /* Object(Foo)#4 (2) refcount(2){ ["NAME"]=> string(12) "foo220051429" refcount(2) ["VALUE"]=> string(12) "bar789825748" refcount(2) } */ } etc.
2004-10-27 10:26:27 +00:00
Prepare and execute $sql; returns the statement object for iteration */
static PHP_METHOD(PDO, query)
Synopsis: PDOStatement::setFetchMode() reset default fetch() mode for a statement to PDO_FETCH_BOTH PDOStatement::setFetchMode(PDO_FETCH_NUM) PDOStatement::setFetchMode(PDO_FETCH_ASSOC) PDOStatement::setFetchMode(PDO_FETCH_BOTH) PDOStatement::setFetchMode(PDO_FETCH_OBJ) set default fetch() mode for a statement. PDOStatement::setFetchMode(PDO_FETCH_COLUMN, int colno) set default fetch() mode to retrieve colno-th column on each fetch() call. PDOStatement::setFetchMode(PDO_FETCH_CLASS, string classname [, array ctor args]) set default fetch() mode to create an instance of classname, calling it's ctor, passing the optional ctor args. The names of the columns in the result set will be used as property names on the object instance. PPP rules apply. [NOTE: calling ctor is not yet implemented] [TODO: this might crash PHP for persistent PDO handles] PDOStatement::setFetchMode(PDO_FETCH_INTO, object obj) Similar to PDO_FETCH_CLASS, except that each iteration will update the supplied object properties. [TODO: this might crash PHP for persistent PDO handles] The default fetch() mode is used when no parameters are passed to PDOStatement::fetch(). When using a statement in an iterator context, PDOStatement::fetch() is called implicitly on each iteration. object PDO::queryAndIterate(string sql, <PDOStatement::setFetchMode args>) This is semantically equivalent to: $stmt = $pdo->prepare($sql); $stmt->execute(); $stmt->setFetchMode($args); return $stmt; Example/Intended usage: /* fetch an array with numeric and string keys */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test") as $row) { debug_zval_dump($row); } /* fetch the value of column 1 into $row on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_COLUMN, 1) as $row) { debug_zval_dump($row); // string(3) "foo" } /* create a new instance of class Foo on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_CLASS, 'Foo') as $row) { debug_zval_dump($row); /* Object(Foo)#4 (2) refcount(2){ ["NAME"]=> string(12) "foo220051429" refcount(2) ["VALUE"]=> string(12) "bar789825748" refcount(2) } */ } etc.
2004-10-27 10:26:27 +00:00
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
pdo_stmt_t *stmt;
char *statement;
int statement_len;
zval *driver_options = NULL;
if (FAILURE == zend_parse_parameters(1 TSRMLS_CC, "s", &statement,
&statement_len)) {
RETURN_FALSE;
}
PDO_DBH_CLEAR_ERR();
stmt = ecalloc(1, sizeof(*stmt));
/* unconditionally keep this for later reference */
stmt->query_string = estrndup(statement, statement_len);
stmt->query_stringlen = statement_len;
stmt->default_fetch_type = PDO_FETCH_BOTH;
2005-01-21 00:38:09 +00:00
if (dbh->methods->preparer(dbh, statement, statement_len, stmt, driver_options TSRMLS_CC)) {
Synopsis: PDOStatement::setFetchMode() reset default fetch() mode for a statement to PDO_FETCH_BOTH PDOStatement::setFetchMode(PDO_FETCH_NUM) PDOStatement::setFetchMode(PDO_FETCH_ASSOC) PDOStatement::setFetchMode(PDO_FETCH_BOTH) PDOStatement::setFetchMode(PDO_FETCH_OBJ) set default fetch() mode for a statement. PDOStatement::setFetchMode(PDO_FETCH_COLUMN, int colno) set default fetch() mode to retrieve colno-th column on each fetch() call. PDOStatement::setFetchMode(PDO_FETCH_CLASS, string classname [, array ctor args]) set default fetch() mode to create an instance of classname, calling it's ctor, passing the optional ctor args. The names of the columns in the result set will be used as property names on the object instance. PPP rules apply. [NOTE: calling ctor is not yet implemented] [TODO: this might crash PHP for persistent PDO handles] PDOStatement::setFetchMode(PDO_FETCH_INTO, object obj) Similar to PDO_FETCH_CLASS, except that each iteration will update the supplied object properties. [TODO: this might crash PHP for persistent PDO handles] The default fetch() mode is used when no parameters are passed to PDOStatement::fetch(). When using a statement in an iterator context, PDOStatement::fetch() is called implicitly on each iteration. object PDO::queryAndIterate(string sql, <PDOStatement::setFetchMode args>) This is semantically equivalent to: $stmt = $pdo->prepare($sql); $stmt->execute(); $stmt->setFetchMode($args); return $stmt; Example/Intended usage: /* fetch an array with numeric and string keys */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test") as $row) { debug_zval_dump($row); } /* fetch the value of column 1 into $row on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_COLUMN, 1) as $row) { debug_zval_dump($row); // string(3) "foo" } /* create a new instance of class Foo on each iteration */ foreach ($pdo->queryAndIterate("select NAME, VALUE from test", PDO_FETCH_CLASS, 'Foo') as $row) { debug_zval_dump($row); /* Object(Foo)#4 (2) refcount(2){ ["NAME"]=> string(12) "foo220051429" refcount(2) ["VALUE"]=> string(12) "bar789825748" refcount(2) } */ } etc.
2004-10-27 10:26:27 +00:00
/* prepared; create a statement object for PHP land to access it */
Z_TYPE_P(return_value) = IS_OBJECT;
Z_OBJ_HANDLE_P(return_value) = zend_objects_store_put(stmt, NULL, pdo_dbstmt_free_storage, NULL TSRMLS_CC);
Z_OBJ_HT_P(return_value) = &pdo_dbstmt_object_handlers;
/* give it a reference to me */
stmt->database_object_handle = *getThis();
zend_objects_store_add_ref(getThis() TSRMLS_CC);
stmt->dbh = dbh;
/* we haven't created a lazy object yet */
ZVAL_NULL(&stmt->lazy_object_ref);
stmt->refcount = 1;
if (ZEND_NUM_ARGS() == 1 ||
SUCCESS == pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU,
stmt, 1)) {
PDO_STMT_CLEAR_ERR();
/* now execute the statement */
PDO_STMT_CLEAR_ERR();
if (stmt->methods->executer(stmt TSRMLS_CC)) {
int ret = 1;
if (!stmt->executed) {
if (stmt->dbh->alloc_own_columns) {
ret = pdo_stmt_describe_columns(stmt TSRMLS_CC);
}
stmt->executed = 1;
}
if (ret) {
return;
}
}
}
/* something broke */
PDO_HANDLE_STMT_ERR();
/* TODO: kill the object handle for the stmt here */
} else {
efree(stmt);
PDO_HANDLE_DBH_ERR();
}
RETURN_FALSE;
}
/* }}} */
2004-05-20 17:22:13 +00:00
/* {{{ proto string PDO::quote(string string [, int paramtype])
quotes string for use in a query. The optional paramtype acts as a hint for drivers that have alternate quoting styles. The default value is PDO_PARAM_STR */
static PHP_METHOD(PDO, quote)
{
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
char *str;
int str_len;
long paramtype = PDO_PARAM_STR;
char *qstr;
size_t qlen;
if (FAILURE == zend_parse_parameters(1 TSRMLS_CC, "s|l", &str, &str_len,
&paramtype)) {
RETURN_FALSE;
}
PDO_DBH_CLEAR_ERR();
if (!dbh->methods->quoter) {
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting" TSRMLS_CC);
RETURN_FALSE;
}
if (dbh->methods->quoter(dbh, str, str_len, &qstr, &qlen, paramtype TSRMLS_CC)) {
RETURN_STRINGL(qstr, qlen, 0);
}
PDO_HANDLE_DBH_ERR();
}
/* }}} */
function_entry pdo_dbh_functions[] = {
2004-06-23 18:06:00 +00:00
PHP_ME_MAPPING(__construct, dbh_constructor, NULL)
PHP_ME(PDO, prepare, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, beginTransaction,NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, commit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, rollBack, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, setAttribute, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, exec, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, query, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, lastInsertId, NULL, ZEND_ACC_PUBLIC)
2004-05-20 17:22:13 +00:00
PHP_ME(PDO, errorCode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, errorInfo, NULL, ZEND_ACC_PUBLIC)
2004-05-20 19:09:35 +00:00
PHP_ME(PDO, getAttribute, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, quote, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* {{{ overloaded object handlers for PDO class */
static zval *dbh_prop_read(zval *object, zval *member, int type TSRMLS_DC)
{
zval *return_value;
MAKE_STD_ZVAL(return_value);
ZVAL_NULL(return_value);
return return_value;
}
static void dbh_prop_write(zval *object, zval *member, zval *value TSRMLS_DC)
{
return;
}
static zval *dbh_read_dim(zval *object, zval *offset, int type TSRMLS_DC)
{
zval *return_value;
MAKE_STD_ZVAL(return_value);
ZVAL_NULL(return_value);
return return_value;
}
static void dbh_write_dim(zval *object, zval *offset, zval *value TSRMLS_DC)
{
return;
}
static int dbh_prop_exists(zval *object, zval *member, int check_empty TSRMLS_DC)
{
return 0;
}
static int dbh_dim_exists(zval *object, zval *member, int check_empty TSRMLS_DC)
{
return 0;
}
static void dbh_prop_delete(zval *object, zval *offset TSRMLS_DC)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot delete properties from a PDO DBH");
}
static void dbh_dim_delete(zval *object, zval *offset TSRMLS_DC)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot delete dimensions from a PDO DBH");
}
static HashTable *dbh_get_properties(zval *object TSRMLS_DC)
{
return NULL;
}
int pdo_hash_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
{
function_entry *funcs;
zend_function func;
zend_internal_function *ifunc = (zend_internal_function*)&func;
int namelen;
char *lc_name;
if (!dbh->methods->get_driver_methods) {
return 0;
}
funcs = dbh->methods->get_driver_methods(dbh,
2005-01-08 12:35:50 +00:00
PDO_DBH_DRIVER_METHOD_KIND_DBH TSRMLS_CC);
if (!funcs) {
return 0;
}
dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent);
zend_hash_init_ex(dbh->cls_methods[kind], 8, NULL, NULL, dbh->is_persistent, 0);
while (funcs->fname) {
ifunc->handler = funcs->handler;
ifunc->function_name = funcs->fname;
ifunc->scope = dbh->ce;
ifunc->prototype = NULL;
if (funcs->arg_info) {
ifunc->arg_info = funcs->arg_info + 1;
ifunc->num_args = funcs->num_args;
if (funcs->arg_info[0].required_num_args == -1) {
ifunc->required_num_args = funcs->num_args;
} else {
ifunc->required_num_args = funcs->arg_info[0].required_num_args;
}
ifunc->pass_rest_by_reference = funcs->arg_info[0].pass_by_reference;
ifunc->return_reference = funcs->arg_info[0].return_reference;
} else {
ifunc->arg_info = NULL;
ifunc->num_args = 0;
ifunc->required_num_args = 0;
ifunc->pass_rest_by_reference = 0;
ifunc->return_reference = 0;
}
if (funcs->flags) {
ifunc->fn_flags = funcs->flags;
} else {
ifunc->fn_flags = ZEND_ACC_PUBLIC;
}
namelen = strlen(funcs->fname);
lc_name = emalloc(namelen+1);
zend_str_tolower_copy(lc_name, funcs->fname, namelen);
zend_hash_add(dbh->cls_methods[kind], lc_name, namelen+1, &func, sizeof(func), NULL);
efree(lc_name);
funcs++;
}
return 1;
}
2004-12-26 04:50:09 +00:00
static union _zend_function *dbh_method_get(
#if PHP_API_VERSION >= 20041225
zval **object_pp,
#else
zval *object,
#endif
char *method_name, int method_len TSRMLS_DC)
{
zend_function *fbc = NULL;
char *lc_method_name;
2004-12-26 04:50:09 +00:00
#if PHP_API_VERSION >= 20041225
zval *object = *object_pp;
#endif
pdo_dbh_t *dbh = zend_object_store_get_object(object TSRMLS_CC);
lc_method_name = emalloc(method_len + 1);
zend_str_tolower_copy(lc_method_name, method_name, method_len);
if (zend_hash_find(&dbh->ce->function_table, lc_method_name, method_len+1, (void**)&fbc) == FAILURE) {
/* not a pre-defined method, nor a user-defined method; check
* the driver specific methods */
if (!dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
if (!pdo_hash_methods(dbh,
PDO_DBH_DRIVER_METHOD_KIND_DBH TSRMLS_CC)
|| !dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
goto out;
}
}
if (zend_hash_find(dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH],
lc_method_name, method_len+1, (void**)&fbc) == FAILURE) {
fbc = NULL;
goto out;
}
/* got it */
}
out:
efree(lc_method_name);
return fbc;
}
static int dbh_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
{
return FAILURE;
}
2004-06-23 18:06:00 +00:00
static union _zend_function *dbh_get_ctor(zval *object TSRMLS_DC)
{
static zend_internal_function ctor = {0};
ctor.type = ZEND_INTERNAL_FUNCTION;
ctor.function_name = "__construct";
ctor.scope = pdo_dbh_ce;
ctor.handler = ZEND_FN(dbh_constructor);
return (union _zend_function*)&ctor;
}
static zend_class_entry *dbh_get_ce(zval *object TSRMLS_DC)
{
pdo_dbh_t *dbh = zend_object_store_get_object(object TSRMLS_CC);
return dbh->ce;
}
static int dbh_get_classname(zval *object, char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC)
{
*class_name = estrndup("PDO", sizeof("PDO")-1);
*class_name_len = sizeof("PDO")-1;
return 0;
}
static int dbh_compare(zval *object1, zval *object2 TSRMLS_DC)
{
return -1;
}
static zend_object_handlers pdo_dbh_object_handlers = {
ZEND_OBJECTS_STORE_HANDLERS,
dbh_prop_read,
dbh_prop_write,
dbh_read_dim,
dbh_write_dim,
NULL,
NULL,
NULL,
dbh_prop_exists,
dbh_prop_delete,
dbh_dim_exists,
dbh_dim_delete,
dbh_get_properties,
dbh_method_get,
dbh_call_method,
dbh_get_ctor,
dbh_get_ce,
dbh_get_classname,
dbh_compare,
NULL, /* cast */
NULL
};
static void dbh_free(pdo_dbh_t *dbh TSRMLS_DC)
{
if (--dbh->refcount)
return;
2004-07-28 00:01:12 +00:00
if (dbh->methods) {
dbh->methods->closer(dbh TSRMLS_CC);
}
2004-05-25 14:12:15 +00:00
if (dbh->data_source) {
2004-05-25 14:30:07 +00:00
pefree((char *)dbh->data_source, dbh->is_persistent);
2004-05-25 14:12:15 +00:00
}
if (dbh->username) {
2004-05-25 14:30:07 +00:00
pefree(dbh->username, dbh->is_persistent);
2004-05-25 14:12:15 +00:00
}
if (dbh->password) {
2004-05-25 14:30:07 +00:00
pefree(dbh->password, dbh->is_persistent);
2004-05-25 14:12:15 +00:00
}
2004-05-25 14:30:07 +00:00
pefree(dbh, dbh->is_persistent);
}
static void pdo_dbh_free_storage(zend_object *object TSRMLS_DC)
{
pdo_dbh_t *dbh = (pdo_dbh_t*)object;
if (!dbh) {
return;
}
if (dbh->properties) {
zend_hash_destroy(dbh->properties);
efree(dbh->properties);
dbh->properties = NULL;
}
if (!dbh->is_persistent) {
dbh_free(dbh TSRMLS_CC);
}
}
zend_object_value pdo_dbh_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
2004-06-23 18:06:00 +00:00
pdo_dbh_t *dbh;
dbh = emalloc(sizeof(*dbh));
memset(dbh, 0, sizeof(*dbh));
dbh->ce = ce;
dbh->refcount = 1;
ALLOC_HASHTABLE(dbh->properties);
zend_hash_init(dbh->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
2004-06-23 18:06:00 +00:00
retval.handle = zend_objects_store_put(dbh, NULL, pdo_dbh_free_storage, NULL TSRMLS_CC);
if(ce == pdo_dbh_ce) {
retval.handlers = &pdo_dbh_object_handlers;
}
else {
retval.handlers = &std_object_handlers;
}
return retval;
}
/* }}} */
ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor)
{
if (rsrc->ptr) {
pdo_dbh_t *dbh = (pdo_dbh_t*)rsrc->ptr;
dbh_free(dbh TSRMLS_CC);
rsrc->ptr = NULL;
}
}
/*
* 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
*/