Skip to content

Commit

Permalink
Handle line wrapped FROM queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrattli committed Apr 27, 2021
1 parent 46d4e2d commit 6d4efb8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/NpgsqlFSharpParser/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr, unit> =
simpleIdentifier |>> Expr.Ident
Expand Down Expand Up @@ -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))
Expand Down
36 changes: 34 additions & 2 deletions tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
""" {
Expand All @@ -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") ]
Expand All @@ -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")) ]
Expand All @@ -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)
}
]

0 comments on commit 6d4efb8

Please sign in to comment.