From 6d4efb802f0cba93fc03a4213ff7f397f3cd2dc6 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Tue, 27 Apr 2021 16:10:09 +0200 Subject: [PATCH] Handle line wrapped FROM queries --- src/NpgsqlFSharpParser/Parser.fs | 10 +++--- .../ParseSelectTests.fs | 36 +++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/NpgsqlFSharpParser/Parser.fs b/src/NpgsqlFSharpParser/Parser.fs index 7829782..da11d2e 100644 --- a/src/NpgsqlFSharpParser/Parser.fs +++ b/src/NpgsqlFSharpParser/Parser.fs @@ -109,15 +109,15 @@ let simpleIdentifier = attempt( stringIdentifier >>= fun schema -> text "." >>. stringIdentifier >>= fun table -> - text "." >>. stringIdentifier .>> spaces >>= fun column -> + text "." >>. stringIdentifier >>= fun column -> preturn (sprintf "%s.%s.%s" schema table column)) <|> attempt( stringIdentifier >>= fun table -> - text "." >>. stringIdentifier .>> spaces >>= fun column -> + text "." >>. stringIdentifier >>= fun column -> preturn (sprintf "%s.%s" table column)) <|> - stringIdentifier + attempt stringIdentifier let identifier : Parser = simpleIdentifier |>> Expr.Ident @@ -261,14 +261,14 @@ let optionalHavingClause = optionalExpr (text "HAVING" >>. expr) let optionalFrom = optionalExpr ( attempt ( - text "FROM " >>. (parens selectQuery) >>= fun subQuery -> + text "FROM" >>. (parens selectQuery) >>= fun subQuery -> optional (text "AS") >>= fun _ -> simpleIdentifier >>= fun alias -> preturn (Expr.As(subQuery, Expr.Ident alias)) ) <|> attempt ( - text "FROM " >>. simpleIdentifier >>= fun table -> + text "FROM" >>. simpleIdentifier >>= fun table -> optional (text "AS") >>= fun _ -> simpleIdentifier >>= fun alias -> preturn (Expr.As(Expr.Ident table, Expr.Ident alias)) diff --git a/tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs b/tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs index 5b6f8a6..0227b61 100644 --- a/tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs +++ b/tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs @@ -442,6 +442,21 @@ let selectQueryTests = testList "Parse SELECT tests" [ Limit = Some (Expr.Integer 1) } + testSelect """ + SELECT * + FROM + (SELECT NOW()) time + LIMIT 1 + """ { + SelectExpr.Default with + Columns = [Expr.Star] + From = Some (Expr.As (Expr.SelectQuery { + SelectExpr.Default with + Columns = [Expr.Function ("NOW", [])] + }, Expr.Ident "time")) + Limit = Some (Expr.Integer 1) + } + testSelect """ SELECT "username" FROM "users" """ { @@ -451,7 +466,7 @@ let selectQueryTests = testList "Parse SELECT tests" [ } testSelect """ - SELECT "username" as "name" FROM "users" + SELECT "username" AS "name" FROM "users" """ { SelectExpr.Default with Columns = [ Expr.As(Expr.Ident "username", Expr.Ident "name") ] @@ -467,7 +482,7 @@ let selectQueryTests = testList "Parse SELECT tests" [ } testSelect """ - SELECT "$Table".timestamp as ts FROM "$Table" + SELECT "$Table".timestamp AS ts FROM "$Table" """ { SelectExpr.Default with Columns = [ Expr.As(Expr.Ident("$Table.timestamp"), Expr.Ident("ts")) ] @@ -481,4 +496,21 @@ let selectQueryTests = testList "Parse SELECT tests" [ Columns = [ Expr.Ident("public.$Table.timestamp") ] From = Expr.Ident "$Table" |> Some } + + testSelect """ + SELECT public."$Table"."timestamp" FROM "$Table" AS tbl + """ { + SelectExpr.Default with + Columns = [ Expr.Ident("public.$Table.timestamp") ] + From = Expr.As(Expr.Ident "$Table", Expr.Ident "tbl") |> Some + } + + testSelect """ + SELECT public."$Table"."timestamp" FROM "$Table" tbl LIMIT 100 + """ { + SelectExpr.Default with + Columns = [ Expr.Ident("public.$Table.timestamp") ] + From = Expr.As(Expr.Ident "$Table", Expr.Ident "tbl") |> Some + Limit = Some (Expr.Integer 100) + } ]