Skip to content

Commit

Permalink
Detect queries within lambda expressions wrapped in single case unions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Francesconi committed Dec 8, 2020
1 parent 1d6d26b commit 784bcf6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ module SyntacticAnalysis =
]

[ { blocks = blocks; range = range; } ]
| SynExpr.Paren(innerExpr, leftRange, rightRange, range) ->
visitSyntacticExpression innerExpr range
| _ ->
[ ]
| SynExpr.LetOrUse(isRecursive, isUse, bindings, body, range) ->
Expand Down
8 changes: 8 additions & 0 deletions tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ let tests =
Expect.equal 1 operations.Length "There should be one syntactic block found"
}

test "Syntactic analysis: SQL block found from lambda body wrapped in single case union" {
match context (find "../examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs") with
| None -> failwith "Could not crack project"
| Some context ->
let operations = SyntacticAnalysis.findSqlOperations context
Expect.equal 1 operations.Length "There should be one syntactic block found"
}

test "Syntactic Analysis: reading queries with [<Literal>] query" {
match context (find "../examples/hashing/syntacticAnalysis-literalStrings.fs") with
| None -> failwith "Could not crack project"
Expand Down
1 change: 1 addition & 0 deletions tests/examples/hashing/examples.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<Compile Include="castingNonNullableStaysNonNullable.fs" />
<Compile Include="syntacticAnalysisFromLambdaBody.fs" />
<Compile Include="syntacticAnalysisFromSingleCaseUnion.fs" />
<Compile Include="topLevelExpressionIsDetected.fs" />
<Compile Include="usingLiteralQueriesWithTransactions.fs" />
<Compile Include="detectingDynamicListsInTransactions.fs" />
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/hashing/syntacticAnalysisFromLambdaBody.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module SyntacticAnalysisFromLambdaBody
open Npgsql.FSharp

let getData =
fun (connectionSring: string) ->
connectionSring
fun (connectionString: string) ->
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM users WHERE user_id = @user_id"
|> Sql.parameters [ "@user_id", Sql.int 42 ]
Expand Down
13 changes: 13 additions & 0 deletions tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module SyntacticAnalysisFromSingleCaseUnion

open Npgsql.FSharp

type SqlAction = SqlAction of (string -> Result<int list, exn>)

let getData =
SqlAction (fun (connectionString: string) ->
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM users WHERE user_id = @user_id"
|> Sql.parameters [ "@user_id", Sql.int 42 ]
|> Sql.execute (fun read -> read.int "user_id"))

0 comments on commit 784bcf6

Please sign in to comment.