Properly dispose prepared statements
This commit is contained in:
parent
218b819a9f
commit
b69abd4d7d
|
@ -166,18 +166,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
|
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
|
||||||
=> connection.PrepareStatement(sql);
|
=> connection.PrepareStatement(sql);
|
||||||
|
|
||||||
public IStatement[] PrepareAll(IDatabaseConnection connection, IReadOnlyList<string> sql)
|
|
||||||
{
|
|
||||||
int len = sql.Count;
|
|
||||||
IStatement[] statements = new IStatement[len];
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
statements[i] = connection.PrepareStatement(sql[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return statements;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected bool TableExists(ManagedConnection connection, string name)
|
protected bool TableExists(ManagedConnection connection, string name)
|
||||||
{
|
{
|
||||||
return connection.RunInTransaction(
|
return connection.RunInTransaction(
|
||||||
|
|
|
@ -624,14 +624,8 @@ namespace Emby.Server.Implementations.Data
|
||||||
|
|
||||||
private void SaveItemsInTransaction(IDatabaseConnection db, IEnumerable<(BaseItem Item, List<Guid> AncestorIds, BaseItem TopParent, string UserDataKey, List<string> InheritedTags)> tuples)
|
private void SaveItemsInTransaction(IDatabaseConnection db, IEnumerable<(BaseItem Item, List<Guid> AncestorIds, BaseItem TopParent, string UserDataKey, List<string> InheritedTags)> tuples)
|
||||||
{
|
{
|
||||||
var statements = PrepareAll(db, new string[]
|
using (var saveItemStatement = PrepareStatement(db, SaveItemCommandText))
|
||||||
{
|
using (var deleteAncestorsStatement = PrepareStatement(db, "delete from AncestorIds where ItemId=@ItemId"))
|
||||||
SaveItemCommandText,
|
|
||||||
"delete from AncestorIds where ItemId=@ItemId"
|
|
||||||
});
|
|
||||||
|
|
||||||
using (var saveItemStatement = statements[0])
|
|
||||||
using (var deleteAncestorsStatement = statements[1])
|
|
||||||
{
|
{
|
||||||
var requiresReset = false;
|
var requiresReset = false;
|
||||||
foreach (var tuple in tuples)
|
foreach (var tuple in tuples)
|
||||||
|
@ -1286,15 +1280,13 @@ namespace Emby.Server.Implementations.Data
|
||||||
CheckDisposed();
|
CheckDisposed();
|
||||||
|
|
||||||
using (var connection = GetConnection(true))
|
using (var connection = GetConnection(true))
|
||||||
|
using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
|
||||||
{
|
{
|
||||||
using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
|
statement.TryBind("@guid", id);
|
||||||
{
|
|
||||||
statement.TryBind("@guid", id);
|
|
||||||
|
|
||||||
foreach (var row in statement.ExecuteQuery())
|
foreach (var row in statement.ExecuteQuery())
|
||||||
{
|
{
|
||||||
return GetItem(row, new InternalItemsQuery());
|
return GetItem(row, new InternalItemsQuery());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1972,22 +1964,19 @@ namespace Emby.Server.Implementations.Data
|
||||||
{
|
{
|
||||||
CheckDisposed();
|
CheckDisposed();
|
||||||
|
|
||||||
|
var chapters = new List<ChapterInfo>();
|
||||||
using (var connection = GetConnection(true))
|
using (var connection = GetConnection(true))
|
||||||
|
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
|
||||||
{
|
{
|
||||||
var chapters = new List<ChapterInfo>();
|
statement.TryBind("@ItemId", item.Id);
|
||||||
|
|
||||||
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
|
foreach (var row in statement.ExecuteQuery())
|
||||||
{
|
{
|
||||||
statement.TryBind("@ItemId", item.Id);
|
chapters.Add(GetChapter(row, item));
|
||||||
|
|
||||||
foreach (var row in statement.ExecuteQuery())
|
|
||||||
{
|
|
||||||
chapters.Add(GetChapter(row, item));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chapters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return chapters;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -1996,16 +1985,14 @@ namespace Emby.Server.Implementations.Data
|
||||||
CheckDisposed();
|
CheckDisposed();
|
||||||
|
|
||||||
using (var connection = GetConnection(true))
|
using (var connection = GetConnection(true))
|
||||||
|
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
|
||||||
{
|
{
|
||||||
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
|
statement.TryBind("@ItemId", item.Id);
|
||||||
{
|
statement.TryBind("@ChapterIndex", index);
|
||||||
statement.TryBind("@ItemId", item.Id);
|
|
||||||
statement.TryBind("@ChapterIndex", index);
|
|
||||||
|
|
||||||
foreach (var row in statement.ExecuteQuery())
|
foreach (var row in statement.ExecuteQuery())
|
||||||
{
|
{
|
||||||
return GetChapter(row, item);
|
return GetChapter(row, item);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2858,13 +2845,10 @@ namespace Emby.Server.Implementations.Data
|
||||||
connection.RunInTransaction(
|
connection.RunInTransaction(
|
||||||
db =>
|
db =>
|
||||||
{
|
{
|
||||||
var itemQueryStatement = PrepareStatement(db, itemQuery);
|
|
||||||
var totalRecordCountQueryStatement = PrepareStatement(db, totalRecordCountQuery);
|
|
||||||
|
|
||||||
if (!isReturningZeroItems)
|
if (!isReturningZeroItems)
|
||||||
{
|
{
|
||||||
using (new QueryTimeLogger(Logger, itemQuery, "GetItems.ItemQuery"))
|
using (new QueryTimeLogger(Logger, itemQuery, "GetItems.ItemQuery"))
|
||||||
using (var statement = itemQueryStatement)
|
using (var statement = PrepareStatement(db, itemQuery))
|
||||||
{
|
{
|
||||||
if (EnableJoinUserData(query))
|
if (EnableJoinUserData(query))
|
||||||
{
|
{
|
||||||
|
@ -2899,7 +2883,7 @@ namespace Emby.Server.Implementations.Data
|
||||||
if (query.EnableTotalRecordCount)
|
if (query.EnableTotalRecordCount)
|
||||||
{
|
{
|
||||||
using (new QueryTimeLogger(Logger, totalRecordCountQuery, "GetItems.TotalRecordCount"))
|
using (new QueryTimeLogger(Logger, totalRecordCountQuery, "GetItems.TotalRecordCount"))
|
||||||
using (var statement = totalRecordCountQueryStatement)
|
using (var statement = PrepareStatement(db, totalRecordCountQuery))
|
||||||
{
|
{
|
||||||
if (EnableJoinUserData(query))
|
if (EnableJoinUserData(query))
|
||||||
{
|
{
|
||||||
|
@ -4768,22 +4752,20 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
||||||
commandText.Append(" LIMIT ").Append(query.Limit);
|
commandText.Append(" LIMIT ").Append(query.Limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var list = new List<string>();
|
||||||
using (var connection = GetConnection(true))
|
using (var connection = GetConnection(true))
|
||||||
|
using (var statement = PrepareStatement(connection, commandText.ToString()))
|
||||||
{
|
{
|
||||||
var list = new List<string>();
|
// Run this again to bind the params
|
||||||
using (var statement = PrepareStatement(connection, commandText.ToString()))
|
GetPeopleWhereClauses(query, statement);
|
||||||
|
|
||||||
|
foreach (var row in statement.ExecuteQuery())
|
||||||
{
|
{
|
||||||
// Run this again to bind the params
|
list.Add(row.GetString(0));
|
||||||
GetPeopleWhereClauses(query, statement);
|
|
||||||
|
|
||||||
foreach (var row in statement.ExecuteQuery())
|
|
||||||
{
|
|
||||||
list.Add(row.GetString(0));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
|
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
|
||||||
|
@ -4808,23 +4790,20 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
|
||||||
commandText += " LIMIT " + query.Limit;
|
commandText += " LIMIT " + query.Limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var list = new List<PersonInfo>();
|
||||||
using (var connection = GetConnection(true))
|
using (var connection = GetConnection(true))
|
||||||
|
using (var statement = PrepareStatement(connection, commandText))
|
||||||
{
|
{
|
||||||
var list = new List<PersonInfo>();
|
// Run this again to bind the params
|
||||||
|
GetPeopleWhereClauses(query, statement);
|
||||||
|
|
||||||
using (var statement = PrepareStatement(connection, commandText))
|
foreach (var row in statement.ExecuteQuery())
|
||||||
{
|
{
|
||||||
// Run this again to bind the params
|
list.Add(GetPerson(row));
|
||||||
GetPeopleWhereClauses(query, statement);
|
|
||||||
|
|
||||||
foreach (var row in statement.ExecuteQuery())
|
|
||||||
{
|
|
||||||
list.Add(GetPerson(row));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement)
|
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user