-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathColumnMetadata.cs
More file actions
70 lines (50 loc) · 2.07 KB
/
ColumnMetadata.cs
File metadata and controls
70 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Dialect;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata;
internal sealed class ColumnMetadata(IProperty property, SqlDialectBuilder dialect)
{
private readonly Func<object, object?> _getter = BuildGetter(property);
public IProperty Property { get; } = property;
public string PropertyName { get; } = property.Name;
public string ColumnName { get; } = property.GetColumnName();
public string QuotedColumName { get; } = dialect.Quote(property.GetColumnName());
public string StoreDefinition { get; } = GetStoreDefinition(property);
public Type ClrType { get; } = property.ClrType;
public bool IsGenerated { get; } = property.ValueGenerated != ValueGenerated.Never;
public object? GetValue(object entity, BulkInsertOptions options)
{
var result = _getter(entity);
if (options.Converters != null && result != null)
{
foreach (var converter in options.Converters)
{
if (converter.TryConvertValue(result, options, out var temp))
{
result = temp;
break;
}
}
}
return result;
}
private static Func<object, object?> BuildGetter(IProperty property)
{
var valueConverter =
property.GetValueConverter() ??
property.GetTypeMapping().Converter;
var propInfo = property.PropertyInfo!;
return PropertyAccessor.CreateGetter(propInfo, valueConverter?.ConvertToProviderExpression);
}
private static string GetStoreDefinition(IProperty property)
{
var typeMapping = property.GetRelationalTypeMapping();
var nullability = property.IsNullable ? "NULL" : "NOT NULL";
return $"{typeMapping.StoreType} {nullability}";
}
public override string ToString()
{
return $"Name: {PropertyName}, Column: {ColumnName}";
}
}