Skip to content

Commit

Permalink
Log query events (#33)
Browse files Browse the repository at this point in the history
Log query events
  • Loading branch information
jessarcher authored Jun 25, 2024
1 parent b851f2f commit f5f1208
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpClickHouseLaravel;

use ClickHouseDB\Client;
use Closure;
use Illuminate\Database\Connection as BaseConnection;

class Connection extends BaseConnection
Expand Down Expand Up @@ -76,18 +77,34 @@ public function select($query, $bindings = [], $useReadPdo = true): array
{
$query = QueryGrammar::prepareParameters($query);

return $this->client->select($query, $bindings)->rows();
return $this->run($query, $bindings, function ($query, $bindings) {
return $this->client->select($query, $bindings)->rows();
});
}

/** @inheritDoc */
public function statement($query, $bindings = []): bool
{
return !$this->client->write($query, $bindings)->isError();
return $this->run($query, $bindings, function ($query, $bindings) {
return !$this->client->write($query, $bindings)->isError();
});
}

/** @inheritDoc */
public function affectingStatement($query, $bindings = []): int
{
return (int)$this->statement($query, $bindings);
}

/** @inheritDoc */
protected function run($query, $bindings, Closure $callback)
{
$start = microtime(true);

$result = $callback($query, $bindings);

$this->logQuery($query, $bindings, $this->getElapsedTime($start));

return $result;
}
}

0 comments on commit f5f1208

Please sign in to comment.