Clean up and document ActivityRepository.cs

This commit is contained in:
Patrick Barron 2020-04-14 15:04:54 -04:00
parent af4d617df2
commit f6899de338

View File

@ -1,5 +1,3 @@
#pragma warning disable CS1591
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -15,11 +13,18 @@ using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Activity namespace Emby.Server.Implementations.Activity
{ {
/// <inheritdoc cref="IActivityRepository" />
public class ActivityRepository : BaseSqliteRepository, IActivityRepository public class ActivityRepository : BaseSqliteRepository, IActivityRepository
{ {
private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US")); private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US"));
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="ActivityRepository"/> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="appPaths">The server application paths.</param>
/// <param name="fileSystem">The filesystem.</param>
public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem) public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem)
: base(loggerFactory.CreateLogger(nameof(ActivityRepository))) : base(loggerFactory.CreateLogger(nameof(ActivityRepository)))
{ {
@ -27,6 +32,9 @@ namespace Emby.Server.Implementations.Activity
_fileSystem = fileSystem; _fileSystem = fileSystem;
} }
/// <summary>
/// Initializes the <see cref="ActivityRepository"/>.
/// </summary>
public void Initialize() public void Initialize()
{ {
try try
@ -45,16 +53,14 @@ namespace Emby.Server.Implementations.Activity
private void InitializeInternal() private void InitializeInternal()
{ {
using (var connection = GetConnection()) using var connection = GetConnection();
connection.RunQueries(new[]
{ {
connection.RunQueries(new[] "create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)",
{ "drop index if exists idx_ActivityLogEntries"
"create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)", });
"drop index if exists idx_ActivityLogEntries"
});
TryMigrate(connection); TryMigrate(connection);
}
} }
private void TryMigrate(ManagedConnection connection) private void TryMigrate(ManagedConnection connection)
@ -78,6 +84,7 @@ namespace Emby.Server.Implementations.Activity
private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLog"; private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLog";
/// <inheritdoc />
public void Create(ActivityLogEntry entry) public void Create(ActivityLogEntry entry)
{ {
if (entry == null) if (entry == null)
@ -85,37 +92,38 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry)); throw new ArgumentNullException(nameof(entry));
} }
using (var connection = GetConnection()) using var connection = GetConnection();
connection.RunInTransaction(db =>
{ {
connection.RunInTransaction(db => using var statement = db.PrepareStatement("insert into ActivityLog (Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)");
statement.TryBind("@Name", entry.Name);
statement.TryBind("@Overview", entry.Overview);
statement.TryBind("@ShortOverview", entry.ShortOverview);
statement.TryBind("@Type", entry.Type);
statement.TryBind("@ItemId", entry.ItemId);
if (entry.UserId.Equals(Guid.Empty))
{ {
using (var statement = db.PrepareStatement("insert into ActivityLog (Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) statement.TryBindNull("@UserId");
{ }
statement.TryBind("@Name", entry.Name); else
{
statement.TryBind("@UserId", entry.UserId.ToString("N", CultureInfo.InvariantCulture));
}
statement.TryBind("@Overview", entry.Overview); statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue());
statement.TryBind("@ShortOverview", entry.ShortOverview); statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.TryBind("@Type", entry.Type);
statement.TryBind("@ItemId", entry.ItemId);
if (entry.UserId.Equals(Guid.Empty)) statement.MoveNext();
{ }, TransactionMode);
statement.TryBindNull("@UserId");
}
else
{
statement.TryBind("@UserId", entry.UserId.ToString("N", CultureInfo.InvariantCulture));
}
statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue());
statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.MoveNext();
}
}, TransactionMode);
}
} }
/// <summary>
/// Adds the provided <see cref="ActivityLogEntry"/> to this repository.
/// </summary>
/// <param name="entry">The activity log entry.</param>
/// <exception cref="ArgumentNullException">If entry is null.</exception>
public void Update(ActivityLogEntry entry) public void Update(ActivityLogEntry entry)
{ {
if (entry == null) if (entry == null)
@ -123,38 +131,35 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry)); throw new ArgumentNullException(nameof(entry));
} }
using (var connection = GetConnection()) using var connection = GetConnection();
connection.RunInTransaction(db =>
{ {
connection.RunInTransaction(db => using var statement = db.PrepareStatement("Update ActivityLog set Name=@Name,Overview=@Overview,ShortOverview=@ShortOverview,Type=@Type,ItemId=@ItemId,UserId=@UserId,DateCreated=@DateCreated,LogSeverity=@LogSeverity where Id=@Id");
statement.TryBind("@Id", entry.Id);
statement.TryBind("@Name", entry.Name);
statement.TryBind("@Overview", entry.Overview);
statement.TryBind("@ShortOverview", entry.ShortOverview);
statement.TryBind("@Type", entry.Type);
statement.TryBind("@ItemId", entry.ItemId);
if (entry.UserId.Equals(Guid.Empty))
{ {
using (var statement = db.PrepareStatement("Update ActivityLog set Name=@Name,Overview=@Overview,ShortOverview=@ShortOverview,Type=@Type,ItemId=@ItemId,UserId=@UserId,DateCreated=@DateCreated,LogSeverity=@LogSeverity where Id=@Id")) statement.TryBindNull("@UserId");
{ }
statement.TryBind("@Id", entry.Id); else
{
statement.TryBind("@UserId", entry.UserId.ToString("N", CultureInfo.InvariantCulture));
}
statement.TryBind("@Name", entry.Name); statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue());
statement.TryBind("@Overview", entry.Overview); statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.TryBind("@ShortOverview", entry.ShortOverview);
statement.TryBind("@Type", entry.Type);
statement.TryBind("@ItemId", entry.ItemId);
if (entry.UserId.Equals(Guid.Empty)) statement.MoveNext();
{ }, TransactionMode);
statement.TryBindNull("@UserId");
}
else
{
statement.TryBind("@UserId", entry.UserId.ToString("N", CultureInfo.InvariantCulture));
}
statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue());
statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.MoveNext();
}
}, TransactionMode);
}
} }
/// <inheritdoc />
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit) public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit)
{ {
var commandText = BaseActivitySelectText; var commandText = BaseActivitySelectText;
@ -164,16 +169,10 @@ namespace Emby.Server.Implementations.Activity
{ {
whereClauses.Add("DateCreated>=@DateCreated"); whereClauses.Add("DateCreated>=@DateCreated");
} }
if (hasUserId.HasValue) if (hasUserId.HasValue)
{ {
if (hasUserId.Value) whereClauses.Add(hasUserId.Value ? "UserId not null" : "UserId is null");
{
whereClauses.Add("UserId not null");
}
else
{
whereClauses.Add("UserId is null");
}
} }
var whereTextWithoutPaging = whereClauses.Count == 0 ? var whereTextWithoutPaging = whereClauses.Count == 0 ?
@ -216,38 +215,33 @@ namespace Emby.Server.Implementations.Activity
var list = new List<ActivityLogEntry>(); var list = new List<ActivityLogEntry>();
var result = new QueryResult<ActivityLogEntry>(); var result = new QueryResult<ActivityLogEntry>();
using (var connection = GetConnection(true)) using var connection = GetConnection(true);
{ connection.RunInTransaction(
connection.RunInTransaction( db =>
db => {
var statements = PrepareAll(db, statementTexts).ToList();
using (var statement = statements[0])
{ {
var statements = PrepareAll(db, statementTexts).ToList(); if (minDate.HasValue)
using (var statement = statements[0])
{ {
if (minDate.HasValue) statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
{
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
}
foreach (var row in statement.ExecuteQuery())
{
list.Add(GetEntry(row));
}
} }
using (var statement = statements[1]) list.AddRange(statement.ExecuteQuery().Select(GetEntry));
{ }
if (minDate.HasValue)
{
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
}
result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); using (var statement = statements[1])
{
if (minDate.HasValue)
{
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
} }
},
ReadTransactionMode); result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First();
} }
},
ReadTransactionMode);
result.Items = list; result.Items = list;
return result; return result;