/// <summary> /// Write a table of data. Uses the TableName property of the specified DataTable. /// </summary> /// <param name="table">The data to write.</param> public void WriteTable(DataTable table) { if (table == null) { return; } // NOTE: This can be called from many threads. Don't actually // write to the database on these threads. We have a single worker // thread to do that. Start(); // Delete old rows in table. if (table.Columns.Contains("SimulationName")) { var simulationNames = DataTableUtilities.GetColumnAsStrings(table, "SimulationName").ToList().Distinct(); DeleteOldRowsInTable(table.TableName, "Current", simulationNamesThatMayNeedCleaning: simulationNames); } else { DeleteOldRowsInTable(table.TableName, "Current"); } AddIndexColumns(table, "Current", null, null); lock (lockObject) { commands.Add(new WriteTableCommand(Connection, table)); if (!TablesModified.Contains(table.TableName)) { TablesModified.Add(table.TableName); } } }
/// <summary> /// Add rows to a table in the db file. Note that the data isn't written immediately. /// </summary> /// <param name="data">Name of simulation the values correspond to.</param> public void WriteTable(ReportData data) { // NOTE: This can be called from many threads. Don't actually // write to the database on these threads. We have a single worker // thread to do that. Start(); var table = data.ToTable(); AddIndexColumns(table, "Current", data.SimulationName, data.FolderName); // Add units AddUnits(data.TableName, data.ColumnNames, data.ColumnUnits); // Delete old rows in table. DeleteOldRowsInTable(data.TableName, "Current", new string[] { data.SimulationName }); lock (lockObject) { commands.Add(new WriteTableCommand(Connection, table)); if (!TablesModified.Contains(table.TableName)) { TablesModified.Add(table.TableName); } } }
/// <summary> /// Deletes a table from the database. /// </summary> /// <param name="tableName">Name of the table to be deleted.</param> public void DeleteTable(string tableName) { Connection.ExecuteNonQuery($"DROP TABLE {tableName}"); lock (lockObject) if (!TablesModified.Contains(tableName)) { TablesModified.Add(tableName); } }
/// <summary> /// Write a table of data. Uses the TableName property of the specified DataTable. /// </summary> /// <param name="table">The data to write.</param> /// <param name="deleteAllData">Delete all existing data from this table in the DB before writing the table to the DB?</param> /// <remarks> /// Before simulations are run, all tables whose names don't start with an underscore /// (ie any table generated by Report,ExcelInput, etc) will be cleaned; that is, all /// data associated with the simulation(s) about to be run will be removed from these /// tables. Additionally, the messages and initial conditions tables will also be cleaned. /// /// That being said, any model which can be run *without* running simulations (e.g. /// any post-simulation tool) should always set the second argument to true, to ensure /// that data is deleted. This is necessary because if the user only wants to run /// post simulation-tools, we cannot clean the datastore on a per-simulation basis, /// so no automatic cleaning occurs at all. /// /// Seting the second argument to true when no data exists is not an error. /// </remarks> public void WriteTable(DataTable table, bool deleteAllData = true) { if (table == null) { return; } // NOTE: This can be called from many threads. Don't actually // write to the database on these threads. We have a single worker // thread to do that. Start(); AddIndexColumns(table, "Current", null, null); lock (lockObject) { commands.Add(new WriteTableCommand(Connection, table, deleteAllData)); if (!TablesModified.Contains(table.TableName)) { TablesModified.Add(table.TableName); } } }
/// <summary> /// Deletes a table from the database. /// </summary> /// <param name="tableName">Name of the table to be deleted.</param> public void DeleteTable(string tableName) { string sql; // If there is only 1 checkpointID in the database, we can just drop the table. // If this table doesn't have a CheckpointID column, we also just drop the table. // Otherwise, we delete all data corresponding to the "Current" checkpoint ID. bool tableHasCheckpointID = Connection.GetColumns(tableName).Any(c => c.Item1 == "CheckpointID"); if (checkpointIDs.Count <= 1 || !tableHasCheckpointID) { sql = $"DROP TABLE \"{tableName}\""; } else { int currentCheckpointID = checkpointIDs["Current"].ID; sql = $"DELETE FROM \"{tableName}\" WHERE CheckpointID = {currentCheckpointID}"; } Connection.ExecuteNonQuery(sql); lock (lockObject) if (!TablesModified.Contains(tableName)) { TablesModified.Add(tableName); } }