All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ebeaninternal.dbmigration.ddlgeneration.platform.WriteUniqueConstraint Maven / Gradle / Ivy

The newest version!
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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy