Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Generator;
use PDO;
use PDOStatement;
use Throwable;

/**
* Decorator for PDO instances.
Expand Down Expand Up @@ -142,7 +143,15 @@ public function perform(
array $values = []
) : PDOStatement
{
$sth = $this->prepare($statement);
try {
$sth = $this->prepare($statement);
} catch (Throwable $error) {
$this->addLogEntry(
$this->newLogEntry($statement, $error)
);

throw $error;
}

foreach ($values as $name => $args) {
$this->performBind($sth, $name, $args);
Expand Down Expand Up @@ -180,9 +189,11 @@ protected function performBind(
public function query(string $statement, mixed ...$fetch) : PDOStatement|false
{
$entry = $this->newLogEntry($statement);
$sth = $this->pdo->query($statement, ...$fetch);
$this->addLogEntry($entry);
return $sth;
try {
return $this->pdo->query($statement, ...$fetch);
} finally {
$this->addLogEntry($entry);
}
}

/* Fetching */
Expand Down Expand Up @@ -389,16 +400,17 @@ public function setQueryLogger(callable $queryLogger) : void
$this->queryLogger = $queryLogger;
}

protected function newLogEntry(?string $statement = null) : array
protected function newLogEntry(?string $statement = null, ?\Throwable $error = null) : array
{
return [
'start' => microtime(true),
'finish' => null,
'duration' => null,
'performed' => null,
'performed' => $error ? false : null,
'statement' => $statement,
'values' => [],
'trace' => null,
'trace' => $error ? $error->getTrace() : null,
'error' => $error,
];
}

Expand All @@ -408,6 +420,10 @@ protected function addLogEntry(array $entry) : void
return;
}

if ($entry['error']) {
$entry['performed'] = false;
}

if ($entry['performed'] === null) {
$entry['performed'] = true;
}
Expand Down
9 changes: 6 additions & 3 deletions src/LoggedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ protected function __construct(
protected array $logEntry
) {
$this->logEntry['statement'] = $this->queryString;
$this->logEntry['start'] = microtime(true);
}

public function execute(?array $inputParameters = null) : bool
{
$result = parent::execute($inputParameters);
$this->log($inputParameters);
return $result;
try {
return parent::execute($inputParameters);
} finally {
$this->log($inputParameters);
}
}

public function bindValue(
Expand Down