Just replying to myself, any advice is welcome from anyone. Here is a little hack i put together from various examples:
PHP Code:
class MyPDO_Wrapper extends PDO
{
public static $log = array();
public $queryCount;
public function __construct( /*$dsn , $username = null, $password = null */ )
{
$args = func_get_args();
$r = call_user_func_array(array($this, 'parent::__construct'), $args);
$this->queryCount = 0;
## PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement.
## Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
$this->setAttribute( PDO::ATTR_STATEMENT_CLASS,
array('MyPDOStatement_Wrapper', array( & $this )) );
}
public function exec ($statement)
{
$this->queryCount++;
$args = func_get_args();
#var_dump($args);
$start = microtime(true);
$r= call_user_func_array(array($this, 'parent::exec'), $args);
$time = microtime(true) - $start;
self::$log[] = array('query' => $args[1], 'time' => sprintf('%01.4f', $time ));
return $r;
}
/**
*
*/
public function query($query)
{
$this->queryCount++;
$args = func_get_args();
#var_dump($args);
$start = microtime(true);
$result = call_user_func_array(array($this, 'parent::query'), $args );
$time = microtime(true) - $start;
self::$log[] = array('query' => $query, 'time' => sprintf('%01.4f', $time ) ) ;
return $result;
}
public static function printLog()
{
$totalTime = 0;
echo '<table border=1><tr><th>Query</th><th>Time (ms)</th></tr>';
foreach(self::$log as $entry)
{
$totalTime += $entry['time'];
echo '<tr><td>' . $entry['query'] . '</td><td>' . $entry['time'] . "</td></tr>\n";
}
echo '<tr><th>' . count(self::$log) . ' queries</th><th>' . $totalTime . "</th></tr>\n";
echo '</table>';
}
public static function printTotalTime()
{
$totalTime = 0;
foreach(self::$log as $entry)
{
$totalTime += $entry['time'];
}
return $totalTime;
}
public function getQueryCount()
{
return $this->queryCount;
}
}#end class
class MyPDOStatement_Wrapper extends PDOStatement
{
private $queryCount;
protected function __construct ( & $this_pdo )
{
$this->queryCount = & $this_pdo->queryCount ;
$this->ptr_mypdo = & $this_pdo;
}
public function execute ($input_parameters = array())
{
$this->queryCount++;
$args = func_get_args();
$start = microtime(true);
$ret = call_user_func_array( array($this, 'parent::execute'), $args);
$time = microtime(true) - $start;
$this->ptr_mypdo-> log[] = array('query' => '[PS] ' . $this->queryString,
'time' => sprintf('%01.4f', $time ) );
return $ret;
}
}#end class.
It should work like:
PHP Code:
try {
$_pdo = new MyPDO_Wrapper("mysql:dbname=". DB_DATABASE .";host=". DB_SERVER , DB_SERVER_USERNAME, DB_SERVER_PASSWORD ,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $exception) {
printf("Failed to obtain database handle %s", $exception->getMessage());
die();
}
$r = $_pdo->query("SELECT ..." );
$ar_r = $r->fetchAll(PDO::FETCH_ASSOC );
PHP Code:
#this could go in the footer of the page
print $_pdo->getQueryCount();
print $_pdo->printTotalTime();
Bookmarks