Skip to content
Merged
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
9 changes: 6 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ private static function executePrepareQuery(string $sql_statement, array $data =
Sanitize::make($data, true)
);

$start_at = microtime(true);
$pdo_statement->execute();
$ended_at = microtime(true);

static::triggerQueryEvent($sql_statement, $data);
static::triggerQueryEvent($sql_statement, $ended_at - $start_at, $data);

return $pdo_statement->rowCount();
}
Expand Down Expand Up @@ -490,12 +492,13 @@ public static function setPdo(PDO $pdo): void
* Trigger the query executed event
*
* @param string $sql
* @param float $execution_time
* @param array $bindings
* @return void
*/
public static function triggerQueryEvent(string $sql, array $bindings = []): void
public static function triggerQueryEvent(string $sql, float $execution_time = 0, array $bindings = []): void
{
$event = new QueryEvent($sql, $bindings);
$event = new QueryEvent($sql, $execution_time, $bindings);

app_event($event);
}
Expand Down
17 changes: 12 additions & 5 deletions src/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,11 @@ public function truncate(): bool

$this->last_query = $sql;

$start_at = microtime(true);
$result = (bool) $this->connection->exec($sql);
$ended_at = microtime(true);

$this->triggerQueryEvent($sql, []);
$this->triggerQueryEvent($sql, $ended_at - $start_at);

$this->last_query = $sql;

Expand Down Expand Up @@ -1523,9 +1525,11 @@ private function execute(string $sql, array $bindings = []): PDOStatement
$this->bind($statement, $bindings);

try {
$start_at = microtime(true);
$statement->execute();
$ended_at = microtime(true);

$this->triggerQueryEvent($sql, $bindings);
$this->triggerQueryEvent($sql, $ended_at - $start_at, $bindings);
} catch (\Exception $e) {
throw new QueryBuilderException(
'Error executing query: ' . $e->getMessage() . ' | Query: ' . $this->last_query,
Expand All @@ -1548,9 +1552,11 @@ public function drop(): bool

$this->last_query = $sql;

$start_at = microtime(true);
$result = (bool) $this->connection->exec($sql);
$ended_at = microtime(true);

$this->triggerQueryEvent($sql, []);
$this->triggerQueryEvent($sql, $ended_at - $start_at);

return $result;
}
Expand Down Expand Up @@ -1668,12 +1674,13 @@ public function setWhereDataBinding(array $data_binding): void
* Trigger the query event
*
* @param string $sql
* @param float $execution_time
* @param array $bindings
* @return void
*/
private function triggerQueryEvent(string $sql, array $bindings): void
private function triggerQueryEvent(string $sql, float $execution_time = 0, array $bindings = []): void
{
Database::triggerQueryEvent($sql, $bindings);
Database::triggerQueryEvent($sql, $execution_time, $bindings);
}

/**
Expand Down
16 changes: 13 additions & 3 deletions src/Database/QueryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ final class QueryEvent implements AppEvent
*/
public string $sql;

/**
* Define the query execution time
*
* @var mixed
*/
public float $execution_time;

/**
* The query bindings
*
Expand All @@ -28,11 +35,14 @@ final class QueryEvent implements AppEvent
/**
* QueryEvent constructor.
*
* @param array $data
* @param string $sql
* @param float $execution_time
* @param array $bindings
*/
public function __construct(string $sql, array $bindings = [])
public function __construct(string $sql, float $execution_time = 0, array $bindings = [])
{
$this->sql = $sql;
$this->execution_time = $execution_time;
$this->bindings = $bindings;
}

Expand All @@ -51,7 +61,7 @@ public function getName(): string
* @param mixed $value
* @throws \Exception
*/
public function __set($name, $value)
final public function __set($name, $value)
{
throw new \Exception("Cannot set property $name on QueryEvent");
}
Expand Down
Loading