Skip to content

Commit

Permalink
Feature: ValueReader & ValueConverter (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs authored May 25, 2024
1 parent 430818a commit a0fcb4e
Show file tree
Hide file tree
Showing 19 changed files with 1,118 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.UUID;
import java.util.stream.Collectors;

import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_BYTES;
import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_STRING;

public final class DefaultMapper {
private DefaultMapper() {
throw new UnsupportedOperationException("This is a utility class.");
Expand Down Expand Up @@ -70,15 +73,15 @@ public static RowMapper<UUID> createUuid(List<SqlType> textTypes, List<SqlType>
var meta = row.getMetaData();
var columnIndexOfType = Results.getFirstColumnIndexOfType(meta, textTypes);
if (columnIndexOfType.isPresent()) {
return row.getUuidFromString(columnIndexOfType.get());
return row.get(columnIndexOfType.get(), UUID_FROM_STRING);
}
columnIndexOfType = Results.getFirstColumnIndexOfType(meta, byteTypes);
var index = columnIndexOfType.orElseThrow(() -> {
List<SqlType> sqlTypes = new ArrayList<>(textTypes);
sqlTypes.addAll(byteTypes);
return createException(sqlTypes, meta);
});
return row.getUuidFromBytes(index);
return row.get(index, UUID_FROM_BYTES);
})
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.mapper.reader;

import de.chojo.sadu.core.conversion.UUIDConverter;
import de.chojo.sadu.mapper.wrapper.Row;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.UUID;

/**
* Default implementations of {@link ValueReader}.
*/
public final class StandardReader {
public static final ValueReader<Instant, Long> INSTANT_FROM_MILLIS = ValueReader.create(Instant::ofEpochMilli, Row::getLong, Row::getLong);
public static final ValueReader<Instant, Long> INSTANT_FROM_SECONDS = ValueReader.create(Instant::ofEpochSecond, Row::getLong, Row::getLong);
public static final ValueReader<Instant, Timestamp> INSTANT_FROM_TIMESTAMP = ValueReader.create(Timestamp::toInstant, Row::getTimestamp, Row::getTimestamp);
public static final ValueReader<LocalDateTime, Timestamp> LOCAL_DATE_TIME = ValueReader.create(Timestamp::toLocalDateTime, Row::getTimestamp, Row::getTimestamp);
public static final ValueReader<OffsetDateTime, Timestamp> OFFSET_DATE_TIME = ValueReader.create(t -> OffsetDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault()), Row::getTimestamp, Row::getTimestamp);
public static final ValueReader<ZonedDateTime, Timestamp> ZONED_DATE_TIME = ValueReader.create(t -> t.toInstant().atZone(ZoneId.systemDefault()), Row::getTimestamp, Row::getTimestamp);
public static final ValueReader<OffsetTime, Time> OFFSET_TIME = ValueReader.create(t -> t.toLocalTime().atOffset(OffsetTime.now().getOffset()), Row::getTime, Row::getTime);
public static final ValueReader<LocalDate, Date> LOCAL_DATE = ValueReader.create(Date::toLocalDate, Row::getDate, Row::getDate);
public static final ValueReader<LocalTime, Time> LOCAL_TIME = ValueReader.create(Time::toLocalTime, Row::getTime, Row::getTime);
public static final ValueReader<UUID, String> UUID_FROM_STRING = ValueReader.create(UUID::fromString, Row::getString, Row::getString);
public static final ValueReader<UUID, byte[]> UUID_FROM_BYTES = ValueReader.create(UUIDConverter::convert, Row::getBytes, Row::getBytes);

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<LocalDate, Date> localDate(Calendar calendar) {
return ValueReader.create(Date::toLocalDate, (row, name) -> row.getDate(name, calendar), (row, integer) -> row.getDate(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<LocalTime, Time> localTime(Calendar calendar) {
return ValueReader.create(Time::toLocalTime, (row, name) -> row.getTime(name, calendar), (row, integer) -> row.getTime(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<OffsetTime, Time> offsetTime(Calendar calendar) {
return ValueReader.create(t -> t.toLocalTime().atOffset(OffsetTime.now().getOffset()), (row, name) -> row.getTime(name, calendar), (row, integer) -> row.getTime(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<LocalDateTime, Timestamp> localDateTime(Calendar calendar) {
return ValueReader.create(Timestamp::toLocalDateTime, (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<OffsetDateTime, Timestamp> offsetDateTime(Calendar calendar) {
return ValueReader.create(t -> OffsetDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault()), (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader<ZonedDateTime, Timestamp> zonedDateTime(Calendar calendar) {
return ValueReader.create(t -> t.toInstant().atZone(ZoneId.systemDefault()), (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}

@Contract(value = "_ -> new", pure = true)
public static <T extends Enum<T>> @NotNull ValueReader<T, String> forEnum(Class<T> clazz) {
return ValueReader.create(v -> Enum.valueOf(clazz, v), Row::getString, Row::getString);
}
}
103 changes: 103 additions & 0 deletions sadu-mapper/src/main/java/de/chojo/sadu/mapper/reader/ValueReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.mapper.reader;

import de.chojo.sadu.core.exceptions.ThrowingBiFunction;
import de.chojo.sadu.mapper.wrapper.Row;
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Definition of a ValueReader to read columns from a {@link Row} and parse it to a java type.
*
* @param <T> The java type that is returned by the adapter
* @param <V> The intermediate SQL type that is retrieved from the row
* @see StandardReader
*/
public interface ValueReader<T, V> {
static <V, T> ValueReader<T, V> create(Function<V, T> parser,
ThrowingBiFunction<Row, String, V, SQLException> nameReader,
ThrowingBiFunction<Row, Integer, V, SQLException> indexReader) {
return new ValueReader<>() {
@Override
public Function<@NotNull V, T> reader() {
return parser;
}

@Override
public ThrowingBiFunction<Row, String, V, SQLException> namedReader() {
return nameReader;
}

@Override
public ThrowingBiFunction<Row, Integer, V, SQLException> indexedReader() {
return indexReader;
}
};
}

static <V, T> ValueReader<T, V> create(Function<V, T> parser,
ThrowingBiFunction<Row, String, V, SQLException> nameReader,
ThrowingBiFunction<Row, Integer, V, SQLException> indexReader,
Supplier<T> defaultValue) {
return new ValueReader<>() {
@Override
public Function<@NotNull V, T> reader() {
return parser;
}

@Override
public ThrowingBiFunction<Row, String, V, SQLException> namedReader() {
return nameReader;
}

@Override
public ThrowingBiFunction<Row, Integer, V, SQLException> indexedReader() {
return indexReader;
}

@Override
public T defaultValue() {
return defaultValue.get();
}
};
}

/**
* A reader that takes the sql type and converts it into a java type.
* The passed sql type instance is never null.
*
* @return a converted java instance of the sql object
*/
Function<@NotNull V, T> reader();

/**
* Function that provides access to a reader that returns the column value via column name
*
* @return function
*/
ThrowingBiFunction<Row, String, V, SQLException> namedReader();

/**
* Function that provides access to a reader that returns the column value via column index
*
* @return function
*/
ThrowingBiFunction<Row, Integer, V, SQLException> indexedReader();

/**
* A default value that should be returned when the SQL value is null
*
* @return a default value or null
*/
default T defaultValue() {
return null;
}
}
Loading

0 comments on commit a0fcb4e

Please sign in to comment.