Collects queries about tables to alter a schema.

@param \Fridge\DBAL\Schema\Diff\SchemaDiff $schemaDiff The schema difference.
774985
private function collectTables(SchemaDiff $schemaDiff)
    {
        foreach ($schemaDiff->getCreatedTables() as $table) {
            $this->createTableSQLCollector->collect($table);
        }

        foreach ($schemaDiff->getDroppedTables() as $table) {
            $this->dropTableSQLCollector->collect($table);
        }

        foreach ($schemaDiff->getAlteredTables() as $tableDiff) {
            $this->alterTableSQLCollector->collect($tableDiff);
        }
    }
130446
function setDiffEngineType( $diffEngineType )
    {
        if ( isset( $diffEngineType ) )
        {
            $this->DiffEngine = $diffEngineType;
            eZDebug::writeNotice( "Changing diff engine to type: " . $diffEngineType, 'eZDiff' );
        }
    }
1057238
public function upgradeSchema(Schema $schema)
    {
        // add new multilingual tables.
        if (!$schema->hasTable('MultilingualPageRelations')) {
            $mpr = $schema->createTable('MultilingualPageRelations');
            $mpr->addColumn('mpRelationID', 'integer', ['notnull' => true, 'unsigned' => true, 'default' => 0]);
            $mpr->addColumn('cID', 'integer', ['notnull' => true, 'unsigned' => true, 'default' => 0]);
            $mpr->addColumn('mpLanguage', 'string', ['notnull' => true, 'default' => '']);
            $mpr->addColumn('mpLocale', 'string', ['notnull' => true]);
            $mpr->setPrimaryKey(['mpRelationID', 'cID', 'mpLocale']);
        }
        if (!$schema->hasTable('MultilingualSections')) {
            $mus = $schema->createTable('MultilingualSections');
            $mus->addColumn('cID', 'integer', ['notnull' => true, 'unsigned' => true, 'default' => 0]);
            $mus->addColumn('msLanguage', 'string', ['notnull' => true, 'default' => '']);
            $mus->addColumn('msCountry', 'string', ['notnull' => true, 'default' => '']);
            $mus->setPrimaryKey(['cID']);
        }
        if (!$schema->hasTable('MultilingualTranslations')) {
            $mts = $schema->createTable('MultilingualTranslations');
            $mts->addColumn('mtID', 'integer', ['autoincrement' => true, 'unsigned' => true]);
            $mts->addColumn('mtSectionID', 'integer', ['unsigned' => true, 'notnull' => true, 'default' => 0]);
            $mts->addColumn('msgid', 'text', ['notnull' => false]);
            $mts->addColumn('msgstr', 'text', ['notnull' => false]);
            $mts->addColumn('context', 'text', ['notnull' => false]);
            $mts->addColumn('comments', 'text', ['notnull' => false]);
            $mts->addColumn('reference', 'text', ['notnull' => false]);
            $mts->addColumn('flags', 'text', ['notnull' => false]);
            $mts->addColumn('updated', 'datetime', ['notnull' => false]);
            $mts->setPrimaryKey(['mtID']);
        }
    }
382285
private function collectPrimaryKeys(TableDiff $tableDiff)
    {
        if ($tableDiff->getCreatedPrimaryKey() !== null) {
            $this->createPrimaryKeyQueries = array_merge(
                $this->createPrimaryKeyQueries,
                $this->platform->getCreatePrimaryKeySQLQueries(
                    $tableDiff->getCreatedPrimaryKey(),
                    $tableDiff->getNewAsset()->getName()
                )
            );
        }

        if ($tableDiff->getDroppedPrimaryKey() !== null) {
            $this->dropPrimaryKeyQueries = array_merge(
                $this->dropPrimaryKeyQueries,
                $this->platform->getDropPrimaryKeySQLQueries(
                    $tableDiff->getDroppedPrimaryKey(),
                    $tableDiff->getNewAsset()->getName()
                )
            );
        }
    }
1265152
public function getModifyTableForeignKeysDDL(TableDiff $tableDiff)
    {
        $ret = '';

        foreach ($tableDiff->getRemovedFks() as $fk) {
            $ret .= $this->getDropForeignKeyDDL($fk);
        }

        foreach ($tableDiff->getAddedFks() as $fk) {
            $ret .= $this->getAddForeignKeyDDL($fk);
        }

        foreach ($tableDiff->getModifiedFks() as $fkModification) {
            list($fromFk, $toFk) = $fkModification;
            $ret .= $this->getDropForeignKeyDDL($fromFk);
            $ret .= $this->getAddForeignKeyDDL($toFk);
        }

        return $ret;
    }
363603
public function getModifyDatabaseDDL(PropelDatabaseDiff $databaseDiff)
    {
        $ret = $this->getBeginDDL();

        foreach ($databaseDiff->getRemovedTables() as $table) {
            $ret .= $this->getDropTableDDL($table);
        }

        foreach ($databaseDiff->getRenamedTables() as $fromTableName => $toTableName) {
            $ret .= $this->getRenameTableDDL($fromTableName, $toTableName);
        }

        foreach ($databaseDiff->getAddedTables() as $table) {
            $ret .= $this->getAddTableDDL($table);
            $ret .= $this->getAddIndicesDDL($table);
        }

        foreach ($databaseDiff->getModifiedTables() as $tableDiff) {
            $ret .= $this->getModifyTableDDL($tableDiff);
        }

        foreach ($databaseDiff->getAddedTables() as $table) {
            $ret .= $this->getAddForeignKeysDDL($table);
        }

        $ret .= $this->getEndDDL();

        return $ret;
    }
1137650
protected function onSchemaAlterTable(TableDiff $diff, &$sql)
    {
        if ($this->_eventManager === null) {
            return false;
        }

        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
            return false;
        }

        $eventArgs = new SchemaAlterTableEventArgs($diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);

        $sql = array_merge($sql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }
896407
private function detectColumnRenamings(TableDiff $tableDifferences)
    {
        $renameCandidates = [];
        foreach ($tableDifferences->addedColumns as $addedColumnName => $addedColumn) {
            foreach ($tableDifferences->removedColumns as $removedColumn) {
                if (count($this->diffColumn($addedColumn, $removedColumn)) !== 0) {
                    continue;
                }

                $renameCandidates[$addedColumn->getName()][] = [$removedColumn, $addedColumn, $addedColumnName];
            }
        }

        foreach ($renameCandidates as $candidateColumns) {
            if (count($candidateColumns) !== 1) {
                continue;
            }

            [$removedColumn, $addedColumn] = $candidateColumns[0];
            $removedColumnName             = strtolower($removedColumn->getName());
            $addedColumnName               = strtolower($addedColumn->getName());

            if (isset($tableDifferences->renamedColumns[$removedColumnName])) {
                continue;
            }

            $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn;
            unset(
                $tableDifferences->addedColumns[$addedColumnName],
                $tableDifferences->removedColumns[$removedColumnName]
            );
        }
    }
1352490
protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
    {
        return array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableIndexForeignKeySQL($diff));
    }
303074
static function diffField( $field1, $field2, $schema1Type, $schema2Type )
    {
        /* Type is always available */
        if ( $field1['type'] != $field2['type'] )
        {
            return array( 'different-options' => array( 'type' ), 'field-def' => $field2 );
        }

        $test_fields = array( 'default', 'not_null' );
        
        // Ignore length for int types
        if ( $field1['type'] === $field2['type'] && $field1['type'] !== 'int' )
            $test_fields[] = 'length';

        $different_options = array();

        foreach ( $test_fields as $test_field )
        {
            if ( isset( $field1[$test_field] ) )
            {
                if ( !isset( $field2[$test_field] ) ||
                     ( $field1[$test_field] != $field2[$test_field] ) )
                {
                    $different_options[] = $test_field;
                }
            }
            else
            {
                if ( isset( $field2[$test_field] ) && $field2[$test_field] )
                {
                    $different_options[] = $test_field;
                }
            }
        }

        if ( $different_options )
            return array( 'different-options' => $different_options, 'field-def' => $field2 );
        else
            return false;
    }
1015627
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff)
    {
        $column = $columnDiff->column->toArray();

        $alterClause = 'ALTER COLUMN ' . $columnDiff->column->getQuotedName($this);

        if ($column['columnDefinition']) {
            return [$alterClause . ' ' . $column['columnDefinition']];
        }

        $clauses = [];

        if ($columnDiff->hasChanged('type') ||
            $columnDiff->hasChanged('length') ||
            $columnDiff->hasChanged('precision') ||
            $columnDiff->hasChanged('scale') ||
            $columnDiff->hasChanged('fixed')
        ) {
            $clauses[] = $alterClause . ' SET DATA TYPE ' . $column['type']->getSQLDeclaration($column, $this);
        }

        if ($columnDiff->hasChanged('notnull')) {
            $clauses[] = $column['notnull'] ? $alterClause . ' SET NOT NULL' : $alterClause . ' DROP NOT NULL';
        }

        if ($columnDiff->hasChanged('default')) {
            if (isset($column['default'])) {
                $defaultClause = $this->getDefaultValueDeclarationSQL($column);

                if ($defaultClause) {
                    $clauses[] = $alterClause . ' SET' . $defaultClause;
                }
            } else {
                $clauses[] = $alterClause . ' DROP DEFAULT';
            }
        }

        return $clauses;
    }
721652
public function compare(Schema $oldSchema, Schema $newSchema)
    {
        $createdTables = $this->getCreatedTables($oldSchema, $newSchema);
        $alteredTables = $this->getAlteredTables($oldSchema, $newSchema);
        $droppedTables = $this->getDroppedTables($oldSchema, $newSchema);

        $this->detectRenamedTables($createdTables, $droppedTables, $alteredTables);

        return new SchemaDiff(
            $oldSchema,
            $newSchema,
            $createdTables,
            $alteredTables,
            $droppedTables,
            $this->getCreatedSequences($oldSchema, $newSchema),
            $this->getDroppedSequences($oldSchema, $newSchema),
            $this->getCreatedViews($oldSchema, $newSchema),
            $this->getDroppedViews($oldSchema, $newSchema)
        );
    }
1021651
private function collectSequences(SchemaDiff $schemaDiff)
    {
        foreach ($schemaDiff->getCreatedSequences() as $sequence) {
            $this->createSequenceQueries = array_merge(
                $this->createSequenceQueries,
                $this->platform->getCreateSequenceSQLQueries($sequence)
            );
        }

        foreach ($schemaDiff->getDroppedSequences() as $sequence) {
            $this->dropSequenceQueries = array_merge(
                $this->dropSequenceQueries,
                $this->platform->getDropSequenceSQLQueries($sequence)
            );
        }
    }
634883
protected static function setRenamedColumns(TableDiff $tableDiff, Fluent $command, Column $column)
    {
        $tableDiff->renamedColumns = [
            $command->from => new Column($command->to, $column->getType(), $column->toArray()),
        ];

        return $tableDiff;
    }
1255804
public function getModifyColumnDDL(ColumnDiff $columnDiff)
    {
        $ret = '';
        $changedProperties = $columnDiff->getChangedProperties();

        $fromColumn = $columnDiff->getFromColumn();
        $toColumn = clone $columnDiff->getToColumn();

        $fromTable = $fromColumn->getTable();
        $table = $toColumn->getTable();

        $colName = $this->quoteIdentifier($toColumn->getName());

        $pattern = "
ALTER TABLE %s ALTER COLUMN %s;
";

        if (isset($changedProperties['autoIncrement'])) {
            $tableName = $table->getName();
            $colPlainName = $toColumn->getName();
            $seqName = "{$tableName}_{$colPlainName}_seq";

            if ($toColumn->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) {

                $defaultValue = "nextval('$seqName'::regclass)";
                $toColumn->setDefaultValue($defaultValue);
                $changedProperties['defaultValueValue'] = [null, $defaultValue];

                //add sequence
                if (!$fromTable->getDatabase()->hasSequence($seqName)) {
                    $this->createOrDropSequences .= sprintf("
CREATE SEQUENCE %s;
",
                        $seqName
                    );
                    $fromTable->getDatabase()->addSequence($seqName);
                }
            }

            if (!$toColumn->isAutoIncrement() && $fromColumn->isAutoIncrement()) {
                $changedProperties['defaultValueValue'] = [$fromColumn->getDefaultValueString(), null];
                $toColumn->setDefaultValue(null);

                //remove sequence
                if ($fromTable->getDatabase()->hasSequence($seqName)) {
                    $this->createOrDropSequences .= sprintf("
DROP SEQUENCE %s CASCADE;
",
                        $seqName
                    );
                    $fromTable->getDatabase()->removeSequence($seqName);
                }
            }
        }

        if (isset($changedProperties['size']) || isset($changedProperties['type']) || isset($changedProperties['sqlType']) || isset($changedProperties['scale'])) {

            $sqlType = $toColumn->getDomain()->getSqlType();

            if ($this->hasSize($sqlType) && $toColumn->isDefaultSqlType($this)) {
                if ($this->isNumber($sqlType)) {
                    if ('NUMERIC' === strtoupper($sqlType)) {
                        $sqlType .= $toColumn->getSizeDefinition();
                    }
                } else {
                    $sqlType .= $toColumn->getSizeDefinition();
                }
            }

            if ($using = $this->getUsingCast($fromColumn, $toColumn)) {
                $sqlType .= $using;
            }
            $ret .= sprintf($pattern,
                $this->quoteIdentifier($table->getName()),
                $colName . ' TYPE ' . $sqlType
            );
        }

        if (isset($changedProperties['defaultValueValue'])) {
            $property = $changedProperties['defaultValueValue'];
            if ($property[0] !== null && $property[1] === null) {
                $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' DROP DEFAULT');
            } else {
                $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' SET ' . $this->getColumnDefaultValueDDL($toColumn));
            }
        }

        if (isset($changedProperties['notNull'])) {
            $property = $changedProperties['notNull'];
            $notNull = ' DROP NOT NULL';
            if ($property[1]) {
                $notNull = ' SET NOT NULL';
            }
            $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . $notNull);
        }

        return $ret;
    }
1346424
public function adjustDiff(TableDiff $tableDiff)
    {
        /** @var IgnoredChange $ignoredChange */
        foreach ($this->ignoredChanges as $ignoredChange) {
            $alterName = $ignoredChange->getAlteration();

            // Array like 'changedColumns', 'changedIndexes', or 'changedForeignKeys'
            $schemaUpdateType = $tableDiff->$alterName;

            if (empty($schemaUpdateType)) {
                continue;
            }

            $this->checkChangedProperties($tableDiff, $schemaUpdateType, $alterName, $ignoredChange);
        }

        return $this->updateDiffTable($tableDiff);
    }
1361131
protected function normalizeRequestDiff($diff) {
		if (count($diff) === 1) {
			reset($diff);
			return key($diff);
		}
		
		return array_keys($diff);
	}