Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
final class CountQueryBuilder implements BuildsQuery
{
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods;
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods, TapsQueryBuilder;

private CountStatement $count;

Expand Down Expand Up @@ -86,4 +86,9 @@ private function getModel(): ModelInspector
{
return $this->model;
}

private function clone(): self
{
return clone $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class DeleteQueryBuilder implements BuildsQuery
{
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods;
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods, TapsQueryBuilder;

private DeleteStatement $delete;

Expand Down Expand Up @@ -81,4 +81,9 @@ private function getModel(): ModelInspector
{
return $this->model;
}

private function clone(): self
{
return clone $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

final class InsertQueryBuilder implements BuildsQuery
{
use HasConditions, OnDatabase;
use HasConditions, OnDatabase, TapsQueryBuilder;

private InsertStatement $insert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
final class SelectQueryBuilder implements BuildsQuery
{
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods;
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods, TapsQueryBuilder;

private ModelInspector $model;

Expand Down Expand Up @@ -232,11 +232,6 @@ public function build(mixed ...$bindings): Query
return new Query($this->select, [...$this->bindings, ...$bindings])->onDatabase($this->onDatabase);
}

private function clone(): self
{
return clone $this;
}

/** @return \Tempest\Database\Relation[] */
private function getIncludedRelations(): array
{
Expand Down Expand Up @@ -270,4 +265,9 @@ private function getModel(): ModelInspector
{
return $this->model;
}

private function clone(): self
{
return clone $this;
}
}
21 changes: 21 additions & 0 deletions packages/database/src/Builder/QueryBuilders/TapsQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tempest\Database\Builder\QueryBuilders;

use function Tempest\Support\tap;

/**
* @phpstan-require-implements BuildsQuery
*/
trait TapsQueryBuilder
{
/**
* Returns a new instance of the query builder with the given callback applied.
*
* @param callable($this) $callback
*/
public function tap(callable $callback): static
{
return tap($this->clone(), $callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
final class UpdateQueryBuilder implements BuildsQuery
{
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods;
use HasConditions, OnDatabase, HasWhereQueryBuilderMethods, TapsQueryBuilder;

private UpdateStatement $update;

Expand Down Expand Up @@ -153,4 +153,9 @@ private function getModel(): ModelInspector
{
return $this->model;
}

private function clone(): self
{
return clone $this;
}
}
4 changes: 2 additions & 2 deletions packages/support/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ function path(Stringable|string ...$parts): Path
* @template T
*
* @param T $value
* @param (Closure(T): void) $callback
* @param (callable(T): void) $callback
*
* @return T
*/
function tap(mixed $value, Closure $callback): mixed
function tap(mixed $value, callable $callback): mixed
{
$callback($value);

Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Database/Builder/CountQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,21 @@ public function test_multiple_where_field(): void

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_tap(): void
{
$this->migrate(CreateMigrationsTable::class, CreatePublishersTable::class, CreateAuthorTable::class);

query('authors')->insert(
['id' => 1, 'name' => 'Brent'],
['id' => 2, 'name' => 'Other'],
)->execute();

$count = query('authors')
->count()
->tap(fn (CountQueryBuilder $query) => $query->where('id = ?', 1))
->execute();

$this->assertSame(1, $count);
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Database/Builder/DeleteQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@ public function test_multiple_where_field(): void

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_tap(): void
{
$query = query('foo')
->delete()
->tap(fn (DeleteQueryBuilder $query) => $query->where('bar = ?', 'boo'))
->build();

$this->assertSameWithoutBackticks(<<<SQL
DELETE FROM `foo`
WHERE bar = ?
SQL, $query->toSql());

$this->assertSameWithoutBackticks('boo', $query->bindings[0]);
}
}
41 changes: 41 additions & 0 deletions tests/Integration/Database/Builder/SelectQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,47 @@ public function test_group_by(): void
$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_tap(): void
{
$sql = query('authors')
->select()
->tap(fn (SelectQueryBuilder $query) => $query->where('name = ?', 'Brent'))
->toSql();

$expected = <<<SQL
SELECT *
FROM authors
WHERE name = ?
SQL;

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_tap_invokable(): void
{
$sql = query('authors')
->select()
->tap(new readonly class('Brent') {
public function __construct(
private string $author,
) {}

public function __invoke(SelectQueryBuilder $query): void
{
$query->where('name = ?', $this->author);
}
})
->toSql();

$expected = <<<SQL
SELECT *
FROM authors
WHERE name = ?
SQL;

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_having(): void
{
$sql = query('authors')
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Database/Builder/UpdateQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,20 @@ public function test_multiple_where_field(): void

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_tap(): void
{
$query = query(Book::class)
->update(title: 'Chapter 02')
->tap(fn (UpdateQueryBuilder $query) => $query->where('id = ?', 10))
->build();

$this->assertSameWithoutBackticks(<<<SQL
UPDATE `books`
SET `title` = ?
WHERE `id` = ?
SQL, $query->toSql());

$this->assertSame(['Chapter 02', 10], $query->bindings);
}
}
Loading