DD.WellWorkover.Cloud/AsbCloudDb/EFExtentions.cs

277 lines
10 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudDb
{
public static class EFExtentions
{
2022-07-04 17:50:16 +05:00
private static readonly System.Text.Json.JsonSerializerOptions jsonSerializerOptions = new()
{
AllowTrailingCommas = true,
WriteIndented = true,
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString |
System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals,
};
public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<TProperty> HasJsonConversion<TProperty>(
this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<TProperty> builder)
=> HasJsonConversion(builder, jsonSerializerOptions);
public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<TProperty> HasJsonConversion<TProperty>(
this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder<TProperty> builder,
System.Text.Json.JsonSerializerOptions jsonSerializerOptions)
{
builder.HasConversion(
s => System.Text.Json.JsonSerializer.Serialize(s, jsonSerializerOptions),
s => System.Text.Json.JsonSerializer.Deserialize<TProperty>(s, jsonSerializerOptions)!);
ValueComparer<TProperty> valueComparer = new (
2022-07-04 17:33:32 +05:00
(a,b) =>
(a!=null) && (b != null)
? a.GetHashCode() == b.GetHashCode()
: (a == null) && (b == null),
i => (i == null) ?-1 : i.GetHashCode(),
i => i);
builder.Metadata.SetValueComparer(valueComparer);
return builder;
}
2021-12-08 15:29:41 +05:00
static Dictionary<Type, IQueryStringFactory> QueryFactories { get; set; } = new();
2021-12-08 15:29:41 +05:00
static QueryStringFactory<T> GetQueryStringFactory<T>(DbSet<T> dbSet)
where T : class
{
var t = typeof(T);
var factory = (QueryStringFactory<T>?)QueryFactories.GetValueOrDefault(t);
if (factory is null)
{
2021-12-08 15:29:41 +05:00
factory = new QueryStringFactory<T>(dbSet);
QueryFactories.Add(t, factory);
}
2022-04-11 18:00:34 +05:00
return factory;
}
2021-12-08 15:29:41 +05:00
public static Task<int> ExecInsertOrUpdateAsync<T>(this Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade database, DbSet<T> dbSet, IEnumerable<T> items, CancellationToken token)
where T : class
{
2021-12-08 15:29:41 +05:00
var factory = GetQueryStringFactory(dbSet);
var query = factory.MakeInsertOrUpdateSql(items);
2022-07-19 10:29:38 +05:00
return database.ExecuteSqlRawAsync(query, token);
}
2021-12-17 12:48:58 +05:00
public static Task<int> ExecInsertOrIgnoreAsync<T>(this Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade database, DbSet<T> dbSet, IEnumerable<T> items, CancellationToken token)
where T : class
{
var factory = GetQueryStringFactory(dbSet);
var query = factory.MakeInsertOrIgnoreSql(items);
return database.ExecuteSqlRawAsync(query, token);
}
2022-10-10 12:39:54 +05:00
public static Task<int> ExecInsertAsync<T>(this Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade database, DbSet<T> dbSet, IEnumerable<T> items, CancellationToken token)
where T : class
{
var factory = GetQueryStringFactory(dbSet);
var query = factory.MakeInsertSql(items);
return database.ExecuteSqlRawAsync(query, token);
}
2021-12-17 12:48:58 +05:00
public static string GetTableName<T>(this DbSet<T> dbSet)
where T : class
{
var factory = GetQueryStringFactory(dbSet);
return factory.TableName;
}
public static IEnumerable<string> GetColumnsNames<T>(this DbSet<T> dbSet)
where T : class
{
var factory = GetQueryStringFactory(dbSet);
return factory.Columns;
}
public static Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<T> Upsert<T>(this DbSet<T> dbSet, T value)
where T : class
{
return dbSet.Contains(value)
? dbSet.Update(value)
2022-06-15 14:57:37 +05:00
: dbSet.Add(value);
}
public static (int updated, int inserted) UpsertRange<T>(this DbSet<T> dbSet, IEnumerable<T> values)
where T : class
{
2022-06-15 14:57:37 +05:00
(int updated, int inserted) stat = (0, 0);
foreach (var value in values)
if (dbSet.Contains(value))
{
stat.updated++;
dbSet.Update(value);
}
else
{
stat.inserted++;
dbSet.Add(value);
}
return stat;
}
2022-10-17 14:42:47 +05:00
public static IQueryable<T> SkipTake<T>(this IQueryable<T> query, int? skip, int? take)
{
if (skip > 0)
query = query.Skip((int)skip);
if (take > 0)
query = query.Take((int)take);
return query;
}
}
2022-04-11 18:00:34 +05:00
interface IQueryStringFactory { }
class QueryStringFactory<T> : IQueryStringFactory
where T : class
{
private readonly string insertHeader;
private readonly string pk;
private readonly string conflictBody;
2021-11-15 16:52:12 +05:00
private readonly IEnumerable<IClrPropertyGetter> getters;
2021-12-17 12:48:58 +05:00
public string TableName { get; }
public IEnumerable<string> Columns { get; }
public QueryStringFactory(DbSet<T> dbset)
{
2021-12-08 15:29:41 +05:00
var properties = dbset.EntityType.GetProperties();
var pkColsNames = dbset.EntityType.FindPrimaryKey()?.Properties.Select(p => p.GetColumnBaseName());
pk = pkColsNames is null ? string.Empty : $"({string.Join(", ", pkColsNames)})";
TableName = dbset.EntityType.GetTableName()!;
2022-07-19 10:29:38 +05:00
getters = properties
.Where(p => !p.IsShadowProperty())
.Select(p => p.GetGetter()).ToList();
2021-12-17 12:48:58 +05:00
Columns = properties.Select(p => $"\"{p.GetColumnBaseName()}\"");
var colunmsString = $"({string.Join(", ", Columns)})";
2021-12-17 12:48:58 +05:00
insertHeader = $"INSERT INTO {TableName} {colunmsString} VALUES ";
var excludedUpdateSet = string.Join(", ", Columns.Select(n => $"{n} = excluded.{n}"));
conflictBody = $" ON CONFLICT {pk} DO UPDATE SET {excludedUpdateSet};";
}
public string MakeInsertOrIgnoreSql(IEnumerable<T> items)
{
var builder = new StringBuilder(insertHeader, 7);
BuildRows(builder, items);
builder.Append(" ON CONFLICT DO NOTHING;");
return builder.ToString();
}
public string MakeInsertOrUpdateSql(IEnumerable<T> items)
{
var builder = new StringBuilder(insertHeader, 7);
BuildRows(builder, items);
if (string.IsNullOrEmpty(pk))
builder.Append(" ON CONFLICT DO NOTHING;");
else
builder.AppendLine(conflictBody);
return builder.ToString();
}
2022-10-10 12:39:54 +05:00
public string MakeInsertSql(IEnumerable<T> items)
{
var builder = new StringBuilder(insertHeader, 7);
BuildRows(builder, items);
builder.Append(';');
return builder.ToString();
}
private StringBuilder BuildRows(StringBuilder builder, IEnumerable<T> items)
{
var list = items.ToList();
2021-12-08 15:29:41 +05:00
for (var i = 0; i < list.Count; i++)
{
2022-04-11 18:00:34 +05:00
if (i > 0)
builder.Append(',');
BuildRow(builder, list[i]);
2022-04-11 18:00:34 +05:00
}
return builder;
}
private StringBuilder BuildRow(StringBuilder builder, T item)
{
var values = getters.Select(getter => FormatValue(getter.GetClrValue(item)));
builder.Append('(');
builder.AppendJoin(",", values);
builder.Append(')');
return builder;
}
private static string FormatValue(object? v)
=> v switch
2022-04-11 18:00:34 +05:00
{
2022-07-19 10:29:38 +05:00
string vStr => $"'{EscapeCurlyBraces(vStr)}'",
2022-04-11 18:00:34 +05:00
DateTime vDate => $"'{FormatDateValue(vDate)}'",
DateTimeOffset vDate => $"'{FormatDateValue(vDate.UtcDateTime)}'",
IFormattable vFormattable => FormatFormattableValue(vFormattable),
_ => System.Text.Json.JsonSerializer.Serialize(v),
};
2022-07-19 10:29:38 +05:00
private static string EscapeCurlyBraces(string vStr)
{
var result = vStr
.Replace("{", "{{")
.Replace("}", "}}");
return result;
}
private static string FormatFormattableValue(IFormattable v)
=> v switch
{
double vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
float vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
decimal vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
int vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
short vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
uint vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
ushort vt => vt.ToString(System.Globalization.CultureInfo.InvariantCulture),
_ => v.ToString(null, System.Globalization.CultureInfo.InvariantCulture),
};
private static string FormatDateValue(DateTime vDate)
{
if (vDate.Kind == DateTimeKind.Unspecified)
vDate = DateTime.SpecifyKind(vDate, DateTimeKind.Utc);
return vDate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.ffffff zzz");
}
}
public class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.FromDateTime(reader.GetDateTime());
}
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
var isoDate = value.ToString("O");
writer.WriteStringValue(isoDate);
}
}
}