Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
d587fde2e8
|
|||
c74789faef
|
|||
1a339d4644
|
|||
491ceeac5a
|
|||
07fcca498d
|
|||
c3aa93e543
|
|||
054ccd8419
|
|||
8df59407d9
|
|||
40012ba334 | |||
b606986b86 | |||
e57e3fe934 | |||
e0bd400107 | |||
7e6687132c | |||
2410dc3203 | |||
e3623e4bd4 | |||
466bb61afc | |||
5fd1cf0b81 | |||
2488b19301 | |||
9c3206ee73 | |||
5de9a138f8 | |||
b58b674c3a | |||
7da3397db4 |
BIN
.gitattributes
vendored
Normal file
BIN
.gitattributes
vendored
Normal file
Binary file not shown.
13
CHANGELOG.md
13
CHANGELOG.md
@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
---
|
||||
|
||||
## [1.2.0] - 2020-04-30
|
||||
|
||||
### Added
|
||||
|
||||
- Format SQL query
|
||||
- Measures the SQL execution time
|
||||
|
||||
---
|
||||
|
||||
## [1.1.0] - 2019-09-20
|
||||
|
||||
### Added
|
||||
@ -13,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- add log message
|
||||
- add position specifications of the calling source code
|
||||
|
||||
---
|
||||
|
||||
## [1.0.0] - 2019-07-21
|
||||
|
||||
available in tumtum/oxid-sql-logger package only
|
||||
|
@ -24,9 +24,12 @@ $list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.
|
||||
|
||||
Browser:
|
||||
|
||||

|
||||

|
||||
|
||||
CLI:
|
||||
|
||||

|
||||

|
||||
|
||||
## Credits
|
||||
|
||||
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.
|
||||
|
@ -3,7 +3,8 @@
|
||||
"description": "Returns all SQL queries in the browser.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"monolog/monolog": "^1"
|
||||
"monolog/monolog": "^1",
|
||||
"nilportugues/sql-query-formatter": "^1.2.2"
|
||||
},
|
||||
"license": "GPL-3.0",
|
||||
"autoload": {
|
||||
|
@ -15,7 +15,10 @@ use Doctrine\DBAL\Configuration;
|
||||
*/
|
||||
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database
|
||||
{
|
||||
public static function enableLogger($message = null)
|
||||
/**
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
*/
|
||||
public function d3EnableLogger($message)
|
||||
{
|
||||
$trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
|
||||
@ -33,17 +36,20 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
|
||||
}
|
||||
|
||||
/**
|
||||
* @return OxidSQLLogger
|
||||
* @return mixed
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
*/
|
||||
public static function getLogger()
|
||||
public function d3GetLogger()
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
return $dbalConfig->getSQLLogger();
|
||||
}
|
||||
|
||||
public static function disableLogger()
|
||||
/**
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
*/
|
||||
public function d3DisableLogger()
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
|
@ -7,8 +7,10 @@
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use D3\ModCfg\Application\Model\d3database;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Monolog;
|
||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||
|
||||
/**
|
||||
* Class OxidSQLLogger
|
||||
@ -21,6 +23,11 @@ class OxidSQLLogger implements SQLLogger
|
||||
public $logStartingClass;
|
||||
public $logStartingFunction;
|
||||
|
||||
/**
|
||||
* @var SQLQuery
|
||||
*/
|
||||
private $SQLQuery = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@ -42,19 +49,37 @@ class OxidSQLLogger implements SQLLogger
|
||||
*/
|
||||
public function startQuery($sql, array $params = null, array $types = null)
|
||||
{
|
||||
Monolog\Registry::sql()->addDebug(
|
||||
$this->message ? $this->message : $sql,
|
||||
[
|
||||
'query' => $sql,
|
||||
'params' => $params,
|
||||
'types' => $types,
|
||||
'logStartingFile' => $this->logStartingFile,
|
||||
'logStartingLine' => $this->logStartingLine,
|
||||
'logStartingClass' => $this->logStartingClass,
|
||||
'logStartingFunction' => $this->logStartingFunction,
|
||||
if ($this->SQLQuery) {
|
||||
$this->SQLQuery->setCanceled();
|
||||
$this->stopQuery();
|
||||
}
|
||||
|
||||
]
|
||||
);
|
||||
$this->getPreparedStatementQuery($sql, $params);
|
||||
|
||||
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
||||
->setParams($params)
|
||||
->setTypes($types)
|
||||
->setLogStartingFile($this->logStartingFile)
|
||||
->setLogStartingLine($this->logStartingLine)
|
||||
->setLogStartingClass($this->logStartingClass)
|
||||
->setLogStartingFunction($this->logStartingFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
*/
|
||||
public function getPreparedStatementQuery(&$sql, array $params = null)
|
||||
{
|
||||
if (class_exists(d3database::class)
|
||||
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
||||
&& count($params)
|
||||
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params))
|
||||
&& strlen(trim($query))
|
||||
) {
|
||||
$sql = $query;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,5 +87,24 @@ class OxidSQLLogger implements SQLLogger
|
||||
*/
|
||||
public function stopQuery()
|
||||
{
|
||||
if ($this->SQLQuery) {
|
||||
$formatter = new Formatter();
|
||||
|
||||
Monolog\Registry::sql()->addDebug(
|
||||
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ? $this->message : $this->SQLQuery->getSql() ),
|
||||
[
|
||||
'query' => $formatter->format($this->SQLQuery->getSql()),
|
||||
'params' => $this->SQLQuery->getParams(),
|
||||
'time' => $this->SQLQuery->getElapsedTime(),
|
||||
'types' => $this->SQLQuery->getTypes(),
|
||||
'logStartingFile' => $this->SQLQuery->getLogStartingFile(),
|
||||
'logStartingLine' => $this->SQLQuery->getLogStartingLine(),
|
||||
'logStartingClass' => $this->SQLQuery->getLogStartingClass(),
|
||||
'logStartingFunction' => $this->SQLQuery->getLogStartingFunction(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$this->SQLQuery = null;
|
||||
}
|
||||
}
|
||||
|
247
src/SQLQuery.php
Normal file
247
src/SQLQuery.php
Normal file
@ -0,0 +1,247 @@
|
||||
<?php
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 21:56
|
||||
*/
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
/**
|
||||
* Class SQLQuery
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class SQLQuery
|
||||
{
|
||||
/**
|
||||
* @var float|null
|
||||
*/
|
||||
private $start_time = null;
|
||||
|
||||
/**
|
||||
* @var float|null
|
||||
*/
|
||||
private $stop_time = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sql = '';
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $params = null;
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $types = null;
|
||||
|
||||
private $logStartingFile;
|
||||
|
||||
private $logStartingLine;
|
||||
|
||||
private $logStartingClass;
|
||||
|
||||
private $logStartingFunction;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSql()
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setSql($sql)
|
||||
{
|
||||
$this->sql = $sql;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $params
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getTypes()
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $types
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setTypes($types)
|
||||
{
|
||||
$this->types = $types;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFile()
|
||||
{
|
||||
return $this->logStartingFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingFile($file)
|
||||
{
|
||||
$this->logStartingFile = $file;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingLine()
|
||||
{
|
||||
return $this->logStartingLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $line
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingLine($line)
|
||||
{
|
||||
$this->logStartingLine = $line;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingClass()
|
||||
{
|
||||
return $this->logStartingClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $classname
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingClass($classname)
|
||||
{
|
||||
$this->logStartingClass = $classname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFunction()
|
||||
{
|
||||
return $this->logStartingFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $functionname
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingFunction($functionname)
|
||||
{
|
||||
$this->logStartingFunction = $functionname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Statement was cancelled prematurely, an error was thrown.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setCanceled()
|
||||
{
|
||||
$this->start_time = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns elapsed time
|
||||
* @return float|string
|
||||
*/
|
||||
public function getElapsedTime()
|
||||
{
|
||||
if ($this->start_time === null) {
|
||||
return 'Statement canceled';
|
||||
}
|
||||
|
||||
if ($this->stop_time === null) {
|
||||
$end_time = microtime(true);
|
||||
$this->stop_time = $end_time - $this->start_time;
|
||||
}
|
||||
|
||||
return (float)$this->stop_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable elapsed time
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getReadableElapsedTime()
|
||||
{
|
||||
return $this->readableElapsedTime($this->getElapsedTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable elapsed time
|
||||
*
|
||||
* @param float $microtime
|
||||
* @param string $format The format to display (printf format)
|
||||
* @return string
|
||||
*/
|
||||
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3)
|
||||
{
|
||||
if (is_string($microtime)) {
|
||||
return $microtime;
|
||||
}
|
||||
|
||||
if ($microtime >= 1) {
|
||||
$unit = 's';
|
||||
$time = round($microtime, $round);
|
||||
} else {
|
||||
$unit = 'ms';
|
||||
$time = round($microtime*1000);
|
||||
|
||||
$format = preg_replace('/(%.[\d]+f)/', '%d', $format);
|
||||
}
|
||||
|
||||
return sprintf($format, $time, $unit);
|
||||
}
|
||||
}
|
@ -6,9 +6,14 @@
|
||||
*/
|
||||
|
||||
function D3StartSQLLog($message = null) {
|
||||
\D3\OxidSqlLogger\OxidEsalesDatabase::enableLogger($message);
|
||||
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */
|
||||
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
|
||||
function D3StopSQLLog() {
|
||||
\D3\OxidSqlLogger\OxidEsalesDatabase::disableLogger();
|
||||
function D3StopSQLLog()
|
||||
{
|
||||
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */
|
||||
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class);
|
||||
$database->d3DisableLogger();
|
||||
}
|
||||
|
Reference in New Issue
Block a user