8
0

22 Commits
1.1.0 ... 1.2.2

Autor SHA1 Nachricht Datum
d587fde2e8 change static methods to non-static methods for better testable code 2021-01-23 21:55:06 +01:00
c74789faef add prepared statement query generator 2021-01-23 21:53:18 +01:00
1a339d4644 change line endings for easy patching 2020-05-25 09:48:12 +02:00
491ceeac5a add missing autoload file item in composer.json 2020-05-01 22:26:26 +02:00
07fcca498d remove Query Status Monitor, complete implementation 2020-05-01 01:08:23 +02:00
c3aa93e543 disable linking smarty plugin 2020-05-01 00:29:56 +02:00
054ccd8419 Merge remote-tracking branch 'upstream/master' 2020-05-01 00:13:59 +02:00
8df59407d9 add SQL formatter 2020-04-30 20:45:41 +02:00
40012ba334 Suitable screenshot 2019-11-06 15:10:45 +01:00
b606986b86 Update CHANGELOG.md 2019-11-06 15:05:36 +01:00
e57e3fe934 Merge pull request #2 from TumTum/feature/sql-status
Feature/sql status
2019-11-06 15:03:02 +01:00
e0bd400107 Documentation to SQL Status Query Monitor 2019-11-06 15:00:55 +01:00
7e6687132c Status display of the DB how many queries were processed. (SHOW STATUS) 2019-11-06 14:44:58 +01:00
2410dc3203 Update README.md 2019-10-04 10:15:41 +02:00
e3623e4bd4 Update CHANGELOG.md 2019-10-01 16:17:07 +02:00
466bb61afc Merge pull request #1 from TumTum/feature/measures_time
Feature/measures time
2019-10-01 12:58:50 +02:00
5fd1cf0b81 Updated documentation 2019-10-01 12:55:33 +02:00
2488b19301 Activate Travis 2019-10-01 12:30:25 +02:00
9c3206ee73 New Featuer: "Measures the sql execution time" 2019-10-01 12:23:02 +02:00
5de9a138f8 Insert PHPSpec Framework 2019-10-01 12:22:43 +02:00
b58b674c3a add credits 2019-09-23 08:45:00 +02:00
7da3397db4 fix README images url 2019-09-23 08:38:19 +02:00
8 geänderte Dateien mit 341 neuen und 22 gelöschten Zeilen

BIN
.gitattributes vendored Normale Datei

Binäre Datei nicht angezeigt.

Datei anzeigen

@ -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

Datei anzeigen

@ -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.

Datei anzeigen

@ -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": {

Datei anzeigen

@ -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();

Datei anzeigen

@ -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 Normale Datei
Datei anzeigen

@ -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);
}
}

Datei anzeigen

@ -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();
}