mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-21 17:44:43 +01:00
update people saving
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user