Skip to content

Commit

Permalink
[NOID] fixes #3766 support for JDBC array columns (#3874)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefan Armbruster <[email protected]>
  • Loading branch information
vga91 and sarmbruster authored Dec 19, 2023
1 parent eda49ee commit 1033d6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions full/src/main/java/apoc/load/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ private Object convert(Object value, int sqlType) {
if (Types.DATE == sqlType) {
return ((java.sql.Date)value).toLocalDate();
}

if (Types.ARRAY == sqlType) {
try {
return ((java.sql.Array)value).getArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return value;
}

Expand Down
17 changes: 17 additions & 0 deletions full/src/test/java/apoc/load/PostgresJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.testcontainers.containers.PostgreSQLContainer;

import java.sql.SQLException;
import java.util.Map;

import static apoc.util.TestUtil.testCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;

public class PostgresJdbcTest extends AbstractJdbcTest {

Expand Down Expand Up @@ -74,6 +76,21 @@ public void testLoadJdbSelect() {
(row) -> assertResult(row));
}

@Test
public void testLoadJdbSelectWithArrays() throws Exception {
testCall(db, "CALL apoc.load.jdbc($url,'SELECT * FROM ARRAY_TABLE',[], $config)", Util.map("url", postgress.getJdbcUrl(),
"config", Util.map("schema", "test",
"credentials", Util.map("user", postgress.getUsername(), "password", postgress.getPassword()))),
(result) -> {
Map<String, Object> row = (Map<String, Object>)result.get("row");
assertEquals("John", row.get("NAME"));
int[] intVals = (int[])row.get("INT_VALUES");
assertArrayEquals(intVals, new int[]{1, 2, 3});
double[] doubleVals = (double[])row.get("DOUBLE_VALUES");
assertArrayEquals(doubleVals, new double[]{ 1.0, 2.0, 3.0}, 0.01);
});
}

@Test
public void testLoadJdbcUpdate() {
testCall(db, "CALL apoc.load.jdbcUpdate($url,'UPDATE PERSON SET \"SURNAME\" = ? WHERE \"NAME\" = ?', ['DOE', 'John'], $config)",
Expand Down
10 changes: 9 additions & 1 deletion full/src/test/resources/init_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ CREATE TABLE PERSON (
"NULL_DATE" DATE);

INSERT INTO PERSON ("NAME", "SURNAME", "HIRE_DATE", "EFFECTIVE_FROM_DATE", "TEST_TIME", "NULL_DATE")
VALUES ('John', null, '2017-05-25', '2016-06-22 19:10:25+02', '15:37', null);
VALUES ('John', null, '2017-05-25', '2016-06-22 19:10:25+02', '15:37', null);

CREATE TABLE ARRAY_TABLE (
"NAME" text,
"INT_VALUES" int[],
"DOUBLE_VALUES" DOUBLE PRECISION[]
);
INSERT INTO ARRAY_TABLE ("NAME", "INT_VALUES", "DOUBLE_VALUES")
VALUES ('John', '{ 1, 2, 3}', '{ 1.0, 2.0, 3.0 }');

0 comments on commit 1033d6d

Please sign in to comment.