From 13f6da1bf43e592264afbf48f0c99015435b8c7e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 18 Jun 2013 15:16:27 -0400 Subject: removed chapters from baseitem --- .../Persistence/SqliteUserRepository.cs | 78 +++++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs index efd39529a9..09e34cf08e 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs @@ -16,15 +16,14 @@ namespace MediaBrowser.Server.Implementations.Persistence /// /// Class SQLiteUserRepository /// - public class SqliteUserRepository : SqliteRepository, IUserRepository + public class SqliteUserRepository : IUserRepository { - /// - /// The repository name - /// - public const string RepositoryName = "SQLite"; - + private readonly ILogger _logger; + private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); + private SQLiteConnection _connection; + /// /// Gets the name of the repository /// @@ -33,7 +32,7 @@ namespace MediaBrowser.Server.Implementations.Persistence { get { - return RepositoryName; + return "SQLite"; } } @@ -56,7 +55,6 @@ namespace MediaBrowser.Server.Implementations.Persistence /// The log manager. /// appPaths public SqliteUserRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager) - : base(logManager) { if (appPaths == null) { @@ -69,6 +67,8 @@ namespace MediaBrowser.Server.Implementations.Persistence _appPaths = appPaths; _jsonSerializer = jsonSerializer; + + _logger = logManager.GetLogger(GetType().Name); } /// @@ -79,7 +79,7 @@ namespace MediaBrowser.Server.Implementations.Persistence { var dbFile = Path.Combine(_appPaths.DataPath, "users.db"); - await ConnectToDb(dbFile).ConfigureAwait(false); + _connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false); string[] queries = { @@ -90,7 +90,7 @@ namespace MediaBrowser.Server.Implementations.Persistence "pragma temp_store = memory" }; - RunQueries(queries); + _connection.RunQueries(queries, _logger); } /// @@ -124,9 +124,9 @@ namespace MediaBrowser.Server.Implementations.Persistence try { - transaction = Connection.BeginTransaction(); + transaction = _connection.BeginTransaction(); - using (var cmd = Connection.CreateCommand()) + using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "replace into users (guid, data) values (@1, @2)"; cmd.AddParam("@1", user.Id); @@ -150,7 +150,7 @@ namespace MediaBrowser.Server.Implementations.Persistence } catch (Exception e) { - Logger.ErrorException("Failed to save user:", e); + _logger.ErrorException("Failed to save user:", e); if (transaction != null) { @@ -176,7 +176,7 @@ namespace MediaBrowser.Server.Implementations.Persistence /// IEnumerable{User}. public IEnumerable RetrieveAllUsers() { - using (var cmd = Connection.CreateCommand()) + using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "select data from users"; @@ -184,7 +184,7 @@ namespace MediaBrowser.Server.Implementations.Persistence { while (reader.Read()) { - using (var stream = GetStream(reader, 0)) + using (var stream = reader.GetMemoryStream(0)) { var user = _jsonSerializer.DeserializeFromStream(stream); yield return user; @@ -221,9 +221,9 @@ namespace MediaBrowser.Server.Implementations.Persistence try { - transaction = Connection.BeginTransaction(); + transaction = _connection.BeginTransaction(); - using (var cmd = Connection.CreateCommand()) + using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "delete from users where guid=@guid"; @@ -248,7 +248,7 @@ namespace MediaBrowser.Server.Implementations.Persistence } catch (Exception e) { - Logger.ErrorException("Failed to delete user:", e); + _logger.ErrorException("Failed to delete user:", e); if (transaction != null) { @@ -267,5 +267,47 @@ namespace MediaBrowser.Server.Implementations.Persistence _writeLock.Release(); } } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private readonly object _disposeLock = new object(); + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + try + { + lock (_disposeLock) + { + if (_connection != null) + { + if (_connection.IsOpen()) + { + _connection.Close(); + } + + _connection.Dispose(); + _connection = null; + } + } + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing database", ex); + } + } + } } } \ No newline at end of file -- cgit v1.2.3