Skip to content

Commit

Permalink
Query v2.2.1 - Allowed selected columns of a leftJoined table to be o…
Browse files Browse the repository at this point in the history
…ptional by wrapping them with `Some`.
  • Loading branch information
JordanMarr committed Nov 10, 2023
1 parent 9e36b6a commit f13ab61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/SqlHydra.Query/LinqExpressionVisitors.fs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ let visitWhere<'T> (filter: Expression<Func<'T, bool>>) (qualifyColumn: string -
| nameof like | nameof op_EqualsPercent -> query.WhereLike(fqCol, pattern, false)
| _ -> query.WhereNotLike(fqCol, pattern, false)
| _ -> notImpl()
| MethodCall m when m.Method.Name = nameof isNullValue || m.Method.Name = nameof isNotNullValue ->
| MethodCall m when List.contains m.Method.Name [ nameof isNullValue; "IsNull"; nameof isNotNullValue ] ->
match m.Arguments.[0] with
| Property p ->
let alias = visitAlias p.Expression
let fqCol = qualifyColumn alias p.Member
if m.Method.Name = nameof isNullValue
if m.Method.Name = nameof isNullValue || m.Method.Name = "IsNull" // CompiledName for `isNull` = `IsNull`
then query.WhereNull(fqCol)
else query.WhereNotNull(fqCol)
| _ -> notImpl()
Expand Down Expand Up @@ -668,6 +668,9 @@ let visitSelect<'T, 'Prop> (propertySelector: Expression<Func<'T, 'Prop>>) =
| MethodCall m when m.Method.Name = "Invoke" ->
// Handle tuples
visit m.Object
| MethodCall m when m.Method.Name = "Some" ->
// Columns selected from leftJoined tables may be wrapped in `Some` to make them optional.
visit m.Arguments.[0]
| AggregateColumn (aggType, p) ->
let alias = visitAlias p.Expression
[ SelectedAggregateColumn (aggType, alias, p.Member.Name) ]
Expand Down
2 changes: 1 addition & 1 deletion src/SqlHydra.Query/SqlHydra.Query.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<WarnOn>3390;$(WarnOn)</WarnOn>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Description>SqlHydra.Query is an F# query builder powered by SqlKata.Query that supports SQL Server, PostgreSQL, Sqlite and Oracle.</Description>
<WarningsAsErrors>
<!-- Incomplete pattern matches on this expression. -->
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/SqlServer/QueryIntegrationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,25 @@ let ``Guid getId Bug Repro Issue 38``() = task {

guid <>! System.Guid.Empty
}

[<Test>]
let ``Individual column from a leftJoin table should be optional if Some``() = task {
let! results =
selectTask HydraReader.Read (Create openContext) {
for o in Sales.SalesOrderHeader do
leftJoin sr in Sales.SalesOrderHeaderSalesReason on (o.SalesOrderID = sr.Value.SalesOrderID)
leftJoin r in Sales.SalesReason on (sr.Value.SalesReasonID = r.Value.SalesReasonID)
where (isNullValue r.Value.Name)
select (o.SalesOrderID, Some r.Value.ReasonType, Some r.Value.Name)
take 10
}

let reasonsExist =
results
|> Seq.forall (fun (id, reasonType, name) ->
reasonType <> None && name <> None
)

gt0 results
reasonsExist =! false
}
14 changes: 14 additions & 0 deletions src/Tests/SqlServer/QueryUnitTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,17 @@ let ``Underscore Assignment Edge Case - insert - should fail with not supported`
with
| :? System.NotSupportedException -> Assert.Pass()
| ex -> Assert.Fail("Should fail with NotSupportedException")

[<Test>]
let ``Individual column from a leftJoin table should be optional if Some``() =
let query =
select {
for o in Sales.SalesOrderHeader do
leftJoin d in Sales.SalesOrderDetail on (o.SalesOrderID = d.Value.SalesOrderID)
select (Some d.Value.OrderQty)
}

let sql = query |> toSql
sql =! """SELECT [d].[OrderQty] FROM [Sales].[SalesOrderHeader] AS [o]
LEFT JOIN [Sales].[SalesOrderDetail] AS [d] ON ([o].[SalesOrderID] = [d].[SalesOrderID])"""

0 comments on commit f13ab61

Please sign in to comment.