Skip to content

Commit 49f019c

Browse files
authored
feat(database): bindings in query methods (tempestphp#859)
1 parent b6064a6 commit 49f019c

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/Tempest/Database/src/Query.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ public function __construct(
1414
) {
1515
}
1616

17-
public function execute(): Id
17+
public function execute(mixed ...$bindings): Id
1818
{
19+
$this->bindings = [...$this->bindings, ...$bindings];
20+
1921
$database = $this->getDatabase();
2022

21-
$database->execute($this);
23+
$query = $this->withBindings($bindings);
24+
25+
$database->execute($query);
2226

23-
return isset($this->bindings['id'])
24-
? new Id($this->bindings['id'])
27+
return isset($query->bindings['id'])
28+
? new Id($query->bindings['id'])
2529
: $database->getLastInsertId();
2630
}
2731

28-
public function fetch(): array
32+
public function fetch(mixed ...$bindings): array
2933
{
30-
return $this->getDatabase()->fetch($this);
34+
return $this->getDatabase()->fetch($this->withBindings($bindings));
3135
}
3236

33-
public function fetchFirst(): ?array
37+
public function fetchFirst(mixed ...$bindings): ?array
3438
{
35-
return $this->getDatabase()->fetchFirst($this);
39+
return $this->getDatabase()->fetchFirst($this->withBindings($bindings));
3640
}
3741

3842
public function getSql(): string
@@ -47,6 +51,15 @@ public function append(string $append): self
4751
return $this;
4852
}
4953

54+
public function withBindings(array $bindings): self
55+
{
56+
$clone = clone $this;
57+
58+
$clone->bindings = [...$clone->bindings, ...$bindings];
59+
60+
return $clone;
61+
}
62+
5063
private function getDatabase(): Database
5164
{
5265
return get(Database::class);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Integration\Database;
4+
5+
use Tempest\Database\Migrations\CreateMigrationsTable;
6+
use Tempest\Database\Query;
7+
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
8+
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
9+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
10+
11+
final class QueryTest extends FrameworkIntegrationTestCase
12+
{
13+
public function test_with_bindings(): void
14+
{
15+
$this->migrate(CreateMigrationsTable::class, CreateAuthorTable::class);
16+
17+
new Author(name: 'A')->save();
18+
new Author(name: 'B')->save();
19+
20+
$this->assertCount(1, new Query('SELECT * FROM authors WHERE name = ?')->fetch('A'));
21+
$this->assertCount(1, new Query('SELECT * FROM authors WHERE name = :name')->fetch(name: 'A'));
22+
$this->assertSame('A', new Query('SELECT * FROM authors WHERE name = ?')->fetchFirst('A')['name']);
23+
$this->assertSame('A', new Query('SELECT * FROM authors WHERE name = :name')->fetchFirst(name: 'A')['name']);
24+
25+
new Query('DELETE FROM authors WHERE name = :name')->execute(name: 'A');
26+
$this->assertCount(0, new Query('SELECT * FROM authors WHERE name = ?')->fetch('A'));
27+
}
28+
}

0 commit comments

Comments
 (0)