Skip to content
Snippets Groups Projects
Commit f76ace0e authored by Raman's avatar Raman
Browse files

1171: Create IG table schema versions

parent 493cf4e7
No related branches found
No related tags found
No related merge requests found
/*
* Sleuth Kit Data Model
*
* Copyright 2018 Basis Technology Corp.
* Copyright 2018-2019 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -68,6 +68,51 @@ public interface CaseDbAccessQueryCallback {
this.tskDB = skCase;
}
/**
* Checks if a column exists in a table.
*
* @param tableName name of the table
* @param columnName column name to check
*
* @return true if the column already exists, false otherwise
* @throws TskCoreException
*/
public boolean doesColumnExist(String tableName, String columnName) throws TskCoreException {
boolean columnExists = false;
Statement statement = null;
CaseDbConnection connection = tskDB.getConnection();
try {
statement = connection.createStatement();
if (DbType.SQLITE == tskDB.getDatabaseType()) {
String tableInfoQuery = "PRAGMA table_info(%s)"; //NON-NLS
ResultSet resultSet = statement.executeQuery(String.format(tableInfoQuery, tableName));
while (resultSet.next()) {
// the second value is the column name
if (resultSet.getString(2).equals(columnName)) {
columnExists = true;
break;
}
}
resultSet.close();
}
else {
String tableInfoQueryTemplate = "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='%s' AND column_name='%s')"; //NON-NLS
ResultSet resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName.toLowerCase(), columnName.toLowerCase()));
if (resultSet.next()) {
columnExists = resultSet.getBoolean(1);
}
}
}
catch (SQLException ex) {
throw new TskCoreException("Error checking if column " + columnName + "exists ", ex);
} finally {
closeStatement(statement);
connection.close();
}
return columnExists;
}
/**
* Creates a table with the specified name and schema.
*
......@@ -104,6 +149,37 @@ public void createTable(final String tableName, final String tableSchema) throws
}
/**
* Alters a table with the specified name.
*
* @param tableName name of the table to alter
* @param alterSQL SQL to alter the table
*
* @throws TskCoreException
*/
public void alterTable(final String tableName, final String alterSQL) throws TskCoreException {
validateTableName(tableName);
validateSQL(alterSQL);
CaseDbConnection connection = tskDB.getConnection();
tskDB.acquireSingleUserCaseWriteLock();
Statement statement = null;
String sql = "ALTER TABLE " + tableName + " " + alterSQL;
try {
statement = connection.createStatement();
statement.execute(sql);
} catch (SQLException ex) {
throw new TskCoreException(String.format("Error altering table %s with SQL = %s", tableName, sql), ex);
} finally {
closeStatement(statement);
connection.close();
tskDB.releaseSingleUserCaseWriteLock();
}
}
/**
* Creates an index on the specified table, on specified column(s).
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment