Collects queries about column to alter a table.

@param \Fridge\DBAL\Schema\Diff\TableDiff $tableDiff The table difference.
16354
private function collectColumns(TableDiff $tableDiff)
    {
        foreach ($tableDiff->getCreatedColumns() as $column) {
            $this->createColumnQueries = array_merge(
                $this->createColumnQueries,
                $this->platform->getCreateColumnSQLQueries($column, $tableDiff->getNewAsset()->getName())
            );
        }

        foreach ($tableDiff->getDroppedColumns() as $column) {
            $this->dropColumnQueries = array_merge(
                $this->dropColumnQueries,
                $this->platform->getDropColumnSQLQueries($column, $tableDiff->getNewAsset()->getName())
            );
        }

        foreach ($tableDiff->getAlteredColumns() as $columnDiff) {
            $this->alterColumnQueries = array_merge(
                $this->alterColumnQueries,
                $this->platform->getAlterColumnSQLQueries($columnDiff, $tableDiff->getNewAsset()->getName())
            );
        }
    }
733461
private function collectChecks(TableDiff $tableDiff)
    {
        foreach ($tableDiff->getCreatedChecks() as $check) {
            $this->createCheckQueries = array_merge(
                $this->createCheckQueries,
                $this->platform->getCreateCheckSQLQueries($check, $tableDiff->getNewAsset()->getName())
            );
        }

        foreach ($tableDiff->getDroppedChecks() as $check) {
            $this->dropCheckQueries = array_merge(
                $this->dropCheckQueries,
                $this->platform->getDropCheckSQLQueries($check, $tableDiff->getNewAsset()->getName())
            );
        }
    }
624707
private function buildDiff(Text $old, Text $new, $diffTable) {
        $inserted = 0;
        $deleted = 0;
        $diff = [];

        $row = count($this->old);
        $col = count($this->new);
        while ($row > 0 || $col > 0) {
            if ($row > 0 && $col > 0 && $this->old[$row - 1] === $this->new[$col - 1]) {
                $diff[] = new UnmodifiedEntry($old, $row - 1, $col - 1);
                $row--;
                $col--;
            } elseif ($col > 0 && $diffTable[$row][$col] === $diffTable[$row][$col - 1]) {
                $diff[] = new InsertedEntry($new, $col - 1);
                $inserted++;
                $col--;
            } else {
                $diff[] = new DeletedEntry($old, $row - 1);
                $deleted++;
                $row--;
            }
        }

        return [array_reverse($diff), $inserted, $deleted];
    }
945246
private function getAddedColumns($tableName, TableDiff $diff)
    {
        /** @var $col \Doctrine\DBAL\Schema\Column */
        foreach ($diff->addedColumns as $col) {
            $this->addMessage($tableName, sprintf('missing column `%s`', $col->getName()));
        }
    }
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()
                )
            );
        }
    }
1209356
private function getChangedColumns($tableName, TableDiff $diff)
    {
        /** @var $col \Doctrine\DBAL\Schema\ColumnDiff */
        foreach ($diff->changedColumns as $col) {
            $this->addMessage($tableName, sprintf('invalid column `%s`', $col->oldColumnName));
        }
    }
138660
public static function table_alter( $table, $changes ) {

		/**
		 * @var $wpdb wpdb
		 */
		global $wpdb;

		$sql = "ALTER TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}` {$changes}";

		return self::query( $sql );
	}
1150660
public function diffTable($table, $schema, $parse = true)
    {
        // parse input schema if required
        if ($parse) {
            $this->getParser()->parseTable($schema);
            $table = $this->getPrefix().$table;
        }

        // if table no exists return sql statament for creating this
        if (!$this->tableExists($table, false)) {
            $sql = $this
                ->getWriter()
                ->createTable($table, $schema);

            return [$sql];
        }

        $queries = $this->diffTableQueries($table, $schema);

        return $queries;
    }
753241
protected function updateColumn($name, \Doctrine\DBAL\Schema\ColumnDiff $col) {
		$res = "\n\t\$table->changeColumn('$name', [";

		$type_changed = in_array('type', $col->changedProperties);
		foreach($col->column->toArray() as $propName=>$prop) {
			if(($type_changed && $prop && $propName !== 'name') || in_array($propName, $col->changedProperties)) {
				if($propName === 'type')
					$res .= "\n\t\t'$propName' => '".strtolower($prop)."',";
				else
					$res .= "\n\t\t'$propName' => '$prop',";
			}
		}
		$res .= "\n\t]);";

		return $res;
	}
564385
public function getModifyTableDDL(TableDiff $tableDiff)
    {
        $ret = '';

        $toTable = $tableDiff->getToTable();

        // drop indices, foreign keys
        foreach ($tableDiff->getRemovedFks() as $fk) {
            $ret .= $this->getDropForeignKeyDDL($fk);
        }
        foreach ($tableDiff->getModifiedFks() as $fkModification) {
            list($fromFk) = $fkModification;
            $ret .= $this->getDropForeignKeyDDL($fromFk);
        }
        foreach ($tableDiff->getRemovedIndices() as $index) {
            $ret .= $this->getDropIndexDDL($index);
        }
        foreach ($tableDiff->getModifiedIndices() as $indexModification) {
            list($fromIndex) = $indexModification;
            $ret .= $this->getDropIndexDDL($fromIndex);
        }

        $columnChanges = '';

        // alter table structure
        if ($tableDiff->hasModifiedPk()) {
            $columnChanges .= $this->getDropPrimaryKeyDDL($tableDiff->getFromTable());
        }
        foreach ($tableDiff->getRenamedColumns() as $columnRenaming) {
            $columnChanges .= $this->getRenameColumnDDL($columnRenaming[0], $columnRenaming[1]);
        }
        if ($modifiedColumns = $tableDiff->getModifiedColumns()) {
            $columnChanges .= $this->getModifyColumnsDDL($modifiedColumns);
        }
        if ($addedColumns = $tableDiff->getAddedColumns()) {
            $columnChanges .= $this->getAddColumnsDDL($addedColumns);
        }
        foreach ($tableDiff->getRemovedColumns() as $column) {
            $columnChanges .= $this->getRemoveColumnDDL($column);
        }

        // add new indices and foreign keys
        if ($tableDiff->hasModifiedPk()) {
            $columnChanges .= $this->getAddPrimaryKeyDDL($tableDiff->getToTable());
        }

        if ($columnChanges) {
            //merge column changes into one command. This is more compatible especially with PK constraints.

            $changes = explode(';', $columnChanges);
            $columnChanges = [];

            foreach ($changes as $change) {
                if (!trim($change)) continue;
                $isCompatibleCall = preg_match(
                    sprintf('/ALTER TABLE %s (?!RENAME)/', $this->quoteIdentifier($toTable->getName())),
                    $change
                );
                if ($isCompatibleCall) {
                    $columnChanges[] = preg_replace(
                        sprintf('/ALTER TABLE %s /', $this->quoteIdentifier($toTable->getName())),
                        "\n\n  ",
                        trim($change)
                    );
                } else {
                    $ret .= $change.";\n";
                }
            }

            if (0 < count($columnChanges)) {
                $ret .= sprintf("
ALTER TABLE %s%s;
",
                    $this->quoteIdentifier($toTable->getName()), implode(',', $columnChanges));
            }
        }

        // create indices, foreign keys
        foreach ($tableDiff->getModifiedIndices() as $indexModification) {
            list($oldIndex, $toIndex) = $indexModification;
            $ret .= $this->getAddIndexDDL($toIndex);
        }
        foreach ($tableDiff->getAddedIndices() as $index) {
            $ret .= $this->getAddIndexDDL($index);
        }
        foreach ($tableDiff->getModifiedFks() as $fkModification) {
            list(, $toFk) = $fkModification;
            $ret .= $this->getAddForeignKeyDDL($toFk);
        }
        foreach ($tableDiff->getAddedFks() as $fk) {
            $ret .= $this->getAddForeignKeyDDL($fk);
        }

        return $ret;
    }
85449
private function diffTableQueries($table, &$schema)
    {
        // first order queries used as output array
        $foQueries = [];

        // second order queries used as output array
        $soQueries = [];

        // describe table get current table description
        $fields = $this->descTable($table);

        // test field definition
        foreach ($schema as $field => &$attributes) {
            $this->diffTableField(
                $table,
                $field,
                $attributes,
                $fields,
                $foQueries,
                $soQueries
            );
        }

        return $this->diffTableMergeQueries(
            $table,
            $fields,
            $foQueries,
            $soQueries
        );
    }
1017429
protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
    {
        if ($this->_eventManager === null) {
            return false;
        }

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

        $eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);

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

        return $eventArgs->isDefaultPrevented();
    }
432639
protected function alterTableAddedIndexes(TableDiff $tableDiff, QualifiedName $fromTableName, IndexColumns $indexColumns, \DOMElement $changeSetElt)
    {
        foreach ($tableDiff->addedIndexes as $index) {
            $createIndexElt = $this->document->createElement('createIndex');

            if ($fromTableName->getNamespaceName()) {
                $createIndexElt->setAttribute('schemaName', $fromTableName->getNamespaceName());
            }
            $createIndexElt->setAttribute('tableName', $fromTableName->getName());
            $createIndexElt->setAttribute('indexName', $index->getName());
            $createIndexElt->setAttribute('unique', $index->isUnique() ? 'true' : 'false');

            foreach ($index->getColumns() as $columnName) {
                $columnElt = $this->document->createElement('column');

                if ($tableDiff->fromTable->hasColumn($columnName)) {
                    $column = $tableDiff->fromTable->getColumn($columnName);
                } else {
                    $column = $tableDiff->addedColumns[$columnName];
                }

                $this->fillColumnAttributes($columnElt, $column, $indexColumns);

                $createIndexElt->appendChild($columnElt);
            }

            $changeSetElt->appendChild($createIndexElt);
        }
    }
177131
public function generateQueries(array $diff_res)
    {
        $queries = array();
        foreach ($diff_res[self::ADD] as $new_field) {
            $queries[] = $this->createAddQuery($new_field);
        }
        foreach ($diff_res[self::DEL] as $del_field) {
            $queries[] = $this->createDeleteQuery($del_field);
        }
        foreach ($diff_res[self::UPDATE] as $mod_field) {
            $queries[] = $this->createUpdateQuery($mod_field);
        }
        return $queries;
    }
485156
private function alterTableMigrate(array $alterDef, array $options = []) {
        $table = $alterDef['name'];
        $currentDef = $this->fetchTableDef($table);

        // Merge the table definitions if we aren't dropping stuff.
        if (!self::val(Db::OPTION_DROP, $options)) {
            $tableDef = $this->mergeTableDefs($currentDef, $alterDef);
        } else {
            $tableDef = $alterDef['def'];
        }

        // Drop all of the indexes on the current table.
        foreach (self::val('indexes', $currentDef, []) as $indexDef) {
            if (self::val('type', $indexDef, Db::INDEX_IX) === Db::INDEX_IX) {
                $this->dropIndex($indexDef['name']);
            }
        }

        $tmpTable = $table.'_'.time();

        // Rename the current table.
        $this->renameTable($table, $tmpTable);

        // Create the new table.
        $this->createTableDb($tableDef, $options);

        // Figure out the columns that we can insert.
        $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns']));

        // Build the insert/select statement.
        $sql = 'insert into '.$this->prefixTable($table)."\n".
            $this->bracketList($columns, '`')."\n".
            $this->buildSelect($tmpTable, [], ['columns' => $columns]);

        $this->queryDefine($sql);

        // Drop the temp table.
        $this->dropTable($tmpTable);
    }
843479
protected function isSchemaDiffEmpty(SchemaDiff $diff) : bool
    {
        return empty(array_filter([
                $diff->changedSequences,
                $diff->changedTables,
                $diff->newNamespaces,
                $diff->newSequences,
                $diff->newTables,
                $diff->removedTables,
                $diff->removedNamespaces,
                $diff->removedSequences,
                $diff->orphanedForeignKeys,
        ]));
    }
1093843
private function getAddedForeignKeys($tableName, TableDiff $diff)
    {
        foreach ($diff->addedForeignKeys as $foreignKey) {
            $this->addForeignKeysMessage($tableName, $foreignKey, 'missing foreign key on `%s` related to `%s.%s`');
        }
    }