1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Schema; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Exception\DBALException; |
6
|
|
|
use Bdf\Prime\Schema\Adapter\Doctrine\DoctrineTable as PrimeTableAdapter; |
7
|
|
|
use Bdf\Prime\Schema\Transformer\Doctrine\TableTransformer; |
8
|
|
|
use Doctrine\DBAL\DBALException as DoctrineDBALException; |
9
|
|
|
use Doctrine\DBAL\Schema\Schema as DoctrineSchema; |
10
|
|
|
use Doctrine\DBAL\Schema\SchemaConfig; |
11
|
|
|
use Doctrine\DBAL\Schema\SchemaDiff as DoctrineSchemaDiff; |
12
|
|
|
use Doctrine\DBAL\Schema\TableDiff as DoctrineTableDiff; |
13
|
|
|
use Doctrine\DBAL\Schema\Table as DoctrineTable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* SchemaManager using doctrine schemas |
17
|
|
|
*/ |
18
|
|
|
class SchemaManager extends AbstractSchemaManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Queries to execute |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $queries = []; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the doctrine schema manager |
30
|
|
|
* |
31
|
|
|
* @return \Doctrine\DBAL\Schema\AbstractSchemaManager |
32
|
|
|
*/ |
33
|
799 |
|
public function getDoctrineManager() |
|
|
|
|
34
|
|
|
{ |
35
|
799 |
|
return $this->connection->getSchemaManager(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the queries to execute |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
5 |
|
public function toSql() |
44
|
|
|
{ |
45
|
5 |
|
return $this->queries; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
11 |
|
public function pending() |
52
|
|
|
{ |
53
|
11 |
|
return $this->queries; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
801 |
|
public function clear() |
60
|
|
|
{ |
61
|
801 |
|
$this->queries = []; |
62
|
|
|
|
63
|
801 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
801 |
|
public function flush() |
70
|
|
|
{ |
71
|
801 |
|
$lastResult = false; |
72
|
801 |
|
$queries = $this->queries; |
73
|
|
|
|
74
|
801 |
|
$this->clear(); |
75
|
|
|
|
76
|
801 |
|
foreach ($queries as $query) { |
77
|
801 |
|
$lastResult = $this->connection->exec($query); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
798 |
|
return $lastResult; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
785 |
|
public function schema($tables = []) |
87
|
|
|
{ |
88
|
785 |
|
if (!is_array($tables)) { |
89
|
785 |
|
$tables = [$tables]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$tables = array_map(function ($table) { |
93
|
785 |
|
if ($table instanceof TableInterface) { |
94
|
785 |
|
return (new TableTransformer($table, $this->platform))->toDoctrine(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $table; |
98
|
785 |
|
}, $tables); |
99
|
|
|
|
100
|
785 |
|
$config = new SchemaConfig(); |
101
|
785 |
|
$config->setName($this->connection->getDatabase()); |
102
|
|
|
|
103
|
785 |
|
return new DoctrineSchema($tables, [], $config); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function loadSchema() |
110
|
|
|
{ |
111
|
1 |
|
return $this->getDoctrineManager()->createSchema(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
|
|
|
|
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
|
|
|
|
117
|
|
|
public function hasDatabase($database) |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
$databases = $this->getDoctrineManager()->listDatabases($database); |
|
|
|
|
121
|
|
|
} catch (DoctrineDBALException $e) { |
122
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return in_array(strtolower($database), array_map('strtolower', $databases)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
|
|
|
|
131
|
|
|
public function getDatabases() |
132
|
|
|
{ |
133
|
|
|
try { |
134
|
|
|
return $this->getDoctrineManager()->listDatabases(); |
135
|
|
|
} catch (DoctrineDBALException $e) { |
136
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
|
|
|
|
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
|
|
|
|
143
|
|
|
public function createDatabase($database) |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
return $this->push( |
147
|
|
|
$this->platform->grammar()->getCreateDatabaseSQL($database) |
148
|
|
|
); |
149
|
|
|
} catch (DoctrineDBALException $e) { |
150
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
|
|
|
|
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
|
|
|
|
157
|
|
|
public function dropDatabase($database) |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
|
|
return $this->push( |
161
|
|
|
$this->platform->grammar()->getDropDatabaseSQL($database) |
162
|
|
|
); |
163
|
|
|
} catch (DoctrineDBALException $e) { |
164
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
|
|
|
|
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
|
|
|
|
171
|
799 |
|
public function hasTable($tableName) |
172
|
|
|
{ |
173
|
|
|
try { |
174
|
799 |
|
return $this->getDoctrineManager()->tablesExist($tableName); |
175
|
|
|
} catch (DoctrineDBALException $e) { |
176
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
|
|
|
|
183
|
88 |
|
public function loadTable($tableName) |
184
|
|
|
{ |
185
|
|
|
try { |
186
|
88 |
|
$manager = $this->getDoctrineManager(); |
187
|
|
|
|
188
|
88 |
|
$foreignKeys = []; |
189
|
|
|
|
190
|
88 |
|
if ($this->platform->grammar()->supportsForeignKeyConstraints()) { |
191
|
|
|
$foreignKeys = $manager->listTableForeignKeys($tableName); |
192
|
|
|
} |
193
|
|
|
|
194
|
88 |
|
return new PrimeTableAdapter( |
195
|
88 |
|
new DoctrineTable( |
196
|
88 |
|
$tableName, |
197
|
88 |
|
$manager->listTableColumns($tableName), |
198
|
88 |
|
$manager->listTableIndexes($tableName), |
199
|
88 |
|
$foreignKeys |
200
|
|
|
), |
201
|
88 |
|
$this->connection->platform()->types() |
202
|
|
|
); |
203
|
|
|
} catch (DoctrineDBALException $e) { |
204
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
205
|
|
|
} |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
|
|
|
|
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
|
|
|
|
211
|
654 |
|
public function drop($tableName) |
212
|
|
|
{ |
213
|
|
|
try { |
214
|
654 |
|
return $this->push( |
215
|
654 |
|
$this->platform->grammar()->getDropTableSQL($tableName) |
216
|
|
|
); |
217
|
25 |
|
} catch (DoctrineDBALException $e) { |
218
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
|
|
|
|
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
|
|
|
|
225
|
53 |
|
public function truncate($tableName, $cascade = false) |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
53 |
|
return $this->push( |
229
|
53 |
|
$this->platform->grammar()->getTruncateTableSQL($tableName, $cascade) |
230
|
|
|
); |
231
|
|
|
} catch (DoctrineDBALException $e) { |
232
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
|
|
|
|
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
85 |
|
public function diff(TableInterface $newTable, TableInterface $oldTable) |
240
|
|
|
{ |
241
|
85 |
|
$comparator = new Comparator(); |
|
|
|
|
242
|
85 |
|
$comparator->setListDropColumn($this->useDrop); |
243
|
|
|
|
244
|
85 |
|
return $comparator->compare( |
245
|
85 |
|
$this->schema($oldTable), |
246
|
85 |
|
$this->schema($newTable) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
|
|
|
|
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
|
|
|
|
253
|
2 |
|
public function rename($from, $to) |
254
|
|
|
{ |
255
|
2 |
|
$diff = new DoctrineTableDiff($from); |
256
|
2 |
|
$diff->newName = $to; |
257
|
|
|
|
258
|
|
|
try { |
259
|
2 |
|
return $this->push( |
260
|
2 |
|
$this->platform->grammar()->getAlterTableSQL($diff) |
261
|
|
|
); |
262
|
|
|
} catch (DoctrineDBALException $e) { |
263
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
|
|
|
|
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
801 |
|
public function push($queries) |
271
|
|
|
{ |
272
|
801 |
|
if ($queries instanceof DoctrineSchema || $queries instanceof DoctrineSchemaDiff) { |
273
|
785 |
|
$queries = $queries->toSql($this->platform->grammar()); |
274
|
|
|
} |
275
|
|
|
|
276
|
801 |
|
foreach ((array)$queries as $query) { |
|
|
|
|
277
|
801 |
|
$this->queries[] = $query; |
278
|
|
|
} |
279
|
|
|
|
280
|
801 |
|
if ($this->autoFlush) { |
281
|
801 |
|
$this->flush(); |
282
|
|
|
} |
283
|
|
|
|
284
|
799 |
|
return $this; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|