Skip to content

Commit

Permalink
Use int64 for Integer to cover BigInt as well.
Browse files Browse the repository at this point in the history
Revert double

Revert attempt
  • Loading branch information
dbrattli committed May 12, 2021
1 parent a68622a commit 89d6451
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
38 changes: 8 additions & 30 deletions src/NpgsqlFSharpParser/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ let text value : Parser<string, unit> =
spaces >>. pstringCI value .>> spacesOrComment

let star : Parser<Expr, unit> =
attempt (text "*" |>> fun _ -> Expr.Star)
text "*" |>> fun _ -> Expr.Star

let opp = new OperatorPrecedenceParser<Expr, unit, unit>()

Expand All @@ -158,22 +158,8 @@ let parens parser = between (text "(") (text ")") parser
let comma = text ","

let integer : Parser<Expr, unit> =
attempt (
spaces >>. pint32 .>> spacesOrComment
|>> Expr.Integer
)

let bigint : Parser<Expr, unit> =
attempt (
spaces >>. pint64 .>> spacesOrComment
|>> Expr.BigInt
)

let smallint : Parser<Expr, unit> =
attempt (
spaces >>. pint16 .>> spacesOrComment
|>> Expr.SmallInt
)
spaces >>. pint64 .>> spacesOrComment
|>> Expr.Integer

let number : Parser<Expr, unit> =
spaces >>. pfloat .>> spacesOrComment
Expand All @@ -190,25 +176,21 @@ let date : Parser<Expr, unit> =
|>> Expr.Date

let boolean : Parser<Expr, unit> =
attempt(
(text "true" |>> fun _ -> Expr.Boolean true)
<|> (text "false" |>> fun _ -> Expr.Boolean false)
)
(text "true" |>> fun _ -> Expr.Boolean true)
<|> (text "false" |>> fun _ -> Expr.Boolean false)

// Parses any string between double quotes
let quotedString =
(attempt (pstring "''") |>> fun _ -> String.Empty)
<|> (skipChar '\'' |> anyStringBetween <| skipChar '\'')

let stringLiteral : Parser<Expr, unit> =
attempt(
quotedString .>> spacesOrComment
|>> Expr.StringLiteral
)
quotedString .>> spacesOrComment
|>> Expr.StringLiteral

/// Parses 2 or more comma separated values. I.e (1, 2), but not (3) which will become an integer.
let valueList =
let numeric = number <|> integer <|> bigint
let numeric = integer <|> number
attempt(
numeric .>> (pstring ",") >>= fun head ->
sepBy1 numeric (pstring ",") >>= fun tail ->
Expand Down Expand Up @@ -428,8 +410,6 @@ let updateQuery =
let toOrEquals =
text "=" <|> text "TO"



// TODO: SET TIME ZONE value is an alias for SET timezone TO value
let setQuery =
text "SET" >>.
Expand Down Expand Up @@ -506,9 +486,7 @@ opp.TermParser <- choice [
(text "(") >>. expr .>> (text ")")
valueList
star
// smallint
integer
bigint
boolean
number
date
Expand Down
5 changes: 1 addition & 4 deletions src/NpgsqlFSharpParser/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ type Expr =
| Parameter of string
| Boolean of bool
| StringLiteral of string
| Integer of int
| BigInt of int64
| SmallInt of int16
| Integer of int64
| Float of float
| Double of double
| Date of string
| Timestamp of string
| Function of name:string * arguments:Expr list
Expand Down
36 changes: 18 additions & 18 deletions tests/NpgsqlFSharpAnalyzer.Tests/ParseSelectTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ let selectQueryTests = testList "Parse SELECT tests" [
}

testSelect "SELECT 1" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect "SELECT 36109712494634" {
SelectExpr.Default with Columns = [Expr.BigInt 36109712494634L]
SelectExpr.Default with Columns = [Expr.Integer 36109712494634L]
}

testSelect "SELECT ''" {
SelectExpr.Default with Columns = [Expr.StringLiteral ""]
}

testSelect "SELECT 1::text" {
SelectExpr.Default with Columns = [Expr.TypeCast(Expr.Integer 1, Expr.Ident "text")]
SelectExpr.Default with Columns = [Expr.TypeCast(Expr.Integer 1L, Expr.Ident "text")]
}

testSelect "SELECT @input::text, value::ltree" {
Expand Down Expand Up @@ -121,7 +121,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [
Expr.As(Expr.StringConcat(Expr.Ident "ename", Expr.Ident "empno"), Expr.Ident "EmpDetails")
Expr.As(Expr.Function("COALESCE", [ Expr.Ident "comm"; Expr.Integer 0 ]), Expr.Ident "TOTALSAL")
Expr.As(Expr.Function("COALESCE", [ Expr.Ident "comm"; Expr.Integer 0L ]), Expr.Ident "TOTALSAL")
]

From = Some (Expr.Ident "sales")
Expand All @@ -143,7 +143,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [Expr.Function("COUNT", [Expr.Star]) ]
From = Some (Expr.Ident "users")
Limit = Some(Expr.Integer 10)
Limit = Some(Expr.Integer 10L)
}

testSelect "SELECT COUNT(*) FROM users LIMIT @numberOfRows" {
Expand Down Expand Up @@ -280,7 +280,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [Expr.Ident "username"; Expr.Ident "email"]
From = Some (Expr.Ident "users")
Where = Some (Expr.In(Expr.Ident "user_id", Expr.List([Expr.Integer 1; Expr.Integer 2; Expr.Integer 3])))
Where = Some (Expr.In(Expr.Ident "user_id", Expr.List([Expr.Integer 1L; Expr.Integer 2L; Expr.Integer 3L])))
}

testSelect """
Expand Down Expand Up @@ -411,7 +411,7 @@ let selectQueryTests = testList "Parse SELECT tests" [

Having = Some(Expr.GreaterThan(Expr.Function("SUM", [Expr.Ident "amount"]), Expr.Ident "users.salary"))

Limit = Some (Expr.Integer 20)
Limit = Some (Expr.Integer 20L)
}

testSelect """
Expand Down Expand Up @@ -443,9 +443,9 @@ let selectQueryTests = testList "Parse SELECT tests" [

Having = Some(Expr.GreaterThan(Expr.Function("SUM", [Expr.Ident "amount"]), Expr.Ident "users.salary"))

Limit = Some (Expr.Integer 20)
Limit = Some (Expr.Integer 20L)

Offset = Some (Expr.Integer 100)
Offset = Some (Expr.Integer 100L)
}

testSelect """
Expand All @@ -459,7 +459,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [Expr.Function ("NOW", [])]
}, Expr.Ident "time"))
Limit = Some (Expr.Integer 1)
Limit = Some (Expr.Integer 1L)
}

testSelect """
Expand All @@ -474,7 +474,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [Expr.Function ("NOW", [])]
}, Expr.Ident "time"))
Limit = Some (Expr.Integer 1)
Limit = Some (Expr.Integer 1L)
}

testSelect """
Expand Down Expand Up @@ -531,37 +531,37 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [ Expr.Ident("public.$Table.timestamp") ]
From = Expr.As(Expr.Ident "$Table", Expr.Ident "tbl") |> Some
Limit = Some (Expr.Integer 100)
Limit = Some (Expr.Integer 100L)
}

testSelect "SELECT 1 -- This is a comment" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect """
-- This is a comment
SELECT 1""" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect """
/* Comment inserted first */
SELECT 1""" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect """
SELECT 1
/* Comment inserted last */
""" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect """
SELECT 1;
/* Comment inserted after semicolon */
""" {
SelectExpr.Default with Columns = [Expr.Integer 1]
SelectExpr.Default with Columns = [Expr.Integer 1L]
}

testSelect """
Expand All @@ -576,7 +576,7 @@ let selectQueryTests = testList "Parse SELECT tests" [
SelectExpr.Default with
Columns = [Expr.Function ("NOW", [])]
}, Expr.Ident "time"))
Limit = Some (Expr.Integer 1)
Limit = Some (Expr.Integer 1L)
}

testSelect """
Expand Down

0 comments on commit 89d6451

Please sign in to comment.