Example #1
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']);
 }
Example #2
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;
 }
 public function getSql(Index $index, $table)
 {
     if ($table instanceof Table) {
         $table = $table->getQuotedName($this->platform);
     }
     $name = $index->getQuotedName($this->platform);
     $columns = $index->getQuotedColumns($this->platform);
     if (count($columns) == 0) {
         throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
     }
     if ($index->isPrimary()) {
         return $this->platform->getCreatePrimaryKeySQL($index, $table);
     }
     $query = 'CREATE INDEX ' . $name . ' ON ' . $table;
     $query .= ' USING gist(' . $this->platform->getIndexFieldDeclarationListSQL($columns) . ')';
     return $query;
 }
 /**
  * {@inheritdoc}
  */
 protected function getAdvancedIndexOptionsSQL(Index $index)
 {
     if ($index->hasFlag('with_nulls_distinct') && $index->hasFlag('with_nulls_not_distinct')) {
         throw new UnexpectedValueException('An Index can either have a "with_nulls_distinct" or "with_nulls_not_distinct" flag but not both.');
     }
     if (!$index->isPrimary() && $index->isUnique() && $index->hasFlag('with_nulls_distinct')) {
         return ' WITH NULLS DISTINCT' . parent::getAdvancedIndexOptionsSQL($index);
     }
     return parent::getAdvancedIndexOptionsSQL($index);
 }
 /**
  * @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;
 }
Example #7
0
 /**
  * Obtains DBMS specific SQL code portion needed to set an index
  * declaration to be used in statements like CREATE TABLE.
  *
  * @param string                       $name  The name of the index.
  * @param \Doctrine\DBAL\Schema\Index  $index The index definition.
  *
  * @return string DBMS specific SQL code portion needed to set an index.
  *
  * @throws \InvalidArgumentException
  */
 public function getIndexDeclarationSQL($name, Index $index)
 {
     $columns = $index->getQuotedColumns($this);
     if (count($columns) === 0) {
         throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
     }
     return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')' . $this->getPartialIndexSQL($index);
 }
 /**
  * Extend unique key constraint with required filters
  *
  * @param string                      $sql
  * @param \Doctrine\DBAL\Schema\Index $index
  *
  * @return string
  */
 private function _appendUniqueConstraintDefinition($sql, Index $index)
 {
     $fields = array();
     foreach ($index->getQuotedColumns($this) as $field) {
         $fields[] = $field . ' IS NOT NULL';
     }
     return $sql . ' WHERE ' . implode(' AND ', $fields);
 }
Example #9
0
 /**
  * Do checks for indexes.
  *
  * @param Index         $index
  * @param IgnoredChange $ignoredChange
  *
  * @return boolean
  */
 protected function checkIndex(Index $index, IgnoredChange $ignoredChange)
 {
     // Not needed to be implemented yet
     if ($ignoredChange->getPropertyName() !== $index->getName()) {
         return false;
     }
     return false;
 }
 /**
  * Drops and creates a new index on a table.
  *
  * @param \Doctrine\DBAL\Schema\Index        $index
  * @param \Doctrine\DBAL\Schema\Table|string $table The name of the table on which the index is to be created.
  *
  * @return void
  */
 public function dropAndCreateIndex(Index $index, $table)
 {
     $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
     $this->createIndex($index, $table);
 }
 /**
  * {@inheritdoc}
  */
 protected function getCreateIndexSQLFlags(Index $index)
 {
     $type = '';
     if ($index->hasFlag('virtual')) {
         $type .= 'VIRTUAL ';
     }
     if ($index->isUnique()) {
         $type .= 'UNIQUE ';
     }
     if ($index->hasFlag('clustered')) {
         $type .= 'CLUSTERED ';
     }
     return $type;
 }
 /**
  * Adds an index to the table.
  *
  * @param Index $indexCandidate
  *
  * @return self
  *
  * @throws SchemaException
  */
 protected function _addIndex(Index $indexCandidate)
 {
     $indexName = $indexCandidate->getName();
     $indexName = $this->normalizeIdentifier($indexName);
     $replacedImplicitIndexes = array();
     foreach ($this->implicitIndexes as $name => $implicitIndex) {
         if ($implicitIndex->isFullfilledBy($indexCandidate) && isset($this->_indexes[$name])) {
             $replacedImplicitIndexes[] = $name;
         }
     }
     if (isset($this->_indexes[$indexName]) && !in_array($indexName, $replacedImplicitIndexes, true) || $this->_primaryKeyName != false && $indexCandidate->isPrimary()) {
         throw SchemaException::indexAlreadyExists($indexName, $this->_name);
     }
     foreach ($replacedImplicitIndexes as $name) {
         unset($this->_indexes[$name], $this->implicitIndexes[$name]);
     }
     if ($indexCandidate->isPrimary()) {
         $this->_primaryKeyName = $indexName;
     }
     $this->_indexes[$indexName] = $indexCandidate;
     return $this;
 }
Example #13
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');
     }
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
 {
     if (strpos($tableName, '.') !== false) {
         list($schema) = explode('.', $tableName);
         $oldIndexName = $schema . '.' . $oldIndexName;
     }
     return array('ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this));
 }
Example #15
0
 /**
  * {@inheritDoc}
  */
 protected function getCreateIndexSQLFlags(Index $index)
 {
     $type = '';
     if ($index->isUnique()) {
         $type .= 'UNIQUE ';
     } elseif ($index->hasFlag('fulltext')) {
         $type .= 'FULLTEXT ';
     } elseif ($index->hasFlag('spatial')) {
         $type .= 'SPATIAL ';
     }
     return $type;
 }
Example #16
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()) . ')';
 }
Example #17
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;
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 protected function getAdvancedIndexOptionsSQL(Index $index)
 {
     if (!$index->isPrimary() && $index->isUnique() && $index->hasFlag('with_nulls_not_distinct')) {
         return ' WITH NULLS NOT DISTINCT' . parent::getAdvancedIndexOptionsSQL($index);
     }
     return parent::getAdvancedIndexOptionsSQL($index);
 }
Example #19
0
 public function getCreatePrimaryKeySQL(Index $index, $table)
 {
     if ($table instanceof Table) {
         $table = $table->getQuotedName($this);
     }
     return 'ALTER TABLE ' . $this->espoQuote($table) . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getQuotedColumns($this)) . ')';
 }
Example #20
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)));
         }
     }
 }
Example #21
0
 /**
  * Do checks for indexes.
  *
  * @param Index $index
  * @param array $alterData
  *
  * @return boolean
  */
 protected function checkIndex(Index $index, array $alterData)
 {
     // Not needed to be implemented yet
     if ($alterData['propertyName'] !== $index->getName()) {
         return false;
     }
     return false;
 }
Example #22
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;
 }
Example #23
0
 /**
  * {@inheritdoc}
  */
 protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
 {
     return array(sprintf("EXEC sp_RENAME N'%s.%s', N'%s', N'INDEX'", $tableName, $oldIndexName, $index->getQuotedName($this)));
 }
Example #24
0
 /**
  * Return whether the two indexes have the same partial index
  * @param \Doctrine\DBAL\Schema\Index $other
  *
  * @return boolean
  */
 private function samePartialIndex(Index $other)
 {
     if ($this->hasOption('where') && $other->hasOption('where') && $this->getOption('where') == $other->getOption('where')) {
         return true;
     }
     if (!$this->hasOption('where') && !$other->hasOption('where')) {
         return true;
     }
     return false;
 }
Example #25
0
 /**
  * Drop and create a new index on a table
  *
  * @param string|Table $table         name of the table on which the index is to be created
  * @param Index $index
  */
 public function dropAndCreateIndex(Index $index, $table)
 {
     $this->tryMethod('dropIndex', $index->getName(), $table);
     $this->createIndex($index, $table);
 }
Example #26
0
 /**
  * Finds the difference between the indexes $index1 and $index2.
  *
  * Compares $index1 with $index2 and returns $index2 if there are any
  * differences or false in case there are no differences.
  *
  * @param \Doctrine\DBAL\Schema\Index $index1
  * @param \Doctrine\DBAL\Schema\Index $index2
  *
  * @return boolean
  */
 public function diffIndex(Index $index1, Index $index2)
 {
     if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) {
         return false;
     }
     return true;
 }
Example #27
0
 /**
  * Add index to table
  * 
  * @param Index $indexCandidate
  * @return Table
  */
 protected function _addIndex(Index $indexCandidate)
 {
     // check for duplicates
     foreach ($this->_indexes as $existingIndex) {
         if ($indexCandidate->isFullfilledBy($existingIndex)) {
             return $this;
         }
     }
     $indexName = $indexCandidate->getName();
     $indexName = strtolower($indexName);
     if (isset($this->_indexes[$indexName]) || $this->_primaryKeyName != false && $indexCandidate->isPrimary()) {
         throw SchemaException::indexAlreadyExists($indexName, $this->_name);
     }
     // remove overruled indexes
     foreach ($this->_indexes as $idxKey => $existingIndex) {
         if ($indexCandidate->overrules($existingIndex)) {
             unset($this->_indexes[$idxKey]);
         }
     }
     if ($indexCandidate->isPrimary()) {
         $this->_primaryKeyName = $indexName;
     }
     $this->_indexes[$indexName] = $indexCandidate;
     return $this;
 }
Example #28
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);
 }
Example #29
0
 /**
  * {@inheritdoc}
  */
 protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
 {
     return array('ALTER INDEX ' . $oldIndexName . ' ON ' . $tableName . ' RENAME TO ' . $index->getQuotedName($this));
 }
Example #30
0
 /**
  * Gets the SQL to drop an index of a table.
  *
  * @param Index $index           name of the index to be dropped
  * @param string|Table $table          name of table that should be used in method
  * @override
  */
 public function getDropIndexSQL($index, $table = null)
 {
     if ($index instanceof Index) {
         $indexName = $index->getQuotedName($this);
     } else {
         if (is_string($index)) {
             $indexName = $index;
         } else {
             throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \\Doctrine\\DBAL\\Schema\\Index.');
         }
     }
     if ($table instanceof Table) {
         $table = $table->getQuotedName($this);
     } else {
         if (!is_string($table)) {
             throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \\Doctrine\\DBAL\\Schema\\Table.');
         }
     }
     if ($index instanceof Index && $index->isPrimary()) {
         // mysql primary keys are always named "PRIMARY",
         // so we cannot use them in statements because of them being keyword.
         return $this->getDropPrimaryKeySQL($table);
     }
     return 'DROP INDEX ' . $indexName . ' ON ' . $table;
 }