Drop support for H2 1.x.

Closes #1243
This commit is contained in:
Jens Schauder
2022-05-16 13:55:31 +02:00
parent 76983827d5
commit e6ff3859ed
7 changed files with 9 additions and 191 deletions

View File

@@ -31,7 +31,7 @@
<!-- databases and jdbc drivers -->
<!-- note that these currently do not control the versions of databases used via Testcontainers for testing -->
<db2.version>11.5.7.0</db2.version>
<h2.version>1.4.200</h2.version>
<h2.version>2.1.212</h2.version>
<hsqldb.version>2.6.1</hsqldb.version>
<mariadb-java-client.version>2.7.5</mariadb-java-client.version>
<mssql.version>9.2.1.jre8</mssql.version>

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2021 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
*
* https://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.springframework.data.jdbc.core.dialect;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.h2.api.TimestampWithTimeZone;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
/**
* Converter converting from an H2 internal representation of a timestamp with time zone to an OffsetDateTime.
*
* Only required for H2 versions < 2.0
*
* @author Jens Schauder
* @since 2.7
*/
@ReadingConverter
public enum H2TimestampWithTimeZoneToOffsetDateTimeConverter
implements Converter<TimestampWithTimeZone, OffsetDateTime> {
INSTANCE;
@Override
public OffsetDateTime convert(TimestampWithTimeZone source) {
long nanosInSecond = 1_000_000_000;
long nanosInMinute = nanosInSecond * 60;
long nanosInHour = nanosInMinute * 60;
long hours = (source.getNanosSinceMidnight() / nanosInHour);
long nanosInHours = hours * nanosInHour;
long nanosLeft = source.getNanosSinceMidnight() - nanosInHours;
long minutes = nanosLeft / nanosInMinute;
long nanosInMinutes = minutes * nanosInMinute;
nanosLeft -= nanosInMinutes;
long seconds = nanosLeft / nanosInSecond;
long nanosInSeconds = seconds * nanosInSecond;
nanosLeft -= nanosInSeconds;
ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds());
return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int) hours, (int) minutes,
(int) seconds, (int) nanosLeft, offset);
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2021 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
*
* https://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.springframework.data.jdbc.core.dialect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.data.relational.core.dialect.Db2Dialect;
import org.springframework.data.relational.core.dialect.H2Dialect;
/**
* {@link Db2Dialect} that registers JDBC specific converters.
*
* @author Jens Schauder
* @author Christoph Strobl
* @since 2.3
*/
public class JdbcH2Dialect extends H2Dialect {
public static JdbcH2Dialect INSTANCE = new JdbcH2Dialect();
protected JdbcH2Dialect() {}
@Override
public Collection<Object> getConverters() {
final Collection<Object> originalConverters = super.getConverters();
if (isH2belowVersion2()) {
List<Object> converters = new ArrayList<>(originalConverters);
converters.add(H2TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE);
return converters;
}
return originalConverters;
}
static boolean isH2belowVersion2() {
try {
JdbcH2Dialect.class.getClassLoader().loadClass("org.h2.api.TimestampWithTimeZone");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}

View File

@@ -29,11 +29,11 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.data.jdbc.core.dialect.JdbcDb2Dialect;
import org.springframework.data.jdbc.core.dialect.JdbcH2Dialect;
import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect;
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.H2Dialect;
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
import org.springframework.data.relational.core.dialect.MariaDbDialect;
import org.springframework.data.relational.core.dialect.OracleDialect;
@@ -119,7 +119,7 @@ public class DialectResolver {
return HsqlDbDialect.INSTANCE;
}
if (name.contains("h2")) {
return JdbcH2Dialect.INSTANCE;
return H2Dialect.INSTANCE;
}
if (name.contains("mysql")) {
return new JdbcMySqlDialect(getIdentifierProcessing(metaData));

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2021 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
*
* https://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.springframework.data.jdbc.core.dialect;
import static org.assertj.core.api.Assertions.*;
import java.time.OffsetDateTime;
import org.h2.api.TimestampWithTimeZone;
import org.h2.util.DateTimeUtils;
import org.junit.jupiter.api.Test;
/**
* Tests for {@link JdbcH2Dialect}.
*
* @author Jens Schauder
*/
class JdbcH2DialectTests {
@Test
void TimestampWithTimeZone2OffsetDateTimeConverterConvertsProperly() {
H2TimestampWithTimeZoneToOffsetDateTimeConverter converter = H2TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE;
long dateValue = 123456789;
long timeNanos = 987654321;
int timeZoneOffsetSeconds = 4 * 60 * 60;
TimestampWithTimeZone timestampWithTimeZone = new TimestampWithTimeZone(dateValue, timeNanos,
timeZoneOffsetSeconds);
OffsetDateTime offsetDateTime = converter.convert(timestampWithTimeZone);
assertThat(offsetDateTime.getOffset().getTotalSeconds()).isEqualTo(timeZoneOffsetSeconds);
assertThat(offsetDateTime.getNano()).isEqualTo(timeNanos);
assertThat(offsetDateTime.toEpochSecond())
.isEqualTo(DateTimeUtils.getEpochSeconds(dateValue, timeNanos, timeZoneOffsetSeconds));
}
}

View File

@@ -42,8 +42,8 @@ CREATE TABLE element_no_id
CREATE TABLE "ARRAY_OWNER"
(
ID SERIAL PRIMARY KEY,
DIGITS ARRAY[10] NOT NULL,
MULTIDIMENSIONAL ARRAY[10] NULL
DIGITS VARCHAR(30) ARRAY[10] NOT NULL,
MULTIDIMENSIONAL VARCHAR(30) ARRAY[10] NULL
);
CREATE TABLE BYTE_ARRAY_OWNER
@@ -55,13 +55,13 @@ CREATE TABLE BYTE_ARRAY_OWNER
CREATE TABLE DOUBLE_LIST_OWNER
(
ID SERIAL PRIMARY KEY,
DIGITS ARRAY[10]
DIGITS DOUBLE ARRAY[10]
);
CREATE TABLE FLOAT_LIST_OWNER
(
ID SERIAL PRIMARY KEY,
DIGITS ARRAY[10]
DIGITS FLOAT ARRAY[10]
);
CREATE TABLE CHAIN4

View File

@@ -30,6 +30,7 @@ import org.springframework.util.ClassUtils;
* @author Mark Paluch
* @author Myeonghyeon Lee
* @author Christph Strobl
* @author Jens Schauder
* @since 2.0
*/
public class H2Dialect extends AbstractDialect {
@@ -105,13 +106,6 @@ public class H2Dialect extends AbstractDialect {
@Override
public Set<Class<?>> simpleTypes() {
if (!ClassUtils.isPresent("org.h2.api.TimestampWithTimeZone", getClass().getClassLoader())) {
return Collections.emptySet();
}
try {
return Collections.singleton(ClassUtils.forName("org.h2.api.TimestampWithTimeZone", getClass().getClassLoader()));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
return Collections.emptySet();
}
}