2004-05-17 23:41:51 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| 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: 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"
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC)
|
|
|
|
{
|
|
|
|
enum pdo_error_type *pdo_err = &dbh->error_code;
|
|
|
|
const char *msg = "<<Unknown>>";
|
|
|
|
char *supp = NULL;
|
|
|
|
long native_code = 0;
|
2004-05-21 03:16:49 +08:00
|
|
|
char *message = NULL;
|
|
|
|
zval *info = NULL;
|
2004-05-20 08:05:22 +08:00
|
|
|
|
2004-05-21 03:16:49 +08:00
|
|
|
if (dbh->error_mode == PDO_ERRMODE_SILENT) {
|
|
|
|
return;
|
|
|
|
}
|
2004-05-20 08:05:22 +08:00
|
|
|
|
|
|
|
if (stmt) {
|
|
|
|
pdo_err = &stmt->error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*pdo_err) {
|
2004-05-20 23:45:16 +08:00
|
|
|
case PDO_ERR_NONE: msg = "No error"; break;
|
2004-05-20 08:05:22 +08:00
|
|
|
case PDO_ERR_CANT_MAP: msg = "Consult errorInfo() for more details"; break;
|
|
|
|
case PDO_ERR_SYNTAX: msg = "Syntax Error"; break;
|
|
|
|
case PDO_ERR_CONSTRAINT:msg = "Constraint violation"; break;
|
|
|
|
case PDO_ERR_NOT_FOUND: msg = "Not found"; break;
|
|
|
|
case PDO_ERR_ALREADY_EXISTS: msg = "Already exists"; break;
|
|
|
|
case PDO_ERR_NOT_IMPLEMENTED: msg = "Not Implemented"; break;
|
|
|
|
case PDO_ERR_MISMATCH: msg = "Mismatch"; break;
|
|
|
|
case PDO_ERR_TRUNCATED: msg = "Truncated"; break;
|
|
|
|
case PDO_ERR_DISCONNECTED: msg = "Disconnected"; break;
|
|
|
|
default: msg = "<<Invalid>>";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->methods->fetch_err) {
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(info);
|
|
|
|
array_init(info);
|
|
|
|
|
2004-05-21 03:16:49 +08:00
|
|
|
add_next_index_long(info, *pdo_err);
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
if (dbh->methods->fetch_err(dbh, stmt, info TSRMLS_CC)) {
|
|
|
|
zval **item;
|
|
|
|
|
2004-05-21 03:16:49 +08:00
|
|
|
if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(info), 1, (void**)&item)) {
|
2004-05-20 08:05:22 +08:00
|
|
|
native_code = Z_LVAL_PP(item);
|
|
|
|
}
|
|
|
|
|
2004-05-21 03:16:49 +08:00
|
|
|
if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(info), 2, (void**)&item)) {
|
2004-05-20 08:05:22 +08:00
|
|
|
supp = estrndup(Z_STRVAL_PP(item), Z_STRLEN_PP(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (supp && *pdo_err == PDO_ERR_CANT_MAP) {
|
2004-05-21 03:16:49 +08:00
|
|
|
spprintf(&message, 0, "%ld %s", native_code, supp);
|
2004-05-20 08:05:22 +08:00
|
|
|
} else if (supp) {
|
2004-05-21 03:16:49 +08:00
|
|
|
spprintf(&message, 0, "%s: %ld %s", msg, native_code, supp);
|
|
|
|
} else {
|
|
|
|
spprintf(&message, 0, "%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->error_mode == PDO_ERRMODE_WARNING) {
|
2004-05-21 22:27:48 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
|
2004-05-21 03:16:49 +08:00
|
|
|
|
|
|
|
if (info) {
|
2004-07-06 05:16:29 +08:00
|
|
|
zval_ptr_dtor(&info);
|
2004-05-21 03:16:49 +08:00
|
|
|
}
|
2004-05-20 08:05:22 +08:00
|
|
|
} else {
|
2004-05-21 03:16:49 +08:00
|
|
|
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_long(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);
|
2004-05-20 08:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (supp) {
|
|
|
|
efree(supp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-17 23:41:51 +08:00
|
|
|
/* {{{ 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 21:20:54 +08:00
|
|
|
int data_source_len;
|
2004-05-17 23:41:51 +08:00
|
|
|
char *colon;
|
2004-05-21 04:25:22 +08:00
|
|
|
char *username=NULL, *password=NULL;
|
2004-06-23 21:20:54 +08:00
|
|
|
int usernamelen, passwordlen;
|
2004-05-17 23:41:51 +08:00
|
|
|
pdo_driver_t *driver = NULL;
|
|
|
|
zval *driver_options = NULL;
|
|
|
|
|
2004-05-21 04:25:22 +08:00
|
|
|
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssa!", &data_source, &data_source_len,
|
2004-05-17 23:41:51 +08:00
|
|
|
&username, &usernamelen, &password, &passwordlen, &driver_options)) {
|
|
|
|
ZVAL_NULL(object);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the data source name */
|
|
|
|
colon = strchr(data_source, ':');
|
|
|
|
|
|
|
|
if (!colon) {
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_SYNTAX TSRMLS_CC, "invalid data source name");
|
2004-05-17 23:41:51 +08: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 */
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NOT_FOUND TSRMLS_CC, "could not find driver");
|
2004-05-17 23:41:51 +08:00
|
|
|
ZVAL_NULL(object);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
dbh = (pdo_dbh_t *) zend_object_store_get_object(object TSRMLS_CC);
|
2004-05-17 23:41:51 +08:00
|
|
|
|
|
|
|
if (dbh == NULL) {
|
|
|
|
/* need this check for persistent allocations */
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "out of memory!?");
|
2004-05-17 23:41:51 +08:00
|
|
|
}
|
|
|
|
dbh->is_persistent = is_persistent;
|
|
|
|
dbh->data_source_len = strlen(colon + 1);
|
|
|
|
/* when persistent stuff is done, we should check the return values here
|
|
|
|
* too */
|
|
|
|
dbh->data_source = (const char*)pestrdup(colon + 1, is_persistent);
|
2004-05-21 04:25:22 +08:00
|
|
|
dbh->username = username ? pestrdup(username, is_persistent) : NULL;
|
|
|
|
dbh->password = password ? pestrdup(password, is_persistent) : NULL;
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-05-26 01:43:24 +08:00
|
|
|
dbh->auto_commit = pdo_attr_lval(driver_options, PDO_ATTR_AUTOCOMMIT, 1 TSRMLS_CC);
|
2004-05-17 23:41:51 +08:00
|
|
|
|
|
|
|
if (driver->db_handle_factory(dbh, driver_options TSRMLS_CC)) {
|
|
|
|
/* all set */
|
|
|
|
|
|
|
|
if (is_persistent) {
|
|
|
|
/* register in the persistent list etc. */
|
2004-06-24 02:06:00 +08:00
|
|
|
/* we should also need to replace the object store entry,
|
|
|
|
since it was created with emalloc */
|
2004-05-17 23:41:51 +08:00
|
|
|
;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
/* the connection failed; things will tidy up in free_storage */
|
2004-05-17 23:41:51 +08:00
|
|
|
/* XXX raise exception */
|
|
|
|
ZVAL_NULL(object);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-05-19 20:37:31 +08:00
|
|
|
/* {{{ proto object PDO::prepare(string statment [, int options [, array driver_options]])
|
2004-05-17 23:41:51 +08:00
|
|
|
Prepares a statement for execution and returns a statement object */
|
2004-05-19 20:37:31 +08:00
|
|
|
/* TODO: options will be a PDO specific bitmask controlling such things as
|
|
|
|
* cursor type. */
|
2004-05-17 23:41:51 +08:00
|
|
|
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 21:20:54 +08:00
|
|
|
int statement_len;
|
2004-05-19 20:37:31 +08:00
|
|
|
zval *driver_options = NULL;
|
|
|
|
long options = 0;
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-05-19 20:37:31 +08:00
|
|
|
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la", &statement,
|
|
|
|
&statement_len, &options, &driver_options)) {
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_DBH_CLEAR_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
stmt = ecalloc(1, sizeof(*stmt));
|
2004-05-19 20:37:31 +08:00
|
|
|
/* unconditionally keep this for later reference */
|
2004-05-18 23:19:31 +08:00
|
|
|
stmt->query_string = estrndup(statement, statement_len);
|
|
|
|
stmt->query_stringlen = statement_len;
|
2004-05-19 20:37:31 +08:00
|
|
|
if (dbh->methods->preparer(dbh, statement, statement_len, stmt, options, driver_options TSRMLS_CC)) {
|
2004-05-17 23:41:51 +08: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;
|
2004-05-21 21:26:58 +08:00
|
|
|
|
|
|
|
/* we haven't created a lazy object yet */
|
|
|
|
ZVAL_NULL(&stmt->lazy_object_ref);
|
2004-05-26 01:43:24 +08:00
|
|
|
|
|
|
|
stmt->refcount = 1;
|
2004-05-17 23:41:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
efree(stmt);
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2004-06-24 02:06:00 +08:00
|
|
|
/* }}} */
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-05-19 21:43:07 +08:00
|
|
|
/* {{{ proto bool PDO::beginTransaction()
|
2004-05-17 23:41:51 +08:00
|
|
|
Initiates a transaction */
|
2004-05-19 21:43:07 +08:00
|
|
|
static PHP_METHOD(PDO, beginTransaction)
|
2004-05-17 23:41:51 +08:00
|
|
|
{
|
|
|
|
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
|
|
|
|
|
|
|
|
if (dbh->in_txn) {
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "There is already an active transaction");
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbh->methods->begin) {
|
2004-05-19 20:37:31 +08:00
|
|
|
/* TODO: this should be an exception; see the auto-commit mode
|
|
|
|
* comments below */
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "This driver doesn't support transactions");
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->methods->begin(dbh TSRMLS_CC)) {
|
|
|
|
dbh->in_txn = 1;
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
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) {
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "There is no active transaction");
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->methods->commit(dbh TSRMLS_CC)) {
|
|
|
|
dbh->in_txn = 0;
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
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) {
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "There is no active transaction");
|
2004-05-17 23:41:51 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->methods->rollback(dbh TSRMLS_CC)) {
|
|
|
|
dbh->in_txn = 0;
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-05-21 03:16:49 +08:00
|
|
|
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(), PDO_ERR_SYNTAX TSRMLS_CC, "Error mode %d is invalid", Z_LVAL_P(value));
|
|
|
|
}
|
|
|
|
RETURN_FALSE;
|
2004-05-21 06:28:53 +08:00
|
|
|
|
|
|
|
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(), PDO_ERR_SYNTAX TSRMLS_CC, "Case folding mode %d is invalid", Z_LVAL_P(value));
|
|
|
|
}
|
|
|
|
RETURN_FALSE;
|
2004-07-19 17:35:36 +08:00
|
|
|
|
|
|
|
case PDO_ATTR_ORACLE_NULLS:
|
|
|
|
convert_to_long(value);
|
|
|
|
dbh->oracle_nulls = Z_LVAL_P(value) ? 1 : 0;
|
|
|
|
RETURN_TRUE;
|
2004-05-21 03:16:49 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2004-05-17 23:41:51 +08:00
|
|
|
if (!dbh->methods->set_attribute) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_DBH_CLEAR_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
if (dbh->methods->set_attribute(dbh, attr, value TSRMLS_CC)) {
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (attr == PDO_ATTR_AUTOCOMMIT) {
|
2004-05-20 08:05:22 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "The auto-commit mode cannot be changed for this driver");
|
2004-05-17 23:41:51 +08:00
|
|
|
} else if (!dbh->methods->set_attribute) {
|
2004-05-26 02:32:48 +08:00
|
|
|
/* XXX: do something better here */
|
2004-05-17 23:41:51 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support setting attributes");
|
2004-05-26 02:32:48 +08:00
|
|
|
} else {
|
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-17 23:41:51 +08:00
|
|
|
}
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-05-26 02:32:48 +08:00
|
|
|
/* {{{ proto mixed PDO::getAttribute(long attribute)
|
|
|
|
Get an attribute */
|
2004-05-21 03:09:35 +08: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;
|
|
|
|
}
|
|
|
|
|
2004-05-21 03:14:44 +08:00
|
|
|
if (!dbh->methods->get_attribute) {
|
2004-05-21 03:09:35 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support fetching attributes");
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PDO_DBH_CLEAR_ERR();
|
|
|
|
switch (dbh->methods->get_attribute(dbh, attr, return_value TSRMLS_CC)) {
|
|
|
|
case -1:
|
|
|
|
PDO_HANDLE_DBH_ERR();
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
|
|
|
case 0:
|
2004-05-26 02:32:48 +08:00
|
|
|
/* XXX: should do something better here */
|
2004-05-21 03:09:35 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support fetching %ld attribute", attr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-05-20 03:27:53 +08:00
|
|
|
/* {{{ proto long PDO::exec(string query)
|
|
|
|
Execute a query that does not return a row set, returning the number of affected rows */
|
2004-05-19 21:43:07 +08:00
|
|
|
static PHP_METHOD(PDO, exec)
|
|
|
|
{
|
|
|
|
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
|
|
|
|
char *statement;
|
2004-06-23 21:20:54 +08:00
|
|
|
int statement_len;
|
2004-05-20 03:27:53 +08:00
|
|
|
long ret;
|
2004-05-19 21:43:07 +08:00
|
|
|
|
|
|
|
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &statement, &statement_len)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!statement_len) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_DBH_CLEAR_ERR();
|
2004-05-20 03:27:53 +08:00
|
|
|
ret = dbh->methods->doer(dbh, statement, statement_len TSRMLS_CC);
|
|
|
|
if(ret == -1) {
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_HANDLE_DBH_ERR();
|
2004-05-19 21:55:41 +08:00
|
|
|
RETURN_FALSE;
|
2004-05-20 08:05:22 +08:00
|
|
|
} else {
|
2004-05-20 03:27:53 +08:00
|
|
|
RETURN_LONG(ret);
|
2004-05-20 01:35:39 +08:00
|
|
|
}
|
2004-05-19 21:43:07 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-05-20 03:27:53 +08:00
|
|
|
|
2004-05-20 01:35:39 +08: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;
|
|
|
|
}
|
|
|
|
|
2004-05-20 08:05:22 +08:00
|
|
|
PDO_DBH_CLEAR_ERR();
|
2004-05-20 01:35:39 +08:00
|
|
|
if (!dbh->methods->last_id) {
|
2004-05-21 02:05:44 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver does not support last inserted id retrieval.");
|
2004-05-20 01:35:39 +08:00
|
|
|
} else {
|
2004-05-20 02:04:47 +08:00
|
|
|
RETURN_LONG(dbh->methods->last_id(dbh TSRMLS_CC));
|
2004-05-20 01:35:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
2004-05-19 21:55:41 +08:00
|
|
|
|
2004-05-21 01:22:13 +08:00
|
|
|
/* {{{ proto int PDO::errorCode()
|
|
|
|
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_LONG(dbh->error_code);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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_long(return_value, dbh->error_code);
|
|
|
|
|
|
|
|
if (dbh->methods->fetch_err) {
|
|
|
|
dbh->methods->fetch_err(dbh, NULL, return_value TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2004-05-17 23:41:51 +08:00
|
|
|
function_entry pdo_dbh_functions[] = {
|
2004-06-24 02:06:00 +08:00
|
|
|
PHP_ME_MAPPING(__construct, dbh_constructor, NULL)
|
2004-05-17 23:41:51 +08:00
|
|
|
PHP_ME(PDO, prepare, NULL, ZEND_ACC_PUBLIC)
|
2004-05-19 21:43:07 +08:00
|
|
|
PHP_ME(PDO, beginTransaction,NULL, ZEND_ACC_PUBLIC)
|
2004-05-17 23:41:51 +08:00
|
|
|
PHP_ME(PDO, commit, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(PDO, rollBack, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(PDO, setAttribute, NULL, ZEND_ACC_PUBLIC)
|
2004-05-19 21:43:07 +08:00
|
|
|
PHP_ME(PDO, exec, NULL, ZEND_ACC_PUBLIC)
|
2004-05-20 01:35:39 +08:00
|
|
|
PHP_ME(PDO, lastInsertId, NULL, ZEND_ACC_PUBLIC)
|
2004-05-21 01:22:13 +08:00
|
|
|
PHP_ME(PDO, errorCode, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(PDO, errorInfo, NULL, ZEND_ACC_PUBLIC)
|
2004-05-21 03:09:35 +08:00
|
|
|
PHP_ME(PDO, getAttribute, NULL, ZEND_ACC_PUBLIC)
|
2004-05-17 23:41:51 +08:00
|
|
|
|
|
|
|
{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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static union _zend_function *dbh_method_get(zval *object, char *method_name, int method_len TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_function *fbc;
|
|
|
|
char *lc_method_name;
|
|
|
|
|
2004-07-06 05:16:29 +08:00
|
|
|
lc_method_name = emalloc(method_len + 1);
|
2004-05-17 23:41:51 +08:00
|
|
|
zend_str_tolower_copy(lc_method_name, method_name, method_len);
|
|
|
|
|
|
|
|
if (zend_hash_find(&pdo_dbh_ce->function_table, lc_method_name, method_len+1, (void**)&fbc) == FAILURE) {
|
2004-07-06 05:16:29 +08:00
|
|
|
efree(lc_method_name);
|
2004-05-17 23:41:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-06 05:16:29 +08:00
|
|
|
efree(lc_method_name);
|
2004-05-17 23:41:51 +08:00
|
|
|
return fbc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dbh_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
|
|
|
|
{
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
|
2004-05-17 23:41:51 +08:00
|
|
|
static union _zend_function *dbh_get_ctor(zval *object TSRMLS_DC)
|
|
|
|
{
|
2004-06-24 02:06:00 +08:00
|
|
|
pdo_dbh_t *dbh = zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
|
|
|
if(dbh->ce != pdo_dbh_ce) {
|
|
|
|
return dbh->ce->constructor;
|
|
|
|
} else {
|
|
|
|
static zend_internal_function ctor = {0};
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
ctor.type = ZEND_INTERNAL_FUNCTION;
|
|
|
|
ctor.function_name = "__construct";
|
|
|
|
ctor.scope = pdo_dbh_ce;
|
|
|
|
ctor.handler = ZEND_FN(dbh_constructor);
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
return (union _zend_function*)&ctor;
|
|
|
|
}
|
2004-05-17 23:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static zend_class_entry *dbh_get_ce(zval *object TSRMLS_DC)
|
|
|
|
{
|
|
|
|
return pdo_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
|
|
|
|
};
|
|
|
|
|
2004-05-18 01:00:35 +08:00
|
|
|
static void pdo_dbh_free_storage(zend_object *object TSRMLS_DC)
|
2004-05-17 23:41:51 +08:00
|
|
|
{
|
|
|
|
pdo_dbh_t *dbh = (pdo_dbh_t*)object;
|
|
|
|
|
|
|
|
if (!dbh) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbh->is_persistent) {
|
|
|
|
/* XXX: don't really free it, just delete the rsrc id */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbh->methods->closer(dbh TSRMLS_CC);
|
2004-05-25 22:12:15 +08:00
|
|
|
|
|
|
|
if (dbh->data_source) {
|
2004-05-25 22:30:07 +08:00
|
|
|
pefree((char *)dbh->data_source, dbh->is_persistent);
|
2004-05-25 22:12:15 +08:00
|
|
|
}
|
|
|
|
if (dbh->username) {
|
2004-05-25 22:30:07 +08:00
|
|
|
pefree(dbh->username, dbh->is_persistent);
|
2004-05-25 22:12:15 +08:00
|
|
|
}
|
|
|
|
if (dbh->password) {
|
2004-05-25 22:30:07 +08:00
|
|
|
pefree(dbh->password, dbh->is_persistent);
|
2004-05-25 22:12:15 +08:00
|
|
|
}
|
|
|
|
|
2004-05-25 22:30:07 +08:00
|
|
|
pefree(dbh, dbh->is_persistent);
|
2004-05-17 23:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zend_object_value pdo_dbh_new(zend_class_entry *ce TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_object_value retval;
|
2004-06-24 02:06:00 +08:00
|
|
|
pdo_dbh_t *dbh;
|
2004-05-17 23:41:51 +08:00
|
|
|
|
2004-06-24 02:06:00 +08:00
|
|
|
dbh = emalloc(sizeof(*dbh));
|
|
|
|
memset(dbh, 0, sizeof(*dbh));
|
|
|
|
dbh->ce = ce;
|
|
|
|
|
|
|
|
retval.handle = zend_objects_store_put(dbh, NULL, pdo_dbh_free_storage, NULL TSRMLS_CC);
|
2004-05-17 23:41:51 +08:00
|
|
|
retval.handlers = &pdo_dbh_object_handlers;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|