77use Closure ;
88use Tempest \Database \Builder \FieldDefinition ;
99use Tempest \Database \Builder \ModelDefinition ;
10- use Tempest \Database \Builder \TableDefinition ;
10+ use Tempest \Database \Builder \ModelInspector ;
1111use Tempest \Database \Id ;
1212use Tempest \Database \Mappers \SelectModelMapper ;
1313use 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 {
0 commit comments