diff --git a/sadu-jackson-mapper/build.gradle.kts b/sadu-jackson-mapper/build.gradle.kts new file mode 100644 index 00000000..b01f6ce5 --- /dev/null +++ b/sadu-jackson-mapper/build.gradle.kts @@ -0,0 +1,4 @@ +dependencies { + api(project(":sadu-mapper")) + api("com.fasterxml.jackson.core", "jackson-databind", "2.17.1") +} diff --git a/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/ResultSetMapper.java b/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/ResultSetMapper.java new file mode 100644 index 00000000..8066a7bf --- /dev/null +++ b/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/ResultSetMapper.java @@ -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 convertRow(Row row, Class clazz) throws SQLException { + ResultSetMetaData meta = row.getMetaData(); + Map 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> 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); + } +} diff --git a/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/exception/UnknownTypeException.java b/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/exception/UnknownTypeException.java new file mode 100644 index 00000000..c1daaa5e --- /dev/null +++ b/sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/exception/UnknownTypeException.java @@ -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."); + } +} diff --git a/sadu-jackson-mapper/src/test/java/de/chojo/sadu/jackson/ResultSetMapperTest.java b/sadu-jackson-mapper/src/test/java/de/chojo/sadu/jackson/ResultSetMapperTest.java new file mode 100644 index 00000000..238dcb9a --- /dev/null +++ b/sadu-jackson-mapper/src/test/java/de/chojo/sadu/jackson/ResultSetMapperTest.java @@ -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() { + } +} diff --git a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/DefaultMapper.java b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/DefaultMapper.java index 425e5512..3a80609e 100644 --- a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/DefaultMapper.java +++ b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/DefaultMapper.java @@ -93,7 +93,10 @@ public static RowMapper create(Class clazz, ThrowingBiFunction createException(types, meta)); return mapper.apply(row, index); - }).build(); + }) + .indexMapper(mapper) + .types(types) + .build(); } private static SQLException createException(List types, ResultSetMetaData meta) { diff --git a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/RowMapperRegistry.java b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/RowMapperRegistry.java index 23518c13..ee5dade8 100644 --- a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/RowMapperRegistry.java +++ b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/RowMapperRegistry.java @@ -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; @@ -26,6 +27,7 @@ */ public class RowMapperRegistry { private final Map, List>> mapper = new HashMap<>(); + private final Map>> types = new HashMap<>(); public RowMapperRegistry() { } @@ -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. *

@@ -178,4 +193,8 @@ public RowMapper findOrWildcard(Class clazz, ResultSetMetaData meta, M } throw MappingException.create(clazz, meta); } + + public Optional> findForType(String name) { + return Optional.ofNullable(types.get(name).get(0)); + } } diff --git a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapper.java b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapper.java index 19d783b4..cf2e970e 100644 --- a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapper.java +++ b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapper.java @@ -6,6 +6,8 @@ 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; @@ -13,7 +15,9 @@ 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; @@ -29,11 +33,15 @@ public class RowMapper implements RowMapping { private final Class clazz; private final RowMapping mapper; private final Set columns; + private final List types; + private final ThrowingBiFunction indexMapper; - RowMapper(Class clazz, RowMapping mapper, Set columns) { + RowMapper(Class clazz, RowMapping mapper, ThrowingBiFunction indexMapper, Set columns, List types) { this.clazz = clazz; this.mapper = mapper; this.columns = columns; + this.types = types; + this.indexMapper = indexMapper; } public static PartialRowMapper forClass(Class clazz) { @@ -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(); } @@ -131,6 +145,9 @@ public int applicable(ResultSetMetaData meta, MapperConfig config) { return overlap.size(); } + public List types() { + return Collections.unmodifiableList(types); + } @Override public String toString() { diff --git a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapperBuilder.java b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapperBuilder.java index 7ba7e48d..de1186e6 100644 --- a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapperBuilder.java +++ b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/rowmapper/RowMapperBuilder.java @@ -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; @@ -19,6 +24,8 @@ public class RowMapperBuilder implements PartialRowMapper { private final Class clazz; private final Set columns = new HashSet<>(); private RowMapping mapper; + private List types; + private ThrowingBiFunction indexMapper; RowMapperBuilder(Class clazz) { this.clazz = clazz; @@ -58,6 +65,16 @@ public RowMapperBuilder addColumns(String... columns) { * @return new RowMapper instance */ public RowMapper build() { - return new RowMapper<>(clazz, mapper, columns); + return new RowMapper<>(clazz, mapper, indexMapper, columns, types); + } + + public RowMapperBuilder types(List types) { + this.types = types; + return this; + } + + public RowMapperBuilder indexMapper(ThrowingBiFunction mapper) { + this.indexMapper = mapper; + return this; } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 0fe599af..fba40d18 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -72,3 +72,4 @@ dependencyResolutionManagement { } } } +include("sadu-jackson-mapper")