Wrapped the repo to catch Sqlite corrupt messages.

This commit is contained in:
tidusjar 2016-07-15 15:36:35 +01:00
parent 33ba1db20b
commit c5ad97780f

View file

@ -25,18 +25,22 @@
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
using Mono.Data.Sqlite;
using NLog; using NLog;
using PlexRequests.Helpers; using PlexRequests.Helpers;
namespace PlexRequests.Store.Repository namespace PlexRequests.Store.Repository
{ {
public abstract class BaseGenericRepository<T> where T : class 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) protected BaseGenericRepository(ISqliteConfiguration config, ICacheProvider cache)
{ {
Config = config; Config = config;
@ -52,95 +56,157 @@ namespace PlexRequests.Store.Repository
public long Insert(T entity) public long Insert(T entity)
{ {
ResetCache(); try
using (var cnn = Config.DbConnection())
{ {
cnn.Open(); ResetCache();
return cnn.Insert(entity); using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
public void Delete(T entity) public void Delete(T entity)
{ {
ResetCache(); try
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
db.Delete(entity); using (var db = Config.DbConnection())
{
db.Open();
db.Delete(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
public async Task DeleteAsync(T entity) public async Task DeleteAsync(T entity)
{ {
ResetCache(); try
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
await db.DeleteAsync(entity); using (var db = Config.DbConnection())
{
db.Open();
await db.DeleteAsync(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
public bool Update(T entity) public bool Update(T entity)
{ {
ResetCache(); try
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
return db.Update(entity); using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
public async Task<bool> UpdateAsync(T entity) public async Task<bool> UpdateAsync(T entity)
{ {
ResetCache(); try
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
return await db.UpdateAsync(entity); using (var db = Config.DbConnection())
{
db.Open();
return await db.UpdateAsync(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
public bool UpdateAll(IEnumerable<T> entity) public bool UpdateAll(IEnumerable<T> entity)
{ {
ResetCache(); try
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
foreach (var e in entity) var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{ {
result.Add(db.Update(e)); db.Open();
foreach (var e in entity)
{
result.Add(db.Update(e));
}
} }
return result.All(x => true);
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
return result.All(x => true);
} }
public async Task<bool> UpdateAllAsync(IEnumerable<T> entity) public async Task<bool> UpdateAllAsync(IEnumerable<T> entity)
{ {
ResetCache(); try
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{ {
db.Open(); ResetCache();
foreach (var e in entity) var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{ {
result.Add(await db.UpdateAsync(e)); db.Open();
foreach (var e in entity)
{
result.Add(await db.UpdateAsync(e));
}
} }
return result.All(x => true);
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
return result.All(x => true);
} }
public async Task<int> InsertAsync(T entity) public async Task<int> InsertAsync(T entity)
{ {
ResetCache(); try
using (var cnn = Config.DbConnection())
{ {
cnn.Open(); ResetCache();
return await cnn.InsertAsync(entity); using (var cnn = Config.DbConnection())
{
cnn.Open();
return await cnn.InsertAsync(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
@ -151,25 +217,38 @@ namespace PlexRequests.Store.Repository
} }
public IEnumerable<T> GetAll() public IEnumerable<T> GetAll()
{ {
try
using (var db = Config.DbConnection())
{ {
db.Open(); using (var db = Config.DbConnection())
var result = db.GetAll<T>(); {
return result; 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() public async Task<IEnumerable<T>> GetAllAsync()
{ {
try
using (var db = Config.DbConnection())
{ {
db.Open(); using (var db = Config.DbConnection())
var result = await db.GetAllAsync<T>(); {
return result; db.Open();
var result = await db.GetAllAsync<T>();
return result;
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
} }
} }
} }
} }