diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 014a863f..4976c5fd 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -133,3 +133,5 @@ #### 1.5.8 - January 13, 2015 * Fix issue #96 on Sql Azure +#### 1.5.9 - February 11, 2015 + * Dynamic sql example. Issue #108 diff --git a/src/SqlClient.Samples/WebApi.Controllers/HomeController.fs b/src/SqlClient.Samples/WebApi.Controllers/HomeController.fs index 8ee21437..48cdea73 100644 --- a/src/SqlClient.Samples/WebApi.Controllers/HomeController.fs +++ b/src/SqlClient.Samples/WebApi.Controllers/HomeController.fs @@ -17,7 +17,7 @@ module SqlCommand = //get connection string at run-time let adventureWorks = WebConfigurationManager.ConnectionStrings.["AdventureWorks2012"].ConnectionString //create command instance with connection string override - (^a : (new : string -> ^a) adventureWorks) + (^a : (new : string * int -> ^a) (adventureWorks, 30)) | _ -> failwithf "Unrecognized command type %s" typeof<'a>.FullName diff --git a/src/SqlClient.Samples/WebApi.Controllers/WebApi.Controllers.fsproj b/src/SqlClient.Samples/WebApi.Controllers/WebApi.Controllers.fsproj index fbdc0a12..0dcf8fbe 100644 --- a/src/SqlClient.Samples/WebApi.Controllers/WebApi.Controllers.fsproj +++ b/src/SqlClient.Samples/WebApi.Controllers/WebApi.Controllers.fsproj @@ -64,7 +64,7 @@ - ..\..\..\packages\FSharp.Data.SqlClient.1.4.4\lib\net40\FSharp.Data.SqlClient.dll + ..\..\..\packages\FSharp.Data.SqlClient.1.5.8\lib\net40\FSharp.Data.SqlClient.dll True diff --git a/src/SqlClient.Samples/WebApi.Controllers/packages.config b/src/SqlClient.Samples/WebApi.Controllers/packages.config index 33245d99..62921baf 100644 --- a/src/SqlClient.Samples/WebApi.Controllers/packages.config +++ b/src/SqlClient.Samples/WebApi.Controllers/packages.config @@ -1,6 +1,6 @@  - + diff --git a/src/SqlClient.Samples/WebApi/WebApi.csproj b/src/SqlClient.Samples/WebApi/WebApi.csproj index 1d93e3e7..2045fd14 100644 --- a/src/SqlClient.Samples/WebApi/WebApi.csproj +++ b/src/SqlClient.Samples/WebApi/WebApi.csproj @@ -43,9 +43,9 @@ True - + False - ..\..\..\packages\FSharp.Data.SqlClient.1.4.4\lib\net40\FSharp.Data.SqlClient.dll + ..\..\..\packages\FSharp.Data.SqlClient.1.5.8\lib\net40\FSharp.Data.SqlClient.dll False diff --git a/src/SqlClient.Samples/WebApi/packages.config b/src/SqlClient.Samples/WebApi/packages.config index 566ed2e6..fb71782a 100644 --- a/src/SqlClient.Samples/WebApi/packages.config +++ b/src/SqlClient.Samples/WebApi/packages.config @@ -1,6 +1,6 @@  - + diff --git a/src/SqlClient.Samples/WpfDataBinding/WpfDataBinding.fsproj b/src/SqlClient.Samples/WpfDataBinding/WpfDataBinding.fsproj index 91b945ae..bd22aabb 100644 --- a/src/SqlClient.Samples/WpfDataBinding/WpfDataBinding.fsproj +++ b/src/SqlClient.Samples/WpfDataBinding/WpfDataBinding.fsproj @@ -41,7 +41,7 @@ - ..\..\..\packages\FSharp.Data.SqlClient.1.4.4\lib\net40\FSharp.Data.SqlClient.dll + ..\..\..\packages\FSharp.Data.SqlClient.1.5.8\lib\net40\FSharp.Data.SqlClient.dll True diff --git a/src/SqlClient.Samples/WpfDataBinding/packages.config b/src/SqlClient.Samples/WpfDataBinding/packages.config index 70747dfd..ef59f262 100644 --- a/src/SqlClient.Samples/WpfDataBinding/packages.config +++ b/src/SqlClient.Samples/WpfDataBinding/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/src/SqlClient.Tests/ConfigurationTest.fs b/src/SqlClient.Tests/ConfigurationTest.fs index 3ce0e07b..19da0e38 100644 --- a/src/SqlClient.Tests/ConfigurationTest.fs +++ b/src/SqlClient.Tests/ConfigurationTest.fs @@ -27,3 +27,5 @@ let RuntimeConfig () = |> should equal ConfigurationManager.ConnectionStrings.[name].ConnectionString type Get42RelativePath = SqlCommandProvider<"sampleCommand.sql", "name=AdventureWorks2012", ResolutionFolder="MySqlFolder"> + +type Get42 = SqlCommandProvider<"SELECT 42", "name=AdventureWorks2012", ConfigFile = "appWithInclude.config"> \ No newline at end of file diff --git a/src/SqlClient.Tests/SqlClient.Tests.fsproj b/src/SqlClient.Tests/SqlClient.Tests.fsproj index 1365121a..82efa3d8 100644 --- a/src/SqlClient.Tests/SqlClient.Tests.fsproj +++ b/src/SqlClient.Tests/SqlClient.Tests.fsproj @@ -68,7 +68,8 @@ - + + @@ -76,6 +77,7 @@ + diff --git a/src/SqlClient.Tests/TypeProviderTest.fs b/src/SqlClient.Tests/TypeProviderTest.fs index e1557183..185e0ff0 100644 --- a/src/SqlClient.Tests/TypeProviderTest.fs +++ b/src/SqlClient.Tests/TypeProviderTest.fs @@ -126,3 +126,28 @@ let CommandTimeout() = use cmd = new LongRunning(commandTimeout = 60) Assert.Equal(60, cmd.CommandTimeout) Assert.Equal(Some 42, cmd.Execute()) + +type DynamicCommand = SqlCommandProvider<" + DECLARE @stmt AS NVARCHAR(MAX) = @tsql + DECLARE @params AS NVARCHAR(MAX) = N'@p1 nvarchar(100)' + DECLARE @p1 AS NVARCHAR(100) = @firstName + EXECUTE sp_executesql @stmt, @params, @p1 + WITH RESULT SETS + ( + ( + Name NVARCHAR(100) + ,UUID UNIQUEIDENTIFIER + ) + ) + ", connection> + +[] +let DynamicSql() = + let cmd = new DynamicCommand() + //provide dynamic sql query with param + cmd.Execute("SELECT CONCAT(FirstName, LastName) AS Name, rowguid AS UUID FROM Person.Person WHERE FirstName = @p1", "Alex") |> Seq.toArray |> Array.length |> should equal 51 + //extend where condition by filetering out additional rows + cmd.Execute("SELECT CONCAT(FirstName, LastName) AS Name, rowguid AS UUID FROM Person.Person WHERE FirstName = @p1 AND EmailPromotion = 2", "Alex") |> Seq.toArray |> Array.length |> should equal 9 + //accessing completely diff table + cmd.Execute("SELECT Name, rowguid AS UUID FROM Production.Product WHERE Name = @p1", "Chainring Nut") |> Seq.toArray |> Array.length |> should equal 1 + diff --git a/src/SqlClient.Tests/appWithInclude.config b/src/SqlClient.Tests/appWithInclude.config new file mode 100644 index 00000000..9ecccecc --- /dev/null +++ b/src/SqlClient.Tests/appWithInclude.config @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SqlClient.Tests/connectionStrings.config b/src/SqlClient.Tests/connectionStrings.config new file mode 100644 index 00000000..bf091f3b --- /dev/null +++ b/src/SqlClient.Tests/connectionStrings.config @@ -0,0 +1,4 @@ + + + + diff --git a/src/SqlClient/AssemblyInfo.fs b/src/SqlClient/AssemblyInfo.fs index 6272042a..61dc0d13 100644 --- a/src/SqlClient/AssemblyInfo.fs +++ b/src/SqlClient/AssemblyInfo.fs @@ -4,9 +4,9 @@ open System.Reflection [] [] [] -[] -[] +[] +[] do () module internal AssemblyVersionInformation = - let [] Version = "1.5.8" + let [] Version = "1.5.9" diff --git a/src/SqlClient/SqlClientExtensions.fs b/src/SqlClient/SqlClientExtensions.fs index 5df641f3..ad9f740f 100644 --- a/src/SqlClient/SqlClientExtensions.fs +++ b/src/SqlClient/SqlClientExtensions.fs @@ -27,6 +27,11 @@ module SqlDataReader = let internal getOption<'a> (key: string) (reader: SqlDataReader) = let v = reader.[key] if Convert.IsDBNull v then None else Some(unbox<'a> v) + + let internal getValueOrDefault<'a> (key: string) defaultValue (reader: SqlDataReader) = + let v = reader.[key] + if Convert.IsDBNull v then defaultValue else unbox<'a> v + let DbNull = box DBNull.Value @@ -259,14 +264,15 @@ type SqlConnection with while reader.Read() do let user_type_id = reader |> SqlDataReader.getOption "user_type_id" let system_type_id = reader.["system_type_id"] |> unbox + let x = { Column.Name = string reader.["name"] Ordinal = unbox reader.["column_ordinal"] TypeInfo = findTypeInfoBySqlEngineTypeId (this.ConnectionString, system_type_id, user_type_id) IsNullable = unbox reader.["is_nullable"] MaxLength = reader.["max_length"] |> unbox |> int - ReadOnly = not(unbox reader.["is_updateable"]) - Identity = unbox reader.["is_identity_column"] + ReadOnly = not( SqlDataReader.getValueOrDefault "is_updateable" true reader) + Identity = SqlDataReader.getValueOrDefault "is_identity_column" false reader } yield x ]