update people saving

This commit is contained in:
Luke Pulverenti
2015-06-28 21:10:45 -04:00
parent b3dd4beb8a
commit b1be09242c
90 changed files with 800 additions and 728 deletions

View File

@@ -2060,7 +2060,7 @@ namespace MediaBrowser.Server.Implementations.Library
public List<PersonInfo> GetPeople(BaseItem item)
{
return item.People ?? new List<PersonInfo>();
return item.People ?? ItemRepository.GetPeople(item.Id);
}
public List<PersonInfo> GetAllPeople()
@@ -2072,10 +2072,15 @@ namespace MediaBrowser.Server.Implementations.Library
.ToList();
}
public Task UpdatePeople(BaseItem item, List<PersonInfo> people)
public async Task UpdatePeople(BaseItem item, List<PersonInfo> people)
{
item.People = people;
return item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
await ItemRepository.UpdatePeople(item.Id, people).ConfigureAwait(false);
if (item.People != null)
{
item.People = null;
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
}
}
}
}

View File

@@ -65,6 +65,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _saveChildrenCommand;
private IDbCommand _deleteItemCommand;
private IDbCommand _deletePeopleCommand;
private IDbCommand _savePersonCommand;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
@@ -121,6 +123,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
"create table if not exists ChildrenIds (ParentId GUID, ItemId GUID, PRIMARY KEY (ParentId, ItemId))",
"create index if not exists idx_ChildrenIds on ChildrenIds(ParentId,ItemId)",
"create table if not exists People (ItemId GUID, Name TEXT NOT NULL, Role TEXT, PersonType TEXT, SortOrder int, ListOrder int)",
//pragmas
"pragma temp_store = memory",
@@ -208,6 +212,19 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveChildrenCommand.CommandText = "replace into ChildrenIds (ParentId, ItemId) values (@ParentId, @ItemId)";
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ParentId");
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ItemId");
_deletePeopleCommand = _connection.CreateCommand();
_deletePeopleCommand.CommandText = "delete from People where ItemId=@Id";
_deletePeopleCommand.Parameters.Add(_deletePeopleCommand, "@Id");
_savePersonCommand = _connection.CreateCommand();
_savePersonCommand.CommandText = "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)";
_savePersonCommand.Parameters.Add(_savePersonCommand, "@ItemId");
_savePersonCommand.Parameters.Add(_savePersonCommand, "@Name");
_savePersonCommand.Parameters.Add(_savePersonCommand, "@Role");
_savePersonCommand.Parameters.Add(_savePersonCommand, "@PersonType");
_savePersonCommand.Parameters.Add(_savePersonCommand, "@SortOrder");
_savePersonCommand.Parameters.Add(_savePersonCommand, "@ListOrder");
}
/// <summary>
@@ -1091,5 +1108,141 @@ namespace MediaBrowser.Server.Implementations.Persistence
CheckDisposed();
return _mediaStreamsRepository.SaveMediaStreams(id, streams, cancellationToken);
}
public List<PersonInfo> GetPeople(Guid itemId)
{
if (itemId == Guid.Empty)
{
throw new ArgumentNullException("itemId");
}
CheckDisposed();
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = "select ItemId, Name, Role, PersonType, SortOrder from People where ItemId=@ItemId order by ListOrder";
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = itemId;
var list = new List<PersonInfo>();
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
list.Add(GetPerson(reader));
}
}
return list;
}
}
public async Task UpdatePeople(Guid itemId, List<PersonInfo> people)
{
if (itemId == Guid.Empty)
{
throw new ArgumentNullException("itemId");
}
if (people == null)
{
throw new ArgumentNullException("people");
}
CheckDisposed();
var cancellationToken = CancellationToken.None;
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
IDbTransaction transaction = null;
try
{
transaction = _connection.BeginTransaction();
// First delete
_deletePeopleCommand.GetParameter(0).Value = itemId;
_deletePeopleCommand.Transaction = transaction;
_deletePeopleCommand.ExecuteNonQuery();
var listIndex = 0;
foreach (var person in people)
{
cancellationToken.ThrowIfCancellationRequested();
_savePersonCommand.GetParameter(0).Value = itemId;
_savePersonCommand.GetParameter(1).Value = person.Name;
_savePersonCommand.GetParameter(2).Value = person.Role;
_savePersonCommand.GetParameter(3).Value = person.Type;
_savePersonCommand.GetParameter(4).Value = person.SortOrder;
_savePersonCommand.GetParameter(5).Value = listIndex;
_savePersonCommand.Transaction = transaction;
_savePersonCommand.ExecuteNonQuery();
listIndex++;
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
_logger.ErrorException("Failed to save people:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
_writeLock.Release();
}
}
private PersonInfo GetPerson(IDataReader reader)
{
var item = new PersonInfo();
item.Name = reader.GetString(1);
if (!reader.IsDBNull(2))
{
item.Role = reader.GetString(2);
}
if (!reader.IsDBNull(3))
{
item.Type = reader.GetString(3);
}
if (!reader.IsDBNull(4))
{
item.SortOrder = reader.GetInt32(4);
}
return item;
}
}
}