1
Fork 0

Merge branch 'default-clause-inside-constraints' into latest

This commit is contained in:
prescientmoon 2024-09-02 19:50:11 +02:00
commit 4b0db42396
Signed by: prescientmoon
SSH key fingerprint: SHA256:WFp/cO76nbarETAoQcQXuV+0h7XJsEsOCI0UsyPIy6U
6 changed files with 129 additions and 91 deletions

View file

@ -100,6 +100,8 @@ data Dialect = Dialect
,diWithoutRowidTables :: Bool
-- | allow omitting types for columns
,diOptionalColumnTypes :: Bool
-- | allow mixing in DEFAULT clauses with other constraints
,diDefaultClausesAsConstraints :: Bool
}
deriving (Eq,Show,Read,Data,Typeable)
@ -126,6 +128,7 @@ ansi2011 = Dialect {diKeywords = ansi2011ReservedKeywords
,diNonCommaSeparatedConstraints = False
,diWithoutRowidTables = False
,diOptionalColumnTypes = False
,diDefaultClausesAsConstraints = False
}
-- | mysql dialect

View file

@ -1741,22 +1741,7 @@ columnDef = do
optionalType <- askDialect diOptionalColumnTypes
ColumnDef <$> name "column name"
<*> (if optionalType then optional typeName else Just <$> typeName)
<*> optional defaultClause
<*> option [] (some colConstraintDef)
where
defaultClause = label "column default clause" $ choice [
keyword_ "default" >>
DefaultClause <$> scalarExpr
-- todo: left factor
,try (keywords_ ["generated","always","as"] >>
GenerationClause <$> parens scalarExpr)
,keyword_ "generated" >>
IdentityColumnSpec
<$> (GeneratedAlways <$ keyword_ "always"
<|> GeneratedByDefault <$ keywords_ ["by", "default"])
<*> (keywords_ ["as", "identity"] *>
option [] (parens sequenceGeneratorOptions))
]
tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
tableConstraintDef =
@ -1805,7 +1790,14 @@ colConstraintDef :: Parser ColConstraintDef
colConstraintDef =
ColConstraintDef
<$> optional (keyword_ "constraint" *> names "constraint name")
<*> (nullable <|> notNull <|> unique <|> primaryKey <|> check <|> references)
<*> (nullable
<|> notNull
<|> unique
<|> primaryKey
<|> check
<|> references
<|> defaultClause
)
where
nullable = ColNullableConstraint <$ keyword "null"
notNull = ColNotNullConstraint <$ keywords_ ["not", "null"]
@ -1824,6 +1816,20 @@ colConstraintDef =
<*> optional (parens $ name "column name")
<*> refMatch
<*> refActions
defaultClause = label "column default clause" $
ColDefaultClause <$> choice
[keyword_ "default"
>> DefaultClause <$> scalarExpr
-- todo: left factor
,try (keywords_ ["generated","always","as"] >>
GenerationClause <$> parens scalarExpr)
,keyword_ "generated" >>
IdentityColumnSpec
<$> (GeneratedAlways <$ keyword_ "always"
<|> GeneratedByDefault <$ keywords_ ["by", "default"])
<*> (keywords_ ["as", "identity"] *>
option [] (parens sequenceGeneratorOptions))
]
-- slightly hacky parser for signed integers

View file

@ -697,25 +697,9 @@ dropBehav DefaultDropBehaviour = mempty
dropBehav Cascade = pretty "cascade"
dropBehav Restrict = pretty "restrict"
columnDef :: Dialect -> ColumnDef -> Doc a
columnDef d (ColumnDef n t mdef cons) =
columnDef d (ColumnDef n t cons) =
name n <+> maybe mempty typeName t
<+> case mdef of
Nothing -> mempty
Just (DefaultClause def) ->
pretty "default" <+> scalarExpr d def
Just (GenerationClause e) ->
texts ["generated","always","as"] <+> parens (scalarExpr d e)
Just (IdentityColumnSpec w o) ->
pretty "generated"
<+> (case w of
GeneratedAlways -> pretty "always"
GeneratedByDefault -> pretty "by" <+> pretty "default")
<+> pretty "as" <+> pretty "identity"
<+> (case o of
[] -> mempty
os -> parens (sep $ map sequenceGeneratorOption os))
<+> sep (map cdef cons)
where
cdef (ColConstraintDef cnm con) =
@ -735,6 +719,20 @@ columnDef d (ColumnDef n t mdef cons) =
<+> refMatch m
<+> refAct "update" u
<+> refAct "delete" del
pcon (ColDefaultClause clause) = case clause of
DefaultClause def ->
pretty "default" <+> scalarExpr d def
GenerationClause e ->
texts ["generated","always","as"] <+> parens (scalarExpr d e)
IdentityColumnSpec w o ->
pretty "generated"
<+> (case w of
GeneratedAlways -> pretty "always"
GeneratedByDefault -> pretty "by" <+> pretty "default")
<+> pretty "as" <+> pretty "identity"
<+> (case o of
[] -> mempty
os -> parens (sep $ map sequenceGeneratorOption os))
sequenceGeneratorOption :: SequenceGeneratorOption -> Doc a
sequenceGeneratorOption (SGODataType t) =

View file

@ -554,7 +554,6 @@ data TableElement =
deriving (Eq,Show,Read,Data,Typeable)
data ColumnDef = ColumnDef Name (Maybe TypeName)
(Maybe DefaultClause)
[ColConstraintDef]
-- (Maybe CollateClause)
deriving (Eq,Show,Read,Data,Typeable)
@ -576,6 +575,7 @@ data ColConstraint =
ReferentialAction
ReferentialAction
| ColCheckConstraint ScalarExpr
| ColDefaultClause DefaultClause
deriving (Eq,Show,Read,Data,Typeable)
data TableConstraint =