io.ebeaninternal.dbmigration.ddlgeneration.platform.WriteUniqueConstraint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ebean-ddlgen Show documentation
Show all versions of ebean-ddlgen Show documentation
DDL and DB Migration generation
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebeaninternal.dbmigration.migration.Column;
import java.util.ArrayList;
import java.util.List;
class WriteUniqueConstraint {
private final List primaryKeys = new ArrayList<>();
private final List uniqueKeys = new ArrayList<>();
WriteUniqueConstraint(List columns) {
// filter for unique and primary keys
for (Column column : columns) {
if (Boolean.TRUE.equals(column.isPrimaryKey())) {
primaryKeys.add(column);
}
if (hasValue(column.getUnique()) || hasValue(column.getUniqueOneToOne())){
uniqueKeys.add(column);
}
}
}
/**
* Return true if null or trimmed string is empty.
*/
boolean hasValue(String value) {
return value != null && !value.trim().isEmpty();
}
/**
* Return the single columns with unique constraints (that are not the primary key).
*/
public List uniqueKeys() {
if (uniqueKeys.isEmpty() || primaryKeys.size() > 1) {
// all single column unique constraints are valid
return uniqueKeys;
}
// filter out PFK
for (Column primaryKey : primaryKeys) {
uniqueKeys.remove(primaryKey);
}
return uniqueKeys;
}
}