/// <summary>
 /// Adds a new record to the table.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="transaction"></param>
 /// <param name="log"></param>
 /// <param name="session"></param>
 public void Insert(IDbConnection connection, IDbTransaction transaction, TextWriter log, LogSession session)
 {
     SqlPreparedCommand command = PrepareInsert(connection, transaction, "Insert",
         "Id",
         "ClientId", "StartTime", "EndTime", "CountRequests", "OtherBytesSent",
         "HtmlBytesSent", "JsonBytesSent", "ImageBytesSent", "AudioBytesSent");
     session.Id = Sql.ExecuteInsert(command, log,
         session.ClientId, session.StartTime, session.EndTime, session.CountRequests, session.OtherBytesSent,
         session.HtmlBytesSent, session.JsonBytesSent, session.ImageBytesSent, session.AudioBytesSent);
 }
        public void LogSession_TotalBytesSent_Sums_Together_All_BytesSent_Properties()
        {
            var logSession = new LogSession();

            foreach(var propertyInfo in typeof(LogSession).GetProperties().OfType<PropertyInfo>().Where(p => p.Name.EndsWith("BytesSent") && p.Name != "TotalBytesSent")) {
                propertyInfo.SetValue(logSession, 42L, null);
                Assert.AreEqual(42L, logSession.TotalBytesSent, propertyInfo.Name);

                propertyInfo.SetValue(logSession, 0L, null);
                Assert.AreEqual(0L, logSession.TotalBytesSent);
            }
        }
        public void LogSession_Constructor_Initialises_To_Known_State_And_Properties_Work()
        {
            var logSession = new LogSession();

            TestUtilities.TestProperty(logSession, "AudioBytesSent", 0L, 102L);
            TestUtilities.TestProperty(logSession, "ClientId", 0L, 1291L);
            TestUtilities.TestProperty(logSession, "CountRequests", 0L, 12L);
            TestUtilities.TestProperty(logSession, "Duration", TimeSpan.Zero, new TimeSpan(9L));
            TestUtilities.TestProperty(logSession, "EndTime", DateTime.MinValue, DateTime.Now);
            TestUtilities.TestProperty(logSession, "HtmlBytesSent", 0L, 23L);
            TestUtilities.TestProperty(logSession, "Id", 0L, 234L);
            TestUtilities.TestProperty(logSession, "ImageBytesSent", 0L, 45L);
            TestUtilities.TestProperty(logSession, "JsonBytesSent", 0L, 98L);
            TestUtilities.TestProperty(logSession, "OtherBytesSent", 0L, 9873L);
            TestUtilities.TestProperty(logSession, "StartTime", DateTime.MinValue, DateTime.Now);
        }
 public void UpdateSession(LogSession session)
 {
     ;
 }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="session"></param>
        public void UpdateSession(LogSession session)
        {
            if(session == null) throw new ArgumentNullException("session");
            if(_Connection == null) throw new InvalidOperationException("The connection must be opened before the session can be updated");

            lock(_SyncLock) {
                _SessionTable.Update(_Connection, _TransactionHelper.Transaction, null, session);
            }
        }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public LogSession EstablishSession(string ipAddress)
        {
            LogSession result = null;

            lock(_SyncLock) {
                CreateConnection();

                var client = _ClientTable.SelectByIpAddress(_Connection, _TransactionHelper.Transaction, null, ipAddress);
                if(client == null) {
                    client = new LogClient() { IpAddress = ipAddress };
                    _ClientTable.Insert(_Connection, _TransactionHelper.Transaction, null, client);
                }

                result = new LogSession() {
                    ClientId = client.Id,
                    StartTime = Provider.UtcNow,
                    EndTime = Provider.UtcNow,
                };
                _SessionTable.Insert(_Connection, _TransactionHelper.Transaction, null, result);
            }

            return result;
        }
        /// <summary>
        /// Executes a select command that returns a collection of records.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        private List<LogSession> PerformSelect(SqlPreparedCommand command)
        {
            var result = new List<LogSession>();

            using(IDataReader reader = command.Command.ExecuteReader()) {
                while(reader.Read()) {
                    var session = new LogSession() {
                        Id = Sql.GetInt64(reader, "Id"),
                        ClientId = Sql.GetInt64(reader, "ClientId"),
                        StartTime = Sql.GetDateTime(reader, "StartTime"),
                        EndTime = Sql.GetDateTime(reader, "EndTime"),
                        CountRequests = Sql.GetInt64(reader, "CountRequests"),
                        OtherBytesSent = Sql.GetInt64(reader, "OtherBytesSent"),
                        HtmlBytesSent = Sql.GetInt64(reader, "HtmlBytesSent"),
                        JsonBytesSent = Sql.GetInt64(reader, "JsonBytesSent"),
                        ImageBytesSent = Sql.GetInt64(reader, "ImageBytesSent"),
                        AudioBytesSent = Sql.GetInt64(reader, "AudioBytesSent"),
                    };

                    result.Add(session);
                }
            }

            return result;
        }
 /// <summary>
 /// Removes an existing record from the database.
 /// </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, LogSession session)
 {
     SqlPreparedCommand command = PrepareCommand(connection, transaction, "Delete",
         String.Format("DELETE FROM [{0}] WHERE [Id] = ?", TableName), 1);
     Sql.SetParameters(command, session.Id);
     Sql.LogCommand(log, command.Command);
     command.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, LogSession session)
 {
     SqlPreparedCommand command = PrepareCommand(connection, transaction, "Update",
         String.Format("UPDATE [{0}] SET" +
                       " [ClientId] = ?, [StartTime] = ?, [EndTime] = ?, [CountRequests] = ?, " +
                       " [OtherBytesSent] = ?, [HtmlBytesSent] = ?, [JsonBytesSent] = ?, " +
                       " [ImageBytesSent] = ?, [AudioBytesSent] = ?" +
                       " WHERE [Id] = ?", TableName), 10);
     Sql.SetParameters(command, session.ClientId, session.StartTime, session.EndTime, session.CountRequests,
                       session.OtherBytesSent, session.HtmlBytesSent, session.JsonBytesSent,
                       session.ImageBytesSent, session.AudioBytesSent,
                       session.Id);
     Sql.LogCommand(log, command.Command);
     command.Command.ExecuteNonQuery();
 }