Skip to content

Commit 6af38e7

Browse files
committed
WIP
1 parent 901f5b1 commit 6af38e7

File tree

13 files changed

+183
-21
lines changed

13 files changed

+183
-21
lines changed

NHibernate.Spatial.MsSql/Dialect/MsSql2012FunctionRegistration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System;
2525
using System.Globalization;
2626
using System.Text;
27+
using NHibernate.Mapping;
2728

2829
namespace NHibernate.Spatial.Dialect
2930
{
@@ -575,13 +576,13 @@ DROP VIEW {0}{1}
575576
/// <param name="dimension">The dimension.</param>
576577
/// <param name="isNullable">Whether or not the column is nullable</param>
577578
/// <returns></returns>
578-
public string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
579+
public string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable)
579580
{
580581
var builder = new StringBuilder();
581582

582583
string quotedSchema = adaptor.QuoteSchema(schema);
583584
string quoteForTableName = adaptor.QuoteForTableName(table);
584-
string quoteForColumnName = adaptor.QuoteForColumnName(column);
585+
string quoteForColumnName = adaptor.QuoteForColumnName(column.Name);
585586

586587
builder.AppendFormat("ALTER TABLE {0}{1} DROP COLUMN {2}"
587588
, quotedSchema
@@ -607,7 +608,7 @@ public string GetSpatialCreateString(string schema, string table, string column,
607608
builder.AppendFormat("EXECUTE('ALTER TABLE {0}{1} WITH CHECK ADD CONSTRAINT {2} CHECK ({3}.{4} = {5})')"
608609
, quotedSchema
609610
, quoteForTableName
610-
, adaptor.Quote("CK_NHSP_" + table + "_" + column + "_SRID")
611+
, adaptor.Quote("CK_NHSP_" + table + "_" + column.Name + "_SRID")
611612
, quoteForColumnName
612613
, adaptor.Quote("STSrid")
613614
, srid
@@ -621,7 +622,7 @@ public string GetSpatialCreateString(string schema, string table, string column,
621622
builder.AppendFormat("ALTER TABLE {0}{1} WITH CHECK ADD CONSTRAINT {2} CHECK ({3}.{4}() = '{5}')"
622623
, quotedSchema
623624
, quoteForTableName
624-
, adaptor.Quote("CK_NHSP_" + table + "_" + column + "_TYPE")
625+
, adaptor.Quote("CK_NHSP_" + table + "_" + column.Name + "_TYPE")
625626
, quoteForColumnName
626627
, adaptor.Quote("STGeometryType")
627628
, subtype

NHibernate.Spatial.MsSql/Dialect/MsSql2012SpatialDialect.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717

1818
using NHibernate.Dialect;
19+
using NHibernate.Mapping;
1920
using NHibernate.Spatial.Metadata;
2021
using NHibernate.Spatial.Type;
2122
using NHibernate.SqlCommand;
@@ -91,7 +92,7 @@ public string GetSpatialCreateString(string schema)
9192
return worker.GetSpatialCreateString(schema);
9293
}
9394

94-
public string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
95+
public string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable)
9596
{
9697
return worker.GetSpatialCreateString(schema, table, column, srid, subtype, dimension, isNullable);
9798
}

NHibernate.Spatial.MySQL/Dialect/MySQL57SpatialDialect.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using NHibernate.Util;
2525
using System;
2626
using System.Text;
27+
using NHibernate.Mapping;
2728

2829
namespace NHibernate.Spatial.Dialect
2930
{
@@ -528,13 +529,13 @@ protected string QuoteSchema(string schema)
528529
/// <param name="dimension">The dimension.</param>
529530
/// <param name="isNullable">Whether or not the column is nullable</param>
530531
/// <returns></returns>
531-
public virtual string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
532+
public virtual string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable)
532533
{
533534
var builder = new StringBuilder();
534535

535536
string quotedSchema = QuoteSchema(schema);
536537
string quoteForTableName = QuoteForTableName(table);
537-
string quoteForColumnName = QuoteForColumnName(column);
538+
string quoteForColumnName = QuoteForColumnName(column.Name);
538539

539540
builder.AppendFormat("ALTER TABLE {0}{1} DROP COLUMN {2}"
540541
, quotedSchema

NHibernate.Spatial.MySQL/Dialect/MySQL80SpatialDialect.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Data;
55
using System.Text;
6+
using NHibernate.Mapping;
67

78
namespace NHibernate.Spatial.Dialect
89
{
@@ -104,13 +105,13 @@ public override SqlString GetSpatialAggregateString(object geometry, SpatialAggr
104105
/// <param name="dimension">The dimension.</param>
105106
/// <param name="isNullable">Whether or not the column is nullable</param>
106107
/// <returns></returns>
107-
public override string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
108+
public override string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable)
108109
{
109110
var builder = new StringBuilder();
110111

111112
string quotedSchema = QuoteSchema(schema);
112113
string quoteForTableName = QuoteForTableName(table);
113-
string quoteForColumnName = QuoteForColumnName(column);
114+
string quoteForColumnName = QuoteForColumnName(column.Name);
114115

115116
builder.AppendFormat("ALTER TABLE {0}{1} DROP COLUMN {2}"
116117
, quotedSchema

NHibernate.Spatial.PostGis/Dialect/PostGis20Dialect.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using NHibernate.Util;
2525
using System;
2626
using System.Text;
27+
using NHibernate.Mapping;
2728

2829
namespace NHibernate.Spatial.Dialect
2930
{
@@ -539,33 +540,46 @@ private string QuoteSchema(string schema)
539540
/// <param name="dimension">The dimension.</param>
540541
/// <param name="isNullable">Whether or not the column is nullable</param>
541542
/// <returns></returns>
542-
public string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
543+
public string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable)
543544
{
544545
var builder = new StringBuilder();
545546

546547
builder.AppendFormat("ALTER TABLE {0}{1} DROP COLUMN {2}"
547548
, QuoteSchema(schema)
548549
, QuoteForTableName(table)
549-
, QuoteForColumnName(column)
550+
, QuoteForColumnName(column.Name)
550551
);
551552

552553
builder.Append(MultipleQueriesSeparator);
553554

554-
builder.AppendFormat("SELECT AddGeometryColumn('{0}','{1}','{2}',{3},'{4}',{5})",
555-
schema, table, column, srid, subtype, dimension);
555+
if (column.Value.Type.Name == nameof(PostGisGeographyType))
556+
{
557+
builder.AppendFormat("ALTER TABLE {0}{1} ADD COLUMN {2} geography({3}, {4})"
558+
, QuoteSchema(schema)
559+
, QuoteForTableName(table)
560+
, QuoteForColumnName(column.Name)
561+
, subtype
562+
, srid
563+
);
564+
}
565+
else
566+
{
567+
builder.AppendFormat("SELECT AddGeometryColumn('{0}','{1}','{2}',{3},'{4}',{5})",
568+
schema, table, column.Name, srid, subtype, dimension);
569+
}
556570

557571
if (!isNullable)
558572
{
559573
builder.Append(MultipleQueriesSeparator);
560574
builder.AppendFormat("ALTER TABLE {0}{1} ALTER COLUMN {2} SET NOT NULL"
561575
, QuoteSchema(schema)
562576
, QuoteForTableName(table)
563-
, QuoteForColumnName(column)
577+
, QuoteForColumnName(column.Name)
564578
);
565579
}
566580

567581
builder.Append(MultipleQueriesSeparator);
568-
builder.Append(GetSpatialIndexCreateString(schema, table, column));
582+
builder.Append(GetSpatialIndexCreateString(schema, table, column.Name));
569583

570584
return builder.ToString();
571585
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2007 - Ricardo Stuven ([email protected])
2+
//
3+
// This file is part of NHibernate.Spatial.
4+
// NHibernate.Spatial is free software; you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation; either version 2 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// NHibernate.Spatial is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with NHibernate.Spatial; if not, write to the Free Software
16+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
18+
using System;
19+
using System.Data.Common;
20+
using NetTopologySuite.Geometries;
21+
using NHibernate.Engine;
22+
using NHibernate.SqlTypes;
23+
using NHibernate.Type;
24+
using Npgsql;
25+
using NpgsqlTypes;
26+
27+
namespace NHibernate.Spatial.Type
28+
{
29+
[Serializable]
30+
public class PostGisGeographyType : GeometryTypeBase<Geometry>
31+
{
32+
private static readonly NullableType GeometryType = new CustomGeometryType();
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="PostGisGeographyType"/> class.
36+
/// </summary>
37+
public PostGisGeographyType()
38+
: base(GeometryType)
39+
{ }
40+
41+
/// <summary>
42+
/// Converts from GeoAPI geometry type to database geography type.
43+
/// </summary>
44+
/// <param name="value">The GeoAPI geometry value.</param>
45+
/// <returns></returns>
46+
protected override Geometry FromGeometry(object value)
47+
{
48+
var geometry = value as Geometry;
49+
if (geometry == null)
50+
{
51+
return null;
52+
}
53+
54+
SetDefaultSRID(geometry);
55+
56+
return geometry;
57+
}
58+
59+
/// <summary>
60+
/// Converts to GeoAPI geometry type from database geography type.
61+
/// </summary>
62+
/// <param name="value">The database geography value.</param>
63+
/// <returns></returns>
64+
protected override Geometry ToGeometry(object value)
65+
{
66+
var geometry = value as Geometry;
67+
if (geometry == null)
68+
{
69+
return null;
70+
}
71+
72+
SetDefaultSRID(geometry);
73+
74+
return geometry;
75+
}
76+
77+
[Serializable]
78+
private class CustomGeometryType : MutableType
79+
{
80+
internal CustomGeometryType() : base(new BinarySqlType())
81+
{ }
82+
83+
public override System.Type ReturnedClass => typeof(Geometry);
84+
85+
public override string Name => "Geography";
86+
87+
public override object Get(DbDataReader rs, int index, ISessionImplementor session)
88+
{
89+
return (Geometry) rs.GetValue(index);
90+
}
91+
92+
public override object Get(DbDataReader rs, string name, ISessionImplementor session)
93+
{
94+
return Get(rs, rs.GetOrdinal(name), session);
95+
}
96+
97+
public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
98+
{
99+
var parameter = (NpgsqlParameter) cmd.Parameters[index];
100+
parameter.NpgsqlDbType = NpgsqlDbType.Geography;
101+
parameter.Value = value;
102+
}
103+
104+
public override object DeepCopyNotNull(object value)
105+
{
106+
var obj = (Geometry) value;
107+
return obj.Copy();
108+
}
109+
}
110+
}
111+
}

NHibernate.Spatial.SpatiaLite/Dialect/SpatiaLiteDialect.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Text;
44
using NHibernate.Dialect;
5+
using NHibernate.Mapping;
56
using NHibernate.Spatial.Dialect.Function;
67
using NHibernate.Spatial.Metadata;
78
using NHibernate.Spatial.Type;
@@ -313,7 +314,7 @@ public string GetSpatialCreateString(string schema)
313314
/// <returns></returns>
314315
public string GetSpatialCreateString(string schema,
315316
string table,
316-
string column,
317+
Column column,
317318
int srid,
318319
string subtype,
319320
int dimension,
@@ -323,13 +324,13 @@ public string GetSpatialCreateString(string schema,
323324

324325
builder.AppendFormat("ALTER TABLE {0} DROP COLUMN {1};"
325326
, QuoteForTableName(table)
326-
, QuoteForColumnName(column)
327+
, QuoteForColumnName(column.Name)
327328
);
328329

329330
builder.AppendFormat("SELECT AddGeometryColumn('{0}','{1}',{2},'{3}','{4}', {5});",
330-
table, column, srid, subtype, ToXyzm(dimension), isNullable ? 0 : 1);
331+
table, column.Name, srid, subtype, ToXyzm(dimension), isNullable ? 0 : 1);
331332

332-
builder.AppendFormat("SELECT CreateSpatialIndex('{0}','{1}');", table, column);
333+
builder.AppendFormat("SELECT CreateSpatialIndex('{0}','{1}');", table, column.Name);
333334

334335
return builder.ToString();
335336
}

NHibernate.Spatial/Dialect/ISpatialDialect.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with NHibernate.Spatial; if not, write to the Free Software
1616
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717

18+
using NHibernate.Mapping;
1819
using NHibernate.Spatial.Metadata;
1920
using NHibernate.Spatial.Type;
2021
using NHibernate.SqlCommand;
@@ -144,7 +145,7 @@ public interface ISpatialDialect
144145
/// <param name="dimension">[3DIS] The dimension</param>
145146
/// <param name="isNullable">Whether or not the column is nullable</param>
146147
/// <returns></returns>
147-
string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable);
148+
string GetSpatialCreateString(string schema, string table, Column column, int srid, string subtype, int dimension, bool isNullable);
148149

149150
/// <summary>
150151
/// Gets the spatial drop string.

NHibernate.Spatial/Mapping/SpatialAuxiliaryDatabaseObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void ColumnVisitorSQLCreate(Table table, Column column, StringBuilder bu
192192
string subtype = geometryType.Subtype;
193193
int dimension = geometryType.Dimension;
194194

195-
builder.Append(spatialDialect.GetSpatialCreateString(defaultSchema, table.Name, column.Name, srid, subtype, dimension, column.IsNullable));
195+
builder.Append(spatialDialect.GetSpatialCreateString(defaultSchema, table.Name, column, srid, subtype, dimension, column.IsNullable));
196196
}
197197
}
198198
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using NetTopologySuite.Geometries;
3+
4+
namespace Tests.NHibernate.Spatial.Models
5+
{
6+
[Serializable]
7+
public class Geography
8+
{
9+
public int Id { get; set; }
10+
11+
public Geometry Geog { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)