init
This commit is contained in:
+176
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
* @link https://cakephp.org CakePHP(tm) Project
|
||||
* @since 3.0.0
|
||||
* @license https://opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
namespace Cake\Database\Log;
|
||||
|
||||
use Cake\Database\Driver\Sqlserver;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* Contains a query string, the params used to executed it, time taken to do it
|
||||
* and the number of rows found or affected by its execution.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class LoggedQuery implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Driver executing the query
|
||||
*
|
||||
* @var \Cake\Database\DriverInterface|null
|
||||
*/
|
||||
public $driver = null;
|
||||
|
||||
/**
|
||||
* Query string that was executed
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $query = '';
|
||||
|
||||
/**
|
||||
* Number of milliseconds this query took to complete
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
public $took = 0;
|
||||
|
||||
/**
|
||||
* Associative array with the params bound to the query string
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $params = [];
|
||||
|
||||
/**
|
||||
* Number of rows affected or returned by the query execution
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $numRows = 0;
|
||||
|
||||
/**
|
||||
* The exception that was thrown by the execution of this query
|
||||
*
|
||||
* @var \Exception|null
|
||||
*/
|
||||
public $error;
|
||||
|
||||
/**
|
||||
* Helper function used to replace query placeholders by the real
|
||||
* params used to execute the query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function interpolate(): string
|
||||
{
|
||||
$params = array_map(function ($p) {
|
||||
if ($p === null) {
|
||||
return 'NULL';
|
||||
}
|
||||
|
||||
if (is_bool($p)) {
|
||||
if ($this->driver instanceof Sqlserver) {
|
||||
return $p ? '1' : '0';
|
||||
}
|
||||
|
||||
return $p ? 'TRUE' : 'FALSE';
|
||||
}
|
||||
|
||||
if (is_string($p)) {
|
||||
// Likely binary data like a blob or binary uuid.
|
||||
// pattern matches ascii control chars.
|
||||
if (preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $p) !== $p) {
|
||||
$p = bin2hex($p);
|
||||
}
|
||||
|
||||
$replacements = [
|
||||
'$' => '\\$',
|
||||
'\\' => '\\\\\\\\',
|
||||
"'" => "''",
|
||||
];
|
||||
|
||||
$p = strtr($p, $replacements);
|
||||
|
||||
return "'$p'";
|
||||
}
|
||||
|
||||
return $p;
|
||||
}, $this->params);
|
||||
|
||||
$keys = [];
|
||||
$limit = is_int(key($params)) ? 1 : -1;
|
||||
foreach ($params as $key => $param) {
|
||||
$keys[] = is_string($key) ? "/:$key\b/" : '/[?]/';
|
||||
}
|
||||
|
||||
return preg_replace($keys, $params, $this->query, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the logging context data for a query.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getContext(): array
|
||||
{
|
||||
return [
|
||||
'numRows' => $this->numRows,
|
||||
'took' => $this->took,
|
||||
'role' => $this->driver ? $this->driver->getRole() : '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data that will be serialized as JSON
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$error = $this->error;
|
||||
if ($error !== null) {
|
||||
$error = [
|
||||
'class' => get_class($error),
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'query' => $this->query,
|
||||
'numRows' => $this->numRows,
|
||||
'params' => $this->params,
|
||||
'took' => $this->took,
|
||||
'error' => $error,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of this logged query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
$sql = $this->query;
|
||||
if (!empty($this->params)) {
|
||||
$sql = $this->interpolate();
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
* @link https://cakephp.org CakePHP(tm) Project
|
||||
* @since 3.0.0
|
||||
* @license https://opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
namespace Cake\Database\Log;
|
||||
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Database\Exception\DatabaseException;
|
||||
use Cake\Database\Statement\StatementDecorator;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use function Cake\Core\deprecationWarning;
|
||||
|
||||
/**
|
||||
* Statement decorator used to
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class LoggingStatement extends StatementDecorator
|
||||
{
|
||||
/**
|
||||
* Logger instance responsible for actually doing the logging task
|
||||
*
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $_logger;
|
||||
|
||||
/**
|
||||
* Holds bound params
|
||||
*
|
||||
* @var array<array>
|
||||
*/
|
||||
protected $_compiledParams = [];
|
||||
|
||||
/**
|
||||
* Query execution start time.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
protected $startTime = 0.0;
|
||||
|
||||
/**
|
||||
* Logged query
|
||||
*
|
||||
* @var \Cake\Database\Log\LoggedQuery|null
|
||||
*/
|
||||
protected $loggedQuery;
|
||||
|
||||
/**
|
||||
* Wrapper for the execute function to calculate time spent
|
||||
* and log the query afterwards.
|
||||
*
|
||||
* @param array|null $params List of values to be bound to query
|
||||
* @return bool True on success, false otherwise
|
||||
* @throws \Exception Re-throws any exception raised during query execution.
|
||||
*/
|
||||
public function execute(?array $params = null): bool
|
||||
{
|
||||
$this->startTime = microtime(true);
|
||||
|
||||
$this->loggedQuery = new LoggedQuery();
|
||||
$this->loggedQuery->driver = $this->_driver;
|
||||
$this->loggedQuery->params = $params ?: $this->_compiledParams;
|
||||
|
||||
try {
|
||||
$result = parent::execute($params);
|
||||
$this->loggedQuery->took = (int)round((microtime(true) - $this->startTime) * 1000, 0);
|
||||
} catch (Exception $e) {
|
||||
$this->loggedQuery->error = $e;
|
||||
$this->_log();
|
||||
|
||||
if (Configure::read('Error.convertStatementToDatabaseException', false) === true) {
|
||||
$code = $e->getCode();
|
||||
if (!is_int($code)) {
|
||||
$code = null;
|
||||
}
|
||||
|
||||
throw new DatabaseException([
|
||||
'message' => $e->getMessage(),
|
||||
'queryString' => $this->queryString,
|
||||
], $code, $e);
|
||||
}
|
||||
|
||||
if (version_compare(PHP_VERSION, '8.2.0', '<')) {
|
||||
deprecationWarning(
|
||||
'4.4.12 - Having queryString set on exceptions is deprecated.' .
|
||||
'If you are not using this attribute there is no action to take.' .
|
||||
'Otherwise, enable Error.convertStatementToDatabaseException.'
|
||||
);
|
||||
/** @psalm-suppress UndefinedPropertyAssignment */
|
||||
$e->queryString = $this->queryString;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if (preg_match('/^(?!SELECT)/i', $this->queryString)) {
|
||||
$this->rowCount();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetch($type = self::FETCH_TYPE_NUM)
|
||||
{
|
||||
$record = parent::fetch($type);
|
||||
|
||||
if ($this->loggedQuery) {
|
||||
$this->rowCount();
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchAll($type = self::FETCH_TYPE_NUM)
|
||||
{
|
||||
$results = parent::fetchAll($type);
|
||||
|
||||
if ($this->loggedQuery) {
|
||||
$this->rowCount();
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function rowCount(): int
|
||||
{
|
||||
$result = parent::rowCount();
|
||||
|
||||
if ($this->loggedQuery) {
|
||||
$this->loggedQuery->numRows = $result;
|
||||
$this->_log();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the logging data to the passed LoggedQuery and sends it
|
||||
* to the logging system.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _log(): void
|
||||
{
|
||||
if ($this->loggedQuery === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->loggedQuery->query = $this->queryString;
|
||||
$this->getLogger()->debug((string)$this->loggedQuery, ['query' => $this->loggedQuery]);
|
||||
|
||||
$this->loggedQuery = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for bindValue function to gather each parameter to be later used
|
||||
* in the logger function.
|
||||
*
|
||||
* @param string|int $column Name or param position to be bound
|
||||
* @param mixed $value The value to bind to variable in query
|
||||
* @param string|int|null $type PDO type or name of configured Type class
|
||||
* @return void
|
||||
*/
|
||||
public function bindValue($column, $value, $type = 'string'): void
|
||||
{
|
||||
parent::bindValue($column, $value, $type);
|
||||
|
||||
if ($type === null) {
|
||||
$type = 'string';
|
||||
}
|
||||
if (!ctype_digit($type)) {
|
||||
$value = $this->cast($value, $type)[0];
|
||||
}
|
||||
$this->_compiledParams[$column] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a logger
|
||||
*
|
||||
* @param \Psr\Log\LoggerInterface $logger Logger object
|
||||
* @return void
|
||||
*/
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
$this->_logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logger object
|
||||
*
|
||||
* @return \Psr\Log\LoggerInterface logger instance
|
||||
*/
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
return $this->_logger;
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
* @link https://cakephp.org CakePHP(tm) Project
|
||||
* @since 3.0.0
|
||||
* @license https://opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
namespace Cake\Database\Log;
|
||||
|
||||
use Cake\Log\Engine\BaseLog;
|
||||
use Cake\Log\Log;
|
||||
|
||||
/**
|
||||
* This class is a bridge used to write LoggedQuery objects into a real log.
|
||||
* by default this class use the built-in CakePHP Log class to accomplish this
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class QueryLogger extends BaseLog
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array<string, mixed> $config Configuration array
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->_defaultConfig['scopes'] = ['queriesLog'];
|
||||
$this->_defaultConfig['connection'] = '';
|
||||
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$context['scope'] = $this->scopes() ?: ['queriesLog'];
|
||||
$context['connection'] = $this->getConfig('connection');
|
||||
|
||||
if ($context['query'] instanceof LoggedQuery) {
|
||||
$context = $context['query']->getContext() + $context;
|
||||
$message = 'connection={connection} role={role} duration={took} rows={numRows} ' . $message;
|
||||
}
|
||||
Log::write('debug', (string)$message, $context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user