Re: PDO custom class for statement
| From: | Ludovic Levesque | Date: | Tue, 09 May 2006 10:19:05 +0000 |
| Subject: | Re: PDO custom class for statement | ||
| References: | 1 2 3 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-3764@lists.php.net to get a copy of this message | ||
After investigation, it seems that $db doesn't keep a pointer to the
class entry from the last request.
The following test (with the test line 707 if (dbh->is_persistent) ...
commented):
<?php
class CustomPDOStatement extends PDOStatement
{
protected function __construct()
{
}
}
echo 'First connection' . "\n";
$dbh = new PDO('pgsql:host=localhost;dbname=fotolia', 'postgres', '',
array(PDO::ATTR_PERSISTENT=> true));
echo 'ATTR_PERSISTENT:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_PERSISTENT));
echo 'ATTR_CASE:' . "\n";
$tmp = $dbh->getAttribute(PDO::ATTR_CASE);
var_dump($tmp);
// change PDO::ATTR_CASE
$dbh->setAttribute(PDO::ATTR_CASE, $tmp == 0);
echo 'ATTR_CASE:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_CASE));
echo 'ATTR_STATEMENT_CLASS:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_STATEMENT_CLASS));
// change PDO::ATTR_STATEMENT_CLASS
$dbh->setAttribute(PDO::ATTR_STATEMENT_CLASS,
array('CustomPDOStatement', array(null, null)));
echo 'ATTR_STATEMENT_CLASS:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_STATEMENT_CLASS));
$dbh = null;
echo "\n";
echo 'Same connection' . "\n";
$dbh = new PDO('pgsql:host=localhost;dbname=fotolia', 'postgres', '',
array(PDO::ATTR_PERSISTENT=> true));
echo 'ATTR_PERSISTENT:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_PERSISTENT));
echo 'ATTR_CASE:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_CASE));
echo 'ATTR_STATEMENT_CLASS:' . "\n";
var_dump($dbh->getAttribute(PDO::ATTR_STATEMENT_CLASS));
Gives:
First connection
ATTR_PERSISTENT:
bool(true)
ATTR_CASE:
int(0)
ATTR_CASE:
int(1)
ATTR_STATEMENT_CLASS:
array(1) {
[0]=>
string(12) "PDOStatement"
}
ATTR_STATEMENT_CLASS:
array(2) {
[0]=>
string(18) "CustomPDOStatement"
[1]=>
array(2) {
[0]=>
NULL
[1]=>
NULL
}
}
Same connection
ATTR_PERSISTENT:
bool(true)
ATTR_CASE:
int(1)
ATTR_STATEMENT_CLASS:
array(1) {
[0]=>
string(12) "PDOStatement"
}
And the value of ATTR_CASE is kept when we make multiple requests with
the same PID via Apache so persistent connections keep their
properties across requests.
For statement custom class, looking at the code and after experiments,
we see these assignments at lines 331-335:
/* let's copy the emalloc bits over from the other handle */
pdbh->ce = dbh->ce;
pdbh->def_stmt_ce = dbh->def_stmt_ce;
pdbh->def_stmt_ctor_args = dbh->def_stmt_ctor_args;
pdbh->properties = dbh->properties;
pdbh is copied from dbh which comes from zend_object_store_get_object
(line 268). dbh object is always initialized with pdo_dbstmt_ce (line
1422 in pdo_dbh_new). This copy happens even if pdbh object comes from
persistent connection list (php_pdo_list_entry), erasing def_stmt_ce.
So persistent connections don't keep references to statement class,
the developer must always call setAttribute(PDO::ATTR_STATEMENT_CLASS)
after connection to have the desired class.
In conclusion, I think (that's just my opinion) the test line 707
should only raise a warning (or notice or nothing) and let the
developer use custom class if he wants.
Thanks for this extension.
Regards,
Ludovic
On 5/4/06, Ludovic Levesque <luddic@gmail.com> wrote:
Ok thanks for the rapid answer, I will take that into account for my code and look in PDO code to fix that. Ludovic On 5/4/06, Wez Furlong <kingwez@gmail.com> wrote: There is no guarantee that the class will exist in subsequent requests, making it unsafe to use. Eg: file1.php: class Foo extends PDOStatement {} $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT=>true)); $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, 'Foo'); then in a separate request (to that same apache child process): file2.php: $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT=>true)); // $db is still referencing a stale pointer to the // Foo class entry from the last request. // this ->query call will crash PHP $stmt = $db->query($sql); This can be fixed, it just hasn't happened yet. In the meantime, we don't let you do it. --Wez. On 5/4/06, Ludovic Levesque <luddic@gmail.com> wrote:Hi everybody, can someone on the list explain me why "PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances" ? line 707 of pdo_dbh.c:if (dbh->is_persistent) { pdo_raise_impl_error(dbh, NULL, "HY000", "PDO::ATTR_STATEMENT_CLASS cannot be used withpersistent PDO instances"TSRMLS_CC); PDO_HANDLE_DBH_ERR(); RETURN_FALSE; }When I test with code commented, nothing fails. Thanks, Ludovic -- PECL development discussion Mailing List (http://pecl.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php