Wrapped the repo to catch Sqlite corrupt messages.

pull/470/head
tidusjar 8 years ago
parent 33ba1db20b
commit c5ad97780f

@ -25,18 +25,22 @@
// ************************************************************************/
#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;
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)
{
Config = config;
@ -52,95 +56,157 @@ namespace PlexRequests.Store.Repository
public long Insert(T entity)
{
ResetCache();
using (var cnn = Config.DbConnection())
try
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
cnn.Open();
return cnn.Insert(entity);
Log.Fatal(CorruptMessage);
throw;
}
}
public void Delete(T entity)
{
ResetCache();
using (var db = Config.DbConnection())
try
{
db.Open();
db.Delete(entity);
ResetCache();
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)
{
ResetCache();
using (var db = Config.DbConnection())
try
{
db.Open();
await db.DeleteAsync(entity);
ResetCache();
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)
{
ResetCache();
using (var db = Config.DbConnection())
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
db.Open();
return db.Update(entity);
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<bool> UpdateAsync(T entity)
{
ResetCache();
using (var db = Config.DbConnection())
try
{
db.Open();
return await db.UpdateAsync(entity);
ResetCache();
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)
{
ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
try
{
db.Open();
foreach (var e in entity)
ResetCache();
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)
{
ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
try
{
db.Open();
foreach (var e in entity)
ResetCache();
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)
{
ResetCache();
using (var cnn = Config.DbConnection())
try
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return await cnn.InsertAsync(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
cnn.Open();
return await cnn.InsertAsync(entity);
Log.Fatal(CorruptMessage);
throw;
}
}
@ -151,25 +217,38 @@ namespace PlexRequests.Store.Repository
}
public IEnumerable<T> GetAll()
{
using (var db = Config.DbConnection())
try
{
using (var db = Config.DbConnection())
{
db.Open();
var result = db.GetAll<T>();
return result;
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
db.Open();
var result = db.GetAll<T>();
return result;
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<IEnumerable<T>> GetAllAsync()
{
using (var db = Config.DbConnection())
try
{
db.Open();
var result = await db.GetAllAsync<T>();
return result;
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;
}
}
}
}
Loading…
Cancel
Save