Skip to content

Commit

Permalink
Draft for jackson mapping of a row onto an object
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed May 25, 2024
1 parent a1ce936 commit 6b62e50
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sadu-jackson-mapper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
api(project(":sadu-mapper"))
api("com.fasterxml.jackson.core", "jackson-databind", "2.17.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.chojo.sadu.mapper.RowMapperRegistry;
import de.chojo.sadu.mapper.wrapper.Row;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class ResultSetMapper {
private final RowMapperRegistry registry;
private final ObjectMapper objectMapper;

public ResultSetMapper(ObjectMapper objectMapper, RowMapperRegistry registry) {
this.objectMapper = objectMapper;
this.registry = registry;
}

public <T> T convertRow(Row row, Class<T> clazz) throws SQLException {
ResultSetMetaData meta = row.getMetaData();
Map<String, Object> map = new HashMap<>();
for (int index = 1; index < meta.getColumnCount() + 1; index++) {
map.put(meta.getColumnName(index), row.getObject(index));
// var type = meta.getColumnTypeName(index);
// Optional<RowMapper<?>> forType = registry.findForType(type);
// var mapper = forType.orElseThrow(() -> new UnknownTypeException(type));
// map.put(meta.getColumnName(index), mapper.map(row, index));
}

return objectMapper.convertValue(map, clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.jackson.exception;

public class UnknownTypeException extends RuntimeException{
public UnknownTypeException(String name) {
super("Unknown type " + name + " found. Please Register a mapper for this type.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.jackson;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ResultSetMapperTest {

@Test
void convertRow() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public static <T> RowMapper<T> create(Class<T> clazz, ThrowingBiFunction<Row, In
var columnIndexOfType = Results.getFirstColumnIndexOfType(meta, types);
var index = columnIndexOfType.orElseThrow(() -> createException(types, meta));
return mapper.apply(row, index);
}).build();
})
.indexMapper(mapper)
.types(types)
.build();
}

private static SQLException createException(List<SqlType> types, ResultSetMetaData meta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package de.chojo.sadu.mapper;

import de.chojo.sadu.core.types.SqlType;
import de.chojo.sadu.mapper.exceptions.MappingAlreadyRegisteredException;
import de.chojo.sadu.mapper.exceptions.MappingException;
import de.chojo.sadu.mapper.rowmapper.RowMapper;
Expand All @@ -26,6 +27,7 @@
*/
public class RowMapperRegistry {
private final Map<Class<?>, List<RowMapper<?>>> mapper = new HashMap<>();
private final Map<String, List<RowMapper<?>>> types = new HashMap<>();

public RowMapperRegistry() {
}
Expand All @@ -47,9 +49,22 @@ public RowMapperRegistry register(RowMapper<?> rowMapper) {
}

rowMappers.add(rowMapper);
if (rowMapper.isWildcard()) {
for (SqlType type : rowMapper.types()) {
registerType(type.name(), rowMapper);
for (String alias : type.alias()) {
registerType(alias, rowMapper);
}
}
}

return this;
}

private void registerType(String name, RowMapper<?> rowMapper) {
types.computeIfAbsent(name, key -> new ArrayList<>()).add(rowMapper);
}

/**
* Registers new mapper.
* <p>
Expand Down Expand Up @@ -178,4 +193,8 @@ public <T> RowMapper<T> findOrWildcard(Class<T> clazz, ResultSetMetaData meta, M
}
throw MappingException.create(clazz, meta);
}

public Optional<RowMapper<?>> findForType(String name) {
return Optional.ofNullable(types.get(name).get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

package de.chojo.sadu.mapper.rowmapper;

import de.chojo.sadu.core.exceptions.ThrowingBiFunction;
import de.chojo.sadu.core.types.SqlType;
import de.chojo.sadu.mapper.MapperConfig;
import de.chojo.sadu.mapper.wrapper.Row;
import org.slf4j.Logger;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static de.chojo.sadu.mapper.util.Results.columnNames;
Expand All @@ -29,11 +33,15 @@ public class RowMapper<T> implements RowMapping<T> {
private final Class<T> clazz;
private final RowMapping<T> mapper;
private final Set<String> columns;
private final List<SqlType> types;
private final ThrowingBiFunction<Row, Integer, T, SQLException> indexMapper;

RowMapper(Class<T> clazz, RowMapping<T> mapper, Set<String> columns) {
RowMapper(Class<T> clazz, RowMapping<T> mapper, ThrowingBiFunction<Row, Integer, T, SQLException> indexMapper, Set<String> columns, List<SqlType> types) {
this.clazz = clazz;
this.mapper = mapper;
this.columns = columns;
this.types = types;
this.indexMapper = indexMapper;
}

public static <T> PartialRowMapper<T> forClass(Class<T> clazz) {
Expand All @@ -53,6 +61,12 @@ public T map(Row row) throws SQLException {
return mapper.map(row);
}

public T map(Row row, int index) throws SQLException {
if(indexMapper == null) throw new UnsupportedOperationException("IndexMapper not set");
return indexMapper.apply(row, index);
}


public boolean isWildcard() {
return columns.isEmpty();
}
Expand Down Expand Up @@ -131,6 +145,9 @@ public int applicable(ResultSetMetaData meta, MapperConfig config) {
return overlap.size();
}

public List<SqlType> types() {
return Collections.unmodifiableList(types);
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

package de.chojo.sadu.mapper.rowmapper;

import de.chojo.sadu.core.exceptions.ThrowingBiFunction;
import de.chojo.sadu.core.types.SqlType;
import de.chojo.sadu.mapper.wrapper.Row;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -19,6 +24,8 @@ public class RowMapperBuilder<T> implements PartialRowMapper<T> {
private final Class<T> clazz;
private final Set<String> columns = new HashSet<>();
private RowMapping<T> mapper;
private List<SqlType> types;
private ThrowingBiFunction<Row, Integer, T, SQLException> indexMapper;

RowMapperBuilder(Class<T> clazz) {
this.clazz = clazz;
Expand Down Expand Up @@ -58,6 +65,16 @@ public RowMapperBuilder<T> addColumns(String... columns) {
* @return new RowMapper instance
*/
public RowMapper<T> build() {
return new RowMapper<>(clazz, mapper, columns);
return new RowMapper<>(clazz, mapper, indexMapper, columns, types);
}

public RowMapperBuilder<T> types(List<SqlType> types) {
this.types = types;
return this;
}

public RowMapperBuilder<T> indexMapper(ThrowingBiFunction<Row, Integer, T, SQLException> mapper) {
this.indexMapper = mapper;
return this;
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ dependencyResolutionManagement {
}
}
}
include("sadu-jackson-mapper")

0 comments on commit 6b62e50

Please sign in to comment.