DB2SchemaManager.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Types\Type;
  4. use const CASE_LOWER;
  5. use function array_change_key_case;
  6. use function is_resource;
  7. use function strpos;
  8. use function strtolower;
  9. use function substr;
  10. use function trim;
  11. /**
  12. * IBM Db2 Schema Manager.
  13. */
  14. class DB2SchemaManager extends AbstractSchemaManager
  15. {
  16. /**
  17. * {@inheritdoc}
  18. *
  19. * Apparently creator is the schema not the user who created it:
  20. * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
  21. */
  22. public function listTableNames()
  23. {
  24. $sql = $this->_platform->getListTablesSQL();
  25. $sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')';
  26. $tables = $this->_conn->fetchAll($sql);
  27. return $this->filterAssetNames($this->_getPortableTablesList($tables));
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function _getPortableTableColumnDefinition($tableColumn)
  33. {
  34. $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
  35. $length = null;
  36. $fixed = null;
  37. $unsigned = false;
  38. $scale = false;
  39. $precision = false;
  40. $default = null;
  41. if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
  42. $default = trim($tableColumn['default'], "'");
  43. }
  44. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
  45. if (isset($tableColumn['comment'])) {
  46. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  47. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  48. }
  49. switch (strtolower($tableColumn['typename'])) {
  50. case 'varchar':
  51. $length = $tableColumn['length'];
  52. $fixed = false;
  53. break;
  54. case 'character':
  55. $length = $tableColumn['length'];
  56. $fixed = true;
  57. break;
  58. case 'clob':
  59. $length = $tableColumn['length'];
  60. break;
  61. case 'decimal':
  62. case 'double':
  63. case 'real':
  64. $scale = $tableColumn['scale'];
  65. $precision = $tableColumn['length'];
  66. break;
  67. }
  68. $options = [
  69. 'length' => $length,
  70. 'unsigned' => (bool) $unsigned,
  71. 'fixed' => (bool) $fixed,
  72. 'default' => $default,
  73. 'autoincrement' => (bool) $tableColumn['autoincrement'],
  74. 'notnull' => (bool) ($tableColumn['nulls'] === 'N'),
  75. 'scale' => null,
  76. 'precision' => null,
  77. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  78. ? $tableColumn['comment']
  79. : null,
  80. 'platformOptions' => [],
  81. ];
  82. if ($scale !== null && $precision !== null) {
  83. $options['scale'] = $scale;
  84. $options['precision'] = $precision;
  85. }
  86. return new Column($tableColumn['colname'], Type::getType($type), $options);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function _getPortableTablesList($tables)
  92. {
  93. $tableNames = [];
  94. foreach ($tables as $tableRow) {
  95. $tableRow = array_change_key_case($tableRow, CASE_LOWER);
  96. $tableNames[] = $tableRow['name'];
  97. }
  98. return $tableNames;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
  104. {
  105. foreach ($tableIndexRows as &$tableIndexRow) {
  106. $tableIndexRow = array_change_key_case($tableIndexRow, CASE_LOWER);
  107. $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
  108. }
  109. return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  115. {
  116. return new ForeignKeyConstraint(
  117. $tableForeignKey['local_columns'],
  118. $tableForeignKey['foreign_table'],
  119. $tableForeignKey['foreign_columns'],
  120. $tableForeignKey['name'],
  121. $tableForeignKey['options']
  122. );
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  128. {
  129. $foreignKeys = [];
  130. foreach ($tableForeignKeys as $tableForeignKey) {
  131. $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
  132. if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
  133. $foreignKeys[$tableForeignKey['index_name']] = [
  134. 'local_columns' => [$tableForeignKey['local_column']],
  135. 'foreign_table' => $tableForeignKey['foreign_table'],
  136. 'foreign_columns' => [$tableForeignKey['foreign_column']],
  137. 'name' => $tableForeignKey['index_name'],
  138. 'options' => [
  139. 'onUpdate' => $tableForeignKey['on_update'],
  140. 'onDelete' => $tableForeignKey['on_delete'],
  141. ],
  142. ];
  143. } else {
  144. $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
  145. $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
  146. }
  147. }
  148. return parent::_getPortableTableForeignKeysList($foreignKeys);
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. protected function _getPortableForeignKeyRuleDef($def)
  154. {
  155. if ($def === 'C') {
  156. return 'CASCADE';
  157. } elseif ($def === 'N') {
  158. return 'SET NULL';
  159. }
  160. return null;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. protected function _getPortableViewDefinition($view)
  166. {
  167. $view = array_change_key_case($view, CASE_LOWER);
  168. // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
  169. //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
  170. if (! is_resource($view['text'])) {
  171. $pos = strpos($view['text'], ' AS ');
  172. $sql = substr($view['text'], $pos+4);
  173. } else {
  174. $sql = '';
  175. }
  176. return new View($view['name'], $sql);
  177. }
  178. }