[jOOQ/jOOQ#3896] Further improvements

- Documented DataType.convert() methods
- Removed some unnecessary Convert.convert() usages in DefaultBinding
- Extracted Tools.converter() convenience API
This commit is contained in:
Lukas Eder 2020-04-15 12:20:19 +02:00
parent a7bdae7c64
commit 3ce7931a61
6 changed files with 55 additions and 37 deletions

View File

@ -185,7 +185,9 @@ public interface DataType<T> extends Serializable {
/**
* Convert an arbitrary object into <code>&lt;T&gt;</code>.
* <p>
* See {@link Convert#convert(Object, Class)} for details about conversion rules.
* See {@link Convert#convert(Object, Class)} for details about conversion
* rules. Notice this does not pass through any
* {@link Configuration#converterProvider()}.
*
* @param object The object to be converted
* @return The converted object
@ -196,7 +198,9 @@ public interface DataType<T> extends Serializable {
/**
* Convert an arbitrary set of objects into <code>&lt;T&gt;</code>.
* <p>
* See {@link Convert#convert(Object, Class)} for details about conversion rules.
* See {@link Convert#convert(Object, Class)} for details about conversion
* rules. Notice this does not pass through any
* {@link Configuration#converterProvider()}.
*
* @param objects The objects to be converted
* @return The converted objects
@ -207,7 +211,9 @@ public interface DataType<T> extends Serializable {
/**
* Convert an arbitrary set of objects into <code>&lt;T&gt;</code>.
* <p>
* See {@link Convert#convert(Object, Class)} for details about conversion rules.
* See {@link Convert#convert(Object, Class)} for details about conversion
* rules. Notice this does not pass through any
* {@link Configuration#converterProvider()}.
*
* @param objects The objects to be converted
* @return The converted objects

View File

@ -254,7 +254,7 @@ abstract class AbstractRecord extends AbstractStore implements Record {
@Override
public final <T> T get(Field<?> field, Class<? extends T> type) {
return (T) Tools.configuration(this).converterProvider().provide(field.getType(), (Class) type).from(get(field));
return (T) Tools.converter(this, field.getType(), (Class) type).from(get(field));
}
@Override
@ -269,7 +269,7 @@ abstract class AbstractRecord extends AbstractStore implements Record {
@Override
public final <T> T get(int index, Class<? extends T> type) {
return (T) Tools.configuration(this).converterProvider().provide(field(safeIndex(index)).getType(), (Class) type).from(get(index));
return (T) Tools.converter(this, field(safeIndex(index)).getType(), (Class) type).from(get(index));
}
@Override

View File

@ -3964,7 +3964,8 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
@Override
final UByte get0(BindingGetResultSetContext<U> ctx) throws SQLException {
return Convert.convert(ctx.resultSet().getString(ctx.index()), UByte.class);
String string = ctx.resultSet().getString(ctx.index());
return string == null ? null : UByte.valueOf(string);
}
@Override
@ -4018,7 +4019,8 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
@Override
final UInteger get0(BindingGetResultSetContext<U> ctx) throws SQLException {
return Convert.convert(ctx.resultSet().getString(ctx.index()), UInteger.class);
String string = ctx.resultSet().getString(ctx.index());
return string == null ? null : UInteger.valueOf(string);
}
@Override
@ -4072,7 +4074,8 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
@Override
final ULong get0(BindingGetResultSetContext<U> ctx) throws SQLException {
return Convert.convert(ctx.resultSet().getString(ctx.index()), ULong.class);
String string = ctx.resultSet().getString(ctx.index());
return string == null ? null : ULong.valueOf(string);
}
@Override
@ -4121,7 +4124,8 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
@Override
final UShort get0(BindingGetResultSetContext<U> ctx) throws SQLException {
return Convert.convert(ctx.resultSet().getString(ctx.index()), UShort.class);
String string = ctx.resultSet().getString(ctx.index());
return string == null ? null : UShort.valueOf(string);
}
@Override

View File

@ -60,7 +60,6 @@ import static org.jooq.impl.Tools.EMPTY_TABLE_RECORD;
import static org.jooq.impl.Tools.EMPTY_UPDATABLE_RECORD;
import static org.jooq.impl.Tools.blocking;
import static org.jooq.impl.Tools.list;
import static org.jooq.tools.Convert.convert;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -3902,17 +3901,13 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
@Override
public BigInteger lastID() {
switch (configuration().family()) {
case DERBY: {
Field<BigInteger> field = field("identity_val_local()", BigInteger.class);
return select(field).fetchOne(field);
}
switch (family()) {
case DERBY:
return fetchValue(field("identity_val_local()", BigInteger.class));
case H2:
case HSQLDB: {
Field<BigInteger> field = field("identity()", BigInteger.class);
return select(field).fetchOne(field);
}
case HSQLDB:
return fetchValue(field("identity()", BigInteger.class));
@ -3922,30 +3917,19 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
case CUBRID:
case MARIADB:
case MYSQL: {
Field<BigInteger> field = field("last_insert_id()", BigInteger.class);
return select(field).fetchOne(field);
}
case MYSQL:
return fetchValue(field("last_insert_id()", BigInteger.class));
case SQLITE: {
Field<BigInteger> field = field("last_insert_rowid()", BigInteger.class);
return select(field).fetchOne(field);
}
case SQLITE:
return fetchValue(field("last_insert_rowid()", BigInteger.class));
case POSTGRES: {
Field<BigInteger> field = field("lastval()", BigInteger.class);
return select(field).fetchOne(field);
}
case POSTGRES:
return fetchValue(field("lastval()", BigInteger.class));

View File

@ -220,7 +220,7 @@ final class ResultImpl<R extends Record> extends AbstractCursor<R> implements Re
@Override
public final <U> List<U> getValues(int fieldIndex, Class<? extends U> type) {
List<U> result = new ArrayList<>(size());
Converter converter = Tools.configuration(this).converterProvider().provide(field(safeIndex(fieldIndex)).getType(), (Class) type);
Converter converter = Tools.converter(this, field(safeIndex(fieldIndex)).getType(), (Class) type);
for (R record : this)
result.add((U) converter.from(record.get(fieldIndex)));

View File

@ -219,6 +219,7 @@ import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Converter;
import org.jooq.ConverterProvider;
import org.jooq.Cursor;
import org.jooq.DSLContext;
import org.jooq.DataType;
@ -248,6 +249,7 @@ import org.jooq.Row;
import org.jooq.RowN;
import org.jooq.SQLDialect;
import org.jooq.Schema;
import org.jooq.Scope;
import org.jooq.Select;
import org.jooq.SelectFieldOrAsterisk;
import org.jooq.SortField;
@ -1071,6 +1073,28 @@ final class Tools {
return configuration != null ? configuration : new DefaultConfiguration();
}
/**
* Get a configuration or a new {@link DefaultConfiguration} if
* <code>null</code>.
*/
static final Configuration configuration(Scope scope) {
return configuration(scope != null ? scope.configuration() : null);
}
/**
* Get a converter from a {@link ConverterProvider}.
*/
static final <T, U> Converter<T, U> converter(Attachable attachable, Class<T> tType, Class<U> uType) {
return configuration(attachable).converterProvider().provide(tType, uType);
}
/**
* Get a converter from a {@link ConverterProvider}.
*/
static final <T, U> Converter<T, U> converter(Scope scope, Class<T> tType, Class<U> uType) {
return configuration(scope).converterProvider().provide(tType, uType);
}
/**
* Get a configuration's settings or default settings if the configuration
* is <code>null</code>.