[#1977] Remove the confusing concept of having a "main key" as opposed

to a "primary key"
This commit is contained in:
Lukas Eder 2013-02-12 12:30:11 +01:00
parent f7e672ec39
commit fba595a302
16 changed files with 53 additions and 66 deletions

View File

@ -123,7 +123,7 @@ public class DefaultGeneratorStrategy extends AbstractGeneratorStrategy {
if (mode == Mode.DAO) {
TableDefinition table = (TableDefinition) definition;
List<ColumnDefinition> keyColumns = table.getMainUniqueKey().getKeyColumns();
List<ColumnDefinition> keyColumns = table.getPrimaryKey().getKeyColumns();
String name = DAO.class.getName();

View File

@ -177,7 +177,7 @@ class GeneratorStrategyWrapper extends AbstractGeneratorStrategy {
reserved = reservedColumns(UDTRecordImpl.class);
}
else if (definition instanceof ColumnDefinition) {
if (((ColumnDefinition) definition).getContainer().getMainUniqueKey() != null) {
if (((ColumnDefinition) definition).getContainer().getPrimaryKey() != null) {
reserved = reservedColumns(UpdatableRecordImpl.class);
}
else {

View File

@ -463,7 +463,7 @@ public class JavaGenerator extends AbstractGenerator {
protected void generateRecord(TableDefinition table) {
log.info("Generating record", getStrategy().getFileName(table, Mode.RECORD));
final UniqueKeyDefinition key = table.getMainUniqueKey();
final UniqueKeyDefinition key = table.getPrimaryKey();
final String className = getStrategy().getJavaClassName(table, Mode.RECORD);
final String tableIdentifier = getStrategy().getFullJavaIdentifier(table);
final String recordType = getStrategy().getFullJavaClassName(table, Mode.RECORD);
@ -1147,7 +1147,7 @@ public class JavaGenerator extends AbstractGenerator {
String tType = "Void";
String pType = getStrategy().getFullJavaClassName(table, Mode.POJO);
UniqueKeyDefinition key = table.getMainUniqueKey();
UniqueKeyDefinition key = table.getPrimaryKey();
ColumnDefinition keyColumn = null;
if (key != null) {
@ -1352,7 +1352,7 @@ public class JavaGenerator extends AbstractGenerator {
}
protected void generateTable(SchemaDefinition schema, TableDefinition table) {
UniqueKeyDefinition mainKey = table.getMainUniqueKey();
UniqueKeyDefinition primaryKey = table.getPrimaryKey();
final String className = getStrategy().getJavaClassName(table);
final String fullClassName = getStrategy().getFullJavaClassName(table);
@ -1363,7 +1363,7 @@ public class JavaGenerator extends AbstractGenerator {
log.info("Generating table", getStrategy().getFileName(table) +
" [input=" + table.getInputName() +
", output=" + table.getOutputName() +
", pk=" + (mainKey != null ? mainKey.getName() : "N/A") +
", pk=" + (primaryKey != null ? primaryKey.getName() : "N/A") +
"]");
JavaWriter out = new JavaWriter(getStrategy().getFile(table));
@ -1371,7 +1371,7 @@ public class JavaGenerator extends AbstractGenerator {
printClassJavadoc(out, table);
Class<?> baseClass;
if (generateRelations() && mainKey != null) {
if (generateRelations() && primaryKey != null) {
baseClass = UpdatableTableImpl.class;
} else {
baseClass = TableImpl.class;
@ -1440,11 +1440,11 @@ public class JavaGenerator extends AbstractGenerator {
}
// The primary / main unique key
if (mainKey != null) {
final String keyFullId = getStrategy().getFullJavaIdentifier(mainKey);
if (primaryKey != null) {
final String keyFullId = getStrategy().getFullJavaIdentifier(primaryKey);
out.tab(1).overrideInherit();
out.tab(1).println("public %s<%s> getMainKey() {", UniqueKey.class, recordType);
out.tab(1).println("public %s<%s> getPrimaryKey() {", UniqueKey.class, recordType);
out.tab(2).println("return %s;", keyFullId);
out.tab(1).println("}");
}

View File

@ -55,8 +55,8 @@ implements TableDefinition {
private List<UniqueKeyDefinition> uniqueKeys;
private List<ForeignKeyDefinition> foreignKeys;
private boolean mainUniqueKeyLoaded;
private UniqueKeyDefinition mainUniqueKey;
private boolean primaryKeyLoaded;
private UniqueKeyDefinition primaryKey;
private boolean identityLoaded;
private IdentityDefinition identity;
@ -65,30 +65,20 @@ implements TableDefinition {
}
@Override
public final UniqueKeyDefinition getMainUniqueKey() {
if (!mainUniqueKeyLoaded) {
mainUniqueKeyLoaded = true;
public final UniqueKeyDefinition getPrimaryKey() {
if (!primaryKeyLoaded) {
primaryKeyLoaded = true;
// Try finding a primary key first
for (ColumnDefinition column : getColumns()) {
if (column.getPrimaryKey() != null) {
mainUniqueKey = column.getPrimaryKey();
return mainUniqueKey;
}
}
// Find best matching unique key. Matching algorithm:
// 1. Prefer single-column indexes
// 2. Prefer scalar-type indexes
for (ColumnDefinition column : getColumns()) {
for (UniqueKeyDefinition uniqueKey : column.getUniqueKeys()) {
mainUniqueKey = uniqueKey;
return mainUniqueKey;
primaryKey = column.getPrimaryKey();
return primaryKey;
}
}
}
return mainUniqueKey;
return primaryKey;
}
@Override

View File

@ -69,9 +69,9 @@ public interface TableDefinition extends Definition {
ColumnDefinition getColumn(int columnIndex);
/**
* Get the primary key or the main unique key for this table
* Get the primary key for this table
*/
UniqueKeyDefinition getMainUniqueKey();
UniqueKeyDefinition getPrimaryKey();
/**
* Get the unique keys for this table

View File

@ -84,7 +84,7 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
UpdatableTable<R> getTable();
/**
* A view holding values for the {@link UpdatableTable#getMainKey()}
* A view holding values for the {@link UpdatableTable#getPrimaryKey()}
* <p>
* This method returns a "view" of this record itself. Modifications to the
* returned record will affect values in this record.
@ -294,7 +294,7 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
* AND [version/timestamp fields = version/timestamp values]</pre></code>
* <p>
* This is in fact the same as calling
* <code>delete(getTable().getMainKey().getFieldsArray())</code>
* <code>delete(getTable().getPrimaryKey().getFieldsArray())</code>
*
* @return <code>1</code> if the record was deleted from the database.
* <code>0</code> if deletion was not necessary.

View File

@ -50,21 +50,18 @@ import org.jooq.conf.Settings;
public interface UpdatableTable<R extends Record> extends Table<R> {
/**
* Retrieve the table's main unique key. If there exists a
* <code>PRIMARY KEY</code> in the table, the <code>PRIMARY KEY</code> is
* returned. Otherwise, the most suitable <code>UNIQUE KEY</code> is
* returned.
* Retrieve the table's primary key
*
* @return The main key. This is never <code>null</code> because
* {@link UpdatableTable}'s always have at least one key.
* @return The primary key. This is never <code>null</code> for an updatable
* table.
*/
UniqueKey<R> getMainKey();
UniqueKey<R> getPrimaryKey();
/**
* Retrieve all of the table's unique keys.
*
* @return All keys. This is never <code>null</code> or empty, because
* {@link UpdatableTable}'s always have at least one key. This
* {@link UpdatableTable}'s always have at least a primary key. This
* method returns an unmodifiable list.
*/
List<UniqueKey<R>> getKeys();

View File

@ -266,11 +266,11 @@ abstract class AbstractRecord extends AbstractStore implements Record {
@Override
public final <T> void setValue(Field<T> field, T value) {
UniqueKey<?> mainKey = getMainKey();
UniqueKey<?> key = getPrimaryKey();
Value<T> val = getValue0(field);
// Normal fields' changed flag is always set to true
if (mainKey == null || !mainKey.getFields().contains(field)) {
if (key == null || !key.getFields().contains(field)) {
val.setValue(value);
}
@ -315,7 +315,7 @@ abstract class AbstractRecord extends AbstractStore implements Record {
/**
* Subclasses may override this
*/
UniqueKey<?> getMainKey() {
UniqueKey<?> getPrimaryKey() {
return null;
}
@ -485,7 +485,7 @@ abstract class AbstractRecord extends AbstractStore implements Record {
// flags should all be reset in order for the returned record to be
// updatable using store()
if (result instanceof AbstractRecord) {
UniqueKey<?> key = ((AbstractRecord) result).getMainKey();
UniqueKey<?> key = ((AbstractRecord) result).getPrimaryKey();
if (key != null) {
boolean isKeySet = true;

View File

@ -268,7 +268,7 @@ public abstract class DAOImpl<R extends UpdatableRecord<R>, P, T> implements DAO
private final Field<?> pk() {
if (table instanceof UpdatableTable) {
UpdatableTable<?> updatable = (UpdatableTable<?>) table;
UniqueKey<?> key = updatable.getMainKey();
UniqueKey<?> key = updatable.getPrimaryKey();
if (key.getFields().size() == 1) {
return key.getFields().get(0);

View File

@ -5440,7 +5440,7 @@ public class Executor implements Configuration {
@Support
public final <R extends UpdatableRecord<R>> int executeUpdate(R record) throws DataAccessException {
UpdateQuery<R> update = updateQuery(record.getTable());
Utils.addConditions(update, record, record.getTable().getMainKey().getFieldsArray());
Utils.addConditions(update, record, record.getTable().getPrimaryKey().getFieldsArray());
update.setRecord(record);
return update.execute();
}
@ -5470,7 +5470,7 @@ public class Executor implements Configuration {
@Support
public final <R extends UpdatableRecord<R>> int executeDelete(R record) throws DataAccessException {
DeleteQuery<R> delete = deleteQuery(record.getTable());
Utils.addConditions(delete, record, record.getTable().getMainKey().getFieldsArray());
Utils.addConditions(delete, record, record.getTable().getPrimaryKey().getFieldsArray());
return delete.execute();
}

View File

@ -375,7 +375,7 @@ class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> implements
Condition condition = null;
List<Field<?>> key = new ArrayList<Field<?>>();
for (Field<?> f : into.getMainKey().getFields()) {
for (Field<?> f : into.getPrimaryKey().getFields()) {
Field<Object> field = (Field<Object>) f;
Field<Object> value = (Field<Object>) insertMaps.getMap().get(field);

View File

@ -116,7 +116,7 @@ class LoaderImpl<R extends TableRecord<R>> implements
private char separator = CSVParser.DEFAULT_SEPARATOR;
private String nullString = null;
private Field<?>[] fields;
private boolean[] mainKey;
private boolean[] primaryKey;
// Result data
// -----------
@ -270,13 +270,13 @@ class LoaderImpl<R extends TableRecord<R>> implements
@Override
public final LoaderImpl<R> fields(Field<?>... f) {
this.fields = f;
this.mainKey = new boolean[f.length];
this.primaryKey = new boolean[f.length];
if (updatable != null) {
for (int i = 0; i < fields.length; i++) {
if (fields[i] != null) {
if (updatable.getMainKey().getFields().contains(fields[i])) {
mainKey[i] = true;
if (updatable.getPrimaryKey().getFields().contains(fields[i])) {
primaryKey[i] = true;
}
}
}
@ -371,7 +371,7 @@ class LoaderImpl<R extends TableRecord<R>> implements
insert.onDuplicateKeyUpdate(true);
for (int i = 0; i < row.length; i++) {
if (i < fields.length && fields[i] != null && !mainKey[i]) {
if (i < fields.length && fields[i] != null && !primaryKey[i]) {
addValueForUpdate0(insert, fields[i], row[i]);
}
}
@ -383,7 +383,7 @@ class LoaderImpl<R extends TableRecord<R>> implements
SelectQuery<R> select = create.selectQuery(table);
for (int i = 0; i < row.length; i++) {
if (i < fields.length && mainKey[i]) {
if (i < fields.length && primaryKey[i]) {
select.addConditions(getCondition(fields[i], row[i]));
}
}

View File

@ -920,7 +920,7 @@ implements
Condition condition = null;
if (getH2Keys().isEmpty()) {
if (table instanceof UpdatableTable) {
UniqueKey<?> key = ((UpdatableTable<?>) table).getMainKey();
UniqueKey<?> key = ((UpdatableTable<?>) table).getPrimaryKey();
onFields.addAll(key.getFields());
for (int i = 0; i < key.getFields().size(); i++) {

View File

@ -313,7 +313,7 @@ class MetaImpl implements Meta {
}
@Override
public final UniqueKey<Record> getMainKey() {
public final UniqueKey<Record> getPrimaryKey() {
return null;
}

View File

@ -85,7 +85,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Record key() {
RecordImpl result = new RecordImpl(getMainKey().getFields());
RecordImpl result = new RecordImpl(getPrimaryKey().getFields());
for (Field<?> field : result.fields) {
result.setValue(field, getValue0(field));
@ -111,13 +111,13 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
}
@Override
final UniqueKey<R> getMainKey() {
return getTable().getMainKey();
final UniqueKey<R> getPrimaryKey() {
return getTable().getPrimaryKey();
}
@Override
public final int store() {
TableField<R, ?>[] keys = getMainKey().getFieldsArray();
TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray();
boolean executeUpdate = false;
for (TableField<R, ?> field : keys) {
@ -153,7 +153,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
@Override
public final int update() {
return storeUpdate(getMainKey().getFieldsArray());
return storeUpdate(getPrimaryKey().getFieldsArray());
}
private final int storeInsert() {
@ -310,7 +310,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
@Override
public final int delete() {
TableField<R, ?>[] keys = getMainKey().getFieldsArray();
TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray();
try {
DeleteQuery<R> delete1 = create().deleteQuery(getTable());
@ -352,7 +352,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
SelectQuery<?> select = create().selectQuery();
select.addSelect(f);
select.addFrom(getTable());
Utils.addConditions(select, this, getMainKey().getFieldsArray());
Utils.addConditions(select, this, getPrimaryKey().getFieldsArray());
if (select.execute() == 1) {
AbstractRecord record = (AbstractRecord) select.getResult().get(0);
@ -374,7 +374,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
result.add(identity.getField());
}
result.addAll(getMainKey().getFields());
result.addAll(getPrimaryKey().getFields());
return result;
}
@ -383,7 +383,7 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
R copy = create().newRecord(getTable());
// Copy all fields. This marks them all as isChanged, which is important
List<TableField<R, ?>> key = getMainKey().getFields();
List<TableField<R, ?>> key = getPrimaryKey().getFields();
for (Field<?> field : fields) {
// Don't copy key values

View File

@ -78,7 +78,7 @@ public class UpdatableTableImpl<R extends Record> extends TableImpl<R> implement
* Subclasses should override this method
*/
@Override
public UniqueKey<R> getMainKey() {
public UniqueKey<R> getPrimaryKey() {
return null;
}