Skip to content

Commit

Permalink
Release 2.2.0 (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed May 27, 2024
2 parents c8addb1 + 11c10ca commit c7dd962
Show file tree
Hide file tree
Showing 28 changed files with 1,155 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {

publishData {
useEldoNexusRepos(false)
publishingVersion = "2.1.0"
publishingVersion = "2.2.0"
}

group = "de.chojo.sadu"
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
6 changes: 3 additions & 3 deletions sadu-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ dependencies {
compileOnly(project(":sadu-postgresql"))

// database driver
compileOnly("org.xerial", "sqlite-jdbc", "3.45.1.0")
compileOnly("org.postgresql", "postgresql", "42.7.2")
compileOnly("org.mariadb.jdbc", "mariadb-java-client", "3.3.3")
compileOnly("org.xerial", "sqlite-jdbc", "3.45.3.0")
compileOnly("org.postgresql", "postgresql", "42.7.3")
compileOnly("org.mariadb.jdbc", "mariadb-java-client", "3.4.0")
compileOnly("mysql", "mysql-connector-java", "8.0.33")
}
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 c7dd962

Please sign in to comment.