Skip to content

Commit 17d980e

Browse files
authored
Merge pull request #138 from nhibernate/feature/postgis-3
Add spatial dialect for PostGIS 3.0+
2 parents f8024d5 + 9d73ede commit 17d980e

19 files changed

+310
-9
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ jobs:
5454
DB_INIT: docker run -d -e POSTGRES_PASSWORD=nhsp_test -p 15432:5432 -v ./Tests.NHibernate.Spatial.PostGis20/initdb:/docker-entrypoint-initdb.d postgis/postgis:12-2.5
5555
TEST_PROJECT: Tests.NHibernate.Spatial.PostGis20
5656

57-
- DB: PostGis20 (PostgreSQL 12 PostGIS 3.4)
58-
DB_INIT: docker run -d -e POSTGRES_PASSWORD=nhsp_test -p 15432:5432 -v ./Tests.NHibernate.Spatial.PostGis20/initdb:/docker-entrypoint-initdb.d postgis/postgis:12-3.4
59-
TEST_PROJECT: Tests.NHibernate.Spatial.PostGis20
57+
- DB: PostGis30 (PostgreSQL 12 PostGIS 3.0)
58+
DB_INIT: docker run -d -e POSTGRES_PASSWORD=nhsp_test -p 15433:5432 -v ./Tests.NHibernate.Spatial.PostGis30/initdb:/docker-entrypoint-initdb.d postgis/postgis:12-3.0
59+
TEST_PROJECT: Tests.NHibernate.Spatial.PostGis30
6060

61-
- DB: PostGis20 (PostgreSQL 16 PostGIS 3.4)
62-
DB_INIT: docker run -d -e POSTGRES_PASSWORD=nhsp_test -p 15432:5432 -v ./Tests.NHibernate.Spatial.PostGis20/initdb:/docker-entrypoint-initdb.d postgis/postgis:16-3.4
63-
TEST_PROJECT: Tests.NHibernate.Spatial.PostGis20
61+
- DB: PostGis30 (PostgreSQL 16 PostGIS 3.4)
62+
DB_INIT: docker run -d -e POSTGRES_PASSWORD=nhsp_test -p 15433:5432 -v ./Tests.NHibernate.Spatial.PostGis30/initdb:/docker-entrypoint-initdb.d postgis/postgis:16-3.4
63+
TEST_PROJECT: Tests.NHibernate.Spatial.PostGis30
6464

6565
steps:
6666
- name: Checkout repository

NHibernate.Spatial.PostGis/Dialect/PostGis20Dialect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public SqlString GetSpatialAggregateString(object geometry, SpatialAggregate agg
282282
return builder.ToSqlString();
283283
}
284284

285-
public SqlString GetSpatialRelationString(object geometry, SpatialRelation relation, object anotherGeometry, bool criterion)
285+
public virtual SqlString GetSpatialRelationString(object geometry, SpatialRelation relation, object anotherGeometry, bool criterion)
286286
{
287287
switch (relation)
288288
{
@@ -428,7 +428,7 @@ public SqlString GetSpatialFilterString(string tableAlias, string geometryColumn
428428
/// <param name="analysis">The analysis.</param>
429429
/// <param name="extraArgument">The extra argument.</param>
430430
/// <returns></returns>
431-
public SqlString GetSpatialAnalysisString(object geometry, SpatialAnalysis analysis, object extraArgument)
431+
public virtual SqlString GetSpatialAnalysisString(object geometry, SpatialAnalysis analysis, object extraArgument)
432432
{
433433
switch (analysis)
434434
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using NHibernate.SqlCommand;
2+
using System;
3+
4+
namespace NHibernate.Spatial.Dialect
5+
{
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public class PostGis30Dialect : PostGis20Dialect
10+
{
11+
public override SqlString GetSpatialRelationString(object geometry, SpatialRelation relation, object anotherGeometry, bool criterion)
12+
{
13+
switch (relation)
14+
{
15+
case SpatialRelation.Covers:
16+
case SpatialRelation.CoveredBy:
17+
case SpatialRelation.EqualsExact:
18+
return base.GetSpatialRelationString(geometry, relation, anotherGeometry, criterion);
19+
20+
default:
21+
return new SqlStringBuilder(6)
22+
.Add(SpatialDialect.IsoPrefix)
23+
.Add(relation.ToString())
24+
.Add("(")
25+
.AddObject(geometry)
26+
.Add(", ")
27+
.AddObject(anotherGeometry)
28+
.Add(")")
29+
.ToSqlString();
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Gets the spatial analysis string.
35+
/// </summary>
36+
/// <param name="geometry">The geometry.</param>
37+
/// <param name="analysis">The analysis.</param>
38+
/// <param name="extraArgument">The extra argument.</param>
39+
/// <returns></returns>
40+
public override SqlString GetSpatialAnalysisString(object geometry, SpatialAnalysis analysis, object extraArgument)
41+
{
42+
switch (analysis)
43+
{
44+
case SpatialAnalysis.Buffer:
45+
case SpatialAnalysis.ConvexHull:
46+
return base.GetSpatialAnalysisString(geometry, analysis, extraArgument);
47+
48+
case SpatialAnalysis.Difference:
49+
case SpatialAnalysis.Distance:
50+
case SpatialAnalysis.Intersection:
51+
case SpatialAnalysis.SymDifference:
52+
case SpatialAnalysis.Union:
53+
return new SqlStringBuilder()
54+
.Add(SpatialDialect.IsoPrefix)
55+
.Add(analysis.ToString())
56+
.Add("(")
57+
.AddObject(geometry)
58+
.Add(",")
59+
.AddObject(extraArgument)
60+
.Add(")")
61+
.ToSqlString();
62+
63+
default:
64+
throw new ArgumentException("Invalid spatial analysis argument");
65+
}
66+
}
67+
}
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
4+
5+
<class name="NHibernate.Spatial.Metadata.GeometryColumn, NHibernate.Spatial"
6+
schema="public"
7+
table="GEOMETRY_COLUMNS"
8+
lazy="false"
9+
mutable="false">
10+
<composite-id>
11+
<key-property name="TableCatalog" column="F_TABLE_CATALOG" />
12+
<key-property name="TableSchema" column="F_TABLE_SCHEMA" />
13+
<key-property name="TableName" column="F_TABLE_NAME" />
14+
<key-property name="Name" column="F_GEOMETRY_COLUMN" />
15+
</composite-id>
16+
<property name="SRID" column="SRID" />
17+
<property name="Subtype" column="TYPE" />
18+
<property name="Dimension" column="COORD_DIMENSION" />
19+
</class>
20+
</hibernate-mapping>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
4+
5+
<class name="NHibernate.Spatial.Metadata.SpatialReferenceSystem, NHibernate.Spatial"
6+
schema="public"
7+
table="SPATIAL_REF_SYS"
8+
lazy="false"
9+
mutable="true">
10+
<id name="SRID" column="SRID" type="Int32">
11+
<generator class="assigned" />
12+
</id>
13+
<property name="AuthorityName" column="AUTH_NAME" type="String" />
14+
<property name="AuthoritySRID" column="AUTH_SRID" type="Int32" />
15+
<property name="WellKnownText" column="SRTEXT" type="String" />
16+
</class>
17+
</hibernate-mapping>

NHibernate.Spatial.PostGis/NHibernate.Spatial.PostGis.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515

1616
<ItemGroup>
1717
<None Remove="Metadata\GeometryColumn.PostGis20Dialect.hbm.xml" />
18+
<None Remove="Metadata\GeometryColumn.PostGis30Dialect.hbm.xml" />
1819
<None Remove="Metadata\SpatialReferenceSystem.PostGis20Dialect.hbm.xml" />
20+
<None Remove="Metadata\SpatialReferenceSystem.PostGis30Dialect.hbm.xml" />
1921
</ItemGroup>
2022

2123
<ItemGroup>
24+
<EmbeddedResource Include="Metadata\GeometryColumn.PostGis30Dialect.hbm.xml" />
2225
<EmbeddedResource Include="Metadata\GeometryColumn.PostGis20Dialect.hbm.xml" />
26+
<EmbeddedResource Include="Metadata\SpatialReferenceSystem.PostGis30Dialect.hbm.xml" />
2327
<EmbeddedResource Include="Metadata\SpatialReferenceSystem.PostGis20Dialect.hbm.xml" />
2428
</ItemGroup>
2529

NHibernate.Spatial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2828
EndProject
2929
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.NHibernate.Spatial.MySQL80", "Tests.NHibernate.Spatial.MySQL80\Tests.NHibernate.Spatial.MySQL80.csproj", "{D6643E3E-D004-41A7-B1AB-96D52018FE17}"
3030
EndProject
31+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.NHibernate.Spatial.PostGis30", "Tests.NHibernate.Spatial.PostGis30\Tests.NHibernate.Spatial.PostGis30.csproj", "{ED1D3C35-D6F7-44CD-88A6-8E9192F685A8}"
32+
EndProject
3133
Global
3234
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3335
Debug|Any CPU = Debug|Any CPU
@@ -74,6 +76,10 @@ Global
7476
{D6643E3E-D004-41A7-B1AB-96D52018FE17}.Debug|Any CPU.Build.0 = Debug|Any CPU
7577
{D6643E3E-D004-41A7-B1AB-96D52018FE17}.Release|Any CPU.ActiveCfg = Release|Any CPU
7678
{D6643E3E-D004-41A7-B1AB-96D52018FE17}.Release|Any CPU.Build.0 = Release|Any CPU
79+
{ED1D3C35-D6F7-44CD-88A6-8E9192F685A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
80+
{ED1D3C35-D6F7-44CD-88A6-8E9192F685A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
81+
{ED1D3C35-D6F7-44CD-88A6-8E9192F685A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
82+
{ED1D3C35-D6F7-44CD-88A6-8E9192F685A8}.Release|Any CPU.Build.0 = Release|Any CPU
7783
EndGlobalSection
7884
GlobalSection(SolutionProperties) = preSolution
7985
HideSolutionNode = FALSE

Tests.NHibernate.Spatial.PostGis20/PostGis20ConformanceItemsFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Tests.NHibernate.Spatial
1111
public class PostGis20ConformanceItemsFixture : ConformanceItemsFixture
1212
{
1313
/// <summary>
14-
/// Overridden because GeometryType includes ISO prefix in PostGIS 2
14+
/// Overridden because GeometryType includes ISO prefix in PostGIS
1515
/// </summary>
1616
[Test]
1717
public override void ConformanceItemT07Hql()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using NHibernate.Cfg;
2+
using NUnit.Framework;
3+
4+
namespace Tests.NHibernate.Spatial
5+
{
6+
[TestFixture]
7+
public class PostGis30ConformanceItemsFixture : PostGis20ConformanceItemsFixture
8+
{
9+
protected override void Configure(Configuration configuration)
10+
{
11+
TestConfiguration.Configure(configuration);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using NHibernate.Cfg;
2+
using NUnit.Framework;
3+
4+
namespace Tests.NHibernate.Spatial
5+
{
6+
[TestFixture]
7+
public class PostGis30CriteriaFixture : CriteriaFixture
8+
{
9+
protected override void Configure(Configuration configuration)
10+
{
11+
TestConfiguration.Configure(configuration);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)