2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2016-11-18 03:39:20 -05:00
|
|
|
using System.Collections.Generic;
|
2024-06-06 14:30:40 -04:00
|
|
|
using System.Threading;
|
2021-12-20 13:31:07 +01:00
|
|
|
using Jellyfin.Extensions;
|
2023-08-21 12:13:32 +02:00
|
|
|
using Microsoft.Data.Sqlite;
|
2018-12-13 14:18:25 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-18 03:39:20 -05:00
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Data
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseSqliteRepository : IDisposable
|
|
|
|
|
{
|
2019-04-03 17:34:54 +02:00
|
|
|
private bool _disposed = false;
|
2024-06-06 14:30:40 -04:00
|
|
|
private SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
|
|
|
|
|
private SqliteConnection _writeConnection;
|
2016-11-18 03:39:20 -05:00
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="BaseSqliteRepository"/> class.
|
|
|
|
|
/// </summary>
|
2019-12-10 16:22:03 +01:00
|
|
|
/// <param name="logger">The logger.</param>
|
2020-06-05 18:15:56 -06:00
|
|
|
protected BaseSqliteRepository(ILogger<BaseSqliteRepository> logger)
|
2016-11-18 03:39:20 -05:00
|
|
|
{
|
|
|
|
|
Logger = logger;
|
2016-11-20 18:48:52 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path to the DB file.
|
|
|
|
|
/// </summary>
|
2019-04-03 17:34:54 +02:00
|
|
|
protected string DbFilePath { get; set; }
|
2016-11-18 03:39:20 -05:00
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The logger.</value>
|
2020-06-05 18:15:56 -06:00
|
|
|
protected ILogger<BaseSqliteRepository> Logger { get; }
|
2016-11-28 14:26:48 -05:00
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cache size.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The cache size or null.</value>
|
2019-04-03 17:34:54 +02:00
|
|
|
protected virtual int? CacheSize => null;
|
2016-12-11 00:12:00 -05:00
|
|
|
|
2023-01-09 00:07:53 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the locking mode. <see href="https://www.sqlite.org/pragma.html#pragma_locking_mode" />.
|
|
|
|
|
/// </summary>
|
2023-04-14 13:43:56 +02:00
|
|
|
protected virtual string LockingMode => "NORMAL";
|
2023-01-09 00:07:53 +01:00
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
2021-08-28 16:32:50 -06:00
|
|
|
/// Gets the journal mode. <see href="https://www.sqlite.org/pragma.html#pragma_journal_mode" />.
|
2019-07-01 18:24:35 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The journal mode.</value>
|
2023-01-09 00:07:53 +01:00
|
|
|
protected virtual string JournalMode => "WAL";
|
2016-12-11 00:12:00 -05:00
|
|
|
|
2023-01-10 22:29:05 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the journal size limit. <see href="https://www.sqlite.org/pragma.html#pragma_journal_size_limit" />.
|
2024-08-20 01:03:40 +09:00
|
|
|
/// The default (-1) is overridden to prevent unconstrained WAL size, as reported by users.
|
2023-01-10 22:29:05 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The journal size limit.</value>
|
2023-04-22 09:18:12 -05:00
|
|
|
protected virtual int? JournalSizeLimit => 134_217_728; // 128MiB
|
2023-01-10 22:29:05 +01:00
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the page size.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The page size or null.</value>
|
2019-04-03 17:34:54 +02:00
|
|
|
protected virtual int? PageSize => null;
|
|
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the temp store mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The temp store mode.</value>
|
|
|
|
|
/// <see cref="TempStoreMode"/>
|
2023-04-14 13:43:56 +02:00
|
|
|
protected virtual TempStoreMode TempStore => TempStoreMode.Memory;
|
2016-11-19 02:51:07 -05:00
|
|
|
|
2019-07-01 18:24:35 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the synchronous mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The synchronous mode or null.</value>
|
|
|
|
|
/// <see cref="SynchronousMode"/>
|
2023-01-09 14:14:19 +01:00
|
|
|
protected virtual SynchronousMode? Synchronous => SynchronousMode.Normal;
|
2016-11-20 16:02:32 -05:00
|
|
|
|
2023-04-14 13:43:56 +02:00
|
|
|
public virtual void Initialize()
|
|
|
|
|
{
|
2023-04-14 21:38:12 +02:00
|
|
|
// Configuration and pragmas can affect VACUUM so it needs to be last.
|
2023-04-14 22:43:14 +02:00
|
|
|
using (var connection = GetConnection())
|
2016-12-13 10:44:34 -05:00
|
|
|
{
|
2023-04-14 21:38:12 +02:00
|
|
|
connection.Execute("VACUUM");
|
2016-12-13 10:44:34 -05:00
|
|
|
}
|
2023-04-14 13:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
protected ManagedConnection GetConnection(bool readOnly = false)
|
2023-04-14 13:43:56 +02:00
|
|
|
{
|
2024-06-06 14:30:40 -04:00
|
|
|
if (!readOnly)
|
|
|
|
|
{
|
|
|
|
|
_writeLock.Wait();
|
|
|
|
|
if (_writeConnection is not null)
|
|
|
|
|
{
|
|
|
|
|
return new ManagedConnection(_writeConnection, _writeLock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var writeConnection = new SqliteConnection($"Filename={DbFilePath};Pooling=False");
|
|
|
|
|
writeConnection.Open();
|
|
|
|
|
|
|
|
|
|
if (CacheSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(LockingMode))
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA locking_mode=" + LockingMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(JournalMode))
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA journal_mode=" + JournalMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (JournalSizeLimit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Synchronous.HasValue)
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PageSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
writeConnection.Execute("PRAGMA page_size=" + PageSize.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
|
|
|
|
|
|
|
|
|
|
return new ManagedConnection(_writeConnection = writeConnection, _writeLock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connection = new SqliteConnection($"Filename={DbFilePath};Mode=ReadOnly");
|
2023-08-21 21:38:16 +02:00
|
|
|
connection.Open();
|
2016-11-29 14:12:37 -05:00
|
|
|
|
2019-04-03 17:34:54 +02:00
|
|
|
if (CacheSize.HasValue)
|
2019-02-20 14:26:49 +01:00
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA cache_size=" + CacheSize.Value);
|
2019-04-03 17:34:54 +02:00
|
|
|
}
|
2017-09-22 01:54:57 -04:00
|
|
|
|
2023-01-09 00:07:53 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(LockingMode))
|
|
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA locking_mode=" + LockingMode);
|
2023-01-09 00:07:53 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 17:34:54 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(JournalMode))
|
|
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA journal_mode=" + JournalMode);
|
2019-02-26 18:30:13 +01:00
|
|
|
}
|
2016-11-27 14:36:56 -05:00
|
|
|
|
2023-01-10 22:29:05 +01:00
|
|
|
if (JournalSizeLimit.HasValue)
|
|
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value);
|
2023-01-10 22:29:05 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 17:34:54 +02:00
|
|
|
if (Synchronous.HasValue)
|
2019-02-26 18:30:13 +01:00
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
|
2019-02-26 18:30:13 +01:00
|
|
|
}
|
2017-09-22 01:54:57 -04:00
|
|
|
|
2019-04-03 17:34:54 +02:00
|
|
|
if (PageSize.HasValue)
|
2019-02-26 18:30:13 +01:00
|
|
|
{
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA page_size=" + PageSize.Value);
|
2019-02-26 18:30:13 +01:00
|
|
|
}
|
2016-11-19 02:51:07 -05:00
|
|
|
|
2023-08-21 20:34:50 +02:00
|
|
|
connection.Execute("PRAGMA temp_store=" + (int)TempStore);
|
2019-08-15 00:00:21 +02:00
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
return new ManagedConnection(connection, null);
|
2023-04-14 13:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
public SqliteCommand PrepareStatement(ManagedConnection connection, string sql)
|
2023-04-14 13:43:56 +02:00
|
|
|
{
|
2023-08-21 12:13:32 +02:00
|
|
|
var command = connection.CreateCommand();
|
|
|
|
|
command.CommandText = sql;
|
|
|
|
|
return command;
|
2016-11-18 03:39:20 -05:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
protected bool TableExists(ManagedConnection connection, string name)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2023-08-21 14:12:49 +02:00
|
|
|
using var statement = PrepareStatement(connection, "select DISTINCT tbl_name from sqlite_master");
|
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(name, row.GetString(0), StringComparison.OrdinalIgnoreCase))
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2023-08-21 14:12:49 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
protected List<string> GetColumnNames(ManagedConnection connection, string table)
|
2016-11-29 14:12:37 -05:00
|
|
|
{
|
2019-07-01 17:59:01 +02:00
|
|
|
var columnNames = new List<string>();
|
2016-11-29 14:12:37 -05:00
|
|
|
|
2019-04-03 18:02:43 +02:00
|
|
|
foreach (var row in connection.Query("PRAGMA table_info(" + table + ")"))
|
2017-08-09 15:56:38 -04:00
|
|
|
{
|
2021-05-16 14:49:11 +02:00
|
|
|
if (row.TryGetString(1, out var columnName))
|
2017-08-09 15:56:38 -04:00
|
|
|
{
|
2021-05-16 14:49:11 +02:00
|
|
|
columnNames.Add(columnName);
|
2019-04-03 18:02:43 +02:00
|
|
|
}
|
2017-08-09 15:56:38 -04:00
|
|
|
}
|
2016-11-29 14:12:37 -05:00
|
|
|
|
2019-07-01 17:59:01 +02:00
|
|
|
return columnNames;
|
2016-11-29 14:12:37 -05:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
protected void AddColumn(ManagedConnection connection, string table, string columnName, string type, List<string> existingColumnNames)
|
2019-04-03 18:02:43 +02:00
|
|
|
{
|
2021-12-20 13:31:07 +01:00
|
|
|
if (existingColumnNames.Contains(columnName, StringComparison.OrdinalIgnoreCase))
|
2019-04-03 18:02:43 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-27 14:36:56 -05:00
|
|
|
|
2019-04-03 18:02:43 +02:00
|
|
|
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
|
|
|
|
|
}
|
2016-11-19 13:09:15 -05:00
|
|
|
|
2016-11-18 03:39:20 -05:00
|
|
|
protected void CheckDisposed()
|
|
|
|
|
{
|
2024-04-13 01:45:01 +02:00
|
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
2016-11-18 03:39:20 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-01 17:59:01 +02:00
|
|
|
/// <inheritdoc />
|
2016-11-18 03:39:20 -05:00
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2019-04-02 22:15:18 +02:00
|
|
|
GC.SuppressFinalize(this);
|
2016-11-18 03:39:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
2019-02-20 14:26:49 +01:00
|
|
|
if (_disposed)
|
2016-11-18 03:39:20 -05:00
|
|
|
{
|
2019-02-20 14:26:49 +01:00
|
|
|
return;
|
2017-09-22 01:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:30:40 -04:00
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
_writeLock.Wait();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_writeConnection.Dispose();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_writeLock.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_writeLock.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_writeConnection = null;
|
|
|
|
|
_writeLock = null;
|
|
|
|
|
|
2019-02-20 14:26:49 +01:00
|
|
|
_disposed = true;
|
2016-11-18 03:39:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|