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

Commit 56ccf26

Browse files
committed
Merge branch 'master' of github.com:ServiceStack/ServiceStack.OrmLite
2 parents 8dacd72 + 3006d71 commit 56ccf26

24 files changed

+885
-277
lines changed

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

Lines changed: 9 additions & 3 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" />
@@ -68,9 +70,13 @@
6870
<Compile Include="SqlServerHierarchyIdTypeConverter.cs" />
6971
<Compile Include="SqlServerGeographyTypeConverter.cs" />
7072
<Compile Include="SqlServerGeometryTypeConverter.cs" />
71-
<Compile Include="SqlServerTypeConverter.cs" />
73+
<Compile Include="SqlServerExtendedStringConverters.cs" />
7274
</ItemGroup>
7375
<ItemGroup>
76+
<ProjectReference Include="..\ServiceStack.OrmLite.SqlServer\ServiceStack.OrmLite.SqlServer.csproj">
77+
<Project>{1887dc99-9139-43e3-a7aa-6d74714b3a5d}</Project>
78+
<Name>ServiceStack.OrmLite.SqlServer</Name>
79+
</ProjectReference>
7480
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
7581
<Project>{96179ac6-f6f1-40c3-9fdd-4f6582f54c5c}</Project>
7682
<Name>ServiceStack.OrmLite</Name>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ public static class SqlServerConverters
1010
public static string Msvcr100FileName = "msvcr100.dll";
1111
public static string SqlServerSpatial110FileName = "SqlServerSpatial110.dll";
1212

13-
public static IOrmLiteDialectProvider Configure(IOrmLiteDialectProvider dialectProvider, string libraryPath = null)
13+
public static IOrmLiteDialectProvider Configure(IOrmLiteDialectProvider dialectProvider, string[] fileNames, string libraryPath = null)
1414
{
15-
LoadAssembly(Msvcr100FileName, libraryPath);
16-
LoadAssembly(SqlServerSpatial110FileName, libraryPath);
15+
foreach (var fileName in fileNames)
16+
{
17+
LoadAssembly(fileName, libraryPath);
18+
}
1719

20+
dialectProvider.RegisterConverter<string>(new SqlServerExtendedStringConverter());
1821
dialectProvider.RegisterConverter<SqlGeography>(new SqlServerGeographyTypeConverter());
1922
dialectProvider.RegisterConverter<SqlGeometry>(new SqlServerGeometryTypeConverter());
2023
dialectProvider.RegisterConverter<SqlHierarchyId>(new SqlServerHierarchyIdTypeConverter());
2124
return dialectProvider;
2225
}
2326

27+
public static IOrmLiteDialectProvider Configure(IOrmLiteDialectProvider dialectProvider, string libraryPath = null) =>
28+
Configure(dialectProvider, new string[] { Msvcr100FileName, SqlServerSpatial110FileName }, libraryPath);
29+
2430
public static void LoadAssembly(string assemblyName, string libraryPath = null)
2531
{
2632
// default libraryPath to Windows System
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+
}

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)

0 commit comments

Comments
 (0)