2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
internal class QueryPatchWrapper
|
|
|
|
{
|
|
|
|
private readonly Dictionary<string, object> _dict = new();
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public QueryPatchWrapper With<T>(string columnName, Partial<T> partialValue)
|
|
|
|
{
|
|
|
|
if (partialValue.IsPresent)
|
|
|
|
_dict.Add(columnName, partialValue);
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return this;
|
2021-09-30 01:51:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:26:54 +00:00
|
|
|
public Query ToQuery(Query q)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return q.AsUpdate(_dict);
|
|
|
|
}
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
{
|
|
|
|
throw new InvalidPatchException();
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static class SqlKataExtensions
|
|
|
|
{
|
|
|
|
internal static Query ApplyPatch(this Query query, Func<QueryPatchWrapper, QueryPatchWrapper> func)
|
|
|
|
=> func(new QueryPatchWrapper()).ToQuery(query);
|
2022-03-23 18:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class InvalidPatchException : Exception {}
|