| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Threading; |
| | | 6 | | using Microsoft.Data.Sqlite; |
| | | 7 | | |
| | | 8 | | namespace Emby.Server.Implementations.Data; |
| | | 9 | | |
| | | 10 | | public sealed class ManagedConnection : IDisposable |
| | | 11 | | { |
| | | 12 | | private readonly SemaphoreSlim? _writeLock; |
| | | 13 | | |
| | | 14 | | private SqliteConnection _db; |
| | | 15 | | |
| | | 16 | | private bool _disposed = false; |
| | | 17 | | |
| | | 18 | | public ManagedConnection(SqliteConnection db, SemaphoreSlim? writeLock) |
| | | 19 | | { |
| | 782 | 20 | | _db = db; |
| | 782 | 21 | | _writeLock = writeLock; |
| | 782 | 22 | | } |
| | | 23 | | |
| | | 24 | | public SqliteTransaction BeginTransaction() |
| | 118 | 25 | | => _db.BeginTransaction(); |
| | | 26 | | |
| | | 27 | | public SqliteCommand CreateCommand() |
| | 788 | 28 | | => _db.CreateCommand(); |
| | | 29 | | |
| | | 30 | | public void Execute(string commandText) |
| | 1656 | 31 | | => _db.Execute(commandText); |
| | | 32 | | |
| | | 33 | | public SqliteCommand PrepareStatement(string sql) |
| | 58 | 34 | | => _db.PrepareStatement(sql); |
| | | 35 | | |
| | | 36 | | public IEnumerable<SqliteDataReader> Query(string commandText) |
| | 105 | 37 | | => _db.Query(commandText); |
| | | 38 | | |
| | | 39 | | public void Dispose() |
| | | 40 | | { |
| | 782 | 41 | | if (_disposed) |
| | | 42 | | { |
| | 0 | 43 | | return; |
| | | 44 | | } |
| | | 45 | | |
| | 782 | 46 | | if (_writeLock is null) |
| | | 47 | | { |
| | | 48 | | // Read connections are managed with an internal pool |
| | 622 | 49 | | _db.Dispose(); |
| | | 50 | | } |
| | | 51 | | else |
| | | 52 | | { |
| | | 53 | | // Write lock is managed by BaseSqliteRepository |
| | | 54 | | // Don't dispose here |
| | 160 | 55 | | _writeLock.Release(); |
| | | 56 | | } |
| | | 57 | | |
| | 782 | 58 | | _db = null!; |
| | | 59 | | |
| | 782 | 60 | | _disposed = true; |
| | 782 | 61 | | } |
| | | 62 | | } |