-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathBelongsTo.php
More file actions
185 lines (146 loc) · 5.54 KB
/
BelongsTo.php
File metadata and controls
185 lines (146 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace Tempest\Database;
use Attribute;
use Tempest\Database\Builder\ModelInspector;
use Tempest\Database\Exceptions\ModelDidNotHavePrimaryColumn;
use Tempest\Database\QueryStatements\FieldStatement;
use Tempest\Database\QueryStatements\JoinStatement;
use Tempest\Database\QueryStatements\WhereExistsStatement;
use Tempest\Reflection\PropertyReflector;
use Tempest\Support\Arr\ImmutableArray;
use function Tempest\Support\str;
#[Attribute(Attribute::TARGET_PROPERTY)]
final class BelongsTo implements Relation
{
use HasTableAlias;
public PropertyReflector $property;
public string $name {
get => $this->property->getName();
}
private ?string $parent = null;
public function __construct(
private readonly ?string $relationJoin = null,
private readonly ?string $ownerJoin = null,
) {}
public function setParent(string $name): self
{
$this->parent = $name;
return $this;
}
public function getOwnerFieldName(): string
{
if ($this->ownerJoin) {
if (str_contains($this->ownerJoin, '.')) {
return explode('.', $this->ownerJoin)[1];
}
return $this->ownerJoin;
}
$relationModel = inspect($this->property->getType()->asClass());
$primaryKey = $relationModel->getPrimaryKey();
if ($primaryKey === null) {
throw ModelDidNotHavePrimaryColumn::neededForRelation($relationModel->getName(), 'BelongsTo');
}
return str($relationModel->getTableName())->singularizeLastWord() . '_' . $primaryKey;
}
public function getSelectFields(): ImmutableArray
{
$relationModel = inspect($this->property->getType()->asClass());
$tableReference = $this->isSelfReferencing()
? $this->property->getName()
: $this->getTableAlias($relationModel->getTableName());
return $relationModel
->getSelectFields()
->map(fn ($field) => new FieldStatement(
$tableReference . '.' . $field,
)
->withAlias(
sprintf('%s.%s', $this->property->getName(), $field),
)
->withAliasPrefix($this->parent));
}
public function getJoinStatement(): JoinStatement
{
$relationModel = inspect($this->property->getType()->asClass());
$ownerModel = inspect($this->property->getClass());
$tableAlias = $this->getTableAlias($relationModel->getTableName());
$relationJoin = $this->getRelationJoin(
relationModel: $relationModel,
tableAlias: $tableAlias,
);
$ownerJoin = $this->getOwnerJoin($ownerModel);
if ($this->isSelfReferencing()) {
return new JoinStatement(sprintf(
'LEFT JOIN %s AS %s ON %s = %s',
$relationModel->getTableName(),
$this->property->getName(),
$relationJoin,
$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,
));
}
private function getRelationJoin(ModelInspector $relationModel, string $tableAlias): string
{
$relationJoin = $this->relationJoin;
$tableReference = $this->isSelfReferencing()
? $this->property->getName()
: $tableAlias;
if ($relationJoin && ! strpos($relationJoin, '.')) {
$relationJoin = sprintf('%s.%s', $tableReference, $relationJoin);
}
if ($relationJoin) {
return $relationJoin;
}
$primaryKey = $relationModel->getPrimaryKey();
if ($primaryKey === null) {
throw ModelDidNotHavePrimaryColumn::neededForRelation($relationModel->getName(), 'BelongsTo');
}
return sprintf('%s.%s', $tableReference, $primaryKey);
}
public function getExistsStatement(): WhereExistsStatement
{
$relatedModel = inspect(model: $this->property->getType()->asClass());
$parentModel = inspect(model: $this->property->getClass());
$relatedTable = $relatedModel->getTableName();
$parentTable = $parentModel->getTableName();
$relatedPK = $relatedModel->getPrimaryKey();
$fk = $this->getOwnerFieldName();
return new WhereExistsStatement(
relatedTable: $relatedTable,
relatedModelName: $relatedModel->getName(),
condition: "{$relatedTable}.{$relatedPK} = {$parentTable}.{$fk}",
);
}
private function isSelfReferencing(): bool
{
$relationModel = inspect($this->property->getType()->asClass());
$ownerModel = inspect($this->property->getClass());
return $relationModel->getTableName() === $ownerModel->getTableName();
}
private function getOwnerJoin(ModelInspector $ownerModel): string
{
$ownerJoin = $this->ownerJoin;
if ($ownerJoin && ! strpos($ownerJoin, '.')) {
$ownerJoin = sprintf('%s.%s', $ownerModel->getTableName(), $ownerJoin);
}
if ($ownerJoin) {
return $ownerJoin;
}
return sprintf(
'%s.%s',
$ownerModel->getTableName(),
$this->getOwnerFieldName(),
);
}
}