Commit 54bc52b3 authored by Marc Gravell's avatar Marc Gravell

1.32: Support for SqlGeography in core library

parent 6600c66b
...@@ -32,5 +32,5 @@ ...@@ -32,5 +32,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -276,6 +276,36 @@ public interface ITypeHandler ...@@ -276,6 +276,36 @@ public interface ITypeHandler
object Parse(Type destinationType, object value); object Parse(Type destinationType, object value);
} }
/// <summary>
/// A type handler for data-types that are supported by the underlying provider, but which need
/// a well-known UdtTypeName to be specified
/// </summary>
public class UdtTypeHandler : ITypeHandler
{
private readonly string udtTypeName;
/// <summary>
/// Creates a new instance of UdtTypeHandler with the specified UdtTypeName
/// </summary>
public UdtTypeHandler(string udtTypeName)
{
if (string.IsNullOrEmpty(udtTypeName)) throw new ArgumentException("Cannot be null or empty", udtTypeName);
this.udtTypeName = udtTypeName;
}
object ITypeHandler.Parse(Type destinationType, object value)
{
return value is DBNull ? null : value;
}
void ITypeHandler.SetValue(IDbDataParameter parameter, object value)
{
parameter.Value = ((object)value) ?? DBNull.Value;
if (parameter is System.Data.SqlClient.SqlParameter)
{
((System.Data.SqlClient.SqlParameter)parameter).UdtTypeName = udtTypeName;
}
}
}
/// <summary> /// <summary>
/// Base-class for simple type-handlers /// Base-class for simple type-handlers
/// </summary> /// </summary>
...@@ -809,10 +839,15 @@ internal static DbType LookupDbType(Type type, string name, out ITypeHandler han ...@@ -809,10 +839,15 @@ internal static DbType LookupDbType(Type type, string name, out ITypeHandler han
{ {
return DbType.Object; return DbType.Object;
} }
throw new NotSupportedException(string.Format("The member {0} of type {1} cannot be used as a parameter value", name, type)); if(type.FullName == "Microsoft.SqlServer.Types.SqlGeography")
{
handler = new UdtTypeHandler("GEOGRAPHY");
AddTypeHandler(type, handler);
return DbType.Object;
}
throw new NotSupportedException(string.Format("The member {0} of type {1} cannot be used as a parameter value", name, type));
} }
/// <summary> /// <summary>
/// Identity of a cached query in Dapper, used for extensability /// Identity of a cached query in Dapper, used for extensability
/// </summary> /// </summary>
......
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -60,9 +60,6 @@ ...@@ -60,9 +60,6 @@
<Compile Include="..\Dapper.EntityFramework NET45\Handlers.cs"> <Compile Include="..\Dapper.EntityFramework NET45\Handlers.cs">
<Link>Handlers.cs</Link> <Link>Handlers.cs</Link>
</Compile> </Compile>
<Compile Include="..\Dapper.EntityFramework NET45\SqlGeographyHandler.cs">
<Link>SqlGeographyHandler.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SqlServerTypes\Loader.cs" /> <Compile Include="SqlServerTypes\Loader.cs" />
</ItemGroup> </ItemGroup>
......
...@@ -55,7 +55,6 @@ ...@@ -55,7 +55,6 @@
<Compile Include="DbGeographyHandler.cs" /> <Compile Include="DbGeographyHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Handlers.cs" /> <Compile Include="Handlers.cs" />
<Compile Include="SqlGeographyHandler.cs" />
<Compile Include="SqlServerTypes\Loader.cs" /> <Compile Include="SqlServerTypes\Loader.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
using Microsoft.SqlServer.Types; namespace Dapper.EntityFramework
namespace Dapper.EntityFramework
{ {
/// <summary> /// <summary>
/// Acts on behalf of all type-handlers in this package /// Acts on behalf of all type-handlers in this package
...@@ -13,7 +11,6 @@ public static class Handlers ...@@ -13,7 +11,6 @@ public static class Handlers
public static void Register() public static void Register()
{ {
SqlMapper.AddTypeHandler(DbGeographyHandler.Default); SqlMapper.AddTypeHandler(DbGeographyHandler.Default);
SqlMapper.AddTypeHandler(SqlGeographyHandler.Default);
} }
} }
} }
using Microsoft.SqlServer.Types;
using System;
using System.Data;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
namespace Dapper.EntityFramework
{
/// <summary>
/// Type-handler for the DbGeography spatial type
/// </summary>
public class SqlGeographyHandler : Dapper.SqlMapper.TypeHandler<SqlGeography>
{
/// <summary>
/// Create a new handler instance
/// </summary>
protected SqlGeographyHandler() { }
/// <summary>
/// Default handler instance
/// </summary>
public static readonly SqlGeographyHandler Default = new SqlGeographyHandler();
/// <summary>
/// Assign the value of a parameter before a command executes
/// </summary>
/// <param name="parameter">The parameter to configure</param>
/// <param name="value">Parameter value</param>
public override void SetValue(IDbDataParameter parameter, SqlGeography value)
{
parameter.Value = ((object)value) ?? DBNull.Value;
if (parameter is SqlParameter)
{
((SqlParameter)parameter).UdtTypeName = "GEOGRAPHY";
}
}
/// <summary>
/// Parse a database value back to a typed value
/// </summary>
/// <param name="value">The value from the database</param>
/// <returns>The typed value</returns>
public override SqlGeography Parse(object value)
{
return value as SqlGeography;
}
}
}
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.31.0.0")] [assembly: AssemblyVersion("1.32.0.0")]
[assembly: AssemblyFileVersion("1.31.0.0")] [assembly: AssemblyFileVersion("1.32.0.0")]
...@@ -3012,8 +3012,7 @@ public void DBGeography_SO24405645_SO24402424() ...@@ -3012,8 +3012,7 @@ public void DBGeography_SO24405645_SO24402424()
public void SqlGeography_SO25538154() public void SqlGeography_SO25538154()
{ {
Dapper.EntityFramework.Handlers.Register(); Dapper.SqlMapper.ResetTypeHandlers();
connection.Execute("create table #SqlGeo (id int, geo geography)"); connection.Execute("create table #SqlGeo (id int, geo geography)");
var obj = new HazSqlGeo var obj = new HazSqlGeo
...@@ -3022,7 +3021,7 @@ public void SqlGeography_SO25538154() ...@@ -3022,7 +3021,7 @@ public void SqlGeography_SO25538154()
Geo = SqlGeography.STLineFromText(new SqlChars(new SqlString("LINESTRING(-122.360 47.656, -122.343 47.656 )")), 4326) Geo = SqlGeography.STLineFromText(new SqlChars(new SqlString("LINESTRING(-122.360 47.656, -122.343 47.656 )")), 4326)
}; };
connection.Execute("insert #SqlGeo(id, geo) values (@Id, @Geo)", obj); connection.Execute("insert #SqlGeo(id, geo) values (@Id, @Geo)", obj);
var row = connection.Query<HazGeo>("select * from #SqlGeo where id=1").SingleOrDefault(); var row = connection.Query<HazSqlGeo>("select * from #SqlGeo where id=1").SingleOrDefault();
row.IsNotNull(); row.IsNotNull();
row.Id.IsEqualTo(1); row.Id.IsEqualTo(1);
row.Geo.IsNotNull(); row.Geo.IsNotNull();
......
...@@ -22,4 +22,12 @@ ...@@ -22,4 +22,12 @@
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers> </providers>
</entityFramework> </entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> </configuration>
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata schemaVersion="2"> <metadata schemaVersion="2">
<id>Dapper</id> <id>Dapper</id>
<version>1.31</version> <version>1.32</version>
<title>Dapper dot net</title> <title>Dapper dot net</title>
<authors>Sam Saffron,Marc Gravell</authors> <authors>Sam Saffron,Marc Gravell</authors>
<owners>Sam Saffron,Marc Gravell</owners> <owners>Sam Saffron,Marc Gravell</owners>
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
<frameworkAssembly assemblyName="Microsoft.CSharp" targetFramework=".NETFramework4.0-Client, .NETFramework4.0" /> <frameworkAssembly assemblyName="Microsoft.CSharp" targetFramework=".NETFramework4.0-Client, .NETFramework4.0" />
</frameworkAssemblies> </frameworkAssemblies>
<releaseNotes> <releaseNotes>
* 1.32 - Support for SqlGeography in core library
* 1.31 - Fix issue with error message when there is a column/type mismatch * 1.31 - Fix issue with error message when there is a column/type mismatch
* 1.30 - Better async cancellation * 1.30 - Better async cancellation
* 1.29 - Make underscore name matching optional (opt-in) - this can be a breaking change for some people * 1.29 - Make underscore name matching optional (opt-in) - this can be a breaking change for some people
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment