/** * Given one xmldb_index, the function returns the name of the index in DB * of false if it doesn't exist * * @param xmldb_table $xmldb_table table to be searched * @param xmldb_index $xmldb_index the index to be searched * @return string|bool Index name or false if no indexes are found. * @throws ddl_table_missing_exception Thrown when table is not found. */ public function find_index_name(xmldb_table $xmldb_table, xmldb_index $xmldb_index) { // Calculate the name of the table $tablename = $xmldb_table->getName(); // Check the table exists if (!$this->table_exists($xmldb_table)) { throw new ddl_table_missing_exception($tablename); } // Extract index columns $indcolumns = $xmldb_index->getFields(); // Get list of indexes in table $indexes = $this->mdb->get_indexes($tablename); // Iterate over them looking for columns coincidence foreach ($indexes as $indexname => $index) { $columns = $index['columns']; // Check if index matches queried index $diferences = array_merge(array_diff($columns, $indcolumns), array_diff($indcolumns, $columns)); // If no differences, we have find the index if (empty($diferences)) { return $indexname; } } // Arriving here, index not found return false; }
/** * Given one correct xmldb_index, returns the SQL statements * needed to create it (in array). * * @param xmldb_table $xmldb_table The xmldb_table instance to create the index on. * @param xmldb_index $xmldb_index The xmldb_index to create. * @return array An array of SQL statements to create the index. * @throws coding_exception Thrown if the xmldb_index does not validate with the xmldb_table. */ public function getCreateIndexSQL($xmldb_table, $xmldb_index) { if ($error = $xmldb_index->validateDefinition($xmldb_table)) { throw new coding_exception($error); } $unique = ''; $suffix = 'ix'; if ($xmldb_index->getUnique()) { $unique = ' UNIQUE'; $suffix = 'uix'; } $index = 'CREATE' . $unique . ' INDEX '; $index .= $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_index->getFields()), $suffix); $index .= ' ON ' . $this->getTableName($xmldb_table); $index .= ' (' . implode(', ', $this->getEncQuoted($xmldb_index->getFields())) . ')'; return array($index); }
/** * Add one index to the table, allowing to specify the desired order * If it's not specified, then the index is added at the end * @param xmldb_index $index * @param xmldb_object $after */ public function addIndex($index, $after = null) { // Detect duplicates first if ($this->getIndex($index->getName())) { throw new coding_exception('Duplicate index ' . $index->getName() . ' specified in table ' . $this->getName()); } // Make sure there are no keys with the index column specs because they would collide. $newfields = $index->getFields(); $allkeys = $this->getKeys(); foreach ($allkeys as $key) { $fields = $key->getFields(); if ($fields === $newfields) { throw new coding_exception('Key ' . $key->getName() . ' collides with index' . $index->getName() . ' specified in table ' . $this->getName()); } } // Calculate the previous and next indexes $previndex = null; $nextindex = null; if (!$after) { $allindexes = $this->getIndexes(); if (!empty($allindexes)) { end($allindexes); $previndex = $allindexes[key($allindexes)]; } } else { $previndex = $this->getIndex($after); } if ($previndex && $previndex->getNext()) { $nextindex = $this->getIndex($previndex->getNext()); } // Set current index previous and next attributes if ($previndex) { $index->setPrevious($previndex->getName()); $previndex->setNext($index->getName()); } if ($nextindex) { $index->setNext($nextindex->getName()); $nextindex->setPrevious($index->getName()); } // Some more attributes $index->setLoaded(true); $index->setChanged(true); // Add the new index $this->indexes[] = $index; // Reorder the indexes $this->orderIndexes($this->indexes); // Recalculate the hash $this->calculateHash(true); // We have one new index, so the table has changed $this->setChanged(true); }