| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Data.Common; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Text; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | |
|
| | 10 | | namespace Jellyfin.Database.Providers.Sqlite; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Injects a series of PRAGMA on each connection starts. |
| | 14 | | /// </summary> |
| | 15 | | public class PragmaConnectionInterceptor : DbConnectionInterceptor |
| | 16 | | { |
| | 17 | | private readonly ILogger _logger; |
| | 18 | | private readonly int? _cacheSize; |
| | 19 | | private readonly string _lockingMode; |
| | 20 | | private readonly int? _journalSizeLimit; |
| | 21 | | private readonly int _tempStoreMode; |
| | 22 | | private readonly int _syncMode; |
| | 23 | | private readonly IDictionary<string, string> _customPragma; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="PragmaConnectionInterceptor"/> class. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="logger">The logger.</param> |
| | 29 | | /// <param name="cacheSize">Cache size.</param> |
| | 30 | | /// <param name="lockingMode">Locking mode.</param> |
| | 31 | | /// <param name="journalSizeLimit">Journal Size.</param> |
| | 32 | | /// <param name="tempStoreMode">The https://sqlite.org/pragma.html#pragma_temp_store pragma.</param> |
| | 33 | | /// <param name="syncMode">The https://sqlite.org/pragma.html#pragma_synchronous pragma.</param> |
| | 34 | | /// <param name="customPragma">A list of custom provided Pragma in the list of CustomOptions starting with "#PRAGMA: |
| 42 | 35 | | public PragmaConnectionInterceptor(ILogger logger, int? cacheSize, string lockingMode, int? journalSizeLimit, int te |
| | 36 | | { |
| 42 | 37 | | _logger = logger; |
| 42 | 38 | | _cacheSize = cacheSize; |
| 42 | 39 | | _lockingMode = lockingMode; |
| 42 | 40 | | _journalSizeLimit = journalSizeLimit; |
| 42 | 41 | | _tempStoreMode = tempStoreMode; |
| 42 | 42 | | _syncMode = syncMode; |
| 42 | 43 | | _customPragma = customPragma; |
| | 44 | |
|
| 42 | 45 | | InitialCommand = BuildCommandText(); |
| 42 | 46 | | _logger.LogInformation("SQLITE connection pragma command set to: \r\n {PragmaCommand}", InitialCommand); |
| 42 | 47 | | } |
| | 48 | |
|
| | 49 | | private string? InitialCommand { get; set; } |
| | 50 | |
|
| | 51 | | /// <inheritdoc/> |
| | 52 | | public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData) |
| | 53 | | { |
| 2301 | 54 | | base.ConnectionOpened(connection, eventData); |
| | 55 | |
|
| 2301 | 56 | | using (var command = connection.CreateCommand()) |
| | 57 | | { |
| | 58 | | #pragma warning disable CA2100 // Review SQL queries for security vulnerabilities |
| 2301 | 59 | | command.CommandText = InitialCommand; |
| | 60 | | #pragma warning restore CA2100 // Review SQL queries for security vulnerabilities |
| 2301 | 61 | | command.ExecuteNonQuery(); |
| 2301 | 62 | | } |
| 2301 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc/> |
| | 66 | | public override async Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, Cancella |
| | 67 | | { |
| | 68 | | await base.ConnectionOpenedAsync(connection, eventData, cancellationToken).ConfigureAwait(false); |
| | 69 | |
|
| | 70 | | var command = connection.CreateCommand(); |
| | 71 | | await using (command.ConfigureAwait(false)) |
| | 72 | | { |
| | 73 | | #pragma warning disable CA2100 // Review SQL queries for security vulnerabilities |
| | 74 | | command.CommandText = InitialCommand; |
| | 75 | | #pragma warning restore CA2100 // Review SQL queries for security vulnerabilities |
| | 76 | | await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| | 80 | | private string BuildCommandText() |
| | 81 | | { |
| 42 | 82 | | var sb = new StringBuilder(); |
| 42 | 83 | | if (_cacheSize.HasValue) |
| | 84 | | { |
| 0 | 85 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA cache_size={_cacheSize.Value};"); |
| | 86 | | } |
| | 87 | |
|
| 42 | 88 | | if (!string.IsNullOrWhiteSpace(_lockingMode)) |
| | 89 | | { |
| 42 | 90 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA locking_mode={_lockingMode};"); |
| | 91 | | } |
| | 92 | |
|
| 42 | 93 | | if (_journalSizeLimit.HasValue) |
| | 94 | | { |
| 42 | 95 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA journal_size_limit={_journalSizeLimit};"); |
| | 96 | | } |
| | 97 | |
|
| 42 | 98 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA synchronous={_syncMode};"); |
| 42 | 99 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA temp_store={_tempStoreMode};"); |
| | 100 | |
|
| 84 | 101 | | foreach (var item in _customPragma) |
| | 102 | | { |
| 0 | 103 | | sb.AppendLine(CultureInfo.InvariantCulture, $"PRAGMA {item.Key}={item.Value};"); |
| | 104 | | } |
| | 105 | |
|
| 42 | 106 | | return sb.ToString(); |
| | 107 | | } |
| | 108 | | } |