Allow random queries to be paginated

This commit is contained in:
Zoe Roux
2023-10-25 00:52:51 +02:00
parent e8b929d4ca
commit e13f9c6aa8
7 changed files with 92 additions and 41 deletions
@@ -41,6 +41,13 @@ namespace Kyoo.Postgresql
/// </remarks>
public abstract class DatabaseContext : DbContext
{
/// <summary>
/// Calculate the MD5 of a string, can only be used in database context.
/// </summary>
/// <param name="str">The string to hash</param>
/// <returns>The hash</returns>
public static string MD5(string str) => throw new NotSupportedException();
/// <summary>
/// All collections of Kyoo. See <see cref="Collection"/>.
/// </summary>
@@ -2,6 +2,7 @@
<PropertyGroup>
<AssemblyName>Kyoo.Postgresql</AssemblyName>
<RootNamespace>Kyoo.Postgresql</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@@ -18,12 +18,14 @@
using System;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using EFCore.NamingConventions.Internal;
using Kyoo.Abstractions.Models;
using Kyoo.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Npgsql;
namespace Kyoo.Postgresql
@@ -104,6 +106,18 @@ namespace Kyoo.Postgresql
modelBuilder.HasPostgresEnum<Genre>();
modelBuilder.HasPostgresEnum<ItemKind>();
modelBuilder.HasDbFunction(typeof(DatabaseContext).GetMethod(nameof(MD5)))
.HasTranslation(args =>
new SqlFunctionExpression(
"md5",
args,
nullable: true,
argumentsPropagateNullability: new[] { false },
type: args[0].Type,
typeMapping: args[0].TypeMapping
)
);
base.OnModelCreating(modelBuilder);
}