#161 - Properly convert arrays for Postgres.

We now properly convert array values (single- and multi-dimensional) when inserting rows with arrays.
This commit is contained in:
Mark Paluch
2019-09-05 15:01:10 +02:00
parent b9afd23528
commit 611b73149b
6 changed files with 166 additions and 28 deletions

View File

@@ -29,9 +29,9 @@ import javax.sql.DataSource;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.data.annotation.Id;
import org.springframework.data.r2dbc.testing.ExternalDatabase;
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
@@ -57,6 +57,7 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
template.execute("DROP TABLE IF EXISTS with_arrays");
template.execute("CREATE TABLE with_arrays (" //
+ "id serial PRIMARY KEY," //
+ "boxed_array INT[]," //
+ "primitive_array INT[]," //
+ "multidimensional_array INT[]," //
@@ -64,10 +65,9 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
}
@Test // gh-30
@Ignore("https://github.com/r2dbc/r2dbc-postgresql/issues/40, r2dbc-postgresql returns Object[] instead of Integer[]")
public void shouldReadAndWritePrimitiveSingleDimensionArrays() {
EntityWithArrays withArrays = new EntityWithArrays(null, new int[] { 1, 2, 3 }, null, null);
EntityWithArrays withArrays = new EntityWithArrays(null, null, new int[] { 1, 2, 3 }, null, null);
insert(withArrays);
selectAndAssert(actual -> {
@@ -76,10 +76,9 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
}
@Test // gh-30
@Ignore("https://github.com/r2dbc/r2dbc-postgresql/issues/67")
public void shouldReadAndWriteBoxedSingleDimensionArrays() {
EntityWithArrays withArrays = new EntityWithArrays(new Integer[] { 1, 2, 3 }, null, null, null);
EntityWithArrays withArrays = new EntityWithArrays(null, new Integer[] { 1, 2, 3 }, null, null, null);
insert(withArrays);
@@ -91,10 +90,9 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
}
@Test // gh-30
@Ignore("https://github.com/r2dbc/r2dbc-postgresql/issues/67")
public void shouldReadAndWriteConvertedDimensionArrays() {
EntityWithArrays withArrays = new EntityWithArrays(null, null, null, Arrays.asList(5, 6, 7));
EntityWithArrays withArrays = new EntityWithArrays(null, null, null, null, Arrays.asList(5, 6, 7));
insert(withArrays);
@@ -104,10 +102,10 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
}
@Test // gh-30
@Ignore("https://github.com/r2dbc/r2dbc-postgresql/issues/42, Multi-dimensional arrays not supported yet")
public void shouldReadAndWriteMultiDimensionArrays() {
EntityWithArrays withArrays = new EntityWithArrays(null, null, new int[][] { { 1, 2, 3 }, { 4, 5 } }, null);
EntityWithArrays withArrays = new EntityWithArrays(null, null, null, new int[][] { { 1, 2, 3 }, { 4, 5, 6 } },
null);
insert(withArrays);
@@ -142,6 +140,7 @@ public class PostgresIntegrationTests extends R2dbcIntegrationTestSupport {
@AllArgsConstructor
static class EntityWithArrays {
@Id Integer id;
Integer[] boxedArray;
int[] primitiveArray;
int[][] multidimensionalArray;

View File

@@ -15,9 +15,17 @@
*/
package org.springframework.data.r2dbc.core;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
import static org.assertj.core.api.Assertions.*;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.data.r2dbc.dialect.PostgresDialect;
import org.springframework.data.r2dbc.mapping.OutboundRow;
/**
* {@link PostgresDialect} specific tests for {@link ReactiveDataAccessStrategy}.
@@ -32,4 +40,35 @@ public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessS
protected ReactiveDataAccessStrategy getStrategy() {
return strategy;
}
@Test
public void shouldConvertPrimitiveMultidimensionArrayToWrapper() {
OutboundRow row = strategy.getOutboundRow(new WithMultidimensionalArray(new int[][] { { 1, 2, 3 }, { 4, 5 } }));
assertThat(row.get("myarray").hasValue()).isTrue();
assertThat(row.get("myarray").getValue()).isInstanceOf(Integer[][].class);
}
@Test
public void shouldConvertCollectionToArray() {
OutboundRow row = strategy.getOutboundRow(new WithIntegerCollection(Arrays.asList(1, 2, 3)));
assertThat(row.get("myarray").hasValue()).isTrue();
assertThat(row.get("myarray").getValue()).isInstanceOf(Integer[].class);
assertThat((Integer[]) row.get("myarray").getValue()).contains(1, 2, 3);
}
@RequiredArgsConstructor
static class WithMultidimensionalArray {
final int[][] myarray;
}
@RequiredArgsConstructor
static class WithIntegerCollection {
final List<Integer> myarray;
}
}