1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Schema; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Exception\DBALException; |
7
|
|
|
use Bdf\Prime\Exception\PrimeException; |
8
|
|
|
use Bdf\Prime\Schema\Adapter\ConstraintTable; |
9
|
|
|
use Bdf\Prime\Schema\Adapter\MapperInfo\MapperInfoConstraintSet; |
10
|
|
|
use Bdf\Prime\Schema\Adapter\MapperInfo\Resolver\MapperInfoForeignKeyResolver; |
11
|
|
|
use Bdf\Prime\Schema\Adapter\Metadata\MetadataTable; |
12
|
|
|
use Bdf\Prime\Schema\Builder\TableBuilder; |
13
|
|
|
use Bdf\Prime\ServiceLocator; |
14
|
|
|
use Bdf\Prime\Mapper\Metadata; |
15
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Schema resolver |
19
|
|
|
* |
20
|
|
|
* manage update on schema |
|
|
|
|
21
|
|
|
* |
22
|
|
|
* @todo gestion du renommage de champs dans le cas où d'autres attributs ont été changés |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
class Resolver implements ResolverInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ServiceLocator |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
protected $service; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Metadata |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
protected $metadata; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SchemaManagerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $schema; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ServiceLocator $service Prime service |
44
|
|
|
* @param Metadata $metadata The entity metadata |
45
|
|
|
* @param SchemaManagerInterface|null $schema If given, force using this schema manager instead of resolving using the configured connection name |
46
|
|
|
*/ |
47
|
592 |
|
public function __construct(ServiceLocator $service, Metadata $metadata, SchemaManagerInterface $schema = null) |
|
|
|
|
48
|
|
|
{ |
49
|
592 |
|
$this->service = $service; |
50
|
592 |
|
$this->metadata = $metadata; |
51
|
592 |
|
$this->schema = $schema; |
52
|
592 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
584 |
|
public function migrate($listDrop = true) |
58
|
|
|
{ |
59
|
584 |
|
$schema = $this->schema()->useDrop($listDrop); |
60
|
584 |
|
$schema->add($this->table()); |
61
|
|
|
|
62
|
584 |
|
if (($schemaSequence = $this->schemaSequence()) !== null) { |
|
|
|
|
63
|
306 |
|
$schemaSequence->useDrop($listDrop); |
64
|
306 |
|
$schemaSequence->add($this->sequence()); |
65
|
|
|
|
66
|
306 |
|
$this->insertSequenceId(); |
67
|
|
|
} |
68
|
584 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function diff($listDrop = true) |
74
|
|
|
{ |
75
|
2 |
|
$queries = $this->schema() |
76
|
|
|
->simulate(function (SchemaManagerInterface $schema) use ($listDrop) { |
|
|
|
|
77
|
2 |
|
$schema->useDrop($listDrop); |
78
|
2 |
|
$schema->add($this->table()); |
79
|
2 |
|
}) |
80
|
2 |
|
->pending(); |
|
|
|
|
81
|
|
|
|
82
|
2 |
|
if (($schemaSequence = $this->schemaSequence()) === null) { |
|
|
|
|
83
|
1 |
|
return $queries; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$sequenceQueries = $schemaSequence |
87
|
|
|
->simulate(function (SchemaManagerInterface $schema) use ($listDrop) { |
|
|
|
|
88
|
1 |
|
$schema->useDrop($listDrop); |
89
|
1 |
|
$schema->add($this->sequence()); |
90
|
1 |
|
}) |
91
|
1 |
|
->pending(); |
|
|
|
|
92
|
|
|
|
93
|
1 |
|
return array_merge($queries, $sequenceQueries); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create table schema from meta |
98
|
|
|
* |
99
|
|
|
* @param bool $foreignKeys Add foreign key constraints to the schema ? |
|
|
|
|
100
|
|
|
* |
101
|
|
|
* @return TableInterface |
102
|
|
|
* @throws PrimeException |
|
|
|
|
103
|
|
|
*/ |
104
|
587 |
|
public function table($foreignKeys = false) |
105
|
|
|
{ |
106
|
587 |
|
$table = new MetadataTable( |
107
|
587 |
|
$this->metadata, |
108
|
587 |
|
$this->connection()->platform()->types() |
109
|
|
|
); |
110
|
|
|
|
111
|
587 |
|
if ($foreignKeys) { |
112
|
1 |
|
$table = new ConstraintTable( |
113
|
1 |
|
$table, |
114
|
1 |
|
new MapperInfoConstraintSet( |
115
|
1 |
|
$this->service->repository($this->metadata->entityName)->mapper()->info(), |
116
|
|
|
[ |
117
|
1 |
|
new MapperInfoForeignKeyResolver($this->service) |
|
|
|
|
118
|
|
|
] |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
587 |
|
return $table; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create sequence schema from meta |
128
|
|
|
* |
129
|
|
|
* @return null|TableInterface |
130
|
|
|
* @throws PrimeException |
|
|
|
|
131
|
|
|
*/ |
132
|
309 |
|
public function sequence() |
133
|
|
|
{ |
134
|
309 |
|
if (!$this->metadata->isSequencePrimaryKey()) { |
135
|
1 |
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
308 |
|
$table = new TableBuilder($this->metadata->sequence['table']); |
139
|
308 |
|
$table->options($this->metadata->sequence['options']); |
140
|
308 |
|
$table->add( |
141
|
308 |
|
$this->metadata->sequence['column'], |
142
|
308 |
|
$this->connection($this->metadata->sequence['connection'])->platform()->types()->native('bigint') |
143
|
|
|
); |
144
|
308 |
|
$table->primary($this->metadata->sequence['column']); |
145
|
|
|
|
146
|
308 |
|
return $table->build(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Insert sequence id into sequence table |
151
|
|
|
* |
152
|
|
|
* @throws PrimeException |
|
|
|
|
153
|
|
|
*/ |
154
|
306 |
|
public function insertSequenceId() |
155
|
|
|
{ |
156
|
306 |
|
if (!$this->metadata->isSequencePrimaryKey()) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
306 |
|
$connection = $this->connection($this->metadata->sequence['connection']); |
161
|
306 |
|
$table = $this->metadata->sequence['table']; |
|
|
|
|
162
|
|
|
|
163
|
306 |
|
$nb = $connection->from($table)->count(); |
|
|
|
|
164
|
|
|
|
165
|
306 |
|
if ($nb == 0) { |
166
|
306 |
|
$connection->insert($table, [$this->metadata->sequence['column'] => 0]); |
|
|
|
|
167
|
|
|
} |
168
|
306 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
|
|
|
|
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
1 |
|
public function truncate($cascade =false) |
|
|
|
|
174
|
|
|
{ |
175
|
1 |
|
return $this->schema()->truncate($this->metadata->table, $cascade); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
|
|
|
|
181
|
560 |
|
public function drop() |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
560 |
|
$this->schema()->drop($this->metadata->table); |
185
|
|
|
|
186
|
556 |
|
if (($schemaSequence = $this->schemaSequence()) !== null) { |
|
|
|
|
187
|
298 |
|
$schemaSequence->drop($this->metadata->sequence['table']); |
188
|
|
|
} |
189
|
|
|
|
190
|
556 |
|
return true; |
191
|
25 |
|
} catch (DBALException $e) { |
192
|
25 |
|
if ($e->getPrevious() instanceof TableNotFoundException) { |
193
|
25 |
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
throw $e; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the schema builder |
202
|
|
|
* |
203
|
|
|
* @return SchemaManagerInterface |
204
|
|
|
* @throws PrimeException |
|
|
|
|
205
|
|
|
*/ |
206
|
588 |
|
protected function schema() |
207
|
|
|
{ |
208
|
588 |
|
if ($this->schema !== null) { |
209
|
|
|
return $this->schema; |
210
|
|
|
} |
211
|
|
|
|
212
|
588 |
|
return $this->connection()->schema(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the schema builder for sequence |
217
|
|
|
* |
218
|
|
|
* @return SchemaManagerInterface |
219
|
|
|
* @throws PrimeException |
|
|
|
|
220
|
|
|
*/ |
221
|
586 |
|
protected function schemaSequence() |
222
|
|
|
{ |
223
|
586 |
|
if (!$this->metadata->isSequencePrimaryKey()) { |
224
|
578 |
|
return null; |
225
|
|
|
} |
226
|
|
|
|
227
|
307 |
|
if ($this->schema !== null) { |
228
|
|
|
return $this->schema; |
229
|
|
|
} |
230
|
|
|
|
231
|
307 |
|
return $this->connection($this->metadata->sequence['connection'])->schema(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get the connection |
236
|
|
|
* |
237
|
|
|
* @param string $profile |
238
|
|
|
* |
239
|
|
|
* @return ConnectionInterface |
240
|
|
|
*/ |
241
|
591 |
|
protected function connection($profile = null) |
242
|
|
|
{ |
243
|
591 |
|
return $this->service->connection($profile ?: $this->metadata->connection); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|