Skip to content

Commit

Permalink
(#309) Support LocalDateTime (#316)
Browse files Browse the repository at this point in the history
* (#309) support LocalDateTime

* nit
  • Loading branch information
pan3793 authored Mar 1, 2021
1 parent 79083a8 commit 938812f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.Struct;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -155,6 +156,9 @@ protected Object convertObjectIfNecessary(Object obj) {
if (obj instanceof Timestamp) {
result = DateTimeUtil.toZonedDateTime((Timestamp) obj, tz);
}
if (obj instanceof LocalDateTime) {
result = ((LocalDateTime) obj).atZone(tz);
}
return result;
}

Expand Down Expand Up @@ -197,6 +201,8 @@ private boolean assembleSimpleParameter(StringBuilder queryBuilder, Object param
return assembleQuotedParameter(queryBuilder, String.valueOf(parameter));
} else if (parameter instanceof LocalDate) {
return assembleQuotedParameter(queryBuilder, dateFmt.format((LocalDate) parameter));
} else if (parameter instanceof LocalDateTime) {
return assembleQuotedParameter(queryBuilder, timestampFmt.format((LocalDateTime) parameter));
} else if (parameter instanceof ZonedDateTime) {
return assembleQuotedParameter(queryBuilder, timestampFmt.format((ZonedDateTime) parameter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,24 @@ public void successfullyInsertData() throws Exception {
statement.execute("CREATE TABLE test(" +
"id UInt8, " +
"day Date, " +
"day2 Date, " +
"time DateTime, " +
"time2 DateTime, " +
"flag Boolean" +
")ENGINE = Log");

PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO test VALUES(?, ?, ?, ?)");
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)");

// 2018-07-01 19:00:00 GMT
// 2018-07-02 03:00:00 Asia/Shanghai
long time = 1530403200 + 19 * 3600;

preparedStatement.setByte(1, (byte) 1);
preparedStatement.setDate(2, new Date(time * 1000));
preparedStatement.setTimestamp(3, new Timestamp(time * 1000));
preparedStatement.setBoolean(4, true);
preparedStatement.setObject(3, LocalDate.of(2018, 7, 2));
preparedStatement.setTimestamp(4, new Timestamp(time * 1000));
preparedStatement.setObject(5, LocalDateTime.of(2018, 7, 2, 3, 0, 0, 0));
preparedStatement.setBoolean(6, true);
assertEquals(1, preparedStatement.executeUpdate());
});
}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ server:
ports:
- 9000:9000
- 8123:8123

0 comments on commit 938812f

Please sign in to comment.