From: stronk7 Date: Sun, 24 Sep 2006 08:26:24 +0000 (+0000) Subject: Given one xmldb_field, finds its name in DB X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=70a8ac6666729101a0012bbfbdb0d3bfff8d0e91;p=moodle.git Given one xmldb_field, finds its name in DB --- diff --git a/lib/ddllib.php b/lib/ddllib.php index 345952bf81..bd3ed162fd 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -239,6 +239,45 @@ function table_column($table, $oldfield, $field, $type='integer', $size='10', } } +/** + * Given one XMLDBIndex, the function returns the name of the index in DB (if exists) + * of false if it doesn't exist + * + * @uses, $db + * @param XMLDBTable the table to be searched + * @param XMLDBIndex the index to be searched + * @return string index name of false + */ +function find_index_name($xmldb_table, $xmldb_index) { + + global $CFG, $db; + +/// Extract index columns + $indcolumns = $xmldb_index->getFields(); + +/// Get list of indexes in table + $indexes = array_change_key_case($db->MetaIndexes($CFG->prefix . $xmldb_table->getName(), CASE_LOWER)); + +/// Iterate over them looking for columns coincidence + if ($indexes) { + foreach ($indexes as $indexname => $index) { + $columns = $index['columns']; + /// Lower case column names + $columns = array_flip($columns); + $columns = array_change_key_case($columns, CASE_LOWER); + $columns = array_flip($columns); + /// Check if index matchs queried index + $diferences = array_merge(array_diff($columns, $indcolumns), array_diff($indcolumns, $columns)); + /// If no diferences, we have find the index + if (empty($diferences)) { + return $indexname; + } + } + } +/// Arriving here, index not found + return false; +} + /** * This function will load one entire XMLDB file, generating all the needed * SQL statements, specific for each RDBMS ($CFG->dbtype) and, finally, it