/**
  * @param string $table
  * @param \Doctrine\DBAL\Schema\Index $index
  * @return array
  */
 protected function indexToArray($table, $index)
 {
     if ($index->isPrimary()) {
         $type = 'primary';
     } elseif ($index->isUnique()) {
         $type = 'unique';
     } else {
         $type = 'index';
     }
     $array = ['type' => $type, 'name' => null, 'columns' => $index->getColumns()];
     if (!$this->isDefaultIndexName($table, $index->getName(), $type, $index->getColumns())) {
         $array['name'] = $index->getName();
     }
     return $array;
 }
 /**
  * @param string $table
  * @param \Doctrine\DBAL\Schema\Index $index
  * @return array
  */
 protected function indexToArray($table, $index)
 {
     if ($index->isPrimary()) {
         $type = 'primary';
     } elseif ($index->isUnique()) {
         $type = 'unique';
     } else {
         $type = 'index';
     }
     $array = ['type' => $type, 'name' => null, 'columns' => $index->getColumns()];
     if (!$this->ignoreIndexNames and !$this->isDefaultIndexName($table, $index->getName(), $type, $index->getColumns())) {
         // Sent Index name to exclude spaces
         $array['name'] = str_replace(' ', '', $index->getName());
     }
     return $array;
 }
Esempio n. 3
0
 /**
  * Populates a primary key based on a index.
  *
  * @param   Attrs  $attrs
  * @param   Index  $index
  */
 private function populatePrimaryKey($attrs, $index)
 {
     if (!$index->isPrimary()) {
         return;
     }
     $columns = $index->getColumns();
     $key = current($columns);
     // We don't support composite primary keys quite yet.
     if (count($columns) > 1) {
         return;
     }
     $attrs->set($this->deriveName($key), ['key' => $key, 'type' => 'primary']);
 }
Esempio n. 4
0
 /**
  * Generates the sql to create a spatial index.
  * 
  * @param SpatialIndex $index
  * @param Table | string $table
  * @return string The sql to create a spatial index on the database.
  * @throws \InvalidArgumentException
  */
 public function getCreateSpatialIndexSQL(Index $index, $table)
 {
     if ($table instanceof Table) {
         $table = $table->getQuotedName($this);
     }
     $name = $index->getQuotedName($this);
     $columns = $index->getColumns();
     if (count($columns) == 0) {
         throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
     }
     $query = 'CREATE INDEX ' . $name . ' ON ' . $table;
     $query .= ' USING GIST (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
     return $query;
 }
Esempio n. 5
0
 /**
  * Detect if the other index is a non-unique, non primary index that can be overwritten by this one.
  *
  * @param Index $other
  * @return bool
  */
 public function overrules(Index $other)
 {
     if ($other->isPrimary()) {
         return false;
     } else {
         if ($this->isSimpleIndex() && $other->isUnique()) {
             return false;
         }
     }
     if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
         return true;
     }
     return false;
 }
Esempio n. 6
0
 /**
  * Obtain DBMS specific SQL code portion needed to set an index
  * declaration to be used in statements like CREATE TABLE.
  *
  * @param string $name          name of the index
  * @param Index $index          index definition
  * @return string               DBMS specific SQL code portion needed to set an index
  */
 public function getIndexDeclarationSQL($name, Index $index)
 {
     $type = '';
     if ($index->isUnique()) {
         $type = 'UNIQUE ';
     }
     if (count($index->getColumns()) == 0) {
         throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
     }
     return $type . 'INDEX ' . $name . ' (' . $this->getIndexFieldDeclarationListSQL($index->getColumns()) . ')';
 }
Esempio n. 7
0
 /**
  * Extend unique key constraint with required filters
  *
  * @param string $sql
  * @param Index $index
  * @return string
  */
 private function _appendUniqueConstraintDefinition($sql, Index $index)
 {
     $fields = array();
     foreach ($index->getColumns() as $field => $definition) {
         if (!is_array($definition)) {
             $field = $definition;
         }
         $fields[] = $field . ' IS NOT NULL';
     }
     return $sql . ' WHERE ' . implode(' AND ', $fields);
 }
Esempio n. 8
0
 /**
  * @param TableDiff $diff
  * @param Index     $index
  *
  * @return string[]
  */
 private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index)
 {
     $sql = array();
     if (!$index->isPrimary() || !$diff->fromTable instanceof Table) {
         return $sql;
     }
     $tableName = $diff->getName($this)->getQuotedName($this);
     // Dropping primary keys requires to unset autoincrement attribute on the particular column first.
     foreach ($index->getColumns() as $columnName) {
         $column = $diff->fromTable->getColumn($columnName);
         if ($column->getAutoincrement() === true) {
             $column->setAutoincrement(false);
             $sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
             // original autoincrement information might be needed later on by other parts of the table alteration
             $column->setAutoincrement(true);
         }
     }
     return $sql;
 }
Esempio n. 9
0
 /**
  * @param Table     $table
  * @param Index     $index
  * @param Migration $migration
  *
  * @throws InvalidNameException
  */
 protected function checkIndex(Table $table, Index $index, Migration $migration)
 {
     $columns = $index->getColumns();
     foreach ($columns as $columnName) {
         if ($table->getColumn($columnName)->getLength() > MySqlPlatform::LENGTH_LIMIT_TINYTEXT) {
             throw new InvalidNameException(sprintf('Could not create index for column with length more than %s. ' . 'Please correct "%s" column length "%s" in table in "%s" migration', MySqlPlatform::LENGTH_LIMIT_TINYTEXT, $columnName, $table->getName(), get_class($migration)));
         }
     }
 }
Esempio n. 10
0
 /**
  * Checks whether this foreign key constraint intersects the given index columns.
  *
  * Returns `true` if at least one of this foreign key's local columns
  * matches one of the given index's columns, `false` otherwise.
  *
  * @param Index $index The index to be checked against.
  *
  * @return boolean
  */
 public function intersectsIndexColumns(Index $index)
 {
     foreach ($index->getColumns() as $indexColumn) {
         foreach ($this->_localColumnNames as $localColumn) {
             if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
                 return true;
             }
         }
     }
     return false;
 }
Esempio n. 11
0
 /**
  * Obtain DBMS specific SQL code portion needed to set an index
  * declaration to be used in statements like CREATE TABLE.
  *
  * @param string $name          name of the index
  * @param Index $index          index definition
  *
  * @return string               DBMS specific SQL code portion needed to set an index
  */
 public function getIndexDeclarationSQL($name, Index $index)
 {
     if (count($index->getColumns()) === 0) {
         throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
     }
     return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' (' . $this->getIndexFieldDeclarationListSQL($index->getColumns()) . ')';
 }
Esempio n. 12
0
 /**
  * @param Index $index
  * @param \SimpleXMLElement $xml
  */
 private static function saveIndex($index, $xml)
 {
     $xml->addChild('name', $index->getName());
     if ($index->isPrimary()) {
         $xml->addChild('primary', 'true');
     } elseif ($index->isUnique()) {
         $xml->addChild('unique', 'true');
     }
     foreach ($index->getColumns() as $column) {
         $field = $xml->addChild('field');
         $field->addChild('name', $column);
         $field->addChild('sorting', 'ascending');
     }
 }
Esempio n. 13
0
 /**
  * Creates a index replacement, which has quoted names.
  *
  * @param Index $index
  *
  * @return Index
  */
 private function createIndexReplacement(Index $index)
 {
     return new Index($this->platform->quoteIdentifier($index->getName()), $this->quoteIdentifiers($index->getColumns()), $index->isUnique(), $index->isPrimary(), $index->getFlags(), $index->getOptions());
 }
Esempio n. 14
0
 /**
  * Accept an index on in a table
  *
  * @param Table $table a table object
  * @param Index $index a column object
  * 
  * @return void
  */
 public function acceptIndex(Table $table, Index $index)
 {
     $this->schemaArray['tables'][$table->getName()]['indexes'][$index->getName()] = array('name' => $index->getName(), 'columns' => $index->getColumns(), 'unique' => $index->isUnique(), 'primary' => $index->isPrimary());
 }