Skip to content

Commit d7b519e

Browse files
committed
wip
1 parent f6a3f19 commit d7b519e

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
use Tempest\Database\QueryStatements\WhereStatement;
2020
use Tempest\Support\Arr\ImmutableArray;
2121
use Tempest\Support\Conditions\HasConditions;
22-
use UnitEnum;
2322

2423
use function Tempest\Database\model;
2524
use function Tempest\map;
25+
use function Tempest\Support\str;
2626

2727
/**
2828
* @template TModelClass of object
@@ -123,7 +123,10 @@ public function chunk(Closure $closure, int $amountPerChunk = 200): void
123123
/** @return self<TModelClass> */
124124
public function where(string $where, mixed ...$bindings): self
125125
{
126-
if ($this->select->where->isNotEmpty()) {
126+
if (
127+
$this->select->where->isNotEmpty()
128+
&& ! str($where)->trim()->startsWith(['AND', 'OR'])
129+
) {
127130
return $this->andWhere($where, ...$bindings);
128131
}
129132

@@ -136,11 +139,7 @@ public function where(string $where, mixed ...$bindings): self
136139

137140
public function andWhere(string $where, mixed ...$bindings): self
138141
{
139-
$this->select->where[] = new WhereStatement("AND {$where}");
140-
141-
$this->bind(...$bindings);
142-
143-
return $this;
142+
return $this->where("AND {$where}", ...$bindings);
144143
}
145144

146145
public function orWhere(string $where, mixed ...$bindings): self

packages/database/src/QueryStatements/SelectStatement.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tempest\Database\QueryStatements;
44

55
use Stringable;
6-
use Tempest\Database\Builder\FieldDefinition;
76
use Tempest\Database\Builder\TableDefinition;
87
use Tempest\Database\Config\DatabaseDialect;
98
use Tempest\Database\QueryStatement;

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ public function test_multiple_where(): void
8585
$sql = query('books')->select()
8686
->where('title = ?', 'a')
8787
->where('author_id = ?', 1)
88+
->where('OR author_id = ?', 2)
89+
->where('AND author_id <> NULL')
8890
->toSql();
8991

9092
$expected = <<<SQL
9193
SELECT *
9294
FROM `books`
9395
WHERE title = ?
9496
AND author_id = ?
97+
OR author_id = ?
98+
AND author_id <> NULL
9599
SQL;
96100

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

0 commit comments

Comments
 (0)