public function __construct(TableInformation $parent, \Doctrine\DBAL\Schema\Table $table, \Doctrine\DBAL\Schema\Column $column) { $this->table = $parent; foreach ($table->getForeignKeys() as $foreign) { if (in_array($column->getName(), $foreign->getColumns())) { $foreign_columns = $foreign->getForeignColumns(); $this->foreignTable = $foreign->getForeignTableName(); $this->foreignColumn = reset($foreign_columns); $this->isForeign = true; } } if ($primary_key = $table->getPrimaryKey()) { $this->isPrimary = in_array($column->getName(), $primary_key->getColumns()); } $this->name = $column->getName(); $this->type = $column->getType()->getName(); $this->length = $column->getLength(); $this->precision = $column->getPrecision(); $this->default = $column->getDefault(); $this->isNotNull = $column->getNotnull(); $this->isUnsigned = $column->getUnsigned(); $this->isFixed = $column->getFixed(); $this->isAutoIncrement = $column->getAutoincrement(); $this->comment = $column->getComment(); if ($this->type === \Doctrine\DBAL\Types\Type::BLOB) { $this->length = min($this->bytesFromIni('post_max_size'), $this->bytesFromIni('upload_max_filesize')); } }
/** * Metodo para verificar se a coluna eh chave primaria * * @param Column $objColumn * @return bool */ private function isPrimaryKey($objColumn) { $booIsPrimaryKey = false; if (is_int(array_search($objColumn->getName(), $this->objTable->getPrimaryKey()->getColumns()))) { $booIsPrimaryKey = true; } return $booIsPrimaryKey; }
/** * Convert all the table columns into form elements * * @return array * @throws \Exception */ public function getElements() { if (!$this->table instanceof Table) { throw new \Exception('Table not set'); } $pkColumns = $this->table->getPrimaryKey()->getColumns(); $elements = []; $fks = []; $fk = $this->table->getForeignKeys(); if (!empty($fk)) { foreach ($fk as $f) { $fks = array_merge($fks, $f->getLocalColumns()); } } foreach ($this->table->getColumns() as $column) { $isPrimaryKey = in_array($column->getName(), $pkColumns); if ($this->helper->isExcluded($column) || $isPrimaryKey) { continue; } $elements[] = $this->mapColumn($column, $fks); } return $elements; }
/** * Check if this sequence is an autoincrement sequence for a given table. * * This is used inside the comparator to not report sequences as missing, * when the "from" schema implicitly creates the sequences. * * @param Table $table * * @return bool */ public function isAutoIncrementsFor(Table $table) { if (!$table->hasPrimaryKey()) { return false; } $pkColumns = $table->getPrimaryKey()->getColumns(); if (count($pkColumns) != 1) { return false; } $column = $table->getColumn($pkColumns[0]); if (!$column->getAutoincrement()) { return false; } $sequenceName = $this->getShortestName($table->getNamespaceName()); $tableName = $table->getShortestName($table->getNamespaceName()); $tableSequenceName = sprintf('%s_%s_seq', $tableName, $pkColumns[0]); return $tableSequenceName === $sequenceName; }
/** * @param \Doctrine\DBAL\Schema\Table $table * * @return string */ private function createTableLabel(Table $table) { // Start the table $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">'; // The title $label .= '<TR><TD BORDER="1" COLSPAN="3" ALIGN="CENTER" BGCOLOR="#fcaf3e"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $table->getName() . '</FONT></TD></TR>'; // The attributes block foreach ($table->getColumns() as $column) { $columnLabel = $column->getName(); $label .= '<TR>'; $label .= '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">'; $label .= '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $columnLabel . '</FONT>'; $label .= '</TD><TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="10">' . strtolower($column->getType()) . '</FONT></TD>'; $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">'; if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) { $label .= "✷"; } $label .= '</TD></TR>'; } // End the table $label .= '</TABLE>>'; return $label; }
/** * @param \TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateIndexDefinitionItem $item * @return \Doctrine\DBAL\Schema\Index * @throws \Doctrine\DBAL\Schema\SchemaException * @throws \InvalidArgumentException */ protected function addIndex(CreateIndexDefinitionItem $item) : Index { $indexName = $item->indexName->getQuotedName(); $columnNames = array_map(function (IndexColumnName $columnName) { if ($columnName->length) { return $columnName->columnName->getQuotedName() . '(' . $columnName->length . ')'; } return $columnName->columnName->getQuotedName(); }, $item->columnNames); if ($item->isPrimary) { $this->table->setPrimaryKey($columnNames); $index = $this->table->getPrimaryKey(); } else { $index = GeneralUtility::makeInstance(Index::class, $indexName, $columnNames, $item->isUnique, $item->isPrimary); if ($item->isFulltext) { $index->addFlag('fulltext'); } elseif ($item->isSpatial) { $index->addFlag('spatial'); } $this->table = GeneralUtility::makeInstance(Table::class, $this->table->getQuotedName($this->platform), $this->table->getColumns(), array_merge($this->table->getIndexes(), [strtolower($indexName) => $index]), $this->table->getForeignKeys(), 0, $this->table->getOptions()); } return $index; }
/** * Load the metadata for a table. * * @param Table $table */ protected function loadMetadataForTable(Table $table) { $tblName = $table->getName(); if (isset($this->defaultAliases[$tblName])) { $className = $this->defaultAliases[$tblName]; } else { $className = $tblName; $this->unmapped[] = $tblName; } $contentKey = $this->schemaManager->getKeyForTable($tblName); $this->metadata[$className] = []; $this->metadata[$className]['identifier'] = $table->getPrimaryKey(); $this->metadata[$className]['table'] = $table->getName(); $this->metadata[$className]['boltname'] = $contentKey; foreach ($table->getColumns() as $colName => $column) { $mapping = ['fieldname' => $colName, 'type' => $column->getType()->getName(), 'fieldtype' => $this->getFieldTypeFor($table->getName(), $column), 'length' => $column->getLength(), 'nullable' => $column->getNotnull(), 'platformOptions' => $column->getPlatformOptions(), 'precision' => $column->getPrecision(), 'scale' => $column->getScale(), 'default' => $column->getDefault(), 'columnDefinition' => $column->getColumnDefinition(), 'autoincrement' => $column->getAutoincrement()]; $this->metadata[$className]['fields'][$colName] = $mapping; if (isset($this->contenttypes[$contentKey]['fields'][$colName])) { $this->metadata[$className]['fields'][$colName]['data'] = $this->contenttypes[$contentKey]['fields'][$colName]; } } // This loop checks the contenttypes definition for any non-db fields and adds them. if ($contentKey) { $this->setRelations($contentKey, $className, $table); $this->setTaxonomies($contentKey, $className, $table); $this->setTemplatefields($contentKey, $className, $table); $this->setRepeaters($contentKey, $className, $table); } foreach ($this->getAliases() as $alias => $table) { if (array_key_exists($table, $this->metadata)) { $this->metadata[$alias] = $this->metadata[$table]; } } }
/** * Retreive schema table definition primary keys. * * @param \Doctrine\DBAL\Schema\Table $table * * @return array */ private function getTablePrimaryKeys(Table $table) { try { return $table->getPrimaryKey()->getColumns(); } catch (SchemaException $e) { // Do nothing } return array(); }
public function testBuilderSetPrimaryKey() { $table = new Table("foo"); $table->addColumn("bar", 'integer'); $table->setPrimaryKey(array("bar")); $this->assertTrue($table->hasIndex("primary")); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Index', $table->getPrimaryKey()); $this->assertTrue($table->getIndex("primary")->isUnique()); $this->assertTrue($table->getIndex("primary")->isPrimary()); }
/** * @param Table $table * @return string * @throws SchemaException if valid primary key does not exist */ protected function getPrimaryKeyColumnName(Table $table) { if (!$table->hasPrimaryKey()) { throw new SchemaException(sprintf('The table "%s" must have a primary key.', $table->getName())); } $primaryKeyColumns = $table->getPrimaryKey()->getColumns(); if (count($primaryKeyColumns) !== 1) { throw new SchemaException(sprintf('A primary key of "%s" table must include only one column.', $table->getName())); } return array_pop($primaryKeyColumns); }
/** * Returns the difference between the tables $table1 and $table2. * * If there are no differences this method returns the boolean false. * * @param \Doctrine\DBAL\Schema\Table $table1 * @param \Doctrine\DBAL\Schema\Table $table2 * * @return boolean|\Doctrine\DBAL\Schema\TableDiff */ public function diffTable(Table $table1, Table $table2) { $changes = 0; $tableDifferences = new TableDiff($table1->getName()); $tableDifferences->fromTable = $table1; $table1Columns = $table1->getColumns(); $table2Columns = $table2->getColumns(); /* See if all the fields in table 1 exist in table 2 */ foreach ($table2Columns as $columnName => $column) { if (!$table1->hasColumn($columnName)) { $tableDifferences->addedColumns[$columnName] = $column; $changes++; } } /* See if there are any removed fields in table 2 */ foreach ($table1Columns as $columnName => $column) { // See if column is removed in table 2. if (!$table2->hasColumn($columnName)) { $tableDifferences->removedColumns[$columnName] = $column; $changes++; continue; } // See if column has changed properties in table 2. $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName)); if (!empty($changedProperties)) { $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); $columnDiff->fromColumn = $column; $tableDifferences->changedColumns[$column->getName()] = $columnDiff; $changes++; } } $this->detectColumnRenamings($tableDifferences); $table1Indexes = $table1->getIndexes(); $table2Indexes = $table2->getIndexes(); /* See if all the indexes in table 1 exist in table 2 */ foreach ($table2Indexes as $indexName => $index) { if ($index->isPrimary() && $table1->hasPrimaryKey() || $table1->hasIndex($indexName)) { continue; } $tableDifferences->addedIndexes[$indexName] = $index; $changes++; } /* See if there are any removed indexes in table 2 */ foreach ($table1Indexes as $indexName => $index) { // See if index is removed in table 2. if ($index->isPrimary() && !$table2->hasPrimaryKey() || !$index->isPrimary() && !$table2->hasIndex($indexName)) { $tableDifferences->removedIndexes[$indexName] = $index; $changes++; continue; } // See if index has changed in table 2. $table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName); if ($this->diffIndex($index, $table2Index)) { $tableDifferences->changedIndexes[$indexName] = $table2Index; $changes++; } } $this->detectIndexRenamings($tableDifferences); $fromFkeys = $table1->getForeignKeys(); $toFkeys = $table2->getForeignKeys(); foreach ($fromFkeys as $key1 => $constraint1) { foreach ($toFkeys as $key2 => $constraint2) { if ($this->diffForeignKey($constraint1, $constraint2) === false) { unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } else { if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { $tableDifferences->changedForeignKeys[] = $constraint2; $changes++; unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } } } } foreach ($fromFkeys as $constraint1) { $tableDifferences->removedForeignKeys[] = $constraint1; $changes++; } foreach ($toFkeys as $constraint2) { $tableDifferences->addedForeignKeys[] = $constraint2; $changes++; } return $changes ? $tableDifferences : false; }