Skip to content

Commit

Permalink
Adding support for java.sql.Date and java.util.Date in JDBC to conver…
Browse files Browse the repository at this point in the history
…t to String (neo4j-contrib#411)
  • Loading branch information
bradnussbaum authored and albertodelazzari committed Jun 28, 2017
1 parent 98a7b90 commit 2645e18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/main/java/apoc/load/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ private Object convert(Object value) {
if (value instanceof UUID || value instanceof BigInteger || value instanceof BigDecimal) {
return value.toString();
}
if (value instanceof java.util.Date) {
return ((java.util.Date) value).getTime();
}
return value;
}

Expand Down
33 changes: 24 additions & 9 deletions src/test/java/apoc/load/JdbcTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package apoc.load;

import apoc.ApocConfiguration;
import apoc.load.Jdbc;
import apoc.util.TestUtil;
import apoc.util.Util;
import org.junit.*;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.sql.*;
import java.time.Instant;
import java.util.Calendar;
import java.util.Properties;

import static apoc.util.MapUtil.map;
import static apoc.util.TestUtil.testCall;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;

public class JdbcTest {

private static GraphDatabaseService db;

private static java.sql.Date hireDate = new java.sql.Date( new Calendar.Builder().setDate( 2017, 04, 25 ).build().getTimeInMillis() );

private static java.sql.Timestamp effectiveFromDate = java.sql.Timestamp.from( Instant.now() );

@BeforeClass
public static void setUp() throws Exception {
db = new TestGraphDatabaseFactory().newImpermanentDatabase();
Expand All @@ -37,38 +42,48 @@ public static void tearDown() {
@Test
public void testLoadJdbc() throws Exception {
testCall(db, "CALL apoc.load.jdbc('jdbc:derby:derbyDB','PERSON')",
(row) -> assertEquals(singletonMap("NAME", "John"), row.get("row")));
(row) -> assertEquals( Util.map("NAME", "John", "HIRE_DATE", hireDate.getTime(),"EFFECTIVE_FROM_DATE",
effectiveFromDate.getTime()), row.get("row")));
}

@Test
public void testLoadJdbcSelect() throws Exception {
testCall(db, "CALL apoc.load.jdbc('jdbc:derby:derbyDB','SELECT * FROM PERSON')",
(row) -> assertEquals(singletonMap("NAME", "John"), row.get("row")));
(row) -> assertEquals( Util.map("NAME", "John", "HIRE_DATE", hireDate.getTime(),"EFFECTIVE_FROM_DATE",
effectiveFromDate.getTime()), row.get("row")));
}

@Test
public void testLoadJdbcParams() throws Exception {
testCall(db, "CALL apoc.load.jdbcParams('jdbc:derby:derbyDB','SELECT * FROM PERSON WHERE NAME = ?',['John'])", // YIELD row RETURN row
(row) -> assertEquals(singletonMap("NAME", "John"), row.get("row")));
(row) -> assertEquals( Util.map("NAME", "John", "HIRE_DATE", hireDate.getTime(),"EFFECTIVE_FROM_DATE",
effectiveFromDate.getTime()), row.get("row")));
}

@Test
public void testLoadJdbcKey() throws Exception {
testCall(db, "CALL apoc.load.jdbc('derby','PERSON')",
(row) -> assertEquals(singletonMap("NAME", "John"), row.get("row")));
(row) -> assertEquals( Util.map("NAME", "John", "HIRE_DATE", hireDate.getTime(),"EFFECTIVE_FROM_DATE",
effectiveFromDate.getTime()), row.get("row")));

}

private static void createPersonTableAndData() throws ClassNotFoundException, SQLException {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:derbyDB;create=true", new Properties());
try { conn.createStatement().execute("DROP TABLE PERSON"); } catch (SQLException se) {/*ignore*/}
conn.createStatement().execute("CREATE TABLE PERSON (NAME varchar(50))");
PreparedStatement ps = conn.prepareStatement("INSERT INTO PERSON values(?)");
conn.createStatement().execute("CREATE TABLE PERSON (NAME varchar(50), HIRE_DATE DATE, EFFECTIVE_FROM_DATE TIMESTAMP )");
PreparedStatement ps = conn.prepareStatement("INSERT INTO PERSON values(?,?,?)");
ps.setString(1, "John");
ps.setDate(2, hireDate);
ps.setTimestamp(3, effectiveFromDate);
int rows = ps.executeUpdate();
assertEquals(1, rows);
ResultSet rs = conn.createStatement().executeQuery("SELECT NAME FROM PERSON");
ResultSet rs = conn.createStatement().executeQuery("SELECT NAME, HIRE_DATE, EFFECTIVE_FROM_DATE FROM PERSON");
assertEquals(true, rs.next());
assertEquals("John", rs.getString("NAME"));
assertEquals(hireDate.getTime(), rs.getDate("HIRE_DATE").getTime());
assertEquals(effectiveFromDate.getTime(), rs.getTimestamp("EFFECTIVE_FROM_DATE").getTime());
assertEquals(false, rs.next());
rs.close();
}
Expand Down

0 comments on commit 2645e18

Please sign in to comment.