DB2SchemaManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Event\SchemaIndexDefinitionEventArgs;
  21. use Doctrine\DBAL\Events;
  22. /**
  23. * IBM Db2 Schema Manager.
  24. *
  25. * @link www.doctrine-project.org
  26. * @since 1.0
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class DB2SchemaManager extends AbstractSchemaManager
  30. {
  31. /**
  32. * {@inheritdoc}
  33. *
  34. * Apparently creator is the schema not the user who created it:
  35. * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
  36. */
  37. public function listTableNames()
  38. {
  39. $sql = $this->_platform->getListTablesSQL();
  40. $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
  41. $tables = $this->_conn->fetchAll($sql);
  42. return $this->_getPortableTablesList($tables);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function _getPortableTableColumnDefinition($tableColumn)
  48. {
  49. $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
  50. $length = null;
  51. $fixed = null;
  52. $unsigned = false;
  53. $scale = false;
  54. $precision = false;
  55. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
  56. switch (strtolower($tableColumn['typename'])) {
  57. case 'varchar':
  58. $length = $tableColumn['length'];
  59. $fixed = false;
  60. break;
  61. case 'character':
  62. $length = $tableColumn['length'];
  63. $fixed = true;
  64. break;
  65. case 'clob':
  66. $length = $tableColumn['length'];
  67. break;
  68. case 'decimal':
  69. case 'double':
  70. case 'real':
  71. $scale = $tableColumn['scale'];
  72. $precision = $tableColumn['length'];
  73. break;
  74. }
  75. $options = array(
  76. 'length' => $length,
  77. 'unsigned' => (bool)$unsigned,
  78. 'fixed' => (bool)$fixed,
  79. 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'],
  80. 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
  81. 'scale' => null,
  82. 'precision' => null,
  83. 'platformOptions' => array(),
  84. );
  85. if ($scale !== null && $precision !== null) {
  86. $options['scale'] = $scale;
  87. $options['precision'] = $precision;
  88. }
  89. return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function _getPortableTablesList($tables)
  95. {
  96. $tableNames = array();
  97. foreach ($tables as $tableRow) {
  98. $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
  99. $tableNames[] = $tableRow['name'];
  100. }
  101. return $tableNames;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  107. {
  108. $eventManager = $this->_platform->getEventManager();
  109. $indexes = array();
  110. foreach($tableIndexes as $indexKey => $data) {
  111. $data = array_change_key_case($data, \CASE_LOWER);
  112. $unique = ($data['uniquerule'] == "D") ? false : true;
  113. $primary = ($data['uniquerule'] == "P");
  114. $indexName = strtolower($data['name']);
  115. $data = array(
  116. 'name' => $indexName,
  117. 'columns' => explode("+", ltrim($data['colnames'], '+')),
  118. 'unique' => $unique,
  119. 'primary' => $primary
  120. );
  121. $index = null;
  122. $defaultPrevented = false;
  123. if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
  124. $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
  125. $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
  126. $defaultPrevented = $eventArgs->isDefaultPrevented();
  127. $index = $eventArgs->getIndex();
  128. }
  129. if ( ! $defaultPrevented) {
  130. $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  131. }
  132. if ($index) {
  133. $indexes[$indexKey] = $index;
  134. }
  135. }
  136. return $indexes;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  142. {
  143. $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
  144. $tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']);
  145. $tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']);
  146. return new ForeignKeyConstraint(
  147. array_map('trim', (array)$tableForeignKey['fkcolnames']),
  148. $tableForeignKey['reftbname'],
  149. array_map('trim', (array)$tableForeignKey['pkcolnames']),
  150. $tableForeignKey['relname'],
  151. array(
  152. 'onUpdate' => $tableForeignKey['updaterule'],
  153. 'onDelete' => $tableForeignKey['deleterule'],
  154. )
  155. );
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. protected function _getPortableForeignKeyRuleDef($def)
  161. {
  162. if ($def == "C") {
  163. return "CASCADE";
  164. } else if ($def == "N") {
  165. return "SET NULL";
  166. }
  167. return null;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. protected function _getPortableViewDefinition($view)
  173. {
  174. $view = array_change_key_case($view, \CASE_LOWER);
  175. // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
  176. //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
  177. if (!is_resource($view['text'])) {
  178. $pos = strpos($view['text'], ' AS ');
  179. $sql = substr($view['text'], $pos+4);
  180. } else {
  181. $sql = '';
  182. }
  183. return new View($view['name'], $sql);
  184. }
  185. }