use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class RegisterConstructorMapperImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterConstructorMapper registerConstructorMapper = (RegisterConstructorMapper) annotation;
RowMappers mappers = registry.get(RowMappers.class);
Class<?> type = registerConstructorMapper.value();
String prefix = registerConstructorMapper.prefix();
if (prefix.isEmpty()) {
mappers.register(ConstructorMapper.factory(type));
} else {
mappers.register(ConstructorMapper.factory(type, prefix));
}
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class ConstructorMapper method specialize0.
private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
final int count = constructor.getParameterCount();
final Parameter[] parameters = constructor.getParameters();
final RowMapper<?>[] mappers = new RowMapper<?>[count];
for (int i = 0; i < count; i++) {
final Parameter parameter = parameters[i];
Nested anno = parameter.getAnnotation(Nested.class);
if (anno == null) {
final String paramName = prefix + paramName(parameters, i, constructorProperties);
final int columnIndex = findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(parameter)).orElseThrow(() -> new IllegalArgumentException(String.format("Constructor '%s' parameter '%s' has no column in the result set. " + "Verify that the Java compiler is configured to emit parameter names, " + "that your result set has the columns expected, or annotate the " + "parameter names explicitly with @ColumnName", constructor, paramName)));
final Type type = parameter.getParameterizedType();
mappers[i] = ctx.findColumnMapperFor(type).map(mapper -> new SingleColumnMapper(mapper, columnIndex + 1)).orElseThrow(() -> new IllegalArgumentException(String.format("Could not find column mapper for type '%s' of parameter '%s' for constructor '%s'", type, paramName, constructor)));
unmatchedColumns.remove(columnNames.get(columnIndex));
} else {
String nestedPrefix = prefix + anno.value();
mappers[i] = nestedMappers.computeIfAbsent(parameter, p -> new ConstructorMapper<>(findConstructorFor(p.getType()), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
}
}
return (r, c) -> {
final Object[] params = new Object[count];
for (int i = 0; i < count; i++) {
params[i] = mappers[i].map(r, c);
}
return construct(params);
};
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class FieldMapper method specialize0.
private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
final List<RowMapper<?>> mappers = new ArrayList<>();
final List<Field> 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 -> {
Type type = field.getGenericType();
ColumnMapper<?> mapper = ctx.findColumnMapperFor(type).orElse((r, n, c) -> rs.getObject(n));
mappers.add(new SingleColumnMapper(mapper, index + 1));
fields.add(field);
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value().toLowerCase();
RowMapper<?> mapper = nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
mappers.add(mapper);
fields.add(field);
}
}
}
if (mappers.isEmpty() && columnNames.size() > 0) {
throw new IllegalArgumentException(String.format("Mapping fields for type %s " + "didn't find any matching columns in result set", type));
}
return (r, c) -> {
T obj = construct();
for (int i = 0; i < mappers.size(); i++) {
RowMapper<?> mapper = mappers.get(i);
Field field = fields.get(i);
Object value = mapper.map(rs, ctx);
writeField(obj, field, value);
}
return obj;
};
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class RegisterFieldMapperImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterFieldMapper registerFieldMapper = (RegisterFieldMapper) annotation;
Class<?> type = registerFieldMapper.value();
String prefix = registerFieldMapper.prefix();
RowMappers mappers = registry.get(RowMappers.class);
if (prefix.isEmpty()) {
mappers.register(FieldMapper.factory(type));
} else {
mappers.register(FieldMapper.factory(type, prefix));
}
}
use of org.jdbi.v3.core.mapper.Mappers in project jdbi by jdbi.
the class RegisterBeanMapperImpl method configureForType.
@Override
@SuppressWarnings("deprecation")
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterBeanMapper registerBeanMapper = (RegisterBeanMapper) annotation;
Class<?> beanClass = registerBeanMapper.value();
String prefix = registerBeanMapper.prefix();
RowMappers mappers = registry.get(RowMappers.class);
if (prefix.isEmpty()) {
mappers.register(BeanMapper.factory(beanClass));
} else {
mappers.register(BeanMapper.factory(beanClass, prefix));
}
}
Aggregations