2019-08-20 23:34:33 +02:00
|
|
|
<?php
|
2019-09-19 19:52:54 +02:00
|
|
|
|
2019-08-20 23:34:33 +02:00
|
|
|
/**
|
2019-09-19 19:52:54 +02:00
|
|
|
* @author Tobias Matthaiou <developer@tobimat.eu>
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
2019-08-20 23:34:33 +02:00
|
|
|
*/
|
|
|
|
|
2019-09-19 19:52:54 +02:00
|
|
|
namespace D3\OxidSqlLogger;
|
2019-08-20 23:34:33 +02:00
|
|
|
|
2021-01-23 21:53:18 +01:00
|
|
|
use D3\ModCfg\Application\Model\d3database;
|
2019-08-20 23:34:33 +02:00
|
|
|
use Doctrine\DBAL\Logging\SQLLogger;
|
|
|
|
use Monolog;
|
2020-05-01 01:08:23 +02:00
|
|
|
use NilPortugues\Sql\QueryFormatter\Formatter;
|
2022-08-18 14:18:43 +02:00
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
2019-08-20 23:34:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class OxidSQLLogger
|
|
|
|
*/
|
|
|
|
class OxidSQLLogger implements SQLLogger
|
|
|
|
{
|
2019-09-19 16:03:06 +02:00
|
|
|
public $message;
|
2019-09-20 10:40:01 +02:00
|
|
|
public $logStartingFile;
|
|
|
|
public $logStartingLine;
|
|
|
|
public $logStartingClass;
|
|
|
|
public $logStartingFunction;
|
2019-09-19 16:03:06 +02:00
|
|
|
|
2019-10-01 12:17:23 +02:00
|
|
|
/**
|
|
|
|
* @var SQLQuery
|
|
|
|
*/
|
|
|
|
private $SQLQuery = null;
|
|
|
|
|
2019-08-20 23:34:33 +02:00
|
|
|
/**
|
2022-08-18 14:18:43 +02:00
|
|
|
* @param $file
|
|
|
|
* @param $line
|
|
|
|
* @param $class
|
|
|
|
* @param $function
|
|
|
|
* @param null $message
|
2019-08-20 23:34:33 +02:00
|
|
|
*/
|
2019-09-20 10:40:01 +02:00
|
|
|
public function __construct($file, $line, $class, $function, $message = null)
|
2019-08-20 23:34:33 +02:00
|
|
|
{
|
|
|
|
if (!Monolog\Registry::hasLogger('sql')) {
|
2019-08-21 08:06:35 +02:00
|
|
|
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
|
2019-08-20 23:34:33 +02:00
|
|
|
}
|
2019-09-19 16:03:06 +02:00
|
|
|
|
2019-09-20 10:40:01 +02:00
|
|
|
$this->message = $message;
|
|
|
|
$this->logStartingFile = $file;
|
|
|
|
$this->logStartingLine = $line;
|
|
|
|
$this->logStartingClass = $class;
|
|
|
|
$this->logStartingFunction = $function;
|
2019-08-20 23:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-18 14:18:43 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @param array|null $params
|
|
|
|
* @param array|null $types
|
|
|
|
*
|
|
|
|
* @throws DatabaseConnectionException
|
2019-08-20 23:34:33 +02:00
|
|
|
*/
|
|
|
|
public function startQuery($sql, array $params = null, array $types = null)
|
|
|
|
{
|
2019-10-01 12:17:23 +02:00
|
|
|
if ($this->SQLQuery) {
|
|
|
|
$this->SQLQuery->setCanceled();
|
|
|
|
$this->stopQuery();
|
|
|
|
}
|
2019-09-20 10:40:01 +02:00
|
|
|
|
2022-09-05 09:05:54 +02:00
|
|
|
$this->getPreparedStatementQuery($sql, $params ?? []);
|
2021-01-23 21:53:18 +01:00
|
|
|
|
2020-05-01 01:08:23 +02:00
|
|
|
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
2019-10-01 12:17:23 +02:00
|
|
|
->setParams($params)
|
2020-05-01 00:13:59 +02:00
|
|
|
->setTypes($types)
|
|
|
|
->setLogStartingFile($this->logStartingFile)
|
|
|
|
->setLogStartingLine($this->logStartingLine)
|
|
|
|
->setLogStartingClass($this->logStartingClass)
|
|
|
|
->setLogStartingFunction($this->logStartingFunction);
|
2019-08-20 23:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-23 21:53:18 +01:00
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
2022-08-18 14:18:43 +02:00
|
|
|
* @throws DatabaseConnectionException
|
2021-01-23 21:53:18 +01:00
|
|
|
*/
|
2022-08-18 14:18:43 +02:00
|
|
|
public function getPreparedStatementQuery(&$sql, array $params = [])
|
2021-01-23 21:53:18 +01:00
|
|
|
{
|
|
|
|
if (class_exists(d3database::class)
|
|
|
|
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
2021-04-29 15:49:20 +02:00
|
|
|
&& is_array($params)
|
2021-01-23 21:53:18 +01:00
|
|
|
&& count($params)
|
|
|
|
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params))
|
|
|
|
&& strlen(trim($query))
|
|
|
|
) {
|
|
|
|
$sql = $query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-20 23:34:33 +02:00
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function stopQuery()
|
|
|
|
{
|
2019-10-01 12:17:23 +02:00
|
|
|
if ($this->SQLQuery) {
|
2020-05-01 01:08:23 +02:00
|
|
|
$formatter = new Formatter();
|
|
|
|
|
2019-10-01 12:17:23 +02:00
|
|
|
Monolog\Registry::sql()->addDebug(
|
2022-08-18 14:18:43 +02:00
|
|
|
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ?: $this->SQLQuery->getSql() ),
|
2019-10-01 12:17:23 +02:00
|
|
|
[
|
2020-05-01 01:08:23 +02:00
|
|
|
'query' => $formatter->format($this->SQLQuery->getSql()),
|
2019-10-01 12:17:23 +02:00
|
|
|
'params' => $this->SQLQuery->getParams(),
|
|
|
|
'time' => $this->SQLQuery->getElapsedTime(),
|
|
|
|
'types' => $this->SQLQuery->getTypes(),
|
2020-05-01 00:13:59 +02:00
|
|
|
'logStartingFile' => $this->SQLQuery->getLogStartingFile(),
|
|
|
|
'logStartingLine' => $this->SQLQuery->getLogStartingLine(),
|
|
|
|
'logStartingClass' => $this->SQLQuery->getLogStartingClass(),
|
|
|
|
'logStartingFunction' => $this->SQLQuery->getLogStartingFunction(),
|
2019-10-01 12:17:23 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->SQLQuery = null;
|
2019-08-20 23:34:33 +02:00
|
|
|
}
|
|
|
|
}
|