8
0
Fork 0
oxid-sql-logger/src/SQLQuery.php

236 Zeilen
4.5 KiB
PHP

<?php
2024-02-02 10:59:40 +01:00
/**
2024-02-02 10:59:40 +01:00
* @author Tobias Matthaiou <developer@tobimat.eu>
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/
2024-02-02 10:59:40 +01:00
declare(strict_types=1);
namespace D3\OxidSqlLogger;
class SQLQuery
{
/**
2024-02-02 10:59:40 +01:00
* @var float
*/
2024-02-02 10:59:40 +01:00
private float $start_time;
/**
2024-02-02 10:59:40 +01:00
* @var float
*/
2024-02-02 10:59:40 +01:00
private float $stop_time = 0.0;
/**
* @var string
*/
2024-02-02 10:59:40 +01:00
private string $sql = '';
2024-02-02 10:59:40 +01:00
private ?array $parameters = null;
2024-02-02 10:59:40 +01:00
private ?array $parameterTypes = null;
2024-02-02 11:39:13 +01:00
2024-02-02 10:59:40 +01:00
private string $logStartingFile;
2024-02-02 11:39:13 +01:00
2024-02-02 10:59:40 +01:00
private int $logStartingLine;
2024-02-02 11:39:13 +01:00
2024-02-02 10:59:40 +01:00
private string $logStartingClass;
2024-02-02 11:39:13 +01:00
2024-02-02 10:59:40 +01:00
private string $logStartingFunction;
public function __construct()
{
$this->start_time = microtime(true);
}
/**
* @return string
*/
2024-02-02 10:59:40 +01:00
public function getSql(): string
{
return $this->sql;
}
/**
* @param string $sql
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setSql(string $sql): static
{
$this->sql = $sql;
return $this;
}
/**
2024-02-02 10:59:40 +01:00
* @return array|null
*/
2024-02-02 10:59:40 +01:00
public function getParams(): ?array
{
2024-02-02 10:59:40 +01:00
return $this->parameters;
}
/**
2024-02-02 10:59:40 +01:00
* @param array|null $params
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setParams(array $params = null): static
{
2024-02-02 10:59:40 +01:00
$this->parameters = $params;
return $this;
}
/**
2024-02-02 10:59:40 +01:00
* @return array|null
*/
2024-02-02 10:59:40 +01:00
public function getTypes(): ?array
{
2024-02-02 10:59:40 +01:00
return $this->parameterTypes;
}
/**
2024-02-02 10:59:40 +01:00
* @param array|null $types
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setTypes(array $types = null): static
{
2024-02-02 10:59:40 +01:00
$this->parameterTypes = $types;
return $this;
}
2024-02-02 11:39:13 +01:00
/**
* @return string
*/
2024-02-02 10:59:40 +01:00
public function getLogStartingFile(): string
{
return $this->logStartingFile;
}
2024-02-02 11:39:13 +01:00
/**
2024-02-02 10:59:40 +01:00
* @param string $file
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setLogStartingFile(string $file): static
{
$this->logStartingFile = $file;
return $this;
}
2024-02-02 10:59:40 +01:00
public function getLogStartingLine(): int
{
return $this->logStartingLine;
}
2024-02-02 10:59:40 +01:00
/**
2024-02-02 10:59:40 +01:00
* @param int $line
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setLogStartingLine(int $line): static
{
$this->logStartingLine = $line;
return $this;
}
/**
* @return string
*/
2024-02-02 10:59:40 +01:00
public function getLogStartingClass(): string
{
return $this->logStartingClass;
}
2024-02-02 10:59:40 +01:00
/**
2024-02-02 10:59:40 +01:00
* @param string $classname
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setLogStartingClass(string $classname): static
{
$this->logStartingClass = $classname;
return $this;
}
/**
* @return string
*/
2024-02-02 10:59:40 +01:00
public function getLogStartingFunction(): string
{
return $this->logStartingFunction;
}
2024-02-02 10:59:40 +01:00
/**
2024-02-02 10:59:40 +01:00
* @param string $functionname
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setLogStartingFunction(string $functionname): static
{
$this->logStartingFunction = $functionname;
return $this;
}
/**
* Statement was cancelled prematurely, an error was thrown.
*
2024-02-02 11:33:11 +01:00
* @return static
*/
2024-02-02 10:59:40 +01:00
public function setCanceled(): static
{
2024-02-02 10:59:40 +01:00
$this->start_time = 0.0;
return $this;
}
/**
2024-02-02 10:59:40 +01:00
* Return elapsed time
* @return float
*/
2024-02-02 10:59:40 +01:00
public function getElapsedTime(): float
{
2024-02-02 10:59:40 +01:00
if ($this->start_time === 0.0) {
return 0.0;
}
2024-02-02 10:59:40 +01:00
if ($this->stop_time === 0.0) {
$end_time = microtime(true);
$this->stop_time = $end_time - $this->start_time;
}
return (float)$this->stop_time;
}
/**
2024-02-02 10:59:40 +01:00
* Returns a human-readable elapsed time
*
* @return string
*/
2024-02-02 10:59:40 +01:00
public function getReadableElapsedTime(): string
{
return $this->readableElapsedTime($this->getElapsedTime());
}
/**
2024-02-02 10:59:40 +01:00
* Returns a human-readable elapsed time
*
* @param float $microtime
* @param string $format The format to display (printf format)
2022-08-18 14:18:43 +02:00
* @param int $round
* @return string
*/
2024-02-02 10:59:40 +01:00
protected function readableElapsedTime(float $microtime, string $format = '%.3f%s', int $round = 3): string
{
if ($microtime >= 1) {
$unit = 's';
$time = round($microtime, $round);
} else {
$unit = 'ms';
$time = round($microtime*1000);
2024-02-02 11:33:11 +01:00
$format = (string) preg_replace('/(%.\d+f)/', '%d', $format);
}
return sprintf($format, $time, $unit);
}
}