google / google/error-prone

Flag setTimestamp, getTimestamp, setObject(.., Timestamp), setObject(.., Date) and similar JDBC methods

Open
#5,291 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

Most of the modern databases (all of them?) support both `timestamp` and `timestamp with time zone`.
In that regard, `setTimestamp(..., java.sql.Timestamp)` becomes ambiguous, and the driver has to guess if the user meant `with` or `without timezone`.

I suggest the applications should use `java.time` APIs, then the JDBC driver would get a clear view on the intention of the user.

For instance:
* `java.time.LocalDate` -> `date`
* `java.time.LocalTime` -> `time`
* `java.time.LocalDateTime` -> `timestamp`
* `java.time.OffsetDateTime` -> `timestamp with time zone`
* ...

Problematic APIs
* `PreparedStatement#setDate`
* `PreparedStatement#setTime`
* `PreparedStatement#setTimestamp`
* `PreparedStatement#setObject(..., java.util.Date,...)`
* `PreparedStatement#setObject(..., java.sql.Date,...)`
* `PreparedStatement#setObject(..., java.sql.Time,...)`
* `PreparedStatement#setObject(..., java.sql.Timestamp,...)`
* `PreparedStatement#setTime`
* `PreparedStatement#setTimestamp`
* `ResultSet#getDate`
* `ResultSet#getTime`
* `ResultSet#getTimestamp`
* `CallableStatement#getDate`
* `CallableStatement#getTime`
* `CallableStatement#getTimestamp`

PS. I'm https://github.com/pgjdbc/pgjdbc co-maintainer, and I contributed to time-related tests and bugfixes. Frankly, I believe JDBC `java.sql.` time-related types are error-prone.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.