+ "-no-unique-cols", context));
return null;
}
// look for an existing constraint on these columns
Table table = cols[0].getTable();
Unique[] unqs = table.getUniques();
Unique exist = null;
for (int i = 0; i < unqs.length; i++) {
if (unqs[i].columnsMatch(cols)) {
exist = unqs[i];
break;
}
}
// remove existing unique?
if (!_canUnq) {
if (exist == null)
return null;
if (!adapt)
throw new MetaDataException(_loc.get(prefix
+ "-unique-exists", context));
table.removeUnique(exist);
return null;
}
// no defaults; return existing constraint (if any)
if (tmplate == null && _unq == null)
return exist;
MappingRepository repos = (MappingRepository) context.getRepository();
if (exist != null) {
if (_unq != null && _unq.isDeferred() && !exist.isDeferred()) {
Log log = repos.getLog();
if (log.isWarnEnabled())
log.warn(_loc.get(prefix + "-defer-unique", context));
}
return exist;
}
// dict can't handle unique constraints?
DBDictionary dict = repos.getDBDictionary();
if (_unq != null && !dict.supportsUniqueConstraints) {
Log log = repos.getLog();
if (log.isWarnEnabled())
log.warn(_loc.get(prefix + "-unique-support", context));
return null;
}
boolean fill = repos.getMappingDefaults().defaultMissingInfo();
if (!adapt && !fill && _unq == null)
return null;
String name;
boolean deferred;
if (_unq != null) {
name = _unq.getName();
deferred = _unq.isDeferred();
} else {
name = tmplate.getName();
deferred = tmplate.isDeferred();
}
if (deferred && !dict.supportsDeferredConstraints) {
Log log = repos.getLog();
if (log.isWarnEnabled())
log.warn(_loc.get(prefix + "-create-defer-unique",
context, dict.platform));
deferred = false;
}
Unique unq = table.addUnique(name);
unq.setDeferred(deferred);
unq.setColumns(cols);
return unq;
}