| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Threading; |
| | | 8 | | using Jellyfin.Extensions; |
| | | 9 | | using Microsoft.Data.Sqlite; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Emby.Server.Implementations.Data |
| | | 13 | | { |
| | | 14 | | public abstract class BaseSqliteRepository : IDisposable |
| | | 15 | | { |
| | | 16 | | private bool _disposed = false; |
| | 66 | 17 | | private SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); |
| | | 18 | | private SqliteConnection _writeConnection; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="BaseSqliteRepository"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="logger">The logger.</param> |
| | | 24 | | protected BaseSqliteRepository(ILogger<BaseSqliteRepository> logger) |
| | | 25 | | { |
| | | 26 | | Logger = logger; |
| | 66 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets or sets the path to the DB file. |
| | | 31 | | /// </summary> |
| | | 32 | | protected string DbFilePath { get; set; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the logger. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value>The logger.</value> |
| | | 38 | | protected ILogger<BaseSqliteRepository> Logger { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the cache size. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <value>The cache size or null.</value> |
| | 23 | 44 | | protected virtual int? CacheSize => null; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the locking mode. <see href="https://www.sqlite.org/pragma.html#pragma_locking_mode" />. |
| | | 48 | | /// </summary> |
| | 1328 | 49 | | protected virtual string LockingMode => "NORMAL"; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the journal mode. <see href="https://www.sqlite.org/pragma.html#pragma_journal_mode" />. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <value>The journal mode.</value> |
| | 1328 | 55 | | protected virtual string JournalMode => "WAL"; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the journal size limit. <see href="https://www.sqlite.org/pragma.html#pragma_journal_size_limit" />. |
| | | 59 | | /// The default (-1) is overridden to prevent unconstrained WAL size, as reported by users. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <value>The journal size limit.</value> |
| | 1328 | 62 | | protected virtual int? JournalSizeLimit => 134_217_728; // 128MiB |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the page size. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <value>The page size or null.</value> |
| | 664 | 68 | | protected virtual int? PageSize => null; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the temp store mode. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <value>The temp store mode.</value> |
| | | 74 | | /// <see cref="TempStoreMode"/> |
| | 23 | 75 | | protected virtual TempStoreMode TempStore => TempStoreMode.Memory; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the synchronous mode. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <value>The synchronous mode or null.</value> |
| | | 81 | | /// <see cref="SynchronousMode"/> |
| | 1328 | 82 | | protected virtual SynchronousMode? Synchronous => SynchronousMode.Normal; |
| | | 83 | | |
| | | 84 | | public virtual void Initialize() |
| | | 85 | | { |
| | | 86 | | // Configuration and pragmas can affect VACUUM so it needs to be last. |
| | 42 | 87 | | using (var connection = GetConnection()) |
| | | 88 | | { |
| | 42 | 89 | | connection.Execute("VACUUM"); |
| | 42 | 90 | | } |
| | 42 | 91 | | } |
| | | 92 | | |
| | | 93 | | protected ManagedConnection GetConnection(bool readOnly = false) |
| | | 94 | | { |
| | 782 | 95 | | if (!readOnly) |
| | | 96 | | { |
| | 160 | 97 | | _writeLock.Wait(); |
| | 160 | 98 | | if (_writeConnection is not null) |
| | | 99 | | { |
| | 118 | 100 | | return new ManagedConnection(_writeConnection, _writeLock); |
| | | 101 | | } |
| | | 102 | | |
| | 42 | 103 | | var writeConnection = new SqliteConnection($"Filename={DbFilePath};Pooling=False"); |
| | 42 | 104 | | writeConnection.Open(); |
| | | 105 | | |
| | 42 | 106 | | if (CacheSize.HasValue) |
| | | 107 | | { |
| | 21 | 108 | | writeConnection.Execute("PRAGMA cache_size=" + CacheSize.Value); |
| | | 109 | | } |
| | | 110 | | |
| | 42 | 111 | | if (!string.IsNullOrWhiteSpace(LockingMode)) |
| | | 112 | | { |
| | 42 | 113 | | writeConnection.Execute("PRAGMA locking_mode=" + LockingMode); |
| | | 114 | | } |
| | | 115 | | |
| | 42 | 116 | | if (!string.IsNullOrWhiteSpace(JournalMode)) |
| | | 117 | | { |
| | 42 | 118 | | writeConnection.Execute("PRAGMA journal_mode=" + JournalMode); |
| | | 119 | | } |
| | | 120 | | |
| | 42 | 121 | | if (JournalSizeLimit.HasValue) |
| | | 122 | | { |
| | 42 | 123 | | writeConnection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value); |
| | | 124 | | } |
| | | 125 | | |
| | 42 | 126 | | if (Synchronous.HasValue) |
| | | 127 | | { |
| | 42 | 128 | | writeConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value); |
| | | 129 | | } |
| | | 130 | | |
| | 42 | 131 | | if (PageSize.HasValue) |
| | | 132 | | { |
| | 0 | 133 | | writeConnection.Execute("PRAGMA page_size=" + PageSize.Value); |
| | | 134 | | } |
| | | 135 | | |
| | 42 | 136 | | writeConnection.Execute("PRAGMA temp_store=" + (int)TempStore); |
| | | 137 | | |
| | 42 | 138 | | return new ManagedConnection(_writeConnection = writeConnection, _writeLock); |
| | | 139 | | } |
| | | 140 | | |
| | 622 | 141 | | var connection = new SqliteConnection($"Filename={DbFilePath};Mode=ReadOnly"); |
| | 622 | 142 | | connection.Open(); |
| | | 143 | | |
| | 622 | 144 | | if (CacheSize.HasValue) |
| | | 145 | | { |
| | 620 | 146 | | connection.Execute("PRAGMA cache_size=" + CacheSize.Value); |
| | | 147 | | } |
| | | 148 | | |
| | 622 | 149 | | if (!string.IsNullOrWhiteSpace(LockingMode)) |
| | | 150 | | { |
| | 622 | 151 | | connection.Execute("PRAGMA locking_mode=" + LockingMode); |
| | | 152 | | } |
| | | 153 | | |
| | 622 | 154 | | if (!string.IsNullOrWhiteSpace(JournalMode)) |
| | | 155 | | { |
| | 622 | 156 | | connection.Execute("PRAGMA journal_mode=" + JournalMode); |
| | | 157 | | } |
| | | 158 | | |
| | 622 | 159 | | if (JournalSizeLimit.HasValue) |
| | | 160 | | { |
| | 622 | 161 | | connection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value); |
| | | 162 | | } |
| | | 163 | | |
| | 622 | 164 | | if (Synchronous.HasValue) |
| | | 165 | | { |
| | 622 | 166 | | connection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value); |
| | | 167 | | } |
| | | 168 | | |
| | 622 | 169 | | if (PageSize.HasValue) |
| | | 170 | | { |
| | 0 | 171 | | connection.Execute("PRAGMA page_size=" + PageSize.Value); |
| | | 172 | | } |
| | | 173 | | |
| | 622 | 174 | | connection.Execute("PRAGMA temp_store=" + (int)TempStore); |
| | | 175 | | |
| | 622 | 176 | | return new ManagedConnection(connection, null); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public SqliteCommand PrepareStatement(ManagedConnection connection, string sql) |
| | | 180 | | { |
| | 788 | 181 | | var command = connection.CreateCommand(); |
| | 788 | 182 | | command.CommandText = sql; |
| | 788 | 183 | | return command; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | protected bool TableExists(ManagedConnection connection, string name) |
| | | 187 | | { |
| | 42 | 188 | | using var statement = PrepareStatement(connection, "select DISTINCT tbl_name from sqlite_master"); |
| | 672 | 189 | | foreach (var row in statement.ExecuteQuery()) |
| | | 190 | | { |
| | 294 | 191 | | if (string.Equals(name, row.GetString(0), StringComparison.OrdinalIgnoreCase)) |
| | | 192 | | { |
| | 0 | 193 | | return true; |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | 42 | 197 | | return false; |
| | 42 | 198 | | } |
| | | 199 | | |
| | | 200 | | protected List<string> GetColumnNames(ManagedConnection connection, string table) |
| | | 201 | | { |
| | 105 | 202 | | var columnNames = new List<string>(); |
| | | 203 | | |
| | 2814 | 204 | | foreach (var row in connection.Query("PRAGMA table_info(" + table + ")")) |
| | | 205 | | { |
| | 1302 | 206 | | if (row.TryGetString(1, out var columnName)) |
| | | 207 | | { |
| | 1302 | 208 | | columnNames.Add(columnName); |
| | | 209 | | } |
| | | 210 | | } |
| | | 211 | | |
| | 105 | 212 | | return columnNames; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | protected void AddColumn(ManagedConnection connection, string table, string columnName, string type, List<string |
| | | 216 | | { |
| | 2121 | 217 | | if (existingColumnNames.Contains(columnName, StringComparison.OrdinalIgnoreCase)) |
| | | 218 | | { |
| | 588 | 219 | | return; |
| | | 220 | | } |
| | | 221 | | |
| | 1533 | 222 | | connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL"); |
| | 1533 | 223 | | } |
| | | 224 | | |
| | | 225 | | protected void CheckDisposed() |
| | | 226 | | { |
| | 791 | 227 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 791 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <inheritdoc /> |
| | | 231 | | public void Dispose() |
| | | 232 | | { |
| | 42 | 233 | | Dispose(true); |
| | 42 | 234 | | GC.SuppressFinalize(this); |
| | 42 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release o |
| | | 241 | | protected virtual void Dispose(bool dispose) |
| | | 242 | | { |
| | 42 | 243 | | if (_disposed) |
| | | 244 | | { |
| | 0 | 245 | | return; |
| | | 246 | | } |
| | | 247 | | |
| | 42 | 248 | | if (dispose) |
| | | 249 | | { |
| | 42 | 250 | | _writeLock.Wait(); |
| | | 251 | | try |
| | | 252 | | { |
| | 42 | 253 | | _writeConnection.Dispose(); |
| | 42 | 254 | | } |
| | | 255 | | finally |
| | | 256 | | { |
| | 42 | 257 | | _writeLock.Release(); |
| | 42 | 258 | | } |
| | | 259 | | |
| | 42 | 260 | | _writeLock.Dispose(); |
| | | 261 | | } |
| | | 262 | | |
| | 42 | 263 | | _writeConnection = null; |
| | 42 | 264 | | _writeLock = null; |
| | | 265 | | |
| | 42 | 266 | | _disposed = true; |
| | 42 | 267 | | } |
| | | 268 | | } |
| | | 269 | | } |