aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations')
-rw-r--r--MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj6
-rw-r--r--MediaBrowser.Server.Implementations/Persistence/SqliteChapterRepository.cs9
-rw-r--r--MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs129
-rw-r--r--MediaBrowser.Server.Implementations/packages.config1
4 files changed, 138 insertions, 7 deletions
diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
index 34930b34e..fde1b77b3 100644
--- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
+++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
@@ -64,6 +64,12 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Common.3.9.54\lib\net35\ServiceStack.Interfaces.dll</HintPath>
</Reference>
+ <Reference Include="ServiceStack.OrmLite">
+ <HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.54\lib\net40\ServiceStack.OrmLite.dll</HintPath>
+ </Reference>
+ <Reference Include="ServiceStack.OrmLite.SqliteNET">
+ <HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.54\lib\net40\ServiceStack.OrmLite.SqliteNET.dll</HintPath>
+ </Reference>
<Reference Include="ServiceStack.OrmLite.SqlServer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.OrmLite.SqlServer.3.9.43\lib\ServiceStack.OrmLite.SqlServer.dll</HintPath>
diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteChapterRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteChapterRepository.cs
index dd6343a67..cac612fc8 100644
--- a/MediaBrowser.Server.Implementations/Persistence/SqliteChapterRepository.cs
+++ b/MediaBrowser.Server.Implementations/Persistence/SqliteChapterRepository.cs
@@ -27,16 +27,13 @@ namespace MediaBrowser.Server.Implementations.Persistence
private SQLiteCommand _saveChapterCommand;
/// <summary>
- /// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
+ /// Initializes a new instance of the <see cref="SqliteItemRepository" /> class.
/// </summary>
/// <param name="appPaths">The app paths.</param>
- /// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logManager">The log manager.</param>
- /// <exception cref="System.ArgumentNullException">
- /// appPaths
+ /// <exception cref="System.ArgumentNullException">appPaths
/// or
- /// jsonSerializer
- /// </exception>
+ /// jsonSerializer</exception>
public SqliteChapterRepository(IApplicationPaths appPaths, ILogManager logManager)
{
if (appPaths == null)
diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
index b3251ddb9..f0597e4cc 100644
--- a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
+++ b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
@@ -56,6 +56,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
private SqliteChapterRepository _chapterRepository;
+ private SQLiteCommand _deleteChildrenCommand;
+ private SQLiteCommand _saveChildrenCommand;
+
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
@@ -95,7 +98,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 = {
@@ -103,6 +106,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
"create table if not exists baseitems (guid GUID primary key, data BLOB)",
"create index if not exists idx_baseitems on baseitems(guid)",
+ "create table if not exists ChildDefinitions (ParentId GUID, ItemId GUID, Type TEXT, PRIMARY KEY (ParentId, ItemId))",
+ "create index if not exists idx_baseitems on baseitems(ParentId,ItemId)",
+
//pragmas
"pragma temp_store = memory"
};
@@ -131,6 +137,22 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.Parameters.Add(new SQLiteParameter("@1"));
_saveItemCommand.Parameters.Add(new SQLiteParameter("@2"));
+
+ _deleteChildrenCommand = new SQLiteCommand
+ {
+ CommandText = "delete from ChildDefinitions where ParentId=@ParentId"
+ };
+
+ _deleteChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
+
+ _saveChildrenCommand = new SQLiteCommand
+ {
+ CommandText = "replace into ChildDefinitions (ParentId, ItemId, Type) values (@ParentId, @ItemId, @Type)"
+ };
+
+ _saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
+ _saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ItemId"));
+ _saveChildrenCommand.Parameters.Add(new SQLiteParameter("@Type"));
}
/// <summary>
@@ -401,5 +423,110 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
}
}
+
+ public IEnumerable<ChildDefinition> GetChildren(Guid parentId)
+ {
+ if (parentId == Guid.Empty)
+ {
+ throw new ArgumentNullException("parentId");
+ }
+
+ using (var cmd = _connection.CreateCommand())
+ {
+ cmd.CommandText = "select ItemId,Type from ChildDefinitions where ParentId = @ParentId";
+
+ cmd.Parameters.Add("@ParentId", DbType.Guid).Value = parentId;
+
+ using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
+ {
+ while (reader.Read())
+ {
+ yield return new ChildDefinition
+ {
+ ItemId = reader.GetGuid(0),
+ Type = reader.GetString(1)
+ };
+ }
+ }
+ }
+ }
+
+ public async Task SaveChildren(Guid parentId, IEnumerable<ChildDefinition> children, CancellationToken cancellationToken)
+ {
+ if (parentId == Guid.Empty)
+ {
+ throw new ArgumentNullException("parentId");
+ }
+
+ if (children == null)
+ {
+ throw new ArgumentNullException("children");
+ }
+
+ if (cancellationToken == null)
+ {
+ throw new ArgumentNullException("cancellationToken");
+ }
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ SQLiteTransaction transaction = null;
+
+ try
+ {
+ transaction = _connection.BeginTransaction();
+
+ // First delete
+ _deleteChildrenCommand.Parameters[0].Value = parentId;
+ _deleteChildrenCommand.Transaction = transaction;
+ await _deleteChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
+
+ foreach (var chapter in children)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ _saveChildrenCommand.Parameters[0].Value = parentId;
+ _saveChildrenCommand.Parameters[1].Value = chapter.ItemId;
+ _saveChildrenCommand.Parameters[2].Value = chapter.Type;
+
+ _saveChildrenCommand.Transaction = transaction;
+
+ await _saveChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
+ }
+
+ transaction.Commit();
+ }
+ catch (OperationCanceledException)
+ {
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ catch (Exception e)
+ {
+ _logger.ErrorException("Failed to save children:", e);
+
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ finally
+ {
+ if (transaction != null)
+ {
+ transaction.Dispose();
+ }
+
+ _writeLock.Release();
+ }
+ }
}
} \ No newline at end of file
diff --git a/MediaBrowser.Server.Implementations/packages.config b/MediaBrowser.Server.Implementations/packages.config
index b3294492f..e13d18747 100644
--- a/MediaBrowser.Server.Implementations/packages.config
+++ b/MediaBrowser.Server.Implementations/packages.config
@@ -10,6 +10,7 @@
<package id="ServiceStack" version="3.9.54" targetFramework="net45" />
<package id="ServiceStack.Api.Swagger" version="3.9.54" targetFramework="net45" />
<package id="ServiceStack.Common" version="3.9.54" targetFramework="net45" />
+ <package id="ServiceStack.OrmLite.Sqlite32" version="3.9.54" targetFramework="net45" />
<package id="ServiceStack.OrmLite.SqlServer" version="3.9.43" targetFramework="net45" />
<package id="ServiceStack.Redis" version="3.9.43" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.54" targetFramework="net45" />