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

Address review comments:

 - alterTable() api supports a transaction
 - renamed doesColumnExist() to columnExists(), also added transaction support to it.
parent f76ace0e
No related branches found
No related tags found
No related merge requests found
...@@ -77,12 +77,44 @@ public interface CaseDbAccessQueryCallback { ...@@ -77,12 +77,44 @@ public interface CaseDbAccessQueryCallback {
* @return true if the column already exists, false otherwise * @return true if the column already exists, false otherwise
* @throws TskCoreException * @throws TskCoreException
*/ */
public boolean doesColumnExist(String tableName, String columnName) throws TskCoreException { public boolean columnExists(String tableName, String columnName) throws TskCoreException {
boolean doesColumnExists = false;
CaseDbTransaction localTrans = tskDB.beginTransaction();
try {
doesColumnExists = columnExists(tableName, columnName, localTrans);
localTrans.commit();
localTrans = null;
}
finally {
if (null != localTrans) {
try {
localTrans.rollback();
} catch (TskCoreException ex) {
logger.log(Level.SEVERE, "Failed to rollback transaction after exception", ex);
}
}
}
return doesColumnExists;
}
/**
* Checks if a column exists in a table.
*
* @param tableName name of the table
* @param columnName column name to check
* @param transaction transaction
*
* @return true if the column already exists, false otherwise
* @throws TskCoreException
*/
public boolean columnExists(String tableName, String columnName, CaseDbTransaction transaction) throws TskCoreException {
boolean columnExists = false; boolean columnExists = false;
Statement statement = null; Statement statement = null;
CaseDbConnection connection = tskDB.getConnection();
try { try {
CaseDbConnection connection = transaction.getConnection();
statement = connection.createStatement(); statement = connection.createStatement();
if (DbType.SQLITE == tskDB.getDatabaseType()) { if (DbType.SQLITE == tskDB.getDatabaseType()) {
String tableInfoQuery = "PRAGMA table_info(%s)"; //NON-NLS String tableInfoQuery = "PRAGMA table_info(%s)"; //NON-NLS
...@@ -108,7 +140,6 @@ public boolean doesColumnExist(String tableName, String columnName) throws TskCo ...@@ -108,7 +140,6 @@ public boolean doesColumnExist(String tableName, String columnName) throws TskCo
throw new TskCoreException("Error checking if column " + columnName + "exists ", ex); throw new TskCoreException("Error checking if column " + columnName + "exists ", ex);
} finally { } finally {
closeStatement(statement); closeStatement(statement);
connection.close();
} }
return columnExists; return columnExists;
} }
...@@ -158,12 +189,39 @@ public void createTable(final String tableName, final String tableSchema) throws ...@@ -158,12 +189,39 @@ public void createTable(final String tableName, final String tableSchema) throws
* @throws TskCoreException * @throws TskCoreException
*/ */
public void alterTable(final String tableName, final String alterSQL) throws TskCoreException { public void alterTable(final String tableName, final String alterSQL) throws TskCoreException {
CaseDbTransaction localTrans = tskDB.beginTransaction();
try {
alterTable(tableName, alterSQL, localTrans);
localTrans.commit();
localTrans = null;
} finally {
if (null != localTrans) {
try {
localTrans.rollback();
} catch (TskCoreException ex) {
logger.log(Level.SEVERE, "Failed to rollback transaction after exception", ex);
}
}
}
}
/**
* Alters a table with the specified name.
*
* @param tableName name of the table to alter
* @param alterSQL SQL to alter the table
* @param transaction transaction
*
* @throws TskCoreException
*/
public void alterTable(final String tableName, final String alterSQL, final CaseDbTransaction transaction) throws TskCoreException {
validateTableName(tableName); validateTableName(tableName);
validateSQL(alterSQL); validateSQL(alterSQL);
CaseDbConnection connection = tskDB.getConnection(); CaseDbConnection connection = transaction.getConnection();
tskDB.acquireSingleUserCaseWriteLock(); transaction.acquireSingleUserCaseWriteLock();
Statement statement = null; Statement statement = null;
String sql = "ALTER TABLE " + tableName + " " + alterSQL; String sql = "ALTER TABLE " + tableName + " " + alterSQL;
...@@ -175,8 +233,7 @@ public void alterTable(final String tableName, final String alterSQL) throws Tsk ...@@ -175,8 +233,7 @@ public void alterTable(final String tableName, final String alterSQL) throws Tsk
throw new TskCoreException(String.format("Error altering table %s with SQL = %s", tableName, sql), ex); throw new TskCoreException(String.format("Error altering table %s with SQL = %s", tableName, sql), ex);
} finally { } finally {
closeStatement(statement); closeStatement(statement);
connection.close(); // NOTE: write lock will be released by transaction
tskDB.releaseSingleUserCaseWriteLock();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment