|
@@ -15,25 +15,19 @@ namespace MediaBrowser.Server.Implementations.Activity
|
|
|
{
|
|
|
public class ActivityRepository : BaseSqliteRepository, IActivityRepository
|
|
|
{
|
|
|
- private IDbConnection _connection;
|
|
|
- private readonly IServerApplicationPaths _appPaths;
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
- private IDbCommand _saveActivityCommand;
|
|
|
-
|
|
|
- public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths)
|
|
|
- : base(logManager)
|
|
|
+ public ActivityRepository(ILogManager logManager, IServerApplicationPaths appPaths, IDbConnector connector)
|
|
|
+ : base(logManager, connector)
|
|
|
{
|
|
|
- _appPaths = appPaths;
|
|
|
+ DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
|
|
|
}
|
|
|
|
|
|
- public async Task Initialize(IDbConnector dbConnector)
|
|
|
+ public async Task Initialize()
|
|
|
{
|
|
|
- var dbFile = Path.Combine(_appPaths.DataPath, "activitylog.db");
|
|
|
-
|
|
|
- _connection = await dbConnector.Connect(dbFile).ConfigureAwait(false);
|
|
|
-
|
|
|
- string[] queries = {
|
|
|
+ using (var connection = await CreateConnection().ConfigureAwait(false))
|
|
|
+ {
|
|
|
+ string[] queries = {
|
|
|
|
|
|
"create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)",
|
|
|
"create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)",
|
|
@@ -44,25 +38,8 @@ namespace MediaBrowser.Server.Implementations.Activity
|
|
|
"pragma shrink_memory"
|
|
|
};
|
|
|
|
|
|
- _connection.RunQueries(queries, Logger);
|
|
|
-
|
|
|
- PrepareStatements();
|
|
|
- }
|
|
|
-
|
|
|
- private void PrepareStatements()
|
|
|
- {
|
|
|
- _saveActivityCommand = _connection.CreateCommand();
|
|
|
- _saveActivityCommand.CommandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)";
|
|
|
-
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Id");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Name");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Overview");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@ShortOverview");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@Type");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@ItemId");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@UserId");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@DateCreated");
|
|
|
- _saveActivityCommand.Parameters.Add(_saveActivityCommand, "@LogSeverity");
|
|
|
+ connection.RunQueries(queries, Logger);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLogEntries";
|
|
@@ -79,128 +56,145 @@ namespace MediaBrowser.Server.Implementations.Activity
|
|
|
throw new ArgumentNullException("entry");
|
|
|
}
|
|
|
|
|
|
- await WriteLock.WaitAsync().ConfigureAwait(false);
|
|
|
+ using (var connection = await CreateConnection().ConfigureAwait(false))
|
|
|
+ {
|
|
|
+ using (var saveActivityCommand = connection.CreateCommand())
|
|
|
+ {
|
|
|
+ saveActivityCommand.CommandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)";
|
|
|
|
|
|
- IDbTransaction transaction = null;
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@Id");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@Name");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@Overview");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@ShortOverview");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@Type");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@ItemId");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@UserId");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@DateCreated");
|
|
|
+ saveActivityCommand.Parameters.Add(saveActivityCommand, "@LogSeverity");
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- transaction = _connection.BeginTransaction();
|
|
|
+ IDbTransaction transaction = null;
|
|
|
|
|
|
- var index = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ transaction = connection.BeginTransaction();
|
|
|
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = new Guid(entry.Id);
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.Name;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.Overview;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.ShortOverview;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.Type;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.ItemId;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.UserId;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.Date;
|
|
|
- _saveActivityCommand.GetParameter(index++).Value = entry.Severity.ToString();
|
|
|
+ var index = 0;
|
|
|
|
|
|
- _saveActivityCommand.Transaction = transaction;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = new Guid(entry.Id);
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.Name;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.Overview;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.ShortOverview;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.Type;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.ItemId;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.UserId;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.Date;
|
|
|
+ saveActivityCommand.GetParameter(index++).Value = entry.Severity.ToString();
|
|
|
|
|
|
- _saveActivityCommand.ExecuteNonQuery();
|
|
|
+ saveActivityCommand.Transaction = transaction;
|
|
|
|
|
|
- transaction.Commit();
|
|
|
- }
|
|
|
- catch (OperationCanceledException)
|
|
|
- {
|
|
|
- if (transaction != null)
|
|
|
- {
|
|
|
- transaction.Rollback();
|
|
|
- }
|
|
|
+ saveActivityCommand.ExecuteNonQuery();
|
|
|
|
|
|
- throw;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Logger.ErrorException("Failed to save record:", e);
|
|
|
+ transaction.Commit();
|
|
|
+ }
|
|
|
+ catch (OperationCanceledException)
|
|
|
+ {
|
|
|
+ if (transaction != null)
|
|
|
+ {
|
|
|
+ transaction.Rollback();
|
|
|
+ }
|
|
|
|
|
|
- if (transaction != null)
|
|
|
- {
|
|
|
- transaction.Rollback();
|
|
|
- }
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Logger.ErrorException("Failed to save record:", e);
|
|
|
|
|
|
- throw;
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- if (transaction != null)
|
|
|
- {
|
|
|
- transaction.Dispose();
|
|
|
- }
|
|
|
+ if (transaction != null)
|
|
|
+ {
|
|
|
+ transaction.Rollback();
|
|
|
+ }
|
|
|
|
|
|
- WriteLock.Release();
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (transaction != null)
|
|
|
+ {
|
|
|
+ transaction.Dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
|
|
|
{
|
|
|
- using (var cmd = _connection.CreateCommand())
|
|
|
+ using (var connection = CreateConnection(true).Result)
|
|
|
{
|
|
|
- cmd.CommandText = BaseActivitySelectText;
|
|
|
-
|
|
|
- var whereClauses = new List<string>();
|
|
|
-
|
|
|
- if (minDate.HasValue)
|
|
|
+ using (var cmd = connection.CreateCommand())
|
|
|
{
|
|
|
- whereClauses.Add("DateCreated>=@DateCreated");
|
|
|
- cmd.Parameters.Add(cmd, "@DateCreated", DbType.Date).Value = minDate.Value;
|
|
|
- }
|
|
|
+ cmd.CommandText = BaseActivitySelectText;
|
|
|
|
|
|
- var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
|
|
- string.Empty :
|
|
|
- " where " + string.Join(" AND ", whereClauses.ToArray());
|
|
|
+ var whereClauses = new List<string>();
|
|
|
|
|
|
- if (startIndex.HasValue && startIndex.Value > 0)
|
|
|
- {
|
|
|
- var pagingWhereText = whereClauses.Count == 0 ?
|
|
|
+ if (minDate.HasValue)
|
|
|
+ {
|
|
|
+ whereClauses.Add("DateCreated>=@DateCreated");
|
|
|
+ cmd.Parameters.Add(cmd, "@DateCreated", DbType.Date).Value = minDate.Value;
|
|
|
+ }
|
|
|
+
|
|
|
+ var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
|
|
string.Empty :
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
|
|
-
|
|
|
- whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})",
|
|
|
- pagingWhereText,
|
|
|
- startIndex.Value.ToString(_usCulture)));
|
|
|
- }
|
|
|
-
|
|
|
- var whereText = whereClauses.Count == 0 ?
|
|
|
- string.Empty :
|
|
|
- " where " + string.Join(" AND ", whereClauses.ToArray());
|
|
|
|
|
|
- cmd.CommandText += whereText;
|
|
|
+ if (startIndex.HasValue && startIndex.Value > 0)
|
|
|
+ {
|
|
|
+ var pagingWhereText = whereClauses.Count == 0 ?
|
|
|
+ string.Empty :
|
|
|
+ " where " + string.Join(" AND ", whereClauses.ToArray());
|
|
|
|
|
|
- cmd.CommandText += " ORDER BY DateCreated DESC";
|
|
|
+ whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})",
|
|
|
+ pagingWhereText,
|
|
|
+ startIndex.Value.ToString(_usCulture)));
|
|
|
+ }
|
|
|
|
|
|
- if (limit.HasValue)
|
|
|
- {
|
|
|
- cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
|
|
|
- }
|
|
|
+ var whereText = whereClauses.Count == 0 ?
|
|
|
+ string.Empty :
|
|
|
+ " where " + string.Join(" AND ", whereClauses.ToArray());
|
|
|
|
|
|
- cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;
|
|
|
+ cmd.CommandText += whereText;
|
|
|
|
|
|
- var list = new List<ActivityLogEntry>();
|
|
|
- var count = 0;
|
|
|
+ cmd.CommandText += " ORDER BY DateCreated DESC";
|
|
|
|
|
|
- using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
|
|
|
- {
|
|
|
- while (reader.Read())
|
|
|
+ if (limit.HasValue)
|
|
|
{
|
|
|
- list.Add(GetEntry(reader));
|
|
|
+ cmd.CommandText += " LIMIT " + limit.Value.ToString(_usCulture);
|
|
|
}
|
|
|
|
|
|
- if (reader.NextResult() && reader.Read())
|
|
|
+ cmd.CommandText += "; select count (Id) from ActivityLogEntries" + whereTextWithoutPaging;
|
|
|
+
|
|
|
+ var list = new List<ActivityLogEntry>();
|
|
|
+ var count = 0;
|
|
|
+
|
|
|
+ using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
|
|
|
{
|
|
|
- count = reader.GetInt32(0);
|
|
|
+ while (reader.Read())
|
|
|
+ {
|
|
|
+ list.Add(GetEntry(reader));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (reader.NextResult() && reader.Read())
|
|
|
+ {
|
|
|
+ count = reader.GetInt32(0);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return new QueryResult<ActivityLogEntry>()
|
|
|
- {
|
|
|
- Items = list.ToArray(),
|
|
|
- TotalRecordCount = count
|
|
|
- };
|
|
|
+ return new QueryResult<ActivityLogEntry>()
|
|
|
+ {
|
|
|
+ Items = list.ToArray(),
|
|
|
+ TotalRecordCount = count
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -260,19 +254,5 @@ namespace MediaBrowser.Server.Implementations.Activity
|
|
|
|
|
|
return info;
|
|
|
}
|
|
|
-
|
|
|
- protected override void CloseConnection()
|
|
|
- {
|
|
|
- if (_connection != null)
|
|
|
- {
|
|
|
- if (_connection.IsOpen())
|
|
|
- {
|
|
|
- _connection.Close();
|
|
|
- }
|
|
|
-
|
|
|
- _connection.Dispose();
|
|
|
- _connection = null;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|