/** * @dataProvider provideColumnDefinitions * */ public function testAddIndexedColumns($columns) { $index = new Index(); $index->setColumns($columns); $this->assertTrue($index->hasColumns()); $this->assertCount(3, $index->getColumns()); $this->assertSame(100, $index->getColumnSize('foo')); $this->assertTrue($index->hasColumnSize('foo')); $this->assertSame(5, $index->getColumnSize('bar')); $this->assertTrue($index->hasColumnSize('bar')); $this->assertNull($index->getColumnSize('baz')); }
/** * Computes the difference between two index objects. * * @param Index $fromIndex * @param Index $toIndex * @param boolean $caseInsensitive * @return boolean */ public static function computeDiff(Index $fromIndex, Index $toIndex, $caseInsensitive = false) { // Check for removed index columns in $toIndex $fromIndexColumns = $fromIndex->getColumns(); $max = count($fromIndexColumns); for ($i = 0; $i < $max; $i++) { $indexColumn = $fromIndexColumns[$i]; if (!$toIndex->hasColumnAtPosition($i, $indexColumn, $fromIndex->getColumnSize($indexColumn), $caseInsensitive)) { return true; } } // Check for new index columns in $toIndex $toIndexColumns = $toIndex->getColumns(); $max = count($toIndexColumns); for ($i = 0; $i < $max; $i++) { $indexColumn = $toIndexColumns[$i]; if (!$fromIndex->hasColumnAtPosition($i, $indexColumn, $toIndex->getColumnSize($indexColumn), $caseInsensitive)) { return true; } } // Check for difference in unicity return $fromIndex->isUnique() !== $toIndex->isUnique(); }
/** * Creates a comma-separated list of column names for the index. * For MySQL unique indexes there is the option of specifying size, so we cannot simply use * the getColumnsList() method. * @param Index $index * @return string */ protected function getIndexColumnListDDL(Index $index) { $list = array(); foreach ($index->getColumns() as $col) { $list[] = $this->quoteIdentifier($col) . ($index->hasColumnSize($col) ? '(' . $index->getColumnSize($col) . ')' : ''); } return implode(', ', $list); }
/** * Builds the DDL SQL to add an Index. * * @param Index $index * @return string */ public function getAddIndexDDL(Index $index) { // don't create index form primary key if ($this->getPrimaryKeyName($index->getTable()) == $this->quoteIdentifier($index->getName())) { return ""; } $pattern = "\nCREATE %sINDEX %s ON %s (%s)%s;\n"; return sprintf($pattern, $index->getIsUnique() ? 'UNIQUE ' : '', $this->quoteIdentifier($index->getName()), $this->quoteIdentifier($index->getTable()->getName()), $this->getColumnListDDL($index->getColumns()), $this->generateBlockStorage($index)); }
/** * Builds the DDL SQL for an Index object. * * @param Index $index * @return string */ public function getIndexDDL(Index $index) { return sprintf('%sINDEX %s (%s)', $index->getIsUnique() ? 'UNIQUE ' : '', $this->quoteIdentifier($index->getName()), $this->getColumnListDDL($index->getColumns())); }
/** * Adds a new index to the indices list and set the * parent table of the column to the current table. * * @param Index|array $index * @return Index * * @throw InvalidArgumentException */ public function addIndex($index) { if ($index instanceof Index) { if ($this->hasIndex($index->getName())) { throw new InvalidArgumentException(sprintf('Index "%s" already exist.', $index->getName())); } if (!$index->getColumns()) { throw new InvalidArgumentException(sprintf('Index "%s" has no columns.', $index->getName())); } $index->setTable($this); // force the name to be created if empty. $this->indices[] = $index; return $index; } $idx = new Index(); $idx->loadMapping($index); foreach ((array) @$index['columns'] as $column) { $idx->addColumn($column); } return $this->addIndex($idx); }
/** * Appends a generice <index> or <unique> XML node to its parent node. * * @param string $nodeType The node type (index or unique) * @param Index $index The Index model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendGenericIndexNode($nodeType, Index $index, \DOMNode $parentNode) { $indexNode = $parentNode->appendChild($this->document->createElement($nodeType)); $indexNode->setAttribute('name', $index->getName()); foreach ($index->getColumns() as $columnName) { $indexColumnNode = $indexNode->appendChild($this->document->createElement($nodeType . '-column')); $indexColumnNode->setAttribute('name', $columnName); if ($size = $index->getColumnSize($columnName)) { $indexColumnNode->setAttribute('size', $size); } } foreach ($index->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $indexNode); } }