Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/reflection/Jdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public class Jdk {
parameterExists = available;
}

public static final boolean dateAndTimeApiExists;

static {
boolean available = false;
try {
Resources.classForName("java.time.Clock");
available = true;
} catch (ClassNotFoundException e) {
// ignore
}
dateAndTimeApiExists = available;
}

private Jdk() {
super();
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/apache/ibatis/type/InstantTypeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;

import org.apache.ibatis.lang.UsesJava8;

/**
* @since 3.4.5
* @author Tomas Rohovsky
*/
@UsesJava8
public class InstantTypeHandler extends BaseTypeHandler<Instant> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, Timestamp.from(parameter));
}

@Override
public Instant getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getInstant(timestamp);
}

@Override
public Instant getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getInstant(timestamp);
}

@Override
public Instant getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getInstant(timestamp);
}

private static Instant getInstant(Timestamp timestamp) {
if (timestamp != null) {
return timestamp.toInstant();
}
return null;
}
}
68 changes: 68 additions & 0 deletions src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.chrono.JapaneseDate;

import org.apache.ibatis.lang.UsesJava8;

/**
* Type Handler for {@link JapaneseDate}.
*
* @since 3.4.5
* @author Kazuki Shimizu
*/
@UsesJava8
public class JapaneseDateTypeHandler extends BaseTypeHandler<JapaneseDate> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, JapaneseDate parameter, JdbcType jdbcType)
throws SQLException {
ps.setDate(i, Date.valueOf(LocalDate.ofEpochDay(parameter.toEpochDay())));
}

@Override
public JapaneseDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
Date date = rs.getDate(columnName);
return getJapaneseDate(date);
}

@Override
public JapaneseDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Date date = rs.getDate(columnIndex);
return getJapaneseDate(date);
}

@Override
public JapaneseDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Date date = cs.getDate(columnIndex);
return getJapaneseDate(date);
}

private static JapaneseDate getJapaneseDate(Date date) {
if (date != null) {
return JapaneseDate.from(date.toLocalDate());
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ibatis.type;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.chrono.JapaneseDate;

import org.apache.ibatis.lang.UsesJava8;

/**
* @since 3.4.5
*/
@UsesJava8
public abstract class Java8TypeHandlersRegistrar {

public static void registerDateAndTimeHandlers(TypeHandlerRegistry registry) {
registry.register(Instant.class, InstantTypeHandler.class);
registry.register(LocalDateTime.class, LocalDateTimeTypeHandler.class);
registry.register(LocalDate.class, LocalDateTypeHandler.class);
registry.register(LocalTime.class, LocalTimeTypeHandler.class);
registry.register(OffsetDateTime.class, OffsetDateTimeTypeHandler.class);
registry.register(OffsetTime.class, OffsetTimeTypeHandler.class);
registry.register(ZonedDateTime.class, ZonedDateTimeTypeHandler.class);
registry.register(Month.class, MonthTypeHandler.class);
registry.register(Year.class, YearTypeHandler.class);
registry.register(YearMonth.class, YearMonthTypeHandler.class);
registry.register(JapaneseDate.class, JapaneseDateTypeHandler.class);
}

}
64 changes: 64 additions & 0 deletions src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;

import org.apache.ibatis.lang.UsesJava8;

/**
* @since 3.4.5
* @author Tomas Rohovsky
*/
@UsesJava8
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setTimestamp(i, Timestamp.valueOf(parameter));
}

@Override
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getLocalDateTime(timestamp);
}

@Override
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getLocalDateTime(timestamp);
}

@Override
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getLocalDateTime(timestamp);
}

private static LocalDateTime getLocalDateTime(Timestamp timestamp) {
if (timestamp != null) {
return timestamp.toLocalDateTime();
}
return null;
}
}
64 changes: 64 additions & 0 deletions src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

import org.apache.ibatis.lang.UsesJava8;

/**
* @since 3.4.5
* @author Tomas Rohovsky
*/
@UsesJava8
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
throws SQLException {
ps.setDate(i, Date.valueOf(parameter));
}

@Override
public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
Date date = rs.getDate(columnName);
return getLocalDate(date);
}

@Override
public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Date date = rs.getDate(columnIndex);
return getLocalDate(date);
}

@Override
public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Date date = cs.getDate(columnIndex);
return getLocalDate(date);
}

private static LocalDate getLocalDate(Date date) {
if (date != null) {
return date.toLocalDate();
}
return null;
}
}
Loading