jellyfin/Emby.Server.Implementations/Data/BaseSqliteRepository.cs

312 lines
10 KiB
C#
Raw Normal View History

#nullable disable
2019-11-01 10:38:54 -07:00
#pragma warning disable CS1591
using System;
2016-11-18 01:39:20 -07:00
using System.Collections.Generic;
2021-12-20 05:31:07 -07:00
using Jellyfin.Extensions;
using Microsoft.Extensions.Logging;
using SQLitePCL.pretty;
2016-11-18 01:39:20 -07:00
namespace Emby.Server.Implementations.Data
{
public abstract class BaseSqliteRepository : IDisposable
{
2019-04-03 08:34:54 -07:00
private bool _disposed = false;
2016-11-18 01:39:20 -07:00
2019-11-01 10:38:54 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="BaseSqliteRepository"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
2020-06-05 17:15:56 -07:00
protected BaseSqliteRepository(ILogger<BaseSqliteRepository> logger)
2016-11-18 01:39:20 -07:00
{
Logger = logger;
2016-11-20 16:48:52 -07:00
}
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets or sets the path to the DB file.
/// </summary>
2019-04-03 08:34:54 -07:00
protected string DbFilePath { get; set; }
2016-11-18 01:39:20 -07:00
2023-04-14 04:43:56 -07:00
/// <summary>
/// Gets or sets the number of write connections to create.
/// </summary>
/// <value>Path to the DB file.</value>
protected int WriteConnectionsCount { get; set; } = 1;
/// <summary>
/// Gets or sets the number of read connections to create.
/// </summary>
protected int ReadConnectionsCount { get; set; } = 1;
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the logger.
/// </summary>
/// <value>The logger.</value>
2020-06-05 17:15:56 -07:00
protected ILogger<BaseSqliteRepository> Logger { get; }
2016-11-28 12:26:48 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the default connection flags.
/// </summary>
/// <value>The default connection flags.</value>
2019-04-03 08:34:54 -07:00
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;
2016-12-10 22:12:00 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the transaction mode.
/// </summary>
/// <value>The transaction mode.</value>>
protected TransactionMode TransactionMode => TransactionMode.Deferred;
2016-11-18 01:39:20 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the transaction mode for read-only operations.
/// </summary>
/// <value>The transaction mode.</value>
protected TransactionMode ReadTransactionMode => TransactionMode.Deferred;
2016-12-10 22:12:00 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the cache size.
/// </summary>
/// <value>The cache size or null.</value>
2019-04-03 08:34:54 -07:00
protected virtual int? CacheSize => null;
2016-12-10 22:12:00 -07:00
2023-01-08 16:07:53 -07:00
/// <summary>
/// Gets the locking mode. <see href="https://www.sqlite.org/pragma.html#pragma_locking_mode" />.
/// </summary>
2023-04-14 04:43:56 -07:00
protected virtual string LockingMode => "NORMAL";
2023-01-08 16:07:53 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
2021-08-28 15:32:50 -07:00
/// Gets the journal mode. <see href="https://www.sqlite.org/pragma.html#pragma_journal_mode" />.
2019-07-01 09:24:35 -07:00
/// </summary>
/// <value>The journal mode.</value>
2023-01-08 16:07:53 -07:00
protected virtual string JournalMode => "WAL";
2016-12-10 22:12:00 -07:00
2023-01-10 14:29:05 -07:00
/// <summary>
/// Gets the journal size limit. <see href="https://www.sqlite.org/pragma.html#pragma_journal_size_limit" />.
/// </summary>
/// <value>The journal size limit.</value>
protected virtual int? JournalSizeLimit => 0;
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the page size.
/// </summary>
/// <value>The page size or null.</value>
2019-04-03 08:34:54 -07:00
protected virtual int? PageSize => null;
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the temp store mode.
/// </summary>
/// <value>The temp store mode.</value>
/// <see cref="TempStoreMode"/>
2023-04-14 04:43:56 -07:00
protected virtual TempStoreMode TempStore => TempStoreMode.Memory;
2016-11-19 00:51:07 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the synchronous mode.
/// </summary>
/// <value>The synchronous mode or null.</value>
/// <see cref="SynchronousMode"/>
2023-01-09 06:14:19 -07:00
protected virtual SynchronousMode? Synchronous => SynchronousMode.Normal;
2016-11-20 14:02:32 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets or sets the write lock.
/// </summary>
/// <value>The write lock.</value>
2023-04-14 04:43:56 -07:00
protected ConnectionPool WriteConnections { get; set; }
2016-11-21 01:54:53 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets or sets the write connection.
/// </summary>
/// <value>The write connection.</value>
2023-04-14 04:43:56 -07:00
protected ConnectionPool ReadConnections { get; set; }
public virtual void Initialize()
{
WriteConnections = new ConnectionPool(WriteConnectionsCount, CreateWriteConnection);
ReadConnections = new ConnectionPool(ReadConnectionsCount, CreateReadConnection);
}
2022-03-05 12:37:23 -07:00
protected ManagedConnection GetConnection(bool readOnly = false)
2016-12-13 08:44:34 -07:00
{
2023-04-14 04:43:56 -07:00
if (readOnly)
2016-12-13 08:44:34 -07:00
{
2023-04-14 04:43:56 -07:00
return ReadConnections.GetConnection();
2016-12-13 08:44:34 -07:00
}
2023-04-14 04:43:56 -07:00
return WriteConnections.GetConnection();
}
protected SQLiteDatabaseConnection CreateWriteConnection()
{
var writeConnection = SQLite3.Open(
2019-03-11 14:33:27 -07:00
DbFilePath,
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
null);
2016-11-29 12:12:37 -07:00
2019-04-03 08:34:54 -07:00
if (CacheSize.HasValue)
2019-02-20 06:26:49 -07:00
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
2019-04-03 08:34:54 -07:00
}
2023-01-08 16:07:53 -07:00
if (!string.IsNullOrWhiteSpace(LockingMode))
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA locking_mode=" + LockingMode);
2023-01-08 16:07:53 -07:00
}
2019-04-03 08:34:54 -07:00
if (!string.IsNullOrWhiteSpace(JournalMode))
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA journal_mode=" + JournalMode);
}
2016-11-27 12:36:56 -07:00
2023-01-10 14:29:05 -07:00
if (JournalSizeLimit.HasValue)
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value);
2023-01-10 14:29:05 -07:00
}
2019-04-03 08:34:54 -07:00
if (Synchronous.HasValue)
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
2019-04-03 08:34:54 -07:00
if (PageSize.HasValue)
{
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA page_size=" + PageSize.Value);
}
2016-11-19 00:51:07 -07:00
2023-04-14 04:43:56 -07:00
writeConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
2019-08-14 15:00:21 -07:00
// Configuration and pragmas can affect VACUUM so it needs to be last.
2023-04-14 04:43:56 -07:00
writeConnection.Execute("VACUUM");
2016-12-13 08:44:34 -07:00
2023-04-14 04:43:56 -07:00
return writeConnection;
}
protected SQLiteDatabaseConnection CreateReadConnection()
{
var writeConnection = SQLite3.Open(
DbFilePath,
DefaultConnectionFlags | ConnectionFlags.ReadOnly,
null);
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 writeConnection;
2016-11-18 01:39:20 -07:00
}
2016-12-13 08:44:34 -07:00
public IStatement PrepareStatement(ManagedConnection connection, string sql)
2019-02-26 10:58:33 -07:00
=> connection.PrepareStatement(sql);
2016-12-13 08:44:34 -07:00
2016-12-10 22:12:00 -07:00
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
2019-02-26 10:58:33 -07:00
=> connection.PrepareStatement(sql);
2016-12-10 22:12:00 -07:00
2018-09-12 10:26:21 -07:00
protected bool TableExists(ManagedConnection connection, string name)
{
2020-10-12 11:05:11 -07:00
return connection.RunInTransaction(
2021-12-24 10:28:27 -07:00
db =>
2018-09-12 10:26:21 -07:00
{
2021-12-24 10:28:27 -07:00
using (var statement = PrepareStatement(db, "select DISTINCT tbl_name from sqlite_master"))
2018-09-12 10:26:21 -07:00
{
2021-12-24 10:28:27 -07:00
foreach (var row in statement.ExecuteQuery())
2018-09-12 10:26:21 -07:00
{
2021-12-24 10:28:27 -07:00
if (string.Equals(name, row.GetString(0), StringComparison.OrdinalIgnoreCase))
{
return true;
}
2018-09-12 10:26:21 -07:00
}
}
2021-12-24 10:28:27 -07:00
return false;
},
ReadTransactionMode);
2018-09-12 10:26:21 -07:00
}
2019-04-03 09:02:43 -07:00
protected List<string> GetColumnNames(IDatabaseConnection connection, string table)
2016-11-29 12:12:37 -07:00
{
2019-07-01 08:59:01 -07:00
var columnNames = new List<string>();
2016-11-29 12:12:37 -07:00
2019-04-03 09:02:43 -07:00
foreach (var row in connection.Query("PRAGMA table_info(" + table + ")"))
{
2021-05-16 05:49:11 -07:00
if (row.TryGetString(1, out var columnName))
{
2021-05-16 05:49:11 -07:00
columnNames.Add(columnName);
2019-04-03 09:02:43 -07:00
}
}
2016-11-29 12:12:37 -07:00
2019-07-01 08:59:01 -07:00
return columnNames;
2016-11-29 12:12:37 -07:00
}
2019-04-03 09:02:43 -07:00
protected void AddColumn(IDatabaseConnection connection, string table, string columnName, string type, List<string> existingColumnNames)
{
2021-12-20 05:31:07 -07:00
if (existingColumnNames.Contains(columnName, StringComparison.OrdinalIgnoreCase))
2019-04-03 09:02:43 -07:00
{
return;
}
2016-11-27 12:36:56 -07:00
2019-04-03 09:02:43 -07:00
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
}
2016-11-19 11:09:15 -07:00
2016-11-18 01:39:20 -07:00
protected void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name, "Object has been disposed and cannot be accessed.");
2016-11-18 01:39:20 -07:00
}
}
2019-07-01 08:59:01 -07:00
/// <inheritdoc />
2016-11-18 01:39:20 -07:00
public void Dispose()
{
Dispose(true);
2019-04-02 13:15:18 -07:00
GC.SuppressFinalize(this);
2016-11-18 01:39:20 -07: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 06:26:49 -07:00
if (_disposed)
2016-11-18 01:39:20 -07:00
{
2019-02-20 06:26:49 -07:00
return;
}
2019-02-26 10:58:33 -07:00
if (dispose)
{
2023-04-14 04:43:56 -07:00
WriteConnections.Dispose();
ReadConnections.Dispose();
}
2016-11-18 01:39:20 -07:00
2019-02-20 06:26:49 -07:00
_disposed = true;
2016-11-18 01:39:20 -07:00
}
}
}