-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft for jackson mapping of a row onto an object
- Loading branch information
1 parent
a1ce936
commit 6b62e50
Showing
9 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
40 changes: 40 additions & 0 deletions
40
sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/ResultSetMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sadu-jackson-mapper/src/main/java/de/chojo/sadu/jackson/exception/UnknownTypeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
sadu-jackson-mapper/src/test/java/de/chojo/sadu/jackson/ResultSetMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,4 @@ dependencyResolutionManagement { | |
} | ||
} | ||
} | ||
include("sadu-jackson-mapper") |