123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /*
- * $Id$
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\DBAL\Schema;
- use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
- /**
- * IBM Db2 Schema Manager
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @link www.doctrine-project.com
- * @since 1.0
- * @version $Revision$
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- */
- class DB2SchemaManager extends AbstractSchemaManager
- {
- /**
- * Return a list of all tables in the current database
- *
- * Apparently creator is the schema not the user who created it:
- * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
- *
- * @return array
- */
- public function listTableNames()
- {
- $sql = $this->_platform->getListTablesSQL();
- $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
- $tables = $this->_conn->fetchAll($sql);
- return $this->_getPortableTablesList($tables);
- }
- /**
- * Get Table Column Definition
- *
- * @param array $tableColumn
- * @return Column
- */
- protected function _getPortableTableColumnDefinition($tableColumn)
- {
- $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
- $length = null;
- $fixed = null;
- $unsigned = false;
- $scale = false;
- $precision = false;
- $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
- switch (strtolower($tableColumn['typename'])) {
- case 'varchar':
- $length = $tableColumn['length'];
- $fixed = false;
- break;
- case 'character':
- $length = $tableColumn['length'];
- $fixed = true;
- break;
- case 'clob':
- $length = $tableColumn['length'];
- break;
- case 'decimal':
- case 'double':
- case 'real':
- $scale = $tableColumn['scale'];
- $precision = $tableColumn['length'];
- break;
- }
- $options = array(
- 'length' => $length,
- 'unsigned' => (bool)$unsigned,
- 'fixed' => (bool)$fixed,
- 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'],
- 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
- 'scale' => null,
- 'precision' => null,
- 'platformOptions' => array(),
- );
- if ($scale !== null && $precision !== null) {
- $options['scale'] = $scale;
- $options['precision'] = $precision;
- }
- return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
- }
- protected function _getPortableTablesList($tables)
- {
- $tableNames = array();
- foreach ($tables AS $tableRow) {
- $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
- $tableNames[] = $tableRow['name'];
- }
- return $tableNames;
- }
- protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
- {
- $eventManager = $this->_platform->getEventManager();
- $tableIndexRows = array();
- $indexes = array();
- foreach($tableIndexes AS $indexKey => $data) {
- $data = array_change_key_case($data, \CASE_LOWER);
- $unique = ($data['uniquerule'] == "D") ? false : true;
- $primary = ($data['uniquerule'] == "P");
- $indexName = strtolower($data['name']);
- if ($primary) {
- $keyName = 'primary';
- } else {
- $keyName = $indexName;
- }
- $data = array(
- 'name' => $indexName,
- 'columns' => explode("+", ltrim($data['colnames'], '+')),
- 'unique' => $unique,
- 'primary' => $primary
- );
- $index = null;
- $defaultPrevented = false;
- if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
- $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
- $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
- $defaultPrevented = $eventArgs->isDefaultPrevented();
- $index = $eventArgs->getIndex();
- }
- if (!$defaultPrevented) {
- $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
- }
- if ($index) {
- $indexes[$indexKey] = $index;
- }
- }
- return $indexes;
- }
- protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
- {
- $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
- $tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']);
- $tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']);
- return new ForeignKeyConstraint(
- array_map('trim', (array)$tableForeignKey['fkcolnames']),
- $tableForeignKey['reftbname'],
- array_map('trim', (array)$tableForeignKey['pkcolnames']),
- $tableForeignKey['relname'],
- array(
- 'onUpdate' => $tableForeignKey['updaterule'],
- 'onDelete' => $tableForeignKey['deleterule'],
- )
- );
- }
- protected function _getPortableForeignKeyRuleDef($def)
- {
- if ($def == "C") {
- return "CASCADE";
- } else if ($def == "N") {
- return "SET NULL";
- }
- return null;
- }
- protected function _getPortableViewDefinition($view)
- {
- $view = array_change_key_case($view, \CASE_LOWER);
- // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
- //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
- if (!is_resource($view['text'])) {
- $pos = strpos($view['text'], ' AS ');
- $sql = substr($view['text'], $pos+4);
- } else {
- $sql = '';
- }
- return new View($view['name'], $sql);
- }
- }
|