Skip to content

Commit

Permalink
Added explain() for Hyperf\Database\Query\Builder. (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s committed May 15, 2024
1 parent 416f63a commit 27de4bf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Concerns/ExplainsQueries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Database\Concerns;

use Hyperf\Collection\Collection;
use Hyperf\Database\Query\Builder;

/**
* @mixin Builder
*/
trait ExplainsQueries
{
/**
* Explains the query.
*/
public function explain(): Collection
{
$sql = $this->toSql();

$bindings = $this->getBindings();

$explanation = $this->getConnection()->select('EXPLAIN ' . $sql, $bindings);

return new Collection($explanation);
}
}
3 changes: 2 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Contract\PaginatorInterface;
use Hyperf\Database\Concerns\BuildsQueries;
use Hyperf\Database\Concerns\ExplainsQueries;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\Exception\InvalidBindingException;
use Hyperf\Database\Model\Builder as ModelBuilder;
Expand All @@ -44,7 +45,7 @@

class Builder
{
use BuildsQueries, ForwardsCalls, Macroable {
use BuildsQueries, ExplainsQueries, ForwardsCalls, Macroable {
__call as macroCall;
}

Expand Down
40 changes: 39 additions & 1 deletion tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Hyperf\Database\Query\Processors\MySqlProcessor;
use Hyperf\Database\Query\Processors\Processor;
use Mockery as m;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -726,6 +727,43 @@ public function testEmptyWhereIntegerInRaw(): void
$this->assertEquals([], $builder->getBindings());
}

public function testExplain(): void
{
$builder = $this->getBuilder();
/**
* @var ConnectionInterface|MockInterface $connection
*/
$connection = $builder->getConnection();
$connection->allows('select')
->once()
->withArgs(function ($sql, $bindings) {
$this->assertSame($sql, 'EXPLAIN select * from "users" where 0 = 1');
$this->assertIsArray($bindings);
$this->assertCount(0, $bindings);
return $sql === 'EXPLAIN select * from "users" where 0 = 1' && $bindings === [];
})
->andReturn([]);
$builder->select('*')->from('users')->whereIntegerInRaw('id', []);
$this->assertCount(0, $builder->explain());

$builder = $this->getBuilder();
/**
* @var ConnectionInterface|MockInterface $connection
*/
$connection = $builder->getConnection();
$connection->allows('select')
->once()
->withArgs(function ($sql, $bindings) {
$this->assertSame($sql, 'EXPLAIN select * from "hyperf" where "id" in (?, ?, ?)');
$this->assertIsArray($bindings);
$this->assertCount(3, $bindings);
return $sql === 'EXPLAIN select * from "hyperf" where "id" in (?, ?, ?)' && $bindings === [1, 2, 3];
})
->andReturn([]);
$builder->select('*')->from('hyperf')->whereIn('id', [1, 2, 3]);
$this->assertCount(0, $builder->explain());
}

public function testEmptyWhereIntegerNotInRaw(): void
{
$builder = $this->getBuilder();
Expand Down Expand Up @@ -831,7 +869,7 @@ protected function getMySqlBuilderWithProcessor(): Builder
return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor);
}

protected function getMockQueryBuilder(): m\MockInterface
protected function getMockQueryBuilder(): MockInterface
{
return m::mock(Builder::class, [
m::mock(ConnectionInterface::class),
Expand Down

0 comments on commit 27de4bf

Please sign in to comment.