/** * SQL to create indexes * @param epClassMap $cm * @return string */ public function createIndex($cm, $db, $curIndex = array()) { // reset return array (sql stmts to drop and create indexes) $sqls = array('drop' => array(), 'create' => array()); foreach ($cm->getIndexKeys() as $name => $key) { $indexname = $this->_indexName($name, $cm->getTable()); if (in_array($indexname, array_keys($curIndex))) { // check to see if the columns are the same if (count($key) == count($curIndex[$indexname]) && count(array_diff($key, $curIndex[$indexname]) == 0)) { unset($curIndex[$indexname]); continue; } unset($curIndex[$indexname]); // otherwise, we need to drop the index $sql = $this->dropIndex($indexname, $cm->getTable(), $db); $sqls['drop'][] = $sql; } // quote keys foreach ($key as $k => $v) { $key[$k] = $db->quoteId($v); } // make CREATE INDEX stmt $sql = "CREATE INDEX "; $sql .= $db->quoteId($indexname); $sql .= " ON "; $sql .= $db->quoteId($cm->getTable()) . " (" . join(', ', $key) . ");"; // collect stmt $sqls['create'][] = $sql; } // drop all the old ones that weren't messed with above foreach ($curIndex as $name => $key) { // otherwise, we need to drop the index $sql = $this->dropIndex($name, $cm->getTable(), $db); $sqls['drop'][] = $sql; } return $sqls; }