@ -25,11 +25,13 @@
// ************************************************************************/
# endregion
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using Dapper.Contrib.Extensions ;
using Mono.Data.Sqlite ;
using NLog ;
using PlexRequests.Helpers ;
@ -37,6 +39,8 @@ namespace PlexRequests.Store.Repository
{
public abstract class BaseGenericRepository < T > where T : class
{
private const string CorruptMessage =
"The database is corrupt, this could be due to the application exiting unexpectedly. See here to fix it: http://www.dosomethinghere.com/2013/02/20/fixing-the-sqlite-error-the-database-disk-image-is-malformed/" ;
protected BaseGenericRepository ( ISqliteConfiguration config , ICacheProvider cache )
{
Config = config ;
@ -51,6 +55,8 @@ namespace PlexRequests.Store.Repository
public abstract Task < T > GetAsync ( string id ) ;
public long Insert ( T entity )
{
try
{
ResetCache ( ) ;
using ( var cnn = Config . DbConnection ( ) )
@ -59,8 +65,16 @@ namespace PlexRequests.Store.Repository
return cnn . Insert ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public void Delete ( T entity )
{
try
{
ResetCache ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -69,8 +83,16 @@ namespace PlexRequests.Store.Repository
db . Delete ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public async Task DeleteAsync ( T entity )
{
try
{
ResetCache ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -79,8 +101,16 @@ namespace PlexRequests.Store.Repository
await db . DeleteAsync ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public bool Update ( T entity )
{
try
{
ResetCache ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -89,8 +119,16 @@ namespace PlexRequests.Store.Repository
return db . Update ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public async Task < bool > UpdateAsync ( T entity )
{
try
{
ResetCache ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -99,11 +137,18 @@ namespace PlexRequests.Store.Repository
return await db . UpdateAsync ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public bool UpdateAll ( IEnumerable < T > entity )
{
try
{
ResetCache ( ) ;
Log . Trace ( "Updating all entities" ) ;
var result = new HashSet < bool > ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -116,11 +161,18 @@ namespace PlexRequests.Store.Repository
}
return result . All ( x = > true ) ;
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public async Task < bool > UpdateAllAsync ( IEnumerable < T > entity )
{
try
{
ResetCache ( ) ;
Log . Trace ( "Updating all entities" ) ;
var result = new HashSet < bool > ( ) ;
using ( var db = Config . DbConnection ( ) )
@ -133,8 +185,16 @@ namespace PlexRequests.Store.Repository
}
return result . All ( x = > true ) ;
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public async Task < int > InsertAsync ( T entity )
{
try
{
ResetCache ( ) ;
using ( var cnn = Config . DbConnection ( ) )
@ -143,6 +203,12 @@ namespace PlexRequests.Store.Repository
return await cnn . InsertAsync ( entity ) ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
private void ResetCache ( )
{
@ -151,25 +217,38 @@ namespace PlexRequests.Store.Repository
}
public IEnumerable < T > GetAll ( )
{
try
{
using ( var db = Config . DbConnection ( ) )
{
db . Open ( ) ;
var result = db . GetAll < T > ( ) ;
return result ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
public async Task < IEnumerable < T > > GetAllAsync ( )
{
try
{
using ( var db = Config . DbConnection ( ) )
{
db . Open ( ) ;
var result = await db . GetAllAsync < T > ( ) ;
return result ;
}
}
catch ( SqliteException e ) when ( e . ErrorCode = = SQLiteErrorCode . Corrupt )
{
Log . Fatal ( CorruptMessage ) ;
throw ;
}
}
}
}