public void BaseStationSession_Constructor_Initialises_To_Known_State_And_Properties_Work() { var session = new BaseStationSession(); TestUtilities.TestProperty(session, "EndTime", null, DateTime.Now); TestUtilities.TestProperty(session, "LocationID", 0, 120); TestUtilities.TestProperty(session, "SessionID", 0, 139); TestUtilities.TestProperty(session, "StartTime", DateTime.MinValue, DateTime.Now); }
public void InsertSession(BaseStationSession session) { ; }
/// <summary> /// Deletes the record passed across. /// </summary> /// <param name="connection"></param> /// <param name="transaction"></param> /// <param name="log"></param> /// <param name="session"></param> public void Delete(IDbConnection connection, IDbTransaction transaction, TextWriter log, BaseStationSession session) { var preparedCommand = PrepareCommand(connection, transaction, "Delete", _DeleteCommandText, 1); Sql.SetParameters(preparedCommand, session.SessionID); Sql.LogCommand(log, preparedCommand.Command); preparedCommand.Command.ExecuteNonQuery(); }
/// <summary> /// Updates an existing record. /// </summary> /// <param name="connection"></param> /// <param name="transaction"></param> /// <param name="log"></param> /// <param name="session"></param> public void Update(IDbConnection connection, IDbTransaction transaction, TextWriter log, BaseStationSession session) { var preparedCommand = PrepareCommand(connection, transaction, "Update", _UpdateCommandText, 4); Sql.SetParameters(preparedCommand, session.LocationID, session.StartTime, session.EndTime, session.SessionID); Sql.LogCommand(log, preparedCommand.Command); preparedCommand.Command.ExecuteNonQuery(); }
/// <summary> /// Inserts a new record and returns the ID. /// </summary> /// <param name="connection"></param> /// <param name="transaction"></param> /// <param name="log"></param> /// <param name="session"></param> /// <returns></returns> public int Insert(IDbConnection connection, IDbTransaction transaction, TextWriter log, BaseStationSession session) { var preparedCommand = PrepareInsert(connection, transaction, "Insert", "SessionID", "LocationID", "StartTime", "EndTime"); return (int)Sql.ExecuteInsert(preparedCommand, log, session.LocationID, session.StartTime, session.EndTime); }
private long AddSession(BaseStationSession session) { long result = 0; using(var connection = new SQLiteConnection(_ConnectionStringBuilder.ConnectionString)) { connection.Open(); using(var command = connection.CreateCommand()) { command.CommandText = "INSERT INTO [Sessions] ([LocationID], [StartTime], [EndTime]) VALUES (?,?,?); SELECT last_insert_rowid();"; command.Parameters.Add(new SQLiteParameter() { Value = session.LocationID }); command.Parameters.Add(new SQLiteParameter() { Value = session.StartTime }); command.Parameters.Add(new SQLiteParameter() { Value = session.EndTime }); result = (long)command.ExecuteScalar(); session.SessionID = (int)result; } } return result; }
public void BaseStationDatabase_CreateDatabaseIfMissing_Creates_Aircraft_Trigger_To_Delete_Flights() { _Database.CreateDatabaseIfMissing(_CreateDatabaseFileName); _Database.FileName = _CreateDatabaseFileName; _Database.WriteSupportEnabled = true; var session = new BaseStationSession() { StartTime = DateTime.Now }; _Database.InsertSession(session); var aircraft = new BaseStationAircraft() { ModeS = "K" }; _Database.InsertAircraft(aircraft); var flight = new BaseStationFlight() { AircraftID = aircraft.AircraftID, SessionID = session.SessionID }; _Database.InsertFlight(flight); _Database.DeleteAircraft(aircraft); Assert.IsNull(_Database.GetFlightById(flight.FlightID)); Assert.AreEqual(session.SessionID, _Database.GetSessions()[0].SessionID); }
/// <summary> /// Closes an open session. /// </summary> private void EndSession() { lock(_SyncLock) { if(_Session != null) { Status = PluginStrings.NotUpdatingDatabase; try { FlushFlights(true); _Session.EndTime = Provider.LocalNow; _Database.UpdateSession(_Session); StatusDescription = null; } catch(ThreadAbortException) { } catch(Exception ex) { Debug.WriteLine(String.Format("BaseStationDatabaseWriter.Plugin.EndSession caught exception {0}", ex.ToString())); StatusDescription = String.Format(PluginStrings.ExceptionCaughtWhenClosingSession, ex.Message); Factory.Singleton.Resolve<ILog>().Singleton.WriteLine("Database writer plugin caught exception on closing session: {0}", ex.ToString()); } finally { _Session = null; } OnStatusChanged(EventArgs.Empty); } } }
public void BaseStationDatabase_CreateDatabaseIfMissing_Creates_Session_Table() { _Database.CreateDatabaseIfMissing(_CreateDatabaseFileName); _Database.FileName = _CreateDatabaseFileName; var location = _Database.GetLocations()[0]; var session = new BaseStationSession() { LocationID = location.LocationID }; _Database.WriteSupportEnabled = true; _Database.InsertSession(session); }
public void BaseStationDatabase_DeleteSession_Throws_If_Writes_Disabled() { var locationId = (int)AddLocation(new BaseStationLocation() { LocationName = "X" }); var session = new BaseStationSession() { LocationID = locationId, StartTime = DateTime.Now }; session.SessionID = (int)AddSession(session); _Database.DeleteSession(session); }
public void BaseStationDatabase_InsertSession_Inserts_Record_Correctly() { _Database.WriteSupportEnabled = true; var locationId = (int)AddLocation(new BaseStationLocation() { LocationName = "X" }); var startTime = new DateTime(2001, 2, 3, 4, 5, 6, 789); var endTime = startTime.AddDays(1); var record = new BaseStationSession() { EndTime = endTime, LocationID = locationId, StartTime = startTime, }; _Database.InsertSession(record); Assert.AreNotEqual(0, record.SessionID); var allSessions = _Database.GetSessions(); Assert.AreEqual(1, allSessions.Count); var readBack = allSessions[0]; Assert.AreEqual(record.SessionID, readBack.SessionID); Assert.AreEqual(locationId, readBack.LocationID); Assert.AreEqual(TruncateDate(startTime), readBack.StartTime); Assert.AreEqual(TruncateDate(endTime), readBack.EndTime); }
/// <summary> /// See interface docs. /// </summary> /// <param name="session"></param> public void DeleteSession(BaseStationSession session) { if(!WriteSupportEnabled) throw new InvalidOperationException("You must enable writes before you can delete a session record"); lock(_ConnectionLock) { OpenConnection(); if(_Connection != null) _SessionsTable.Delete(_Connection, _TransactionHelper.Transaction, _DatabaseLog, session); } }
/// <summary> /// See interface docs. /// </summary> /// <param name="session"></param> public void UpdateSession(BaseStationSession session) { if(!WriteSupportEnabled) throw new InvalidOperationException("You must enable writes before you can update a session record"); session.StartTime = SQLiteDateHelper.Truncate(session.StartTime); session.EndTime = SQLiteDateHelper.Truncate(session.EndTime); lock(_ConnectionLock) { OpenConnection(); if(_Connection != null) _SessionsTable.Update(_Connection, _TransactionHelper.Transaction, _DatabaseLog, session); } }
/// <summary> /// See interface docs. /// </summary> /// <returns></returns> public IList<BaseStationSession> GetSessions() { IList<BaseStationSession> result = null; lock(_ConnectionLock) { OpenConnection(); if(_Connection != null) result = _SessionsTable.GetAllRecords(_Connection, _TransactionHelper.Transaction, _DatabaseLog); else result = new BaseStationSession[] {}; } return result; }
/// <summary> /// Creates a new session. /// </summary> private void StartSession() { lock(_SyncLock) { if(_Session == null) { if(!_Options.Enabled) { Status = PluginStrings.Disabled; StatusDescription = null; } else if(String.IsNullOrEmpty(_Database.FileName)) { Status = PluginStrings.EnabledNoDatabase; StatusDescription = null; } else if(!Provider.FileExists(_Database.FileName)) { Status = PluginStrings.EnabledNotUpdating; StatusDescription = String.Format(PluginStrings.SomethingDoesNotExist, _Database.FileName); } else if(Provider.FileSize(_Database.FileName) == 0L) { Status = PluginStrings.EnabledNotUpdating; StatusDescription = String.Format(PluginStrings.SomethingIsZeroLength, _Database.FileName); } else if(!_Options.AllowUpdateOfOtherDatabases && !DatabaseCreatedByPlugin()) { Status = PluginStrings.EnabledNotUpdating; StatusDescription = PluginStrings.UpdatingDatabasesNotCreatedByPluginForbidden; } else { Status = String.Format(PluginStrings.EnabledAndUpdatingSomething, _Database.FileName); StatusDescription = null; try { _Database.WriteSupportEnabled = true; var location = _Database.GetLocations().OrderBy(c => c.LocationID).FirstOrDefault(); _Session = new BaseStationSession() { LocationID = location == null ? 0 : location.LocationID, StartTime = Provider.LocalNow, }; _Database.InsertSession(_Session); } catch(ThreadAbortException) { } catch(Exception ex) { Debug.WriteLine(String.Format("BaseStationDatabaseWriter.Plugin.StartSession caught exception {0}", ex.ToString())); Status = PluginStrings.EnabledNotUpdating; StatusDescription = String.Format(PluginStrings.ExceptionCaughtWhenStartingSession, ex.Message); Factory.Singleton.Resolve<ILog>().Singleton.WriteLine("Database writer plugin caught exception on starting session: {0}", ex.ToString()); } } OnStatusChanged(EventArgs.Empty); } } }
public void UpdateSession(BaseStationSession session) { ; }
public void DeleteSession(BaseStationSession session) { ; }
public void BaseStationDatabase_CreateDatabaseIfMissing_Creates_Flights_Table() { _Database.CreateDatabaseIfMissing(_CreateDatabaseFileName); _Database.FileName = _CreateDatabaseFileName; var aircraft = new BaseStationAircraft() { ModeS = "1" }; var session = new BaseStationSession() { StartTime = DateTime.Now }; _Database.WriteSupportEnabled = true; _Database.InsertSession(session); _Database.InsertAircraft(aircraft); _Database.InsertFlight(new BaseStationFlight() { AircraftID = aircraft.AircraftID, SessionID = session.SessionID }); }