From b7f9d7cafaf312553d9f865e02cd3b1680a8ab2b Mon Sep 17 00:00:00 2001 From: AbstractCoder Date: Mon, 5 Apr 2021 02:06:14 +0300 Subject: [PATCH] Add Interval to Postgres simple types. io.r2dbc.postgresql.codec.Interval is now considered a simple type. Closes #573 Original pull request: #574. --- .../data/r2dbc/dialect/PostgresDialect.java | 3 ++ .../r2dbc/core/PostgresIntegrationTests.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/org/springframework/data/r2dbc/dialect/PostgresDialect.java b/src/main/java/org/springframework/data/r2dbc/dialect/PostgresDialect.java index 322e2a19559..9bb861585ec 100644 --- a/src/main/java/org/springframework/data/r2dbc/dialect/PostgresDialect.java +++ b/src/main/java/org/springframework/data/r2dbc/dialect/PostgresDialect.java @@ -66,6 +66,9 @@ public class PostgresDialect extends org.springframework.data.relational.core.di // conditional Postgres JSON support. ifClassPresent("io.r2dbc.postgresql.codec.Json", simpleTypes::add); + // conditional Postgres Interval support + ifClassPresent("io.r2dbc.postgresql.codec.Interval", simpleTypes::add); + SIMPLE_TYPES = simpleTypes; } diff --git a/src/test/java/org/springframework/data/r2dbc/core/PostgresIntegrationTests.java b/src/test/java/org/springframework/data/r2dbc/core/PostgresIntegrationTests.java index aba1c7d1f97..3acb08aa8fb 100644 --- a/src/test/java/org/springframework/data/r2dbc/core/PostgresIntegrationTests.java +++ b/src/test/java/org/springframework/data/r2dbc/core/PostgresIntegrationTests.java @@ -23,6 +23,7 @@ import io.r2dbc.postgresql.codec.Box; import io.r2dbc.postgresql.codec.Circle; import io.r2dbc.postgresql.codec.EnumCodec; +import io.r2dbc.postgresql.codec.Interval; import io.r2dbc.postgresql.codec.Line; import io.r2dbc.postgresql.codec.Lseg; import io.r2dbc.postgresql.codec.Path; @@ -34,6 +35,7 @@ import lombok.Data; import reactor.test.StepVerifier; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -245,6 +247,27 @@ void shouldReadAndWriteGeoTypes() { assertThat(saved).isEqualTo(loaded); } + @Test // gh-573 + void shouldReadAndWriteInterval() { + EntityWithInterval entityWithInterval = new EntityWithInterval(); + entityWithInterval.interval = Interval.of(Duration.ofHours(3)); + + template.execute("DROP TABLE IF EXISTS with_interval"); + template.execute("CREATE TABLE with_interval (" // + + "id serial PRIMARY KEY," // + + "interval INTERVAL" // + + ")"); + + R2dbcEntityTemplate template = new R2dbcEntityTemplate(client, + new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE)); + + EntityWithInterval saved = template.insert(entityWithInterval).block(); + EntityWithInterval loaded = template.select(Query.empty(), EntityWithInterval.class) // + .blockLast(); + + assertThat(saved.interval).isEqualTo(loaded.interval); + } + private void insert(EntityWithArrays object) { client.insert() // @@ -300,4 +323,15 @@ static class GeoType { org.springframework.data.geo.Point springDataPoint; org.springframework.data.geo.Polygon springDataPolygon; } + + @Data + @Table("with_interval") + static class EntityWithInterval { + + @Id Integer id; + + Interval interval; + + } + }