Skip to content

Commit

Permalink
[#4152] Add <T> Field<T> TableLike.field( { String | int }, { Class<T…
Browse files Browse the repository at this point in the history
…>, DataType<T>, Field<T> } )
  • Loading branch information
lukaseder committed Mar 23, 2015
1 parent 8c48397 commit b423fe3
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 40 deletions.
36 changes: 36 additions & 0 deletions jOOQ/src/main/java/org/jooq/RecordType.java
Expand Up @@ -81,6 +81,24 @@ public interface RecordType<R extends Record> {
*/
Field<?> field(String fieldName);

/**
* Get a specific field from this record type coerced to <code>type</code>.
*
* @param fieldName The field to fetch
* @param type The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(String fieldName, Class<T> type);

/**
* Get a specific field from this record type coerced to <code>dataType</code>.
*
* @param fieldName The field to fetch
* @param dataType The data type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(String fieldName, DataType<T> dataType);

/**
* Get a specific field from this record type.
*
Expand All @@ -89,6 +107,24 @@ public interface RecordType<R extends Record> {
*/
Field<?> field(int fieldIndex);

/**
* Get a specific field from this record type coerced to <code>type</code>.
*
* @param fieldIndex The field's index of the field to fetch
* @param type The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(int fieldIndex, Class<T> type);

/**
* Get a specific field from this record type coerced to <code>dataType</code>.
*
* @param fieldIndex The field's index of the field to fetch
* @param dataType The data type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(int fieldIndex, DataType<T> dataType);

/**
* Get all fields from this record type.
*
Expand Down
28 changes: 28 additions & 0 deletions jOOQ/src/main/java/org/jooq/Result.java
Expand Up @@ -96,13 +96,41 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
Field<?> field(String name);

/**
* Get a specific field from this Result, coerced to <code>type</code>.
*
* @see Row#field(String, Class)
*/
<T> Field<T> field(String name, Class<T> type);

/**
* Get a specific field from this Result, coerced to <code>dataType</code>.
*
* @see Row#field(String, DataType)
*/
<T> Field<T> field(String name, DataType<T> dataType);

/**
* Get a specific field from this Result.
*
* @see Row#field(int)
*/
Field<?> field(int index);

/**
* Get a specific field from this Result, coerced to <code>type</code>.
*
* @see Row#field(int, Class)
*/
<T> Field<T> field(int index, Class<T> type);

/**
* Get a specific field from this Result, coerced to <code>dataType</code>.
*
* @see Row#field(int, DataType)
*/
<T> Field<T> field(int index, DataType<T> dataType);

/**
* Get all fields from this Result.
*
Expand Down
36 changes: 36 additions & 0 deletions jOOQ/src/main/java/org/jooq/Row.java
Expand Up @@ -77,6 +77,24 @@ public interface Row extends QueryPart {
*/
Field<?> field(String fieldName);

/**
* Get a specific field from this row and coerce it to <code>type</code>.
*
* @param fieldName The field to fetch
* @param type The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(String fieldName, Class<T> type);

/**
* Get a specific field from this row and coerce it to <code>dataType</code>.
*
* @param fieldName The field to fetch
* @param dataType The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(String fieldName, DataType<T> dataType);

/**
* Get a specific field from this row.
*
Expand All @@ -85,6 +103,24 @@ public interface Row extends QueryPart {
*/
Field<?> field(int fieldIndex);

/**
* Get a specific field from this row and coerce it to <code>type</code>.
*
* @param fieldIndex The field's index of the field to fetch
* @param type The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(int fieldIndex, Class<T> type);

/**
* Get a specific field from this row and coerce it to <code>dataType</code>.
*
* @param fieldIndex The field's index of the field to fetch
* @param dataType The type to coerce the resulting field to
* @return The field with the given name
*/
<T> Field<T> field(int fieldIndex, DataType<T> dataType);

/**
* Get all fields from this row.
*
Expand Down
30 changes: 30 additions & 0 deletions jOOQ/src/main/java/org/jooq/TableLike.java
Expand Up @@ -67,13 +67,43 @@ public interface TableLike<R extends Record> extends QueryPart {
*/
Field<?> field(String name);

/**
* Get a specific field from this Record and coerce it to <code>type</code>.
*
* @see Row#field(String, Class)
*/
<T> Field<T> field(String name, Class<T> type);

/**
* Get a specific field from this Record and coerce it to
* <code>dataType</code>.
*
* @see Row#field(String, DataType)
*/
<T> Field<T> field(String name, DataType<T> dataType);

/**
* Get a specific field from this Record.
*
* @see Row#field(int)
*/
Field<?> field(int index);

/**
* Get a specific field from this Record and coerce it to <code>type</code>.
*
* @see Row#field(int, Class)
*/
<T> Field<T> field(int index, Class<T> type);

/**
* Get a specific field from this Record and coerce it to
* <code>dataType</code>.
*
* @see Row#field(int, DataType)
*/
<T> Field<T> field(int index, DataType<T> dataType);

/**
* Get all fields from this Record.
*
Expand Down
42 changes: 31 additions & 11 deletions jOOQ/src/main/java/org/jooq/impl/AbstractTable.java
Expand Up @@ -98,9 +98,9 @@ abstract class AbstractTable<R extends Record> extends AbstractQueryPart impleme
private static final long serialVersionUID = 3155496238969274871L;
private static final Clause[] CLAUSES = { TABLE };

private final Schema schema;
private final String name;
private final String comment;
private final Schema tableschema;
private final String tablename;
private final String tablecomment;

AbstractTable(String name) {
this(name, null, null);
Expand All @@ -113,9 +113,9 @@ abstract class AbstractTable<R extends Record> extends AbstractQueryPart impleme
AbstractTable(String name, Schema schema, String comment) {
super();

this.schema = schema;
this.name = name;
this.comment = comment;
this.tableschema = schema;
this.tablename = name;
this.tablecomment = comment;
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -165,11 +165,31 @@ public final Field<?> field(String string) {
return fieldsRow().field(string);
}

@Override
public final <T> Field<T> field(String name, Class<T> type) {
return fieldsRow().field(name, type);
}

@Override
public final <T> Field<T> field(String name, DataType<T> dataType) {
return fieldsRow().field(name, dataType);
}

@Override
public final Field<?> field(int index) {
return fieldsRow().field(index);
}

@Override
public final <T> Field<T> field(int index, Class<T> type) {
return fieldsRow().field(index, type);
}

@Override
public final <T> Field<T> field(int index, DataType<T> dataType) {
return fieldsRow().field(index, dataType);
}

@Override
public final Field<?>[] fields() {
return fieldsRow().fields();
Expand All @@ -196,17 +216,17 @@ public final Table<R> asTable(String alias, String... fieldAliases) {

@Override
public final Schema getSchema() {
return schema;
return tableschema;
}

@Override
public final String getName() {
return name;
return tablename;
}

@Override
public final String getComment() {
return comment;
return tablecomment;
}

/**
Expand Down Expand Up @@ -811,7 +831,7 @@ public boolean equals(Object that) {
// [#2144] Non-equality can be decided early, without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof AbstractTable) {
if (StringUtils.equals(name, (((AbstractTable<?>) that).name))) {
if (StringUtils.equals(tablename, (((AbstractTable<?>) that).tablename))) {
return super.equals(that);
}

Expand All @@ -826,6 +846,6 @@ public int hashCode() {

// [#1938] This is a much more efficient hashCode() implementation
// compared to that of standard QueryParts
return name.hashCode();
return tablename.hashCode();
}
}
24 changes: 24 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/Fields.java
Expand Up @@ -116,6 +116,18 @@ public final Field<?> field(String name) {
return null;
}

@Override
public final <T> Field<T> field(String fieldName, Class<T> type) {
Field<?> result = field(fieldName);
return result == null ? null : result.coerce(type);
}

@Override
public final <T> Field<T> field(String fieldName, DataType<T> dataType) {
Field<?> result = field(fieldName);
return result == null ? null : result.coerce(dataType);
}

@Override
public final Field<?> field(int index) {
if (index >= 0 && index < fields.length) {
Expand All @@ -125,6 +137,18 @@ public final Field<?> field(int index) {
return null;
}

@Override
public final <T> Field<T> field(int fieldIndex, Class<T> type) {
Field<?> result = field(fieldIndex);
return result == null ? null : result.coerce(type);
}

@Override
public final <T> Field<T> field(int fieldIndex, DataType<T> dataType) {
Field<?> result = field(fieldIndex);
return result == null ? null : result.coerce(dataType);
}

@Override
public final Field<?>[] fields() {
return fields;
Expand Down
21 changes: 21 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/ResultImpl.java
Expand Up @@ -79,6 +79,7 @@
import org.jooq.Configuration;
import org.jooq.Converter;
import org.jooq.DSLContext;
import org.jooq.DataType;
import org.jooq.EnumType;
import org.jooq.Field;
import org.jooq.ForeignKey;
Expand Down Expand Up @@ -206,11 +207,31 @@ public final Field<?> field(String name) {
return fields.field(name);
}

@Override
public final <T> Field<T> field(String name, Class<T> type) {
return fields.field(name, type);
}

@Override
public final <T> Field<T> field(String name, DataType<T> dataType) {
return fields.field(name, dataType);
}

@Override
public final Field<?> field(int index) {
return fields.field(index);
}

@Override
public final <T> Field<T> field(int index, Class<T> type) {
return fields.field(index, type);
}

@Override
public final <T> Field<T> field(int index, DataType<T> dataType) {
return fields.field(index, dataType);
}

@Override
public final Field<?>[] fields() {
return fields.fields().clone();
Expand Down

0 comments on commit b423fe3

Please sign in to comment.