1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Schema; |
---|
21 | |
---|
22 | use Doctrine\DBAL\Events; |
---|
23 | use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs; |
---|
24 | use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs; |
---|
25 | use Doctrine\DBAL\Types; |
---|
26 | use Doctrine\DBAL\DBALException; |
---|
27 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
---|
28 | |
---|
29 | /** |
---|
30 | * Base class for schema managers. Schema managers are used to inspect and/or |
---|
31 | * modify the database schema/structure. |
---|
32 | * |
---|
33 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
34 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
---|
35 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
---|
36 | * @author Roman Borschel <roman@code-factory.org> |
---|
37 | * @author Jonathan H. Wage <jonwage@gmail.com> |
---|
38 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
39 | * @since 2.0 |
---|
40 | */ |
---|
41 | abstract class AbstractSchemaManager |
---|
42 | { |
---|
43 | /** |
---|
44 | * Holds instance of the Doctrine connection for this schema manager |
---|
45 | * |
---|
46 | * @var \Doctrine\DBAL\Connection |
---|
47 | */ |
---|
48 | protected $_conn; |
---|
49 | |
---|
50 | /** |
---|
51 | * Holds instance of the database platform used for this schema manager |
---|
52 | * |
---|
53 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
---|
54 | */ |
---|
55 | protected $_platform; |
---|
56 | |
---|
57 | /** |
---|
58 | * Constructor. Accepts the Connection instance to manage the schema for |
---|
59 | * |
---|
60 | * @param \Doctrine\DBAL\Connection $conn |
---|
61 | */ |
---|
62 | public function __construct(\Doctrine\DBAL\Connection $conn) |
---|
63 | { |
---|
64 | $this->_conn = $conn; |
---|
65 | $this->_platform = $this->_conn->getDatabasePlatform(); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Return associated platform. |
---|
70 | * |
---|
71 | * @return \Doctrine\DBAL\Platform\AbstractPlatform |
---|
72 | */ |
---|
73 | public function getDatabasePlatform() |
---|
74 | { |
---|
75 | return $this->_platform; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Try any method on the schema manager. Normally a method throws an |
---|
80 | * exception when your DBMS doesn't support it or if an error occurs. |
---|
81 | * This method allows you to try and method on your SchemaManager |
---|
82 | * instance and will return false if it does not work or is not supported. |
---|
83 | * |
---|
84 | * <code> |
---|
85 | * $result = $sm->tryMethod('dropView', 'view_name'); |
---|
86 | * </code> |
---|
87 | * |
---|
88 | * @return mixed |
---|
89 | */ |
---|
90 | public function tryMethod() |
---|
91 | { |
---|
92 | $args = func_get_args(); |
---|
93 | $method = $args[0]; |
---|
94 | unset($args[0]); |
---|
95 | $args = array_values($args); |
---|
96 | |
---|
97 | try { |
---|
98 | return call_user_func_array(array($this, $method), $args); |
---|
99 | } catch (\Exception $e) { |
---|
100 | return false; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * List the available databases for this connection |
---|
106 | * |
---|
107 | * @return array $databases |
---|
108 | */ |
---|
109 | public function listDatabases() |
---|
110 | { |
---|
111 | $sql = $this->_platform->getListDatabasesSQL(); |
---|
112 | |
---|
113 | $databases = $this->_conn->fetchAll($sql); |
---|
114 | |
---|
115 | return $this->_getPortableDatabasesList($databases); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * List the available sequences for this connection |
---|
120 | * |
---|
121 | * @return Sequence[] |
---|
122 | */ |
---|
123 | public function listSequences($database = null) |
---|
124 | { |
---|
125 | if (is_null($database)) { |
---|
126 | $database = $this->_conn->getDatabase(); |
---|
127 | } |
---|
128 | $sql = $this->_platform->getListSequencesSQL($database); |
---|
129 | |
---|
130 | $sequences = $this->_conn->fetchAll($sql); |
---|
131 | |
---|
132 | return $this->filterAssetNames($this->_getPortableSequencesList($sequences)); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * List the columns for a given table. |
---|
137 | * |
---|
138 | * In contrast to other libraries and to the old version of Doctrine, |
---|
139 | * this column definition does try to contain the 'primary' field for |
---|
140 | * the reason that it is not portable accross different RDBMS. Use |
---|
141 | * {@see listTableIndexes($tableName)} to retrieve the primary key |
---|
142 | * of a table. We're a RDBMS specifies more details these are held |
---|
143 | * in the platformDetails array. |
---|
144 | * |
---|
145 | * @param string $table The name of the table. |
---|
146 | * @param string $database |
---|
147 | * @return Column[] |
---|
148 | */ |
---|
149 | public function listTableColumns($table, $database = null) |
---|
150 | { |
---|
151 | if (!$database) { |
---|
152 | $database = $this->_conn->getDatabase(); |
---|
153 | } |
---|
154 | |
---|
155 | $sql = $this->_platform->getListTableColumnsSQL($table, $database); |
---|
156 | |
---|
157 | $tableColumns = $this->_conn->fetchAll($sql); |
---|
158 | |
---|
159 | return $this->_getPortableTableColumnList($table, $database, $tableColumns); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * List the indexes for a given table returning an array of Index instances. |
---|
164 | * |
---|
165 | * Keys of the portable indexes list are all lower-cased. |
---|
166 | * |
---|
167 | * @param string $table The name of the table |
---|
168 | * @return Index[] $tableIndexes |
---|
169 | */ |
---|
170 | public function listTableIndexes($table) |
---|
171 | { |
---|
172 | $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase()); |
---|
173 | |
---|
174 | $tableIndexes = $this->_conn->fetchAll($sql); |
---|
175 | |
---|
176 | return $this->_getPortableTableIndexesList($tableIndexes, $table); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Return true if all the given tables exist. |
---|
181 | * |
---|
182 | * @param array $tableNames |
---|
183 | * @return bool |
---|
184 | */ |
---|
185 | public function tablesExist($tableNames) |
---|
186 | { |
---|
187 | $tableNames = array_map('strtolower', (array)$tableNames); |
---|
188 | return count($tableNames) == count(\array_intersect($tableNames, array_map('strtolower', $this->listTableNames()))); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Return a list of all tables in the current database |
---|
193 | * |
---|
194 | * @return array |
---|
195 | */ |
---|
196 | public function listTableNames() |
---|
197 | { |
---|
198 | $sql = $this->_platform->getListTablesSQL(); |
---|
199 | |
---|
200 | $tables = $this->_conn->fetchAll($sql); |
---|
201 | $tableNames = $this->_getPortableTablesList($tables); |
---|
202 | return $this->filterAssetNames($tableNames); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Filter asset names if they are configured to return only a subset of all |
---|
207 | * the found elements. |
---|
208 | * |
---|
209 | * @param array $assetNames |
---|
210 | * @return array |
---|
211 | */ |
---|
212 | protected function filterAssetNames($assetNames) |
---|
213 | { |
---|
214 | $filterExpr = $this->getFilterSchemaAssetsExpression(); |
---|
215 | if (!$filterExpr) { |
---|
216 | return $assetNames; |
---|
217 | } |
---|
218 | return array_values ( |
---|
219 | array_filter($assetNames, function ($assetName) use ($filterExpr) { |
---|
220 | $assetName = ($assetName instanceof AbstractAsset) ? $assetName->getName() : $assetName; |
---|
221 | return preg_match('(' . $filterExpr . ')', $assetName); |
---|
222 | }) |
---|
223 | ); |
---|
224 | } |
---|
225 | |
---|
226 | protected function getFilterSchemaAssetsExpression() |
---|
227 | { |
---|
228 | return $this->_conn->getConfiguration()->getFilterSchemaAssetsExpression(); |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * List the tables for this connection |
---|
233 | * |
---|
234 | * @return Table[] |
---|
235 | */ |
---|
236 | public function listTables() |
---|
237 | { |
---|
238 | $tableNames = $this->listTableNames(); |
---|
239 | |
---|
240 | $tables = array(); |
---|
241 | foreach ($tableNames AS $tableName) { |
---|
242 | $tables[] = $this->listTableDetails($tableName); |
---|
243 | } |
---|
244 | |
---|
245 | return $tables; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * @param string $tableName |
---|
250 | * @return Table |
---|
251 | */ |
---|
252 | public function listTableDetails($tableName) |
---|
253 | { |
---|
254 | $columns = $this->listTableColumns($tableName); |
---|
255 | $foreignKeys = array(); |
---|
256 | if ($this->_platform->supportsForeignKeyConstraints()) { |
---|
257 | $foreignKeys = $this->listTableForeignKeys($tableName); |
---|
258 | } |
---|
259 | $indexes = $this->listTableIndexes($tableName); |
---|
260 | |
---|
261 | return new Table($tableName, $columns, $indexes, $foreignKeys, false, array()); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * List the views this connection has |
---|
266 | * |
---|
267 | * @return View[] |
---|
268 | */ |
---|
269 | public function listViews() |
---|
270 | { |
---|
271 | $database = $this->_conn->getDatabase(); |
---|
272 | $sql = $this->_platform->getListViewsSQL($database); |
---|
273 | $views = $this->_conn->fetchAll($sql); |
---|
274 | |
---|
275 | return $this->_getPortableViewsList($views); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * List the foreign keys for the given table |
---|
280 | * |
---|
281 | * @param string $table The name of the table |
---|
282 | * @return ForeignKeyConstraint[] |
---|
283 | */ |
---|
284 | public function listTableForeignKeys($table, $database = null) |
---|
285 | { |
---|
286 | if (is_null($database)) { |
---|
287 | $database = $this->_conn->getDatabase(); |
---|
288 | } |
---|
289 | $sql = $this->_platform->getListTableForeignKeysSQL($table, $database); |
---|
290 | $tableForeignKeys = $this->_conn->fetchAll($sql); |
---|
291 | |
---|
292 | return $this->_getPortableTableForeignKeysList($tableForeignKeys); |
---|
293 | } |
---|
294 | |
---|
295 | /* drop*() Methods */ |
---|
296 | |
---|
297 | /** |
---|
298 | * Drops a database. |
---|
299 | * |
---|
300 | * NOTE: You can not drop the database this SchemaManager is currently connected to. |
---|
301 | * |
---|
302 | * @param string $database The name of the database to drop |
---|
303 | */ |
---|
304 | public function dropDatabase($database) |
---|
305 | { |
---|
306 | $this->_execSql($this->_platform->getDropDatabaseSQL($database)); |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * Drop the given table |
---|
311 | * |
---|
312 | * @param string $table The name of the table to drop |
---|
313 | */ |
---|
314 | public function dropTable($table) |
---|
315 | { |
---|
316 | $this->_execSql($this->_platform->getDropTableSQL($table)); |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * Drop the index from the given table |
---|
321 | * |
---|
322 | * @param Index|string $index The name of the index |
---|
323 | * @param string|Table $table The name of the table |
---|
324 | */ |
---|
325 | public function dropIndex($index, $table) |
---|
326 | { |
---|
327 | if($index instanceof Index) { |
---|
328 | $index = $index->getQuotedName($this->_platform); |
---|
329 | } |
---|
330 | |
---|
331 | $this->_execSql($this->_platform->getDropIndexSQL($index, $table)); |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | * Drop the constraint from the given table |
---|
336 | * |
---|
337 | * @param Constraint $constraint |
---|
338 | * @param string $table The name of the table |
---|
339 | */ |
---|
340 | public function dropConstraint(Constraint $constraint, $table) |
---|
341 | { |
---|
342 | $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table)); |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * Drops a foreign key from a table. |
---|
347 | * |
---|
348 | * @param ForeignKeyConstraint|string $table The name of the table with the foreign key. |
---|
349 | * @param Table|string $name The name of the foreign key. |
---|
350 | * @return boolean $result |
---|
351 | */ |
---|
352 | public function dropForeignKey($foreignKey, $table) |
---|
353 | { |
---|
354 | $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table)); |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * Drops a sequence with a given name. |
---|
359 | * |
---|
360 | * @param string $name The name of the sequence to drop. |
---|
361 | */ |
---|
362 | public function dropSequence($name) |
---|
363 | { |
---|
364 | $this->_execSql($this->_platform->getDropSequenceSQL($name)); |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * Drop a view |
---|
369 | * |
---|
370 | * @param string $name The name of the view |
---|
371 | * @return boolean $result |
---|
372 | */ |
---|
373 | public function dropView($name) |
---|
374 | { |
---|
375 | $this->_execSql($this->_platform->getDropViewSQL($name)); |
---|
376 | } |
---|
377 | |
---|
378 | /* create*() Methods */ |
---|
379 | |
---|
380 | /** |
---|
381 | * Creates a new database. |
---|
382 | * |
---|
383 | * @param string $database The name of the database to create. |
---|
384 | */ |
---|
385 | public function createDatabase($database) |
---|
386 | { |
---|
387 | $this->_execSql($this->_platform->getCreateDatabaseSQL($database)); |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Create a new table. |
---|
392 | * |
---|
393 | * @param Table $table |
---|
394 | * @param int $createFlags |
---|
395 | */ |
---|
396 | public function createTable(Table $table) |
---|
397 | { |
---|
398 | $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS; |
---|
399 | $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags)); |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * Create a new sequence |
---|
404 | * |
---|
405 | * @param Sequence $sequence |
---|
406 | * @throws Doctrine\DBAL\ConnectionException if something fails at database level |
---|
407 | */ |
---|
408 | public function createSequence($sequence) |
---|
409 | { |
---|
410 | $this->_execSql($this->_platform->getCreateSequenceSQL($sequence)); |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Create a constraint on a table |
---|
415 | * |
---|
416 | * @param Constraint $constraint |
---|
417 | * @param string|Table $table |
---|
418 | */ |
---|
419 | public function createConstraint(Constraint $constraint, $table) |
---|
420 | { |
---|
421 | $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table)); |
---|
422 | } |
---|
423 | |
---|
424 | /** |
---|
425 | * Create a new index on a table |
---|
426 | * |
---|
427 | * @param Index $index |
---|
428 | * @param string $table name of the table on which the index is to be created |
---|
429 | */ |
---|
430 | public function createIndex(Index $index, $table) |
---|
431 | { |
---|
432 | $this->_execSql($this->_platform->getCreateIndexSQL($index, $table)); |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * Create a new foreign key |
---|
437 | * |
---|
438 | * @param ForeignKeyConstraint $foreignKey ForeignKey instance |
---|
439 | * @param string|Table $table name of the table on which the foreign key is to be created |
---|
440 | */ |
---|
441 | public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) |
---|
442 | { |
---|
443 | $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table)); |
---|
444 | } |
---|
445 | |
---|
446 | /** |
---|
447 | * Create a new view |
---|
448 | * |
---|
449 | * @param View $view |
---|
450 | */ |
---|
451 | public function createView(View $view) |
---|
452 | { |
---|
453 | $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql())); |
---|
454 | } |
---|
455 | |
---|
456 | /* dropAndCreate*() Methods */ |
---|
457 | |
---|
458 | /** |
---|
459 | * Drop and create a constraint |
---|
460 | * |
---|
461 | * @param Constraint $constraint |
---|
462 | * @param string $table |
---|
463 | * @see dropConstraint() |
---|
464 | * @see createConstraint() |
---|
465 | */ |
---|
466 | public function dropAndCreateConstraint(Constraint $constraint, $table) |
---|
467 | { |
---|
468 | $this->tryMethod('dropConstraint', $constraint, $table); |
---|
469 | $this->createConstraint($constraint, $table); |
---|
470 | } |
---|
471 | |
---|
472 | /** |
---|
473 | * Drop and create a new index on a table |
---|
474 | * |
---|
475 | * @param string|Table $table name of the table on which the index is to be created |
---|
476 | * @param Index $index |
---|
477 | */ |
---|
478 | public function dropAndCreateIndex(Index $index, $table) |
---|
479 | { |
---|
480 | $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table); |
---|
481 | $this->createIndex($index, $table); |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | * Drop and create a new foreign key |
---|
486 | * |
---|
487 | * @param ForeignKeyConstraint $foreignKey associative array that defines properties of the foreign key to be created. |
---|
488 | * @param string|Table $table name of the table on which the foreign key is to be created |
---|
489 | */ |
---|
490 | public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) |
---|
491 | { |
---|
492 | $this->tryMethod('dropForeignKey', $foreignKey, $table); |
---|
493 | $this->createForeignKey($foreignKey, $table); |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * Drop and create a new sequence |
---|
498 | * |
---|
499 | * @param Sequence $sequence |
---|
500 | * @throws Doctrine\DBAL\ConnectionException if something fails at database level |
---|
501 | */ |
---|
502 | public function dropAndCreateSequence(Sequence $sequence) |
---|
503 | { |
---|
504 | $this->tryMethod('dropSequence', $sequence->getQuotedName($this->_platform)); |
---|
505 | $this->createSequence($sequence); |
---|
506 | } |
---|
507 | |
---|
508 | /** |
---|
509 | * Drop and create a new table. |
---|
510 | * |
---|
511 | * @param Table $table |
---|
512 | */ |
---|
513 | public function dropAndCreateTable(Table $table) |
---|
514 | { |
---|
515 | $this->tryMethod('dropTable', $table->getQuotedName($this->_platform)); |
---|
516 | $this->createTable($table); |
---|
517 | } |
---|
518 | |
---|
519 | /** |
---|
520 | * Drop and creates a new database. |
---|
521 | * |
---|
522 | * @param string $database The name of the database to create. |
---|
523 | */ |
---|
524 | public function dropAndCreateDatabase($database) |
---|
525 | { |
---|
526 | $this->tryMethod('dropDatabase', $database); |
---|
527 | $this->createDatabase($database); |
---|
528 | } |
---|
529 | |
---|
530 | /** |
---|
531 | * Drop and create a new view |
---|
532 | * |
---|
533 | * @param View $view |
---|
534 | */ |
---|
535 | public function dropAndCreateView(View $view) |
---|
536 | { |
---|
537 | $this->tryMethod('dropView', $view->getQuotedName($this->_platform)); |
---|
538 | $this->createView($view); |
---|
539 | } |
---|
540 | |
---|
541 | /* alterTable() Methods */ |
---|
542 | |
---|
543 | /** |
---|
544 | * Alter an existing tables schema |
---|
545 | * |
---|
546 | * @param TableDiff $tableDiff |
---|
547 | */ |
---|
548 | public function alterTable(TableDiff $tableDiff) |
---|
549 | { |
---|
550 | $queries = $this->_platform->getAlterTableSQL($tableDiff); |
---|
551 | if (is_array($queries) && count($queries)) { |
---|
552 | foreach ($queries AS $ddlQuery) { |
---|
553 | $this->_execSql($ddlQuery); |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | /** |
---|
559 | * Rename a given table to another name |
---|
560 | * |
---|
561 | * @param string $name The current name of the table |
---|
562 | * @param string $newName The new name of the table |
---|
563 | */ |
---|
564 | public function renameTable($name, $newName) |
---|
565 | { |
---|
566 | $tableDiff = new TableDiff($name); |
---|
567 | $tableDiff->newName = $newName; |
---|
568 | $this->alterTable($tableDiff); |
---|
569 | } |
---|
570 | |
---|
571 | /** |
---|
572 | * Methods for filtering return values of list*() methods to convert |
---|
573 | * the native DBMS data definition to a portable Doctrine definition |
---|
574 | */ |
---|
575 | |
---|
576 | protected function _getPortableDatabasesList($databases) |
---|
577 | { |
---|
578 | $list = array(); |
---|
579 | foreach ($databases as $key => $value) { |
---|
580 | if ($value = $this->_getPortableDatabaseDefinition($value)) { |
---|
581 | $list[] = $value; |
---|
582 | } |
---|
583 | } |
---|
584 | return $list; |
---|
585 | } |
---|
586 | |
---|
587 | protected function _getPortableDatabaseDefinition($database) |
---|
588 | { |
---|
589 | return $database; |
---|
590 | } |
---|
591 | |
---|
592 | protected function _getPortableFunctionsList($functions) |
---|
593 | { |
---|
594 | $list = array(); |
---|
595 | foreach ($functions as $key => $value) { |
---|
596 | if ($value = $this->_getPortableFunctionDefinition($value)) { |
---|
597 | $list[] = $value; |
---|
598 | } |
---|
599 | } |
---|
600 | return $list; |
---|
601 | } |
---|
602 | |
---|
603 | protected function _getPortableFunctionDefinition($function) |
---|
604 | { |
---|
605 | return $function; |
---|
606 | } |
---|
607 | |
---|
608 | protected function _getPortableTriggersList($triggers) |
---|
609 | { |
---|
610 | $list = array(); |
---|
611 | foreach ($triggers as $key => $value) { |
---|
612 | if ($value = $this->_getPortableTriggerDefinition($value)) { |
---|
613 | $list[] = $value; |
---|
614 | } |
---|
615 | } |
---|
616 | return $list; |
---|
617 | } |
---|
618 | |
---|
619 | protected function _getPortableTriggerDefinition($trigger) |
---|
620 | { |
---|
621 | return $trigger; |
---|
622 | } |
---|
623 | |
---|
624 | protected function _getPortableSequencesList($sequences) |
---|
625 | { |
---|
626 | $list = array(); |
---|
627 | foreach ($sequences as $key => $value) { |
---|
628 | if ($value = $this->_getPortableSequenceDefinition($value)) { |
---|
629 | $list[] = $value; |
---|
630 | } |
---|
631 | } |
---|
632 | return $list; |
---|
633 | } |
---|
634 | |
---|
635 | /** |
---|
636 | * @param array $sequence |
---|
637 | * @return Sequence |
---|
638 | */ |
---|
639 | protected function _getPortableSequenceDefinition($sequence) |
---|
640 | { |
---|
641 | throw DBALException::notSupported('Sequences'); |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * Independent of the database the keys of the column list result are lowercased. |
---|
646 | * |
---|
647 | * The name of the created column instance however is kept in its case. |
---|
648 | * |
---|
649 | * @param string $table The name of the table. |
---|
650 | * @param string $database |
---|
651 | * @param array $tableColumns |
---|
652 | * @return array |
---|
653 | */ |
---|
654 | protected function _getPortableTableColumnList($table, $database, $tableColumns) |
---|
655 | { |
---|
656 | $eventManager = $this->_platform->getEventManager(); |
---|
657 | |
---|
658 | $list = array(); |
---|
659 | foreach ($tableColumns as $key => $tableColumn) { |
---|
660 | $column = null; |
---|
661 | $defaultPrevented = false; |
---|
662 | |
---|
663 | if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) { |
---|
664 | $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn); |
---|
665 | $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs); |
---|
666 | |
---|
667 | $defaultPrevented = $eventArgs->isDefaultPrevented(); |
---|
668 | $column = $eventArgs->getColumn(); |
---|
669 | } |
---|
670 | |
---|
671 | if (!$defaultPrevented) { |
---|
672 | $column = $this->_getPortableTableColumnDefinition($tableColumn); |
---|
673 | } |
---|
674 | |
---|
675 | if ($column) { |
---|
676 | $name = strtolower($column->getQuotedName($this->_platform)); |
---|
677 | $list[$name] = $column; |
---|
678 | } |
---|
679 | } |
---|
680 | return $list; |
---|
681 | } |
---|
682 | |
---|
683 | /** |
---|
684 | * Get Table Column Definition |
---|
685 | * |
---|
686 | * @param array $tableColumn |
---|
687 | * @return Column |
---|
688 | */ |
---|
689 | abstract protected function _getPortableTableColumnDefinition($tableColumn); |
---|
690 | |
---|
691 | /** |
---|
692 | * Aggregate and group the index results according to the required data result. |
---|
693 | * |
---|
694 | * @param array $tableIndexRows |
---|
695 | * @param string $tableName |
---|
696 | * @return array |
---|
697 | */ |
---|
698 | protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null) |
---|
699 | { |
---|
700 | $result = array(); |
---|
701 | foreach($tableIndexRows AS $tableIndex) { |
---|
702 | $indexName = $keyName = $tableIndex['key_name']; |
---|
703 | if($tableIndex['primary']) { |
---|
704 | $keyName = 'primary'; |
---|
705 | } |
---|
706 | $keyName = strtolower($keyName); |
---|
707 | |
---|
708 | if(!isset($result[$keyName])) { |
---|
709 | $result[$keyName] = array( |
---|
710 | 'name' => $indexName, |
---|
711 | 'columns' => array($tableIndex['column_name']), |
---|
712 | 'unique' => $tableIndex['non_unique'] ? false : true, |
---|
713 | 'primary' => $tableIndex['primary'], |
---|
714 | ); |
---|
715 | } else { |
---|
716 | $result[$keyName]['columns'][] = $tableIndex['column_name']; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | $eventManager = $this->_platform->getEventManager(); |
---|
721 | |
---|
722 | $indexes = array(); |
---|
723 | foreach($result AS $indexKey => $data) { |
---|
724 | $index = null; |
---|
725 | $defaultPrevented = false; |
---|
726 | |
---|
727 | if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) { |
---|
728 | $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn); |
---|
729 | $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs); |
---|
730 | |
---|
731 | $defaultPrevented = $eventArgs->isDefaultPrevented(); |
---|
732 | $index = $eventArgs->getIndex(); |
---|
733 | } |
---|
734 | |
---|
735 | if (!$defaultPrevented) { |
---|
736 | $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']); |
---|
737 | } |
---|
738 | |
---|
739 | if ($index) { |
---|
740 | $indexes[$indexKey] = $index; |
---|
741 | } |
---|
742 | } |
---|
743 | |
---|
744 | return $indexes; |
---|
745 | } |
---|
746 | |
---|
747 | protected function _getPortableTablesList($tables) |
---|
748 | { |
---|
749 | $list = array(); |
---|
750 | foreach ($tables as $key => $value) { |
---|
751 | if ($value = $this->_getPortableTableDefinition($value)) { |
---|
752 | $list[] = $value; |
---|
753 | } |
---|
754 | } |
---|
755 | return $list; |
---|
756 | } |
---|
757 | |
---|
758 | protected function _getPortableTableDefinition($table) |
---|
759 | { |
---|
760 | return $table; |
---|
761 | } |
---|
762 | |
---|
763 | protected function _getPortableUsersList($users) |
---|
764 | { |
---|
765 | $list = array(); |
---|
766 | foreach ($users as $key => $value) { |
---|
767 | if ($value = $this->_getPortableUserDefinition($value)) { |
---|
768 | $list[] = $value; |
---|
769 | } |
---|
770 | } |
---|
771 | return $list; |
---|
772 | } |
---|
773 | |
---|
774 | protected function _getPortableUserDefinition($user) |
---|
775 | { |
---|
776 | return $user; |
---|
777 | } |
---|
778 | |
---|
779 | protected function _getPortableViewsList($views) |
---|
780 | { |
---|
781 | $list = array(); |
---|
782 | foreach ($views as $key => $value) { |
---|
783 | if ($view = $this->_getPortableViewDefinition($value)) { |
---|
784 | $viewName = strtolower($view->getQuotedName($this->_platform)); |
---|
785 | $list[$viewName] = $view; |
---|
786 | } |
---|
787 | } |
---|
788 | return $list; |
---|
789 | } |
---|
790 | |
---|
791 | protected function _getPortableViewDefinition($view) |
---|
792 | { |
---|
793 | return false; |
---|
794 | } |
---|
795 | |
---|
796 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
---|
797 | { |
---|
798 | $list = array(); |
---|
799 | foreach ($tableForeignKeys as $key => $value) { |
---|
800 | if ($value = $this->_getPortableTableForeignKeyDefinition($value)) { |
---|
801 | $list[] = $value; |
---|
802 | } |
---|
803 | } |
---|
804 | return $list; |
---|
805 | } |
---|
806 | |
---|
807 | protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
---|
808 | { |
---|
809 | return $tableForeignKey; |
---|
810 | } |
---|
811 | |
---|
812 | protected function _execSql($sql) |
---|
813 | { |
---|
814 | foreach ((array) $sql as $query) { |
---|
815 | $this->_conn->executeUpdate($query); |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | /** |
---|
820 | * Create a schema instance for the current database. |
---|
821 | * |
---|
822 | * @return Schema |
---|
823 | */ |
---|
824 | public function createSchema() |
---|
825 | { |
---|
826 | $sequences = array(); |
---|
827 | if($this->_platform->supportsSequences()) { |
---|
828 | $sequences = $this->listSequences(); |
---|
829 | } |
---|
830 | $tables = $this->listTables(); |
---|
831 | |
---|
832 | return new Schema($tables, $sequences, $this->createSchemaConfig()); |
---|
833 | } |
---|
834 | |
---|
835 | /** |
---|
836 | * Create the configuration for this schema. |
---|
837 | * |
---|
838 | * @return SchemaConfig |
---|
839 | */ |
---|
840 | public function createSchemaConfig() |
---|
841 | { |
---|
842 | $schemaConfig = new SchemaConfig(); |
---|
843 | $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength()); |
---|
844 | |
---|
845 | $searchPaths = $this->getSchemaSearchPaths(); |
---|
846 | if (isset($searchPaths[0])) { |
---|
847 | $schemaConfig->setName($searchPaths[0]); |
---|
848 | } |
---|
849 | |
---|
850 | return $schemaConfig; |
---|
851 | } |
---|
852 | |
---|
853 | /** |
---|
854 | * The search path for namespaces in the currently connected database. |
---|
855 | * |
---|
856 | * The first entry is usually the default namespace in the Schema. All |
---|
857 | * further namespaces contain tables/sequences which can also be addressed |
---|
858 | * with a short, not full-qualified name. |
---|
859 | * |
---|
860 | * For databases that don't support subschema/namespaces this method |
---|
861 | * returns the name of the currently connected database. |
---|
862 | * |
---|
863 | * @return array |
---|
864 | */ |
---|
865 | public function getSchemaSearchPaths() |
---|
866 | { |
---|
867 | return array($this->_conn->getDatabase()); |
---|
868 | } |
---|
869 | |
---|
870 | /** |
---|
871 | * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns |
---|
872 | * the type given as default. |
---|
873 | * |
---|
874 | * @param string $comment |
---|
875 | * @param string $currentType |
---|
876 | * @return string |
---|
877 | */ |
---|
878 | public function extractDoctrineTypeFromComment($comment, $currentType) |
---|
879 | { |
---|
880 | if (preg_match("(\(DC2Type:([a-zA-Z0-9]+)\))", $comment, $match)) { |
---|
881 | $currentType = $match[1]; |
---|
882 | } |
---|
883 | return $currentType; |
---|
884 | } |
---|
885 | |
---|
886 | public function removeDoctrineTypeFromComment($comment, $type) |
---|
887 | { |
---|
888 | return str_replace('(DC2Type:'.$type.')', '', $comment); |
---|
889 | } |
---|
890 | } |
---|