/// <summary>Called to run the command. Can throw on error.</summary> /// <param name="cancelToken">Is cancellation pending?</param> public void Run(CancellationTokenSource cancelToken) { if (database.TableExists(table)) { string sql = string.Empty; bool dropEntireTable = false; if (simIds == null) { if (checkId == 0) { dropEntireTable = true; } else { sql = string.Format("DELETE FROM [{0}] WHERE [CheckpointID]={1}", table, checkId); } } else { List <string> columns = database.GetTableColumns(table); string ids = StringUtilities.BuildString(simIds, ","); if (columns.Contains("SimulationID")) { sql = string.Format("DELETE FROM [{0}] WHERE [CheckpointID]={1} AND [SimulationID] IN ({2})", table, checkId, ids); } else if (columns.Contains("SimulationName")) { sql = string.Format("DELETE T FROM [{0}] T INNER JOIN [_Simulations] S ON T.[SimulationName]=S.[SimulationName] WHERE [SimulationID] IN ({1})", table, ids); if (checkId != 0) { sql += string.Format(" AND [CheckpointID]={0}", checkId); } } else { dropEntireTable = true; } } if (dropEntireTable) { database.DropTable(table); } else { database.ExecuteNonQuery(sql); // If there are no rows left in table then drop the table. if (database.TableIsEmpty(table)) { database.DropTable(table); } } } }
/// <summary>Called to run the command. Can throw on error.</summary> /// <param name="cancelToken">Is cancellation pending?</param> public void Run(CancellationTokenSource cancelToken) { if (database == null) { return; } if (database.TableExists("_Checkpoints")) { var checkpointData = new DataView(database.ExecuteQuery("SELECT * FROM [_Checkpoints]")); checkpointData.RowFilter = "Name='Current'"; if (checkpointData.Count == 1) { int checkId = Convert.ToInt32(checkpointData[0]["ID"], CultureInfo.InvariantCulture); // Delete current data from all tables. foreach (string tableName in database.GetTableNames()) { if (!tableName.StartsWith("_") && database.TableExists(tableName)) { database.ExecuteNonQuery(string.Format("DELETE FROM [{0}] WHERE [CheckpointID]={1}", tableName, checkId)); } } } } else { // No checkpoint information, so get rid of everything // Delete current data from all tables. foreach (string tableName in database.GetTableNames()) { if (!tableName.StartsWith("_")) { database.ExecuteNonQuery(string.Format("DELETE FROM [{0}]", tableName)); } } } // Delete empty tables. List <string> tableNamesToDelete = new List <string>(); bool allTablesEmpty = true; foreach (string tableName in database.GetTableNames()) { if (!tableName.StartsWith("_")) { if (database.TableIsEmpty(tableName)) { tableNamesToDelete.Add(tableName); } else { allTablesEmpty = false; } } } // If all data tables were emptied then delete all tables. if (allTablesEmpty) { tableNamesToDelete = database.GetTableNames(); } foreach (string tableName in tableNamesToDelete) { database.DropTable(tableName); } }