You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../Sync/SyncRepository.cs

836 lines
32 KiB

10 years ago
using MediaBrowser.Controller;
using MediaBrowser.Controller.Sync;
10 years ago
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
10 years ago
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Sync;
using MediaBrowser.Server.Implementations.Persistence;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
9 years ago
public class SyncRepository : BaseSqliteRepository, ISyncRepository
{
private IDbConnection _connection;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
10 years ago
private IDbCommand _insertJobCommand;
private IDbCommand _updateJobCommand;
private IDbCommand _deleteJobCommand;
10 years ago
private IDbCommand _deleteJobItemsCommand;
10 years ago
private IDbCommand _insertJobItemCommand;
private IDbCommand _updateJobItemCommand;
10 years ago
private readonly IJsonSerializer _json;
9 years ago
private readonly IServerApplicationPaths _appPaths;
10 years ago
9 years ago
public SyncRepository(ILogManager logManager, IJsonSerializer json, IServerApplicationPaths appPaths)
: base(logManager)
{
10 years ago
_json = json;
9 years ago
_appPaths = appPaths;
}
public async Task Initialize()
{
10 years ago
var dbFile = Path.Combine(_appPaths.DataPath, "sync14.db");
9 years ago
_connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
string[] queries = {
10 years ago
"create table if not exists SyncJobs (Id GUID PRIMARY KEY, TargetId TEXT NOT NULL, Name TEXT NOT NULL, Profile TEXT, Quality TEXT, Bitrate INT, Status TEXT NOT NULL, Progress FLOAT, UserId TEXT NOT NULL, ItemIds TEXT NOT NULL, Category TEXT, ParentId TEXT, UnwatchedOnly BIT, ItemLimit INT, SyncNewContent BIT, DateCreated DateTime, DateLastModified DateTime, ItemCount int)",
"create index if not exists idx_SyncJobs on SyncJobs(Id)",
10 years ago
"create table if not exists SyncJobItems (Id GUID PRIMARY KEY, ItemId TEXT, ItemName TEXT, MediaSourceId TEXT, JobId TEXT, TemporaryPath TEXT, OutputPath TEXT, Status TEXT, TargetId TEXT, DateCreated DateTime, Progress FLOAT, AdditionalFiles TEXT, MediaSource TEXT, IsMarkedForRemoval BIT, JobItemIndex INT)",
"create index if not exists idx_SyncJobItems on SyncJobs(Id)",
//pragmas
"pragma temp_store = memory",
"pragma shrink_memory"
};
9 years ago
_connection.RunQueries(queries, Logger);
9 years ago
_connection.AddColumn(Logger, "SyncJobs", "Profile", "TEXT");
_connection.AddColumn(Logger, "SyncJobs", "Bitrate", "INT");
10 years ago
PrepareStatements();
}
private void PrepareStatements()
{
10 years ago
// _deleteJobCommand
_deleteJobCommand = _connection.CreateCommand();
_deleteJobCommand.CommandText = "delete from SyncJobs where Id=@Id";
_deleteJobCommand.Parameters.Add(_deleteJobCommand, "@Id");
10 years ago
// _deleteJobItemsCommand
_deleteJobItemsCommand = _connection.CreateCommand();
_deleteJobItemsCommand.CommandText = "delete from SyncJobItems where JobId=@JobId";
_deleteJobItemsCommand.Parameters.Add(_deleteJobItemsCommand, "@JobId");
10 years ago
10 years ago
// _insertJobCommand
_insertJobCommand = _connection.CreateCommand();
10 years ago
_insertJobCommand.CommandText = "insert into SyncJobs (Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount) values (@Id, @TargetId, @Name, @Profile, @Quality, @Bitrate, @Status, @Progress, @UserId, @ItemIds, @Category, @ParentId, @UnwatchedOnly, @ItemLimit, @SyncNewContent, @DateCreated, @DateLastModified, @ItemCount)";
10 years ago
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Id");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@TargetId");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Name");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Profile");
10 years ago
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Quality");
10 years ago
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Bitrate");
10 years ago
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Status");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Progress");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@UserId");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@ItemIds");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@Category");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@ParentId");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@UnwatchedOnly");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@ItemLimit");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@SyncNewContent");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@DateCreated");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@DateLastModified");
_insertJobCommand.Parameters.Add(_insertJobCommand, "@ItemCount");
// _updateJobCommand
_updateJobCommand = _connection.CreateCommand();
10 years ago
_updateJobCommand.CommandText = "update SyncJobs set TargetId=@TargetId,Name=@Name,Profile=@Profile,Quality=@Quality,Bitrate=@Bitrate,Status=@Status,Progress=@Progress,UserId=@UserId,ItemIds=@ItemIds,Category=@Category,ParentId=@ParentId,UnwatchedOnly=@UnwatchedOnly,ItemLimit=@ItemLimit,SyncNewContent=@SyncNewContent,DateCreated=@DateCreated,DateLastModified=@DateLastModified,ItemCount=@ItemCount where Id=@ID";
10 years ago
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Id");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@TargetId");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Name");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Profile");
10 years ago
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Quality");
10 years ago
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Bitrate");
10 years ago
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Status");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Progress");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@UserId");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@ItemIds");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@Category");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@ParentId");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@UnwatchedOnly");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@ItemLimit");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@SyncNewContent");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@DateCreated");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@DateLastModified");
_updateJobCommand.Parameters.Add(_updateJobCommand, "@ItemCount");
10 years ago
// _insertJobItemCommand
_insertJobItemCommand = _connection.CreateCommand();
_insertJobItemCommand.CommandText = "insert into SyncJobItems (Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex) values (@Id, @ItemId, @ItemName, @MediaSourceId, @JobId, @TemporaryPath, @OutputPath, @Status, @TargetId, @DateCreated, @Progress, @AdditionalFiles, @MediaSource, @IsMarkedForRemoval, @JobItemIndex)";
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@Id");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@ItemId");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@ItemName");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@MediaSourceId");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@JobId");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@TemporaryPath");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@OutputPath");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@Status");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@TargetId");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@DateCreated");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@Progress");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@AdditionalFiles");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@MediaSource");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@IsMarkedForRemoval");
_insertJobItemCommand.Parameters.Add(_insertJobItemCommand, "@JobItemIndex");
// _updateJobItemCommand
_updateJobItemCommand = _connection.CreateCommand();
_updateJobItemCommand.CommandText = "update SyncJobItems set ItemId=@ItemId,ItemName=@ItemName,MediaSourceId=@MediaSourceId,JobId=@JobId,TemporaryPath=@TemporaryPath,OutputPath=@OutputPath,Status=@Status,TargetId=@TargetId,DateCreated=@DateCreated,Progress=@Progress,AdditionalFiles=@AdditionalFiles,MediaSource=@MediaSource,IsMarkedForRemoval=@IsMarkedForRemoval,JobItemIndex=@JobItemIndex where Id=@Id";
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@Id");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@ItemId");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@ItemName");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@MediaSourceId");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@JobId");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@TemporaryPath");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@OutputPath");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@Status");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@TargetId");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@DateCreated");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@Progress");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@AdditionalFiles");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@MediaSource");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@IsMarkedForRemoval");
_updateJobItemCommand.Parameters.Add(_updateJobItemCommand, "@JobItemIndex");
}
10 years ago
private const string BaseJobSelectText = "select Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount from SyncJobs";
10 years ago
private const string BaseJobItemSelectText = "select Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex from SyncJobItems";
public SyncJob GetJob(string id)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException("id");
}
CheckDisposed();
var guid = new Guid(id);
if (guid == Guid.Empty)
{
throw new ArgumentNullException("id");
}
10 years ago
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = BaseJobSelectText + " where Id=@Id";
cmd.Parameters.Add(cmd, "@Id", DbType.Guid).Value = guid;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
{
if (reader.Read())
{
return GetJob(reader);
}
}
}
return null;
}
private SyncJob GetJob(IDataReader reader)
{
var info = new SyncJob
{
Id = reader.GetGuid(0).ToString("N"),
TargetId = reader.GetString(1),
Name = reader.GetString(2)
};
if (!reader.IsDBNull(3))
{
info.Profile = reader.GetString(3);
}
if (!reader.IsDBNull(4))
{
info.Quality = reader.GetString(4);
}
if (!reader.IsDBNull(5))
{
10 years ago
info.Bitrate = reader.GetInt32(5);
}
if (!reader.IsDBNull(6))
{
10 years ago
info.Status = (SyncJobStatus)Enum.Parse(typeof(SyncJobStatus), reader.GetString(6), true);
}
if (!reader.IsDBNull(7))
{
10 years ago
info.Progress = reader.GetDouble(7);
}
if (!reader.IsDBNull(8))
{
10 years ago
info.UserId = reader.GetString(8);
}
if (!reader.IsDBNull(9))
{
10 years ago
info.RequestedItemIds = reader.GetString(9).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
10 years ago
if (!reader.IsDBNull(10))
{
10 years ago
info.Category = (SyncCategory)Enum.Parse(typeof(SyncCategory), reader.GetString(10), true);
10 years ago
}
if (!reader.IsDBNull(11))
{
10 years ago
info.ParentId = reader.GetString(11);
}
if (!reader.IsDBNull(12))
{
10 years ago
info.UnwatchedOnly = reader.GetBoolean(12);
}
if (!reader.IsDBNull(13))
{
info.ItemLimit = reader.GetInt32(13);
10 years ago
}
10 years ago
info.SyncNewContent = reader.GetBoolean(14);
10 years ago
info.DateCreated = reader.GetDateTime(15).ToUniversalTime();
info.DateLastModified = reader.GetDateTime(16).ToUniversalTime();
info.ItemCount = reader.GetInt32(17);
return info;
}
public Task Create(SyncJob job)
{
10 years ago
return InsertOrUpdate(job, _insertJobCommand);
}
public Task Update(SyncJob job)
{
return InsertOrUpdate(job, _updateJobCommand);
}
10 years ago
private async Task InsertOrUpdate(SyncJob job, IDbCommand cmd)
{
if (job == null)
{
throw new ArgumentNullException("job");
}
CheckDisposed();
9 years ago
await WriteLock.WaitAsync().ConfigureAwait(false);
IDbTransaction transaction = null;
try
{
transaction = _connection.BeginTransaction();
var index = 0;
10 years ago
cmd.GetParameter(index++).Value = new Guid(job.Id);
cmd.GetParameter(index++).Value = job.TargetId;
cmd.GetParameter(index++).Value = job.Name;
cmd.GetParameter(index++).Value = job.Profile;
10 years ago
cmd.GetParameter(index++).Value = job.Quality;
10 years ago
cmd.GetParameter(index++).Value = job.Bitrate;
10 years ago
cmd.GetParameter(index++).Value = job.Status.ToString();
cmd.GetParameter(index++).Value = job.Progress;
cmd.GetParameter(index++).Value = job.UserId;
cmd.GetParameter(index++).Value = string.Join(",", job.RequestedItemIds.ToArray());
cmd.GetParameter(index++).Value = job.Category;
cmd.GetParameter(index++).Value = job.ParentId;
cmd.GetParameter(index++).Value = job.UnwatchedOnly;
cmd.GetParameter(index++).Value = job.ItemLimit;
cmd.GetParameter(index++).Value = job.SyncNewContent;
cmd.GetParameter(index++).Value = job.DateCreated;
cmd.GetParameter(index++).Value = job.DateLastModified;
cmd.GetParameter(index++).Value = job.ItemCount;
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
9 years ago
Logger.ErrorException("Failed to save record:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
9 years ago
WriteLock.Release();
}
}
public async Task DeleteJob(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException("id");
}
CheckDisposed();
9 years ago
await WriteLock.WaitAsync().ConfigureAwait(false);
IDbTransaction transaction = null;
try
{
transaction = _connection.BeginTransaction();
var index = 0;
_deleteJobCommand.GetParameter(index++).Value = new Guid(id);
_deleteJobCommand.Transaction = transaction;
_deleteJobCommand.ExecuteNonQuery();
10 years ago
index = 0;
_deleteJobItemsCommand.GetParameter(index++).Value = id;
_deleteJobItemsCommand.Transaction = transaction;
_deleteJobItemsCommand.ExecuteNonQuery();
10 years ago
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
9 years ago
Logger.ErrorException("Failed to save record:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
9 years ago
WriteLock.Release();
}
}
public QueryResult<SyncJob> GetJobs(SyncJobQuery query)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
CheckDisposed();
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = BaseJobSelectText;
var whereClauses = new List<string>();
if (query.Statuses.Length > 0)
{
10 years ago
var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray());
whereClauses.Add(string.Format("Status in ({0})", statuses));
}
10 years ago
if (!string.IsNullOrWhiteSpace(query.TargetId))
{
whereClauses.Add("TargetId=@TargetId");
10 years ago
cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId;
10 years ago
}
if (!string.IsNullOrWhiteSpace(query.UserId))
{
whereClauses.Add("UserId=@UserId");
cmd.Parameters.Add(cmd, "@UserId", DbType.String).Value = query.UserId;
}
10 years ago
if (query.SyncNewContent.HasValue)
{
whereClauses.Add("SyncNewContent=@SyncNewContent");
cmd.Parameters.Add(cmd, "@SyncNewContent", DbType.Boolean).Value = query.SyncNewContent.Value;
}
10 years ago
cmd.CommandText += " mainTable";
var whereTextWithoutPaging = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
var startIndex = query.StartIndex ?? 0;
if (startIndex > 0)
{
10 years ago
whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobs ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC LIMIT {0})",
startIndex.ToString(_usCulture)));
}
if (whereClauses.Count > 0)
{
cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray());
}
10 years ago
cmd.CommandText += " ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC";
if (query.Limit.HasValue)
{
cmd.CommandText += " LIMIT " + query.Limit.Value.ToString(_usCulture);
}
cmd.CommandText += "; select count (Id) from SyncJobs" + whereTextWithoutPaging;
var list = new List<SyncJob>();
var count = 0;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
list.Add(GetJob(reader));
}
if (reader.NextResult() && reader.Read())
{
count = reader.GetInt32(0);
}
}
return new QueryResult<SyncJob>()
{
Items = list.ToArray(),
TotalRecordCount = count
};
}
}
public SyncJobItem GetJobItem(string id)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException("id");
}
CheckDisposed();
var guid = new Guid(id);
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = BaseJobItemSelectText + " where Id=@Id";
cmd.Parameters.Add(cmd, "@Id", DbType.Guid).Value = guid;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
{
if (reader.Read())
{
return GetJobItem(reader);
}
}
}
return null;
}
10 years ago
private QueryResult<T> GetJobItemReader<T>(SyncJobItemQuery query, string baseSelectText, Func<IDataReader, T> itemFactory)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
using (var cmd = _connection.CreateCommand())
{
10 years ago
cmd.CommandText = baseSelectText;
var whereClauses = new List<string>();
if (!string.IsNullOrWhiteSpace(query.JobId))
{
whereClauses.Add("JobId=@JobId");
cmd.Parameters.Add(cmd, "@JobId", DbType.String).Value = query.JobId;
}
if (!string.IsNullOrWhiteSpace(query.ItemId))
{
whereClauses.Add("ItemId=@ItemId");
cmd.Parameters.Add(cmd, "@ItemId", DbType.String).Value = query.ItemId;
}
10 years ago
if (!string.IsNullOrWhiteSpace(query.TargetId))
{
whereClauses.Add("TargetId=@TargetId");
cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId;
}
10 years ago
if (query.Statuses.Length > 0)
10 years ago
{
var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray());
whereClauses.Add(string.Format("Status in ({0})", statuses));
10 years ago
}
var whereTextWithoutPaging = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses.ToArray());
var startIndex = query.StartIndex ?? 0;
if (startIndex > 0)
{
10 years ago
whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobItems ORDER BY JobItemIndex, DateCreated LIMIT {0})",
startIndex.ToString(_usCulture)));
}
if (whereClauses.Count > 0)
{
cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray());
}
10 years ago
cmd.CommandText += " ORDER BY JobItemIndex, DateCreated";
if (query.Limit.HasValue)
{
cmd.CommandText += " LIMIT " + query.Limit.Value.ToString(_usCulture);
}
cmd.CommandText += "; select count (Id) from SyncJobItems" + whereTextWithoutPaging;
10 years ago
var list = new List<T>();
var count = 0;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
10 years ago
list.Add(itemFactory(reader));
}
if (reader.NextResult() && reader.Read())
{
count = reader.GetInt32(0);
}
}
10 years ago
return new QueryResult<T>()
{
Items = list.ToArray(),
TotalRecordCount = count
};
}
}
public QueryResult<SyncedItemProgress> GetSyncedItemProgresses(SyncJobItemQuery query)
10 years ago
{
return GetJobItemReader(query, "select ItemId,Status from SyncJobItems", GetSyncedItemProgress);
10 years ago
}
public QueryResult<SyncJobItem> GetJobItems(SyncJobItemQuery query)
{
return GetJobItemReader(query, BaseJobItemSelectText, GetJobItem);
}
public Task Create(SyncJobItem jobItem)
{
10 years ago
return InsertOrUpdate(jobItem, _insertJobItemCommand);
}
public Task Update(SyncJobItem jobItem)
{
return InsertOrUpdate(jobItem, _updateJobItemCommand);
}
10 years ago
private async Task InsertOrUpdate(SyncJobItem jobItem, IDbCommand cmd)
{
if (jobItem == null)
{
throw new ArgumentNullException("jobItem");
}
CheckDisposed();
9 years ago
await WriteLock.WaitAsync().ConfigureAwait(false);
IDbTransaction transaction = null;
try
{
transaction = _connection.BeginTransaction();
var index = 0;
10 years ago
cmd.GetParameter(index++).Value = new Guid(jobItem.Id);
cmd.GetParameter(index++).Value = jobItem.ItemId;
cmd.GetParameter(index++).Value = jobItem.ItemName;
cmd.GetParameter(index++).Value = jobItem.MediaSourceId;
cmd.GetParameter(index++).Value = jobItem.JobId;
cmd.GetParameter(index++).Value = jobItem.TemporaryPath;
cmd.GetParameter(index++).Value = jobItem.OutputPath;
cmd.GetParameter(index++).Value = jobItem.Status.ToString();
cmd.GetParameter(index++).Value = jobItem.TargetId;
cmd.GetParameter(index++).Value = jobItem.DateCreated;
cmd.GetParameter(index++).Value = jobItem.Progress;
cmd.GetParameter(index++).Value = _json.SerializeToString(jobItem.AdditionalFiles);
cmd.GetParameter(index++).Value = jobItem.MediaSource == null ? null : _json.SerializeToString(jobItem.MediaSource);
cmd.GetParameter(index++).Value = jobItem.IsMarkedForRemoval;
cmd.GetParameter(index++).Value = jobItem.JobItemIndex;
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
9 years ago
Logger.ErrorException("Failed to save record:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
9 years ago
WriteLock.Release();
}
}
private SyncJobItem GetJobItem(IDataReader reader)
{
var info = new SyncJobItem
{
Id = reader.GetGuid(0).ToString("N"),
10 years ago
ItemId = reader.GetString(1)
};
10 years ago
if (!reader.IsDBNull(2))
{
info.ItemName = reader.GetString(2);
10 years ago
}
10 years ago
if (!reader.IsDBNull(3))
{
info.MediaSourceId = reader.GetString(3);
}
info.JobId = reader.GetString(4);
10 years ago
if (!reader.IsDBNull(5))
{
info.TemporaryPath = reader.GetString(5);
}
10 years ago
if (!reader.IsDBNull(6))
{
10 years ago
info.OutputPath = reader.GetString(6);
}
10 years ago
if (!reader.IsDBNull(7))
{
10 years ago
info.Status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), reader.GetString(7), true);
}
10 years ago
info.TargetId = reader.GetString(8);
10 years ago
info.DateCreated = reader.GetDateTime(9).ToUniversalTime();
10 years ago
if (!reader.IsDBNull(10))
{
10 years ago
info.Progress = reader.GetDouble(10);
}
10 years ago
10 years ago
if (!reader.IsDBNull(11))
{
var json = reader.GetString(11);
if (!string.IsNullOrWhiteSpace(json))
{
info.AdditionalFiles = _json.DeserializeFromString<List<ItemFileInfo>>(json);
}
}
if (!reader.IsDBNull(12))
{
var json = reader.GetString(12);
if (!string.IsNullOrWhiteSpace(json))
{
info.MediaSource = _json.DeserializeFromString<MediaSourceInfo>(json);
}
}
10 years ago
info.IsMarkedForRemoval = reader.GetBoolean(13);
10 years ago
info.JobItemIndex = reader.GetInt32(14);
10 years ago
return info;
}
private SyncedItemProgress GetSyncedItemProgress(IDataReader reader)
10 years ago
{
var item = new SyncedItemProgress();
item.ItemId = reader.GetString(0);
if (!reader.IsDBNull(1))
{
item.Status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), reader.GetString(1), true);
}
return item;
10 years ago
}
private bool _disposed;
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name + " has been disposed and cannot be accessed.");
}
}
9 years ago
protected override void Dispose(bool dispose)
{
if (dispose)
{
_disposed = true;
9 years ago
}
base.Dispose(dispose);
}
9 years ago
protected override void CloseConnection()
{
if (_connection != null)
{
if (_connection.IsOpen())
{
9 years ago
_connection.Close();
}
9 years ago
_connection.Dispose();
_connection = null;
}
}
}
}