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