6151: Do not add account_id column to non-account correlation attribute instance tables

This commit is contained in:
Raman Arora 2020-03-17 16:16:04 -04:00
parent ce5d6fa0e6
commit 291d778ee2
4 changed files with 82 additions and 25 deletions

View File

@ -51,7 +51,7 @@ public class CentralRepoDbUpgrader13To14 implements CentralRepoDbUpgrader {
if (type.getId() >= CorrelationAttributeInstance.ADDITIONAL_TYPES_BASE_ID) { if (type.getId() >= CorrelationAttributeInstance.ADDITIONAL_TYPES_BASE_ID) {
// these are new Correlation types - new tables need to be created // these are new Correlation types - new tables need to be created
statement.execute(String.format(RdbmsCentralRepoFactory.getCreateArtifactInstancesTableTemplate(selectedPlatform), instance_type_dbname, instance_type_dbname)); statement.execute(String.format(RdbmsCentralRepoFactory.getCreateAccountInstancesTableTemplate(selectedPlatform), instance_type_dbname, instance_type_dbname));
statement.execute(String.format(RdbmsCentralRepoFactory.getAddCaseIdIndexTemplate(), instance_type_dbname, instance_type_dbname)); statement.execute(String.format(RdbmsCentralRepoFactory.getAddCaseIdIndexTemplate(), instance_type_dbname, instance_type_dbname));
statement.execute(String.format(RdbmsCentralRepoFactory.getAddDataSourceIdIndexTemplate(), instance_type_dbname, instance_type_dbname)); statement.execute(String.format(RdbmsCentralRepoFactory.getAddDataSourceIdIndexTemplate(), instance_type_dbname, instance_type_dbname));
statement.execute(String.format(RdbmsCentralRepoFactory.getAddValueIndexTemplate(), instance_type_dbname, instance_type_dbname)); statement.execute(String.format(RdbmsCentralRepoFactory.getAddValueIndexTemplate(), instance_type_dbname, instance_type_dbname));
@ -61,9 +61,9 @@ public class CentralRepoDbUpgrader13To14 implements CentralRepoDbUpgrader {
// add new correlation type // add new correlation type
CentralRepoDbUtil.insertCorrelationType(connection, type); CentralRepoDbUtil.insertCorrelationType(connection, type);
} else { } else { // existing attributes
// Alter the existing _instance tables for Phone and Email attributes to add account_id column
// Alter the existing X_Instance tables to add account_id column if (type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID || type.getId() == CorrelationAttributeInstance.PHONE_TYPE_ID) {
String sqlStr = String.format(getAlterArtifactInstancesAddAccountIdTemplate(selectedPlatform), instance_type_dbname); String sqlStr = String.format(getAlterArtifactInstancesAddAccountIdTemplate(selectedPlatform), instance_type_dbname);
statement.execute(sqlStr); statement.execute(sqlStr);
@ -76,6 +76,7 @@ public class CentralRepoDbUpgrader13To14 implements CentralRepoDbUpgrader {
} }
} }
} }
}
// insert default accounts data // insert default accounts data
RdbmsCentralRepoFactory.insertDefaultAccountsTablesContent(connection, selectedPlatform); RdbmsCentralRepoFactory.insertDefaultAccountsTablesContent(connection, selectedPlatform);

View File

@ -325,4 +325,17 @@ public class CentralRepoDbUtil {
closeStatement(preparedStatement); closeStatement(preparedStatement);
} }
/**
* Checks if the given correlation attribute type has an account behind it.
*
* @param type Correlation type to check.
*
* @return True If the specified correlation type has an account.
*/
static boolean correlationAttribHasAnAccount(CorrelationAttributeInstance.Type type) {
return (type.getId() >= CorrelationAttributeInstance.ADDITIONAL_TYPES_BASE_ID)
|| type.getId() == CorrelationAttributeInstance.PHONE_TYPE_ID
|| type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID;
}
} }

View File

@ -1002,18 +1002,26 @@ abstract class RdbmsCentralRepo implements CentralRepository {
public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws CentralRepoException { public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws CentralRepoException {
checkAddArtifactInstanceNulls(eamArtifact); checkAddArtifactInstanceNulls(eamArtifact);
// @@@ We should cache the case and data source IDs in memory // @@@ We should cache the case and data source IDs in memory
String tableName = CentralRepoDbUtil.correlationTypeToInstanceTableName(eamArtifact.getCorrelationType()); String tableName = CentralRepoDbUtil.correlationTypeToInstanceTableName(eamArtifact.getCorrelationType());
String sql boolean artifactHasAnAccount = CentralRepoDbUtil.correlationAttribHasAnAccount(eamArtifact.getCorrelationType());
= "INSERT INTO "
String sql;
// _instance table for accounts have an additional account_id column
if (artifactHasAnAccount) {
sql = "INSERT INTO "
+ tableName + tableName
+ "(case_id, data_source_id, value, file_path, known_status, comment, file_obj_id, account_id) " + "(case_id, data_source_id, value, file_path, known_status, comment, file_obj_id, account_id) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?) "
+ getConflictClause(); + getConflictClause();
}
else {
sql = "INSERT INTO "
+ tableName
+ "(case_id, data_source_id, value, file_path, known_status, comment, file_obj_id) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?) "
+ getConflictClause();
}
try (Connection conn = connect(); try (Connection conn = connect();
PreparedStatement preparedStatement = conn.prepareStatement(sql);) { PreparedStatement preparedStatement = conn.prepareStatement(sql);) {
@ -1032,11 +1040,14 @@ abstract class RdbmsCentralRepo implements CentralRepository {
} }
preparedStatement.setLong(7, eamArtifact.getFileObjectId()); preparedStatement.setLong(7, eamArtifact.getFileObjectId());
// set in the accountId only for artifacts that represent accounts
if (artifactHasAnAccount) {
if (eamArtifact.getAccountId() >= 0) { if (eamArtifact.getAccountId() >= 0) {
preparedStatement.setLong(8, eamArtifact.getAccountId()); preparedStatement.setLong(8, eamArtifact.getAccountId());
} else { } else {
preparedStatement.setNull(8, Types.INTEGER); preparedStatement.setNull(8, Types.INTEGER);
} }
}
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} }

View File

@ -83,6 +83,7 @@ public class RdbmsCentralRepoFactory {
public boolean initializeDatabaseSchema() { public boolean initializeDatabaseSchema() {
String createArtifactInstancesTableTemplate = getCreateArtifactInstancesTableTemplate(selectedPlatform); String createArtifactInstancesTableTemplate = getCreateArtifactInstancesTableTemplate(selectedPlatform);
String createAccountInstancesTableTemplate = getCreateAccountInstancesTableTemplate(selectedPlatform);
String instancesCaseIdIdx = getAddCaseIdIndexTemplate(); String instancesCaseIdIdx = getAddCaseIdIndexTemplate();
String instancesDatasourceIdIdx = getAddDataSourceIdIndexTemplate(); String instancesDatasourceIdIdx = getAddDataSourceIdIndexTemplate();
@ -147,7 +148,13 @@ public class RdbmsCentralRepoFactory {
reference_type_dbname = CentralRepoDbUtil.correlationTypeToReferenceTableName(type); reference_type_dbname = CentralRepoDbUtil.correlationTypeToReferenceTableName(type);
instance_type_dbname = CentralRepoDbUtil.correlationTypeToInstanceTableName(type); instance_type_dbname = CentralRepoDbUtil.correlationTypeToInstanceTableName(type);
stmt.execute(String.format(createArtifactInstancesTableTemplate, instance_type_dbname, instance_type_dbname)); // use the correct create table template, based on whether the attribute type represents an account or not.
String createTableTemplate = (CentralRepoDbUtil.correlationAttribHasAnAccount(type))
? createAccountInstancesTableTemplate
: createArtifactInstancesTableTemplate;
stmt.execute(String.format(createTableTemplate, instance_type_dbname, instance_type_dbname));
stmt.execute(String.format(instancesCaseIdIdx, instance_type_dbname, instance_type_dbname)); stmt.execute(String.format(instancesCaseIdIdx, instance_type_dbname, instance_type_dbname));
stmt.execute(String.format(instancesDatasourceIdIdx, instance_type_dbname, instance_type_dbname)); stmt.execute(String.format(instancesDatasourceIdIdx, instance_type_dbname, instance_type_dbname));
stmt.execute(String.format(instancesValueIdx, instance_type_dbname, instance_type_dbname)); stmt.execute(String.format(instancesValueIdx, instance_type_dbname, instance_type_dbname));
@ -357,7 +364,7 @@ public class RdbmsCentralRepoFactory {
+ ")"; + ")";
} }
/** /**
* Get the template String for creating a new _instances table in a Sqlite * Get the template String for creating a new _instances table for non account artifacts in
* central repository. %s will exist in the template where the name of the * central repository. %s will exist in the template where the name of the
* new table will be added. * new table will be added.
* *
@ -365,6 +372,31 @@ public class RdbmsCentralRepoFactory {
*/ */
static String getCreateArtifactInstancesTableTemplate(CentralRepoPlatforms selectedPlatform) { static String getCreateArtifactInstancesTableTemplate(CentralRepoPlatforms selectedPlatform) {
// Each "%s" will be replaced with the relevant TYPE_instances table name. // Each "%s" will be replaced with the relevant TYPE_instances table name.
return "CREATE TABLE IF NOT EXISTS %s ("
+ getNumericPrimaryKeyClause("id", selectedPlatform)
+ "case_id integer NOT NULL,"
+ "data_source_id integer NOT NULL,"
+ "value text NOT NULL,"
+ "file_path text NOT NULL,"
+ "known_status integer NOT NULL,"
+ "comment text,"
+ "file_obj_id " + getBigIntType(selectedPlatform) + " ,"
+ "CONSTRAINT %s_multi_unique UNIQUE(data_source_id, value, file_path)" + getOnConflictIgnoreClause(selectedPlatform) + ","
+ "foreign key (case_id) references cases(id) ON UPDATE SET NULL ON DELETE SET NULL,"
+ "foreign key (data_source_id) references data_sources(id) ON UPDATE SET NULL ON DELETE SET NULL)";
}
/**
* Get the template String for creating a new _instances table for Accounts in
* central repository. %s will exist in the template where the name of the
* new table will be added.
*
* @return a String which is a template for creating a _instances table
*/
static String getCreateAccountInstancesTableTemplate(CentralRepoPlatforms selectedPlatform) {
// Each "%s" will be replaced with the relevant TYPE_instances table name.
return "CREATE TABLE IF NOT EXISTS %s (" return "CREATE TABLE IF NOT EXISTS %s ("
+ getNumericPrimaryKeyClause("id", selectedPlatform) + getNumericPrimaryKeyClause("id", selectedPlatform)
+ "case_id integer NOT NULL," + "case_id integer NOT NULL,"