limit knowledge of sqlite
This commit is contained in:
parent
373bdf4825
commit
cdfb009df8
|
@ -5,7 +5,6 @@ using MediaBrowser.Model.Logging;
|
|||
using MediaBrowser.Model.Serialization;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -17,7 +16,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// </summary>
|
||||
public class SqliteDisplayPreferencesRepository : IDisplayPreferencesRepository
|
||||
{
|
||||
private SQLiteConnection _connection;
|
||||
private IDbConnection _connection;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
@ -124,7 +123,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
|
||||
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
SQLiteTransaction transaction = null;
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -133,14 +132,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
using (var cmd = _connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "replace into userdisplaypreferences (id, userid, client, data) values (@1, @2, @3, @4)";
|
||||
cmd.AddParam("@1", displayPreferences.Id);
|
||||
cmd.AddParam("@2", userId);
|
||||
cmd.AddParam("@3", client);
|
||||
cmd.AddParam("@4", serialized);
|
||||
|
||||
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = displayPreferences.Id;
|
||||
cmd.Parameters.Add(cmd, "@2", DbType.Guid).Value = userId;
|
||||
cmd.Parameters.Add(cmd, "@3", DbType.String).Value = client;
|
||||
cmd.Parameters.Add(cmd, "@4", DbType.Binary).Value = serialized;
|
||||
|
||||
cmd.Transaction = transaction;
|
||||
|
||||
await cmd.ExecuteNonQueryAsync(cancellationToken);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
|
@ -194,14 +194,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
var cmd = _connection.CreateCommand();
|
||||
cmd.CommandText = "select data from userdisplaypreferences where id = @id and userId=@userId and client=@client";
|
||||
|
||||
var idParam = cmd.Parameters.Add("@id", DbType.Guid);
|
||||
idParam.Value = displayPreferencesId;
|
||||
|
||||
var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid);
|
||||
userIdParam.Value = userId;
|
||||
|
||||
var clientParam = cmd.Parameters.Add("@client", DbType.String);
|
||||
clientParam.Value = client;
|
||||
cmd.Parameters.Add(cmd, "@id", DbType.Guid).Value = displayPreferencesId;
|
||||
cmd.Parameters.Add(cmd, "@userId", DbType.Guid).Value = userId;
|
||||
cmd.Parameters.Add(cmd, "@client", DbType.String).Value = client;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
||||
{
|
||||
|
|
|
@ -12,40 +12,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// </summary>
|
||||
static class SqliteExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the param.
|
||||
/// </summary>
|
||||
/// <param name="cmd">The CMD.</param>
|
||||
/// <param name="param">The param.</param>
|
||||
/// <returns>SQLiteParameter.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public static SQLiteParameter AddParam(this SQLiteCommand cmd, string param)
|
||||
{
|
||||
if (string.IsNullOrEmpty(param))
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
var sqliteParam = new SQLiteParameter(param);
|
||||
cmd.Parameters.Add(sqliteParam);
|
||||
return sqliteParam;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the param.
|
||||
/// </summary>
|
||||
/// <param name="cmd">The CMD.</param>
|
||||
/// <param name="param">The param.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>SQLiteParameter.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public static SQLiteParameter AddParam(this SQLiteCommand cmd, string param, object data)
|
||||
{
|
||||
var sqliteParam = AddParam(cmd, param);
|
||||
sqliteParam.Value = data;
|
||||
return sqliteParam;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified conn is open.
|
||||
/// </summary>
|
||||
|
@ -160,7 +126,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// <param name="dbPath">The db path.</param>
|
||||
/// <returns>Task{IDbConnection}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">dbPath</exception>
|
||||
public static async Task<SQLiteConnection> ConnectToDb(string dbPath)
|
||||
public static async Task<IDbConnection> ConnectToDb(string dbPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dbPath))
|
||||
{
|
||||
|
|
|
@ -7,7 +7,6 @@ using MediaBrowser.Model.Serialization;
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
@ -20,12 +19,12 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// </summary>
|
||||
public class SqliteItemRepository : IItemRepository
|
||||
{
|
||||
private SQLiteConnection _connection;
|
||||
private IDbConnection _connection;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly TypeMapper _typeMapper = new TypeMapper();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the repository
|
||||
/// </summary>
|
||||
|
@ -52,15 +51,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// <summary>
|
||||
/// The _save item command
|
||||
/// </summary>
|
||||
private SQLiteCommand _saveItemCommand;
|
||||
private IDbCommand _saveItemCommand;
|
||||
|
||||
private readonly string _criticReviewsPath;
|
||||
|
||||
private SqliteChapterRepository _chapterRepository;
|
||||
|
||||
private SQLiteCommand _deleteChildrenCommand;
|
||||
private SQLiteCommand _saveChildrenCommand;
|
||||
|
||||
private IDbCommand _deleteChildrenCommand;
|
||||
private IDbCommand _saveChildrenCommand;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
|
||||
/// </summary>
|
||||
|
@ -104,7 +103,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
public async Task Initialize()
|
||||
{
|
||||
var dbFile = Path.Combine(_appPaths.DataPath, "library.db");
|
||||
|
||||
|
||||
_connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false);
|
||||
|
||||
string[] queries = {
|
||||
|
@ -136,29 +135,20 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
/// </summary>
|
||||
private void PrepareStatements()
|
||||
{
|
||||
_saveItemCommand = new SQLiteCommand
|
||||
{
|
||||
CommandText = "replace into TypedBaseItems (guid, type, data) values (@1, @2, @3)"
|
||||
};
|
||||
_saveItemCommand = _connection.CreateCommand();
|
||||
_saveItemCommand.CommandText = "replace into TypedBaseItems (guid, type, data) values (@1, @2, @3)";
|
||||
_saveItemCommand.Parameters.Add(_saveItemCommand, "@1");
|
||||
_saveItemCommand.Parameters.Add(_saveItemCommand, "@2");
|
||||
_saveItemCommand.Parameters.Add(_saveItemCommand, "@3");
|
||||
|
||||
_saveItemCommand.Parameters.Add(new SQLiteParameter("@1"));
|
||||
_saveItemCommand.Parameters.Add(new SQLiteParameter("@2"));
|
||||
_saveItemCommand.Parameters.Add(new SQLiteParameter("@3"));
|
||||
_deleteChildrenCommand = _connection.CreateCommand();
|
||||
_deleteChildrenCommand.CommandText = "delete from ChildrenIds where ParentId=@ParentId";
|
||||
_deleteChildrenCommand.Parameters.Add(_deleteChildrenCommand, "@ParentId");
|
||||
|
||||
_deleteChildrenCommand = new SQLiteCommand
|
||||
{
|
||||
CommandText = "delete from ChildrenIds where ParentId=@ParentId"
|
||||
};
|
||||
|
||||
_deleteChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
|
||||
|
||||
_saveChildrenCommand = new SQLiteCommand
|
||||
{
|
||||
CommandText = "replace into ChildrenIds (ParentId, ItemId) values (@ParentId, @ItemId)"
|
||||
};
|
||||
|
||||
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
|
||||
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ItemId"));
|
||||
_saveChildrenCommand = _connection.CreateCommand();
|
||||
_saveChildrenCommand.CommandText = "replace into ChildrenIds (ParentId, ItemId) values (@ParentId, @ItemId)";
|
||||
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ParentId");
|
||||
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ItemId");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -205,7 +195,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
|
||||
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
SQLiteTransaction transaction = null;
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -215,13 +205,13 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
_saveItemCommand.Parameters[0].Value = item.Id;
|
||||
_saveItemCommand.Parameters[1].Value = item.GetType().FullName;
|
||||
_saveItemCommand.Parameters[2].Value = _jsonSerializer.SerializeToBytes(item);
|
||||
_saveItemCommand.GetParameter(0).Value = item.Id;
|
||||
_saveItemCommand.GetParameter(1).Value = item.GetType().FullName;
|
||||
_saveItemCommand.GetParameter(2).Value = _jsonSerializer.SerializeToBytes(item);
|
||||
|
||||
_saveItemCommand.Transaction = transaction;
|
||||
|
||||
await _saveItemCommand.ExecuteNonQueryAsync(cancellationToken);
|
||||
_saveItemCommand.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
|
@ -274,8 +264,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
using (var cmd = _connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "select type,data from TypedBaseItems where guid = @guid";
|
||||
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
|
||||
guidParam.Value = id;
|
||||
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = id;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
||||
{
|
||||
|
@ -446,7 +435,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
{
|
||||
cmd.CommandText = "select ItemId from ChildrenIds where ParentId = @ParentId";
|
||||
|
||||
cmd.Parameters.Add("@ParentId", DbType.Guid).Value = parentId;
|
||||
cmd.Parameters.Add(cmd, "@ParentId", DbType.Guid).Value = parentId;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
{
|
||||
|
@ -479,27 +468,28 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
|||
|
||||
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
SQLiteTransaction transaction = null;
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
transaction = _connection.BeginTransaction();
|
||||
|
||||
// First delete
|
||||
_deleteChildrenCommand.Parameters[0].Value = parentId;
|
||||
_deleteChildrenCommand.GetParameter(0).Value = parentId;
|
||||
_deleteChildrenCommand.Transaction = transaction;
|
||||
await _deleteChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
|
||||
|
||||
_deleteChildrenCommand.ExecuteNonQuery();
|
||||
|
||||
foreach (var id in children)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
_saveChildrenCommand.Parameters[0].Value = parentId;
|
||||
_saveChildrenCommand.Parameters[1].Value = id;
|
||||
_saveChildrenCommand.GetParameter(0).Value = parentId;
|
||||
_saveChildrenCommand.GetParameter(1).Value = id;
|
||||
|
||||
_saveChildrenCommand.Transaction = transaction;
|
||||
|
||||
await _saveChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
|
||||
_saveChildrenCommand.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
|
|
Loading…
Reference in New Issue
Block a user