forked from PhenX/PhenX.EntityFrameworkCore.BulkInsert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgreSqlGeometryConverter.cs
More file actions
49 lines (38 loc) · 1.2 KB
/
PostgreSqlGeometryConverter.cs
File metadata and controls
49 lines (38 loc) · 1.2 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
using Microsoft.EntityFrameworkCore.Metadata;
using NetTopologySuite.Geometries;
using NpgsqlTypes;
using PhenX.EntityFrameworkCore.BulkInsert.Abstractions;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert.PostgreSql;
internal sealed class PostgreSqlGeometryConverter : IBulkValueConverter, IPostgresTypeProvider
{
public static readonly PostgreSqlGeometryConverter Instance = new();
private PostgreSqlGeometryConverter()
{
}
public bool TryConvertValue(object source, BulkInsertOptions options, out object result)
{
if (source is Geometry geometry)
{
if (geometry.SRID != options.SRID)
{
geometry = geometry.Copy();
geometry.SRID = options.SRID;
}
result = geometry.ToBinary();
return true;
}
result = source;
return false;
}
public bool TryGetType(IProperty property, out NpgsqlDbType result)
{
if (property.ClrType.IsAssignableTo(typeof(Geometry)))
{
result = NpgsqlDbType.Bytea;
return true;
}
result = default;
return false;
}
}