mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-07-08 23:00:51 +02:00
Simplify db code
This commit is contained in:
parent
b3b08fecb2
commit
cec22ad10d
|
@ -57,7 +57,7 @@ namespace Emby.Server.Implementations.Activity
|
|||
}
|
||||
}
|
||||
|
||||
private void TryMigrate(ManagedConnection connection)
|
||||
private void TryMigrate(SQLiteDatabaseConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -85,7 +85,6 @@ namespace Emby.Server.Implementations.Activity
|
|||
throw new ArgumentNullException(nameof(entry));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -124,7 +123,6 @@ namespace Emby.Server.Implementations.Activity
|
|||
throw new ArgumentNullException(nameof(entry));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -159,7 +157,6 @@ namespace Emby.Server.Implementations.Activity
|
|||
|
||||
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit)
|
||||
{
|
||||
using (WriteLock.Read())
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var commandText = BaseActivitySelectText;
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SQLitePCL;
|
||||
using SQLitePCL.pretty;
|
||||
|
@ -12,15 +13,12 @@ namespace Emby.Server.Implementations.Data
|
|||
public abstract class BaseSqliteRepository : IDisposable
|
||||
{
|
||||
protected string DbFilePath { get; set; }
|
||||
protected ReaderWriterLockSlim WriteLock;
|
||||
|
||||
protected ILogger Logger { get; private set; }
|
||||
|
||||
protected BaseSqliteRepository(ILogger logger)
|
||||
{
|
||||
Logger = logger;
|
||||
|
||||
WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
|
||||
}
|
||||
|
||||
protected TransactionMode TransactionMode => TransactionMode.Deferred;
|
||||
|
@ -31,42 +29,20 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
static BaseSqliteRepository()
|
||||
{
|
||||
SQLite3.EnableSharedCache = false;
|
||||
|
||||
int rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MEMSTATUS, 0);
|
||||
//CheckOk(rc);
|
||||
|
||||
rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD, 1);
|
||||
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SINGLETHREAD, 1);
|
||||
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SERIALIZED, 1);
|
||||
//CheckOk(rc);
|
||||
|
||||
rc = raw.sqlite3_enable_shared_cache(1);
|
||||
|
||||
ThreadSafeMode = raw.sqlite3_threadsafe();
|
||||
}
|
||||
|
||||
private static bool _versionLogged;
|
||||
|
||||
private string _defaultWal;
|
||||
protected ManagedConnection _connection;
|
||||
|
||||
protected virtual bool EnableSingleConnection => true;
|
||||
|
||||
protected ManagedConnection CreateConnection(bool isReadOnly = false)
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
return _connection;
|
||||
}
|
||||
|
||||
lock (WriteLock)
|
||||
protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false)
|
||||
{
|
||||
if (!_versionLogged)
|
||||
{
|
||||
_versionLogged = true;
|
||||
Logger.LogInformation("Sqlite version: " + SQLite3.Version);
|
||||
Logger.LogInformation("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray()));
|
||||
Logger.LogInformation("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions));
|
||||
}
|
||||
|
||||
ConnectionFlags connectionFlags;
|
||||
|
@ -74,9 +50,8 @@ namespace Emby.Server.Implementations.Data
|
|||
if (isReadOnly)
|
||||
{
|
||||
//Logger.LogInformation("Opening read connection");
|
||||
//connectionFlags = ConnectionFlags.ReadOnly;
|
||||
connectionFlags = ConnectionFlags.Create;
|
||||
connectionFlags |= ConnectionFlags.ReadWrite;
|
||||
//connectionFlags = ConnectionFlags.Create;
|
||||
connectionFlags = ConnectionFlags.ReadOnly;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -85,16 +60,8 @@ namespace Emby.Server.Implementations.Data
|
|||
connectionFlags |= ConnectionFlags.ReadWrite;
|
||||
}
|
||||
|
||||
if (EnableSingleConnection)
|
||||
{
|
||||
connectionFlags |= ConnectionFlags.PrivateCache;
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionFlags |= ConnectionFlags.SharedCached;
|
||||
}
|
||||
|
||||
connectionFlags |= ConnectionFlags.NoMutex;
|
||||
connectionFlags |= ConnectionFlags.FullMutex;
|
||||
|
||||
var db = SQLite3.Open(DbFilePath, connectionFlags, null);
|
||||
|
||||
|
@ -111,7 +78,7 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
//"PRAGMA cache size=-10000"
|
||||
//"PRAGMA read_uncommitted = true",
|
||||
"PRAGMA synchronous=Normal"
|
||||
//"PRAGMA synchronous=Normal"
|
||||
};
|
||||
|
||||
if (CacheSize.HasValue)
|
||||
|
@ -143,18 +110,15 @@ namespace Emby.Server.Implementations.Data
|
|||
throw;
|
||||
}
|
||||
|
||||
_connection = new ManagedConnection(db, false);
|
||||
|
||||
return _connection;
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
public IStatement PrepareStatement(ManagedConnection connection, string sql)
|
||||
public IStatement PrepareStatement(SQLiteDatabaseConnection connection, string sql)
|
||||
{
|
||||
return connection.PrepareStatement(sql);
|
||||
}
|
||||
|
||||
public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
|
||||
public IStatement PrepareStatementSafe(SQLiteDatabaseConnection connection, string sql)
|
||||
{
|
||||
return connection.PrepareStatement(sql);
|
||||
}
|
||||
|
@ -179,7 +143,7 @@ namespace Emby.Server.Implementations.Data
|
|||
return sql.Select(connection.PrepareStatement).ToList();
|
||||
}
|
||||
|
||||
protected bool TableExists(ManagedConnection connection, string name)
|
||||
protected bool TableExists(SQLiteDatabaseConnection connection, string name)
|
||||
{
|
||||
return connection.RunInTransaction(db =>
|
||||
{
|
||||
|
@ -199,7 +163,7 @@ namespace Emby.Server.Implementations.Data
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
|
||||
protected void RunDefaultInitialization(ManagedConnection db)
|
||||
protected void RunDefaultInitialization(SQLiteDatabaseConnection db)
|
||||
{
|
||||
var queries = new List<string>
|
||||
{
|
||||
|
@ -243,7 +207,7 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
|
@ -255,42 +219,13 @@ namespace Emby.Server.Implementations.Data
|
|||
/// <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)
|
||||
{
|
||||
if (dispose)
|
||||
if (_disposed)
|
||||
{
|
||||
DisposeConnection();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private void DisposeConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
lock (_disposeLock)
|
||||
{
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
using (_connection)
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
_connection = null;
|
||||
}
|
||||
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error disposing database");
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void CloseConnection()
|
||||
{
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
protected List<string> GetColumnNames(IDatabaseConnection connection, string table)
|
||||
|
@ -320,60 +255,4 @@ namespace Emby.Server.Implementations.Data
|
|||
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReaderWriterLockSlimExtensions
|
||||
{
|
||||
private sealed class ReadLockToken : IDisposable
|
||||
{
|
||||
private ReaderWriterLockSlim _sync;
|
||||
public ReadLockToken(ReaderWriterLockSlim sync)
|
||||
{
|
||||
_sync = sync;
|
||||
sync.EnterReadLock();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_sync != null)
|
||||
{
|
||||
_sync.ExitReadLock();
|
||||
_sync = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
private sealed class WriteLockToken : IDisposable
|
||||
{
|
||||
private ReaderWriterLockSlim _sync;
|
||||
public WriteLockToken(ReaderWriterLockSlim sync)
|
||||
{
|
||||
_sync = sync;
|
||||
sync.EnterWriteLock();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_sync != null)
|
||||
{
|
||||
_sync.ExitWriteLock();
|
||||
_sync = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable Read(this ReaderWriterLockSlim obj)
|
||||
{
|
||||
//if (BaseSqliteRepository.ThreadSafeMode > 0)
|
||||
//{
|
||||
// return new DummyToken();
|
||||
//}
|
||||
return new WriteLockToken(obj);
|
||||
}
|
||||
|
||||
public static IDisposable Write(this ReaderWriterLockSlim obj)
|
||||
{
|
||||
//if (BaseSqliteRepository.ThreadSafeMode > 0)
|
||||
//{
|
||||
// return new DummyToken();
|
||||
//}
|
||||
return new WriteLockToken(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SQLitePCL.pretty;
|
||||
|
||||
namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
public class ManagedConnection : IDisposable
|
||||
{
|
||||
private SQLiteDatabaseConnection db;
|
||||
private readonly bool _closeOnDispose;
|
||||
|
||||
public ManagedConnection(SQLiteDatabaseConnection db, bool closeOnDispose)
|
||||
{
|
||||
this.db = db;
|
||||
_closeOnDispose = closeOnDispose;
|
||||
}
|
||||
|
||||
public IStatement PrepareStatement(string sql)
|
||||
{
|
||||
return db.PrepareStatement(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<IStatement> PrepareAll(string sql)
|
||||
{
|
||||
return db.PrepareAll(sql);
|
||||
}
|
||||
|
||||
public void ExecuteAll(string sql)
|
||||
{
|
||||
db.ExecuteAll(sql);
|
||||
}
|
||||
|
||||
public void Execute(string sql, params object[] values)
|
||||
{
|
||||
db.Execute(sql, values);
|
||||
}
|
||||
|
||||
public void RunQueries(string[] sql)
|
||||
{
|
||||
db.RunQueries(sql);
|
||||
}
|
||||
|
||||
public void RunInTransaction(Action<IDatabaseConnection> action, TransactionMode mode)
|
||||
{
|
||||
db.RunInTransaction(action, mode);
|
||||
}
|
||||
|
||||
public T RunInTransaction<T>(Func<IDatabaseConnection, T> action, TransactionMode mode)
|
||||
{
|
||||
return db.RunInTransaction(action, mode);
|
||||
}
|
||||
|
||||
public IEnumerable<IReadOnlyList<IResultSetValue>> Query(string sql)
|
||||
{
|
||||
return db.Query(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<IReadOnlyList<IResultSetValue>> Query(string sql, params object[] values)
|
||||
{
|
||||
return db.Query(sql, values);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
using (db)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_closeOnDispose)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -98,8 +98,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -108,7 +106,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
|
||||
{
|
||||
|
@ -142,8 +139,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -155,7 +150,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
|
@ -174,8 +168,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
var guidId = displayPreferencesId.GetMD5();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
|
||||
|
@ -196,7 +188,6 @@ namespace Emby.Server.Implementations.Data
|
|||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all display preferences for the given user.
|
||||
|
@ -208,8 +199,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
var list = new List<DisplayPreferences>();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
|
||||
|
@ -222,7 +211,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
|
|
@ -141,7 +141,7 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
|
||||
public static void Attach(ManagedConnection db, string path, string alias)
|
||||
public static void Attach(SQLiteDatabaseConnection db, string path, string alias)
|
||||
{
|
||||
var commandText = string.Format("attach @path as {0};", alias);
|
||||
|
||||
|
|
|
@ -319,7 +319,7 @@ namespace Emby.Server.Implementations.Data
|
|||
connection.RunQueries(postQueries);
|
||||
}
|
||||
|
||||
userDataRepo.Initialize(WriteLock, _connection, userManager);
|
||||
userDataRepo.Initialize(userManager);
|
||||
}
|
||||
|
||||
private static readonly string[] _retriveItemColumns =
|
||||
|
@ -551,8 +551,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -567,7 +565,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the items.
|
||||
|
@ -605,18 +602,14 @@ namespace Emby.Server.Implementations.Data
|
|||
tuples.Add((item, ancestorIds, topParent, userdataKey, inheritedTags));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
{
|
||||
SaveItemsInTranscation(db, tuples);
|
||||
|
||||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveItemsInTranscation(IDatabaseConnection db, IEnumerable<(BaseItem, List<Guid>, BaseItem, string, List<string>)> tuples)
|
||||
{
|
||||
|
@ -1193,8 +1186,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
|
||||
|
@ -1206,11 +1197,10 @@ namespace Emby.Server.Implementations.Data
|
|||
return GetItem(row, new InternalItemsQuery());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TypeRequiresDeserialization(Type type)
|
||||
{
|
||||
|
@ -1909,8 +1899,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<ChapterInfo>();
|
||||
|
@ -1928,7 +1916,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single chapter for an item
|
||||
|
@ -1941,8 +1928,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
|
||||
|
@ -1956,7 +1941,7 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -2012,8 +1997,6 @@ namespace Emby.Server.Implementations.Data
|
|||
throw new ArgumentNullException(nameof(chapters));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -2028,7 +2011,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InsertChapters(byte[] idBlob, List<ChapterInfo> chapters, IDatabaseConnection db)
|
||||
{
|
||||
|
@ -2551,8 +2533,6 @@ namespace Emby.Server.Implementations.Data
|
|||
commandText += " where " + string.Join(" AND ", whereClauses);
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = PrepareStatementSafe(connection, commandText))
|
||||
|
@ -2573,8 +2553,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public List<BaseItem> GetItemList(InternalItemsQuery query)
|
||||
|
@ -2624,8 +2602,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<BaseItem>();
|
||||
|
@ -2686,7 +2662,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string FixUnicodeChars(string buffer)
|
||||
{
|
||||
|
@ -2845,8 +2820,6 @@ namespace Emby.Server.Implementations.Data
|
|||
statementTexts.Add(commandText);
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
return connection.RunInTransaction(db =>
|
||||
|
@ -2915,7 +2888,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string GetOrderByText(InternalItemsQuery query)
|
||||
{
|
||||
|
@ -3080,8 +3052,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<Guid>();
|
||||
|
@ -3110,7 +3080,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Tuple<Guid, string>> GetItemIdsWithPath(InternalItemsQuery query)
|
||||
{
|
||||
|
@ -3149,8 +3118,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
var list = new List<Tuple<Guid, string>>();
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
|
@ -3182,7 +3149,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public QueryResult<Guid> GetItemIds(InternalItemsQuery query)
|
||||
{
|
||||
|
@ -3265,8 +3231,6 @@ namespace Emby.Server.Implementations.Data
|
|||
statementTexts.Add(commandText);
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
return connection.RunInTransaction(db =>
|
||||
|
@ -3324,7 +3288,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAlphaNumeric(string str)
|
||||
{
|
||||
|
@ -4898,8 +4861,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
|
||||
private void UpdateInheritedTags(CancellationToken cancellationToken)
|
||||
{
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
|
@ -4921,7 +4882,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<string, string[]> GetTypeMapDictionary()
|
||||
{
|
||||
|
@ -4965,8 +4925,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -4993,7 +4951,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteWithSingleParam(IDatabaseConnection db, string query, byte[] value)
|
||||
{
|
||||
|
@ -5025,8 +4982,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
commandText += " order by ListOrder";
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<string>();
|
||||
|
@ -5043,7 +4998,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
|
||||
{
|
||||
|
@ -5065,8 +5019,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
commandText += " order by ListOrder";
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<PersonInfo>();
|
||||
|
@ -5085,7 +5037,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement)
|
||||
{
|
||||
|
@ -5294,8 +5245,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
commandText += " Group By CleanValue";
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<string>();
|
||||
|
@ -5316,7 +5265,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private QueryResult<(BaseItem, ItemCounts)> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
|
||||
{
|
||||
|
@ -5483,8 +5431,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
statementTexts.Add(countText);
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
return connection.RunInTransaction(db =>
|
||||
|
@ -5578,7 +5524,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ItemCounts GetItemCounts(IReadOnlyList<IResultSetValue> reader, int countStartColumn, string[] typesToCount)
|
||||
{
|
||||
|
@ -5753,8 +5698,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
CheckDisposed();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -5767,8 +5710,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
InsertPeople(itemIdBlob, people, db);
|
||||
|
||||
}, TransactionMode);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5874,8 +5815,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
cmdText += " order by StreamIndex ASC";
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var list = new List<MediaStream>();
|
||||
|
@ -5903,7 +5842,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveMediaStreams(Guid id, List<MediaStream> streams, CancellationToken cancellationToken)
|
||||
{
|
||||
|
@ -5921,8 +5859,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -5937,7 +5873,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InsertMediaStreams(byte[] idBlob, List<MediaStream> streams, IDatabaseConnection db)
|
||||
{
|
||||
|
|
|
@ -7,7 +7,6 @@ using MediaBrowser.Common.Configuration;
|
|||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Model.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SQLitePCL.pretty;
|
||||
|
||||
|
@ -33,13 +32,8 @@ namespace Emby.Server.Implementations.Data
|
|||
/// Opens the connection to the database
|
||||
/// </summary>
|
||||
/// <returns>Task.</returns>
|
||||
public void Initialize(ReaderWriterLockSlim writeLock, ManagedConnection managedConnection, IUserManager userManager)
|
||||
public void Initialize(IUserManager userManager)
|
||||
{
|
||||
_connection = managedConnection;
|
||||
|
||||
WriteLock.Dispose();
|
||||
WriteLock = writeLock;
|
||||
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
var userDatasTableExists = TableExists(connection, "UserDatas");
|
||||
|
@ -178,8 +172,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -188,7 +180,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveUserData(IDatabaseConnection db, long internalUserId, string key, UserItemData userData)
|
||||
{
|
||||
|
@ -249,8 +240,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -262,7 +251,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user data.
|
||||
|
@ -281,13 +269,12 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
throw new ArgumentNullException(nameof(internalUserId));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where key =@Key and userId=@UserId"))
|
||||
|
@ -304,7 +291,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UserItemData GetUserData(long internalUserId, List<string> keys)
|
||||
{
|
||||
|
@ -335,8 +321,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
var list = new List<UserItemData>();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where userId=@UserId"))
|
||||
|
@ -349,7 +333,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
@ -397,10 +380,5 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
// handled by library database
|
||||
}
|
||||
|
||||
protected override void CloseConnection()
|
||||
{
|
||||
// handled by library database
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
|
||||
private void TryMigrateToLocalUsersTable(ManagedConnection connection)
|
||||
private void TryMigrateToLocalUsersTable(SQLiteDatabaseConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -119,8 +119,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
var serialized = _jsonSerializer.SerializeToBytes(user);
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -133,7 +131,7 @@ namespace Emby.Server.Implementations.Data
|
|||
statement.MoveNext();
|
||||
}
|
||||
|
||||
var createdUser = GetUser(user.Id, false);
|
||||
var createdUser = GetUser(user.Id, connection);
|
||||
|
||||
if (createdUser == null)
|
||||
{
|
||||
|
@ -145,7 +143,6 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateUser(User user)
|
||||
{
|
||||
|
@ -156,8 +153,6 @@ namespace Emby.Server.Implementations.Data
|
|||
|
||||
var serialized = _jsonSerializer.SerializeToBytes(user);
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -172,13 +167,8 @@ namespace Emby.Server.Implementations.Data
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private User GetUser(Guid guid, bool openLock)
|
||||
{
|
||||
using (openLock ? WriteLock.Read() : null)
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
private User GetUser(Guid guid, SQLiteDatabaseConnection connection)
|
||||
{
|
||||
using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
|
||||
{
|
||||
|
@ -189,8 +179,6 @@ namespace Emby.Server.Implementations.Data
|
|||
return GetUser(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -218,8 +206,6 @@ namespace Emby.Server.Implementations.Data
|
|||
{
|
||||
var list = new List<User>();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
|
||||
|
@ -227,7 +213,6 @@ namespace Emby.Server.Implementations.Data
|
|||
list.Add(GetUser(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
@ -245,8 +230,6 @@ namespace Emby.Server.Implementations.Data
|
|||
throw new ArgumentNullException(nameof(user));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -260,5 +243,4 @@ namespace Emby.Server.Implementations.Data
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace Emby.Server.Implementations.Security
|
|||
}
|
||||
}
|
||||
|
||||
private void TryMigrate(ManagedConnection connection, bool tableNewlyCreated)
|
||||
private void TryMigrate(SQLiteDatabaseConnection connection, bool tableNewlyCreated)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -87,8 +87,6 @@ namespace Emby.Server.Implementations.Security
|
|||
throw new ArgumentNullException(nameof(info));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -113,7 +111,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(AuthenticationInfo info)
|
||||
{
|
||||
|
@ -122,8 +119,6 @@ namespace Emby.Server.Implementations.Security
|
|||
throw new ArgumentNullException(nameof(info));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -148,7 +143,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(AuthenticationInfo info)
|
||||
{
|
||||
|
@ -157,8 +151,6 @@ namespace Emby.Server.Implementations.Security
|
|||
throw new ArgumentNullException(nameof(info));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -172,7 +164,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}, TransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const string BaseSelectText = "select Tokens.Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, UserName, DateCreated, DateLastActivity, Devices.CustomName from Tokens left join Devices on Tokens.DeviceId=Devices.Id";
|
||||
|
||||
|
@ -257,8 +248,6 @@ namespace Emby.Server.Implementations.Security
|
|||
|
||||
var list = new List<AuthenticationInfo>();
|
||||
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
return connection.RunInTransaction(db =>
|
||||
|
@ -297,7 +286,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AuthenticationInfo Get(IReadOnlyList<IResultSetValue> reader)
|
||||
{
|
||||
|
@ -357,8 +345,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}
|
||||
|
||||
public DeviceOptions GetDeviceOptions(string deviceId)
|
||||
{
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
|
@ -384,7 +370,6 @@ namespace Emby.Server.Implementations.Security
|
|||
}, ReadTransactionMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
||||
{
|
||||
|
@ -393,8 +378,6 @@ namespace Emby.Server.Implementations.Security
|
|||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
connection.RunInTransaction(db =>
|
||||
|
@ -419,5 +402,4 @@ namespace Emby.Server.Implementations.Security
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue