Skip to content

Commit

Permalink
Geography parameter handling (#25)
Browse files Browse the repository at this point in the history
* Added custom handling for geography parameters

* Bumped version

* Tweaking setting of udt type

* Added geography integration tests
  • Loading branch information
carl-berg authored Feb 20, 2023
1 parent dcc542b commit d3f9624
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
6 changes: 6 additions & 0 deletions DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ CREATE TABLE Test_Nullable_Text_Data_Type(
)
GO

CREATE TABLE Test_Geography_Data(
Id INT IDENTITY PRIMARY KEY,
Position GEOGRAPHY NULL,
)
GO

CREATE TRIGGER People.EmployeeUpdatedAt ON People.Employee AFTER UPDATE AS BEGIN
UPDATE People.Employee
SET People.Employee.UpdatedAt = GETDATE()
Expand Down
1 change: 1 addition & 0 deletions DataDude.Tests/DataDude.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="ADatabaseFixture.SqlServer" Version="0.4.0" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
<PackageReference Include="Respawn" Version="6.0.0" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down
26 changes: 26 additions & 0 deletions DataDude.Tests/Inserts/InstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DataDude.Instructions.Insert;
using DataDude.Instructions.Insert.Insertion;
using DataDude.Tests.Core;
using Microsoft.SqlServer.Types;
using Shouldly;
using Xunit;

Expand Down Expand Up @@ -345,5 +346,30 @@ public async Task Split_Inserts_Keeps_Track_Of_Previously_Inserted_Data()
var officeOccupancies = await connection.QueryAsync<dynamic>("SELECT * FROM People.OfficeOccupancy");
officeOccupancies.ShouldContain(x => true, 2, "Should contain two occupancies");
}

[Fact]
public async Task Can_Insert_Geography_Data()
{
using var connection = Fixture.CreateNewConnection();
var position = SqlGeography.Point(50, 11, 4326);

var dude = new Dude();
await dude
.Insert("Test_Geography_Data",
new { Position = position },
new { Position = (SqlGeography)null })
.Go(connection);

var geography_data = (await connection.QueryAsync<(decimal? Lat, decimal? Long)>("SELECT Position.Lat, Position.Long FROM Test_Geography_Data ORDER BY Id")).ToList();
geography_data.ShouldSatisfyAllConditions(
positions => positions[0].ShouldSatisfyAllConditions(
position => position.Lat.ShouldBe(50),
position => position.Long.ShouldBe(11)),
positions => positions[1].ShouldSatisfyAllConditions(
position => position.Lat.ShouldBeNull(),
position => position.Long.ShouldBeNull()));


}
}
}
2 changes: 1 addition & 1 deletion DataDude/DataDude.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Version>0.8.0-preview.1</Version>
<Version>0.8.0-preview.2</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
28 changes: 17 additions & 11 deletions DataDude/Database/DataDudeDbParameter.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
using DataDude.Instructions.Insert;
using DataDude.Schema;

namespace DataDude.Core
{
public struct DataDudeDbParameter
{
public DataDudeDbParameter(string name, object? value, DbType? dbType)
public DataDudeDbParameter(ColumnInformation column, ColumnValue? value)
{
Name = name;
Value = value;
DbType = dbType;
Column = column;
ColumnValue = value;
}

public string Name { get; }
public object? Value { get; }
public DbType? DbType { get; }
public ColumnInformation Column { get; }
public ColumnValue? ColumnValue { get; }

public void AddParameterTo(DbCommand cmd)
{
var param = cmd.CreateParameter();
param.ParameterName = $"@{Name}";
param.Value = Value ?? DBNull.Value;
param.ParameterName = $"@{Column.Name}";
param.Value = ColumnValue?.Value ?? DBNull.Value;
param.Direction = ParameterDirection.Input;
if (DbType is { })

if (ColumnValue?.DbType is { } dbType)
{
param.DbType = DbType.Value;
param.DbType = dbType;
}
else if (Column is { DataType: "geography" } && param.GetType().GetProperty("UdtTypeName") is PropertyInfo udtProperty)
{
udtProperty.SetValue(param, "geography");
}

cmd.Parameters.Add(param);
Expand Down
2 changes: 1 addition & 1 deletion DataDude/Instructions/IInstructionDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DataDude.Instructions
{
/// <summary>
///
/// Intercept context before and after instructions have been executed
/// </summary>
public interface IInstructionDecorator
{
Expand Down
2 changes: 1 addition & 1 deletion DataDude/Instructions/Insert/Insertion/RowInsertHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected internal (string columns, string values, IReadOnlyList<DataDudeDbParam
{
if (!(value.Value is RawSql))
{
parameters.Add(new (column.Name, value.Value, value.DbType));
parameters.Add(new (column, value));
}
}

Expand Down

0 comments on commit d3f9624

Please sign in to comment.