use of org.jdbi.v3.core.mapper.PropagateNull in project jdbi by jdbi.
the class BeanMapperTest method propagateNull.
@Test
public void propagateNull() {
Handle handle = h2Extension.getSharedHandle();
assertThat(handle.registerRowMapper(BeanMapper.factory(PropagateNullThing.class)).select("SELECT null as testValue, 'foo' as s").mapTo(PropagateNullThing.class).one()).isNull();
}
use of org.jdbi.v3.core.mapper.PropagateNull in project jdbi by jdbi.
the class PojoMapper method specialize0.
private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
final List<PropertyData<T>> propList = new ArrayList<>();
for (PojoProperty<T> property : getProperties(ctx.getConfig()).getProperties().values()) {
Nested anno = property.getAnnotation(Nested.class).orElse(null);
if (property.getAnnotation(Unmappable.class).map(Unmappable::value).orElse(false)) {
continue;
}
if (anno == null) {
String paramName = prefix + getName(property);
findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(property)).ifPresent(index -> {
@SuppressWarnings({ "unchecked", "rawtypes" }) ColumnMapper<?> mapper = ctx.findColumnMapperFor(property.getQualifiedType().mapType(GenericTypes::box)).orElseGet(() -> (ColumnMapper) defaultColumnMapper(property));
propList.add(new PropertyData<>(property, new SingleColumnMapper<>(mapper, index + 1)));
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value();
if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
nestedMappers.computeIfAbsent(property, d -> createNestedMapper(ctx, d, nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(nestedMapper -> propList.add(new PropertyData<>(property, nestedMapper)));
}
}
}
if (propList.isEmpty() && !columnNames.isEmpty()) {
return Optional.empty();
}
propList.sort(Comparator.comparing(p -> p.propagateNull ? 1 : 0));
final Optional<String> nullMarkerColumn = Optional.ofNullable(GenericTypes.getErasedType(type).getAnnotation(PropagateNull.class)).map(PropagateNull::value);
return Optional.of((r, c) -> {
if (propagateNull(r, nullMarkerColumn)) {
return null;
}
final PojoBuilder<T> pojo = getProperties(c.getConfig()).create();
for (PropertyData<T> p : propList) {
Object value = p.mapper.map(r, ctx);
if (p.propagateNull && (value == null || (p.isPrimitive && r.wasNull()))) {
return null;
}
if (value != null) {
pojo.set(p.property, value);
}
}
return pojo.build();
});
}
use of org.jdbi.v3.core.mapper.PropagateNull in project jdbi by jdbi.
the class FieldMapperTest method propagateNull.
@Test
public void propagateNull() {
Handle handle = h2Extension.getSharedHandle();
assertThat(handle.registerRowMapper(FieldMapper.factory(PropagateNullThing.class)).select("SELECT null as testValue, 'foo' as s").mapTo(PropagateNullThing.class).one()).isNull();
}
use of org.jdbi.v3.core.mapper.PropagateNull in project jdbi by jdbi.
the class ConstructorMapper method specialize0.
private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
final int count = factory.getParameterCount();
final Parameter[] parameters = factory.getParameters();
boolean matchedColumns = false;
final List<String> unmatchedParameters = new ArrayList<>();
final List<ParameterData> paramData = new ArrayList<>();
for (int i = 0; i < count; i++) {
final Parameter parameter = parameters[i];
boolean nullable = isNullable(parameter);
Nested anno = parameter.getAnnotation(Nested.class);
if (anno == null) {
final String paramName = prefix + paramName(parameters, i, constructorProperties);
final OptionalInt columnIndex = findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(parameter));
if (columnIndex.isPresent()) {
int colIndex = columnIndex.getAsInt();
final QualifiedType<?> type = QualifiedType.of(parameter.getParameterizedType()).withAnnotations(ctx.getConfig(Qualifiers.class).findFor(parameter));
paramData.add(new ParameterData(i, parameter, ctx.findColumnMapperFor(type).map(mapper -> new SingleColumnMapper<>(mapper, colIndex + 1)).orElseThrow(() -> new IllegalArgumentException(format("Could not find column mapper for type '%s' of parameter '%s' for instance factory '%s'", type, paramName, factory)))));
matchedColumns = true;
unmatchedColumns.remove(columnNames.get(colIndex));
} else if (nullable) {
paramData.add(new ParameterData(i, parameter, (r, c) -> null));
} else {
unmatchedParameters.add(paramName);
}
} else {
final String nestedPrefix = prefix + anno.value();
final Optional<? extends RowMapper<?>> nestedMapper = nestedMappers.computeIfAbsent(parameter, p -> new ConstructorMapper<>(findFactoryFor(p.getType()), nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns);
if (nestedMapper.isPresent()) {
paramData.add(new ParameterData(i, parameter, nestedMapper.get()));
matchedColumns = true;
} else if (nullable) {
paramData.add(new ParameterData(i, parameter, (r, c) -> null));
} else {
unmatchedParameters.add(paramName(parameters, i, constructorProperties));
}
}
}
if (!matchedColumns) {
return Optional.empty();
}
paramData.sort(Comparator.comparing(p -> p.propagateNull ? 1 : 0));
if (!unmatchedParameters.isEmpty()) {
throw new IllegalArgumentException(format(UNMATCHED_CONSTRUCTOR_PARAMETER, factory, unmatchedParameters));
}
final Optional<String> nullMarkerColumn = Optional.ofNullable(factory.getAnnotationIncludingType(PropagateNull.class)).map(PropagateNull::value);
return Optional.of((r, c) -> {
if (PojoMapper.propagateNull(r, nullMarkerColumn)) {
return null;
}
final Object[] params = new Object[count];
for (ParameterData p : paramData) {
params[p.index] = p.mapper.map(r, c);
if (p.propagateNull && (params[p.index] == null || (p.isPrimitive && r.wasNull()))) {
return null;
}
}
return factory.newInstance(params);
});
}
use of org.jdbi.v3.core.mapper.PropagateNull in project jdbi by jdbi.
the class FieldMapper method specialize0.
private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
final List<FieldData> fields = new ArrayList<>();
for (Class<?> aType = type; aType != null; aType = aType.getSuperclass()) {
for (Field field : aType.getDeclaredFields()) {
Nested anno = field.getAnnotation(Nested.class);
if (anno == null) {
String paramName = prefix + paramName(field);
findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(field)).ifPresent(index -> {
QualifiedType<?> fieldType = QualifiedType.of(field.getGenericType()).withAnnotations(ctx.getConfig(Qualifiers.class).findFor(field));
@SuppressWarnings("unchecked") ColumnMapper<?> mapper = ctx.findColumnMapperFor(fieldType).orElse((ColumnMapper) (r, n, c) -> r.getObject(n));
fields.add(new FieldData(field, new SingleColumnMapper<>(mapper, index + 1)));
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value().toLowerCase();
if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(mapper -> fields.add(new FieldData(field, mapper)));
}
}
}
}
if (fields.isEmpty() && !columnNames.isEmpty()) {
return Optional.empty();
}
Collections.sort(fields, Comparator.comparing(f -> f.propagateNull ? 1 : 0));
final Optional<String> nullMarkerColumn = Optional.ofNullable(type.getAnnotation(PropagateNull.class)).map(PropagateNull::value);
return Optional.of((r, c) -> {
if (PojoMapper.propagateNull(r, nullMarkerColumn)) {
return null;
}
T obj = construct();
for (FieldData f : fields) {
Object value = f.mapper.map(r, ctx);
if (f.propagateNull && (value == null || (f.isPrimitive && r.wasNull()))) {
return null;
}
writeField(obj, f.field, value);
}
return obj;
});
}
Aggregations