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 MIT license. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
  23. use Doctrine\DBAL\Events;
  24. /**
  25. * IBM Db2 Schema Manager
  26. *
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.com
  29. * @since 1.0
  30. * @version $Revision$
  31. * @author Benjamin Eberlei <kontakt@beberlei.de>
  32. */
  33. class DB2SchemaManager extends AbstractSchemaManager
  34. {
  35. /**
  36. * Return a list of all tables in the current database
  37. *
  38. * Apparently creator is the schema not the user who created it:
  39. * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
  40. *
  41. * @return array
  42. */
  43. public function listTableNames()
  44. {
  45. $sql = $this->_platform->getListTablesSQL();
  46. $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
  47. $tables = $this->_conn->fetchAll($sql);
  48. return $this->_getPortableTablesList($tables);
  49. }
  50. /**
  51. * Get Table Column Definition
  52. *
  53. * @param array $tableColumn
  54. * @return Column
  55. */
  56. protected function _getPortableTableColumnDefinition($tableColumn)
  57. {
  58. $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
  59. $length = null;
  60. $fixed = null;
  61. $unsigned = false;
  62. $scale = false;
  63. $precision = false;
  64. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
  65. switch (strtolower($tableColumn['typename'])) {
  66. case 'varchar':
  67. $length = $tableColumn['length'];
  68. $fixed = false;
  69. break;
  70. case 'character':
  71. $length = $tableColumn['length'];
  72. $fixed = true;
  73. break;
  74. case 'clob':
  75. $length = $tableColumn['length'];
  76. break;
  77. case 'decimal':
  78. case 'double':
  79. case 'real':
  80. $scale = $tableColumn['scale'];
  81. $precision = $tableColumn['length'];
  82. break;
  83. }
  84. $options = array(
  85. 'length' => $length,
  86. 'unsigned' => (bool)$unsigned,
  87. 'fixed' => (bool)$fixed,
  88. 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'],
  89. 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
  90. 'scale' => null,
  91. 'precision' => null,
  92. 'platformOptions' => array(),
  93. );
  94. if ($scale !== null && $precision !== null) {
  95. $options['scale'] = $scale;
  96. $options['precision'] = $precision;
  97. }
  98. return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  99. }
  100. protected function _getPortableTablesList($tables)
  101. {
  102. $tableNames = array();
  103. foreach ($tables as $tableRow) {
  104. $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
  105. $tableNames[] = $tableRow['name'];
  106. }
  107. return $tableNames;
  108. }
  109. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  110. {
  111. $eventManager = $this->_platform->getEventManager();
  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. }