Doctrine\DBAL\Platforms\DB2Platform::getAlterTableSQL PHP Method

getAlterTableSQL() public method

{@inheritDoc}
public getAlterTableSQL ( Doctrine\DBAL\Schema\TableDiff $diff )
$diff Doctrine\DBAL\Schema\TableDiff
    public function getAlterTableSQL(TableDiff $diff)
    {
        $sql = array();
        $columnSql = array();
        $commentsSQL = array();
        $queryParts = array();
        foreach ($diff->addedColumns as $column) {
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
            }
            $columnDef = $column->toArray();
            $queryPart = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);
            // Adding non-nullable columns to a table requires a default value to be specified.
            if (!empty($columnDef['notnull']) && !isset($columnDef['default']) && empty($columnDef['autoincrement'])) {
                $queryPart .= ' WITH DEFAULT';
            }
            $queryParts[] = $queryPart;
            $comment = $this->getColumnComment($column);
            if (null !== $comment && '' !== $comment) {
                $commentsSQL[] = $this->getCommentOnColumnSQL($diff->getName($this)->getQuotedName($this), $column->getQuotedName($this), $comment);
            }
        }
        foreach ($diff->removedColumns as $column) {
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
            }
            $queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
        }
        foreach ($diff->changedColumns as $columnDiff) {
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
            }
            if ($columnDiff->hasChanged('comment')) {
                $commentsSQL[] = $this->getCommentOnColumnSQL($diff->getName($this)->getQuotedName($this), $columnDiff->column->getQuotedName($this), $this->getColumnComment($columnDiff->column));
                if (count($columnDiff->changedProperties) === 1) {
                    continue;
                }
            }
            $this->gatherAlterColumnSQL($diff->fromTable, $columnDiff, $sql, $queryParts);
        }
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
                continue;
            }
            $oldColumnName = new Identifier($oldColumnName);
            $queryParts[] = 'RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
        }
        $tableSql = array();
        if (!$this->onSchemaAlterTable($diff, $tableSql)) {
            if (count($queryParts) > 0) {
                $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(" ", $queryParts);
            }
            // Some table alteration operations require a table reorganization.
            if (!empty($diff->removedColumns) || !empty($diff->changedColumns)) {
                $sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $diff->getName($this)->getQuotedName($this) . "')";
            }
            $sql = array_merge($sql, $commentsSQL);
            if ($diff->newName !== false) {
                $sql[] = 'RENAME TABLE ' . $diff->getName($this)->getQuotedName($this) . ' TO ' . $diff->getNewName()->getQuotedName($this);
            }
            $sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff));
        }
        return array_merge($sql, $tableSql, $columnSql);
    }

Usage Example

Example #1
0
 /**
  * @group DBAL-944
  *
  * @dataProvider getGeneratesAlterColumnSQL
  */
 public function testGeneratesAlterColumnSQL($changedProperty, Column $column, $expectedSQLClause = null)
 {
     $tableDiff = new TableDiff('foo');
     $tableDiff->fromTable = new Table('foo');
     $tableDiff->changedColumns['bar'] = new ColumnDiff('bar', $column, array($changedProperty));
     $expectedSQL = array();
     if (null !== $expectedSQLClause) {
         $expectedSQL[] = 'ALTER TABLE foo ALTER COLUMN bar ' . $expectedSQLClause;
     }
     $expectedSQL[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE foo')";
     $this->assertSame($expectedSQL, $this->_platform->getAlterTableSQL($tableDiff));
 }