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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
### Fixed
- We fixed an issue where JabRef would not terminated after asking to collect anonymous statistics [#2955 comment](https://github.com/JabRef/jabref/issues/2955#issuecomment-334591123)
- We fixed an issue where JabRef would not shut down when started with the '-n' (No GUI) option.
- We improved the way metadata is updated in remote databases. [#3235](https://github.com/JabRef/jabref/issues/3235)

### Removed

Expand Down
36 changes: 26 additions & 10 deletions src/main/java/org/jabref/shared/DBMSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,23 +527,39 @@ 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);
}
Expand Down