[#7470] Add indexOf() methods to all TableLike types

This commit is contained in:
lukaseder 2018-11-28 15:45:03 +01:00
parent 9b50c5d4c4
commit 4cb0990735
13 changed files with 305 additions and 152 deletions

View File

@ -167,6 +167,33 @@ public interface Cursor<R extends Record> extends Iterable<R>, Formattable , Aut
*/
Field<?>[] fields(int... fieldIndexes);
/**
* Get a field's index from this cursor.
*
* @param field The field to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this cursor.
*/
int indexOf(Field<?> field);
/**
* Get a field's index from this cursor.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this cursor.
*/
int indexOf(String fieldName);
/**
* Get a field's index from this cursor.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this cursor
*/
int indexOf(Name fieldName);
/**
* Check whether this cursor has a next record.
* <p>

View File

@ -189,6 +189,33 @@ public interface Record extends Attachable, Comparable<Record>, Formattable {
*/
Field<?>[] fields(int... fieldIndexes);
/**
* Get a field's index from this record.
*
* @param field The field to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this record.
*/
int indexOf(Field<?> field);
/**
* Get a field's index from this record.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this record.
*/
int indexOf(String fieldName);
/**
* Get a field's index from this record.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this record
*/
int indexOf(Name fieldName);
/**
* Get this record's values as a {@link Row}.
*/

View File

@ -193,6 +193,33 @@ public interface Result<R extends Record> extends List<R>, Attachable, Formattab
*/
Field<?>[] fields(int... fieldIndexes);
/**
* Get a field's index from this result.
*
* @param field The field to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this result.
*/
int indexOf(Field<?> field);
/**
* Get a field's index from this result.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this result.
*/
int indexOf(String fieldName);
/**
* Get a field's index from this result.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this result
*/
int indexOf(Name fieldName);
/**
* Convenience method to fetch a value at a given position in the result.
*

View File

@ -201,6 +201,33 @@ public interface TableLike<R extends Record> extends QueryPart {
*/
Field<?>[] fields(int... fieldIndexes);
/**
* Get a field's index from this table.
*
* @param field The field to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this table.
*/
int indexOf(Field<?> field);
/**
* Get a field's index from this table.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this table.
*/
int indexOf(String fieldName);
/**
* Get a field's index from this table.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this table
*/
int indexOf(Name fieldName);
/**
* The underlying table representation of this object.
* <p>

View File

@ -140,6 +140,33 @@ public interface UDT<R extends UDTRecord<R>> extends Named {
*/
Field<?>[] fields(int... fieldIndexes);
/**
* Get a field's index from this udt.
*
* @param field The field to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this udt.
*/
int indexOf(Field<?> field);
/**
* Get a field's index from this udt.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this udt.
*/
int indexOf(String fieldName);
/**
* Get a field's index from this udt.
*
* @param fieldName The field name to look for
* @return The field's index or <code>-1</code> if the field is not
* contained in this udt
*/
int indexOf(Name fieldName);
/**
* Get the UDT catalog.
*/

View File

@ -73,12 +73,16 @@ import org.jooq.Configuration;
import org.jooq.Constants;
import org.jooq.Cursor;
import org.jooq.DSLContext;
import org.jooq.DataType;
import org.jooq.EnumType;
import org.jooq.Field;
import org.jooq.Formattable;
import org.jooq.JSONFormat;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.Schema;
import org.jooq.TXTFormat;
import org.jooq.Table;
@ -114,6 +118,95 @@ abstract class AbstractCursor<R extends Record> extends AbstractFormattable impl
this.fields = fields;
}
// -------------------------------------------------------------------------
// XXX: RecordType API of subtypes
// -------------------------------------------------------------------------
public final RecordType<R> recordType() {
return fields;
}
@SuppressWarnings("rawtypes")
public final Row fieldsRow() {
return new RowImpl(fields);
}
public final <T> Field<T> field(Field<T> field) {
return fields.field(field);
}
public final Field<?> field(String name) {
return fields.field(name);
}
public final <T> Field<T> field(String name, Class<T> type) {
return fields.field(name, type);
}
public final <T> Field<T> field(String name, DataType<T> dataType) {
return fields.field(name, dataType);
}
public final Field<?> field(Name name) {
return fields.field(name);
}
public final <T> Field<T> field(Name name, Class<T> type) {
return fields.field(name, type);
}
public final <T> Field<T> field(Name name, DataType<T> dataType) {
return fields.field(name, dataType);
}
public final Field<?> field(int index) {
return fields.field(index);
}
public final <T> Field<T> field(int index, Class<T> type) {
return fields.field(index, type);
}
public final <T> Field<T> field(int index, DataType<T> dataType) {
return fields.field(index, dataType);
}
public final Field<?>[] fields() {
return fields.fields().clone();
}
public final Field<?>[] fields(Field<?>... f) {
return fields.fields(f);
}
public final Field<?>[] fields(int... indexes) {
return fields.fields(indexes);
}
public final Field<?>[] fields(String... names) {
return fields.fields(names);
}
public final Field<?>[] fields(Name... names) {
return fields.fields(names);
}
public final int indexOf(Field<?> field) {
return fields.indexOf(field);
}
public final int indexOf(String fieldName) {
return fields.indexOf(fieldName);
}
public final int indexOf(Name fieldName) {
return fields.indexOf(fieldName);
}
// -------------------------------------------------------------------------
// XXX: Formattable API
// -------------------------------------------------------------------------
@Override
public final void format(Writer writer, TXTFormat format) {
try {

View File

@ -214,6 +214,21 @@ abstract class AbstractRecord extends AbstractStore implements Record {
return fields.fields(fieldIndexes);
}
@Override
public final int indexOf(Field<?> field) {
return fields.indexOf(field);
}
@Override
public final int indexOf(String fieldName) {
return fields.indexOf(fieldName);
}
@Override
public final int indexOf(Name fieldName) {
return fields.indexOf(fieldName);
}
// ------------------------------------------------------------------------
// XXX: Record API
// ------------------------------------------------------------------------

View File

@ -309,6 +309,21 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return fieldsRow().fields(fieldIndexes);
}
@Override
public final int indexOf(Field<?> field) {
return fieldsRow().indexOf(field);
}
@Override
public final int indexOf(String fieldName) {
return fieldsRow().indexOf(fieldName);
}
@Override
public final int indexOf(Name fieldName) {
return fieldsRow().indexOf(fieldName);
}
@Override
public final Table<R> asTable() {
return this;

View File

@ -80,13 +80,10 @@ import org.jooq.Cursor;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RecordHandler;
import org.jooq.RecordMapper;
import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.Table;
import org.jooq.exception.ControlFlowSignal;
import org.jooq.tools.JooqLogger;
@ -102,7 +99,6 @@ final class CursorImpl<R extends Record> extends AbstractCursor<R> implements Cu
private final ExecuteContext ctx;
private final ExecuteListener listener;
private final Field<?>[] cursorFields;
private final boolean[] intern;
private final boolean keepResultSet;
private final boolean keepStatement;
@ -131,7 +127,6 @@ final class CursorImpl<R extends Record> extends AbstractCursor<R> implements Cu
this.ctx = ctx;
this.listener = (listener != null ? listener : ExecuteListeners.get(ctx));
this.cursorFields = fields;
this.factory = recordFactory(type, fields);
this.keepStatement = keepStatement;
this.keepResultSet = keepResultSet;
@ -205,63 +200,6 @@ final class CursorImpl<R extends Record> extends AbstractCursor<R> implements Cu
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final RecordType<R> recordType() {
return new RowImpl(cursorFields).fields;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Row fieldsRow() {
return new RowImpl(cursorFields);
}
@Override
public final <T> Field<T> field(Field<T> field) {
return fieldsRow().field(field);
}
@Override
public final Field<?> field(String name) {
return fieldsRow().field(name);
}
@Override
public final Field<?> field(Name name) {
return fieldsRow().field(name);
}
@Override
public final Field<?> field(int index) {
return index >= 0 && index < cursorFields.length ? cursorFields[index] : null;
}
@Override
public final Field<?>[] fields() {
return fieldsRow().fields();
}
@Override
public final Field<?>[] fields(Field<?>... fields) {
return fieldsRow().fields(fields);
}
@Override
public final Field<?>[] fields(String... fieldNames) {
return fieldsRow().fields(fieldNames);
}
@Override
public final Field<?>[] fields(Name... fieldNames) {
return fieldsRow().fields(fieldNames);
}
@Override
public final Field<?>[] fields(int... fieldIndexes) {
return fieldsRow().fields(fieldIndexes);
}
@Override
public final Iterator<R> iterator() {
@ -400,7 +338,7 @@ final class CursorImpl<R extends Record> extends AbstractCursor<R> implements Cu
// Before listener.resultStart(ctx)
iterator();
ResultImpl<R> result = new ResultImpl<R>(((DefaultExecuteContext) ctx).originalConfiguration(), cursorFields);
ResultImpl<R> result = new ResultImpl<R>(((DefaultExecuteContext) ctx).originalConfiguration(), fields.fields);
ctx.result(result);
listener.resultStart(ctx);
@ -1666,7 +1604,7 @@ final class CursorImpl<R extends Record> extends AbstractCursor<R> implements Cu
}
record = Tools.newRecord(true, (RecordFactory<AbstractRecord>) factory, ((DefaultExecuteContext) ctx).originalConfiguration())
.operate(new CursorRecordInitialiser(cursorFields, 0));
.operate(new CursorRecordInitialiser(fields.fields, 0));
rows++;
}

View File

@ -57,7 +57,6 @@ import java.util.Set;
import org.jooq.Configuration;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Name;
@ -86,9 +85,7 @@ import org.jooq.Record8;
import org.jooq.Record9;
import org.jooq.RecordHandler;
import org.jooq.RecordMapper;
import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.TXTFormat;
import org.jooq.Table;
import org.jooq.TableRecord;
@ -152,91 +149,6 @@ final class ResultImpl<R extends Record> extends AbstractCursor<R> implements Re
// XXX: Result API
// -------------------------------------------------------------------------
@Override
public final RecordType<R> recordType() {
return fields;
}
@Override
public final Row fieldsRow() {
return new RowImpl(fields);
}
@Override
public final <T> Field<T> field(Field<T> field) {
return fields.field(field);
}
@Override
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(Name name) {
return fields.field(name);
}
@Override
public final <T> Field<T> field(Name name, Class<T> type) {
return fields.field(name, type);
}
@Override
public final <T> Field<T> field(Name 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();
}
@Override
public final Field<?>[] fields(Field<?>... f) {
return fields.fields(f);
}
@Override
public final Field<?>[] fields(int... indexes) {
return fields.fields(indexes);
}
@Override
public final Field<?>[] fields(String... names) {
return fields.fields(names);
}
@Override
public final Field<?>[] fields(Name... names) {
return fields.fields(names);
}
@Override
public final boolean isEmpty() {
return records.isEmpty();

View File

@ -3792,6 +3792,21 @@ final class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10
return getDelegate().fields(fieldIndexes);
}
@Override
public final int indexOf(Field<?> field) {
return getDelegate().indexOf(field);
}
@Override
public final int indexOf(String fieldName) {
return getDelegate().indexOf(fieldName);
}
@Override
public final int indexOf(Name fieldName) {
return getDelegate().indexOf(fieldName);
}
/**
* The {@link SelectImpl} current condition step
* <p>

View File

@ -409,6 +409,21 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
return asTable().fields(fieldIndexes);
}
@Override
public final int indexOf(Field<?> field) {
return asTable().indexOf(field);
}
@Override
public final int indexOf(String fieldName) {
return asTable().indexOf(fieldName);
}
@Override
public final int indexOf(Name fieldName) {
return asTable().indexOf(fieldName);
}
@Override
public final Table<R> asTable() {
// Its usually better to alias nested selects that are used in

View File

@ -168,6 +168,21 @@ public class UDTImpl<R extends UDTRecord<R>> extends AbstractNamed implements UD
return fieldsRow().fields(fieldIndexes);
}
@Override
public final int indexOf(Field<?> field) {
return fieldsRow().indexOf(field);
}
@Override
public final int indexOf(String fieldName) {
return fieldsRow().indexOf(fieldName);
}
@Override
public final int indexOf(Name fieldName) {
return fieldsRow().indexOf(fieldName);
}
final Fields<R> fields0() {
return fields;
}