Skip to content

Commit

Permalink
feat(database): bindings in query methods (tempestphp#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Dec 14, 2024
1 parent b6064a6 commit 49f019c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Tempest/Database/src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ public function __construct(
) {
}

public function execute(): Id
public function execute(mixed ...$bindings): Id
{
$this->bindings = [...$this->bindings, ...$bindings];

$database = $this->getDatabase();

$database->execute($this);
$query = $this->withBindings($bindings);

$database->execute($query);

return isset($this->bindings['id'])
? new Id($this->bindings['id'])
return isset($query->bindings['id'])
? new Id($query->bindings['id'])
: $database->getLastInsertId();
}

public function fetch(): array
public function fetch(mixed ...$bindings): array
{
return $this->getDatabase()->fetch($this);
return $this->getDatabase()->fetch($this->withBindings($bindings));
}

public function fetchFirst(): ?array
public function fetchFirst(mixed ...$bindings): ?array
{
return $this->getDatabase()->fetchFirst($this);
return $this->getDatabase()->fetchFirst($this->withBindings($bindings));
}

public function getSql(): string
Expand All @@ -47,6 +51,15 @@ public function append(string $append): self
return $this;
}

public function withBindings(array $bindings): self
{
$clone = clone $this;

$clone->bindings = [...$clone->bindings, ...$bindings];

return $clone;
}

private function getDatabase(): Database
{
return get(Database::class);
Expand Down
28 changes: 28 additions & 0 deletions tests/Integration/Database/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Integration\Database;

use Tempest\Database\Migrations\CreateMigrationsTable;
use Tempest\Database\Query;
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

final class QueryTest extends FrameworkIntegrationTestCase
{
public function test_with_bindings(): void
{
$this->migrate(CreateMigrationsTable::class, CreateAuthorTable::class);

new Author(name: 'A')->save();
new Author(name: 'B')->save();

$this->assertCount(1, new Query('SELECT * FROM authors WHERE name = ?')->fetch('A'));
$this->assertCount(1, new Query('SELECT * FROM authors WHERE name = :name')->fetch(name: 'A'));
$this->assertSame('A', new Query('SELECT * FROM authors WHERE name = ?')->fetchFirst('A')['name']);
$this->assertSame('A', new Query('SELECT * FROM authors WHERE name = :name')->fetchFirst(name: 'A')['name']);

new Query('DELETE FROM authors WHERE name = :name')->execute(name: 'A');
$this->assertCount(0, new Query('SELECT * FROM authors WHERE name = ?')->fetch('A'));
}
}

0 comments on commit 49f019c

Please sign in to comment.