From 28a919e74a2ed01d395f28d00693a91c7e854b7c Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Mon, 15 Jul 2013 17:45:49 -0700 Subject: [PATCH] fixed marr threading bug. datamapper instances should not be reused!!! --- .../Datastore/ObjectDatabaseFixture.cs | 2 +- NzbDrone.Core/Datastore/BasicRepository.cs | 2 +- NzbDrone.Core/Datastore/Database.cs | 9 +++----- .../SeriesStats/SeriesStatisticsRepository.cs | 13 ++++++----- NzbDrone.Core/Tv/EpisodeRepository.cs | 22 ++++++++++--------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index 6d805f83d..30f412d1b 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -18,7 +18,7 @@ public class DatabaseFixture : DbTest public void SingleOrDefault_should_return_null_on_empty_db() { Mocker.Resolve() - .DataMapper.Query() + .GetDataMapper().Query() .SingleOrDefault(c => c.CleanTitle == "SomeTitle") .Should() .BeNull(); diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index bb49496ef..6447e67d5 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -43,7 +43,7 @@ namespace NzbDrone.Core.Datastore private IDataMapper DataMapper { - get { return _database.DataMapper; } + get { return _database.GetDataMapper(); } } public BasicRepository(IDatabase database, IMessageAggregator messageAggregator) diff --git a/NzbDrone.Core/Datastore/Database.cs b/NzbDrone.Core/Datastore/Database.cs index 963761bbb..0890699fc 100644 --- a/NzbDrone.Core/Datastore/Database.cs +++ b/NzbDrone.Core/Datastore/Database.cs @@ -5,7 +5,7 @@ namespace NzbDrone.Core.Datastore { public interface IDatabase { - IDataMapper DataMapper { get; } + IDataMapper GetDataMapper(); } public class Database : IDatabase @@ -17,12 +17,9 @@ public Database(Func dataMapperFactory) _dataMapperFactory = dataMapperFactory; } - public IDataMapper DataMapper + public IDataMapper GetDataMapper() { - get - { - return _dataMapperFactory(); - } + return _dataMapperFactory(); } } } \ No newline at end of file diff --git a/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs b/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs index c813e3bb6..5d9413d87 100644 --- a/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs +++ b/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using Marr.Data; using NzbDrone.Core.Datastore; namespace NzbDrone.Core.SeriesStats @@ -12,18 +11,20 @@ public interface ISeriesStatisticsRepository public class SeriesStatisticsRepository : ISeriesStatisticsRepository { - private readonly IDataMapper _dataMapper; + private readonly IDatabase _database; public SeriesStatisticsRepository(IDatabase database) { - _dataMapper = database.DataMapper; + _database = database; } public List SeriesStatistics() { - _dataMapper.AddParameter("currentDate", DateTime.UtcNow); + var mapper = _database.GetDataMapper(); - var queryText = @"SELECT + mapper.AddParameter("currentDate", DateTime.UtcNow); + + const string queryText = @"SELECT SeriesId, SUM(CASE WHEN Monitored = 1 AND Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount, SUM(CASE WHEN Monitored = 1 AND Episodes.EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount, @@ -32,7 +33,7 @@ public List SeriesStatistics() FROM Episodes GROUP BY SeriesId"; - return _dataMapper.Query(queryText); + return mapper.Query(queryText); } } } diff --git a/NzbDrone.Core/Tv/EpisodeRepository.cs b/NzbDrone.Core/Tv/EpisodeRepository.cs index 178bc4e9c..2c2e8d2db 100644 --- a/NzbDrone.Core/Tv/EpisodeRepository.cs +++ b/NzbDrone.Core/Tv/EpisodeRepository.cs @@ -31,12 +31,12 @@ public interface IEpisodeRepository : IBasicRepository public class EpisodeRepository : BasicRepository, IEpisodeRepository { - private readonly IDataMapper _dataMapper; + private readonly IDatabase _database; public EpisodeRepository(IDatabase database, IMessageAggregator messageAggregator) : base(database, messageAggregator) { - _dataMapper = database.DataMapper; + _database = database; } public Episode Get(int seriesId, int season, int episodeNumber) @@ -135,16 +135,18 @@ public void SetMonitoredFlat(Episode episode, bool monitored) public void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored) { - _dataMapper.AddParameter("seriesId", seriesId); - _dataMapper.AddParameter("seasonNumber", seasonNumber); - _dataMapper.AddParameter("monitored", monitored); + var mapper = _database.GetDataMapper(); - var sql = "UPDATE Episodes " + - "SET Monitored = @monitored " + - "WHERE SeriesId = @seriesId " + - "AND SeasonNumber = @seasonNumber"; + mapper.AddParameter("seriesId", seriesId); + mapper.AddParameter("seasonNumber", seasonNumber); + mapper.AddParameter("monitored", monitored); - _dataMapper.ExecuteNonQuery(sql); + const string sql = "UPDATE Episodes " + + "SET Monitored = @monitored " + + "WHERE SeriesId = @seriesId " + + "AND SeasonNumber = @seasonNumber"; + + mapper.ExecuteNonQuery(sql); } public void SetFileId(int episodeId, int fileId)