diff --git a/.gitignore b/.gitignore index a206d436a..8530d0a58 100644 --- a/.gitignore +++ b/.gitignore @@ -104,7 +104,7 @@ _TeamCity* # Radarr Backups/ logs/ -MediaCover/ +#MediaCover/ UpdateLogs/ xdg/ config.xml diff --git a/debian/rules b/debian/rules old mode 100755 new mode 100644 diff --git a/src/Marr.Data/DataMapper.cs b/src/Marr.Data/DataMapper.cs index 51423b988..c632471cf 100644 --- a/src/Marr.Data/DataMapper.cs +++ b/src/Marr.Data/DataMapper.cs @@ -941,21 +941,21 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - if (Command.Transaction != null) + if (_command != null) { - Command.Transaction.Dispose(); - Command.Transaction = null; - } + if (_command.Transaction != null) + { + _command.Transaction.Dispose(); + _command.Transaction = null; + } - if (Command.Connection != null) - { - Command.Connection.Dispose(); - Command.Connection = null; - } + if (_command.Connection != null) + { + _command.Connection.Dispose(); + _command.Connection = null; + } - if (Command != null) - { - Command.Dispose(); + _command.Dispose(); _command = null; } } diff --git a/src/Marr.Data/Mapping/MappingHelper.cs b/src/Marr.Data/Mapping/MappingHelper.cs index c1fbd42b4..80e2acb47 100644 --- a/src/Marr.Data/Mapping/MappingHelper.cs +++ b/src/Marr.Data/Mapping/MappingHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Data.Common; @@ -91,9 +91,11 @@ private void PrepareLazyLoadedProperties(object ent) Type entType = ent.GetType(); if (_repos.Relationships.ContainsKey(entType)) { + var provider = _db.ProviderFactory; + var connectionString = _db.ConnectionString; Func dbCreate = () => { - var db = new DataMapper(_db.ProviderFactory, _db.ConnectionString); + var db = new DataMapper(provider, connectionString); db.SqlMode = SqlModes.Text; return db; }; diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 8b888e1ce..aaae3f032 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -123,27 +123,6 @@ public bool CanUseGDIPlus() } } - public bool IsValidGDIPlusImage(string filename) - { - if (!CanUseGDIPlus()) - { - return true; - } - - try - { - using (var bmp = new Bitmap(filename)) - { - } - return true; - } - catch (Exception ex) - { - Logger.Debug(ex, "Corrupted image found at: {0}.", filename); - return false; - } - } - public bool FolderWritable(string path) { Ensure.That(path, () => path).IsValidPath(); diff --git a/src/NzbDrone.Common/Disk/IDiskProvider.cs b/src/NzbDrone.Common/Disk/IDiskProvider.cs index 33e8d0228..52b1fa4f0 100644 --- a/src/NzbDrone.Common/Disk/IDiskProvider.cs +++ b/src/NzbDrone.Common/Disk/IDiskProvider.cs @@ -20,7 +20,6 @@ public interface IDiskProvider bool FileExists(string path); bool FileExists(string path, StringComparison stringComparison); bool CanUseGDIPlus(); - bool IsValidGDIPlusImage(string path); bool FolderWritable(string path); string[] GetDirectories(string path); string[] GetFiles(string path, SearchOption searchOption); diff --git a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs index 43414836d..233e9a81f 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs @@ -66,95 +66,96 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) lock (CurlGlobalHandle.Instance) { - Stream responseStream = new MemoryStream(); - Stream headerStream = new MemoryStream(); - - using (var curlEasy = new CurlEasy()) + using (Stream responseStream = new MemoryStream()) + using (Stream headerStream = new MemoryStream()) { - curlEasy.AutoReferer = false; - curlEasy.WriteFunction = (b, s, n, o) => + using (var curlEasy = new CurlEasy()) { - responseStream.Write(b, 0, s * n); - return s * n; - }; - curlEasy.HeaderFunction = (b, s, n, o) => - { - headerStream.Write(b, 0, s * n); - return s * n; - }; - - AddProxy(curlEasy, request); - - curlEasy.Url = request.Url.FullUri; - - switch (request.Method) - { - case HttpMethod.GET: - curlEasy.HttpGet = true; - break; - - case HttpMethod.POST: - curlEasy.Post = true; - break; - - case HttpMethod.PUT: - curlEasy.Put = true; - break; - - default: - throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method)); - } - curlEasy.FollowLocation = false; - curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; ; - - if (request.RequestTimeout != TimeSpan.Zero) - { - curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds); - } - - if (OsInfo.IsWindows) - { - curlEasy.CaInfo = _caBundleFilePath; - } - - if (cookies != null) - { - curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url); - } - - if (request.ContentData != null) - { - curlEasy.PostFieldSize = request.ContentData.Length; - curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v))); - } - - // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state - using (var httpRequestHeaders = SerializeHeaders(request)) - { - curlEasy.HttpHeader = httpRequestHeaders; - - var result = curlEasy.Perform(); - - if (result != CurlCode.Ok) + curlEasy.AutoReferer = false; + curlEasy.WriteFunction = (b, s, n, o) => { - switch (result) + responseStream.Write(b, 0, s * n); + return s * n; + }; + curlEasy.HeaderFunction = (b, s, n, o) => + { + headerStream.Write(b, 0, s * n); + return s * n; + }; + + AddProxy(curlEasy, request); + + curlEasy.Url = request.Url.FullUri; + + switch (request.Method) + { + case HttpMethod.GET: + curlEasy.HttpGet = true; + break; + + case HttpMethod.POST: + curlEasy.Post = true; + break; + + case HttpMethod.PUT: + curlEasy.Put = true; + break; + + default: + throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method)); + } + curlEasy.FollowLocation = false; + curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; ; + + if (request.RequestTimeout != TimeSpan.Zero) + { + curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds); + } + + if (OsInfo.IsWindows) + { + curlEasy.CaInfo = _caBundleFilePath; + } + + if (cookies != null) + { + curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url); + } + + if (request.ContentData != null) + { + curlEasy.PostFieldSize = request.ContentData.Length; + curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v))); + } + + // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state + using (var httpRequestHeaders = SerializeHeaders(request)) + { + curlEasy.HttpHeader = httpRequestHeaders; + + var result = curlEasy.Perform(); + + if (result != CurlCode.Ok) { - case CurlCode.SslCaCert: - case (CurlCode)77: - throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url)); - default: - throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url)); - + switch (result) + { + case CurlCode.SslCaCert: + case (CurlCode)77: + throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url)); + default: + throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url)); + + } } } + + var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream); + var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection); + + var httpHeader = new HttpHeader(webHeaderCollection); + + return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode); } - - var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream); - var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection); - - var httpHeader = new HttpHeader(webHeaderCollection); - - return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode); } } } @@ -259,6 +260,7 @@ private string FixSetCookieHeader(string setCookie) private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream, WebHeaderCollection webHeaderCollection) { + byte[] bytes = null; responseStream.Position = 0; if (responseStream.Length != 0) @@ -268,21 +270,27 @@ private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream, { if (encoding.IndexOf("gzip") != -1) { - responseStream = new GZipStream(responseStream, CompressionMode.Decompress); + using (var zipStream = new GZipStream(responseStream, CompressionMode.Decompress)) + { + bytes = zipStream.ToBytes(); + } webHeaderCollection.Remove("Content-Encoding"); } else if (encoding.IndexOf("deflate") != -1) { - responseStream = new DeflateStream(responseStream, CompressionMode.Decompress); + using (var deflateStream = new DeflateStream(responseStream, CompressionMode.Decompress)) + { + bytes = deflateStream.ToBytes(); + } webHeaderCollection.Remove("Content-Encoding"); } } } - return responseStream.ToBytes(); - + if (bytes == null) bytes = responseStream.ToBytes(); + return bytes; } } diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 6d4e6b6b3..01b345f9a 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -20,72 +20,84 @@ public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider, I public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) { - var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); - - // Deflate is not a standard and could break depending on implementation. - // we should just stick with the more compatible Gzip - //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net - webRequest.AutomaticDecompression = DecompressionMethods.GZip; - - webRequest.Method = request.Method.ToString(); - webRequest.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; - webRequest.KeepAlive = request.ConnectionKeepAlive; - webRequest.AllowAutoRedirect = false; - webRequest.CookieContainer = cookies; - - if (request.RequestTimeout != TimeSpan.Zero) - { - webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds); - } - - AddProxy(webRequest, request); - - if (request.Headers != null) - { - AddRequestHeaders(webRequest, request.Headers); - } - - if (request.ContentData != null) - { - webRequest.ContentLength = request.ContentData.Length; - using (var writeStream = webRequest.GetRequestStream()) - { - writeStream.Write(request.ContentData, 0, request.ContentData.Length); - } - } - - HttpWebResponse httpWebResponse; - + HttpWebResponse httpWebResponse = null; + HttpWebRequest webRequest = null; try { - httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); + webRequest = (HttpWebRequest) WebRequest.Create((Uri) request.Url); + + // Deflate is not a standard and could break depending on implementation. + // we should just stick with the more compatible Gzip + //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net + webRequest.AutomaticDecompression = DecompressionMethods.GZip; + + webRequest.Method = request.Method.ToString(); + webRequest.UserAgent = request.UseSimplifiedUserAgent + ? UserAgentBuilder.UserAgentSimplified + : UserAgentBuilder.UserAgent; + webRequest.KeepAlive = request.ConnectionKeepAlive; + webRequest.AllowAutoRedirect = false; + webRequest.CookieContainer = cookies; + + if (request.RequestTimeout != TimeSpan.Zero) + { + webRequest.Timeout = (int) Math.Ceiling(request.RequestTimeout.TotalMilliseconds); + } + + AddProxy(webRequest, request); + + if (request.Headers != null) + { + AddRequestHeaders(webRequest, request.Headers); + } + + if (request.ContentData != null) + { + webRequest.ContentLength = request.ContentData.Length; + using (var writeStream = webRequest.GetRequestStream()) + { + writeStream.Write(request.ContentData, 0, request.ContentData.Length); + } + } + + try + { + httpWebResponse = (HttpWebResponse) webRequest.GetResponse(); + } + catch (WebException e) + { + if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows) + { + SecurityProtocolPolicy.DisableTls12(); + } + + httpWebResponse = (HttpWebResponse) e.Response; + + if (httpWebResponse == null) + { + throw; + } + } + + byte[] data = null; + + using (var responseStream = httpWebResponse.GetResponseStream()) + { + if (responseStream != null) + { + data = responseStream.ToBytes(); + } + } + + return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, + httpWebResponse.StatusCode); } - catch (WebException e) + finally { - if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows) - { - SecurityProtocolPolicy.DisableTls12(); - } - - httpWebResponse = (HttpWebResponse)e.Response; - - if (httpWebResponse == null) - { - throw; - } + webRequest = null; + (httpWebResponse as IDisposable)?.Dispose(); + httpWebResponse = null; } - - byte[] data = null; - - using (var responseStream = httpWebResponse.GetResponseStream()) - { - if (responseStream != null) - { - data = responseStream.ToBytes(); - } - } - - return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode); } protected virtual void AddProxy(HttpWebRequest webRequest, HttpRequest request) diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 2f985d552..1c234c427 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -226,11 +226,13 @@ public void DownloadFile(string url, string fileName) _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); - var webClient = new GZipWebClient(); - webClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgentBuilder.UserAgent); - webClient.DownloadFile(url, fileName); - stopWatch.Stop(); - _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); + using (var webClient = new GZipWebClient()) + { + webClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgentBuilder.UserAgent); + webClient.DownloadFile(url, fileName); + stopWatch.Stop(); + _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); + } } catch (WebException e) { diff --git a/src/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs b/src/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs index 93f66ca6d..2fee91f56 100644 --- a/src/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs +++ b/src/NzbDrone.Common/Instrumentation/GlobalExceptionHandlers.cs @@ -44,6 +44,8 @@ private static void HandleAppDomainException(object sender, UnhandledExceptionEv return; } } + + Console.WriteLine(exception.StackTrace); Console.WriteLine("EPIC FAIL: {0}", exception); Logger.Fatal(exception, "EPIC FAIL: " + exception.Message); diff --git a/src/NzbDrone.Core.Test/MediaCoverTests/CoverExistsSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaCoverTests/CoverExistsSpecificationFixture.cs index 9b6add7ba..26d427d61 100644 --- a/src/NzbDrone.Core.Test/MediaCoverTests/CoverExistsSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/MediaCoverTests/CoverExistsSpecificationFixture.cs @@ -37,15 +37,6 @@ private void GivenExistingFileSize(long bytes) } - private void GivenImageFileCorrupt(bool corrupt) - { - GivenFileExistsOnDisk(); - Mocker.GetMock() - .Setup(c => c.IsValidGDIPlusImage(It.IsAny())) - .Returns(!corrupt); - } - - [Test] public void should_return_false_if_file_not_exists() { @@ -61,21 +52,11 @@ public void should_return_false_if_file_exists_but_diffrent_size() Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse(); } - [Test] - public void should_return_false_if_file_exists_and_same_size_and_corrupt() - { - GivenExistingFileSize(100); - GivenImageFileCorrupt(true); - _httpResponse.Headers.ContentLength = 100; - Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse(); - } - [Test] public void should_return_true_if_file_exists_and_same_size_and_not_corrupt() { GivenExistingFileSize(100); - GivenImageFileCorrupt(false); _httpResponse.Headers.ContentLength = 100; Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeTrue(); } diff --git a/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs b/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs index bc559d548..99d0a210e 100644 --- a/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs @@ -8,6 +8,7 @@ using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.MediaCover; +using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Movies; using NzbDrone.Core.Movies.Events; @@ -28,6 +29,18 @@ public void Setup() .With(v => v.Id = 2) .With(v => v.Images = new List { new MediaCover.MediaCover(MediaCoverTypes.Poster, "") }) .Build(); + + Mocker.GetMock().Setup(m => m.GetMovie(It.Is(id => id == _movie.Id))).Returns(_movie); + } + + private void ExecuteAndVerifyCommand(Movie movie) + { + Subject.HandleAsync(new MovieUpdatedEvent(movie)); + + Mocker.GetMock() + .Verify(v => v.Push(It.Is(c => c.MovieId == movie.Id), It.IsAny(), It.IsAny()), Times.Once()); + + Subject.Execute(new EnsureMediaCoversCommand(movie.Id)); } [Test] @@ -76,7 +89,7 @@ public void should_resize_covers_if_main_downloaded() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); - Subject.HandleAsync(new MovieUpdatedEvent(_movie)); + ExecuteAndVerifyCommand(_movie); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); @@ -93,7 +106,7 @@ public void should_resize_covers_if_missing() .Setup(v => v.FileExists(It.IsAny())) .Returns(false); - Subject.HandleAsync(new MovieUpdatedEvent(_movie)); + ExecuteAndVerifyCommand(_movie); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); @@ -114,7 +127,7 @@ public void should_not_resize_covers_if_exists() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(1000); - Subject.HandleAsync(new MovieUpdatedEvent(_movie)); + ExecuteAndVerifyCommand(_movie); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); @@ -135,7 +148,7 @@ public void should_resize_covers_if_existing_is_empty() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(0); - Subject.HandleAsync(new MovieUpdatedEvent(_movie)); + ExecuteAndVerifyCommand(_movie); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); @@ -156,7 +169,7 @@ public void should_log_error_if_resize_failed() .Setup(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(); - Subject.HandleAsync(new MovieUpdatedEvent(_movie)); + ExecuteAndVerifyCommand(_movie); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); diff --git a/src/NzbDrone.Core/Authentication/UserRepository.cs b/src/NzbDrone.Core/Authentication/UserRepository.cs index 0c6539dd7..bb3ffa7c9 100644 --- a/src/NzbDrone.Core/Authentication/UserRepository.cs +++ b/src/NzbDrone.Core/Authentication/UserRepository.cs @@ -20,12 +20,12 @@ public UserRepository(IMainDatabase database, IEventAggregator eventAggregator) 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()); } } } diff --git a/src/NzbDrone.Core/Backup/MakeDatabaseBackup.cs b/src/NzbDrone.Core/Backup/MakeDatabaseBackup.cs index bafcd8232..2b82e4bc3 100644 --- a/src/NzbDrone.Core/Backup/MakeDatabaseBackup.cs +++ b/src/NzbDrone.Core/Backup/MakeDatabaseBackup.cs @@ -26,7 +26,11 @@ public MakeDatabaseBackup(Logger logger) 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)); diff --git a/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs b/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs index 168e495c5..a3678910c 100644 --- a/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs +++ b/src/NzbDrone.Core/Blacklisting/BlacklistRepository.cs @@ -22,19 +22,19 @@ public BlacklistRepository(IMainDatabase database, IEventAggregator eventAggrega public List 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 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 BlacklistedByMovie(int movieId) { - return Query.Where(b => b.MovieId == movieId); + return Query(q => q.Where(b => b.MovieId == movieId).ToList()); } protected override SortBuilder GetPagedQuery(QueryBuilder query, PagingSpec pagingSpec) diff --git a/src/NzbDrone.Core/Configuration/ConfigRepository.cs b/src/NzbDrone.Core/Configuration/ConfigRepository.cs index c9092c7e5..375980f9b 100644 --- a/src/NzbDrone.Core/Configuration/ConfigRepository.cs +++ b/src/NzbDrone.Core/Configuration/ConfigRepository.cs @@ -21,7 +21,7 @@ public ConfigRepository(IMainDatabase database, IEventAggregator eventAggregator 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) diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index 2a073556a..dd102a6db 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -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 @@ public BasicRepository(IDatabase database, IEventAggregator eventAggregator) _eventAggregator = eventAggregator; } - protected QueryBuilder Query => AddJoinQueries(DataMapper.Query()); + + protected T Query(Func, T> finalizeQuery) + { + using (var mapper = DataMapper()) + { + var query = AddJoinQueries(mapper.Query()); + return finalizeQuery(query); + } + } protected void Delete(Expression> filter) { - DataMapper.Delete(filter); + using (var db = DataMapper()) + { + db.Delete(filter); + } } public IEnumerable All() { - return Query.ToList(); + return Query((q => q.ToList())); } public int Count() { - return DataMapper.Query().GetRowCount(); + using (var db = DataMapper()) + { + return db.Query().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 @@ public IEnumerable Get(IEnumerable ids) { 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 @@ public TModel Insert(TModel model) 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 @@ public TModel Update(TModel model) 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 @@ public void Delete(TModel model) public void InsertMany(IList models) { - using (var unitOfWork = new UnitOfWork(() => DataMapper)) + using (var unitOfWork = new UnitOfWork(() => DataMapper())) { unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); @@ -154,7 +177,7 @@ public void InsertMany(IList models) public void UpdateMany(IList models) { - using (var unitOfWork = new UnitOfWork(() => DataMapper)) + using (var unitOfWork = new UnitOfWork(() => DataMapper())) { unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); @@ -192,12 +215,15 @@ public TModel Upsert(TModel model) public void Delete(int id) { - DataMapper.Delete(c => c.Id == id); + using (var db = DataMapper()) + { + db.Delete(c => c.Id == id); + } } public void DeleteMany(IEnumerable ids) { - using (var unitOfWork = new UnitOfWork(() => DataMapper)) + using (var unitOfWork = new UnitOfWork(() => DataMapper())) { unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); @@ -214,7 +240,10 @@ public void DeleteMany(IEnumerable ids) public void Purge(bool vacuum = false) { - DataMapper.Delete(c => c.Id > -1); + using (var db = DataMapper()) + { + db.Delete(c => c.Id > -1); + } if (vacuum) { Vacuum(); @@ -238,20 +267,23 @@ public void SetFields(TModel model, params Expression>[] pr throw new InvalidOperationException("Attempted to updated model without ID"); } - DataMapper.Update() - .Where(c => c.Id == model.Id) - .ColumnsIncluding(properties) - .Entity(model) - .Execute(); + using (var db = DataMapper()) + { + db.Update() + .Where(c => c.Id == model.Id) + .ColumnsIncluding(properties) + .Entity(model) + .Execute(); + } ModelUpdated(model); } public virtual PagingSpec GetPaged(PagingSpec 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 @@ private void PublishModelEvent(TModel model, ModelAction action) } } - protected virtual QueryBuilder AddJoinQueries(QueryBuilder baseQuery) + protected virtual QueryBuilder AddJoinQueries(QueryBuilder baseQuery) { return baseQuery; } diff --git a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs index 522b72e94..366b084a3 100644 --- a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs +++ b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs @@ -35,7 +35,7 @@ private static string GetConnectionString(string dbPath) var connectionBuilder = new SQLiteConnectionStringBuilder(); connectionBuilder.DataSource = dbPath; - connectionBuilder.CacheSize = (int)-10.Megabytes(); + connectionBuilder.CacheSize = (int)-20000; connectionBuilder.DateTimeKind = DateTimeKind.Utc; connectionBuilder.JournalMode = OsInfo.IsOsx ? SQLiteJournalModeEnum.Truncate : SQLiteJournalModeEnum.Wal; connectionBuilder.Pooling = true; diff --git a/src/NzbDrone.Core/Datastore/Database.cs b/src/NzbDrone.Core/Datastore/Database.cs index 991cd9b0e..2a8417ed6 100644 --- a/src/NzbDrone.Core/Datastore/Database.cs +++ b/src/NzbDrone.Core/Datastore/Database.cs @@ -34,8 +34,11 @@ public Version Version { 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 @@ public void Vacuum() 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) diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseRepository.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseRepository.cs index ddcf41769..8d9d21d58 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseRepository.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseRepository.cs @@ -24,7 +24,7 @@ public void DeleteByMovieId(int movieId) public List AllByMovieId(int movieId) { - return Query.Where(p => p.MovieId == movieId); + return Query(q => q.Where(p => p.MovieId == movieId).ToList()); } } } diff --git a/src/NzbDrone.Core/Extras/Files/ExtraFileRepository.cs b/src/NzbDrone.Core/Extras/Files/ExtraFileRepository.cs index 57a66e016..c3370c1ef 100644 --- a/src/NzbDrone.Core/Extras/Files/ExtraFileRepository.cs +++ b/src/NzbDrone.Core/Extras/Files/ExtraFileRepository.cs @@ -34,17 +34,17 @@ public void DeleteForMovieFile(int movieFileId) public List GetFilesByMovie(int movieId) { - return Query.Where(c => c.MovieId == movieId); + return Query(q => q.Where(c => c.MovieId == movieId).ToList()); } public List 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()); } } } diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index 1ff9653e1..819f3c9af 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -30,37 +30,37 @@ public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregato public List 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 FindByDownloadId(string downloadId) { - return Query.Where(h => h.DownloadId == downloadId); + return Query(q => q.Where(h => h.DownloadId == downloadId).ToList()); } public List 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 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 @@ protected override SortBuilder GetPagedQuery(QueryBuilder quer 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()); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs index 099180b3b..de6dcc0de 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs @@ -13,9 +13,9 @@ public CleanupAbsolutePathMetadataFiles(IMainDatabase database) 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 @@ OR RelativePath OR RelativePath LIKE '/%' )"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs index a86b209e0..e63fd2dfa 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs @@ -13,12 +13,14 @@ public CleanupAdditionalNamingSpecs(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs index b533d5960..9103dc4c1 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs @@ -13,13 +13,15 @@ public CleanupAdditionalUsers(IMainDatabase database) 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)"); - } + } + } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs index 3373fb4b7..5a405de77 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs @@ -19,28 +19,32 @@ public void Clean() 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 )"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlternativeTitles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlternativeTitles.cs index 1f1ad2020..6ef15ac2f 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlternativeTitles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlternativeTitles.cs @@ -13,14 +13,16 @@ public CleanupOrphanedAlternativeTitles(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlacklist.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlacklist.cs index 280765819..7816c5ab9 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlacklist.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlacklist.cs @@ -13,14 +13,16 @@ public CleanupOrphanedBlacklist(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs index 617bb2abc..d69112d97 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs @@ -18,14 +18,16 @@ public void Clean() 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs index b3cf47027..ac021bb3c 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs @@ -13,14 +13,16 @@ public CleanupOrphanedIndexerStatus(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs index ae6b48495..1c89b1c46 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs @@ -20,38 +20,44 @@ public void Clean() 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMovieFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMovieFiles.cs index d62c5988d..d36d1da66 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMovieFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMovieFiles.cs @@ -13,14 +13,15 @@ public CleanupOrphanedMovieFiles(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs index e993c9686..bed514b93 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs @@ -13,14 +13,16 @@ public CleanupOrphanedPendingReleases(IMainDatabase database) 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)"); + } } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs index a9a0bea0e..9b07f8319 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs @@ -17,16 +17,18 @@ public CleanupUnusedTags(IMainDatabase database) 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) diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs index 1d5cdef35..c4ed9f501 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs @@ -23,12 +23,14 @@ public void Clean() _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"); + } } } } diff --git a/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs b/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs index 8a70b790a..3a403a821 100644 --- a/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs +++ b/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs @@ -20,7 +20,7 @@ public IndexerStatusRepository(IMainDatabase database, IEventAggregator eventAgg public IndexerStatus FindByIndexerId(int indexerId) { - return Query.Where(c => c.IndexerId == indexerId).SingleOrDefault(); + return Query(q => q.Where(c => c.IndexerId == indexerId).SingleOrDefault()); } } } diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index f69de8224..96013e845 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -15,13 +15,14 @@ public class DatabaseTarget : TargetWithLayout, IHandle c.TypeName == type.FullName).Single(); + return Query(q => q.Where(c => c.TypeName == type.FullName).Single()); } public void SetLastExecutionTime(int id, DateTime executionTime) diff --git a/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs b/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs index bb5b148a9..49e4f60de 100644 --- a/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs +++ b/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs @@ -31,12 +31,6 @@ public bool AlreadyExists(string url, string path) return false; } - if (!_diskProvider.IsValidGDIPlusImage(path)) - { - _diskProvider.DeleteFile(path); - return false; - } - var headers = _httpClient.Head(new HttpRequest(url)).Headers; var fileSize = _diskProvider.GetFileSize(path); return fileSize == headers.ContentLength; diff --git a/src/NzbDrone.Core/MediaCover/EnsureMediaCoversCommand.cs b/src/NzbDrone.Core/MediaCover/EnsureMediaCoversCommand.cs new file mode 100644 index 000000000..0cb4b02dd --- /dev/null +++ b/src/NzbDrone.Core/MediaCover/EnsureMediaCoversCommand.cs @@ -0,0 +1,19 @@ +using NzbDrone.Core.Messaging.Commands; + +namespace NzbDrone.Core.MediaCover +{ + public class EnsureMediaCoversCommand : Command + { + public int MovieId { get; set; } + + public EnsureMediaCoversCommand() + { + + } + + public EnsureMediaCoversCommand(int movieId) + { + MovieId = movieId; + } + } +} diff --git a/src/NzbDrone.Core/MediaCover/MediaCoverService.cs b/src/NzbDrone.Core/MediaCover/MediaCoverService.cs index b4bc2aed2..a809e6cc6 100644 --- a/src/NzbDrone.Core/MediaCover/MediaCoverService.cs +++ b/src/NzbDrone.Core/MediaCover/MediaCoverService.cs @@ -8,6 +8,7 @@ using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Movies; using NzbDrone.Core.Movies.Events; @@ -24,6 +25,7 @@ public class MediaCoverService : IHandleAsync, IHandleAsync, IHandleAsync, + IExecute, IMapCoversToLocal { private readonly IImageResizer _resizer; @@ -32,6 +34,8 @@ public class MediaCoverService : private readonly ICoverExistsSpecification _coverExistsSpecification; private readonly IConfigFileProvider _configFileProvider; private readonly IEventAggregator _eventAggregator; + private readonly IManageCommandQueue _commandQueue; + private readonly IMovieService _movieService; private readonly Logger _logger; private readonly string _coverRootFolder; @@ -43,6 +47,8 @@ public MediaCoverService(IImageResizer resizer, ICoverExistsSpecification coverExistsSpecification, IConfigFileProvider configFileProvider, IEventAggregator eventAggregator, + IManageCommandQueue commandQueue, + IMovieService movieService, Logger logger) { _resizer = resizer; @@ -51,6 +57,8 @@ public MediaCoverService(IImageResizer resizer, _coverExistsSpecification = coverExistsSpecification; _configFileProvider = configFileProvider; _eventAggregator = eventAggregator; + _commandQueue = commandQueue; + _movieService = movieService; _logger = logger; _coverRootFolder = appFolderInfo.GetMediaCoverPath(); @@ -109,7 +117,7 @@ private void EnsureCovers(Movie movie, int retried = 0) _logger.Warn(e, string.Format("Couldn't download media cover for {0}. {1}", movie, e.Message)); if (retried < 3) { - retried = +1; + retried += 1; _logger.Warn("Retrying for the {0}. time in ten seconds.", retried); System.Threading.Thread.Sleep(10 * 1000); EnsureCovers(movie, retried); @@ -182,16 +190,26 @@ private void EnsureResizedCovers(Movie movie, MediaCover cover, bool forceResize } } + public void Execute(EnsureMediaCoversCommand command) + { + var movie = _movieService.GetMovie(command.MovieId); + EnsureCovers(movie); + _eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(movie)); + } + public void HandleAsync(MovieUpdatedEvent message) { - EnsureCovers(message.Movie); - _eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie)); + //EnsureCovers(message.Movie); + _logger.Info("Testing: {0}, {1}", _commandQueue, message.Movie.Id); + _commandQueue.Push(new EnsureMediaCoversCommand(message.Movie.Id)); + //_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie)); } public void HandleAsync(MovieAddedEvent message) { - EnsureCovers(message.Movie); - _eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie)); + //EnsureCovers(message.Movie); + _commandQueue.Push(new EnsureMediaCoversCommand(message.Movie.Id)); + //_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie)); } public void HandleAsync(MovieDeletedEvent message) diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs index 6ef40f409..3e4344bf7 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs @@ -21,12 +21,12 @@ public MediaFileRepository(IMainDatabase database, IEventAggregator eventAggrega public List GetFilesByMovie(int movieId) { - return Query.Where(c => c.MovieId == movieId).ToList(); + return Query(q => q.Where(c => c.MovieId == movieId).ToList()); } public List GetFilesWithoutMediaInfo() { - return Query.Where(c => c.MediaInfo == null).ToList(); + return Query(q => q.Where(c => c.MediaInfo == null).ToList()); } } } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandRepository.cs b/src/NzbDrone.Core/Messaging/Commands/CommandRepository.cs index b873d28aa..da21c788a 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandRepository.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandRepository.cs @@ -37,37 +37,39 @@ public void Trim() 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 FindCommands(string name) { - return Query.Where(c => c.Name == name).ToList(); + return Query(q => q.Where(c => c.Name == name).ToList()); } public List 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 Queued() { - return Query.Where(c => c.Status == CommandStatus.Queued); + return Query(q => q.Where(c => c.Status == CommandStatus.Queued).ToList()); } public List 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) diff --git a/src/NzbDrone.Core/Movies/AlternativeTitles/AlternativeTitleRepository.cs b/src/NzbDrone.Core/Movies/AlternativeTitles/AlternativeTitleRepository.cs index 1759adced..63c8b53b1 100644 --- a/src/NzbDrone.Core/Movies/AlternativeTitles/AlternativeTitleRepository.cs +++ b/src/NzbDrone.Core/Movies/AlternativeTitles/AlternativeTitleRepository.cs @@ -25,17 +25,17 @@ public AlternativeTitleRepository(IMainDatabase database, IEventAggregator event 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 FindBySourceIds(List sourceIds) { - return Query.Where(t => t.SourceId.In(sourceIds)).ToList(); + return Query(q => q.Where(t => t.SourceId.In(sourceIds)).ToList()); } public List FindByMovieId(int movieId) { - return Query.Where(t => t.MovieId == movieId).ToList(); + return Query(q => q.Where(t => t.MovieId == movieId).ToList()); } } } diff --git a/src/NzbDrone.Core/Movies/MovieRepository.cs b/src/NzbDrone.Core/Movies/MovieRepository.cs index 21d0d9cc3..c9b6a0acd 100644 --- a/src/NzbDrone.Core/Movies/MovieRepository.cs +++ b/src/NzbDrone.Core/Movies/MovieRepository.cs @@ -41,7 +41,7 @@ public MovieRepository(IMainDatabase database, IEventAggregator eventAggregator) 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 @@ public Movie FindByTitle(string cleanTitle, int year) 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 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 @@ public void SetFileId(int fileId, int movieId) public Movie FindByTitleSlug(string slug) { - return Query.FirstOrDefault(m => m.TitleSlug == slug); + return Query(q => q.Where(m => m.TitleSlug == slug).FirstOrDefault()); } public List 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 MoviesWithFiles(int movieId) { - return Query.Join(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id) - .Where(m => m.Id == movieId); + return Query(q => q.Join(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id) + .Where(m => m.Id == movieId).ToList()); } public PagingSpec MoviesWithoutFiles(PagingSpec 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 @@ public PagingSpec MoviesWithoutFiles(PagingSpec pagingSpec) return newQuery; }*/ - public SortBuilder GetMoviesWithoutFilesQuery(PagingSpec pagingSpec) + public SortBuilder GetMoviesWithoutFilesQuery(QueryBuilder Query, PagingSpec pagingSpec) { return Query.Where(pagingSpec.FilterExpression) .AndWhere(m => m.MovieFileId == 0) @@ -168,13 +173,13 @@ public SortBuilder GetMoviesWithoutFilesQuery(PagingSpec pagingSpe public PagingSpec MoviesWhereCutoffUnmet(PagingSpec pagingSpec, List 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 MoviesWhereCutoffUnmetQuery(PagingSpec pagingSpec, List qualitiesBelowCutoff) + private SortBuilder MoviesWhereCutoffUnmetQuery(QueryBuilder Query, PagingSpec pagingSpec, List qualitiesBelowCutoff) { return Query.Where(pagingSpec.FilterExpression) .AndWhere(m => m.MovieFileId != 0) @@ -229,34 +234,41 @@ private Movie FindByTitle(string cleanTitle, int? year) 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 movies = All(); - Func titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower()); - Func, string, bool> altTitleComparer = - (alternativeTitles, atitle) => - alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/ + result = + q.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ?? + q.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year); - /*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) || - altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) || - altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/ + if (result == null) + { + /*IEnumerable movies = All(); + Func titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower()); + Func, string, bool> altTitleComparer = + (alternativeTitles, atitle) => + alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/ - //result = Query.Join(JoinType.Inner, m => m._newAltTitles, + /*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) || + altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) || + altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/ + + //result = Query.Join(JoinType.Inner, m => m._newAltTitles, //(m, t) => m.Id == t.MovieId && (t.CleanTitle == cleanTitle)).FirstWithYear(year); - result = Query.Where(t => - t.CleanTitle == cleanTitle || t.CleanTitle == cleanTitleWithArabicNumbers - || t.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year); + result = q.Where(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 @@ private Movie FindByTitle(string cleanTitle, int? year) : results?.FirstOrDefault();*/ } - protected override QueryBuilder AddJoinQueries(QueryBuilder baseQuery) + protected override QueryBuilder AddJoinQueries(QueryBuilder baseQuery) { baseQuery = base.AddJoinQueries(baseQuery); baseQuery = baseQuery.Join(JoinType.Left, m => m.AlternativeTitles, @@ -276,7 +288,7 @@ protected override QueryBuilder AddJoinQueries(QueryBuilder baseQu public Movie FindByTmdbId(int tmdbid) { - return Query.Where(m => m.TmdbId == tmdbid).FirstOrDefault(); + return Query(q => q.Where(m => m.TmdbId == tmdbid).FirstOrDefault()); } } } diff --git a/src/NzbDrone.Core/Movies/QueryExtensions.cs b/src/NzbDrone.Core/Movies/QueryExtensions.cs index b0c5d3604..17847d50a 100644 --- a/src/NzbDrone.Core/Movies/QueryExtensions.cs +++ b/src/NzbDrone.Core/Movies/QueryExtensions.cs @@ -8,7 +8,7 @@ public static class QueryExtensions { public static Movie FirstWithYear(this SortBuilder 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(); } } diff --git a/src/NzbDrone.Core/NetImport/ImportExclusions/ImportExclusionsRepository.cs b/src/NzbDrone.Core/NetImport/ImportExclusions/ImportExclusionsRepository.cs index 7846cb0f3..ca9db1888 100644 --- a/src/NzbDrone.Core/NetImport/ImportExclusions/ImportExclusionsRepository.cs +++ b/src/NzbDrone.Core/NetImport/ImportExclusions/ImportExclusionsRepository.cs @@ -30,12 +30,12 @@ public ImportExclusionsRepository(IMainDatabase database, IEventAggregator event 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()); } } } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index b691b348d..428840ac3 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -153,6 +153,7 @@ + diff --git a/src/NzbDrone.Core/Profiles/ProfileRepository.cs b/src/NzbDrone.Core/Profiles/ProfileRepository.cs index 4e071a0cf..3889f203a 100644 --- a/src/NzbDrone.Core/Profiles/ProfileRepository.cs +++ b/src/NzbDrone.Core/Profiles/ProfileRepository.cs @@ -17,7 +17,10 @@ public ProfileRepository(IMainDatabase database, IEventAggregator eventAggregato public bool Exists(int id) { - return DataMapper.Query().Where(p => p.Id == id).GetRowCount() == 1; + using (var mapper = DataMapper()) + { + return mapper.Query().Where(p => p.Id == id).GetRowCount() == 1; + } } } } diff --git a/src/NzbDrone.Core/Tags/TagRepository.cs b/src/NzbDrone.Core/Tags/TagRepository.cs index 500502843..c7a471e9e 100644 --- a/src/NzbDrone.Core/Tags/TagRepository.cs +++ b/src/NzbDrone.Core/Tags/TagRepository.cs @@ -19,7 +19,7 @@ public TagRepository(IMainDatabase database, IEventAggregator eventAggregator) 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) {