Skip to content

Add TempTableDefinitions and TableVarMapping #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
if [ ! -f packages/FAKE/tools/FAKE.exe ]; then
mono .nuget/NuGet.exe install FAKE -OutputDirectory packages -ExcludeVersion
mono .nuget/NuGet.exe install FAKE -OutputDirectory packages -ExcludeVersion -Version 4.1.2
fi
#workaround assembly resolution issues in build.fsx
mono packages/FAKE/tools/FAKE.exe build.fsx $@
7 changes: 4 additions & 3 deletions src/SqlClient.Tests/Lib/Lib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@
<Compile Include="Library1.fs" />
<None Include="Script.fsx" />
<Content Include="App.config" />
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="FSharp.Core">
<HintPath>..\..\..\packages\FSharp.Core.4.0.0.1\lib\net40\FSharp.Core.dll</HintPath>
</Reference>
<Reference Include="FSharp.Data.SqlClient">
<HintPath>..\..\..\bin\FSharp.Data.SqlClient.dll</HintPath>
</Reference>
<Reference Include="Microsoft.SqlServer.Types">
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Types.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@
<ItemGroup>
<Compile Include="Program.fs" />
<Content Include="Uncomment.App.config" />
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="FSharp.Core">
<HintPath>..\..\..\packages\FSharp.Core.4.0.0.1\lib\net40\FSharp.Core.dll</HintPath>
</Reference>
<Reference Include="FSharp.Data.SqlClient">
<HintPath>..\..\..\bin\FSharp.Data.SqlClient.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
Expand Down
5 changes: 3 additions & 2 deletions src/SqlClient.Tests/SqlClient.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="SynonymsTests.fs" />
<Compile Include="CreateCommand.fs" />
<Compile Include="UnitsOfMeasure.fs" />
<Compile Include="TempTableTests.fs" />
<None Include="sampleCommand.sql" />
<None Include="extensions.sql" />
<None Include="MySqlFolder\sampleCommand.sql" />
Expand All @@ -101,8 +102,8 @@
<HintPath>..\..\packages\FSharp.Configuration.0.5.3\lib\net40\FSharp.Configuration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
<Reference Include="FSharp.Core">
<HintPath>..\..\packages\FSharp.Core.4.0.0.1\lib\net40\FSharp.Core.dll</HintPath>
</Reference>
<Reference Include="FSharp.Data.SqlClient">
<HintPath>..\..\bin\FSharp.Data.SqlClient.dll</HintPath>
Expand Down
20 changes: 20 additions & 0 deletions src/SqlClient.Tests/TVPTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,23 @@ let UsingTVPInQuery() =
|> Seq.toList

Assert.Equal<_ list>(expected, actual)

type MappedTVP =
SqlCommandProvider<"
SELECT myId, myName from @input
", ConnectionStrings.AdventureWorksLiteral, TableVarMapping = "@input=dbo.MyTableType">
[<Fact>]
let UsingMappedTVPInQuery() =
printfn "%s" ConnectionStrings.AdventureWorksLiteral
use cmd = new MappedTVP(ConnectionStrings.AdventureWorksLiteral)
let expected = [
1, Some "monkey"
2, Some "donkey"
]

let actual =
cmd.Execute(input = [ for id, name in expected -> MappedTVP.MyTableType(id, name) ])
|> Seq.map(fun x -> x.myId, x.myName)
|> Seq.toList

Assert.Equal<_ list>(expected, actual)
106 changes: 106 additions & 0 deletions src/SqlClient.Tests/TempTableTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module FSharp.Data.TempTableTests

open FSharp.Data
open Xunit
open System.Data.SqlClient

type TempTable =
SqlCommandProvider<
TempTableDefinitions = "
CREATE TABLE #Temp (
Id INT NOT NULL,
Name NVARCHAR(100) NULL)",
CommandText = "
SELECT Id, Name FROM #Temp",
ConnectionStringOrName =
ConnectionStrings.AdventureWorksLiteral>

[<Fact>]
let usingTempTable() =
use conn = new SqlConnection(ConnectionStrings.AdventureWorksLiteral)
conn.Open()

use cmd = new TempTable(conn)

cmd.LoadTempTables(
Temp =
[ TempTable.Temp(Id = 1, Name = Some "monkey")
TempTable.Temp(Id = 2, Name = Some "donkey") ])

let actual =
cmd.Execute()
|> Seq.map(fun x -> x.Id, x.Name)
|> Seq.toList

let expected = [
1, Some "monkey"
2, Some "donkey"
]

Assert.Equal<_ list>(expected, actual)

[<Fact>]
let queryWithHash() =
// We shouldn't mangle the statement when it's run
use cmd =
new SqlCommandProvider<
CommandText = "
SELECT Id, Name
FROM
(
SELECT 1 AS Id, '#name' AS Name UNION
SELECT 2, 'some other value'
) AS a
WHERE Name = '#name'",
ConnectionStringOrName =
ConnectionStrings.AdventureWorksLiteral>(ConnectionStrings.AdventureWorksLiteral)

let actual =
cmd.Execute()
|> Seq.map(fun x -> x.Id, x.Name)
|> Seq.toList

let expected = [
1, "#name"
]

Assert.Equal<_ list>(expected, actual)

type TempTableHash =
SqlCommandProvider<
TempTableDefinitions = "
CREATE TABLE #Temp (
Id INT NOT NULL)",
CommandText = "
SELECT a.Id, a.Name
FROM
(
SELECT 1 AS Id, '#Temp' AS Name UNION
SELECT 2, 'some other value'
) AS a
INNER JOIN #Temp t ON t.Id = a.Id",
ConnectionStringOrName =
ConnectionStrings.AdventureWorksLiteral>

[<Fact>]
let queryWithHashAndTempTable() =
// We shouldn't mangle the statement when it's run
use conn = new SqlConnection(ConnectionStrings.AdventureWorksLiteral)
conn.Open()

use cmd = new TempTableHash(conn)

cmd.LoadTempTables(
Temp =
[ TempTableHash.Temp(Id = 1) ])

let actual =
cmd.Execute()
|> Seq.map(fun x -> x.Id, x.Name)
|> Seq.toList

let expected = [
1, "#Temp"
]

Assert.Equal<_ list>(expected, actual)
2 changes: 1 addition & 1 deletion src/SqlClient.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.3.1.0" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't touch this for now.

</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
Expand Down
1 change: 1 addition & 0 deletions src/SqlClient.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FSharp.Configuration" version="0.5.3" targetFramework="net451" />
<package id="FSharp.Core" version="4.0.0.1" targetFramework="net451" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net451" />
<package id="xunit" version="1.9.2" targetFramework="net45" />
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net451" />
Expand Down
Loading