diff --git a/PlexRequests.Store/Repository/BaseGenericRepository.cs b/PlexRequests.Store/Repository/BaseGenericRepository.cs index f92fdb218..0bd272e26 100644 --- a/PlexRequests.Store/Repository/BaseGenericRepository.cs +++ b/PlexRequests.Store/Repository/BaseGenericRepository.cs @@ -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 where T : class + public abstract class BaseGenericRepository 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 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 entity) { - ResetCache(); - Log.Trace("Updating all entities"); - var result = new HashSet(); - - using (var db = Config.DbConnection()) + try { - db.Open(); - foreach (var e in entity) + ResetCache(); + var result = new HashSet(); + + 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 UpdateAllAsync(IEnumerable entity) { - ResetCache(); - Log.Trace("Updating all entities"); - var result = new HashSet(); - - using (var db = Config.DbConnection()) + try { - db.Open(); - foreach (var e in entity) + ResetCache(); + var result = new HashSet(); + + 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 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 GetAll() { - - using (var db = Config.DbConnection()) + try + { + using (var db = Config.DbConnection()) + { + db.Open(); + var result = db.GetAll(); + return result; + } + } + catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt) { - db.Open(); - var result = db.GetAll(); - return result; + Log.Fatal(CorruptMessage); + throw; } } public async Task> GetAllAsync() { - - using (var db = Config.DbConnection()) + try { - db.Open(); - var result = await db.GetAllAsync(); - return result; + using (var db = Config.DbConnection()) + { + db.Open(); + var result = await db.GetAllAsync(); + return result; + } + } + catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt) + { + Log.Fatal(CorruptMessage); + throw; } - } } } \ No newline at end of file