using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.FileOrganization;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
public class SqliteFileOrganizationRepository : BaseSqliteRepository, IFileOrganizationRepository, IDisposable
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
public SqliteFileOrganizationRepository(ILogger logger, IServerApplicationPaths appPaths) : base(logger)
{
DbFilePath = Path.Combine(appPaths.DataPath, "fileorganization.db");
}
///
/// Opens the connection to the database
///
/// Task.
public void Initialize()
{
using (var connection = CreateConnection())
{
string[] queries = {
"create table if not exists FileOrganizerResults (ResultId GUID PRIMARY KEY, OriginalPath TEXT, TargetPath TEXT, FileLength INT, OrganizationDate datetime, Status TEXT, OrganizationType TEXT, StatusMessage TEXT, ExtractedName TEXT, ExtractedYear int null, ExtractedSeasonNumber int null, ExtractedEpisodeNumber int null, ExtractedEndingEpisodeNumber, DuplicatePaths TEXT int null)",
"create index if not exists idx_FileOrganizerResults on FileOrganizerResults(ResultId)"
};
connection.RunQueries(queries);
}
}
public async Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
cancellationToken.ThrowIfCancellationRequested();
using (var connection = CreateConnection())
{
connection.RunInTransaction(db =>
{
var paramList = new List