Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonbSetColumnMapperFactory #1026

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dc-commons-jdbi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Added

- `JsonbSetColumnMapperFactory` and `JsonbSetArgumentFactory` for JSONB columns
that contain a set (just an array in JSON though)

## [7.0.3](https://github.com/dbmdz/digitalcollections-commons/releases/tag/dc-commons-jdbi-7.0.3) - 2023-09-15

### Changed
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-jdbi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>dc-commons-jdbi</artifactId>
<version>7.0.3</version>
<version>7.0.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>DigitalCollections: Commons JDBI</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.digitalcollections.commons.jdbi;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Optional;
import java.util.Set;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.argument.ArgumentFactory;
import org.jdbi.v3.core.argument.NullArgument;
import org.jdbi.v3.core.config.ConfigRegistry;

public class JsonbSetArgumentFactory<T> implements ArgumentFactory {

private final Class<T> clz;
private final ObjectMapper objectMapper;

public JsonbSetArgumentFactory(Class<T> clz, ObjectMapper objectMapper) {
this.clz = clz;
this.objectMapper = objectMapper;
}

protected Argument build(Set<T> value, ConfigRegistry config) {
return (i, p, cx) -> {
if (value == null) {
p.setNull(i, Types.OTHER);
} else {
try {
p.setString(i, objectMapper.writeValueAsString(value));
} catch (IOException ex) {
throw new SQLException(ex);
}
}
};
}

@Override
@SuppressWarnings("unchecked")
public Optional<Argument> build(Type type, Object value, ConfigRegistry config) {
if (!(type instanceof ParameterizedType)) {
return Optional.empty();
}

Type setType = ((ParameterizedType) type).getActualTypeArguments()[0];

if (!clz.equals(setType)) {
return Optional.empty();
}
return Optional.of(
value == null ? new NullArgument(Types.OTHER) : build((Set<T>) value, config));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.digitalcollections.commons.jdbi;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.Set;
import org.jdbi.v3.core.config.ConfigRegistry;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.mapper.ColumnMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonbSetColumnMapperFactory<T> implements ColumnMapperFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonbSetColumnMapperFactory.class);

private final Class clz;
private final ObjectMapper objectMapper;

public JsonbSetColumnMapperFactory(Class<T> clz, ObjectMapper objectMapper) {
this.clz = clz;
this.objectMapper = objectMapper;
}

@Override
@SuppressWarnings("unchecked")
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
if (!(type instanceof ParameterizedType)) {
return Optional.empty();
}

Type setType = ((ParameterizedType) type).getActualTypeArguments()[0];
if (!clz.equals(setType)) {
return Optional.empty();
}
return Optional.of(
(r, i, c) -> {
String jsonb = r.getString(i);
if (jsonb == null) {
return null;
}
/* see https://stackoverflow.com/a/11681540 */
try {
JavaType javaType =
objectMapper.getTypeFactory().constructParametricType(Set.class, (Class) setType);

return objectMapper.readValue(jsonb, javaType);
} catch (IOException ex) {
LOGGER.error("IO error deserializing JSON: " + ex, ex);
return null;
} catch (Exception ex) {
LOGGER.error("Error deserializing JSON: " + ex, ex);
return null;
}
});
}
}