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