AbstractSchemaManager.php 29 KB

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