/** * @param array $columnNames * @param string|null $indexName * @param array $flags * @param array $options * * @return self */ public function addIndex(array $columnNames, $indexName = null, array $flags = array(), array $options = array()) { if ($indexName) { $indexName = $this->connection->replacePrefix($indexName); } $this->table->addIndex($columnNames, $indexName, $flags, $options); return $this; }
/** * Load indexes for this table */ protected function addIndexes(Table $table, $oid, $version) { $stmt = $this->dbh->prepare("SELECT\n\t\t\t\t\t\t\t\t\t\tDISTINCT ON(cls.relname)\n\t\t\t\t\t\t\t\t\t\tcls.relname as idxname,\n\t\t\t\t\t\t\t\t indkey,\n\t\t\t\t\t\t\t\t indisunique\n\t\t\t\t\t\t\t\t FROM pg_index idx\n\t\t\t\t\t\t\t\t JOIN pg_class cls ON cls.oid=indexrelid\n\t\t\t\t\t\t\t\t WHERE indrelid = ? AND NOT indisprimary\n\t\t\t\t\t\t\t\t ORDER BY cls.relname"); $stmt->bindValue(1, $oid); $stmt->execute(); $stmt2 = $this->dbh->prepare("SELECT a.attname\n\t\t\t\t\t\t\t\t\t\tFROM pg_catalog.pg_class c JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid\n\t\t\t\t\t\t\t\t\t\tWHERE c.oid = ? AND a.attnum = ? AND NOT a.attisdropped\n\t\t\t\t\t\t\t\t\t\tORDER BY a.attnum"); $indexes = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $name = $row["idxname"]; $unique = $row["indisunique"] == 't' ? true : false; if (!isset($indexes[$name])) { if ($unique) { $indexes[$name] = new Unique($name); } else { $indexes[$name] = new Index($name); } $table->addIndex($indexes[$name]); } $arrColumns = explode(' ', $row['indkey']); foreach ($arrColumns as $intColNum) { $stmt2->bindValue(1, $oid); $stmt2->bindValue(2, $intColNum); $stmt2->execute(); $row2 = $stmt2->fetch(PDO::FETCH_ASSOC); $indexes[$name]->addColumn($table->getColumn($row2['attname'])); } // foreach ($arrColumns as $intColNum) } }
/** * Load indexes for this table */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("SHOW INDEX FROM `" . $table->getName() . "`"); // Loop through the returned results, grouping the same key_name together // adding each column for that key. $indexes = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $colName = $row["Column_name"]; $name = $row["Key_name"]; if ($name == "PRIMARY") { continue; } if (!isset($indexes[$name])) { $isUnique = $row["Non_unique"] == 0; if ($isUnique) { $indexes[$name] = new Unique($name); } else { $indexes[$name] = new Index($name); } if ($this->addVendorInfo) { $vi = $this->getNewVendorInfoObject($row); $indexes[$name]->addVendorInfo($vi); } $table->addIndex($indexes[$name]); } $indexes[$name]->addColumn($table->getColumn($colName)); } }
/** * Adds Indexes to the specified table. * * @param Table $table The Table model class to add columns to. */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("SELECT COLUMN_NAME, INDEX_NAME FROM USER_IND_COLUMNS WHERE TABLE_NAME = '" . $table->getName() . "' ORDER BY COLUMN_NAME"); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $indices = array(); foreach ($rows as $row) { $indices[$row['INDEX_NAME']][] = $row['COLUMN_NAME']; } foreach ($indices as $indexName => $columnNames) { $index = new Index($indexName); foreach ($columnNames as $columnName) { // Oracle deals with complex indices using an internal reference, so... // let's ignore this kind of index if ($table->hasColumn($columnName)) { $index->addColumn($table->getColumn($columnName)); } } // since some of the columns are pruned above, we must only add an index if it has columns if ($index->hasColumns()) { $table->addIndex($index); } } }
public function testGetIndexDDLFulltext() { $table = new Table('foo'); $column1 = new Column('bar1'); $column1->getDomain()->copy($this->getPlatform()->getDomainForType('LONGVARCHAR')); $table->addColumn($column1); $index = new Index('bar_index'); $index->addColumn($column1); $vendor = new VendorInfo('mysql'); $vendor->setParameter('Index_type', 'FULLTEXT'); $index->addVendorInfo($vendor); $table->addIndex($index); $expected = 'FULLTEXT INDEX `bar_index` (`bar1`)'; $this->assertEquals($expected, $this->getPLatform()->getIndexDDL($index)); }
/** * Load indexes for this table */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("sp_indexes_rowset '" . $table->getName() . "'"); $indexes = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $colName = $row["COLUMN_NAME"]; $name = $row['INDEX_NAME']; // FIXME -- Add UNIQUE support if (!isset($indexes[$name])) { $indexes[$name] = new Index($name); $table->addIndex($indexes[$name]); } $indexes[$name]->addColumn($table->getColumn($colName)); } }
public function testCompareModifiedIndices() { $t1 = new Table(); $c1 = new Column('Foo'); $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR')); $c1->getDomain()->replaceSize(255); $c1->setNotNull(false); $t1->addColumn($c1); $i1 = new Index('Foo_Index'); $i1->addColumn($c1); $t1->addIndex($i1); $t2 = new Table(); $c2 = new Column('Foo'); $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE')); $c2->getDomain()->replaceScale(2); $c2->getDomain()->replaceSize(3); $c2->setNotNull(true); $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE)); $t2->addColumn($c2); $i2 = new Unique('Foo_Index'); $i2->addColumn($c2); $t2->addIndex($i2); $tc = new PropelTableComparator(); $tc->setFromTable($t1); $tc->setToTable($t2); $nbDiffs = $tc->compareIndices(); $tableDiff = $tc->getTableDiff(); $this->assertEquals(1, $nbDiffs); $this->assertEquals(1, count($tableDiff->getModifiedIndices())); $this->assertEquals(array('Foo_Index' => array($i1, $i2)), $tableDiff->getModifiedIndices()); }
/** * Load indexes for this table */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("PRAGMA index_list('" . $table->getName() . "')"); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $name = $row['name']; $index = new Index($name); $stmt2 = $this->dbh->query("PRAGMA index_info('" . $name . "')"); while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) { $colname = $row2['name']; $index->addColumn($table->getColumn($colname)); } $table->addIndex($index); } }
/** * Adds Indexes to the specified table. * * @param Table $table The Table model class to add columns to. */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("SELECT COLUMN_NAME, INDEX_NAME FROM USER_IND_COLUMNS WHERE TABLE_NAME = '" . $table->getName() . "' ORDER BY COLUMN_NAME"); /* @var stmt PDOStatement */ $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if (count($rows) > 0) { $index = new Index($rows[0]['INDEX_NAME']); foreach($rows AS $row) { $index->addColumn($row['COLUMN_NAME']); } $table->addIndex($index); } }
public function providerForTestGetIndexDDL() { $table = new Table('foo'); $column1 = new Column('bar1'); $column1->getDomain()->copy(new Domain('FOOTYPE')); $table->addColumn($column1); $column2 = new Column('bar2'); $column2->getDomain()->copy(new Domain('BARTYPE')); $table->addColumn($column2); $index = new Index('babar'); $index->addColumn($column1); $index->addColumn($column2); $table->addIndex($index); return array(array($index)); }
/** * {@inheritDoc} */ public function convertIndexDescription(Table $table, $row) { $type = Table::INDEX_INDEX; $name = $row['index_name']; if ($row['is_primary_key']) { $name = $type = Table::CONSTRAINT_PRIMARY; } if ($row['is_unique_constraint'] && $type === Table::INDEX_INDEX) { $type = Table::CONSTRAINT_UNIQUE; } if ($type === Table::INDEX_INDEX) { $existing = $table->index($name); } else { $existing = $table->constraint($name); } $columns = [$row['column_name']]; if (!empty($existing)) { $columns = array_merge($existing['columns'], $columns); } if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) { $table->addConstraint($name, ['type' => $type, 'columns' => $columns]); return; } $table->addIndex($name, ['type' => $type, 'columns' => $columns]); }
/** * Load indexes for this table */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("SELECT INDEX_NAME, COLUMN_NAME FROM USER_IND_COLUMNS WHERE TABLE_NAME = '" . $table->getName() . "'"); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $name = $row['INDEX_NAME']; $index = new Index($name); $colname = $row['COLUMN_NAME']; $index->addColumn($table->getColumn($colname)); $table->addIndex($index); } }