-
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.
- Loading branch information
Showing
28 changed files
with
1,155 additions
and
108 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
Binary file not shown.
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
78 changes: 78 additions & 0 deletions
78
sadu-mapper/src/main/java/de/chojo/sadu/mapper/reader/StandardReader.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,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
103
sadu-mapper/src/main/java/de/chojo/sadu/mapper/reader/ValueReader.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,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; | ||
} | ||
} |
Oops, something went wrong.