| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?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 Cake\Database\DriverInterface;
- use Exception;
- use JsonSerializable;
- use Stringable;
- /**
- * 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, Stringable
- {
- /**
- * Driver executing the query
- *
- * @var \Cake\Database\DriverInterface|null
- */
- protected ?DriverInterface $driver = null;
- /**
- * Query string that was executed
- *
- * @var string
- */
- protected string $query = '';
- /**
- * Number of milliseconds this query took to complete
- *
- * @var float
- */
- protected float $took = 0;
- /**
- * Associative array with the params bound to the query string
- *
- * @var array
- */
- protected array $params = [];
- /**
- * Number of rows affected or returned by the query execution
- *
- * @var int
- */
- protected int $numRows = 0;
- /**
- * The exception that was thrown by the execution of this query
- *
- * @var \Exception|null
- */
- protected ?Exception $error = null;
- /**
- * 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,
- ];
- }
- /**
- * Set logging context for this query.
- *
- * @param array $context Context data.
- * @return void
- */
- public function setContext(array $context): void
- {
- foreach ($context as $key => $val) {
- $this->{$key} = $val;
- }
- }
- /**
- * 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;
- }
- }
|