First steps for SQLite

pull/3113/head
markus101 12 years ago committed by kay.one
parent 29ec800996
commit ebbf5ea21f

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

@ -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<IDbFactory>().Create();
}).As<IDbConnection>().SingleInstance();
container.RegisterGeneric(typeof(BasicDb<>)).As(typeof(IBasicDb<>));
container.Register(c =>
{
@ -65,6 +71,7 @@ namespace NzbDrone.Core
}).As<IObjectDatabase>().SingleInstance();
container.RegisterGeneric(typeof(BasicRepository<>)).As(typeof(IBasicRepository<>));
}
}
}

@ -0,0 +1,126 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ServiceStack.OrmLite;
namespace NzbDrone.Core.Datastore
{
public interface IBasicDb<TModel> where TModel : ModelBase, new()
{
IEnumerable<TModel> 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<TModel> models);
void UpdateMany(IList<TModel> models);
void DeleteMany(List<TModel> models);
void Purge();
bool HasItems();
}
public class BasicDb<TModel> : IBasicDb<TModel> where TModel : ModelBase, new()
{
public BasicDb(IDbConnection database)
{
Database = database;
}
public IDbConnection Database { get; private set; }
public IEnumerable<TModel> All()
{
return Database.Select<TModel>();
}
public int Count()
{
return (int)Database.Count<TModel>();
}
public TModel Get(int id)
{
return Database.GetById<TModel>(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<TModel> models)
{
Database.InsertAll(models);
}
public void UpdateMany(IList<TModel> models)
{
Database.UpdateAll(models);
}
public void DeleteMany(List<TModel> 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<TModel>(id);
}
public void DeleteMany(IEnumerable<int> ids)
{
Database.DeleteByIds<TModel>(ids);
}
public void Purge()
{
Database.DeleteAll<TModel>();
}
public bool HasItems()
{
return Count() > 0;
}
}
}

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

@ -154,6 +154,21 @@
<Reference Include="RestSharp">
<HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Common">
<HintPath>..\packages\ServiceStack.Common.3.9.42\lib\net35\ServiceStack.Common.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Interfaces">
<HintPath>..\packages\ServiceStack.Common.3.9.42\lib\net35\ServiceStack.Interfaces.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.OrmLite">
<HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.42\lib\net40\ServiceStack.OrmLite.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.OrmLite.SqliteNET">
<HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.42\lib\net40\ServiceStack.OrmLite.SqliteNET.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.3.9.42\lib\net35\ServiceStack.Text.dll</HintPath>
</Reference>
<Reference Include="siaqodb">
<HintPath>..\Libraries\Siaqodb\siaqodb.dll</HintPath>
</Reference>
@ -165,6 +180,13 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite">
<HintPath>..\packages\System.Data.SQLite.1.0.84.0\lib\net40\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Data.SQLite.Linq">
<HintPath>..\packages\System.Data.SQLite.1.0.84.0\lib\net40\System.Data.SQLite.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web" />
@ -184,6 +206,7 @@
<Compile Include="Configuration\IConfigService.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ContainerExtensions.cs" />
<Compile Include="Datastore\DbFactory.cs" />
<Compile Include="Datastore\ModelBase.cs" />
<Compile Include="Datastore\BasicRepository.cs" />
<Compile Include="Datastore\ObjectDbFactory.cs" />
@ -559,6 +582,12 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="NzbDrone.ico" />
<Content Include="x64\SQLite.Interop.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="x86\SQLite.Interop.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

@ -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<Season>
public interface ISeasonRepository : IBasicDb<Season>
{
IList<int> GetSeasonNumbers(int seriesId);
Season Get(int seriesId, int seasonNumber);
@ -14,26 +14,28 @@ namespace NzbDrone.Core.Tv
List<Season> GetSeasonBySeries(int seriesId);
}
public class SeasonRepository : BasicRepository<Season>, ISeasonRepository
public class SeasonRepository : BasicDb<Season>, ISeasonRepository
{
public SeasonRepository(IObjectDatabase database)
: base(database)
private readonly IDbConnection _database;
public SeasonRepository(IDbConnection database)
: base(database)
{
}
public IList<int> GetSeasonNumbers(int seriesId)
{
return Queryable.Where(c => c.SeriesId == seriesId).Select(c => c.SeasonNumber).ToList();
return _database.List<int>("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<Season>(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<Season>(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).SingleOrDefault();
if(season == null) return false;
@ -42,8 +44,7 @@ namespace NzbDrone.Core.Tv
public List<Season> GetSeasonBySeries(int seriesId)
{
return Queryable.Where(c => c.SeriesId == seriesId).ToList();
return _database.Select<Season>(s => s.SeriesId == seriesId);
}
}
}

@ -36,7 +36,7 @@ namespace NzbDrone.Core.Tv
public DateTime? LastDiskSync { get; set; }
public int Runtime { get; set; }
public List<MediaCover.MediaCover> 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; }

@ -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<Series>
public interface ISeriesRepository : IBasicDb<Series>
{
bool SeriesPathExists(string path);
List<Series> Search(string title);
Series GetByTitle(string cleanTitle);
Series FindByTvdbId(int tvdbId);
void SetSeriesType(int seriesId, SeriesTypes seriesTypes);
}
public class SeriesRepository : BasicRepository<Series>, ISeriesRepository
public class SeriesRepository : BasicDb<Series>, 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<Series>("WHERE Path = {0}", path);
}
public List<Series> Search(string title)
{
return Queryable.Where(s => s.Title.Contains(title)).ToList();
return Database.Select<Series>(s => s.Title.Contains(title));
}
public Series GetByTitle(string cleanTitle)
{
return Queryable.SingleOrDefault(s => s.CleanTitle.Equals(cleanTitle));
return Database.Select<Series>(s => s.CleanTitle.Equals(cleanTitle)).SingleOrDefault();
}
public Series FindByTvdbId(int tvdbId)
{
return Queryable.SingleOrDefault(s => s.TvDbId == tvdbId);
return Database.Select<Series>(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);
}
}
}

@ -9,7 +9,11 @@
<package id="NLog" version="2.0.0.2000" />
<package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" />
<package id="RestSharp" version="104.1" targetFramework="net40" />
<package id="ServiceStack.Common" version="3.9.42" targetFramework="net40" />
<package id="ServiceStack.OrmLite.Sqlite32" version="3.9.42" targetFramework="net40" />
<package id="ServiceStack.Text" version="3.9.42" targetFramework="net40" />
<package id="SignalR.Hosting.Common" version="0.5.3" targetFramework="net40" />
<package id="SignalR.Server" version="0.5.3" targetFramework="net40" />
<package id="System.Data.SQLite" version="1.0.84.0" targetFramework="net40" />
<package id="twitterizer" version="2.4.0.26532" />
</packages>
Loading…
Cancel
Save