From 9f7565889cf773259de00aa4e4d9b702628cec37 Mon Sep 17 00:00:00 2001 From: Zaid Date: Fri, 9 Apr 2021 00:37:23 +0200 Subject: [PATCH] Npgsql.FSharp.Analyzer v3.24.0 fixes #27 and other problems with respect to dynamically applied parameters --- RELEASE_NOTES.md | 3 ++ src/FParsec/AssemblyInfo.fs | 20 ++++---- src/NpgsqlFSharpAnalyzer.Core/AssemblyInfo.fs | 20 ++++---- .../SyntacticAnalysis.fs | 49 ++++++++++++++---- src/NpgsqlFSharpAnalyzer/AssemblyInfo.fs | 20 ++++---- src/NpgsqlFSharpParser/AssemblyInfo.fs | 20 ++++---- .../source.extension.vsixmanifest | 2 +- src/Ubik/AssemblyInfo.fs | 20 ++++---- src/Ubik/Ubik.fsproj | 4 +- .../AssemblyInfo.fs | 20 ++++---- tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs | 22 +++++++- tests/examples/hashing/AssemblyInfo.fs | 20 ++++---- .../castingNonNullableStaysNonNullable.fs | 2 +- tests/examples/hashing/examples.fsproj | 3 +- .../examples/hashing/parameterTypeMismatch.fs | 1 - .../hashing/readAttemptIntegerTypeMismatch.fs | 1 - .../hashing/semanticAnalysis-missingColumn.fs | 1 - .../semanticAnalysis-redundantParameters.fs | 1 - .../hashing/semanticAnalysis-typeMismatch.fs | 1 - tests/examples/hashing/syntacticAnalysis.fs | 50 +++++++++++++++---- .../hashing/syntacticAnalysisExecuteScalar.fs | 1 - .../syntacticAnalysisFromSingleCaseUnion.fs | 2 +- .../hashing/syntacticAnalysisProcessedList.fs | 2 +- .../hashing/syntacticAnalysisSimpleQuery.fs | 1 - .../syntaxAnalysis-usingTransactions.fs | 2 +- ...nalysisReferencingQueryDoesNotGiveError.fs | 3 +- .../hashing/usingProcessedParameters.fs | 14 ++++++ 27 files changed, 199 insertions(+), 106 deletions(-) create mode 100644 tests/examples/hashing/usingProcessedParameters.fs diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 004442f..9d4cee0 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,6 @@ +### 3.24.0 - 2021-04-09 +* fixes dynamically applied parameters with more complex expressions + ### 3.23.0 - 2021-03-26 * Detect typed let bindings diff --git a/src/FParsec/AssemblyInfo.fs b/src/FParsec/AssemblyInfo.fs index b5d6270..bc4e4dd 100644 --- a/src/FParsec/AssemblyInfo.fs +++ b/src/FParsec/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "FParsec" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/src/NpgsqlFSharpAnalyzer.Core/AssemblyInfo.fs b/src/NpgsqlFSharpAnalyzer.Core/AssemblyInfo.fs index f504a8e..ac5c6a5 100644 --- a/src/NpgsqlFSharpAnalyzer.Core/AssemblyInfo.fs +++ b/src/NpgsqlFSharpAnalyzer.Core/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "NpgsqlFSharpAnalyzer.Core" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs b/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs index a11383e..87e63d1 100644 --- a/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs +++ b/src/NpgsqlFSharpAnalyzer.Core/SyntacticAnalysis.fs @@ -54,6 +54,8 @@ module SyntacticAnalysis = let (|ParameterTuple|_|) = function | SynExpr.Tuple(isStruct, [ SynExpr.Const(SynConst.String(parameterName, paramRange), constRange); Apply(funcName, exprArgs, funcRange, appRange) ], commaRange, tupleRange) -> Some (parameterName, paramRange, funcName, funcRange, Some appRange) + | SynExpr.Tuple(isStruct, [ SynExpr.Const(SynConst.String(parameterName, paramRange), constRange); SynExpr.Paren(Apply(funcName, exprArgs, funcRange, appRange), leftParenRange, rightParenRange, _) ], commaRange, tupleRange) -> + Some (parameterName, paramRange, funcName, funcRange, Some appRange) | SynExpr.Tuple(isStruct, [ SynExpr.Const(SynConst.String(parameterName, paramRange), constRange); secondItem ], commaRange, tupleRange) -> match secondItem with | SynExpr.LongIdent(isOptional, longDotId, altName, identRange) -> @@ -65,9 +67,27 @@ module SyntacticAnalysis = |> String.concat "." Some (parameterName, paramRange, fullName, identRange, None) - | _ -> + | Apply(funcName, innerArg, funcRange, appRange) -> + Some (parameterName, paramRange, funcName, funcRange, None) + + | SynExpr.App(flag, inFix, funcExpr, argExpr, fullAppRange) -> + match argExpr with + | SynExpr.LongIdent(isOptional, longDotId, altName, identRange) -> + match longDotId with + | LongIdentWithDots(listOfIds, ranges) -> + let fullName = + listOfIds + |> List.map (fun id -> id.idText) + |> String.concat "." + + Some (parameterName, paramRange, fullName, identRange, None) + | Apply(funcName, innerArg, funcRange, appRange) -> + Some (parameterName, paramRange, funcName, funcRange, None) + | otherwise -> + None + | otherwise -> None - | _ -> + | otherwise -> None let rec readParameters = function @@ -75,6 +95,8 @@ module SyntacticAnalysis = [ name, range, func, funcRange, appRange ] | SynExpr.Sequential(_debugSeqPoint, isTrueSeq, expr1, expr2, seqRange) -> [ yield! readParameters expr1; yield! readParameters expr2 ] + | SynExpr.Paren(ParameterTuple (name, range, func, funcRange, appRange), _, _, _) -> + [ name, range, func, funcRange, appRange ] | _ -> [ ] @@ -89,9 +111,9 @@ module SyntacticAnalysis = match listExpr with | SynExpr.CompExpr(isArrayOfList, isNotNakedRefCell, compExpr, compRange) -> Some (readParameters compExpr, compRange) - | _ -> + | otherwise -> None - | _ -> + | otherwise -> None let readParameterSets parameterSetsExpr = @@ -125,7 +147,7 @@ module SyntacticAnalysis = range = range applicationRange = appRange } - | _ -> + | otherwise -> () ] @@ -146,7 +168,7 @@ module SyntacticAnalysis = range = range applicationRange = appRange } - | _ -> + | otherwise -> () ] @@ -240,7 +262,7 @@ module SyntacticAnalysis = } Some transaction - | _ -> + | otherwise -> None let rec readTransactionQueries = function @@ -261,7 +283,13 @@ module SyntacticAnalysis = Some (readTransactionQueries compExpr) | _ -> None - | _ -> + | Apply (("Sql.executeTransaction"|"Sql.executeTransactionAsync"), SynExpr.ArrayOrList(isList, listExpr, range), funcRange, appRange) -> + let expressions = + listExpr + |> List.collect readTransactionQueries + + Some expressions + | otherwise -> None let (|ReadColumnAttempt|_|) = function @@ -382,7 +410,7 @@ module SyntacticAnalysis = | SynExpr.App(exprAtomic, isInfix, funcExpr, argExpr, range) -> [ yield! findParameters funcExpr; yield! findParameters argExpr ] - | _ -> + | otherwise -> [ ] let rec findExecuteTransaction = function @@ -592,6 +620,7 @@ module SyntacticAnalysis = yield! findQuery funcExpr yield! findParameters funcExpr yield! findSkipAnalysis funcExpr + yield! findExecuteTransaction funcExpr ] [ { blocks = blocks; range = range; } ] @@ -601,6 +630,7 @@ module SyntacticAnalysis = yield! findFunc funcExpr yield! findQuery funcExpr yield! findParameters funcExpr + yield! findExecuteTransaction funcExpr yield SqlAnalyzerBlock.SkipAnalysis yield SqlAnalyzerBlock.ReadingColumns (findReadColumnAttempts funcExpr) ] @@ -613,6 +643,7 @@ module SyntacticAnalysis = yield! findQuery funcExpr yield! findParameters funcExpr yield! findSkipAnalysis funcExpr + yield! findExecuteTransaction funcExpr yield SqlAnalyzerBlock.ReadingColumns (findReadColumnAttempts funcExpr) ] diff --git a/src/NpgsqlFSharpAnalyzer/AssemblyInfo.fs b/src/NpgsqlFSharpAnalyzer/AssemblyInfo.fs index fe204f0..e3d4e11 100644 --- a/src/NpgsqlFSharpAnalyzer/AssemblyInfo.fs +++ b/src/NpgsqlFSharpAnalyzer/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "NpgsqlFSharpAnalyzer" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/src/NpgsqlFSharpParser/AssemblyInfo.fs b/src/NpgsqlFSharpParser/AssemblyInfo.fs index 08dc4c7..e2232a8 100644 --- a/src/NpgsqlFSharpParser/AssemblyInfo.fs +++ b/src/NpgsqlFSharpParser/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "NpgsqlFSharpParser" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/src/NpgsqlFSharpVs/source.extension.vsixmanifest b/src/NpgsqlFSharpVs/source.extension.vsixmanifest index ce4d96b..8abe11c 100644 --- a/src/NpgsqlFSharpVs/source.extension.vsixmanifest +++ b/src/NpgsqlFSharpVs/source.extension.vsixmanifest @@ -3,7 +3,7 @@ xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011"> - + NpgsqlFSharpVs F# Analyzer for embedded SQL syntax analysis, type-checking for parameters and result sets and nullable column detection when writing queries using Npgsql.FSharp. https://github.com/Zaid-Ajaj/Npgsql.FSharp.Analyzer diff --git a/src/Ubik/AssemblyInfo.fs b/src/Ubik/AssemblyInfo.fs index 22c830d..d7e72f0 100644 --- a/src/Ubik/AssemblyInfo.fs +++ b/src/Ubik/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "Ubik" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/src/Ubik/Ubik.fsproj b/src/Ubik/Ubik.fsproj index e9e3a29..730bf8a 100644 --- a/src/Ubik/Ubik.fsproj +++ b/src/Ubik/Ubik.fsproj @@ -7,8 +7,8 @@ true true Major - 3.23.0 - Detect typed let bindings + 3.24.0 + Fixes complex applied expressions to the diff --git a/tests/NpgsqlFSharpAnalyzer.Tests/AssemblyInfo.fs b/tests/NpgsqlFSharpAnalyzer.Tests/AssemblyInfo.fs index 1064955..92fbad8 100644 --- a/tests/NpgsqlFSharpAnalyzer.Tests/AssemblyInfo.fs +++ b/tests/NpgsqlFSharpAnalyzer.Tests/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "NpgsqlFSharpAnalyzer.Tests" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs b/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs index dd010b9..a006f02 100644 --- a/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs +++ b/tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs @@ -38,7 +38,7 @@ let tests = | None -> failwith "Could not crack project" | Some context -> let operationBlocks = SyntacticAnalysis.findSqlOperations context - Expect.equal (List.length operationBlocks) 12 "Found 12 operation blocks" + Expect.equal (List.length operationBlocks) 15 "Found 15 operation blocks" } test "Syntactic analysis: no SQL blocks should be found using sprintf" { @@ -49,6 +49,26 @@ let tests = Expect.isEmpty operations "There should be no syntactic blocks" } + test "Syntactic Analysis: finding processed parameters" { + match context (find "../examples/hashing/usingProcessedParameters.fs") with + | None -> failwith "Could not crack project" + | Some context -> + let operationBlocks = SyntacticAnalysis.findSqlOperations context + Expect.equal (List.length operationBlocks) 1 "Found 1 operation" + let parameters = + [ + for operation in operationBlocks do + for block in operation.blocks do + match block with + | SqlAnalyzerBlock.Parameters (parameters, _) -> + yield! parameters + | _ -> + () + ] + + Expect.equal 2 parameters.Length "There are 2 parameters" + } + test "Syntactic analysis: SQL block found from top-level expression in module" { match context (find "../examples/hashing/topLevelExpressionIsDetected.fs") with | None -> failwith "Could not crack project" diff --git a/tests/examples/hashing/AssemblyInfo.fs b/tests/examples/hashing/AssemblyInfo.fs index bead391..aedc145 100644 --- a/tests/examples/hashing/AssemblyInfo.fs +++ b/tests/examples/hashing/AssemblyInfo.fs @@ -4,20 +4,20 @@ open System.Reflection [] [] -[] -[] -[] -[] +[] +[] +[] +[] [] -[] +[] do () module internal AssemblyVersionInformation = let [] AssemblyTitle = "examples" let [] AssemblyProduct = "NpgsqlFSharpAnalyzer" - let [] AssemblyVersion = "3.23.0" - let [] AssemblyMetadata_ReleaseDate = "2021-03-26T00:00:00.0000000" - let [] AssemblyFileVersion = "3.23.0" - let [] AssemblyInformationalVersion = "3.23.0" + let [] AssemblyVersion = "3.24.0" + let [] AssemblyMetadata_ReleaseDate = "2021-04-09T00:00:00.0000000" + let [] AssemblyFileVersion = "3.24.0" + let [] AssemblyInformationalVersion = "3.24.0" let [] AssemblyMetadata_ReleaseChannel = "release" - let [] AssemblyMetadata_GitHash = "bc8bff18abbd6b4cfd14b67738c9ec22a5412c90" + let [] AssemblyMetadata_GitHash = "025b5de26ae76e867d47a4047d85d2ab5683940b" diff --git a/tests/examples/hashing/castingNonNullableStaysNonNullable.fs b/tests/examples/hashing/castingNonNullableStaysNonNullable.fs index fb2ee47..49ddd41 100644 --- a/tests/examples/hashing/castingNonNullableStaysNonNullable.fs +++ b/tests/examples/hashing/castingNonNullableStaysNonNullable.fs @@ -1,6 +1,6 @@ module castingNonNullableStaysNonNullable -open Npgsql.FSharp.Tasks +open Npgsql.FSharp let userIds connection = connection diff --git a/tests/examples/hashing/examples.fsproj b/tests/examples/hashing/examples.fsproj index 8604673..31a8bdd 100644 --- a/tests/examples/hashing/examples.fsproj +++ b/tests/examples/hashing/examples.fsproj @@ -5,6 +5,7 @@ + @@ -39,7 +40,7 @@ - + diff --git a/tests/examples/hashing/parameterTypeMismatch.fs b/tests/examples/hashing/parameterTypeMismatch.fs index 336ea92..cfd7e6e 100644 --- a/tests/examples/hashing/parameterTypeMismatch.fs +++ b/tests/examples/hashing/parameterTypeMismatch.fs @@ -1,7 +1,6 @@ module parameterTypeMismatch open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/readAttemptIntegerTypeMismatch.fs b/tests/examples/hashing/readAttemptIntegerTypeMismatch.fs index a334378..11298bc 100644 --- a/tests/examples/hashing/readAttemptIntegerTypeMismatch.fs +++ b/tests/examples/hashing/readAttemptIntegerTypeMismatch.fs @@ -1,7 +1,6 @@ module readAttemptIntegerTypeMismatch open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/semanticAnalysis-missingColumn.fs b/tests/examples/hashing/semanticAnalysis-missingColumn.fs index 415a65e..b209b38 100644 --- a/tests/examples/hashing/semanticAnalysis-missingColumn.fs +++ b/tests/examples/hashing/semanticAnalysis-missingColumn.fs @@ -1,7 +1,6 @@ module semanticAnalysis open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/semanticAnalysis-redundantParameters.fs b/tests/examples/hashing/semanticAnalysis-redundantParameters.fs index 999e22f..3b5a001 100644 --- a/tests/examples/hashing/semanticAnalysis-redundantParameters.fs +++ b/tests/examples/hashing/semanticAnalysis-redundantParameters.fs @@ -1,7 +1,6 @@ module semanticAnalysis_redundantParameters open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/semanticAnalysis-typeMismatch.fs b/tests/examples/hashing/semanticAnalysis-typeMismatch.fs index 882a9fd..abc2bf0 100644 --- a/tests/examples/hashing/semanticAnalysis-typeMismatch.fs +++ b/tests/examples/hashing/semanticAnalysis-typeMismatch.fs @@ -1,7 +1,6 @@ module semanticAnalysis_typeMismatch open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/syntacticAnalysis.fs b/tests/examples/hashing/syntacticAnalysis.fs index 3590a9a..d7e966f 100644 --- a/tests/examples/hashing/syntacticAnalysis.fs +++ b/tests/examples/hashing/syntacticAnalysis.fs @@ -1,7 +1,14 @@ module Postgres open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow + +type OptionBuilder() = + member x.Bind(v,f) = Option.bind f v + member x.Return v = Some v + member x.ReturnFrom o = o + member x.Zero () = None + +let option = OptionBuilder() let connectionString = "Dummy connection string" @@ -51,6 +58,25 @@ let executeFunction() = |> Sql.func "getNumberOfUsers" |> Sql.execute (fun read -> read.text "username") +let executeFunctionAsync() = + Sql.host "localhost" + |> Sql.connectFromConfig + |> Sql.func "getNumberOfUsers" + |> Sql.executeAsync (fun read -> read.text "username") + |> Async.AwaitTask + +let executeTransactionAsync() = + Sql.host "localhost" + |> Sql.connectFromConfig + |> Sql.executeTransactionAsync [ "SOME QUERY", [ [ "@someNumber", Sql.int 42 ] ] ] + |> Async.AwaitTask + +let executeTransactionWithEmptyList() = + Sql.host "localhost" + |> Sql.connectFromConfig + |> Sql.executeTransactionAsync [ ] + |> Async.AwaitTask + type Whatever() = member this.Hello = let whateverSql() = @@ -69,17 +95,21 @@ let doCoolStuff() = async { } let doCoolStuffAsync() = async { - return! Sql.host "localhost" - |> Sql.connectFromConfig - |> Sql.query "SELECT COUNT(*) as count FROM users" - |> Sql.executeAsync (fun read -> read.int64 "count") + let value = + Sql.host "localhost" + |> Sql.connectFromConfig + |> Sql.query "SELECT COUNT(*) as count FROM users" + |> Sql.executeAsync (fun read -> read.int64 "count") + + return 1 } let doCoolStuffAsyncWithLet() = async { - let! x = Sql.host "localhost" - |> Sql.connectFromConfig - |> Sql.query "SELECT COUNT(*) as count FROM users" - |> Sql.executeAsync (fun read -> read.int64 "count") + let x = + Sql.host "localhost" + |> Sql.connectFromConfig + |> Sql.query "SELECT COUNT(*) as count FROM users" + |> Sql.executeAsync (fun read -> read.int64 "count") return x; } @@ -92,7 +122,7 @@ module InsidePostgres = |> Sql.query "SELECT COUNT(*) as count FROM users" |> Sql.execute (fun read -> read.int64 "count") - let nextedDeclaretionWithExplicitReturnType() : Result= + let nextedDeclaretionWithExplicitReturnType() : int64 list = Sql.host "localhost" |> Sql.connectFromConfig |> Sql.query "SELECT COUNT(*) as count FROM users" diff --git a/tests/examples/hashing/syntacticAnalysisExecuteScalar.fs b/tests/examples/hashing/syntacticAnalysisExecuteScalar.fs index 79fade2..d6f1d98 100644 --- a/tests/examples/hashing/syntacticAnalysisExecuteScalar.fs +++ b/tests/examples/hashing/syntacticAnalysisExecuteScalar.fs @@ -1,7 +1,6 @@ module syntacticAnalysisExecuteScalar open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs b/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs index 9fd53c3..1e6ba4e 100644 --- a/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs +++ b/tests/examples/hashing/syntacticAnalysisFromSingleCaseUnion.fs @@ -2,7 +2,7 @@ module SyntacticAnalysisFromSingleCaseUnion open Npgsql.FSharp -type SqlAction = SqlAction of (string -> Result) +type SqlAction = SqlAction of (string -> int list) let getData = SqlAction (fun (connectionString: string) -> diff --git a/tests/examples/hashing/syntacticAnalysisProcessedList.fs b/tests/examples/hashing/syntacticAnalysisProcessedList.fs index 99ee8b1..b2b60bd 100644 --- a/tests/examples/hashing/syntacticAnalysisProcessedList.fs +++ b/tests/examples/hashing/syntacticAnalysisProcessedList.fs @@ -1,7 +1,6 @@ module semanticAnalysisProcessedList open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" @@ -10,6 +9,7 @@ let findUsernames() = |> Sql.connect |> Sql.query "SELECT * FROM users" |> Sql.execute (fun read -> read.text "username") + |> Ok |> function | Error error -> None | Ok users -> Some users diff --git a/tests/examples/hashing/syntacticAnalysisSimpleQuery.fs b/tests/examples/hashing/syntacticAnalysisSimpleQuery.fs index 1984d38..c2cf3ef 100644 --- a/tests/examples/hashing/syntacticAnalysisSimpleQuery.fs +++ b/tests/examples/hashing/syntacticAnalysisSimpleQuery.fs @@ -1,7 +1,6 @@ module syntacticAnalysisSimpleQuery open Npgsql.FSharp -open Npgsql.FSharp.OptionWorkflow let connectionString = "Dummy connection string" diff --git a/tests/examples/hashing/syntaxAnalysis-usingTransactions.fs b/tests/examples/hashing/syntaxAnalysis-usingTransactions.fs index 7f8a66b..eaec44f 100644 --- a/tests/examples/hashing/syntaxAnalysis-usingTransactions.fs +++ b/tests/examples/hashing/syntaxAnalysis-usingTransactions.fs @@ -10,7 +10,7 @@ let executeMultipleQueries = [ "@user_id", Sql.int 42 ] [ "@user_id", Sql.int 43 ] [ "@user_id", Sql.int 44 ] - [ "@user_id", Sql.int 44 ] + [ "@user_id", (Sql.int 44) ] ] "DELETE FROM users", [ ] diff --git a/tests/examples/hashing/syntaxAnalysisReferencingQueryDoesNotGiveError.fs b/tests/examples/hashing/syntaxAnalysisReferencingQueryDoesNotGiveError.fs index 9e9b3a8..16dc784 100644 --- a/tests/examples/hashing/syntaxAnalysisReferencingQueryDoesNotGiveError.fs +++ b/tests/examples/hashing/syntaxAnalysisReferencingQueryDoesNotGiveError.fs @@ -2,7 +2,8 @@ module SyntaxAnalysisReferencingQueryDoesNotGiveError open Npgsql open Npgsql.FSharp -// this shouldn't be analyzed whe n query is dynamic + +// this shouldn't be analyzed when query is dynamic let deleteUsers connection = diff --git a/tests/examples/hashing/usingProcessedParameters.fs b/tests/examples/hashing/usingProcessedParameters.fs new file mode 100644 index 0000000..a59d608 --- /dev/null +++ b/tests/examples/hashing/usingProcessedParameters.fs @@ -0,0 +1,14 @@ +module UsingProcessedParameters + +open Npgsql.FSharp + +let add x y = x + y + +let findRoles connection = + connection + |> Sql.query "SELECT * FROM users WHERE username = @username" + |> Sql.parameters [ + "@username", "Hello" |> Sql.text + "@user_id", 5 |> add 10 |> Sql.int + ] + |> Sql.execute (fun read -> read.int "user_id")