|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Data; |
| 5 | +using GeoJSON.Net; |
| 6 | +using GeoJSON.Net.Geometry; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Npgsql.Internal; |
| 9 | +using Npgsql.Internal.TypeHandling; |
| 10 | +using Npgsql.PostgresTypes; |
| 11 | +using Npgsql.TypeMapping; |
| 12 | +using NpgsqlTypes; |
| 13 | + |
| 14 | +namespace Npgsql.GeoJSON.Internal |
| 15 | +{ |
| 16 | + public class GeoJSONTypeHandlerResolver : ITypeHandlerResolver |
| 17 | + { |
| 18 | + readonly NpgsqlDatabaseInfo _databaseInfo; |
| 19 | + readonly GeoJsonHandler _geometryHandler, _geographyHandler; |
| 20 | + readonly bool _geographyAsDefault; |
| 21 | + |
| 22 | + static readonly ConcurrentDictionary<string, CrsMap> CRSMaps = new(); |
| 23 | + |
| 24 | + internal GeoJSONTypeHandlerResolver(NpgsqlConnector connector, GeoJSONOptions options, bool geographyAsDefault) |
| 25 | + { |
| 26 | + _databaseInfo = connector.DatabaseInfo; |
| 27 | + _geographyAsDefault = geographyAsDefault; |
| 28 | + |
| 29 | + var crsMap = (options & (GeoJSONOptions.ShortCRS | GeoJSONOptions.LongCRS)) == GeoJSONOptions.None |
| 30 | + ? default : CRSMaps.GetOrAdd(connector.Settings.ConnectionString, _ => |
| 31 | + { |
| 32 | + var builder = new CrsMapBuilder(); |
| 33 | + using (var cmd = connector.CreateCommand( |
| 34 | + "SELECT min(srid), max(srid), auth_name " + |
| 35 | + "FROM(SELECT srid, auth_name, srid - rank() OVER(ORDER BY srid) AS range " + |
| 36 | + "FROM spatial_ref_sys) AS s GROUP BY range, auth_name ORDER BY 1;")) |
| 37 | + using (var reader = cmd.ExecuteReader()) |
| 38 | + while (reader.Read()) |
| 39 | + { |
| 40 | + builder.Add(new CrsMapEntry( |
| 41 | + reader.GetInt32(0), |
| 42 | + reader.GetInt32(1), |
| 43 | + reader.GetString(2))); |
| 44 | + } |
| 45 | + return builder.Build(); |
| 46 | + }); |
| 47 | + |
| 48 | + var (pgGeometryType, pgGeographyType) = (PgType("geometry"), PgType("geography")); |
| 49 | + |
| 50 | + _geometryHandler = new GeoJsonHandler(pgGeometryType, options, crsMap); |
| 51 | + _geographyHandler = new GeoJsonHandler(pgGeographyType, options, crsMap); |
| 52 | + } |
| 53 | + |
| 54 | + public NpgsqlTypeHandler? ResolveByDataTypeName(string typeName) |
| 55 | + => typeName switch |
| 56 | + { |
| 57 | + "geometry" => _geometryHandler, |
| 58 | + "geography" => _geographyHandler, |
| 59 | + _ => null |
| 60 | + }; |
| 61 | + |
| 62 | + public NpgsqlTypeHandler? ResolveByClrType(Type type) |
| 63 | + => ClrTypeToDataTypeName(type, _geographyAsDefault) is { } dataTypeName && ResolveByDataTypeName(dataTypeName) is { } handler |
| 64 | + ? handler |
| 65 | + : null; |
| 66 | + |
| 67 | + internal static string? ClrTypeToDataTypeName(Type type, bool geographyAsDefault) |
| 68 | + => type.BaseType != typeof(GeoJSONObject) |
| 69 | + ? null |
| 70 | + : geographyAsDefault |
| 71 | + ? "geography" |
| 72 | + : "geometry"; |
| 73 | + |
| 74 | + public TypeMappingInfo? GetMappingByDataTypeName(string dataTypeName) |
| 75 | + => DoGetMappingByDataTypeName(dataTypeName); |
| 76 | + |
| 77 | + internal static TypeMappingInfo? DoGetMappingByDataTypeName(string dataTypeName) |
| 78 | + => dataTypeName switch |
| 79 | + { |
| 80 | + "geometry" => new(NpgsqlDbType.Geometry, DbType.Object, "geometry"), |
| 81 | + "geography" => new(NpgsqlDbType.Geography, DbType.Object, "geography"), |
| 82 | + _ => null |
| 83 | + }; |
| 84 | + |
| 85 | + PostgresType PgType(string pgTypeName) => _databaseInfo.GetPostgresTypeByName(pgTypeName); |
| 86 | + } |
| 87 | +} |
0 commit comments