Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added StringBasedOffsetDateTimeTypeHandler #1296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
import org.apache.ibatis.lang.UsesJava8;

/**
* Note: If you intend to select from a database column that contains date
* time with time zone information with this type handler, be aware of the fact
* that it <em>ignores</em> the offset information! Instead, the
* {@link OffsetDateTime} object that it creates uses the system-default time
* zone for the offset!
* <p>
* If you want to retain the offset information of the time zone consider using this type handler instead: {@link StringBasedOffsetDateTimeTypeHandler}.
*
*
* @since 3.4.5
* @author Tomas Rohovsky
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import org.apache.ibatis.lang.UsesJava8;

/**
* This is an alternative implementation to the
* {@link OffsetDateTimeTypeHandler}. In contrast to the former, it is capable
* of actually retaining information about the time zone offset and does not
* assume it to be the same as the system default.
* <p>
* In order for this to work however, it does not work on TIMESTAMPs but rather
* on Strings that represent the point in time in the ISO format.
* <p>
* For instance, when working with an Oracle database and having a column that
* is of type "TIMESTAMP WITH TIME ZONE" instead of writing:
* <p>
* <code>
* SELECT MY_COLUMN FROM MY_TABLE
* </code>
* <p>
* one would have to write the following:
* <p>
* <code> SELECT to_char(MY_COLUMN,'YYYY-MM-DD"T"HH24:MI:SSFFTZH:TZM') AS
* MY_COLUMN FROM MY_TABLE
* </code>
* <p>
* Then, this type handler can be used to correctly convert the timestamp stored
* in the cell into the equivalent OffsetDateTime Java object.
*
* @author Malte Mauelshagen
*
*/
@UsesJava8
public class StringBasedOffsetDateTimeTypeHandler extends BaseTypeHandler<OffsetDateTime> {

/** Example: 2011-12-03T10:15:30+01:00 */
public static final DateTimeFormatter OFFSET_DATE_TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

@Override
public void setNonNullParameter(PreparedStatement aPreparedStatement, int anIndex, OffsetDateTime aParameter, JdbcType aJdbcType) throws SQLException {
String formattedOffsetDateTime = aParameter.format(OFFSET_DATE_TIME_FORMATTER);
aPreparedStatement.setString(anIndex, formattedOffsetDateTime);
}

@Override
public OffsetDateTime getNullableResult(ResultSet aResultSet, String aColumnName) throws SQLException {
String tmpDateString = aResultSet.getString(aColumnName);
OffsetDateTime tmpOffsetDateTime = parseOffsetDateTimeString(tmpDateString);
return tmpOffsetDateTime;
}

@Override
public OffsetDateTime getNullableResult(ResultSet aResultSet, int aColumnName) throws SQLException {
String tmpDateString = aResultSet.getString(aColumnName);
OffsetDateTime tmpOffsetDateTime = parseOffsetDateTimeString(tmpDateString);
return tmpOffsetDateTime;
}

@Override
public OffsetDateTime getNullableResult(CallableStatement aCallableStatement, int aColumnIndex) throws SQLException {
String tmpDateString = aCallableStatement.getString(aColumnIndex);
OffsetDateTime tmpOffsetDateTime = parseOffsetDateTimeString(tmpDateString);
return tmpOffsetDateTime;
}

private OffsetDateTime parseOffsetDateTimeString(String aDateString) {
if (aDateString == null) {
return null;
}
try {
OffsetDateTime tmpOffsetDateTime = OffsetDateTime.parse(aDateString, OFFSET_DATE_TIME_FORMATTER);
return tmpOffsetDateTime;
} catch (DateTimeParseException e) {
final String tmpMessage = "Note, that the Date-String is expected to come in the following format: 2011-12-03T10:15:30+01:00" + " Pay attention to the spaces! Make sure to adapt your to_char() method accordingly.";
throw new IllegalArgumentException(tmpMessage, e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2009-2018 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.usesjava8;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import org.apache.ibatis.type.BaseTypeHandlerTest;
import org.apache.ibatis.type.StringBasedOffsetDateTimeTypeHandler;
import org.apache.ibatis.type.TypeHandler;
import org.junit.Test;

public class StringBasedOffsetDateTimeTypeHandlerTest extends BaseTypeHandlerTest {

private static final TypeHandler<OffsetDateTime> TYPE_HANDLER = new StringBasedOffsetDateTimeTypeHandler();
private static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2018, 5, 8, 9, 47, 55, 0, ZoneOffset.ofHours(13));
private static final String OFFSET_DATE_TIME_TO_CHAR = OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

@Override
@Test
public void shouldSetParameter() throws Exception {
TYPE_HANDLER.setParameter(ps, 1, OFFSET_DATE_TIME, null);
verify(ps).setString(1, OFFSET_DATE_TIME_TO_CHAR);
}

@Override
@Test
public void shouldGetResultFromResultSetByName() throws Exception {
when(rs.getString("column")).thenReturn(OFFSET_DATE_TIME_TO_CHAR);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByName() throws Exception {
when(rs.getString("column")).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromResultSetByPosition() throws Exception {
when(rs.getString(1)).thenReturn(OFFSET_DATE_TIME_TO_CHAR);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
when(rs.getString(1)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromCallableStatement() throws Exception {
when(cs.getString(1)).thenReturn(OFFSET_DATE_TIME_TO_CHAR);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromCallableStatement() throws Exception {
when(cs.getString(1)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}
}