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

Commit 560c53c

Browse files
committed
Added new SqlServerExtendedStringConverter.
Added new SqlServerExtendedStringConverter to handle SqlHierarchyId, SqlGeometry, and SqlGeography string conversions.
1 parent b92206d commit 560c53c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
<HintPath>..\packages\Microsoft.SqlServer.Types.11.0.2\lib\net20\Microsoft.SqlServer.Types.dll</HintPath>
5050
<Private>True</Private>
5151
</Reference>
52-
<Reference Include="ServiceStack.Text, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
53-
<SpecificVersion>False</SpecificVersion>
52+
<Reference Include="ServiceStack.Interfaces">
53+
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
54+
</Reference>
55+
<Reference Include="ServiceStack.Text">
5456
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
5557
</Reference>
5658
<Reference Include="System" />
@@ -69,8 +71,13 @@
6971
<Compile Include="SqlServerGeographyTypeConverter.cs" />
7072
<Compile Include="SqlServerGeometryTypeConverter.cs" />
7173
<Compile Include="SqlServerTypeConverter.cs" />
74+
<Compile Include="SqlServerExtendedStringConverters.cs" />
7275
</ItemGroup>
7376
<ItemGroup>
77+
<ProjectReference Include="..\ServiceStack.OrmLite.SqlServer\ServiceStack.OrmLite.SqlServer.csproj">
78+
<Project>{1887dc99-9139-43e3-a7aa-6d74714b3a5d}</Project>
79+
<Name>ServiceStack.OrmLite.SqlServer</Name>
80+
</ProjectReference>
7481
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
7582
<Project>{96179ac6-f6f1-40c3-9fdd-4f6582f54c5c}</Project>
7683
<Name>ServiceStack.OrmLite</Name>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static IOrmLiteDialectProvider Configure(IOrmLiteDialectProvider dialectP
1717
LoadAssembly(fileName, libraryPath);
1818
}
1919

20+
dialectProvider.RegisterConverter<string>(new SqlServerExtendedStringConverter());
2021
dialectProvider.RegisterConverter<SqlGeography>(new SqlServerGeographyTypeConverter());
2122
dialectProvider.RegisterConverter<SqlGeometry>(new SqlServerGeometryTypeConverter());
2223
dialectProvider.RegisterConverter<SqlHierarchyId>(new SqlServerHierarchyIdTypeConverter());
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
4+
using ServiceStack.DataAnnotations;
5+
using Microsoft.SqlServer.Types;
6+
7+
namespace ServiceStack.OrmLite.SqlServer.Converters
8+
{
9+
public class SqlServerExtendedStringConverter : SqlServerStringConverter
10+
{
11+
public override object FromDbValue(Type fieldType, object value)
12+
{
13+
if (value is SqlHierarchyId)
14+
{
15+
var hierarchyId = (SqlHierarchyId)value;
16+
return (hierarchyId.IsNull) ? null : hierarchyId.ToString();
17+
}
18+
19+
if (value is SqlGeography)
20+
{
21+
var geography = (SqlGeography)value;
22+
return (geography.IsNull) ? null : geography.ToString();
23+
}
24+
25+
if (value is SqlGeometry)
26+
{
27+
var geometry = (SqlGeometry)value;
28+
return (geometry.IsNull) ? null : geometry.ToString();
29+
}
30+
31+
return base.FromDbValue(fieldType, value);
32+
}
33+
34+
//public override object ToDbValue(Type fieldType, object value)
35+
//{
36+
// var str = value?.ToString();
37+
38+
// if (fieldType == typeof(SqlHierarchyId))
39+
// {
40+
// return (str == null) ? SqlHierarchyId.Null : SqlHierarchyId.Parse(str);
41+
// }
42+
43+
// if (fieldType == typeof(SqlGeography))
44+
// {
45+
// var geography = (SqlGeography)value;
46+
// var srid = geography.STSrid.Value;
47+
// return (str == null) ? SqlGeography.Null : SqlGeography.STGeomFromText(new System.Data.SqlTypes.SqlChars(str), srid);
48+
// }
49+
50+
// if (fieldType == typeof(SqlGeometry))
51+
// {
52+
// var geometry = (SqlGeometry)value;
53+
// var srid = geometry.STSrid.Value;
54+
// return (str == null) ? SqlGeometry.Null : SqlGeometry.STGeomFromText(new System.Data.SqlTypes.SqlChars(str), srid);
55+
// }
56+
57+
// return base.ToDbValue(fieldType, value);
58+
//}
59+
}
60+
}

0 commit comments

Comments
 (0)