From 784bcf6618c1563cafd39f9b3af82ea7d62ee979 Mon Sep 17 00:00:00 2001 From: Daniel Francesconi <> Date: Tue, 8 Dec 2020 08:43:08 +0100 Subject: [PATCH] Detect queries within lambda expressions wrapped in single case unions --- src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs | 2 ++ tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs | 8 ++++++++ tests/examples/hashing/examples.fsproj | 1 + .../hashing/syntacticAnalysisFromLambdaBody.fs | 4 ++-- .../hashing/syntacticAnalysisFromSingleCaseUnion.fs | 13 +++++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs diff --git a/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs b/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs index 5305c34..dd014b9 100644 --- a/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs +++ b/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs @@ -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) -> diff --git a/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs b/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs index 2ec54e4..a3627b9 100644 --- a/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs +++ b/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs @@ -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 [] query" { match context (find "../examples/hashing/syntacticAnalysis-literalStrings.fs") with | None -> failwith "Could not crack project" diff --git a/tests/examples/hashing/examples.fsproj b/tests/examples/hashing/examples.fsproj index 6311395..8604673 100644 --- a/tests/examples/hashing/examples.fsproj +++ b/tests/examples/hashing/examples.fsproj @@ -7,6 +7,7 @@ + diff --git a/tests/examples/hashing/syntacticAnalysisFromLambdaBody.fs b/tests/examples/hashing/syntacticAnalysisFromLambdaBody.fs index bd5ab62..4ebecea 100644 --- a/tests/examples/hashing/syntacticAnalysisFromLambdaBody.fs +++ b/tests/examples/hashing/syntacticAnalysisFromLambdaBody.fs @@ -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 ] diff --git a/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs b/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs new file mode 100644 index 0000000..9fd53c3 --- /dev/null +++ b/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs @@ -0,0 +1,13 @@ +module SyntacticAnalysisFromSingleCaseUnion + +open Npgsql.FSharp + +type SqlAction = SqlAction of (string -> Result) + +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"))