/// <summary> /// Sets up JSON.NET mappings for the PostgreSQL json and jsonb types. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> /// <param name="jsonbClrTypes">A list of CLR types to map to PostgreSQL jsonb (no need to specify NpgsqlDbType.Jsonb)</param> /// <param name="jsonClrTypes">A list of CLR types to map to PostgreSQL json (no need to specify NpgsqlDbType.Json)</param> /// <param name="settings">Optional settings to customize JSON serialization</param> public static INpgsqlTypeMapper UseJsonNet( this INpgsqlTypeMapper mapper, Type[]?jsonbClrTypes = null, Type[]?jsonClrTypes = null, JsonSerializerSettings?settings = null ) { mapper.AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "jsonb", NpgsqlDbType = NpgsqlDbType.Jsonb, ClrTypes = jsonbClrTypes, TypeHandlerFactory = new JsonbHandlerFactory(settings) }.Build()); mapper.AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "json", NpgsqlDbType = NpgsqlDbType.Json, ClrTypes = jsonClrTypes, TypeHandlerFactory = new JsonHandlerFactory(settings) }.Build()); return(mapper); }
/// <summary> /// Sets up the legacy PostGIS types to an Npgsql type mapper. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> public static INpgsqlTypeMapper UseLegacyPostgis(this INpgsqlTypeMapper mapper) { var typeHandlerFactory = new LegacyPostgisHandlerFactory(); return(mapper .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geometry", NpgsqlDbType = NpgsqlDbType.Geometry, ClrTypes = new[] { typeof(PostgisGeometry), typeof(PostgisPoint), typeof(PostgisMultiPoint), typeof(PostgisLineString), typeof(PostgisMultiLineString), typeof(PostgisPolygon), typeof(PostgisMultiPolygon), typeof(PostgisGeometryCollection), }, TypeHandlerFactory = typeHandlerFactory }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geography", NpgsqlDbType = NpgsqlDbType.Geography, DbTypes = new DbType[0], ClrTypes = new Type[0], InferredDbType = DbType.Object, TypeHandlerFactory = typeHandlerFactory }.Build())); }
/// <summary> /// Sets up the legacy PostGIS types to an Npgsql type mapper. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> public static INpgsqlTypeMapper UseRawPostgis(this INpgsqlTypeMapper mapper) => mapper .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geometry", NpgsqlDbType = NpgsqlDbType.Geometry, TypeHandlerFactory = new PostgisRawHandlerFactory() }.Build());
/// <summary> /// Maps the CrateObjectHandler to the json database type. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> public static INpgsqlTypeMapper UseCrateDbObjectHandler(this INpgsqlTypeMapper mapper) { // Map CrateDb-specific json-Handler. mapper.AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "json", NpgsqlDbType = NpgsqlDbType.Json, TypeHandlerFactory = new CrateDbObjectHandlerFactory() } .Build()); return(mapper); }
/// <summary> /// Sets up NodaTime mappings for the PostgreSQL date/time types. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> public static INpgsqlTypeMapper UseNodaTime(this INpgsqlTypeMapper mapper) => mapper .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "timestamp without time zone", NpgsqlDbType = NpgsqlDbType.Timestamp, DbTypes = new[] { DbType.DateTime, DbType.DateTime2 }, ClrTypes = new[] { typeof(Instant), typeof(LocalDateTime) }, InferredDbType = DbType.DateTime, TypeHandlerFactory = new TimestampHandlerFactory() }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "timestamp with time zone", NpgsqlDbType = NpgsqlDbType.TimestampTz, ClrTypes = new[] { typeof(ZonedDateTime), typeof(OffsetDateTime) }, TypeHandlerFactory = new TimestampTzHandlerFactory() }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "date", NpgsqlDbType = NpgsqlDbType.Date, DbTypes = new[] { DbType.Date }, ClrTypes = new[] { typeof(LocalDate) }, TypeHandlerFactory = new DateHandlerFactory() }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "time without time zone", NpgsqlDbType = NpgsqlDbType.Time, DbTypes = new[] { DbType.Time }, ClrTypes = new[] { typeof(LocalTime) }, TypeHandlerFactory = new TimeHandlerFactory() }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "time with time zone", NpgsqlDbType = NpgsqlDbType.TimeTz, ClrTypes = new[] { typeof(OffsetTime) }, TypeHandlerFactory = new TimeTzHandlerFactory() }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "interval", NpgsqlDbType = NpgsqlDbType.Interval, ClrTypes = new[] { typeof(Period) }, TypeHandlerFactory = new IntervalHandlerFactory() }.Build());
/// <summary> /// Sets up NetTopologySuite mappings for the PostGIS types. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific).</param> /// <param name="coordinateSequenceFactory">The factory which knows how to build a particular implementation of ICoordinateSequence from an array of Coordinates.</param> /// <param name="precisionModel">Specifies the grid of allowable points.</param> /// <param name="handleOrdinates">Specifies the ordinates which will be handled. Not specified ordinates will be ignored. /// If <see cref="F:GeoAPI.Geometries.Ordiantes.None" /> is specified, an actual value will be taken from /// the <see cref="P:GeoAPI.Geometries.ICoordinateSequenceFactory.Ordinates"/> property of <paramref name="coordinateSequenceFactory"/>.</param> /// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param> public static INpgsqlTypeMapper UseNetTopologySuite( this INpgsqlTypeMapper mapper, ICoordinateSequenceFactory coordinateSequenceFactory = null, IPrecisionModel precisionModel = null, Ordinates handleOrdinates = Ordinates.None, bool geographyAsDefault = false) { if (coordinateSequenceFactory == null) { coordinateSequenceFactory = GeometryServiceProvider.Instance.DefaultCoordinateSequenceFactory; } if (precisionModel == null) { precisionModel = GeometryServiceProvider.Instance.DefaultPrecisionModel; } if (handleOrdinates == Ordinates.None) { handleOrdinates = coordinateSequenceFactory.Ordinates; } NetTopologySuiteBootstrapper.Bootstrap(); var typeHandlerFactory = new NetTopologySuiteHandlerFactory( new PostGisReader(coordinateSequenceFactory, precisionModel, handleOrdinates), new PostGisWriter() { HandleOrdinates = handleOrdinates }); return(mapper .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geometry", NpgsqlDbType = NpgsqlDbType.Geometry, ClrTypes = geographyAsDefault ? Type.EmptyTypes : ClrTypes, InferredDbType = DbType.Object, TypeHandlerFactory = typeHandlerFactory }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geography", NpgsqlDbType = NpgsqlDbType.Geography, ClrTypes = geographyAsDefault ? ClrTypes : Type.EmptyTypes, InferredDbType = DbType.Object, TypeHandlerFactory = typeHandlerFactory }.Build())); }
/// <summary> /// Sets up GeoJSON mappings for the PostGIS types. /// </summary> /// <param name="mapper">The type mapper to set up (global or connection-specific)</param> /// <param name="options">Options to use when constructing objects.</param> /// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param> public static INpgsqlTypeMapper UseGeoJson(this INpgsqlTypeMapper mapper, GeoJSONOptions options = GeoJSONOptions.None, bool geographyAsDefault = false) { var factory = new GeoJSONHandlerFactory(options); return(mapper .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geometry", NpgsqlDbType = NpgsqlDbType.Geometry, ClrTypes = geographyAsDefault ? Type.EmptyTypes : ClrTypes, InferredDbType = DbType.Object, TypeHandlerFactory = factory }.Build()) .AddMapping(new NpgsqlTypeMappingBuilder { PgTypeName = "geography", NpgsqlDbType = NpgsqlDbType.Geography, ClrTypes = geographyAsDefault ? ClrTypes : Type.EmptyTypes, InferredDbType = DbType.Object, TypeHandlerFactory = factory }.Build())); }