Just out of curiosity I looked further into the “capture sql string” part of the problem. It’s possible to get the effective SQL string from PHPPDO’s db specific statement script.
For the mysql driver mysql_statement.php
protected function _execute()
{
$query = $this->_build_query();
// SQL string now in $query
...
}
The sqlite driver sqlite2_statement.php looks exactly the same but I didn’t test this.
protected function _execute()
{
$query = $this->_build_query();
if(!$query) return false;
$this->_result = @sqlite_query($this->_link, $query, SQLITE_NUM, $errstr);
...
}
From my very limited investigation I can tell there’s at least one more place to care about:
In my small test case, the method exec() of phppdo.php is used to issue the statement “SET NAMES ‘utf8’”
public function exec($statement)
{
// SQL string in $statement
...
}
I also tried to find a way to just hook into PDOStatement using the real PDO, but wasn’t able to get the “effective SQL” (with parameters bound).
My extended PdoStatement installed without problem
Item::model()->getDbConnection()->getPdoInstance()->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPdoStatement'));
class MyPdoStatement extends PDOStatement
{
public function execute($input_parameters = array())
{
$retval = parent::execute($input_parameters);
// 1st try: just unbounded SQL, no params
Yii::log('querystring: '.$this->queryString, 'info', 'sql');
// 2nd try: SQL plus params
ob_start(function(){return '';});
$this->debugDumpParams();
$query = ob_get_flush();
Yii::log('query dump:'.$query, 'info', 'sql');
return $retval;
}
}
Bottom line is: Using PHPPDO it can be done. Without PHPPDO we can get the same unassembled SQL in an even more parsing friendly format from the Yii standard logging.
/Tommy