[jOOQ/jOOQ#9435] Let Key extend Named

This commit is contained in:
Lukas Eder 2019-10-23 15:48:45 +02:00
parent 69011c2272
commit e4c8dc6aeb
5 changed files with 29 additions and 50 deletions

View File

@ -37,7 +37,6 @@
*/
package org.jooq;
import java.io.Serializable;
import java.util.List;
@ -51,12 +50,7 @@ import java.util.List;
* @param <R> The <code>KEY</code>'s owner table record
* @author Lukas Eder
*/
public interface Key<R extends Record> extends Serializable {
/**
* The <code>Key</code>'s name.
*/
String getName();
public interface Key<R extends Record> extends Named {
/**
* The <code>Key</code>'s owner table

View File

@ -40,6 +40,7 @@ package org.jooq.impl;
import java.util.Arrays;
import java.util.List;
import org.jooq.Context;
import org.jooq.Key;
import org.jooq.Record;
import org.jooq.Table;
@ -50,14 +51,13 @@ import org.jooq.TableField;
*
* @author Lukas Eder
*/
abstract class AbstractKey<R extends Record> implements Key<R> {
abstract class AbstractKey<R extends Record> extends AbstractNamed implements Key<R> {
/**
* Generated UID
*/
private static final long serialVersionUID = 8176874459141379340L;
private final String name;
private final Table<R> table;
private final TableField<R, ?>[] fields;
@ -72,14 +72,10 @@ abstract class AbstractKey<R extends Record> implements Key<R> {
@SafeVarargs
AbstractKey(Table<R> table, String name, TableField<R, ?>... fields) {
this.table = table;
this.name = name;
this.fields = fields;
}
super(name == null ? null : DSL.name(name), null);
@Override
public final String getName() {
return name;
this.table = table;
this.fields = fields;
}
@Override
@ -97,11 +93,16 @@ abstract class AbstractKey<R extends Record> implements Key<R> {
return fields;
}
@Override
public final void accept(Context<?> ctx) {
ctx.visit(getUnqualifiedName());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + getQualifiedName().hashCode();
result = prime * result + ((table == null) ? 0 : table.hashCode());
return result;
}
@ -115,11 +116,7 @@ abstract class AbstractKey<R extends Record> implements Key<R> {
if (getClass() != obj.getClass())
return false;
AbstractKey<?> other = (AbstractKey<?>) obj;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
if (!getQualifiedName().equals(other.getQualifiedName()))
return false;
if (table == null) {
if (other.table != null)

View File

@ -51,7 +51,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jooq.Constraint;
@ -61,7 +60,6 @@ import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Index;
import org.jooq.Key;
import org.jooq.Meta;
import org.jooq.Named;
import org.jooq.Queries;
@ -159,7 +157,7 @@ final class DDL {
List<Constraint> result = new ArrayList<>();
if (configuration.flags().contains(UNIQUE))
for (UniqueKey<?> key : sortKeysIf(table.getKeys(), !configuration.respectConstraintOrder()))
for (UniqueKey<?> key : sortIf(table.getKeys(), !configuration.respectConstraintOrder()))
if (!key.isPrimary())
result.add(constraint(key.getName()).unique(key.getFieldsArray()));
@ -170,7 +168,7 @@ final class DDL {
List<Constraint> result = new ArrayList<>();
if (configuration.flags().contains(FOREIGN_KEY))
for (ForeignKey<?, ?> key : sortKeysIf(table.getReferences(), !configuration.respectConstraintOrder()))
for (ForeignKey<?, ?> key : sortIf(table.getReferences(), !configuration.respectConstraintOrder()))
result.add(constraint(key.getName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray()));
return result;
@ -273,22 +271,6 @@ final class DDL {
return ctx.queries(queries);
}
// [#9435] TODO: Remove this again when Key extends Named
private final <N extends Key<?>> List<N> sortKeysIf(List<N> input, boolean sort) {
if (sort) {
List<N> result = new ArrayList<>(input);
Collections.sort(result, new Comparator<Key<?>>() {
@Override
public int compare(Key<?> o1, Key<?> o2) {
return o1.getName().compareTo(o2.getName());
}
});
return result;
}
return input;
}
private final <N extends Named> List<N> sortIf(List<N> input, boolean sort) {
if (sort) {
List<N> result = new ArrayList<>(input);

View File

@ -290,6 +290,11 @@ package org.jooq.impl;

View File

@ -109,6 +109,7 @@ import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.ConnectionCallable;
import org.jooq.Constraint;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.ForeignKey;
@ -828,28 +829,23 @@ final class MetaImpl extends AbstractMeta {
}
}
private final class MetaPrimaryKey implements UniqueKey<Record> {
private final class MetaPrimaryKey extends AbstractNamed implements UniqueKey<Record> {
/**
* Generated UID
*/
private static final long serialVersionUID = 6997258619475953490L;
private final String pkName;
private final Table<Record> pkTable;
private final TableField<Record, ?>[] pkFields;
MetaPrimaryKey(Table<Record> table, String pkName, TableField<Record, ?>[] fields) {
this.pkName = pkName;
super(pkName == null ? null : DSL.name(pkName), null);
this.pkTable = table;
this.pkFields = fields;
}
@Override
public final String getName() {
return pkName;
}
@Override
public final Table<Record> getTable() {
return pkTable;
@ -939,6 +935,11 @@ final class MetaImpl extends AbstractMeta {
else
return DSL.constraint(getName()).unique(getFieldsArray());
}
@Override
public final void accept(Context<?> ctx) {
ctx.visit(getUnqualifiedName());
}
}
@Override