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

288 lines
9.0 KiB
C#
Raw Normal View History

using System;
2016-11-18 01:39:20 -07:00
using System.Collections.Generic;
using System.Linq;
2016-11-18 01:39:20 -07:00
using System.Threading;
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
protected BaseSqliteRepository(ILogger logger)
{
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>
/// <value>Path to the DB file.</value>
2019-04-03 08:34:54 -07:00
protected string DbFilePath { get; set; }
2016-11-18 01:39:20 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the logger.
/// </summary>
/// <value>The logger.</value>
2019-04-03 08:34:54 -07:00
protected ILogger 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
2019-07-01 09:24:35 -07:00
/// <summary>
/// Gets the journal mode.
/// </summary>
/// <value>The journal mode.</value>
2019-04-03 08:34:54 -07:00
protected virtual string JournalMode => "WAL";
2016-12-10 22:12:00 -07:00
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"/>
2019-04-03 08:34:54 -07:00
protected virtual TempStoreMode TempStore => TempStoreMode.Default;
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"/>
2019-04-03 08:34:54 -07:00
protected virtual SynchronousMode? Synchronous => null;
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>
2019-07-01 08:59:01 -07:00
protected SemaphoreSlim WriteLock { get; set; } = new SemaphoreSlim(1, 1);
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>
2019-07-01 08:59:01 -07:00
protected SQLiteDatabaseConnection WriteConnection { get; set; }
2016-12-13 08:44:34 -07:00
2019-03-11 15:07:38 -07:00
protected ManagedConnection GetConnection(bool _ = false)
2016-12-13 08:44:34 -07:00
{
2019-04-02 13:15:18 -07:00
WriteLock.Wait();
if (WriteConnection != null)
2016-12-13 08:44:34 -07:00
{
2019-04-02 13:15:18 -07:00
return new ManagedConnection(WriteConnection, WriteLock);
2016-12-13 08:44:34 -07:00
}
2019-04-02 13:15:18 -07:00
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
{
2019-07-01 08:59:01 -07:00
WriteConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
2019-04-03 08:34:54 -07:00
}
2019-04-03 08:34:54 -07:00
if (!string.IsNullOrWhiteSpace(JournalMode))
{
WriteConnection.Execute("PRAGMA journal_mode=" + JournalMode);
}
2016-11-27 12:36:56 -07:00
2019-04-03 08:34:54 -07:00
if (Synchronous.HasValue)
{
2019-04-03 08:34:54 -07:00
WriteConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
2019-04-03 08:34:54 -07:00
if (PageSize.HasValue)
{
2019-07-01 08:59:01 -07:00
WriteConnection.Execute("PRAGMA page_size=" + PageSize.Value);
}
2016-11-19 00:51:07 -07:00
2019-04-03 08:34:54 -07:00
WriteConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
2016-12-13 08:44:34 -07:00
2019-04-02 13:15:18 -07:00
return new ManagedConnection(WriteConnection, WriteLock);
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
2019-04-03 04:46:07 -07:00
public IEnumerable<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)
2019-02-26 10:58:33 -07:00
=> sql.Select(connection.PrepareStatement);
2016-12-10 22:12:00 -07:00
2018-09-12 10:26:21 -07:00
protected bool TableExists(ManagedConnection connection, string name)
{
return connection.RunInTransaction(db =>
{
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 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 + ")"))
{
2019-04-03 09:02:43 -07:00
if (row[1].SQLiteType != SQLiteType.Null)
{
2019-04-03 09:02:43 -07:00
var name = row[1].ToString();
2019-07-01 08:59:01 -07:00
columnNames.Add(name);
2019-04-03 09:02:43 -07:00
}
}
// Configuration and pragmas can affect VACUUM so it needs to be last.
queries.Add("VACUUM");
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)
{
if (existingColumnNames.Contains(columnName, StringComparer.OrdinalIgnoreCase))
{
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)
{
2019-04-02 13:15:18 -07:00
WriteLock.Wait();
2019-02-26 10:58:33 -07:00
try
2016-11-18 01:39:20 -07:00
{
2019-04-02 13:15:18 -07:00
WriteConnection.Dispose();
2016-11-18 01:39:20 -07:00
}
2019-02-26 10:58:33 -07:00
finally
{
2019-04-02 13:15:18 -07:00
WriteLock.Release();
2019-02-26 10:58:33 -07:00
}
2019-04-02 13:15:18 -07:00
WriteLock.Dispose();
}
2016-11-18 01:39:20 -07:00
2019-04-02 13:15:18 -07:00
WriteConnection = null;
WriteLock = null;
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
}
}
2016-11-18 02:28:39 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// The disk synchronization mode, controls how aggressively SQLite will write data
/// all the way out to physical storage.
/// </summary>
2019-04-03 08:34:54 -07:00
public enum SynchronousMode
{
2019-07-01 09:24:35 -07:00
/// <summary>
/// SQLite continues without syncing as soon as it has handed data off to the operating system
/// </summary>
2019-04-03 08:34:54 -07:00
Off = 0,
2016-11-18 02:28:39 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// SQLite database engine will still sync at the most critical moments
/// </summary>
2019-04-03 08:34:54 -07:00
Normal = 1,
2019-07-01 09:24:35 -07:00
/// <summary>
/// SQLite database engine will use the xSync method of the VFS
/// to ensure that all content is safely written to the disk surface prior to continuing.
/// </summary>
2019-04-03 08:34:54 -07:00
Full = 2,
2019-07-01 09:24:35 -07:00
/// <summary>
/// EXTRA synchronous is like FULL with the addition that the directory containing a rollback journal
/// is synced after that journal is unlinked to commit a transaction in DELETE mode.
/// </summary>
2019-04-03 08:34:54 -07:00
Extra = 3
2016-11-18 01:39:20 -07:00
}
2016-11-19 01:40:13 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Storage mode used by temporary database files.
/// </summary>
2019-04-03 08:34:54 -07:00
public enum TempStoreMode
2016-11-19 01:40:13 -07:00
{
2019-07-01 09:24:35 -07:00
/// <summary>
/// The compile-time C preprocessor macro SQLITE_TEMP_STORE
/// is used to determine where temporary tables and indices are stored.
/// </summary>
2019-04-03 08:34:54 -07:00
Default = 0,
2016-11-19 01:40:13 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Temporary tables and indices are stored in a file.
/// </summary>
2019-04-03 08:34:54 -07:00
File = 1,
2019-02-08 14:59:28 -07:00
2019-07-01 09:24:35 -07:00
/// <summary>
/// Temporary tables and indices are kept in as if they were pure in-memory databases memory.
/// </summary>
2019-04-03 08:34:54 -07:00
Memory = 2
2016-12-10 22:12:00 -07:00
}
2016-11-18 01:39:20 -07:00
}