fixed marr threading bug.

datamapper instances should not be reused!!!
This commit is contained in:
Keivan Beigi 2013-07-15 17:45:49 -07:00
parent 440a128f28
commit 28a919e74a
5 changed files with 24 additions and 24 deletions

View File

@ -18,7 +18,7 @@ namespace NzbDrone.Core.Test.Datastore
public void SingleOrDefault_should_return_null_on_empty_db()
{
Mocker.Resolve<IDatabase>()
.DataMapper.Query<Series>()
.GetDataMapper().Query<Series>()
.SingleOrDefault(c => c.CleanTitle == "SomeTitle")
.Should()
.BeNull();

View File

@ -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)

View File

@ -5,7 +5,7 @@ namespace NzbDrone.Core.Datastore
{
public interface IDatabase
{
IDataMapper DataMapper { get; }
IDataMapper GetDataMapper();
}
public class Database : IDatabase
@ -17,12 +17,9 @@ namespace NzbDrone.Core.Datastore
_dataMapperFactory = dataMapperFactory;
}
public IDataMapper DataMapper
public IDataMapper GetDataMapper()
{
get
{
return _dataMapperFactory();
}
return _dataMapperFactory();
}
}
}

View File

@ -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 @@ namespace NzbDrone.Core.SeriesStats
public class SeriesStatisticsRepository : ISeriesStatisticsRepository
{
private readonly IDataMapper _dataMapper;
private readonly IDatabase _database;
public SeriesStatisticsRepository(IDatabase database)
{
_dataMapper = database.DataMapper;
_database = database;
}
public List<SeriesStatistics> 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 @@ namespace NzbDrone.Core.SeriesStats
FROM Episodes
GROUP BY SeriesId";
return _dataMapper.Query<SeriesStatistics>(queryText);
return mapper.Query<SeriesStatistics>(queryText);
}
}
}

View File

@ -31,12 +31,12 @@ namespace NzbDrone.Core.Tv
public class EpisodeRepository : BasicRepository<Episode>, 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 @@ namespace NzbDrone.Core.Tv
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)