44using Npgsql . EntityFrameworkCore . PostgreSQL . Infrastructure ;
55using Npgsql . EntityFrameworkCore . PostgreSQL . Query . ExpressionTranslators . Internal ;
66using Npgsql . EntityFrameworkCore . PostgreSQL . Storage . Internal ;
7+ using Npgsql . EntityFrameworkCore . PostgreSQL . Storage . Internal . Mapping ;
8+ using NpgsqlTypes ;
79
810namespace Npgsql . EntityFrameworkCore . PostgreSQL . NodaTime
911{
@@ -24,6 +26,29 @@ public class NodaTimePlugin : NpgsqlEntityFrameworkPlugin
2426 readonly TimeTzMapping _timetz = new TimeTzMapping ( ) ;
2527 readonly IntervalMapping _period = new IntervalMapping ( ) ;
2628
29+ readonly NpgsqlRangeTypeMapping < LocalDateTime > _timestampLocalDateTimeRange ;
30+ readonly NpgsqlRangeTypeMapping < Instant > _timestampInstantRange ;
31+ readonly NpgsqlRangeTypeMapping < Instant > _timestamptzInstantRange ;
32+ readonly NpgsqlRangeTypeMapping < ZonedDateTime > _timestamptzZonedDateTimeRange ;
33+ readonly NpgsqlRangeTypeMapping < OffsetDateTime > _timestamptzOffsetDateTimeRange ;
34+ readonly NpgsqlRangeTypeMapping < LocalDate > _dateRange ;
35+
36+ public NodaTimePlugin ( )
37+ {
38+ _timestampLocalDateTimeRange = new NpgsqlRangeTypeMapping < LocalDateTime > (
39+ "tsrange" , typeof ( NpgsqlRange < LocalDateTime > ) , _timestampLocalDateTime , NpgsqlDbType . Range | NpgsqlDbType . Timestamp ) ;
40+ _timestampInstantRange = new NpgsqlRangeTypeMapping < Instant > (
41+ "tsrange" , typeof ( NpgsqlRange < Instant > ) , _timestampInstant , NpgsqlDbType . Range | NpgsqlDbType . Timestamp ) ;
42+ _timestamptzInstantRange = new NpgsqlRangeTypeMapping < Instant > (
43+ "tstzrange" , typeof ( NpgsqlRange < Instant > ) , _timestamptzInstant , NpgsqlDbType . Range | NpgsqlDbType . TimestampTz ) ;
44+ _timestamptzZonedDateTimeRange = new NpgsqlRangeTypeMapping < ZonedDateTime > (
45+ "tstzrange" , typeof ( NpgsqlRange < ZonedDateTime > ) , _timestamptzZonedDateTime , NpgsqlDbType . Range | NpgsqlDbType . TimestampTz ) ;
46+ _timestamptzOffsetDateTimeRange = new NpgsqlRangeTypeMapping < OffsetDateTime > (
47+ "tstzrange" , typeof ( NpgsqlRange < OffsetDateTime > ) , _timestamptzOffsetDateTime , NpgsqlDbType . Range | NpgsqlDbType . TimestampTz ) ;
48+ _dateRange = new NpgsqlRangeTypeMapping < LocalDate > (
49+ "daterange" , typeof ( NpgsqlRange < LocalDate > ) , _date , NpgsqlDbType . Range | NpgsqlDbType . Date ) ; ;
50+ }
51+
2752 public override void AddMappings ( NpgsqlTypeMappingSource typeMappingSource )
2853 {
2954 typeMappingSource . ClrTypeMappings [ typeof ( Instant ) ] = _timestampInstant ;
@@ -38,7 +63,7 @@ public override void AddMappings(NpgsqlTypeMappingSource typeMappingSource)
3863
3964 typeMappingSource . StoreTypeMappings [ "timestamptz" ] =
4065 typeMappingSource . StoreTypeMappings [ "timestamp with time zone" ] =
41- new RelationalTypeMapping [ ] { _timestamptzInstant , _timestamptzOffsetDateTime , _timestamptzZonedDateTime } ;
66+ new RelationalTypeMapping [ ] { _timestamptzInstant , _timestamptzZonedDateTime , _timestamptzOffsetDateTime } ;
4267
4368 typeMappingSource . ClrTypeMappings [ typeof ( LocalDate ) ] = _date ;
4469 typeMappingSource . StoreTypeMappings [ "date" ] = new RelationalTypeMapping [ ] { _date } ;
@@ -51,6 +76,22 @@ public override void AddMappings(NpgsqlTypeMappingSource typeMappingSource)
5176
5277 typeMappingSource . ClrTypeMappings [ typeof ( Period ) ] = _period ;
5378 typeMappingSource . StoreTypeMappings [ "interval" ] = new RelationalTypeMapping [ ] { _period } ;
79+
80+ // Ranges
81+ typeMappingSource . ClrTypeMappings [ typeof ( NpgsqlRange < Instant > ) ] = _timestampInstantRange ;
82+ typeMappingSource . ClrTypeMappings [ typeof ( NpgsqlRange < LocalDateTime > ) ] = _timestampLocalDateTimeRange ;
83+
84+ typeMappingSource . StoreTypeMappings [ "tsrange" ] =
85+ new RelationalTypeMapping [ ] { _timestampInstantRange , _timestampLocalDateTimeRange } ;
86+
87+ typeMappingSource . ClrTypeMappings [ typeof ( NpgsqlRange < ZonedDateTime > ) ] = _timestamptzZonedDateTimeRange ;
88+ typeMappingSource . ClrTypeMappings [ typeof ( NpgsqlRange < OffsetDateTime > ) ] = _timestamptzOffsetDateTimeRange ;
89+
90+ typeMappingSource . StoreTypeMappings [ "tstzrange" ] =
91+ new RelationalTypeMapping [ ] { _timestamptzInstantRange , _timestamptzZonedDateTimeRange , _timestamptzOffsetDateTimeRange } ;
92+
93+ typeMappingSource . ClrTypeMappings [ typeof ( NpgsqlRange < LocalDate > ) ] = _dateRange ;
94+ typeMappingSource . StoreTypeMappings [ "daterange" ] = new RelationalTypeMapping [ ] { _dateRange } ;
5495 }
5596
5697 static readonly IMemberTranslator [ ] MemberTranslators =
0 commit comments