}
// look for an existing index on these columns
Table table = cols[0].getTable();
Index[] idxs = table.getIndexes();
Index exist = null;
for (int i = 0; i < idxs.length; i++) {
if (idxs[i].columnsMatch(cols)) {
exist = idxs[i];
break;
}
}
// remove existing index?
if (!_canIdx) {
if (exist == null)
return null;
if (!adapt)
throw new MetaDataException(_loc.get(prefix + "-index-exists",
context));
table.removeIndex(exist);
return null;
}
// if we have an existing index, merge given info into it
if (exist != null) {
if (_idx != null && _idx.isUnique() && !exist.isUnique()) {
if (!adapt)
throw new MetaDataException(_loc.get(prefix
+ "-index-not-unique", context));
exist.setUnique(true);
}
return exist;
}
// if no defaults return null
MappingRepository repos = (MappingRepository) context.getRepository();
boolean fill = repos.getMappingDefaults().defaultMissingInfo();
if (_idx == null && (tmplate == null || (!adapt && !fill)))
return null;
String name = null;
boolean unq;
if (_idx != null) {
name = _idx.getName();
unq = _idx.isUnique();
} else
unq = tmplate.isUnique();
// if no name provided by user info, make one
if (name == null) {
if (tmplate != null)
name = tmplate.getName();
else {
name = cols[0].getName();
name = repos.getDBDictionary().getValidIndexName(name, table);
}
}
Index idx = table.addIndex(name);
idx.setUnique(unq);
idx.setColumns(cols);
return idx;
}