From 1348c2f48d95b72ff6caefcac48116fb8bab5829 Mon Sep 17 00:00:00 2001 From: Abraham Polk Date: Thu, 2 Jan 2020 08:34:16 -0500 Subject: [PATCH 1/3] Initial changes to DBMSProcessor for insert entry functionality --- .../jabref/logic/shared/DBMSProcessor.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/logic/shared/DBMSProcessor.java b/src/main/java/org/jabref/logic/shared/DBMSProcessor.java index 963a2f23929..8b9d55f800e 100644 --- a/src/main/java/org/jabref/logic/shared/DBMSProcessor.java +++ b/src/main/java/org/jabref/logic/shared/DBMSProcessor.java @@ -213,25 +213,30 @@ private boolean checkForBibEntryExistence(BibEntry bibEntry) { private void insertIntoFieldTable(BibEntry bibEntry) { try { // Inserting into FIELD table - for (Field field : bibEntry.getFields()) { - StringBuilder insertFieldQuery = new StringBuilder() - .append("INSERT INTO ") - .append(escape("FIELD")) - .append("(") - .append(escape("ENTRY_SHARED_ID")) - .append(", ") - .append(escape("NAME")) - .append(", ") - .append(escape("VALUE")) - .append(") VALUES(?, ?, ?)"); - - try (PreparedStatement preparedFieldStatement = connection.prepareStatement(insertFieldQuery.toString())) { + // Coerce to ArrayList in order to use List.get() + List fields = new ArrayList<>(bibEntry.getFields()); + StringBuilder insertFieldQuery = new StringBuilder() + .append("INSERT INTO ") + .append(escape("FIELD")) + .append("(") + .append(escape("ENTRY_SHARED_ID")) + .append(", ") + .append(escape("NAME")) + .append(", ") + .append(escape("VALUE")) + .append(") VALUES(?, ?, ?)"); + // Number of commas is fields.size() - 1 + for (int i = 0; i < fields.size() - 1; i++) { + insertFieldQuery.append(", (?, ?, ?)"); + } + try (PreparedStatement preparedFieldStatement = connection.prepareStatement(insertFieldQuery.toString())) { + for (int i = 0; i < fields.size(); i++) { // columnIndex starts with 1 - preparedFieldStatement.setInt(1, bibEntry.getSharedBibEntryData().getSharedID()); - preparedFieldStatement.setString(2, field.getName()); - preparedFieldStatement.setString(3, bibEntry.getField(field).get()); - preparedFieldStatement.executeUpdate(); + preparedFieldStatement.setInt((3 * i) + 1, bibEntry.getSharedBibEntryData().getSharedID()); + preparedFieldStatement.setString((3 * i) + 2, fields.get(i).getName()); + preparedFieldStatement.setString((3 * i) + 3, bibEntry.getField(fields.get(i)).get()); } + preparedFieldStatement.executeUpdate(); } } catch (SQLException e) { LOGGER.error("SQL Error: ", e); From 7e4e76f99bc7327e4a8955f0eab7a76a69e1ed86 Mon Sep 17 00:00:00 2001 From: Abraham Polk Date: Thu, 2 Jan 2020 11:50:24 -0500 Subject: [PATCH 2/3] Added unit test for inserting multiple entries, with multiple of BibEntry --- .../logic/shared/DBMSProcessorTest.java | 82 ++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java b/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java index 1aae2ccb1dc..681a60249db 100644 --- a/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java +++ b/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java @@ -95,6 +95,64 @@ void testInsertEntry() throws SQLException { assertEquals(expectedFieldMap, actualFieldMap); } + @Test + void testInsertMultipleEntries() throws SQLException { + BibEntry firstEntry = getBibEntryExample(); + String firstId = firstEntry.getId(); + BibEntry secondEntry = getBibEntryExample2(); + String secondId = secondEntry.getId(); + BibEntry thirdEntry = getBibEntryExample3(); + String thirdId = thirdEntry.getId(); + + // This must eventually be changed to insertEntries() once that method is implemented + dbmsProcessor.insertEntry(firstEntry); + dbmsProcessor.insertEntry(secondEntry); + dbmsProcessor.insertEntry(thirdEntry); + + Map> actualFieldMap = new HashMap<>(); + + try (ResultSet entryResultSet = selectFrom("ENTRY", dbmsConnection, dbmsProcessor)) { + assertTrue(entryResultSet.next()); + assertEquals(1, entryResultSet.getInt("SHARED_ID")); + assertEquals("inproceedings", entryResultSet.getString("TYPE")); + assertEquals(1, entryResultSet.getInt("VERSION")); + assertTrue(entryResultSet.next()); + assertEquals(2, entryResultSet.getInt("SHARED_ID")); + assertEquals("inproceedings", entryResultSet.getString("TYPE")); + assertEquals(1, entryResultSet.getInt("VERSION")); + assertTrue(entryResultSet.next()); + assertEquals(3, entryResultSet.getInt("SHARED_ID")); + assertFalse(entryResultSet.next()); + + try (ResultSet fieldResultSet = selectFrom("FIELD", dbmsConnection, dbmsProcessor)) { + while (fieldResultSet.next()) { + System.out.println(fieldResultSet.getString("ENTRY_SHARED_ID")); + System.out.println(fieldResultSet.getString("NAME")); + System.out.println(fieldResultSet.getString("VALUE")); + System.out.println(); + + if (actualFieldMap.keySet().contains(fieldResultSet.getInt("ENTRY_SHARED_ID"))) { + actualFieldMap.get(fieldResultSet.getInt("ENTRY_SHARED_ID")).put( + fieldResultSet.getString("NAME"), fieldResultSet.getString("VALUE")); + } else { + int sharedId = fieldResultSet.getInt("ENTRY_SHARED_ID"); + actualFieldMap.put(sharedId, + new HashMap<>()); + actualFieldMap.get(sharedId).put(fieldResultSet.getString("NAME"), + fieldResultSet.getString("VALUE")); + } + } + } + } + List entries = Arrays.asList(firstEntry, secondEntry, thirdEntry); + Map> expectedFieldMap = entries.stream() + .collect(Collectors.toMap(bibEntry -> bibEntry.getSharedBibEntryData().getSharedID(), + (bibEntry) -> bibEntry.getFieldMap().entrySet().stream() + .collect(Collectors.toMap((entry) -> entry.getKey().getName(), Map.Entry::getValue)))); + + assertEquals(expectedFieldMap, actualFieldMap); + } + @Test void testUpdateEntry() throws Exception { BibEntry expectedEntry = getBibEntryExample(); @@ -156,7 +214,7 @@ void testUpdateEqualEntry() throws OfflineLockException, SQLException { @Test void testRemoveAllEntries() throws SQLException { BibEntry firstEntry = getBibEntryExample(); - BibEntry secondEntry = getBibEntryExample(); + BibEntry secondEntry = getBibEntryExample2(); List entriesToRemove = Arrays.asList(firstEntry, secondEntry); dbmsProcessor.insertEntry(firstEntry); dbmsProcessor.insertEntry(secondEntry); @@ -170,8 +228,8 @@ void testRemoveAllEntries() throws SQLException { @Test void testRemoveSomeEntries() throws SQLException { BibEntry firstEntry = getBibEntryExample(); - BibEntry secondEntry = getBibEntryExample(); - BibEntry thirdEntry = getBibEntryExample(); + BibEntry secondEntry = getBibEntryExample2(); + BibEntry thirdEntry = getBibEntryExample3(); // Remove the first and third entries - the second should remain (SHARED_ID will be 2) @@ -311,6 +369,24 @@ private static BibEntry getBibEntryExample() { .withCiteKey("nanoproc1994"); } + private static BibEntry getBibEntryExample2() { + return new BibEntry(StandardEntryType.InProceedings) + .withField(StandardField.AUTHOR, "Shelah, Saharon and Ziegler, Martin") + .withField(StandardField.TITLE, "Algebraically closed groups of large cardinality") + .withField(StandardField.JOURNAL, "The Journal of Symbolic Logic") + .withField(StandardField.YEAR, "1979") + .withCiteKey("algegrou1979"); + } + + private static BibEntry getBibEntryExample3() { + return new BibEntry(StandardEntryType.InProceedings) + .withField(StandardField.AUTHOR, "Hodges, Wilfrid and Shelah, Saharon") + .withField(StandardField.TITLE, "Infinite games and reduced products") + .withField(StandardField.JOURNAL, "Annals of Mathematical Logic") + .withField(StandardField.YEAR, "1981") + .withCiteKey("infigame1981"); + } + private ResultSet selectFrom(String table, DBMSConnection dbmsConnection, DBMSProcessor dbmsProcessor) { try { return dbmsConnection.getConnection().createStatement().executeQuery("SELECT * FROM " + escape(table, dbmsProcessor)); From 8cf40b474eb30ced745cf588e4c68ec43e2856b6 Mon Sep 17 00:00:00 2001 From: Abraham Polk Date: Thu, 2 Jan 2020 11:58:47 -0500 Subject: [PATCH 3/3] Remove StandardOut --- src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java b/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java index 681a60249db..754e601e0b2 100644 --- a/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java +++ b/src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java @@ -126,11 +126,6 @@ void testInsertMultipleEntries() throws SQLException { try (ResultSet fieldResultSet = selectFrom("FIELD", dbmsConnection, dbmsProcessor)) { while (fieldResultSet.next()) { - System.out.println(fieldResultSet.getString("ENTRY_SHARED_ID")); - System.out.println(fieldResultSet.getString("NAME")); - System.out.println(fieldResultSet.getString("VALUE")); - System.out.println(); - if (actualFieldMap.keySet().contains(fieldResultSet.getInt("ENTRY_SHARED_ID"))) { actualFieldMap.get(fieldResultSet.getInt("ENTRY_SHARED_ID")).put( fieldResultSet.getString("NAME"), fieldResultSet.getString("VALUE"));