Re: [PATCH] PDO::setDefaultFetchMode() methode

From: Date: Wed, 19 Oct 2005 14:52:00 +0000
Subject: Re: [PATCH] PDO::setDefaultFetchMode() methode
References: 1  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-3123@lists.php.net to get a copy of this message
Hi ! Yesterday I wondered that this "feature" is not yet implemented. I also want to set the fetchmode at one time, not for every query. Nice idea guy! +1 - Benny On Wed, 2005-10-19 at 14:34 +0200, Andreas Korthaus wrote: > Hi! > > I created a patch against 5.1 branch of PDO, which implements a > setDefaultFetchMode() methode in PDO class. This methode can be used to > set the FetchMode for PDOStatements once in the script/application, so > you can avoid to set a FetchMode for each PDO::query() call, each > statement or each PDOStatement::fetch() in your application, if you are > not happy with PDO::FETCH_BOTH (I'm not), which is hardcoded today. > > e.g. if you want to use PDO::FETCH_OBJ, you have to pass the PDO class > constant to every PDO::query() call in your application, or use > PDOStatement::setFetchMode(PDO::FETCH_OBJ) on every statement. > > I think the PDO API will benefit from setDefaultFetchMode(), because it > belongs behind the database abstraction API and makes it possible to > write code on a consistent abstraction level (IMHO). > > The attatched patch is not complete yet, it only works with simple > FetchModes, without extra parameters. I'm posting now, because I'd like > to know how the chances are, to get something like that into PDO. If > there is no chance, I don't want to put too much time into it to finish > it (I'm not an experienced C programmer, so it takes some time...). > > I'd really appreciate it if a default FetchMode could make it into PDO > class, which has also been requested on php and pecl bug-tracker: > > http://bugs.php.net/bug.php?id=34909 > http://pecl.php.net/bugs/bug.php?id=4732 > > For an experienced C/PDO programmer it is not very difficult I think, > PDO::setDefaultFetchMode() could be based on > > pdo_stmt_setup_fetch_mode() and PHP_METHOD(PDOStatement, setFetchMode): > > http://cvs.php.net/co.php/php-src/ext/pdo/pdo_stmt.c?r=1.128#1648 > > I had to strip most of the code to get it work, but I think you don't > need to change too much, you only have to know WHAT to change and HOW to > change it, and that's where I failed ;-) > > The second issue is to copy the data definition from "_pdo_stmt_t" > struct to "_pdo_dbh_t" struct: > > http://cvs.php.net/co.php/php-src/ext/pdo/php_pdo_driver.h?r=1.69#603 > > Than you only have to set the "default_fetch_type" in the PDO > constructor and pass it to "_pdo_stmt_t" when created. > > What do you think about it? > > Any comments appreciated! > > > best regards > Andreas > plain text document attachment (pdo_default_fetchmode_patch.txt) > Index: ext/pdo/pdo_dbh.c > =================================================================== > RCS file: /repository/php-src/ext/pdo/pdo_dbh.c,v > retrieving revision 1.82.2.11 > diff -u -r1.82.2.11 pdo_dbh.c > --- ext/pdo/pdo_dbh.c 3 Oct 2005 23:27:26 -0000 1.82.2.11 > +++ ext/pdo/pdo_dbh.c 19 Oct 2005 11:30:08 -0000 > @@ -361,6 +361,7 @@ > dbh->password = password ? pestrdup(password, is_persistent) : NULL; > > dbh->auto_commit = pdo_attr_lval(options, PDO_ATTR_AUTOCOMMIT, 1 TSRMLS_CC); > + dbh->default_fetch_type = PDO_FETCH_BOTH; > > if (!dbh->data_source || (username && !dbh->username) || (password && > !dbh->password)) { > php_error_docref(NULL TSRMLS_CC, E_ERROR, "out of memory"); > @@ -555,7 +556,7 @@ > /* 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; > + stmt->default_fetch_type = dbh->default_fetch_type; > stmt->dbh = dbh; > /* give it a reference to me */ > zend_objects_store_add_ref(getThis() TSRMLS_CC); > @@ -977,7 +978,7 @@ > /* 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; > + stmt->default_fetch_type = dbh->default_fetch_type; > stmt->active_query_string = stmt->query_string; > stmt->active_query_stringlen = statement_len; > stmt->dbh = dbh; > @@ -1048,6 +1049,51 @@ > } > /* }}} */ > > +/* {{{ proto bool PDO::setDefaultFetchMode(int mode) > + Changes the default fetch mode for subsequent queries */ > +static PHP_METHOD(PDO, setDefaultFetchMode) > +{ > + pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > + long mode = PDO_FETCH_BOTH; > + > + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mode)) { > + RETURN_FALSE; > + } > + > + PDO_DBH_CLEAR_ERR(); > + PDO_CONSTRUCT_CHECK; > + > + dbh->default_fetch_type = PDO_FETCH_BOTH; > + > + switch (mode) { > + case PDO_FETCH_LAZY: > + case PDO_FETCH_ASSOC: > + case PDO_FETCH_NUM: > + case PDO_FETCH_BOTH: > + case PDO_FETCH_OBJ: > + case PDO_FETCH_BOUND: > + case PDO_FETCH_NAMED: > + break; > + > + case PDO_FETCH_COLUMN: > + case PDO_FETCH_CLASS: > + case PDO_FETCH_INTO: > + do_raise_impl_error(dbh, NULL, "HY000", "FetchMode not implemented yet" > TSRMLS_CC); > + PDO_HANDLE_DBH_ERR(); > + RETURN_FALSE > + > + default: > + do_raise_impl_error(dbh, NULL, "HY000", "FetchMode not available" > TSRMLS_CC); > + PDO_HANDLE_DBH_ERR(); > + RETURN_FALSE > + } > + > + dbh->default_fetch_type = mode; > + > + RETURN_TRUE; > +} > +/* }}} */ > + > /* {{{ proto int PDO::__wakeup() > Prevents use of a PDO instance that has been unserialized */ > static PHP_METHOD(PDO, __wakeup) > @@ -1095,6 +1141,7 @@ > PHP_ME(PDO, errorInfo, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, getAttribute, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, quote, NULL, ZEND_ACC_PUBLIC) > + PHP_ME(PDO, setDefaultFetchMode, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) > PHP_ME(PDO, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) > PHP_ME(PDO, getAvailableDrivers, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) > Index: ext/pdo/php_pdo_driver.h > =================================================================== > RCS file: /repository/php-src/ext/pdo/php_pdo_driver.h,v > retrieving revision 1.66.2.3 > diff -u -r1.66.2.3 php_pdo_driver.h > --- ext/pdo/php_pdo_driver.h 2 Oct 2005 22:11:17 -0000 1.66.2.3 > +++ ext/pdo/php_pdo_driver.h 19 Oct 2005 11:30:08 -0000 > @@ -466,6 +466,9 @@ > > enum pdo_case_conversion native_case, desired_case; > > + /* the default FetchMode for statements */ > + enum pdo_fetch_type default_fetch_type; > + > /* persistent hash key associated with this handle */ > const char *persistent_id; > int persistent_id_len; > > -- > PECL development discussion Mailing List (http://pecl.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.pecl.dev (#3123) next »