-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query): postgresql specific explain output
- Loading branch information
Showing
4 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\PostgresqlEnhanced\Query; | ||
|
||
use Illuminate\Database\Query\Builder as BaseBuilder; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
|
||
class Builder extends BaseBuilder | ||
{ | ||
public function explain(bool $analyze = false): Collection | ||
{ | ||
$version = $this->getConnection()->selectOne('SHOW server_version')->server_version; | ||
$options = match (true) { | ||
$analyze && version_compare($version, '13') >= 0 => 'ANALYZE TRUE, BUFFERS TRUE, SETTINGS TRUE, VERBOSE TRUE, WAL TRUE', | ||
$analyze && version_compare($version, '12') >= 0 => 'ANALYZE TRUE, BUFFERS TRUE, SETTINGS TRUE, VERBOSE TRUE', | ||
$analyze => 'ANALYZE TRUE, BUFFERS TRUE, VERBOSE TRUE', | ||
version_compare($version, '12') >= 0 => 'SETTINGS TRUE, SUMMARY TRUE, VERBOSE TRUE', | ||
default => 'SUMMARY TRUE, VERBOSE TRUE', | ||
}; | ||
|
||
return (new Collection($this->getConnection()->select("EXPLAIN ({$options}) {$this->toSql()}", $this->getBindings()))) | ||
->map(fn ($row) => Arr::first($row)) | ||
->reduce(function (?Collection $carry, string $item) { | ||
if (null === $carry) { | ||
return new Collection([$item]); | ||
} | ||
|
||
return new Collection(["{$carry[0]}\n{$item}"]); | ||
}); | ||
} | ||
} |