Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions packages/database/src/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,18 @@ public function getJoinStatement(): JoinStatement
if ($this->isSelfReferencing()) {
return new JoinStatement(sprintf(
'LEFT JOIN %s AS %s ON %s = %s',
$relationModel->getTableName(),
$this->property->getName(),
$relationJoin,
$ownerJoin,
$this->quoteIdentifier($relationModel->getTableName()),
$this->quoteIdentifier($this->property->getName()),
$this->quoteIdentifier($relationJoin),
$this->quoteIdentifier($ownerJoin),
));
}

$tableName = $relationModel->getTableName();
$tableRef = $tableAlias !== $tableName
? sprintf('%s AS %s', $tableName, $tableAlias)
: $tableName;

// LEFT JOIN authors ON authors.id = books.author_id
return new JoinStatement(sprintf(
'LEFT JOIN %s ON %s = %s',
$tableRef,
$relationJoin,
$ownerJoin,
$this->quoteTableReference($relationModel->getTableName(), $tableAlias),
$this->quoteIdentifier($relationJoin),
$this->quoteIdentifier($ownerJoin),
));
}

Expand Down Expand Up @@ -153,13 +147,16 @@ public function getExistsStatement(): WhereExistsStatement
$relatedTable = $relatedModel->getTableName();
$parentTable = $parentModel->getTableName();
$relatedPK = $relatedModel->getPrimaryKey();

$fk = $this->getOwnerFieldName();

return new WhereExistsStatement(
relatedTable: $relatedTable,
relatedTable: $this->quoteIdentifier($relatedTable),
relatedModelName: $relatedModel->getName(),
condition: "{$relatedTable}.{$relatedPK} = {$parentTable}.{$fk}",
condition: sprintf(
'%s = %s',
$this->qualifyIdentifier((string) $relatedPK, $relatedTable),
$this->qualifyIdentifier($fk, $parentTable),
),
);
}

Expand Down Expand Up @@ -210,13 +207,11 @@ public function query(PrimaryKey $primaryKey, string|UnitEnum|null $onDatabase =
->onDatabase(databaseTag: $onDatabase)
->scope(scope: new WhereRawScope(
statement: sprintf(
'%s.%s = (SELECT %s FROM %s WHERE %s.%s = ?)',
$relatedTable,
$relatedPK,
$fk,
$ownerTable,
$ownerTable,
$ownerPK,
'%s = (SELECT %s FROM %s WHERE %s = ?)',
$this->qualifyIdentifier((string) $relatedPK, $relatedTable),
$this->quoteIdentifier($fk),
$this->quoteIdentifier($ownerTable),
$this->qualifyIdentifier((string) $ownerPK, $ownerTable),
),
binding: $primaryKey,
));
Expand Down
49 changes: 26 additions & 23 deletions packages/database/src/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ private function buildFirstJoin(
): string {
return sprintf(
'LEFT JOIN %s ON %s = %s',
$pivotTable,
$this->resolveOwnerJoin(
$this->quoteIdentifier($pivotTable),
$this->quoteIdentifier($this->resolveOwnerJoin(
ownerModel: $ownerModel,
pivotTable: $pivotTable,
),
$this->resolveRelationJoin(ownerModel: $ownerModel),
)),
$this->quoteIdentifier($this->resolveRelationJoin(ownerModel: $ownerModel)),
);
}

Expand All @@ -182,22 +182,17 @@ private function buildSecondJoin(
string $pivotTable,
string $tableAlias,
): string {
$tableName = $targetModel->getTableName();
$tableRef = $tableAlias !== $tableName
? sprintf('%s AS %s', $tableName, $tableAlias)
: $tableName;

return sprintf(
'LEFT JOIN %s ON %s = %s',
$tableRef,
$this->resolveRelatedRelationJoin(
$this->quoteTableReference($targetModel->getTableName(), $tableAlias),
$this->quoteIdentifier($this->resolveRelatedRelationJoin(
targetModel: $targetModel,
tableAlias: $tableAlias,
),
$this->resolveRelatedOwnerJoin(
)),
$this->quoteIdentifier($this->resolveRelatedOwnerJoin(
targetModel: $targetModel,
pivotTable: $pivotTable,
),
)),
);
}

Expand Down Expand Up @@ -389,11 +384,20 @@ public function getExistsStatement(): WhereExistsStatement
$targetFK = $this->relatedOwnerJoin ?? str(string: $targetTable)->singularizeLastWord()->append(suffix: "_{$targetPK}");

return new WhereExistsStatement(
relatedTable: $pivotTable,
relatedTable: $this->quoteIdentifier($pivotTable),
relatedModelName: $targetModel->getName(),
condition: "{$pivotTable}.{$fk} = {$ownerTable}.{$ownerPK}",
condition: sprintf(
'%s = %s',
$this->qualifyIdentifier((string) $fk, $pivotTable),
$this->qualifyIdentifier((string) $ownerPK, $ownerTable),
),
joinStatement: new JoinStatement(
statement: "INNER JOIN {$targetTable} ON {$targetTable}.{$targetPK} = {$pivotTable}.{$targetFK}",
statement: sprintf(
'INNER JOIN %s ON %s = %s',
$this->quoteIdentifier($targetTable),
$this->qualifyIdentifier((string) $targetPK, $targetTable),
$this->qualifyIdentifier((string) $targetFK, $pivotTable),
),
),
);
}
Expand All @@ -416,12 +420,11 @@ public function query(PrimaryKey $primaryKey, string|UnitEnum|null $onDatabase =
->onDatabase(databaseTag: $onDatabase)
->scope(scope: new WhereRawScope(
statement: sprintf(
'%s.%s IN (SELECT %s FROM %s WHERE %s = ?)',
$targetTable,
$targetPK,
$targetFK,
$pivotTable,
$ownerFK,
'%s IN (SELECT %s FROM %s WHERE %s = ?)',
$this->qualifyIdentifier((string) $targetPK, $targetTable),
$this->quoteIdentifier((string) $targetFK),
$this->quoteIdentifier($pivotTable),
$this->quoteIdentifier((string) $ownerFK),
),
binding: $primaryKey,
));
Expand Down
30 changes: 14 additions & 16 deletions packages/database/src/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,18 @@ public function getJoinStatement(): JoinStatement
if ($this->isSelfReferencing()) {
return new JoinStatement(sprintf(
'LEFT JOIN %s AS %s ON %s = %s',
$ownerModel->getTableName(),
$this->property->getName(),
$ownerJoin,
$relationJoin,
$this->quoteIdentifier($ownerModel->getTableName()),
$this->quoteIdentifier($this->property->getName()),
$this->quoteIdentifier($ownerJoin),
$this->quoteIdentifier($relationJoin),
));
}

$tableName = $ownerModel->getTableName();
$tableRef = $tableAlias !== $tableName
? sprintf('%s AS %s', $tableName, $tableAlias)
: $tableName;

return new JoinStatement(sprintf(
'LEFT JOIN %s ON %s = %s',
$tableRef,
$ownerJoin,
$relationJoin,
$this->quoteTableReference($ownerModel->getTableName(), $tableAlias),
$this->quoteIdentifier($ownerJoin),
$this->quoteIdentifier($relationJoin),
));
}

Expand Down Expand Up @@ -171,13 +166,16 @@ public function getExistsStatement(): WhereExistsStatement
$relatedTable = $relatedModel->getTableName();
$parentTable = $parentModel->getTableName();
$parentPK = $parentModel->getPrimaryKey();

$fk = $this->ownerJoin ?? str(string: $parentTable)->singularizeLastWord()->append(suffix: "_{$parentPK}");
$fk = (string) ($this->ownerJoin ?? str(string: $parentTable)->singularizeLastWord()->append(suffix: "_{$parentPK}"));

return new WhereExistsStatement(
relatedTable: $relatedTable,
relatedTable: $this->quoteIdentifier($relatedTable),
relatedModelName: $relatedModel->getName(),
condition: "{$relatedTable}.{$fk} = {$parentTable}.{$parentPK}",
condition: sprintf(
'%s = %s',
$this->qualifyIdentifier($fk, $relatedTable),
$this->qualifyIdentifier((string) $parentPK, $parentTable),
),
);
}

Expand Down
43 changes: 24 additions & 19 deletions packages/database/src/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ private function buildFirstJoin(
): string {
return sprintf(
'LEFT JOIN %s ON %s = %s',
$intermediateModel->getTableName(),
$this->resolveOwnerJoin(
$this->quoteIdentifier($intermediateModel->getTableName()),
$this->quoteIdentifier($this->resolveOwnerJoin(
intermediateModel: $intermediateModel,
ownerModel: $ownerModel,
),
$this->resolveRelationJoin(ownerModel: $ownerModel),
)),
$this->quoteIdentifier($this->resolveRelationJoin(ownerModel: $ownerModel)),
);
}

Expand All @@ -159,20 +159,16 @@ private function buildSecondJoin(
ModelInspector $targetModel,
): string {
$tableAlias = $this->getTableAlias(tableName: $targetModel->getTableName());
$tableName = $targetModel->getTableName();
$tableRef = $tableAlias !== $tableName
? sprintf('%s AS %s', $tableName, $tableAlias)
: $tableName;

return sprintf(
'LEFT JOIN %s ON %s = %s',
$tableRef,
$this->resolveThroughOwnerJoin(
$this->quoteTableReference($targetModel->getTableName(), $tableAlias),
$this->quoteIdentifier($this->resolveThroughOwnerJoin(
targetModel: $targetModel,
intermediateModel: $intermediateModel,
tableAlias: $tableAlias,
),
$this->resolveThroughRelationJoin(intermediateModel: $intermediateModel),
)),
$this->quoteIdentifier($this->resolveThroughRelationJoin(intermediateModel: $intermediateModel)),
);
}

Expand Down Expand Up @@ -355,11 +351,20 @@ public function getExistsStatement(): WhereExistsStatement
$targetFK = $this->throughOwnerJoin ?? str(string: $intermediateTable)->singularizeLastWord()->append(suffix: "_{$intermediatePK}");

return new WhereExistsStatement(
relatedTable: $intermediateTable,
relatedTable: $this->quoteIdentifier($intermediateTable),
relatedModelName: $targetModel->getName(),
condition: "{$intermediateTable}.{$fk} = {$ownerTable}.{$ownerPK}",
condition: sprintf(
'%s = %s',
$this->qualifyIdentifier((string) $fk, $intermediateTable),
$this->qualifyIdentifier((string) $ownerPK, $ownerTable),
),
joinStatement: new JoinStatement(
statement: "INNER JOIN {$targetTable} ON {$targetTable}.{$targetFK} = {$intermediateTable}.{$intermediatePK}",
statement: sprintf(
'INNER JOIN %s ON %s = %s',
$this->quoteIdentifier($targetTable),
$this->qualifyIdentifier((string) $targetFK, $targetTable),
$this->qualifyIdentifier((string) $intermediatePK, $intermediateTable),
),
),
);
}
Expand All @@ -383,10 +388,10 @@ public function query(PrimaryKey $primaryKey, string|UnitEnum|null $onDatabase =
->scope(scope: new WhereRawScope(
statement: sprintf(
'%s IN (SELECT %s FROM %s WHERE %s = ?)',
$relatedTable . '.' . $targetFK,
$intermediatePK,
$intermediateTable,
$ownerFK,
$this->qualifyIdentifier((string) $targetFK, $relatedTable),
$this->quoteIdentifier((string) $intermediatePK),
$this->quoteIdentifier($intermediateTable),
$this->quoteIdentifier((string) $ownerFK),
),
binding: $primaryKey,
));
Expand Down
30 changes: 14 additions & 16 deletions packages/database/src/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,18 @@ public function getJoinStatement(): JoinStatement
if ($this->isSelfReferencing()) {
return new JoinStatement(sprintf(
'LEFT JOIN %s AS %s ON %s = %s',
$ownerModel->getTableName(),
$this->property->getName(),
$ownerJoin,
$relationJoin,
$this->quoteIdentifier($ownerModel->getTableName()),
$this->quoteIdentifier($this->property->getName()),
$this->quoteIdentifier($ownerJoin),
$this->quoteIdentifier($relationJoin),
));
}

$tableName = $ownerModel->getTableName();
$tableRef = $tableAlias !== $tableName
? sprintf('%s AS %s', $tableName, $tableAlias)
: $tableName;

return new JoinStatement(sprintf(
'LEFT JOIN %s ON %s = %s',
$tableRef,
$ownerJoin,
$relationJoin,
$this->quoteTableReference($ownerModel->getTableName(), $tableAlias),
$this->quoteIdentifier($ownerJoin),
$this->quoteIdentifier($relationJoin),
));
}

Expand Down Expand Up @@ -141,13 +136,16 @@ public function getExistsStatement(): WhereExistsStatement
$relatedTable = $relatedModel->getTableName();
$parentTable = $parentModel->getTableName();
$parentPK = $parentModel->getPrimaryKey();

$fk = $this->ownerJoin ?? str(string: $parentTable)->singularizeLastWord()->append(suffix: "_{$parentPK}");
$fk = (string) ($this->ownerJoin ?? str(string: $parentTable)->singularizeLastWord()->append(suffix: "_{$parentPK}"));

return new WhereExistsStatement(
relatedTable: $relatedTable,
relatedTable: $this->quoteIdentifier($relatedTable),
relatedModelName: $relatedModel->getName(),
condition: "{$relatedTable}.{$fk} = {$parentTable}.{$parentPK}",
condition: sprintf(
'%s = %s',
$this->qualifyIdentifier($fk, $relatedTable),
$this->qualifyIdentifier((string) $parentPK, $parentTable),
),
);
}

Expand Down
Loading
Loading