Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3235: remote metadata is updated instead of delete + insert #3282

Merged
merged 4 commits into from
Oct 13, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/main/java/org/jabref/shared/DBMSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,25 +527,41 @@ public Map<String, String> getSharedMetaData() {
* @param data JabRef meta data as map
*/
public void setSharedMetaData(Map<String, String> data) throws SQLException {
connection.createStatement().executeUpdate("TRUNCATE TABLE " + escape("METADATA")); // delete data all data from table

for (Map.Entry<String, String> metaEntry : data.entrySet()) {

StringBuilder query = new StringBuilder()
StringBuilder updateQuery = new StringBuilder()
.append("UPDATE ")
.append(escape("METADATA"))
.append(" SET ")
.append(escape("VALUE"))
.append(" = ? ")
.append(" WHERE ")
.append(escape("KEY"))
.append(" = ?");

StringBuilder insertQuery = new StringBuilder()
.append("INSERT INTO ")
.append(escape("METADATA"))
.append("(")
.append(escape("KEY"))
.append(", ")
.append(escape("VALUE"))
.append(") VALUES(?, ?)");

try (PreparedStatement preparedStatement = connection.prepareStatement(query.toString())) {
preparedStatement.setString(1, metaEntry.getKey());
preparedStatement.setString(2, metaEntry.getValue());
preparedStatement.executeUpdate();

for (Map.Entry<String, String> metaEntry : data.entrySet()) {
try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery.toString())) {
updateStatement.setString(2, metaEntry.getKey());
updateStatement.setString(1, metaEntry.getValue());
if (updateStatement.executeUpdate() == 0) {
// No rows updated -> insert data
try (PreparedStatement insertStatement = connection.prepareStatement(insertQuery.toString())) {
insertStatement.setString(1, metaEntry.getKey());
insertStatement.setString(2, metaEntry.getValue());
insertStatement.executeUpdate();
} catch (SQLException e) {
LOGGER.error("SQL Error: ", e);
}
}
} catch (SQLException e) {
LOGGER.error("SQL Error: ", e);
LOGGER.error("SQL Error: ", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an extra level of indentation here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there was an additional indent (I'm using the github editor for these edits since I don't have a proper IDE at the moment).

}
}
}
Expand Down