[#4471] Record.getValue(Field) returns wrong value if ambiguous column names are contained in the record, and the schema name is not present in the argument

This commit is contained in:
lukaseder 2015-08-15 13:33:16 +02:00
parent 7cdbe165f6
commit 6d9bff6883
2 changed files with 47 additions and 8 deletions

View File

@ -52,6 +52,8 @@ import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RecordType;
import org.jooq.Table;
import org.jooq.TableField;
/**
* A simple wrapper for <code>Field[]</code>, providing some useful lookup
@ -91,11 +93,34 @@ class Fields<R extends Record> extends AbstractQueryPart implements RecordType<R
}
}
// In case no exact match was found, return the first field with matching name
String name = field.getName();
for (Field<?> f1 : fields) {
if (f1.getName().equals(name)) {
return (Field<T>) f1;
// [#4283] table / column matches are better than only column matches
Field<?> columnMatch = null;
String tableName = tableName(field);
String fieldName = field.getName();
for (Field<?> f : fields) {
if (tableName != null) {
String tName = tableName(f);
if (tName != null && tableName.equals(tName) && f.getName().equals(fieldName))
return (Field<T>) f;
}
// In case no exact match was found, return the first field with matching name
if (columnMatch == null && f.getName().equals(fieldName))
columnMatch = f;
}
return (Field<T>) columnMatch;
}
private final String tableName(Field<?> field) {
if (field instanceof TableField) {
Table<?> table = ((TableField<?, ?>) field).getTable();
if (table != null) {
return table.getName();
}
}

View File

@ -40,11 +40,16 @@
*/
package org.jooq.impl;
import java.util.Arrays;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RenderContext;
import org.jooq.Table;
import org.jooq.TableField;
/**
* A <code>QualifiedField</code> is a {@link Field} that always renders a field name
@ -52,19 +57,23 @@ import org.jooq.RenderContext;
*
* @author Lukas Eder
*/
class QualifiedField<T> extends AbstractField<T> {
class QualifiedField<T> extends AbstractField<T> implements TableField<Record, T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 6937002867156868761L;
private static final long serialVersionUID = 6937002867156868761L;
private final Name name;
private final Name name;
private final Table<Record> table;
QualifiedField(Name name, DataType<T> type) {
super(name.getName()[name.getName().length - 1], type);
this.name = name;
this.table = name.getName().length > 1
? DSL.table(DSL.name(Arrays.copyOf(name.getName(), name.getName().length - 1)))
: null;
}
// ------------------------------------------------------------------------
@ -75,4 +84,9 @@ class QualifiedField<T> extends AbstractField<T> {
public final void accept(Context<?> ctx) {
ctx.visit(name);
}
@Override
public final Table<Record> getTable() {
return table;
}
}