DB2SchemaManager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
  23. /**
  24. * IBM Db2 Schema Manager
  25. *
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.com
  28. * @since 1.0
  29. * @version $Revision$
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. */
  32. class DB2SchemaManager extends AbstractSchemaManager
  33. {
  34. /**
  35. * Return a list of all tables in the current database
  36. *
  37. * Apparently creator is the schema not the user who created it:
  38. * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
  39. *
  40. * @return array
  41. */
  42. public function listTableNames()
  43. {
  44. $sql = $this->_platform->getListTablesSQL();
  45. $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
  46. $tables = $this->_conn->fetchAll($sql);
  47. return $this->_getPortableTablesList($tables);
  48. }
  49. /**
  50. * Get Table Column Definition
  51. *
  52. * @param array $tableColumn
  53. * @return Column
  54. */
  55. protected function _getPortableTableColumnDefinition($tableColumn)
  56. {
  57. $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
  58. $length = null;
  59. $fixed = null;
  60. $unsigned = false;
  61. $scale = false;
  62. $precision = false;
  63. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
  64. switch (strtolower($tableColumn['typename'])) {
  65. case 'varchar':
  66. $length = $tableColumn['length'];
  67. $fixed = false;
  68. break;
  69. case 'character':
  70. $length = $tableColumn['length'];
  71. $fixed = true;
  72. break;
  73. case 'clob':
  74. $length = $tableColumn['length'];
  75. break;
  76. case 'decimal':
  77. case 'double':
  78. case 'real':
  79. $scale = $tableColumn['scale'];
  80. $precision = $tableColumn['length'];
  81. break;
  82. }
  83. $options = array(
  84. 'length' => $length,
  85. 'unsigned' => (bool)$unsigned,
  86. 'fixed' => (bool)$fixed,
  87. 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'],
  88. 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
  89. 'scale' => null,
  90. 'precision' => null,
  91. 'platformOptions' => array(),
  92. );
  93. if ($scale !== null && $precision !== null) {
  94. $options['scale'] = $scale;
  95. $options['precision'] = $precision;
  96. }
  97. return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  98. }
  99. protected function _getPortableTablesList($tables)
  100. {
  101. $tableNames = array();
  102. foreach ($tables AS $tableRow) {
  103. $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
  104. $tableNames[] = $tableRow['name'];
  105. }
  106. return $tableNames;
  107. }
  108. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  109. {
  110. $eventManager = $this->_platform->getEventManager();
  111. $tableIndexRows = array();
  112. $indexes = array();
  113. foreach($tableIndexes AS $indexKey => $data) {
  114. $data = array_change_key_case($data, \CASE_LOWER);
  115. $unique = ($data['uniquerule'] == "D") ? false : true;
  116. $primary = ($data['uniquerule'] == "P");
  117. $indexName = strtolower($data['name']);
  118. if ($primary) {
  119. $keyName = 'primary';
  120. } else {
  121. $keyName = $indexName;
  122. }
  123. $data = array(
  124. 'name' => $indexName,
  125. 'columns' => explode("+", ltrim($data['colnames'], '+')),
  126. 'unique' => $unique,
  127. 'primary' => $primary
  128. );
  129. $index = null;
  130. $defaultPrevented = false;
  131. if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
  132. $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
  133. $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
  134. $defaultPrevented = $eventArgs->isDefaultPrevented();
  135. $index = $eventArgs->getIndex();
  136. }
  137. if (!$defaultPrevented) {
  138. $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  139. }
  140. if ($index) {
  141. $indexes[$indexKey] = $index;
  142. }
  143. }
  144. return $indexes;
  145. }
  146. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  147. {
  148. $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
  149. $tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']);
  150. $tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']);
  151. return new ForeignKeyConstraint(
  152. array_map('trim', (array)$tableForeignKey['fkcolnames']),
  153. $tableForeignKey['reftbname'],
  154. array_map('trim', (array)$tableForeignKey['pkcolnames']),
  155. $tableForeignKey['relname'],
  156. array(
  157. 'onUpdate' => $tableForeignKey['updaterule'],
  158. 'onDelete' => $tableForeignKey['deleterule'],
  159. )
  160. );
  161. }
  162. protected function _getPortableForeignKeyRuleDef($def)
  163. {
  164. if ($def == "C") {
  165. return "CASCADE";
  166. } else if ($def == "N") {
  167. return "SET NULL";
  168. }
  169. return null;
  170. }
  171. protected function _getPortableViewDefinition($view)
  172. {
  173. $view = array_change_key_case($view, \CASE_LOWER);
  174. // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
  175. //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
  176. if (!is_resource($view['text'])) {
  177. $pos = strpos($view['text'], ' AS ');
  178. $sql = substr($view['text'], $pos+4);
  179. } else {
  180. $sql = '';
  181. }
  182. return new View($view['name'], $sql);
  183. }
  184. }