-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowCreateTable.php
More file actions
149 lines (139 loc) · 4.37 KB
/
ShowCreateTable.php
File metadata and controls
149 lines (139 loc) · 4.37 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
<?php
declare(strict_types = 1);
namespace Formal\ORM\Adapter\SQL;
use Formal\ORM\Definition\Aggregates;
use Formal\AccessLayer\{
Query,
Query\Constraint\ForeignKey,
Table\Name,
Table\Column,
};
use Innmind\Immutable\Sequence;
final class ShowCreateTable
{
/**
* @psalm-mutation-free
*/
private function __construct(
private Aggregates $aggregates,
private MapType $mapType,
private bool $ifNotExists,
) {
}
/**
* @param class-string $class
*
* @return Sequence<Query>
*/
public function __invoke(string $class): Sequence
{
$definition = $this->aggregates->get($class);
$mainTable = MainTable::of($definition);
/** @psalm-suppress NamedArgumentNotAllowed */
$create = match ($this->ifNotExists) {
true => static fn(Name $name, Column $first, Column ...$rest) => Query\CreateTable::ifNotExists(
$name, $first, ...$rest,
),
false => static fn(Name $name, Column $first, Column ...$rest) => Query\CreateTable::named(
$name, $first, ...$rest,
),
};
$entities = $mainTable
->entities()
->map(fn($entity) => $create(
$entity->name()->name(),
$entity->primaryKey(),
...$entity
->columnsDefinition($this->mapType)
->toList(),
)
->constraint(
ForeignKey::of(
$entity->primaryKey()->name(),
$mainTable->name()->name(),
$mainTable->primaryKey()->name(),
)
->onDeleteCascade()
->named($entity->name()->name()->toString()),
)
->unique($entity->primaryKey()->name()),
)
->toList();
$optionals = $mainTable
->optionals()
->map(fn($optional) => $create(
$optional->name()->name(),
$optional->primaryKey(),
...$optional
->columnsDefinition($this->mapType)
->toList(),
)
->constraint(
ForeignKey::of(
$optional->primaryKey()->name(),
$mainTable->name()->name(),
$mainTable->primaryKey()->name(),
)
->onDeleteCascade()
->named($optional->name()->name()->toString()),
)
->unique($optional->primaryKey()->name()),
)
->toList();
$collections = $mainTable
->collections()
->map(
fn($collection) => $create(
$collection->name()->name(),
$collection->foreignKey(),
...$collection
->columnsDefinition($this->mapType)
->toList(),
)
->constraint(
ForeignKey::of(
$collection->foreignKey()->name(),
$mainTable->name()->name(),
$mainTable->primaryKey()->name(),
)
->onDeleteCascade()
->named($collection->name()->name()->toString()),
),
)
->toList();
$main = $create(
$mainTable->name()->name(),
$mainTable->primaryKey(),
...$mainTable
->columnsDefinition($this->mapType)
->toList(),
)->primaryKey($mainTable->primaryKey()->name());
return Sequence::of(...[
$main,
...$entities,
...$optionals,
...$collections,
]);
}
public static function of(Aggregates $aggregates): self
{
return new self(
$aggregates,
MapType::new(),
false,
);
}
/**
* This will add the "IF NOT EXIST" to the sql queries
*
* @psalm-mutation-free
*/
public function ifNotExists(): self
{
return new self(
$this->aggregates,
$this->mapType,
true,
);
}
}