Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 84dbd0e

Browse files
committed
Resolved continued issue #474
Fixed issue with the updating of SqlGeography types with SqlServer 2012 dialect provider. Added unit test and cleaned up a few other small issues.
1 parent 499cbb1 commit 84dbd0e

File tree

6 files changed

+86
-83
lines changed

6 files changed

+86
-83
lines changed

src/ServiceStack.OrmLite.SqlServer.Converters/SqlServerGeographyTypeConverter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ namespace ServiceStack.OrmLite.SqlServer.Converters
99
/// </summary>
1010
public class SqlServerGeographyTypeConverter : SqlServerSpatialTypeConverter
1111
{
12-
public override string ColumnDefinition
13-
{
14-
get { return "geography"; }
15-
}
12+
public override string ColumnDefinition => "GEOGRAPHY";
1613

1714
public override object FromDbValue(Type fieldType, object value)
1815
{

src/ServiceStack.OrmLite.SqlServer.Converters/SqlServerGeometryTypeConverter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ namespace ServiceStack.OrmLite.SqlServer.Converters
99
/// </summary>
1010
public class SqlServerGeometryTypeConverter : SqlServerSpatialTypeConverter
1111
{
12-
public override string ColumnDefinition
13-
{
14-
get { return "geometry"; }
15-
}
12+
public override string ColumnDefinition => "GEOMETRY";
1613

1714
public override object FromDbValue(Type fieldType, object value)
1815
{

src/ServiceStack.OrmLite.SqlServer.Converters/SqlServerHierarchyIdTypeConverter.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@ public class SqlServerHierarchyIdTypeConverter : OrmLiteConverter
1414
public SqlServerHierarchyIdTypeConverter() : base()
1515
{ }
1616

17-
public override string ColumnDefinition
18-
{
19-
get { return "hierarchyid"; }
20-
}
17+
public override string ColumnDefinition => "HIERARCHYID";
2118

22-
public override DbType DbType
23-
{
24-
get { return DbType.Object; }
25-
}
19+
public override DbType DbType => DbType.Object;
2620

2721
public override void InitDbParam(IDbDataParameter p, Type fieldType)
2822
{

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Data;
2-
using System.Text;
3-
using ServiceStack.Text;
1+
using ServiceStack.Text;
42

53
namespace ServiceStack.OrmLite.SqlServer
64
{
75
public class SqlServer2012OrmLiteDialectProvider : SqlServerOrmLiteDialectProvider
86
{
9-
public static new SqlServer2012OrmLiteDialectProvider Instance = new SqlServer2012OrmLiteDialectProvider();
7+
public new static SqlServer2012OrmLiteDialectProvider Instance = new SqlServer2012OrmLiteDialectProvider();
108

119
public override string ToSelectStatement(ModelDefinition modelDef,
1210
string selectExpression,
@@ -41,23 +39,5 @@ public override string ToSelectStatement(ModelDefinition modelDef,
4139

4240
return StringBuilderCache.ReturnAndFree(sb);
4341
}
44-
45-
public override void AppendFieldCondition(StringBuilder sqlFilter, FieldDefinition fieldDef, IDbCommand cmd)
46-
{
47-
if (fieldDef.FieldType.Name == "SqlGeography")
48-
{
49-
sqlFilter
50-
.Append(GetQuotedColumnName(fieldDef.FieldName))
51-
.Append(".STEquals(")
52-
.Append(this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName)))
53-
.Append(") = 1");
54-
55-
AddParameter(cmd, fieldDef);
56-
}
57-
else
58-
{
59-
base.AppendFieldCondition(sqlFilter, fieldDef, cmd);
60-
}
61-
}
6242
}
6343
}

src/ServiceStack.OrmLite.SqlServer/SqlServer2016OrmLiteDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace ServiceStack.OrmLite.SqlServer
77
{
88
public class SqlServer2016OrmLiteDialectProvider : SqlServer2014OrmLiteDialectProvider
99
{
10-
public static new SqlServer2016OrmLiteDialectProvider Instance = new SqlServer2016OrmLiteDialectProvider();
10+
public new static SqlServer2016OrmLiteDialectProvider Instance = new SqlServer2016OrmLiteDialectProvider();
1111
}
1212
}
Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using Microsoft.SqlServer.Types;
34
using NUnit.Framework;
45
using ServiceStack.DataAnnotations;
@@ -12,81 +13,104 @@ public class SpatialTests : SqlServerConvertersOrmLiteTestBase
1213
[Test]
1314
public void Can_insert_and_retrieve_SqlGeography()
1415
{
15-
using (var db = OpenDbConnection())
16-
{
17-
db.DropAndCreateTable<GeoTest>();
16+
Db.DropAndCreateTable<GeoTest>();
1817

19-
// Statue of Liberty
20-
var geo = SqlGeography.Point(40.6898329, -74.0452177, 4326);
18+
// Statue of Liberty
19+
var geo = SqlGeography.Point(40.6898329, -74.0452177, 4326);
2120

22-
db.Insert(new GeoTest {Id = 1, Location = geo, NullLocation = SqlGeography.Null});
21+
Db.Insert(new GeoTest {Id = 1, Location = geo, NullLocation = SqlGeography.Null});
2322

24-
var result = db.SingleById<GeoTest>(1);
23+
var result = Db.SingleById<GeoTest>(1);
2524

26-
Assert.AreEqual(geo.Lat, result.Location.Lat);
27-
Assert.AreEqual(geo.Long, result.Location.Long);
28-
Assert.AreEqual(geo.STSrid, result.Location.STSrid);
25+
Assert.AreEqual(geo.Lat, result.Location.Lat);
26+
Assert.AreEqual(geo.Long, result.Location.Long);
27+
Assert.AreEqual(geo.STSrid, result.Location.STSrid);
2928

30-
// Converter always resolves to null even when Null property inserted into database
31-
Assert.AreEqual(null, result.NullLocation);
29+
// Converter always resolves to null even when Null property inserted into database
30+
Assert.AreEqual(null, result.NullLocation);
3231

33-
result.PrintDump();
34-
}
32+
result.PrintDump();
3533
}
3634

3735
[Test]
3836
public void Can_insert_and_retrieve_SqlGeometry()
3937
{
40-
using (var db = OpenDbConnection())
41-
{
42-
db.DropAndCreateTable<GeoTest>();
38+
Db.DropAndCreateTable<GeoTest>();
4339

44-
// A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
45-
var wkt = new System.Data.SqlTypes.SqlChars("LINESTRING(0 0, 4 4)".ToCharArray());
46-
var shape = SqlGeometry.STLineFromText(wkt, 0);
40+
// A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
41+
var wkt = new System.Data.SqlTypes.SqlChars("LINESTRING(0 0, 4 4)".ToCharArray());
42+
var shape = SqlGeometry.STLineFromText(wkt, 0);
4743

48-
db.Insert(new GeoTest { Id = 1, Shape = shape});
44+
Db.Insert(new GeoTest { Id = 1, Shape = shape});
4945

50-
var result = db.SingleById<GeoTest>(1).Shape;
46+
var result = Db.SingleById<GeoTest>(1).Shape;
5147

52-
var lengths = db.Column<double>("select Shape.STLength() AS Length from GeoTest");
48+
var lengths = Db.Column<double>("select Shape.STLength() AS Length from GeoTest");
5349

54-
Assert.AreEqual((double) result.STLength(), lengths.First());
50+
Assert.AreEqual((double) result.STLength(), lengths.First());
5551

56-
Assert.AreEqual(shape.STStartPoint().STX, result.STStartPoint().STX);
57-
Assert.AreEqual(shape.STStartPoint().STY, result.STStartPoint().STY);
52+
Assert.AreEqual(shape.STStartPoint().STX, result.STStartPoint().STX);
53+
Assert.AreEqual(shape.STStartPoint().STY, result.STStartPoint().STY);
5854

59-
Assert.AreEqual(shape.STEndPoint().STX, result.STEndPoint().STX);
60-
Assert.AreEqual(shape.STEndPoint().STY, result.STEndPoint().STY);
55+
Assert.AreEqual(shape.STEndPoint().STX, result.STEndPoint().STX);
56+
Assert.AreEqual(shape.STEndPoint().STY, result.STEndPoint().STY);
6157

62-
Assert.AreEqual(2, (int) result.STNumPoints());
58+
Assert.AreEqual(2, (int) result.STNumPoints());
6359

64-
result.PrintDump();
65-
}
60+
result.PrintDump();
6661
}
6762

6863
[Test]
6964
public void Can_insert_SqlGeography_and_SqlGeometry()
7065
{
71-
using (var db = OpenDbConnection())
72-
{
73-
db.DropAndCreateTable<GeoTest>();
66+
Db.DropAndCreateTable<GeoTest>();
67+
68+
// Statue of Liberty
69+
var geo = SqlGeography.Point(40.6898329, -74.0452177, 4326);
70+
71+
// A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
72+
var wkt = new System.Data.SqlTypes.SqlChars("LINESTRING(0 0, 4 4)".ToCharArray());
73+
var shape = SqlGeometry.STLineFromText(wkt, 0);
74+
75+
Db.Insert(new GeoTest { Id = 1, Location = geo, Shape = shape });
76+
77+
shape = Db.SingleById<GeoTest>(1).Shape;
78+
79+
Assert.That(shape, Is.Not.Null);
7480

75-
// Statue of Liberty
76-
var geo = SqlGeography.Point(40.6898329, -74.0452177, 4326);
81+
new { shape.STEndPoint().STX, shape.STEndPoint().STY }.PrintDump();
82+
}
7783

78-
// A simple line from (0,0) to (4,4) Length = SQRT(2 * 4^2)
79-
var wkt = new System.Data.SqlTypes.SqlChars("LINESTRING(0 0, 4 4)".ToCharArray());
80-
var shape = SqlGeometry.STLineFromText(wkt, 0);
84+
[Test]
85+
public void Can_insert_and_update_SqlGeography()
86+
{
87+
Db.DropAndCreateTable<ModelWithSqlGeography>();
8188

82-
db.Insert(new GeoTest { Id = 1, Location = geo, Shape = shape });
89+
var wkt = "POINT(38.028495788574205 55.895460650576936)";
90+
var geo = SqlGeography.STGeomFromText(new System.Data.SqlTypes.SqlChars(wkt), 4326);
8391

84-
shape = db.SingleById<GeoTest>(1).Shape;
92+
var obj = new ModelWithSqlGeography { Name = "Test", Created = DateTime.UtcNow, Geo = geo };
8593

86-
Assert.That(shape, Is.Not.Null);
94+
var id = (int)Db.Insert(obj, selectIdentity: true);
95+
obj.ID = id;
8796

88-
new { shape.STEndPoint().STX, shape.STEndPoint().STY }.PrintDump();
97+
try
98+
{
99+
// Update of POCO with SqlGeography proprety should work
100+
obj.Name = "Test - modified";
101+
obj.Edited = DateTime.UtcNow;
102+
Db.Update(obj);
103+
}
104+
catch (Exception ex)
105+
{
106+
Assert.Fail(ex.ToString());
89107
}
108+
finally
109+
{
110+
// GetLastSql shouldn't return null after exception
111+
var lastSql = Db.GetLastSql();
112+
Assert.IsNotNull(lastSql);
113+
}
90114
}
91115
}
92116

@@ -100,4 +124,15 @@ public class GeoTest
100124

101125
public SqlGeometry Shape { get; set; }
102126
}
127+
128+
public class ModelWithSqlGeography
129+
{
130+
[AutoIncrement]
131+
public int ID { get; set; }
132+
[StringLength(255)]
133+
public string Name { get; set; }
134+
public DateTime Created { get; set; }
135+
public DateTime? Edited { get; set; }
136+
public SqlGeography Geo { get; set; }
137+
}
103138
}

0 commit comments

Comments
 (0)