Skip to content

Commit

Permalink
added interval type to parser and compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Montmorency committed Feb 10, 2023
1 parent 3b22b87 commit 12b45ce
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Compiler.hs
Expand Up @@ -184,6 +184,7 @@ compilePostgresType PPolygon = "POLYGON"
compilePostgresType PDate = "DATE"
compilePostgresType PBinary = "BYTEA"
compilePostgresType PTime = "TIME"
compilePostgresType PInterval = "INTERVAL"
compilePostgresType (PNumeric (Just precision) (Just scale)) = "NUMERIC(" <> show precision <> "," <> show scale <> ")"
compilePostgresType (PNumeric (Just precision) Nothing) = "NUMERIC(" <> show precision <> ")"
compilePostgresType (PNumeric Nothing _) = "NUMERIC"
Expand Down
2 changes: 1 addition & 1 deletion IHP/IDE/SchemaDesigner/Controller/Columns.hs
Expand Up @@ -218,4 +218,4 @@ validateColumn = validateNameInSchema "column name" [] Nothing
referencingColumnForeignKeyConstraints tableName columnName =
find \case
AddConstraint { tableName = constraintTable, constraint = ForeignKeyConstraint { columnName = fkColumnName } } -> constraintTable == tableName && fkColumnName == columnName
otherwise -> False
otherwise -> False
14 changes: 14 additions & 0 deletions IHP/IDE/SchemaDesigner/Parser.hs
Expand Up @@ -246,6 +246,7 @@ sqlType :: Parser PostgresType
sqlType = choice $ map optionalArray
[ uuid
, text
, interval --Needs higher precedence eotherwise parsed as Int
, bigint
, smallint
, int -- order int after smallint/bigint because symbol INT is prefix og INT2, INT8
Expand Down Expand Up @@ -347,6 +348,12 @@ sqlType = choice $ map optionalArray
symbol' "ZONE"
pure PTime

interval = do
try (symbol' "INTERVAL")
optional do
choice $ map symbol' intervalFields
pure PInterval

numericPS = do
try (symbol' "NUMERIC(")
values <- between (space) (char ')' >> space) (varExpr `sepBy` (char ',' >> space))
Expand Down Expand Up @@ -430,6 +437,13 @@ sqlType = choice $ map optionalArray
theType <- try (takeWhile1P (Just "Custom type") (\c -> isAlphaNum c || c == '_'))
pure (PCustomType theType)


intervalFields :: [Text]
intervalFields = [ "YEAR TO MONTH", "DAY TO HOUR", "DAY TO MINUTE", "DAY TO SECOND"
, "HOUR TO MINUTE", "HOUR TO SECOND", "MINUTE TO SECOND"
, "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND"]


term = parens expression <|> try callExpr <|> try doubleExpr <|> try intExpr <|> selectExpr <|> varExpr <|> (textExpr <* optional space)
where
parens f = between (char '(' >> space) (char ')' >> space) f
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Types.hs
Expand Up @@ -211,6 +211,7 @@ data PostgresType
| PDate
| PBinary
| PTime
| PInterval
| PNumeric { precision :: Maybe Int, scale :: Maybe Int }
| PVaryingN (Maybe Int)
| PCharacterN Int
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/View/Columns/Edit.hs
Expand Up @@ -123,6 +123,7 @@ typeSelector postgresType enumNames = [hsx|
{option isSelected "POINT" "Point"}
{option isSelected "BYTEA" "Binary"}
{option isSelected "Time" "Time"}
{option isSelected "INTERVAL" "Interval"}
{option isSelected "BIGSERIAL" "Bigserial"}
{option isSelected "SMALLINT" "Int (16bit)"}
{option isSelected "BIGINT" "Int (64bit)"}
Expand Down

0 comments on commit 12b45ce

Please sign in to comment.