AbstractSchemaManager.php 29 KB

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