From fe59e66e02c58e02b4ba15ccb42d2897e267812a Mon Sep 17 00:00:00 2001 From: Lilly <46890129+RainbowDashLabs@users.noreply.github.com> Date: Fri, 24 May 2024 00:16:38 +0200 Subject: [PATCH] Replace internal calls with reader implementations --- .../de/chojo/sadu/mapper/wrapper/Row.java | 114 +++++++----------- 1 file changed, 45 insertions(+), 69 deletions(-) diff --git a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/wrapper/Row.java b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/wrapper/Row.java index 21b339d6..6fd6d5b7 100644 --- a/sadu-mapper/src/main/java/de/chojo/sadu/mapper/wrapper/Row.java +++ b/sadu-mapper/src/main/java/de/chojo/sadu/mapper/wrapper/Row.java @@ -7,7 +7,6 @@ package de.chojo.sadu.mapper.wrapper; import de.chojo.sadu.core.conversion.ArrayConverter; -import de.chojo.sadu.core.conversion.UUIDConverter; import de.chojo.sadu.mapper.MapperConfig; import de.chojo.sadu.mapper.reader.StandardReader; import de.chojo.sadu.mapper.reader.ValueReader; @@ -36,13 +35,27 @@ 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.List; import java.util.Map; import java.util.UUID; +import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_DATE; +import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_DATE_TIME; +import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_TIME; +import static de.chojo.sadu.mapper.reader.StandardReader.OFFSET_DATE_TIME; +import static de.chojo.sadu.mapper.reader.StandardReader.OFFSET_TIME; +import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_BYTES; +import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_STRING; +import static de.chojo.sadu.mapper.reader.StandardReader.ZONED_DATE_TIME; +import static de.chojo.sadu.mapper.reader.StandardReader.localDate; +import static de.chojo.sadu.mapper.reader.StandardReader.localDateTime; +import static de.chojo.sadu.mapper.reader.StandardReader.localTime; +import static de.chojo.sadu.mapper.reader.StandardReader.offsetDateTime; +import static de.chojo.sadu.mapper.reader.StandardReader.offsetTime; +import static de.chojo.sadu.mapper.reader.StandardReader.zonedDateTime; + /** * Represents the row of a result set. Made to restrict actions on valid calls. */ @@ -132,11 +145,7 @@ public > T getEnum(int columnIndex, Class clazz) throws SQL */ @Deprecated public UUID getUuidFromString(int columnIndex) throws SQLException { - var value = resultSet.getString(columnIndex); - if (value == null) { - return null; - } - return UUID.fromString(value); + return get(columnIndex, StandardReader.UUID_FROM_STRING); } /** @@ -290,7 +299,7 @@ public byte[] getBytes(int columnIndex) throws SQLException { */ @Deprecated public UUID getUuidFromBytes(int columnIndex) throws SQLException { - return UUIDConverter.convert(resultSet.getBytes(columnIndex)); + return get(columnIndex, UUID_FROM_BYTES); } /** @@ -324,8 +333,7 @@ public Date getDate(int columnIndex) throws SQLException { */ @Deprecated public LocalDate getLocalDate(int columnIndex) throws SQLException { - var date = getDate(columnIndex); - return date == null ? null : date.toLocalDate(); + return get(columnIndex, LOCAL_DATE); } /** @@ -339,9 +347,7 @@ public LocalDate getLocalDate(int columnIndex) throws SQLException { * @throws SQLException if the columnIndex is not valid; * if a database access error occurs or this method is * called on a closed result set - * @deprecated Use {@link #get(String, ValueReader)} with one of the {@link StandardReader} */ - @Deprecated public Time getTime(int columnIndex) throws SQLException { return resultSet.getTime(columnIndex); } @@ -361,8 +367,7 @@ public Time getTime(int columnIndex) throws SQLException { */ @Deprecated public LocalTime getLocalTime(int columnIndex) throws SQLException { - var time = getTime(columnIndex); - return time != null ? time.toLocalTime() : null; + return get(columnIndex, LOCAL_TIME); } /** @@ -396,8 +401,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException { */ @Deprecated public LocalDateTime getLocalDateTime(int columnIndex) throws SQLException { - Timestamp timestamp = getTimestamp(columnIndex); - return timestamp == null ? null : timestamp.toLocalDateTime(); + return get(columnIndex, LOCAL_DATE_TIME); } /** @@ -415,8 +419,7 @@ public LocalDateTime getLocalDateTime(int columnIndex) throws SQLException { */ @Deprecated public ZonedDateTime getZonedDateTime(int columnIndex) throws SQLException { - var timestamp = getTimestamp(columnIndex); - return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault()); + return get(columnIndex, ZONED_DATE_TIME); } /** @@ -434,8 +437,7 @@ public ZonedDateTime getZonedDateTime(int columnIndex) throws SQLException { */ @Deprecated public OffsetDateTime getOffsetDateTime(int columnIndex) throws SQLException { - LocalDateTime localDateTime = getLocalDateTime(columnIndex); - return localDateTime == null ? null : OffsetDateTime.from(localDateTime); + return get(columnIndex, OFFSET_DATE_TIME); } /** @@ -453,8 +455,7 @@ public OffsetDateTime getOffsetDateTime(int columnIndex) throws SQLException { */ @Deprecated public OffsetTime getOffsetTime(int columnIndex) throws SQLException { - LocalTime localDateTime = getLocalTime(columnIndex); - return localDateTime == null ? null : OffsetTime.from(localDateTime); + return get(columnIndex, OFFSET_TIME); } /** @@ -547,11 +548,7 @@ public String getString(String columnLabel) throws SQLException { * @since 1.2 */ public > T getEnum(String columnLabel, Class clazz) throws SQLException { - var value = getString(columnLabel); - if (value == null) { - return null; - } - return Enum.valueOf(clazz, value); + return get(columnLabel, StandardReader.forEnum(clazz)); } /** @@ -569,11 +566,7 @@ public > T getEnum(String columnLabel, Class clazz) throws */ @Deprecated public UUID getUuidFromString(String columnLabel) throws SQLException { - var value = resultSet.getString(columnAlias(columnLabel)); - if (value == null) { - return null; - } - return UUID.fromString(value); + return get(columnLabel, UUID_FROM_STRING); } /** @@ -727,7 +720,7 @@ public byte[] getBytes(String columnLabel) throws SQLException { */ @Deprecated public UUID getUuidFromBytes(String columnLabel) throws SQLException { - return UUIDConverter.convert(resultSet.getBytes(columnAlias(columnLabel))); + return get(columnLabel, UUID_FROM_BYTES); } /** @@ -794,8 +787,7 @@ public Date getDate(String columnLabel) throws SQLException { */ @Deprecated public LocalDate getLocalDate(String columnLabel) throws SQLException { - var date = getDate(columnLabel); - return date == null ? null : date.toLocalDate(); + return get(columnLabel, LOCAL_DATE); } /** @@ -831,8 +823,7 @@ public Time getTime(String columnLabel) throws SQLException { */ @Deprecated public LocalTime getLocalTime(String columnLabel) throws SQLException { - var time = getTime(columnLabel); - return time != null ? time.toLocalTime() : null; + return get(columnLabel, LOCAL_TIME); } /** @@ -866,8 +857,7 @@ public Timestamp getTimestamp(String columnLabel) throws SQLException { */ @Deprecated public LocalDateTime getLocalDateTime(String columnLabel) throws SQLException { - Timestamp timestamp = getTimestamp(columnLabel); - return timestamp == null ? null : timestamp.toLocalDateTime(); + return get(columnLabel, LOCAL_DATE_TIME); } /** @@ -885,8 +875,7 @@ public LocalDateTime getLocalDateTime(String columnLabel) throws SQLException { */ @Deprecated public ZonedDateTime getZonedDateTime(String columnLabel) throws SQLException { - var timestamp = getTimestamp(columnLabel); - return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault()); + return get(columnLabel, ZONED_DATE_TIME); } /** @@ -904,8 +893,7 @@ public ZonedDateTime getZonedDateTime(String columnLabel) throws SQLException { */ @Deprecated public OffsetDateTime getOffsetDateTime(String columnLabel) throws SQLException { - var localDateTime = getTimestamp(columnLabel); - return localDateTime == null ? null : OffsetDateTime.from(localDateTime.toInstant()); + return get(columnLabel, OFFSET_DATE_TIME); } /** @@ -923,8 +911,7 @@ public OffsetDateTime getOffsetDateTime(String columnLabel) throws SQLException */ @Deprecated public OffsetTime getOffsetTime(String columnLabel) throws SQLException { - LocalTime localDateTime = getLocalTime(columnLabel); - return localDateTime == null ? null : OffsetTime.from(localDateTime); + return get(columnLabel, OFFSET_TIME); } /** @@ -1372,8 +1359,7 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException { */ @Deprecated public LocalDate getLocalDate(int columnIndex, Calendar cal) throws SQLException { - var date = getDate(columnIndex, cal); - return date == null ? null : date.toLocalDate(); + return get(columnIndex, localDate(cal)); } /** @@ -1419,8 +1405,7 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException { */ @Deprecated public LocalDate getLocalDate(String columnLabel, Calendar cal) throws SQLException { - var date = getDate(columnLabel, cal); - return date == null ? null : date.toLocalDate(); + return get(columnLabel, localDate(cal)); } /** @@ -1466,8 +1451,7 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException { */ @Deprecated public LocalTime getLocalTime(int columnIndex, Calendar cal) throws SQLException { - var time = getTime(columnIndex, cal); - return time != null ? time.toLocalTime() : null; + return get(columnIndex, localTime(cal)); } @@ -1514,8 +1498,7 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException { */ @Deprecated public LocalTime getLocalTime(String columnLabel, Calendar cal) throws SQLException { - var time = getTime(columnLabel, cal); - return time != null ? time.toLocalTime() : null; + return get(columnLabel, localTime(cal)); } /** @@ -1584,8 +1567,7 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept */ @Deprecated public LocalDateTime getLocalDateTime(int columnIndex, Calendar cal) throws SQLException { - Timestamp timestamp = getTimestamp(columnIndex, cal); - return timestamp == null ? null : timestamp.toLocalDateTime(); + return get(columnIndex, localDateTime(cal)); } @@ -1610,8 +1592,7 @@ public LocalDateTime getLocalDateTime(int columnIndex, Calendar cal) throws SQLE */ @Deprecated public LocalDateTime getLocalDateTime(String columnLabel, Calendar cal) throws SQLException { - Timestamp timestamp = getTimestamp(columnLabel, cal); - return timestamp == null ? null : timestamp.toLocalDateTime(); + return get(columnLabel, localDateTime(cal)); } /** @@ -1635,8 +1616,7 @@ public LocalDateTime getLocalDateTime(String columnLabel, Calendar cal) throws S */ @Deprecated public ZonedDateTime getZonedDateTime(int columnIndex, Calendar cal) throws SQLException { - var timestamp = getTimestamp(columnIndex, cal); - return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault()); + return get(columnIndex, zonedDateTime(cal)); } /** @@ -1660,8 +1640,7 @@ public ZonedDateTime getZonedDateTime(int columnIndex, Calendar cal) throws SQLE */ @Deprecated public ZonedDateTime getZonedDateTime(String columnLabel, Calendar cal) throws SQLException { - var timestamp = getTimestamp(columnLabel, cal); - return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault()); + return get(columnLabel, zonedDateTime(cal)); } /** @@ -1684,8 +1663,7 @@ public ZonedDateTime getZonedDateTime(String columnLabel, Calendar cal) throws S */ @Deprecated public OffsetDateTime getOffsetDateTime(int columnIndex, Calendar cal) throws SQLException { - LocalDateTime localDateTime = getLocalDateTime(columnIndex, cal); - return localDateTime == null ? null : OffsetDateTime.from(localDateTime); + return get(columnIndex, offsetDateTime(cal)); } /** @@ -1709,8 +1687,7 @@ public OffsetDateTime getOffsetDateTime(int columnIndex, Calendar cal) throws SQ */ @Deprecated public OffsetDateTime getOffsetDateTime(String columnLabel, Calendar cal) throws SQLException { - LocalDateTime localDateTime = getLocalDateTime(columnLabel, cal); - return localDateTime == null ? null : OffsetDateTime.from(localDateTime); + return get(columnLabel, offsetDateTime(cal)); } /** @@ -1734,8 +1711,7 @@ public OffsetDateTime getOffsetDateTime(String columnLabel, Calendar cal) throws */ @Deprecated public OffsetTime getOffsetTime(int columnIndex, Calendar cal) throws SQLException { - LocalTime localDateTime = getLocalTime(columnIndex, cal); - return localDateTime == null ? null : OffsetTime.from(localDateTime); + return get(columnIndex, offsetTime(cal)); } /** @@ -1759,8 +1735,7 @@ public OffsetTime getOffsetTime(int columnIndex, Calendar cal) throws SQLExcepti */ @Deprecated public OffsetTime getOffsetTime(String columnLabel, Calendar cal) throws SQLException { - LocalTime localDateTime = getLocalTime(columnLabel, cal); - return localDateTime == null ? null : OffsetTime.from(localDateTime); + return get(columnLabel, offsetTime(cal)); } /** @@ -2089,6 +2064,7 @@ public T get(String columnLabel, ValueReader reader) throws SQLExce */ public T get(int index, ValueReader reader) throws SQLException { V value = reader.indexedReader().apply(this, index); + if (value == null) return null; return reader.parser().apply(value); }