remove java.time.Instant

This commit is contained in:
Jörg Prante 2025-03-18 20:29:12 +01:00
parent d01695aff4
commit 5a1c5af6a5
12 changed files with 105 additions and 162 deletions

View file

@ -12,6 +12,7 @@ import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -477,6 +478,7 @@ public class DatabaseImpl implements Database {
case Boolean b -> sql.argBoolean(k, b);
case LocalDate localDate -> sql.argLocalDate(k, localDate);
case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
case OffsetDateTime offsetDateTime -> sql.argOffsetDateTime(k, offsetDateTime);
case null, default ->
throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null"));
}
@ -496,6 +498,7 @@ public class DatabaseImpl implements Database {
case Boolean b -> sql.argBoolean(k, b);
case LocalDate localDate -> sql.argLocalDate(k, localDate);
case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
case OffsetDateTime offsetDateTime -> sql.argOffsetDateTime(k, offsetDateTime);
case null -> sql.argNull(k);
default -> throw new DatabaseException("unknown type for param: " + v.getClass());
}
@ -515,6 +518,7 @@ public class DatabaseImpl implements Database {
case Boolean b -> sql.argBoolean(k, b);
case LocalDate localDate -> sql.argLocalDate(k, localDate);
case LocalDateTime localDateTime -> sql.argLocalDateTime(k, localDateTime);
case OffsetDateTime offsetDateTime -> sql.argOffsetDateTime(k, offsetDateTime);
case null -> sql.argNull(k);
default -> throw new DatabaseException("unknown type for param: " + v.getClass());
}

View file

@ -4,7 +4,6 @@ import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -356,12 +355,6 @@ public interface Row {
InputStream getBlobInputStreamOrEmpty(String columnName);
Instant getInstantOrNull();
Instant getInstantOrNull(int columnOneBased);
Instant getInstantOrNull(String columnName);
LocalDateTime getLocalDateTimeOrNull();
LocalDateTime getLocalDateTimeOrNull(int columnOneBased);

View file

@ -9,7 +9,6 @@ import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -655,31 +654,6 @@ class RowsAdapter implements Rows {
return result;
}
@Override
public Instant getInstantOrNull() {
return getInstantOrNull(column++);
}
@Override
public Instant getInstantOrNull(int columnOneBased) {
try {
column = columnOneBased + 1;
return rs.getObject(columnOneBased, Instant.class);
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
@Override
public Instant getInstantOrNull(String columnName) {
try {
column = rs.findColumn(columnName) + 1;
return rs.getObject(columnName, Instant.class);
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
@Override
public LocalDateTime getLocalDateTimeOrNull() {
return getLocalDateTimeOrNull(column++);

View file

@ -3,7 +3,6 @@ package org.xbib.jdbc.query;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -41,10 +40,6 @@ public interface SqlInsert {
SqlInsert argString( String argName, String arg);
SqlInsert argInstant(Instant arg);
SqlInsert argInstant(String argName, Instant arg);
SqlInsert argLocalDateTime(LocalDateTime arg);
SqlInsert argLocalDateTime(String argName, LocalDateTime arg);
@ -61,6 +56,10 @@ public interface SqlInsert {
SqlInsert argLocalDateTimeNowPerDb(String argName);
SqlInsert argOffsetDateTimeNowPerDb();
SqlInsert argOffsetDateTimeNowPerDb(String argName);
SqlInsert argBlobBytes(byte[] arg);
SqlInsert argBlobBytes(String argName, byte[] arg);

View file

@ -13,7 +13,6 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -142,16 +141,6 @@ public class SqlInsertImpl implements SqlInsert {
return namedArg(argName, adaptor.nullString(arg));
}
@Override
public SqlInsert argInstant(Instant arg) {
return positionalArg(adaptor.nullInstant(arg));
}
@Override
public SqlInsert argInstant(String argName, Instant arg) {
return namedArg(argName, adaptor.nullInstant(arg));
}
@Override
public SqlInsert argLocalDateTime(LocalDateTime arg) {
return positionalArg(adaptor.nullLocalDateTime(arg));
@ -198,6 +187,22 @@ public class SqlInsertImpl implements SqlInsert {
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlInsert argOffsetDateTimeNowPerDb() {
if (options.useClientClock()) {
return positionalArg(adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return positionalArg(new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlInsert argOffsetDateTimeNowPerDb(String argName) {
if (options.useClientClock()) {
return namedArg(argName, adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlInsert argBlobBytes(byte[] arg) {
return positionalArg(adaptor.nullBytes(arg));

View file

@ -1,7 +1,6 @@
package org.xbib.jdbc.query;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -40,10 +39,6 @@ public interface SqlSelect {
SqlSelect argString( String argName, String arg);
SqlSelect argInstant(Instant arg);
SqlSelect argInstant(String argName, Instant arg);
SqlSelect argLocalDateTime(LocalDateTime arg);
SqlSelect argLocalDateTime(String argName, LocalDateTime arg);
@ -56,14 +51,14 @@ public interface SqlSelect {
SqlSelect argLocalDate(String argName, LocalDate arg);
SqlSelect argInstantNowPerDb();
SqlSelect argInstantNowPerDb(String argName);
SqlSelect argLocalDateTimeNowPerDb();
SqlSelect argLocalDateTimeNowPerDb(String argName);
SqlSelect argOffsetDateTimeNowPerDb();
SqlSelect argOffsetDateTimeNowPerDb(String argName);
SqlSelect withTimeoutSeconds(int seconds);
SqlSelect withMaxRows(int rows);
@ -126,14 +121,14 @@ public interface SqlSelect {
*/
List<String> queryStrings();
Instant queryInstantOrNull();
List<Instant> queryInstants();
LocalDateTime queryLocalDateTimeOrNull();
List<LocalDateTime> queryLocalDateTimes();
OffsetDateTime queryOffsetDateTimeOrNull();
List<OffsetDateTime> queryOffsetDateTimes();
LocalDate queryLocalDateOrNull();
List<LocalDate> queryLocalDates();

View file

@ -9,7 +9,6 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -136,16 +135,6 @@ public class SqlSelectImpl implements SqlSelect {
return namedArg(argName, adaptor.nullString(arg));
}
@Override
public SqlSelect argInstant(Instant arg) {
return positionalArg(adaptor.nullInstant(arg));
}
@Override
public SqlSelect argInstant(String argName, Instant arg) {
return namedArg(argName, adaptor.nullInstant(arg));
}
@Override
public SqlSelect argLocalDateTime(LocalDateTime arg) {
return positionalArg(adaptor.nullLocalDateTime(arg));
@ -176,22 +165,6 @@ public class SqlSelectImpl implements SqlSelect {
return namedArg(argName, adaptor.nullLocalDate(arg));
}
@Override
public SqlSelect argInstantNowPerDb() {
if (options.useClientClock()) {
return positionalArg(adaptor.nullInstant(Instant.now()));
}
return positionalArg(new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlSelect argInstantNowPerDb(String argName) {
if (options.useClientClock()) {
return namedArg(argName, adaptor.nullInstant(Instant.now()));
}
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlSelect argLocalDateTimeNowPerDb() {
if (options.useClientClock()) {
@ -207,7 +180,23 @@ public class SqlSelectImpl implements SqlSelect {
}
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlSelect argOffsetDateTimeNowPerDb() {
if (options.useClientClock()) {
return positionalArg(adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return positionalArg(new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlSelect argOffsetDateTimeNowPerDb(String argName) {
if (options.useClientClock()) {
return namedArg(argName, adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlSelect withTimeoutSeconds(int seconds) {
timeoutSeconds = seconds;
@ -467,31 +456,6 @@ public class SqlSelectImpl implements SqlSelect {
});
}
@Override
public Instant queryInstantOrNull() {
return queryWithTimeout(rs -> {
if (rs.next()) {
return rs.getInstantOrNull(1);
}
return null;
});
}
@Override
public List<Instant> queryInstants() {
return queryWithTimeout(rs -> {
List<Instant> result = new ArrayList<>();
while (rs.next()) {
Instant value = rs.getInstantOrNull(1);
if (value != null) {
result.add(value);
}
}
return result;
});
}
@Override
public LocalDateTime queryLocalDateTimeOrNull() {
return queryWithTimeout(rs -> {
@ -516,6 +480,16 @@ public class SqlSelectImpl implements SqlSelect {
});
}
@Override
public OffsetDateTime queryOffsetDateTimeOrNull() {
return null;
}
@Override
public List<OffsetDateTime> queryOffsetDateTimes() {
return List.of();
}
@Override
public LocalDate queryLocalDateOrNull() {
return queryWithTimeout(rs -> {

View file

@ -5,6 +5,7 @@ import java.io.Reader;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
/**
* Interface for configuring (setting parameters) and executing a chunk of SQL.
@ -43,6 +44,10 @@ public interface SqlUpdate {
SqlUpdate argLocalDateTime(String argName, LocalDateTime arg);
SqlUpdate argOffsetDateTime(OffsetDateTime arg);
SqlUpdate argOffsetDateTime(String argName, OffsetDateTime arg);
SqlUpdate argLocalDate(LocalDate arg);
SqlUpdate argLocalDate(String argName, LocalDate arg);
@ -51,6 +56,10 @@ public interface SqlUpdate {
SqlUpdate argLocalDateTimeNowPerDb(String argName);
SqlUpdate argOffsetDateTimePerDb();
SqlUpdate argOffsetDateTimePerDb(String argName);
SqlUpdate argBlobBytes(byte[] arg);
SqlUpdate argBlobBytes(String argName, byte[] arg);

View file

@ -2,6 +2,7 @@ package org.xbib.jdbc.query;
import java.sql.Statement;
import java.sql.Types;
import java.time.OffsetDateTime;
import org.xbib.jdbc.query.util.DebugSql;
import org.xbib.jdbc.query.util.InternalStringReader;
import org.xbib.jdbc.query.util.Metric;
@ -27,7 +28,7 @@ import java.util.logging.Logger;
*/
public class SqlUpdateImpl implements SqlUpdate {
private static final Logger log = Logger.getLogger(Database.class.getName());
private static final Logger logger = Logger.getLogger(SqlUpdateImpl.class.getName());
private final Connection connection;
@ -130,6 +131,16 @@ public class SqlUpdateImpl implements SqlUpdate {
return namedArg(argName, adaptor.nullLocalDateTime(arg));
}
@Override
public SqlUpdate argOffsetDateTime(OffsetDateTime arg) {
return positionalArg(adaptor.nullOffsetDateTime(arg));
}
@Override
public SqlUpdate argOffsetDateTime(String argName, OffsetDateTime arg) {
return namedArg(argName, adaptor.nullOffsetDateTime(arg));
}
@Override
public SqlUpdate argLocalDate(LocalDate arg) {
return positionalArg(adaptor.nullLocalDate(arg));
@ -156,6 +167,22 @@ public class SqlUpdateImpl implements SqlUpdate {
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlUpdate argOffsetDateTimePerDb() {
if (options.useClientClock()) {
return positionalArg(adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return positionalArg(new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlUpdate argOffsetDateTimePerDb(String argName) {
if (options.useClientClock()) {
return namedArg(argName, adaptor.nullOffsetDateTime(OffsetDateTime.now()));
}
return namedArg(argName, new RewriteArg(options.flavor().dbTimeMillis()));
}
@Override
public SqlUpdate argBlobBytes(byte[] arg) {
return positionalArg(adaptor.nullBytes(arg));
@ -258,7 +285,7 @@ public class SqlUpdateImpl implements SqlUpdate {
throw new DatabaseException("Batch insert requires parameters");
}
PreparedStatement ps = null;
Metric metric = new Metric(log.isLoggable(Level.FINE));
Metric metric = new Metric(logger.isLoggable(Level.FINE));
String executeSql = sql;
List<Object> firstRowParameters = null;
List<List<Object>> parameters = new ArrayList<>();
@ -300,19 +327,19 @@ public class SqlUpdateImpl implements SqlUpdate {
logEx = e;
throw DatabaseException.wrap(DebugSql.exceptionMessage(executeSql, firstRowParameters, errorCode, options), e);
} finally {
adaptor.closeQuietly(ps, log);
adaptor.closeQuietly(ps, logger);
metric.done("close");
if (isSuccess) {
DebugSql.logSuccess("Insert", log, metric, executeSql, firstRowParameters, options);
DebugSql.logSuccess("Insert", logger, metric, executeSql, firstRowParameters, options);
} else {
DebugSql.logError("Insert", log, metric, errorCode, executeSql, firstRowParameters, options, logEx);
DebugSql.logError("Insert", logger, metric, errorCode, executeSql, firstRowParameters, options, logEx);
}
}
}
private int updateInternal(int expectedNumAffectedRows) {
PreparedStatement ps = null;
Metric metric = new Metric(log.isLoggable(Level.FINE));
Metric metric = new Metric(logger.isLoggable(Level.FINE));
String executeSql = sql;
List<Object> parameters = null;
boolean isSuccess = false;
@ -346,12 +373,12 @@ public class SqlUpdateImpl implements SqlUpdate {
logEx = e;
throw DatabaseException.wrap(DebugSql.exceptionMessage(executeSql, parameters, errorCode, options), e);
} finally {
adaptor.closeQuietly(ps, log);
adaptor.closeQuietly(ps, logger);
metric.done("close");
if (isSuccess) {
DebugSql.logSuccess("Update", log, metric, executeSql, parameters, options);
DebugSql.logSuccess("Update", logger, metric, executeSql, parameters, options);
} else {
DebugSql.logError("Update", log, metric, errorCode, executeSql, parameters, options, logEx);
DebugSql.logError("Update", logger, metric, errorCode, executeSql, parameters, options, logEx);
}
}
}

View file

@ -11,12 +11,10 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
@ -96,10 +94,6 @@ public class StatementAdapter {
}
}
public Object nullInstant(Instant arg) {
return arg == null ? new SqlNull(Types.TIMESTAMP) : Timestamp.valueOf(arg.atOffset(ZoneOffset.UTC).toLocalDateTime());
}
public Object nullLocalDateTime(LocalDateTime arg) {
return arg == null ? new SqlNull(Types.TIMESTAMP) : Timestamp.valueOf(arg);
}

View file

@ -10,7 +10,6 @@ import java.io.StringReader;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@ -579,23 +578,6 @@ public class RowStub {
return new ByteArrayInputStream(getBlobBytesOrZeroLen(columnName));
}
@Override
public Instant getInstantOrNull() {
return toInstant(rows.get(row)[++col]);
}
@Override
public Instant getInstantOrNull(int columnOneBased) {
col = columnOneBased;
return toInstant(rows.get(row)[columnOneBased - 1]);
}
@Override
public Instant getInstantOrNull(String columnName) {
col = columnIndexByName(columnName) + 1;
return toInstant(rows.get(row)[columnIndexByName(columnName)]);
}
@Override
public LocalDateTime getLocalDateTimeOrNull() {
return toLocalDateTime(rows.get(row)[++col]);
@ -716,19 +698,6 @@ public class RowStub {
return (BigDecimal) o;
}
private Instant toInstant(Object o) {
if (o instanceof String s) {
if (s.length() == "yyyy-MM-dd".length()) {
return Instant.parse(s);
}
if (s.length() == "yyyy-MM-ddThh:mm:ss".length()) {
return Instant.parse(s);
}
throw new DatabaseException("Didn't understand date string: " + s);
}
return (Instant) o;
}
private LocalDateTime toLocalDateTime(Object o) {
if (o instanceof String s) {
if (s.length() == "yyyy-MM-dd".length()) {

View file

@ -1,5 +1,6 @@
package org.xbib.jdbc.test;
import java.time.Instant;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -25,7 +26,6 @@ import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;