Skip to content

Commit

Permalink
Add Nullable annotation to KiwiJdbc methods that can return null (#1179)
Browse files Browse the repository at this point in the history
Closes #1178
Closes #1180
  • Loading branch information
sleberknight authored Aug 9, 2024
1 parent 56ac40d commit 0e4f3d8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static long epochMillisFromTimestamp(Timestamp timestamp) {
* @return the converted Date or {@code null} if the column was {@code NULL}
* @throws SQLException if there is a database problem
*/
@Nullable
public static Date dateFromTimestamp(ResultSet rs, String columnName) throws SQLException {
return dateFromTimestamp(rs.getTimestamp(columnName));
}
Expand All @@ -123,6 +124,7 @@ public static Date dateFromTimestamp(ResultSet rs, String columnName) throws SQL
* @param timestamp the timestamp to convert
* @return a {@link Date} or null if the timestamp is null
*/
@Nullable
public static Date dateFromTimestamp(Timestamp timestamp) {
return isNull(timestamp) ? null : Date.from(timestamp.toInstant());
}
Expand All @@ -135,6 +137,7 @@ public static Date dateFromTimestamp(Timestamp timestamp) {
* @return the converted Instant or {@code null} if the column was {@code NULL}
* @throws SQLException if there is a database problem
*/
@Nullable
public static Instant instantFromTimestamp(ResultSet rs, String columnName) throws SQLException {
return instantFromTimestamp(rs.getTimestamp(columnName));
}
Expand All @@ -145,6 +148,7 @@ public static Instant instantFromTimestamp(ResultSet rs, String columnName) thro
* @param timestamp the timestamp to convert
* @return an Instant or {@code null} if the timestamp is {@code null}
*/
@Nullable
public static Instant instantFromTimestamp(Timestamp timestamp) {
return isNull(timestamp) ? null : timestamp.toInstant();
}
Expand All @@ -157,6 +161,7 @@ public static Instant instantFromTimestamp(Timestamp timestamp) {
* @return the converted LocalDateTime or {@code null} if the column was {@code NULL}
* @throws SQLException if there is a database problem
*/
@Nullable
public static LocalDateTime localDateTimeFromTimestamp(ResultSet rs, String columnName) throws SQLException {
return localDateTimeFromTimestamp(rs.getTimestamp(columnName));
}
Expand All @@ -167,6 +172,7 @@ public static LocalDateTime localDateTimeFromTimestamp(ResultSet rs, String colu
* @param timestamp the timestamp to convert
* @return a LocalDateTime or {@code null} if the timestamp is {@code null}
*/
@Nullable
public static LocalDateTime localDateTimeFromTimestamp(Timestamp timestamp) {
return isNull(timestamp) ? null : timestamp.toLocalDateTime();
}
Expand Down Expand Up @@ -203,6 +209,7 @@ public static LocalDate localDateFromDateOrNull(java.sql.@Nullable Date date) {
* @return a UTC ZonedDateTime or {@code null} if the column was {@code NULL}
* @throws SQLException if there is a database problem
*/
@Nullable
public static ZonedDateTime utcZonedDateTimeFromTimestamp(ResultSet rs, String columnName)
throws SQLException {
return zonedDateTimeFromTimestamp(rs, columnName, ZoneOffset.UTC);
Expand All @@ -217,6 +224,7 @@ public static ZonedDateTime utcZonedDateTimeFromTimestamp(ResultSet rs, String c
* @return a ZonedDateTime in the specified zone, or {@code null} if the column was {@code NULL}
* @throws SQLException if there is a database problem
*/
@Nullable
public static ZonedDateTime zonedDateTimeFromTimestamp(ResultSet rs, String columnName, ZoneId zoneId)
throws SQLException {
return zonedDateTimeFromTimestamp(rs.getTimestamp(columnName), zoneId);
Expand All @@ -228,6 +236,7 @@ public static ZonedDateTime zonedDateTimeFromTimestamp(ResultSet rs, String colu
* @param timestamp the timestamp to convert
* @return a UTC ZonedDateTime or {@code null} if the timestamp is {@code null}
*/
@Nullable
public static ZonedDateTime utcZonedDateTimeFromTimestamp(Timestamp timestamp) {
return zonedDateTimeFromTimestamp(timestamp, ZoneOffset.UTC);
}
Expand All @@ -251,6 +260,7 @@ public static ZonedDateTime utcZonedDateTimeFromEpochMilli(long epochMilli) {
* @param zoneId the time zone ID
* @return a ZonedDateTime in the specified zone, or {@code null} if the timestamp is {@code null}
*/
@Nullable
public static ZonedDateTime zonedDateTimeFromTimestamp(Timestamp timestamp, ZoneId zoneId) {
return isNull(timestamp) ? null : ZonedDateTime.ofInstant(timestamp.toInstant(), zoneId);
}
Expand All @@ -259,8 +269,9 @@ public static ZonedDateTime zonedDateTimeFromTimestamp(Timestamp timestamp, Zone
* Returns a {@link Timestamp} from the given {@link Instant}.
*
* @param instant the Instant to convert
* @return an Instant or {@code null} if the instant is {@code null}
* @return a Timestamp or {@code null} if the instant is {@code null}
*/
@Nullable
public static Timestamp timestampFromInstant(Instant instant) {
return isNull(instant) ? null : Timestamp.from(instant);
}
Expand All @@ -269,8 +280,9 @@ public static Timestamp timestampFromInstant(Instant instant) {
* Returns a {@link Timestamp} from the given {@link ZonedDateTime}.
*
* @param zonedDateTime the ZonedDateTime to convert
* @return a ZonedDateTime or {@code null} if the zonedDateTime is {@code null}
* @return a Timestamp or {@code null} if the zonedDateTime is {@code null}
*/
@Nullable
public static Timestamp timestampFromZonedDateTime(ZonedDateTime zonedDateTime) {
return isNull(zonedDateTime) ? null : Timestamp.from(zonedDateTime.toInstant());
}
Expand All @@ -284,6 +296,7 @@ public static Timestamp timestampFromZonedDateTime(ZonedDateTime zonedDateTime)
* @return a {@link Long} or {@code null}
* @throws SQLException if there is a database problem
*/
@Nullable
public static Long longValueOrNull(ResultSet rs, String columnName) throws SQLException {
var value = rs.getLong(columnName);
return rs.wasNull() ? null : value;
Expand All @@ -298,6 +311,7 @@ public static Long longValueOrNull(ResultSet rs, String columnName) throws SQLEx
* @return an {@link Integer} or {@code null}
* @throws SQLException if there is a database problem
*/
@Nullable
public static Integer intValueOrNull(ResultSet rs, String columnName) throws SQLException {
var value = rs.getInt(columnName);
return rs.wasNull() ? null : value;
Expand All @@ -309,9 +323,10 @@ public static Integer intValueOrNull(ResultSet rs, String columnName) throws SQL
*
* @param rs the ResultSet
* @param columnName the column name
* @return a {@link Long} or {@code null}
* @return a {@link Double} or {@code null}
* @throws SQLException if there is a database problem
*/
@Nullable
public static Double doubleValueOrNull(ResultSet rs, String columnName) throws SQLException {
var value = rs.getDouble(columnName);
return rs.wasNull() ? null : value;
Expand All @@ -329,6 +344,7 @@ public static Double doubleValueOrNull(ResultSet rs, String columnName) throws S
* @throws IllegalArgumentException if the value from the ResultSet is an invalid enum constant
* @see Enum#valueOf(Class, String)
*/
@Nullable
public static <T extends Enum<T>> T enumValueOrNull(ResultSet rs, String columnName, Class<T> enumType)
throws SQLException {
return enumValueOrEmpty(rs, columnName, enumType).orElse(null);
Expand Down

0 comments on commit 0e4f3d8

Please sign in to comment.