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

Commit 4412ab3

Browse files
committed
Updated SqlServer spatial converters and unit tests
1. Renamed HierarchyIdTests.cs to SqlServerHierarchyIdTests.cs 2. Renamed SqlServerConvertersOrmLiteTestBase to SqlServer2012ConvertersOrmLiteTestBase in ConvertersOrmLiteTestBase.cs 3. Added SqlServer2014ConvertersOrmLiteTestBase & SqlServer2016ConvertersOrmLiteTestBase in ConvertersOrmLiteTestBase.cs for more targetted Sql Server version testing. 4. Updated SqlGeometry, SqlGeography, & SqlHierarchyId converters to be more complete (overrides for InitDbParam, ToDbValue, FromDbValue, and ToQuotedString). 5. Added more unit tests for SqlGeometry, SqlGeography, & SqlHierarchyId converters to cover the ToDBValue, FromDBValue, and ToQuotedString methods.
1 parent 560c53c commit 4412ab3

17 files changed

+616
-269
lines changed

src/ServiceStack.OrmLite.SqlServer.Converters/ServiceStack.OrmLite.SqlServer.Converters.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
<Compile Include="SqlServerHierarchyIdTypeConverter.cs" />
7171
<Compile Include="SqlServerGeographyTypeConverter.cs" />
7272
<Compile Include="SqlServerGeometryTypeConverter.cs" />
73-
<Compile Include="SqlServerTypeConverter.cs" />
7473
<Compile Include="SqlServerExtendedStringConverters.cs" />
7574
</ItemGroup>
7675
<ItemGroup>

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
24
using Microsoft.SqlServer.Types;
35

46
namespace ServiceStack.OrmLite.SqlServer.Converters
@@ -7,24 +9,61 @@ namespace ServiceStack.OrmLite.SqlServer.Converters
79
/// SqlServer Database Converter for the Geometry data type
810
/// https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlgeography.aspx
911
/// </summary>
10-
public class SqlServerGeographyTypeConverter : SqlServerSpatialTypeConverter
12+
public class SqlServerGeographyTypeConverter : OrmLiteConverter
1113
{
12-
public override string ColumnDefinition => "GEOGRAPHY";
14+
public override string ColumnDefinition => "geography";
15+
16+
public override DbType DbType => DbType.Object;
17+
18+
public override string ToQuotedString(Type fieldType, object value)
19+
{
20+
if (fieldType == typeof(SqlGeography))
21+
{
22+
string str = null;
23+
if (value != null)
24+
{
25+
var geo = (SqlGeography)value;
26+
if (!geo.IsNull)
27+
str = geo.ToString();
28+
}
29+
str = (str == null) ? "null" : $"'{str}'";
30+
return $"CAST({str} AS {ColumnDefinition})";
31+
}
32+
33+
return base.ToQuotedString(fieldType, value);
34+
}
35+
36+
public override void InitDbParam(IDbDataParameter p, Type fieldType)
37+
{
38+
if (fieldType == typeof(SqlGeography))
39+
{
40+
var sqlParam = (SqlParameter)p;
41+
sqlParam.IsNullable = fieldType.IsNullableType();
42+
sqlParam.SqlDbType = SqlDbType.Udt;
43+
sqlParam.UdtTypeName = ColumnDefinition;
44+
}
45+
base.InitDbParam(p, fieldType);
46+
}
1347

1448
public override object FromDbValue(Type fieldType, object value)
1549
{
16-
var geo = value as SqlGeography;
17-
if (geo == null || geo.IsNull)
50+
if (value == null || value is DBNull)
51+
return SqlGeography.Null;
52+
53+
if (value is SqlGeography)
54+
return (SqlGeography)value;
55+
56+
if (value is string)
1857
{
19-
return null;
58+
return SqlGeography.Parse(value.ToString());
2059
}
2160

2261
return base.FromDbValue(fieldType, value);
2362
}
2463

2564
public override object ToDbValue(Type fieldType, object value)
2665
{
27-
if (value == null)
66+
if (value == null || value is DBNull)
2867
{
2968
return SqlGeography.Null;
3069
}
@@ -40,14 +79,6 @@ public override object ToDbValue(Type fieldType, object value)
4079
return SqlGeography.Parse(str);
4180
}
4281

43-
if (value is byte[])
44-
{
45-
var bin = value as byte[];
46-
var sqlBin = new System.Data.SqlTypes.SqlBytes(bin);
47-
48-
return SqlGeography.Deserialize(sqlBin);
49-
}
50-
5182
return base.ToDbValue(fieldType, value);
5283
}
5384
}

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
24
using Microsoft.SqlServer.Types;
35

46
namespace ServiceStack.OrmLite.SqlServer.Converters
@@ -7,24 +9,61 @@ namespace ServiceStack.OrmLite.SqlServer.Converters
79
/// SqlServer Database Converter for the Geometry data type
810
/// https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.types.sqlgeometry.aspx
911
/// </summary>
10-
public class SqlServerGeometryTypeConverter : SqlServerSpatialTypeConverter
12+
public class SqlServerGeometryTypeConverter : OrmLiteConverter
1113
{
12-
public override string ColumnDefinition => "GEOMETRY";
14+
public override string ColumnDefinition => "geometry";
15+
16+
public override DbType DbType => DbType.Object;
17+
18+
public override string ToQuotedString(Type fieldType, object value)
19+
{
20+
if (fieldType == typeof(SqlGeometry))
21+
{
22+
string str = null;
23+
if (value != null)
24+
{
25+
var geo = (SqlGeometry)value;
26+
if (!geo.IsNull)
27+
str = geo.ToString();
28+
}
29+
str = (str == null) ? "null" : $"'{str}'";
30+
return $"CAST({str} AS {ColumnDefinition})";
31+
}
32+
33+
return base.ToQuotedString(fieldType, value);
34+
}
35+
36+
public override void InitDbParam(IDbDataParameter p, Type fieldType)
37+
{
38+
if (fieldType == typeof(SqlGeometry))
39+
{
40+
var sqlParam = (SqlParameter)p;
41+
sqlParam.IsNullable = fieldType.IsNullableType();
42+
sqlParam.SqlDbType = SqlDbType.Udt;
43+
sqlParam.UdtTypeName = ColumnDefinition;
44+
}
45+
base.InitDbParam(p, fieldType);
46+
}
1347

1448
public override object FromDbValue(Type fieldType, object value)
1549
{
16-
var geo = value as SqlGeometry;
17-
if (geo == null || geo.IsNull)
50+
if (value == null || value is DBNull)
51+
return SqlGeometry.Null;
52+
53+
if (value is SqlGeometry)
54+
return (SqlGeometry)value;
55+
56+
if (value is string)
1857
{
19-
return null;
58+
return SqlGeometry.Parse(value.ToString());
2059
}
2160

2261
return base.FromDbValue(fieldType, value);
2362
}
2463

2564
public override object ToDbValue(Type fieldType, object value)
2665
{
27-
if (value == null)
66+
if (value == null || value is DBNull)
2867
{
2968
return SqlGeometry.Null;
3069
}
@@ -40,14 +79,6 @@ public override object ToDbValue(Type fieldType, object value)
4079
return SqlGeometry.Parse(str);
4180
}
4281

43-
if (value is byte[])
44-
{
45-
var bin = value as byte[];
46-
var sqlBin = new System.Data.SqlTypes.SqlBytes(bin);
47-
48-
return SqlGeometry.Deserialize(sqlBin);
49-
}
50-
5182
return base.ToDbValue(fieldType, value);
5283
}
5384
}

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,66 @@ namespace ServiceStack.OrmLite.SqlServer.Converters
1111
/// </summary>
1212
public class SqlServerHierarchyIdTypeConverter : OrmLiteConverter
1313
{
14-
public SqlServerHierarchyIdTypeConverter() : base()
15-
{ }
16-
17-
public override string ColumnDefinition => "HIERARCHYID";
14+
public override string ColumnDefinition => "hierarchyId";
1815

1916
public override DbType DbType => DbType.Object;
2017

18+
public override string ToQuotedString(Type fieldType, object value)
19+
{
20+
if (fieldType == typeof(SqlHierarchyId))
21+
{
22+
string str = null;
23+
if (value != null)
24+
{
25+
var hierarchyId = (SqlHierarchyId)value;
26+
if (!hierarchyId.IsNull)
27+
str = hierarchyId.ToString();
28+
}
29+
str = (str == null) ? "null" : $"'{str}'";
30+
return $"CAST({str} AS {ColumnDefinition})";
31+
}
32+
33+
return base.ToQuotedString(fieldType, value);
34+
}
35+
2136
public override void InitDbParam(IDbDataParameter p, Type fieldType)
2237
{
23-
var sqlParam = (SqlParameter)p;
24-
sqlParam.IsNullable = fieldType.IsNullableType();
25-
sqlParam.SqlDbType = SqlDbType.Udt;
26-
sqlParam.UdtTypeName = ColumnDefinition;
38+
if (fieldType == typeof(SqlHierarchyId))
39+
{
40+
var sqlParam = (SqlParameter)p;
41+
sqlParam.IsNullable = fieldType.IsNullableType();
42+
sqlParam.SqlDbType = SqlDbType.Udt;
43+
sqlParam.UdtTypeName = ColumnDefinition;
44+
}
45+
base.InitDbParam(p, fieldType);
2746
}
2847

2948
public override object FromDbValue(Type fieldType, object value)
3049
{
31-
if (((SqlHierarchyId)value).IsNull && fieldType.IsNullableType())
50+
if (value == null || value is DBNull)
51+
return SqlHierarchyId.Null;
52+
53+
if (value is SqlHierarchyId)
54+
return (SqlHierarchyId)value;
55+
56+
if (value is string)
3257
{
33-
return null;
58+
return SqlHierarchyId.Parse(value.ToString());
3459
}
3560

3661
return base.FromDbValue(fieldType, value);
3762
}
3863

3964
public override object ToDbValue(Type fieldType, object value)
4065
{
41-
if (value is SqlHierarchyId)
66+
if (value == null || value is DBNull)
4267
{
43-
return value;
68+
return SqlHierarchyId.Null;
4469
}
4570

46-
if (value == null)
71+
if (value is SqlHierarchyId)
4772
{
48-
return SqlHierarchyId.Null;
73+
return value;
4974
}
5075

5176
if (value is string)

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/ServiceStack.OrmLite.SqlServerTests/Converters/ConvertersOrmLiteTestBase.cs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
namespace ServiceStack.OrmLite.SqlServerTests.Converters
99
{
10-
public class SqlServerConvertersOrmLiteTestBase
10+
public class SqlServer2012ConvertersOrmLiteTestBase : OrmLiteTestBase
1111
{
12-
protected virtual string ConnectionString { get; set; }
13-
14-
public IDbConnection Db { get; set; }
15-
1612
[TestFixtureSetUp]
17-
public void TestFixtureSetUp()
13+
public override void TestFixtureSetUp()
1814
{
1915
try
2016
{
@@ -23,7 +19,7 @@ public void TestFixtureSetUp()
2319
// Appending the Sql Server Type System Version to use SqlServerSpatial110.dll (2012) assembly
2420
// Sql Server defaults to SqlServerSpatial100.dll (2008 R2) even for versions greater
2521
// https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
26-
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + ";Type System Version=SQL Server 2012;";
22+
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2012;";
2723

2824
var dialectProvider = SqlServerConverters.Configure(SqlServer2012Dialect.Provider);
2925

@@ -34,18 +30,55 @@ public void TestFixtureSetUp()
3430
throw ex;
3531
}
3632
}
33+
}
3734

38-
public void Log(string text)
35+
public class SqlServer2014ConvertersOrmLiteTestBase : SqlServer2012ConvertersOrmLiteTestBase
36+
{
37+
[TestFixtureSetUp]
38+
public override void TestFixtureSetUp()
3939
{
40-
Console.WriteLine(text);
40+
try
41+
{
42+
LogManager.LogFactory = new ConsoleLogFactory();
43+
44+
// Appending the Sql Server Type System Version to use SqlServerSpatial110.dll (2012) assembly
45+
// Sql Server defaults to SqlServerSpatial100.dll (2008 R2) even for versions greater
46+
// https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
47+
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2014;";
48+
49+
var dialectProvider = SqlServerConverters.Configure(SqlServer2014Dialect.Provider);
50+
51+
Db = new OrmLiteConnectionFactory(ConnectionString, dialectProvider).OpenDbConnection();
52+
}
53+
catch (Exception ex)
54+
{
55+
throw ex;
56+
}
4157
}
58+
}
4259

43-
public virtual IDbConnection OpenDbConnection(string connString = null, IOrmLiteDialectProvider dialectProvider = null)
60+
public class SqlServer2016ConvertersOrmLiteTestBase : SqlServer2014ConvertersOrmLiteTestBase
61+
{
62+
[TestFixtureSetUp]
63+
public override void TestFixtureSetUp()
4464
{
45-
dialectProvider = dialectProvider ?? OrmLiteConfig.DialectProvider;
46-
connString = connString ?? ConnectionString;
65+
try
66+
{
67+
LogManager.LogFactory = new ConsoleLogFactory();
4768

48-
return new OrmLiteConnectionFactory(connString, dialectProvider).OpenDbConnection();
69+
// Appending the Sql Server Type System Version to use SqlServerSpatial110.dll (2012) assembly
70+
// Sql Server defaults to SqlServerSpatial100.dll (2008 R2) even for versions greater
71+
// https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
72+
ConnectionString = ConfigurationManager.ConnectionStrings["testDb"].ConnectionString + "Type System Version=SQL Server 2016;";
73+
74+
var dialectProvider = SqlServerConverters.Configure(SqlServer2016Dialect.Provider);
75+
76+
Db = new OrmLiteConnectionFactory(ConnectionString, dialectProvider).OpenDbConnection();
77+
}
78+
catch (Exception ex)
79+
{
80+
throw ex;
81+
}
4982
}
5083
}
5184
}

0 commit comments

Comments
 (0)