aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs')
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs
new file mode 100644
index 0000000000..a356932a78
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs
@@ -0,0 +1,72 @@
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using Jellyfin.Database.Implementations;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Diagnostics;
+
+namespace Jellyfin.Server.Implementations.Tests.Item;
+
+/// <summary>
+/// Records the reader commands EF Core sends, so a test can ask SQLite how it would run one.
+/// </summary>
+internal sealed class QueryPlanRecorder : DbCommandInterceptor
+{
+ /// <summary>
+ /// Gets the recorded commands, oldest first.
+ /// </summary>
+ public List<RecordedCommand> Commands { get; } = [];
+
+ /// <summary>
+ /// Returns the <c>EXPLAIN QUERY PLAN</c> detail lines for a recorded command.
+ /// </summary>
+ /// <param name="context">A context on the database the command ran against.</param>
+ /// <param name="query">The recorded command.</param>
+ /// <returns>One entry per plan line.</returns>
+ public static string[] Explain(JellyfinDbContext context, RecordedCommand query)
+ {
+ using var command = context.Database.GetDbConnection().CreateCommand();
+#pragma warning disable CA2100 // query.Sql is generated by EF Core; query values remain bound parameters.
+ command.CommandText = "EXPLAIN QUERY PLAN " + query.Sql;
+#pragma warning restore CA2100
+ foreach (var value in query.Parameters)
+ {
+ var parameter = command.CreateParameter();
+ parameter.ParameterName = value.Name;
+ parameter.Value = value.Value;
+ command.Parameters.Add(parameter);
+ }
+
+ using var reader = command.ExecuteReader();
+ var plan = new List<string>();
+ while (reader.Read())
+ {
+ plan.Add(reader.GetString(3));
+ }
+
+ return plan.ToArray();
+ }
+
+ /// <inheritdoc />
+ public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
+ {
+ Record(command);
+ return result;
+ }
+
+ /// <inheritdoc />
+ public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
+ {
+ Record(command);
+ return ValueTask.FromResult(result);
+ }
+
+ private void Record(DbCommand command)
+ {
+ Commands.Add(new RecordedCommand(
+ command.CommandText,
+ command.Parameters.Cast<DbParameter>().Select(p => (p.ParameterName, p.Value)).ToArray()));
+ }
+}