From d2a8d5a80fbf3ed11494dc7dc46b0e5a1d6fc020 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 25 May 2004 18:32:48 +0000 Subject: [PATCH] get/set attributes for statements --- ext/pdo/pdo_dbh.c | 8 +++-- ext/pdo/pdo_stmt.c | 65 ++++++++++++++++++++++++++++++++++++++++ ext/pdo/php_pdo_driver.h | 9 ++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 6e5e632c6b..8d40d24d32 100755 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -377,12 +377,17 @@ fail: if (attr == PDO_ATTR_AUTOCOMMIT) { zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_NONE TSRMLS_CC, "The auto-commit mode cannot be changed for this driver"); } else if (!dbh->methods->set_attribute) { + /* XXX: do something better here */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support setting attributes"); + } else { + PDO_HANDLE_DBH_ERR(); } RETURN_FALSE; } /* }}} */ +/* {{{ proto mixed PDO::getAttribute(long attribute) + Get an attribute */ static PHP_METHOD(PDO, getAttribute) { pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC); @@ -402,15 +407,14 @@ static PHP_METHOD(PDO, getAttribute) case -1: PDO_HANDLE_DBH_ERR(); RETURN_FALSE; - break; case 0: + /* XXX: should do something better here */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support fetching %ld attribute", attr); break; default: return; - break; } } /* }}} */ diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 2f0cf60e05..9992633319 100755 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -636,7 +636,70 @@ static PHP_METHOD(PDOStatement, errorInfo) } /* }}} */ +/* {{{ proto bool PDOStatement::setAttribute(long attribute, mixed value) + Set an attribute */ +static PHP_METHOD(PDOStatement, setAttribute) +{ + pdo_stmt_t *stmt = (pdo_stmt_t*)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; + } + + if (!stmt->methods->set_attribute) { + goto fail; + } + + PDO_STMT_CLEAR_ERR(); + if (stmt->methods->set_attribute(stmt, attr, value TSRMLS_CC)) { + RETURN_TRUE; + } + +fail: + if (!stmt->methods->set_attribute) { + /* XXX: do something better here */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support setting attributes"); + } else { + PDO_HANDLE_STMT_ERR(); + } + RETURN_FALSE; +} +/* }}} */ +/* {{{ proto mixed PDOStatement::getAttribute(long attribute) + Get an attribute */ +static PHP_METHOD(PDOStatement, getAttribute) +{ + pdo_stmt_t *stmt = (pdo_stmt_t*)zend_object_store_get_object(getThis() TSRMLS_CC); + long attr; + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &attr)) { + RETURN_FALSE; + } + + if (!stmt->methods->get_attribute) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support fetching attributes"); + RETURN_FALSE; + } + + PDO_STMT_CLEAR_ERR(); + switch (stmt->methods->get_attribute(stmt, attr, return_value TSRMLS_CC)) { + case -1: + PDO_HANDLE_STMT_ERR(); + RETURN_FALSE; + + case 0: + /* XXX: should do something better here */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "This driver doesn't support fetching %ld attribute", attr); + break; + + default: + return; + } +} +/* }}} */ function_entry pdo_dbstmt_functions[] = { PHP_ME(PDOStatement, execute, NULL, ZEND_ACC_PUBLIC) @@ -648,6 +711,8 @@ function_entry pdo_dbstmt_functions[] = { PHP_ME(PDOStatement, fetchAll, NULL, ZEND_ACC_PUBLIC) PHP_ME(PDOStatement, errorCode, NULL, ZEND_ACC_PUBLIC) PHP_ME(PDOStatement, errorInfo, NULL, ZEND_ACC_PUBLIC) + PHP_ME(PDOStatement, setAttribute, NULL, ZEND_ACC_PUBLIC) + PHP_ME(PDOStatement, getAttribute, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index 58327804a6..a0f9ac16ab 100755 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -219,6 +219,13 @@ enum pdo_param_event { typedef int (*pdo_stmt_param_hook_func)(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type TSRMLS_DC); +/* setting of attributes */ +typedef int (*pdo_stmt_set_attr_func)(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC); + +/* fetching of attributes */ +typedef int (*pdo_stmt_get_attr_func)(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC); + + struct pdo_stmt_methods { pdo_stmt_dtor_func dtor; pdo_stmt_execute_func executer; @@ -226,6 +233,8 @@ struct pdo_stmt_methods { pdo_stmt_describe_col_func describer; pdo_stmt_get_col_data_func get_col; pdo_stmt_param_hook_func param_hook; + pdo_stmt_set_attr_func set_attribute; + pdo_stmt_get_attr_func get_attribute; }; /* }}} */ -- 2.50.1