Fixed: DataMapper not being disposed, leading to resource leakage.

pull/2/head
Leonardo Galli 6 years ago
parent d38562664d
commit 8e7b718209

3842
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -44,6 +44,8 @@ namespace NzbDrone.Common.Instrumentation
return;
}
}
Console.WriteLine(exception.StackTrace);
Console.WriteLine("EPIC FAIL: {0}", exception);
Logger.Fatal(exception, "EPIC FAIL: " + exception.Message);

@ -20,12 +20,12 @@ namespace NzbDrone.Core.Authentication
public User FindUser(string username)
{
return Query.Where(u => u.Username == username).SingleOrDefault();
return Query(q => q.Where(u => u.Username == username).SingleOrDefault());
}
public User FindUser(Guid identifier)
{
return Query.Where(u => u.Identifier == identifier).SingleOrDefault();
return Query(q => q.Where(u => u.Identifier == identifier).SingleOrDefault());
}
}
}

@ -26,7 +26,11 @@ namespace NzbDrone.Core.Backup
public void BackupDatabase(IDatabase database, string targetDirectory)
{
var sourceConnectionString = database.GetDataMapper().ConnectionString;
var sourceConnectionString = "";
using (var db = database.GetDataMapper())
{
sourceConnectionString = db.ConnectionString;
}
var backupConnectionStringBuilder = new SQLiteConnectionStringBuilder(sourceConnectionString);
backupConnectionStringBuilder.DataSource = Path.Combine(targetDirectory, Path.GetFileName(backupConnectionStringBuilder.DataSource));

@ -22,19 +22,19 @@ namespace NzbDrone.Core.Blacklisting
public List<Blacklist> BlacklistedByTitle(int movieId, string sourceTitle)
{
return Query.Where(e => e.MovieId == movieId)
.AndWhere(e => e.SourceTitle.Contains(sourceTitle));
return Query(q => q.Where(e => e.MovieId == movieId)
.AndWhere(e => e.SourceTitle.Contains(sourceTitle)).ToList());
}
public List<Blacklist> BlacklistedByTorrentInfoHash(int movieId, string torrentInfoHash)
{
return Query.Where(e => e.MovieId == movieId)
.AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash));
return Query(q => q.Where(e => e.MovieId == movieId)
.AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash)).ToList());
}
public List<Blacklist> BlacklistedByMovie(int movieId)
{
return Query.Where(b => b.MovieId == movieId);
return Query(q => q.Where(b => b.MovieId == movieId).ToList());
}
protected override SortBuilder<Blacklist> GetPagedQuery(QueryBuilder<Blacklist> query, PagingSpec<Blacklist> pagingSpec)

@ -21,7 +21,7 @@ namespace NzbDrone.Core.Configuration
public Config Get(string key)
{
return Query.Where(c => c.Key == key).SingleOrDefault();
return Query(q => q.Where(c => c.Key == key).SingleOrDefault());
}
public Config Upsert(string key, string value)

@ -42,7 +42,10 @@ namespace NzbDrone.Core.Datastore
private readonly IDatabase _database;
private readonly IEventAggregator _eventAggregator;
protected IDataMapper DataMapper => _database.GetDataMapper();
protected IDataMapper DataMapper()
{
return _database.GetDataMapper();
}
public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
{
@ -50,26 +53,40 @@ namespace NzbDrone.Core.Datastore
_eventAggregator = eventAggregator;
}
protected QueryBuilder<TModel> Query => AddJoinQueries(DataMapper.Query<TModel>());
protected T Query<T>(Func<QueryBuilder<TModel>, T> finalizeQuery)
{
using (var mapper = DataMapper())
{
var query = AddJoinQueries(mapper.Query<TModel>());
return finalizeQuery(query);
}
}
protected void Delete(Expression<Func<TModel, bool>> filter)
{
DataMapper.Delete(filter);
using (var db = DataMapper())
{
db.Delete(filter);
}
}
public IEnumerable<TModel> All()
{
return Query.ToList();
return Query((q => q.ToList()));
}
public int Count()
{
return DataMapper.Query<TModel>().GetRowCount();
using (var db = DataMapper())
{
return db.Query<TModel>().GetRowCount();
}
}
public TModel Get(int id)
{
var model = Query.Where(c => c.Id == id).SingleOrDefault();
TModel model = Query(q => q.Where(c => c.Id == id).SingleOrDefault());
if (model == null)
{
@ -83,7 +100,7 @@ namespace NzbDrone.Core.Datastore
{
var idList = ids.ToList();
var query = string.Format("Id IN ({0})", string.Join(",", idList));
var result = Query.Where(m => m.Id.In(idList)).ToList();
var result = Query(q => q.Where(m => m.Id.In(idList)).ToList());
//var result = Query.Where(query).ToList();
if (result.Count != idList.Count())
@ -111,7 +128,10 @@ namespace NzbDrone.Core.Datastore
throw new InvalidOperationException("Can't insert model with existing ID " + model.Id);
}
DataMapper.Insert(model);
using (var db = DataMapper())
{
db.Insert(model);
}
ModelCreated(model);
@ -125,7 +145,10 @@ namespace NzbDrone.Core.Datastore
throw new InvalidOperationException("Can't update model with ID 0");
}
DataMapper.Update(model, c => c.Id == model.Id);
using (var db = DataMapper())
{
db.Update(model, c => c.Id == model.Id);
}
ModelUpdated(model);
@ -139,7 +162,7 @@ namespace NzbDrone.Core.Datastore
public void InsertMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -154,7 +177,7 @@ namespace NzbDrone.Core.Datastore
public void UpdateMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -192,12 +215,15 @@ namespace NzbDrone.Core.Datastore
public void Delete(int id)
{
DataMapper.Delete<TModel>(c => c.Id == id);
using (var db = DataMapper())
{
db.Delete<TModel>(c => c.Id == id);
}
}
public void DeleteMany(IEnumerable<int> ids)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -214,7 +240,10 @@ namespace NzbDrone.Core.Datastore
public void Purge(bool vacuum = false)
{
DataMapper.Delete<TModel>(c => c.Id > -1);
using (var db = DataMapper())
{
db.Delete<TModel>(c => c.Id > -1);
}
if (vacuum)
{
Vacuum();
@ -238,20 +267,23 @@ namespace NzbDrone.Core.Datastore
throw new InvalidOperationException("Attempted to updated model without ID");
}
DataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
using (var db = DataMapper())
{
db.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
}
ModelUpdated(model);
}
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
{
pagingSpec.Records = GetPagedQuery(Query, pagingSpec).Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize).ToList();
pagingSpec.TotalRecords = GetPagedQuery(Query, pagingSpec).GetRowCount();
pagingSpec.Records = Query(q => GetPagedQuery(q, pagingSpec).Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize).ToList());
pagingSpec.TotalRecords = Query(q => GetPagedQuery(q, pagingSpec).GetRowCount());
return pagingSpec;
}
@ -285,7 +317,7 @@ namespace NzbDrone.Core.Datastore
}
}
protected virtual QueryBuilder<TModel> AddJoinQueries(QueryBuilder<TModel> baseQuery)
protected virtual QueryBuilder<TActual> AddJoinQueries<TActual>(QueryBuilder<TActual> baseQuery)
{
return baseQuery;
}

@ -34,8 +34,11 @@ namespace NzbDrone.Core.Datastore
{
get
{
var version = _datamapperFactory().ExecuteScalar("SELECT sqlite_version()").ToString();
return new Version(version);
using (var db = _datamapperFactory())
{
var version = db.ExecuteScalar("SELECT sqlite_version()").ToString();
return new Version(version);
}
}
}
@ -44,7 +47,10 @@ namespace NzbDrone.Core.Datastore
try
{
_logger.Info("Vacuuming {0} database", _databaseName);
_datamapperFactory().ExecuteNonQuery("Vacuum;");
using (var db = _datamapperFactory())
{
db.ExecuteNonQuery("Vacuum;");
}
_logger.Info("{0} database compressed", _databaseName);
}
catch (Exception e)

@ -24,7 +24,7 @@ namespace NzbDrone.Core.Download.Pending
public List<PendingRelease> AllByMovieId(int movieId)
{
return Query.Where(p => p.MovieId == movieId);
return Query(q => q.Where(p => p.MovieId == movieId).ToList());
}
}
}

@ -34,17 +34,17 @@ namespace NzbDrone.Core.Extras.Files
public List<TExtraFile> GetFilesByMovie(int movieId)
{
return Query.Where(c => c.MovieId == movieId);
return Query(q => q.Where(c => c.MovieId == movieId).ToList());
}
public List<TExtraFile> GetFilesByMovieFile(int movieFileId)
{
return Query.Where(c => c.MovieFileId == movieFileId);
return Query(q => q.Where(c => c.MovieFileId == movieFileId).ToList());
}
public TExtraFile FindByPath(string path)
{
return Query.Where(c => c.RelativePath == path).SingleOrDefault();
return Query(q => q.Where(c => c.RelativePath == path).SingleOrDefault());
}
}
}

@ -30,37 +30,37 @@ namespace NzbDrone.Core.History
public List<QualityModel> GetBestQualityInHistory(int movieId)
{
var history = Query.Where(c => c.MovieId == movieId);
var history = Query(q => q.Where(c => c.MovieId == movieId).ToList());
return history.Select(h => h.Quality).ToList();
}
public History MostRecentForDownloadId(string downloadId)
{
return Query.Where(h => h.DownloadId == downloadId)
return Query(q => q.Where(h => h.DownloadId == downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
.FirstOrDefault());
}
public List<History> FindByDownloadId(string downloadId)
{
return Query.Where(h => h.DownloadId == downloadId);
return Query(q => q.Where(h => h.DownloadId == downloadId).ToList());
}
public List<History> FindDownloadHistory(int idMovieId, QualityModel quality)
{
return Query.Where(h =>
return Query(q => q.Where(h =>
h.MovieId == idMovieId &&
h.Quality == quality &&
(h.EventType == HistoryEventType.Grabbed ||
h.EventType == HistoryEventType.DownloadFailed ||
h.EventType == HistoryEventType.DownloadFolderImported)
).ToList();
).ToList());
}
public List<History> FindByMovieId(int movieId)
{
return Query.Where(h => h.MovieId == movieId);
return Query(q => q.Where(h => h.MovieId == movieId).ToList());
}
public void DeleteForMovie(int movieId)
@ -77,9 +77,9 @@ namespace NzbDrone.Core.History
public History MostRecentForMovie(int movieId)
{
return Query.Where(h => h.MovieId == movieId)
return Query(q => q.Where(h => h.MovieId == movieId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
.FirstOrDefault());
}
}
}

@ -13,9 +13,9 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE RelativePath
@ -25,6 +25,7 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
OR RelativePath
LIKE '/%'
)");
}
}
}
}

@ -13,12 +13,14 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM NamingConfig
mapper.ExecuteNonQuery(@"DELETE FROM NamingConfig
WHERE ID NOT IN (
SELECT ID FROM NamingConfig
LIMIT 1)");
}
}
}
}

@ -13,13 +13,15 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM Users
mapper.ExecuteNonQuery(@"DELETE FROM Users
WHERE ID NOT IN (
SELECT ID FROM Users
LIMIT 1)");
}
}
}
}
}

@ -19,28 +19,32 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteDuplicateMovieMetadata()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 1
GROUP BY MovieId, Consumer
HAVING COUNT(MovieId) > 1
)");
}
}
private void DeleteDuplicateMovieFileMetadata()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 1
GROUP BY MovieFileId, Consumer
HAVING COUNT(MovieFileId) > 1
)");
}
}
}
}

@ -13,14 +13,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM AlternativeTitles
mapper.ExecuteNonQuery(@"DELETE FROM AlternativeTitles
WHERE Id IN (
SELECT AlternativeTitles.Id FROM AlternativeTitles
LEFT OUTER JOIN Movies
ON AlternativeTitles.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

@ -13,14 +13,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
WHERE Id IN (
SELECT Blacklist.Id FROM Blacklist
LEFT OUTER JOIN Movies
ON Blacklist.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

@ -18,14 +18,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void CleanupOrphanedByMovie()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM History
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Movies
ON History.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

@ -13,14 +13,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM IndexerStatus
mapper.ExecuteNonQuery(@"DELETE FROM IndexerStatus
WHERE Id IN (
SELECT IndexerStatus.Id FROM IndexerStatus
LEFT OUTER JOIN Indexers
ON IndexerStatus.IndexerId = Indexers.Id
WHERE Indexers.Id IS NULL)");
}
}
}
}

@ -20,38 +20,44 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedByMovie()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN Movies
ON MetadataFiles.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
private void DeleteOrphanedByMovieFile()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN MovieFiles
ON MetadataFiles.MovieFileId = MovieFiles.Id
WHERE MetadataFiles.MovieFileId > 0
AND MovieFiles.Id IS NULL)");
}
}
private void DeleteWhereMovieFileIsZero()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type IN (1, 2)
AND MovieFileId = 0)");
}
}
}
}

@ -13,14 +13,15 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MovieFiles
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MovieFiles
WHERE Id IN (
SELECT MovieFiles.Id FROM MovieFiles
LEFT OUTER JOIN Movies
ON MovieFiles.Id = Movies.MovieFileId
WHERE Movies.Id IS NULL)");
}
}
}
}

@ -13,14 +13,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM PendingReleases
mapper.ExecuteNonQuery(@"DELETE FROM PendingReleases
WHERE Id IN (
SELECT PendingReleases.Id FROM PendingReleases
LEFT OUTER JOIN Movies
ON PendingReleases.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

@ -17,16 +17,18 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
var usedTags = new[] {"Movies", "Notifications", "DelayProfiles", "Restrictions"}
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray());
var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray());
mapper.ExecuteNonQuery($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})");
mapper.ExecuteNonQuery($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})");
}
}
private int[] GetUsedTags(string table, IDataMapper mapper)

@ -23,12 +23,14 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
_logger.Debug("Not running scheduled task last execution cleanup during debug");
}
var mapper = _database.GetDataMapper();
mapper.AddParameter("time", DateTime.UtcNow);
using (var mapper = _database.GetDataMapper())
{
mapper.AddParameter("time", DateTime.UtcNow);
mapper.ExecuteNonQuery(@"UPDATE ScheduledTasks
mapper.ExecuteNonQuery(@"UPDATE ScheduledTasks
SET LastExecution = @time
WHERE LastExecution > @time");
}
}
}
}

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Indexers
public IndexerStatus FindByIndexerId(int indexerId)
{
return Query.Where(c => c.IndexerId == indexerId).SingleOrDefault();
return Query(q => q.Where(c => c.IndexerId == indexerId).SingleOrDefault());
}
}
}

@ -22,7 +22,7 @@ namespace NzbDrone.Core.Jobs
public ScheduledTask GetDefinition(Type type)
{
return Query.Where(c => c.TypeName == type.FullName).Single();
return Query(q => q.Where(c => c.TypeName == type.FullName).Single());
}
public void SetLastExecutionTime(int id, DateTime executionTime)

@ -21,12 +21,12 @@ namespace NzbDrone.Core.MediaFiles
public List<MovieFile> GetFilesByMovie(int movieId)
{
return Query.Where(c => c.MovieId == movieId).ToList();
return Query(q => q.Where(c => c.MovieId == movieId).ToList());
}
public List<MovieFile> GetFilesWithoutMediaInfo()
{
return Query.Where(c => c.MediaInfo == null).ToList();
return Query(q => q.Where(c => c.MediaInfo == null).ToList());
}
}
}

@ -37,37 +37,39 @@ namespace NzbDrone.Core.Messaging.Commands
public void OrphanStarted()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.Parameters.Add(new SQLiteParameter("@orphaned", (int)CommandStatus.Orphaned));
mapper.Parameters.Add(new SQLiteParameter("@started", (int)CommandStatus.Started));
mapper.Parameters.Add(new SQLiteParameter("@ended", DateTime.UtcNow));
mapper.Parameters.Add(new SQLiteParameter("@orphaned", (int) CommandStatus.Orphaned));
mapper.Parameters.Add(new SQLiteParameter("@started", (int) CommandStatus.Started));
mapper.Parameters.Add(new SQLiteParameter("@ended", DateTime.UtcNow));
mapper.ExecuteNonQuery(@"UPDATE Commands
mapper.ExecuteNonQuery(@"UPDATE Commands
SET Status = @orphaned, EndedAt = @ended
WHERE Status = @started");
}
}
public List<CommandModel> FindCommands(string name)
{
return Query.Where(c => c.Name == name).ToList();
return Query(q => q.Where(c => c.Name == name).ToList());
}
public List<CommandModel> FindQueuedOrStarted(string name)
{
return Query.Where(c => c.Name == name)
return Query(q => q.Where(c => c.Name == name)
.AndWhere("[Status] IN (0,1)")
.ToList();
.ToList());
}
public List<CommandModel> Queued()
{
return Query.Where(c => c.Status == CommandStatus.Queued);
return Query(q => q.Where(c => c.Status == CommandStatus.Queued).ToList());
}
public List<CommandModel> Started()
{
return Query.Where(c => c.Status == CommandStatus.Started);
return Query(q => q.Where(c => c.Status == CommandStatus.Started).ToList());
}
public void Start(CommandModel command)

@ -25,17 +25,17 @@ namespace NzbDrone.Core.Movies.AlternativeTitles
public AlternativeTitle FindBySourceId(int sourceId)
{
return Query.Where(t => t.SourceId == sourceId).FirstOrDefault();
return Query(q => q.Where(t => t.SourceId == sourceId).FirstOrDefault());
}
public List<AlternativeTitle> FindBySourceIds(List<int> sourceIds)
{
return Query.Where(t => t.SourceId.In(sourceIds)).ToList();
return Query(q => q.Where(t => t.SourceId.In(sourceIds)).ToList());
}
public List<AlternativeTitle> FindByMovieId(int movieId)
{
return Query.Where(t => t.MovieId == movieId).ToList();
return Query(q => q.Where(t => t.MovieId == movieId).ToList());
}
}
}

@ -41,7 +41,7 @@ namespace NzbDrone.Core.Movies
public bool MoviePathExists(string path)
{
return Query.Where(c => c.Path == path).Any();
return Query(q => q.Where(c => c.Path == path).Any());
}
public Movie FindByTitle(string cleanTitle)
@ -57,12 +57,12 @@ namespace NzbDrone.Core.Movies
public Movie FindByImdbId(string imdbid)
{
var imdbIdWithPrefix = Parser.Parser.NormalizeImdbId(imdbid);
return Query.Where(s => s.ImdbId == imdbIdWithPrefix).SingleOrDefault();
return Query(q => q.Where(s => s.ImdbId == imdbIdWithPrefix).SingleOrDefault());
}
public List<Movie> GetMoviesByFileId(int fileId)
{
return Query.Where(m => m.MovieFileId == fileId).ToList();
return Query(q => q.Where(m => m.MovieFileId == fileId).ToList());
}
public void SetFileId(int fileId, int movieId)
@ -72,32 +72,37 @@ namespace NzbDrone.Core.Movies
public Movie FindByTitleSlug(string slug)
{
return Query.FirstOrDefault(m => m.TitleSlug == slug);
return Query(q => q.Where(m => m.TitleSlug == slug).FirstOrDefault());
}
public List<Movie> MoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored)
{
var query = Query.Where(m => (m.InCinemas >= start && m.InCinemas <= end) || (m.PhysicalRelease >= start && m.PhysicalRelease <= end));
if (!includeUnmonitored)
return Query(q =>
{
query.AndWhere(e => e.Monitored == true);
}
var query = q.Where(m =>
(m.InCinemas >= start && m.InCinemas <= end) ||
(m.PhysicalRelease >= start && m.PhysicalRelease <= end));
if (!includeUnmonitored)
{
query.AndWhere(e => e.Monitored == true);
}
return query.ToList();
return query.ToList();
});
}
public List<Movie> MoviesWithFiles(int movieId)
{
return Query.Join<Movie, MovieFile>(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id)
.Where(m => m.Id == movieId);
return Query(q => q.Join<Movie, MovieFile>(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id)
.Where(m => m.Id == movieId).ToList());
}
public PagingSpec<Movie> MoviesWithoutFiles(PagingSpec<Movie> pagingSpec)
{
pagingSpec.TotalRecords = GetMoviesWithoutFilesQuery(pagingSpec).GetRowCount();
pagingSpec.Records = GetMoviesWithoutFilesQuery(pagingSpec).ToList();
pagingSpec.TotalRecords = Query(q => GetMoviesWithoutFilesQuery(q, pagingSpec).GetRowCount());
pagingSpec.Records = Query(q => GetMoviesWithoutFilesQuery(q, pagingSpec).ToList());
return pagingSpec;
}
@ -157,7 +162,7 @@ namespace NzbDrone.Core.Movies
return newQuery;
}*/
public SortBuilder<Movie> GetMoviesWithoutFilesQuery(PagingSpec<Movie> pagingSpec)
public SortBuilder<Movie> GetMoviesWithoutFilesQuery(QueryBuilder<Movie> Query, PagingSpec<Movie> pagingSpec)
{
return Query.Where(pagingSpec.FilterExpression)
.AndWhere(m => m.MovieFileId == 0)
@ -168,13 +173,13 @@ namespace NzbDrone.Core.Movies
public PagingSpec<Movie> MoviesWhereCutoffUnmet(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
{
pagingSpec.TotalRecords = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).GetRowCount();
pagingSpec.Records = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).ToList();
pagingSpec.TotalRecords = Query(q => MoviesWhereCutoffUnmetQuery(q, pagingSpec, qualitiesBelowCutoff).GetRowCount());
pagingSpec.Records = Query(q => MoviesWhereCutoffUnmetQuery(q, pagingSpec, qualitiesBelowCutoff).ToList());
return pagingSpec;
}
private SortBuilder<Movie> MoviesWhereCutoffUnmetQuery(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
private SortBuilder<Movie> MoviesWhereCutoffUnmetQuery(QueryBuilder<Movie> Query, PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
{
return Query.Where(pagingSpec.FilterExpression)
.AndWhere(m => m.MovieFileId != 0)
@ -229,34 +234,41 @@ namespace NzbDrone.Core.Movies
cleanTitleWithArabicNumbers = cleanTitleWithArabicNumbers.Replace(romanNumber, arabicNumber);
}
Movie result = Query.Where(s => s.CleanTitle == cleanTitle).FirstWithYear(year);
if (result == null)
return Query(q =>
{
result = Query.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ??
Query.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
Movie result = q.Where(s => s.CleanTitle == cleanTitle).FirstWithYear(year);
if (result == null)
{
/*IEnumerable<Movie> movies = All();
Func<string, string> titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower());
Func<IEnumerable<AlternativeTitle>, string, bool> altTitleComparer =
(alternativeTitles, atitle) =>
alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/
/*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/
//result = Query.Join<Movie, AlternativeTitle>(JoinType.Inner, m => m._newAltTitles,
result =
q.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ??
q.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
if (result == null)
{
/*IEnumerable<Movie> movies = All();
Func<string, string> titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower());
Func<IEnumerable<AlternativeTitle>, string, bool> altTitleComparer =
(alternativeTitles, atitle) =>
alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/
/*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/
//result = Query.Join<Movie, AlternativeTitle>(JoinType.Inner, m => m._newAltTitles,
//(m, t) => m.Id == t.MovieId && (t.CleanTitle == cleanTitle)).FirstWithYear(year);
result = Query.Where<AlternativeTitle>(t =>
t.CleanTitle == cleanTitle || t.CleanTitle == cleanTitleWithArabicNumbers
|| t.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
result = q.Where<AlternativeTitle>(t =>
t.CleanTitle == cleanTitle || t.CleanTitle == cleanTitleWithArabicNumbers
|| t.CleanTitle == cleanTitleWithRomanNumbers)
.FirstWithYear(year);
}
}
}
return result;
return result;
});
/*return year.HasValue
? results?.FirstOrDefault(movie => movie.Year == year.Value)
@ -264,7 +276,7 @@ namespace NzbDrone.Core.Movies
: results?.FirstOrDefault();*/
}
protected override QueryBuilder<Movie> AddJoinQueries(QueryBuilder<Movie> baseQuery)
protected override QueryBuilder<TActual> AddJoinQueries<TActual>(QueryBuilder<TActual> baseQuery)
{
baseQuery = base.AddJoinQueries(baseQuery);
baseQuery = baseQuery.Join<Movie, AlternativeTitle>(JoinType.Left, m => m.AlternativeTitles,
@ -276,7 +288,7 @@ namespace NzbDrone.Core.Movies
public Movie FindByTmdbId(int tmdbid)
{
return Query.Where(m => m.TmdbId == tmdbid).FirstOrDefault();
return Query(q => q.Where(m => m.TmdbId == tmdbid).FirstOrDefault());
}
}
}

@ -8,7 +8,7 @@ namespace NzbDrone.Core.Movies
{
public static Movie FirstWithYear(this SortBuilder<Movie> query, int? year)
{
return year.HasValue ? query.FirstOrDefault(movie => movie.Year == year || movie.SecondaryYear == year) : query.FirstOrDefault();
return year.HasValue ? query.AndWhere(movie => movie.Year == year || movie.SecondaryYear == year).FirstOrDefault() : query.FirstOrDefault();
}
}

@ -30,12 +30,12 @@ namespace NzbDrone.Core.NetImport.ImportExclusions
public bool IsMovieExcluded(int tmdbid)
{
return Query.Where(ex => ex.TmdbId == tmdbid).Any();
return Query(q => q.Where(ex => ex.TmdbId == tmdbid).Any());
}
public ImportExclusion GetByTmdbid(int tmdbid)
{
return Query.Where(ex => ex.TmdbId == tmdbid).First();
return Query(q => q.Where(ex => ex.TmdbId == tmdbid).First());
}
}
}

@ -17,7 +17,10 @@ namespace NzbDrone.Core.Profiles
public bool Exists(int id)
{
return DataMapper.Query<Profile>().Where(p => p.Id == id).GetRowCount() == 1;
using (var mapper = DataMapper())
{
return mapper.Query<Profile>().Where(p => p.Id == id).GetRowCount() == 1;
}
}
}
}

@ -19,7 +19,7 @@ namespace NzbDrone.Core.Tags
public Tag GetByLabel(string label)
{
var model = Query.Where(c => c.Label == label).SingleOrDefault();
var model = Query(q => q.Where(c => c.Label == label).SingleOrDefault());
if (model == null)
{

Loading…
Cancel
Save