Skip to content

Commit 7d3950b

Browse files
committed
wip
1 parent f563a13 commit 7d3950b

File tree

4 files changed

+21
-35
lines changed

4 files changed

+21
-35
lines changed

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

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Closure;
88
use Tempest\Database\Builder\FieldDefinition;
99
use Tempest\Database\Builder\ModelDefinition;
10-
use Tempest\Database\Builder\TableDefinition;
10+
use Tempest\Database\Builder\ModelInspector;
1111
use Tempest\Database\Id;
1212
use Tempest\Database\Mappers\SelectModelMapper;
1313
use Tempest\Database\Query;
@@ -33,7 +33,7 @@ final class SelectQueryBuilder implements BuildsQuery
3333
/** @var class-string<TModelClass> $modelClass */
3434
private readonly string $modelClass;
3535

36-
private ?ModelDefinition $modelDefinition;
36+
private ModelInspector $model;
3737

3838
private SelectStatement $select;
3939

@@ -45,15 +45,14 @@ final class SelectQueryBuilder implements BuildsQuery
4545

4646
public function __construct(string|object $model, ?ImmutableArray $fields = null)
4747
{
48-
$this->modelDefinition = ModelDefinition::tryFrom($model);
4948
$this->modelClass = is_object($model) ? $model::class : $model;
50-
$model = model($this->modelClass);
49+
$this->model = model($this->modelClass);
5150

5251
$this->select = new SelectStatement(
53-
table: $this->resolveTable($model),
54-
fields: $fields ?? $model
55-
->getSelectFields()
56-
->map(fn (string $fieldName) => new FieldStatement("{$model->getTableName()}.{$fieldName}")->withAlias()),
52+
table: $this->model->getTableDefinition(),
53+
fields: $fields ?? $this->model
54+
->getSelectFields()
55+
->map(fn (string $fieldName) => new FieldStatement("{$this->model->getTableName()}.{$fieldName}")->withAlias()),
5756
);
5857
}
5958

@@ -64,7 +63,7 @@ public function first(mixed ...$bindings): mixed
6463
{
6564
$query = $this->build(...$bindings);
6665

67-
if (! $this->modelDefinition) {
66+
if (! $this->model->isObjectModel()) {
6867
return $query->fetchFirst();
6968
}
7069

@@ -92,7 +91,7 @@ public function all(mixed ...$bindings): array
9291
{
9392
$query = $this->build(...$bindings);
9493

95-
if (! $this->modelDefinition) {
94+
if (! $this->model->isObjectModel()) {
9695
return $query->fetch();
9796
}
9897

@@ -143,14 +142,10 @@ public function orWhere(string $where, mixed ...$bindings): self
143142
/** @return self<TModelClass> */
144143
public function whereField(string $field, mixed $value): self
145144
{
146-
if ($this->modelDefinition) {
147-
$field = $this->modelDefinition->getFieldDefinition($field);
148-
} else {
149-
$field = new FieldDefinition(
150-
$this->resolveTable($this->modelClass),
151-
$field,
152-
);
153-
}
145+
$field = new FieldDefinition(
146+
$this->model->getTableDefinition(),
147+
$field,
148+
);
154149

155150
return $this->where("{$field} = :{$field->name}", ...[$field->name => $value]);
156151
}
@@ -238,15 +233,6 @@ private function clone(): self
238233
return clone $this;
239234
}
240235

241-
private function resolveTable(string|object $model): TableDefinition
242-
{
243-
if ($this->modelDefinition === null) {
244-
return new TableDefinition($model);
245-
}
246-
247-
return $this->modelDefinition->getTableDefinition();
248-
}
249-
250236
/** @return \Tempest\Database\Relation[] */
251237
private function getIncludedRelations(): array
252238
{

tests/Integration/Database/Builder/InsertQueryBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function test_insert_on_model_table_with_new_relation(): void
123123
VALUES (?)
124124
SQL;
125125

126-
$this->assertSame($expectedAuthorQuery, $authorQuery->getSql());
126+
$this->assertSame($expectedAuthorQuery, $authorQuery->toSql());
127127
$this->assertSame('Brent', $authorQuery->bindings[0]);
128128
}
129129

@@ -201,9 +201,9 @@ public function test_then_method(): void
201201
$book = Book::select()->with('chapters')->get($id);
202202

203203
$this->assertCount(3, $book->chapters);
204-
$this->assertSame('Chapter 01', $book->chapters[1]->title);
205-
$this->assertSame('Chapter 02', $book->chapters[2]->title);
206-
$this->assertSame('Chapter 03', $book->chapters[3]->title);
204+
$this->assertSame('Chapter 01', $book->chapters[0]->title);
205+
$this->assertSame('Chapter 02', $book->chapters[1]->title);
206+
$this->assertSame('Chapter 03', $book->chapters[2]->title);
207207
}
208208

209209
public function test_insert_with_non_object_model(): void

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function test_select_query(): void
3333
->build();
3434

3535
$expected = <<<SQL
36-
SELECT `title`, `index`
36+
SELECT title, index
3737
FROM `chapters`
3838
WHERE `title` = ?
3939
AND `index` <> ?
@@ -69,7 +69,7 @@ public function test_select_from_model(): void
6969
$sql = $query->toSql();
7070

7171
$expected = <<<SQL
72-
SELECT `authors`.`name` AS `authors.name`, `authors`.`type` AS `authors.type`, `authors`.`id` AS `authors.id`
72+
SELECT authors.name AS `authors.name`, authors.type AS `authors.type`, authors.id AS `authors.id`
7373
FROM `authors`
7474
SQL;
7575

@@ -241,7 +241,7 @@ public function test_select_query_with_conditions(): void
241241
->build();
242242

243243
$expected = <<<SQL
244-
SELECT `title`, `index`
244+
SELECT title, index
245245
FROM `chapters`
246246
WHERE `title` = ?
247247
AND `index` <> ?

tests/Integration/Database/Builder/UpdateQueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function test_insert_new_relation_on_update(): void
175175
INSERT INTO `authors` (`name`)
176176
VALUES (?)
177177
SQL,
178-
$authorQuery->getSql(),
178+
$authorQuery->toSql(),
179179
);
180180

181181
$this->assertSame(['Brent'], $authorQuery->bindings);

0 commit comments

Comments
 (0)