org.jooq.util.DefaultRelations Maven / Gradle / Ivy
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jooq.tools.JooqLogger;
public class DefaultRelations implements Relations {
private static final JooqLogger log = JooqLogger.getLogger(DefaultRelations.class);
private Map primaryKeys = new LinkedHashMap();
private Map uniqueKeys = new LinkedHashMap();
private Map foreignKeys = new LinkedHashMap();
private Map checkConstraints = new LinkedHashMap();
private transient Map primaryKeysByColumn;
private transient Map> uniqueKeysByColumn;
private transient Map> foreignKeysByColumn;
private transient Map> checkConstraintsByTable;
public void addPrimaryKey(String keyName, ColumnDefinition column) {
// [#2718] Column exclusions may hit primary key references. Ignore
// such primary keys
if (column == null) {
log.info("Ignoring primary key", keyName + "(column unavailable)");
return;
}
if (log.isDebugEnabled()) {
log.debug("Adding primary key", keyName + " (" + column + ")");
}
UniqueKeyDefinition key = getUniqueKey(keyName, column, true);
key.getKeyColumns().add(column);
}
public void addUniqueKey(String keyName, ColumnDefinition column) {
// [#2718] Column exclusions may hit unique key references. Ignore
// such unique keys
if (column == null) {
log.info("Ignoring unique key", keyName + "(column unavailable)");
return;
}
if (log.isDebugEnabled()) {
log.debug("Adding unique key", keyName + " (" + column + ")");
}
UniqueKeyDefinition key = getUniqueKey(keyName, column, false);
key.getKeyColumns().add(column);
}
private UniqueKeyDefinition getUniqueKey(String keyName, ColumnDefinition column, boolean isPK) {
UniqueKeyDefinition key = uniqueKeys.get(key(column, keyName));
if (key == null) {
key = new DefaultUniqueKeyDefinition(column.getSchema(), keyName, column.getContainer(), isPK);
uniqueKeys.put(key(column, keyName), key);
if (isPK) {
primaryKeys.put(key(column, keyName), key);
}
}
return key;
}
public void addForeignKey(
String foreignKeyName,
String uniqueKeyName,
ColumnDefinition foreignKeyColumn,
SchemaDefinition uniqueKeySchema) {
// [#2718] Column exclusions may hit foreign key references. Ignore
// such foreign keys
if (foreignKeyColumn == null) {
log.info("Ignoring foreign key", foreignKeyColumn + "(column unavailable)");
return;
}
// [#1134] Prevent NPE's when a foreign key references a unique key
// from another schema
if (uniqueKeySchema == null) {
log.warn("Ignoring foreign key", foreignKeyName + " (" + foreignKeyColumn + ") referencing " + uniqueKeyName + " references a schema out of scope for jooq-meta");
return;
}
log.info("Adding foreign key", foreignKeyName + " (" + foreignKeyColumn + ") referencing " + uniqueKeyName);
ForeignKeyDefinition foreignKey = foreignKeys.get(key(foreignKeyColumn, foreignKeyName));
if (foreignKey == null) {
UniqueKeyDefinition uniqueKey = uniqueKeys.get(key(uniqueKeySchema, uniqueKeyName));
// If the unique key is not loaded, ignore this foreign key
if (uniqueKey != null) {
foreignKey = new DefaultForeignKeyDefinition(foreignKeyColumn.getSchema(), foreignKeyName, foreignKeyColumn.getContainer(), uniqueKey);
foreignKeys.put(key(foreignKeyColumn, foreignKeyName), foreignKey);
uniqueKey.getForeignKeys().add(foreignKey);
}
}
if (foreignKey != null) {
foreignKey.getKeyColumns().add(foreignKeyColumn);
}
}
public void addCheckConstraint(TableDefinition table, CheckConstraintDefinition constraint) {
checkConstraints.put(key(table, constraint.getName()), constraint);
}
@Override
public UniqueKeyDefinition getPrimaryKey(ColumnDefinition column) {
if (primaryKeysByColumn == null) {
primaryKeysByColumn = new LinkedHashMap();
for (UniqueKeyDefinition primaryKey : primaryKeys.values()) {
for (ColumnDefinition keyColumn : primaryKey.getKeyColumns()) {
primaryKeysByColumn.put(keyColumn, primaryKey);
}
}
}
return primaryKeysByColumn.get(column);
}
@Override
public List getUniqueKeys(ColumnDefinition column) {
if (uniqueKeysByColumn == null) {
uniqueKeysByColumn = new LinkedHashMap>();
for (UniqueKeyDefinition uniqueKey : uniqueKeys.values()) {
for (ColumnDefinition keyColumn : uniqueKey.getKeyColumns()) {
List list = uniqueKeysByColumn.get(keyColumn);
if (list == null) {
list = new ArrayList();
uniqueKeysByColumn.put(keyColumn, list);
}
list.add(uniqueKey);
}
}
}
List list = uniqueKeysByColumn.get(column);
return list != null ? list : Collections.emptyList();
}
@Override
public List getUniqueKeys(TableDefinition table) {
Set result = new LinkedHashSet();
for (ColumnDefinition column : table.getColumns()) {
result.addAll(getUniqueKeys(column));
}
return new ArrayList(result);
}
@Override
public List getForeignKeys(ColumnDefinition column) {
if (foreignKeysByColumn == null) {
foreignKeysByColumn = new LinkedHashMap>();
for (ForeignKeyDefinition foreignKey : foreignKeys.values()) {
for (ColumnDefinition keyColumn : foreignKey.getKeyColumns()) {
List list = foreignKeysByColumn.get(keyColumn);
if (list == null) {
list = new ArrayList();
foreignKeysByColumn.put(keyColumn, list);
}
list.add(foreignKey);
}
}
}
List list = foreignKeysByColumn.get(column);
return list != null ? list : Collections.emptyList();
}
@Override
public List getForeignKeys(TableDefinition table) {
Set result = new LinkedHashSet();
for (ColumnDefinition column : table.getColumns()) {
result.addAll(getForeignKeys(column));
}
return new ArrayList(result);
}
@Override
public List getCheckConstraints(TableDefinition table) {
if (checkConstraintsByTable == null) {
checkConstraintsByTable = new LinkedHashMap>();
for (CheckConstraintDefinition constraint : checkConstraints.values()) {
List list = checkConstraintsByTable.get(table);
if (list == null) {
list = new ArrayList();
checkConstraintsByTable.put(table, list);
}
list.add(constraint);
}
}
List list = checkConstraintsByTable.get(table);
return list != null ? list : Collections.emptyList();
}
private static Key key(Definition definition, String keyName) {
return new Key(definition.getSchema(), keyName);
}
/**
* A simple local wrapper for a key definition (schema + key name)
*/
private static class Key {
final SchemaDefinition schema;
final String keyName;
Key(SchemaDefinition schema, String keyName) {
this.schema = schema;
this.keyName = keyName;
}
@Override
public String toString() {
return "Key [schema=" + schema + ", keyName=" + keyName + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
result = prime * result + ((schema == null) ? 0 : schema.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key other = (Key) obj;
if (keyName == null) {
if (other.keyName != null)
return false;
}
else if (!keyName.equals(other.keyName))
return false;
if (schema == null) {
if (other.schema != null)
return false;
}
else if (!schema.equals(other.schema))
return false;
return true;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy