/**
  * {@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);
 }
Exemplo n.º 2
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']);
 }
Exemplo n.º 3
0
 /**
  * @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;
 }
 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;
 }
Exemplo n.º 6
0
 /**
  * Return the INDEX query section dealing with non-standard
  * SQL Anywhere options.
  *
  * @param Index $index Index definition
  *
  * @return string
  */
 protected function getAdvancedIndexOptionsSQL(Index $index)
 {
     $sql = '';
     if (!$index->isPrimary() && $index->hasFlag('for_olap_workload')) {
         $sql .= ' FOR OLAP WORKLOAD';
     }
     return $sql;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
0
 /**
  * Gets the SQL to create an index on a table on this platform.
  *
  * @param Index $index
  * @param string|Table $table name of the table on which the index is to be created
  * @return string
  */
 public function getCreateIndexSQL(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.");
     }
     if ($index->isPrimary()) {
         return $this->getCreatePrimaryKeySQL($index, $table);
     } else {
         $type = '';
         if ($index->isUnique()) {
             $type = 'UNIQUE ';
         }
         $query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
         $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
     }
     return $query;
 }
Exemplo n.º 10
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);
 }
Exemplo n.º 11
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;
 }
Exemplo 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');
     }
 }
Exemplo 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());
 }
Exemplo 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());
 }