DB2SchemaManager.php 6.3KB

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