Merge branch 'DbMerge' of https://github.com/awhitford/DependencyCheck into awhitford-DbMerge

This commit is contained in:
Jeremy Long
2015-10-12 05:48:03 -04:00
2 changed files with 54 additions and 67 deletions

View File

@@ -29,8 +29,10 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.MissingResourceException;
import java.util.Properties; import java.util.Properties;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
@@ -74,9 +76,17 @@ public class CveDB {
*/ */
public CveDB() throws DatabaseException { public CveDB() throws DatabaseException {
super(); super();
statementBundle = ResourceBundle.getBundle("data/dbStatements");
try { try {
open(); open();
try {
final String databaseProductName = conn.getMetaData().getDatabaseProductName();
LOGGER.debug("Database dialect: {}", databaseProductName);
final Locale dbDialect = new Locale(databaseProductName);
statementBundle = ResourceBundle.getBundle("data/dbStatements", dbDialect);
} catch (SQLException se) {
LOGGER.warn("Problem loading database specific dialect!", se);
statementBundle = ResourceBundle.getBundle("data/dbStatements");
}
databaseProperties = new DatabaseProperties(this); databaseProperties = new DatabaseProperties(this);
} catch (DatabaseException ex) { } catch (DatabaseException ex) {
throw ex; throw ex;
@@ -252,44 +262,6 @@ public class CveDB {
return prop; return prop;
} }
/**
* Saves a set of properties to the database.
*
* @param props a collection of properties
*/
void saveProperties(Properties props) {
PreparedStatement updateProperty = null;
PreparedStatement insertProperty = null;
try {
try {
updateProperty = getConnection().prepareStatement(statementBundle.getString("UPDATE_PROPERTY"));
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;
}
for (Entry<Object, Object> entry : props.entrySet()) {
final String key = entry.getKey().toString();
final String value = entry.getValue().toString();
try {
updateProperty.setString(1, value);
updateProperty.setString(2, key);
if (updateProperty.executeUpdate() == 0) {
insertProperty.setString(1, key);
insertProperty.setString(2, value);
}
} catch (SQLException ex) {
LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value);
LOGGER.debug("", ex);
}
}
} finally {
DBUtils.closeStatement(updateProperty);
DBUtils.closeStatement(insertProperty);
}
}
/** /**
* Saves a property to the database. * Saves a property to the database.
* *
@@ -297,38 +269,38 @@ 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 insertProperty = null;
try { try {
try { try {
updateProperty = getConnection().prepareStatement(statementBundle.getString("UPDATE_PROPERTY")); final PreparedStatement mergeProperty = getConnection().prepareStatement(statementBundle.getString("MERGE_PROPERTY"));
} catch (SQLException ex) { try {
LOGGER.warn("Unable to save properties to the database"); mergeProperty.setString(1, key);
LOGGER.debug("Unable to save properties to the database", ex); mergeProperty.setString(2, value);
return; mergeProperty.executeUpdate();
} } finally {
try { DBUtils.closeStatement(mergeProperty);
updateProperty.setString(1, value); }
updateProperty.setString(2, key); } catch (MissingResourceException mre) {
if (updateProperty.executeUpdate() == 0) { // No Merge statement, so doing an Update/Insert...
try { PreparedStatement updateProperty = null;
insertProperty = getConnection().prepareStatement(statementBundle.getString("INSERT_PROPERTY")); PreparedStatement insertProperty = null;
} catch (SQLException ex) { try {
LOGGER.warn("Unable to save properties to the database"); updateProperty = getConnection().prepareStatement(statementBundle.getString("UPDATE_PROPERTY"));
LOGGER.debug("Unable to save properties to the database", ex); updateProperty.setString(1, value);
return; updateProperty.setString(2, key);
} if (updateProperty.executeUpdate() == 0) {
insertProperty.setString(1, key); insertProperty = getConnection().prepareStatement(statementBundle.getString("INSERT_PROPERTY"));
insertProperty.setString(2, value); insertProperty.setString(1, key);
insertProperty.execute(); insertProperty.setString(2, value);
insertProperty.executeUpdate();
}
} finally {
DBUtils.closeStatement(updateProperty);
DBUtils.closeStatement(insertProperty);
} }
} catch (SQLException ex) {
LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value);
LOGGER.debug("", ex);
} }
} finally { } catch (SQLException ex) {
DBUtils.closeStatement(updateProperty); LOGGER.warn("Unable to save property '{}' with a value of '{}' to the database", key, value);
DBUtils.closeStatement(insertProperty); LOGGER.debug("", ex);
} }
} }

View File

@@ -0,0 +1,15 @@
# Copyright 2015 OWASP.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
MERGE_PROPERTY=MERGE INTO properties (id, value) KEY(id) VALUES(?, ?)