Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wesolowski/14aef51676880cae5597c1231f6ee5fd to your computer and use it in GitHub Desktop.
Save wesolowski/14aef51676880cae5597c1231f6ee5fd to your computer and use it in GitHub Desktop.
Shopware DB-Debug
<?php
public function _execute(array $params = null)
{
try {
$start = microtime(true);
if ($params !== null) {
$ret = $this->_stmt->execute($params);
} else {
$ret = $this->_stmt->execute();
}
$sqlQuery = $this->_stmt->queryString;
if (strpos($sqlQuery, 'db_log') === false) {
Shopware()->Db()->insert(
'db_log',
[
'time' => microtime(true) - $start,
'ident' => $sqlQuery,
'data' => json_encode($params, JSON_PRETTY_PRINT),
'trace' => json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), JSON_PRETTY_PRINT),
'server' => json_encode($_SERVER, JSON_PRETTY_PRINT),
]
);
}
return $ret;
} catch (PDOException $e) {
require_once 'Zend/Db/Statement/Exception.php';
throw new Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
<?php
public function _execute(array $params = null)
{
try {
$start = microtime(true);
if ($params !== null) {
$ret = $this->_stmt->execute($params);
} else {
$ret = $this->_stmt->execute();
}
if (!isset($_SERVER['HTTP_USER_AGENT']) || !isset($_SESSION)) {
} else {
$sqlQuery = $this->_stmt->queryString;
if (strpos($sqlQuery, 'db_log') === false) {
Shopware()->Db()->insert(
'db_log',
[
'time' => microtime(true) - $start,
'ident' => $sqlQuery,
'data' => json_encode($params, JSON_PRETTY_PRINT),
'trace' => json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), JSON_PRETTY_PRINT),
'server' => json_encode($_SERVER, JSON_PRETTY_PRINT),
]
);
}
}
return $ret;
} catch (PDOException $e) {
require_once 'Zend/Db/Statement/Exception.php';
throw new Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
CREATE TABLE IF NOT EXISTS `db_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time` double DEFAULT NULL,
`ident` text,
`data` text,
`trace` text,
`server` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<?php
//vendor/doctrine/dbal/lib/Doctrine/DBAL/Configuration.php:60
public function getSQLLogger()
{
if ($this->_attributes['sqlLogger'] === null ) {
$this->_attributes['sqlLogger'] = new \Doctrine\DBAL\Logging\DebugStack();
}
return $this->_attributes['sqlLogger'] ?? null;
}
// vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/DebugStack.php:21
public function stopQuery()
{
if ($this->enabled) {
$this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
Shopware()->Db()->insert(
'db_log',
[
'time' => microtime(true) - $this->start,
'ident' => $this->queries[$this->currentQuery]['sql'],
'data' => json_encode($this->queries[$this->currentQuery]['params'], JSON_PRETTY_PRINT),
'trace' => json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), JSON_PRETTY_PRINT),
'server' => json_encode($_SERVER, JSON_PRETTY_PRINT),
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment