Replaced update or insert property logic with merge property logic.

This commit is contained in:
Anthony Whitford
2015-09-09 23:18:38 -07:00
parent 57ae0f1676
commit ece4a51b94
2 changed files with 13 additions and 33 deletions

View File

@@ -258,12 +258,10 @@ public class CveDB {
* @param props a collection of properties * @param props a collection of properties
*/ */
void saveProperties(Properties props) { void saveProperties(Properties props) {
PreparedStatement updateProperty = null; PreparedStatement mergeProperty = null;
PreparedStatement insertProperty = null;
try { try {
try { try {
updateProperty = getConnection().prepareStatement(statementBundle.getString("UPDATE_PROPERTY")); mergeProperty = getConnection().prepareStatement(statementBundle.getString("MERGE_PROPERTY"));
insertProperty = getConnection().prepareStatement(statementBundle.getString("INSERT_PROPERTY"));
} catch (SQLException ex) { } catch (SQLException ex) {
LOGGER.warn("Unable to save properties to the database"); LOGGER.warn("Unable to save properties to the database");
LOGGER.debug("Unable to save properties to the database", ex); LOGGER.debug("Unable to save properties to the database", ex);
@@ -273,20 +271,16 @@ public class CveDB {
final String key = entry.getKey().toString(); final String key = entry.getKey().toString();
final String value = entry.getValue().toString(); final String value = entry.getValue().toString();
try { try {
updateProperty.setString(1, value); mergeProperty.setString(1, key);
updateProperty.setString(2, key); mergeProperty.setString(2, value);
if (updateProperty.executeUpdate() == 0) { mergeProperty.executeUpdate();
insertProperty.setString(1, key);
insertProperty.setString(2, value);
}
} catch (SQLException ex) { } catch (SQLException ex) {
LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value); LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value);
LOGGER.debug("", ex); LOGGER.debug("", ex);
} }
} }
} finally { } finally {
DBUtils.closeStatement(updateProperty); DBUtils.closeStatement(mergeProperty);
DBUtils.closeStatement(insertProperty);
} }
} }
@@ -297,38 +291,25 @@ public class CveDB {
* @param value the property value * @param value the property value
*/ */
void saveProperty(String key, String value) { void saveProperty(String key, String value) {
PreparedStatement updateProperty = null; PreparedStatement mergeProperty = null;
PreparedStatement insertProperty = null;
try { try {
try { try {
updateProperty = getConnection().prepareStatement(statementBundle.getString("UPDATE_PROPERTY")); mergeProperty = getConnection().prepareStatement(statementBundle.getString("MERGE_PROPERTY"));
} catch (SQLException ex) { } catch (SQLException ex) {
LOGGER.warn("Unable to save properties to the database"); LOGGER.warn("Unable to save properties to the database");
LOGGER.debug("Unable to save properties to the database", ex); LOGGER.debug("Unable to save properties to the database", ex);
return; return;
} }
try { try {
updateProperty.setString(1, value); mergeProperty.setString(1, key);
updateProperty.setString(2, key); mergeProperty.setString(2, value);
if (updateProperty.executeUpdate() == 0) { mergeProperty.executeUpdate();
try {
insertProperty = getConnection().prepareStatement(statementBundle.getString("INSERT_PROPERTY"));
} catch (SQLException ex) {
LOGGER.warn("Unable to save properties to the database");
LOGGER.debug("Unable to save properties to the database", ex);
return;
}
insertProperty.setString(1, key);
insertProperty.setString(2, value);
insertProperty.execute();
}
} catch (SQLException ex) { } catch (SQLException ex) {
LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value); LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value);
LOGGER.debug("", ex); LOGGER.debug("", ex);
} }
} finally { } finally {
DBUtils.closeStatement(updateProperty); DBUtils.closeStatement(mergeProperty);
DBUtils.closeStatement(insertProperty);
} }
} }

View File

@@ -31,8 +31,7 @@ SELECT_VULNERABILITY=SELECT id, description, cwe, cvssScore, cvssAccessVector, c
SELECT_VULNERABILITY_ID=SELECT id FROM vulnerability WHERE cve = ? SELECT_VULNERABILITY_ID=SELECT id FROM vulnerability WHERE cve = ?
SELECT_PROPERTIES=SELECT id, value FROM properties SELECT_PROPERTIES=SELECT id, value FROM properties
SELECT_PROPERTY=SELECT id, value FROM properties WHERE id = ? SELECT_PROPERTY=SELECT id, value FROM properties WHERE id = ?
INSERT_PROPERTY=INSERT INTO properties (id, value) VALUES (?, ?) MERGE_PROPERTY=MERGE INTO properties (id, value) KEY(id) VALUES(?, ?)
UPDATE_PROPERTY=UPDATE properties SET value = ? WHERE id = ?
DELETE_PROPERTY=DELETE FROM properties WHERE id = ? DELETE_PROPERTY=DELETE FROM properties WHERE id = ?
DELETE_UNUSED_DICT_CPE=DELETE FROM cpeEntry WHERE dictionaryEntry=true AND id NOT IN (SELECT cpeEntryId FROM software) DELETE_UNUSED_DICT_CPE=DELETE FROM cpeEntry WHERE dictionaryEntry=true AND id NOT IN (SELECT cpeEntryId FROM software)