diff --git a/NzbDrone.Common/PathExtentions.cs b/NzbDrone.Common/PathExtentions.cs index 6886816fd..3ab725f16 100644 --- a/NzbDrone.Common/PathExtentions.cs +++ b/NzbDrone.Common/PathExtentions.cs @@ -15,6 +15,7 @@ namespace NzbDrone.Common public const string NZBDRONE_EXE = "NzbDrone.exe"; public const string OBJ_DB_FOLDER = "objDb"; + public const string NZBDRONE_DB = "nzbdrone.db"; private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; @@ -40,10 +41,6 @@ namespace NzbDrone.Common return info.FullName.Trim('/', '\\', ' '); } - - - - public static string GetWebRoot(this EnvironmentProvider environmentProvider) { return Path.Combine(environmentProvider.ApplicationPath, WEB_FOLDER); @@ -74,7 +71,6 @@ namespace NzbDrone.Common return Path.Combine(environmentProvider.GetAppDataPath(), OBJ_DB_FOLDER); } - public static string GetMediaCoverPath(this EnvironmentProvider environmentProvider) { return Path.Combine(environmentProvider.GetWebRoot(), "MediaCover"); diff --git a/NzbDrone.Core/ContainerExtensions.cs b/NzbDrone.Core/ContainerExtensions.cs index 006040aef..111677586 100644 --- a/NzbDrone.Core/ContainerExtensions.cs +++ b/NzbDrone.Core/ContainerExtensions.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Reflection; @@ -57,7 +58,12 @@ namespace NzbDrone.Core var appDataPath = new EnvironmentProvider().GetAppDataPath(); if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); + container.Register(c => + { + return c.Resolve().Create(); + }).As().SingleInstance(); + container.RegisterGeneric(typeof(BasicDb<>)).As(typeof(IBasicDb<>)); container.Register(c => { @@ -65,6 +71,7 @@ namespace NzbDrone.Core }).As().SingleInstance(); container.RegisterGeneric(typeof(BasicRepository<>)).As(typeof(IBasicRepository<>)); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/BasicDb.cs b/NzbDrone.Core/Datastore/BasicDb.cs new file mode 100644 index 000000000..90cd6a6e0 --- /dev/null +++ b/NzbDrone.Core/Datastore/BasicDb.cs @@ -0,0 +1,126 @@ +using System.Collections.Generic; +using System.Data; +using System.Linq; +using ServiceStack.OrmLite; + +namespace NzbDrone.Core.Datastore +{ + public interface IBasicDb where TModel : ModelBase, new() + { + IEnumerable All(); + int Count(); + TModel Get(int id); + //TModel Single(); + //TModel SingleOrDefault(); + TModel Insert(TModel model); + TModel Update(TModel model); + TModel Upsert(TModel model); + void Delete(int id); + void Delete(TModel model); + void InsertMany(IList models); + void UpdateMany(IList models); + void DeleteMany(List models); + void Purge(); + bool HasItems(); + } + + public class BasicDb : IBasicDb where TModel : ModelBase, new() + { + public BasicDb(IDbConnection database) + { + Database = database; + } + + public IDbConnection Database { get; private set; } + + public IEnumerable All() + { + return Database.Select(); + } + + public int Count() + { + return (int)Database.Count(); + } + + public TModel Get(int id) + { + return Database.GetById(id); + } + + //public TModel Single() + //{ + // return Queryable.Single(); + //} + + //public TModel SingleOrDefault() + //{ + // return Queryable.SingleOrDefault(); + //} + + public TModel Insert(TModel model) + { + Database.Insert(model); + model.Id = (int)Database.GetLastInsertId(); + return model; + } + + public TModel Update(TModel model) + { + Database.Update(model); + return model; + } + + public void Delete(TModel model) + { + Database.Delete(model); + } + + public void InsertMany(IList models) + { + Database.InsertAll(models); + } + + public void UpdateMany(IList models) + { + Database.UpdateAll(models); + } + + public void DeleteMany(List models) + { + Database.DeleteAll(models); + } + + public TModel Upsert(TModel model) + { + if (model.Id == 0) + { + Database.Insert(model); + model.Id = (int)Database.GetLastInsertId(); + return model; + } + Database.Update(model); + return model; + } + + public void Delete(int id) + { + Database.DeleteById(id); + } + + public void DeleteMany(IEnumerable ids) + { + Database.DeleteByIds(ids); + } + + public void Purge() + { + Database.DeleteAll(); + } + + public bool HasItems() + { + return Count() > 0; + } + } +} diff --git a/NzbDrone.Core/Datastore/DbFactory.cs b/NzbDrone.Core/Datastore/DbFactory.cs new file mode 100644 index 000000000..17fb3796f --- /dev/null +++ b/NzbDrone.Core/Datastore/DbFactory.cs @@ -0,0 +1,39 @@ +using System; +using System.Data; +using System.Linq; +using NzbDrone.Common; +using ServiceStack.OrmLite; + +namespace NzbDrone.Core.Datastore +{ + public interface IDbFactory + { + IDbConnection Create(string dbPath = null); + } + + public class DbFactory : IDbFactory + { + private readonly EnvironmentProvider _environmentProvider; + + public DbFactory(EnvironmentProvider environmentProvider) + { + _environmentProvider = environmentProvider; + } + + public IDbConnection Create(string dbPath = null) + { + if (string.IsNullOrWhiteSpace(dbPath)) + { + dbPath = _environmentProvider.GetObjectDbFolder(); + } + + var dbFactory = new OrmLiteConnectionFactory(GetConnectionString(dbPath)); + return dbFactory.OpenDbConnection(); + } + + private string GetConnectionString(string dbPath) + { + return String.Format("Data Source={0};Version=3;", dbPath); + } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 26b4ef508..c4856cb50 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -154,6 +154,21 @@ ..\packages\RestSharp.104.1\lib\net4\RestSharp.dll + + ..\packages\ServiceStack.Common.3.9.42\lib\net35\ServiceStack.Common.dll + + + ..\packages\ServiceStack.Common.3.9.42\lib\net35\ServiceStack.Interfaces.dll + + + ..\packages\ServiceStack.OrmLite.Sqlite32.3.9.42\lib\net40\ServiceStack.OrmLite.dll + + + ..\packages\ServiceStack.OrmLite.Sqlite32.3.9.42\lib\net40\ServiceStack.OrmLite.SqliteNET.dll + + + ..\packages\ServiceStack.Text.3.9.42\lib\net35\ServiceStack.Text.dll + ..\Libraries\Siaqodb\siaqodb.dll @@ -165,6 +180,13 @@ + + + ..\packages\System.Data.SQLite.1.0.84.0\lib\net40\System.Data.SQLite.dll + + + ..\packages\System.Data.SQLite.1.0.84.0\lib\net40\System.Data.SQLite.Linq.dll + @@ -184,6 +206,7 @@ + @@ -559,6 +582,12 @@ Always + + Always + + + Always + diff --git a/NzbDrone.Core/Tv/SeasonRepository.cs b/NzbDrone.Core/Tv/SeasonRepository.cs index 9323700b3..2d5eba1de 100644 --- a/NzbDrone.Core/Tv/SeasonRepository.cs +++ b/NzbDrone.Core/Tv/SeasonRepository.cs @@ -1,12 +1,12 @@ using System.Collections.Generic; +using System.Data; using System.Linq; -using NLog; using NzbDrone.Core.Datastore; - +using ServiceStack.OrmLite; namespace NzbDrone.Core.Tv { - public interface ISeasonRepository : IBasicRepository + public interface ISeasonRepository : IBasicDb { IList GetSeasonNumbers(int seriesId); Season Get(int seriesId, int seasonNumber); @@ -14,26 +14,28 @@ namespace NzbDrone.Core.Tv List GetSeasonBySeries(int seriesId); } - public class SeasonRepository : BasicRepository, ISeasonRepository + public class SeasonRepository : BasicDb, ISeasonRepository { - public SeasonRepository(IObjectDatabase database) - : base(database) + private readonly IDbConnection _database; + + public SeasonRepository(IDbConnection database) + : base(database) { } public IList GetSeasonNumbers(int seriesId) { - return Queryable.Where(c => c.SeriesId == seriesId).Select(c => c.SeasonNumber).ToList(); + return _database.List("SELECT SeasonNumber WHERE SeriesId = {0}", seriesId); } public Season Get(int seriesId, int seasonNumber) { - return Queryable.Single(c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber); + return _database.Select(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).Single(); } public bool IsIgnored(int seriesId, int seasonNumber) { - var season = Queryable.SingleOrDefault(c => c.Id == seriesId && c.SeasonNumber == seasonNumber); + var season = _database.Select(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).SingleOrDefault(); if(season == null) return false; @@ -42,8 +44,7 @@ namespace NzbDrone.Core.Tv public List GetSeasonBySeries(int seriesId) { - return Queryable.Where(c => c.SeriesId == seriesId).ToList(); + return _database.Select(s => s.SeriesId == seriesId); } - } } \ No newline at end of file diff --git a/NzbDrone.Core/Tv/Series.cs b/NzbDrone.Core/Tv/Series.cs index 7a4e6a0bc..625eda2ea 100644 --- a/NzbDrone.Core/Tv/Series.cs +++ b/NzbDrone.Core/Tv/Series.cs @@ -36,7 +36,7 @@ namespace NzbDrone.Core.Tv public DateTime? LastDiskSync { get; set; } public int Runtime { get; set; } public List Covers { get; set; } - public SeriesTypes SeriesTypes { get; set; } + public SeriesTypes SeriesType { get; set; } public BacklogSettingType BacklogSetting { get; set; } public string Network { get; set; } public DateTime? CustomStartDate { get; set; } diff --git a/NzbDrone.Core/Tv/SeriesRepository.cs b/NzbDrone.Core/Tv/SeriesRepository.cs index 75a9547eb..5ca1e4c17 100644 --- a/NzbDrone.Core/Tv/SeriesRepository.cs +++ b/NzbDrone.Core/Tv/SeriesRepository.cs @@ -1,50 +1,50 @@ using System.Collections.Generic; +using System.Data; using System.Linq; -using NzbDrone.Common; using NzbDrone.Core.Datastore; +using ServiceStack.OrmLite; namespace NzbDrone.Core.Tv { - public interface ISeriesRepository : IBasicRepository + public interface ISeriesRepository : IBasicDb { bool SeriesPathExists(string path); List Search(string title); Series GetByTitle(string cleanTitle); Series FindByTvdbId(int tvdbId); void SetSeriesType(int seriesId, SeriesTypes seriesTypes); - } - public class SeriesRepository : BasicRepository, ISeriesRepository + public class SeriesRepository : BasicDb, ISeriesRepository { - public SeriesRepository(IObjectDatabase objectDatabase) - : base(objectDatabase) + public SeriesRepository(IDbConnection database) + : base(database) { } public bool SeriesPathExists(string path) { - return Queryable.Any(s => DiskProvider.PathEquals(s.Path, path)); + return Database.Exists("WHERE Path = {0}", path); } public List Search(string title) { - return Queryable.Where(s => s.Title.Contains(title)).ToList(); + return Database.Select(s => s.Title.Contains(title)); } public Series GetByTitle(string cleanTitle) { - return Queryable.SingleOrDefault(s => s.CleanTitle.Equals(cleanTitle)); + return Database.Select(s => s.CleanTitle.Equals(cleanTitle)).SingleOrDefault(); } public Series FindByTvdbId(int tvdbId) { - return Queryable.SingleOrDefault(s => s.TvDbId == tvdbId); + return Database.Select(s => s.TvDbId.Equals(tvdbId)).SingleOrDefault(); } - public void SetSeriesType(int seriesId, SeriesTypes seriesTypes) + public void SetSeriesType(int seriesId, SeriesTypes seriesType) { - ObjectDatabase.UpdateField(new Series(){Id = seriesId, SeriesTypes =seriesTypes }, "SeriesType"); + Database.UpdateOnly(new Series { SeriesType = seriesType }, s => s.SeriesType, s => s.Id == seriesId); } } } \ No newline at end of file diff --git a/NzbDrone.Core/packages.config b/NzbDrone.Core/packages.config index f532359c6..9d367ca15 100644 --- a/NzbDrone.Core/packages.config +++ b/NzbDrone.Core/packages.config @@ -9,7 +9,11 @@ + + + + \ No newline at end of file diff --git a/NzbDrone.Core/x86/SQLite.Interop.dll b/NzbDrone.Core/x86/SQLite.Interop.dll new file mode 100644 index 000000000..1838fba8d Binary files /dev/null and b/NzbDrone.Core/x86/SQLite.Interop.dll differ