Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
Daniel Seifert | 07fcca498d | |
Daniel Seifert | c3aa93e543 | |
Daniel Seifert | 054ccd8419 | |
Daniel Seifert | 8df59407d9 | |
Tobias Matthaiou | 40012ba334 | |
Tobias Matthaiou | b606986b86 | |
Tobias Matthaiou | e57e3fe934 | |
Tobias Matthaiou | e0bd400107 | |
Tobias Matthaiou | 7e6687132c | |
Tobias Matthaiou | 2410dc3203 | |
Tobias Matthaiou | e3623e4bd4 | |
Tobias Matthaiou | 466bb61afc | |
Tobias Matthaiou | 5fd1cf0b81 | |
Tobias Matthaiou | 2488b19301 | |
Tobias Matthaiou | 9c3206ee73 | |
Tobias Matthaiou | 5de9a138f8 | |
Daniel Seifert | b58b674c3a | |
Daniel Seifert | 7da3397db4 |
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:
|
||||
|
||||
![Example all sqls](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-a.jpg)
|
||||
![Example all sqls](https://raw.githubusercontent.com/d3datadevelopment/oxid-sql-logger/master/img/screenshot-a.jpg)
|
||||
|
||||
CLI:
|
||||
|
||||
![Example CLI](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-cli.jpg)
|
||||
![Example CLI](https://raw.githubusercontent.com/d3datadevelopment/oxid-sql-logger/master/img/screenshot-cli.jpg)
|
||||
|
||||
## Credits
|
||||
|
||||
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.
|
||||
|
|
|
@ -3,16 +3,14 @@
|
|||
"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": {
|
||||
"psr-4": {
|
||||
"D3\\OxidSqlLogger\\": "./src"
|
||||
},
|
||||
"files": [
|
||||
"./src/functions.php"
|
||||
]
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@ namespace D3\OxidSqlLogger;
|
|||
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Monolog;
|
||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||
|
||||
/**
|
||||
* Class OxidSQLLogger
|
||||
|
@ -21,6 +22,11 @@ class OxidSQLLogger implements SQLLogger
|
|||
public $logStartingClass;
|
||||
public $logStartingFunction;
|
||||
|
||||
/**
|
||||
* @var SQLQuery
|
||||
*/
|
||||
private $SQLQuery = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
@ -42,19 +48,18 @@ 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->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
||||
->setParams($params)
|
||||
->setTypes($types)
|
||||
->setLogStartingFile($this->logStartingFile)
|
||||
->setLogStartingLine($this->logStartingLine)
|
||||
->setLogStartingClass($this->logStartingClass)
|
||||
->setLogStartingFunction($this->logStartingFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,5 +67,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue