1
Fork 0

Merge pull request from Airsequel/latest

Mixing default clauses and constraints & columns without types
This commit is contained in:
Jake Wheat 2024-10-07 09:47:43 +01:00 committed by GitHub
commit 4d907fee7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 151 additions and 100 deletions

View file

@ -98,6 +98,10 @@ data Dialect = Dialect
,diNonCommaSeparatedConstraints :: Bool
-- | allow marking tables as "without rowid"
,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)
@ -123,6 +127,8 @@ ansi2011 = Dialect {diKeywords = ansi2011ReservedKeywords
,diAutoincrement = False
,diNonCommaSeparatedConstraints = False
,diWithoutRowidTables = False
,diOptionalColumnTypes = False
,diDefaultClausesAsConstraints = False
}
-- | mysql dialect

View file

@ -1737,23 +1737,11 @@ createIndex =
<*> parens (commaSep1 (name "column name"))
columnDef :: Parser ColumnDef
columnDef = ColumnDef <$> name "column name" <*> 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))
]
columnDef = do
optionalType <- askDialect diOptionalColumnTypes
ColumnDef <$> name "column name"
<*> (if optionalType then optional typeName else Just <$> typeName)
<*> option [] (some colConstraintDef)
tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
tableConstraintDef =
@ -1802,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"]
@ -1821,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

@ -698,25 +698,9 @@ dropBehav DefaultDropBehaviour = mempty
dropBehav Cascade = pretty "cascade"
dropBehav Restrict = pretty "restrict"
columnDef :: Dialect -> ColumnDef -> Doc a
columnDef d (ColumnDef n t mdef cons) =
name n <+> 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))
columnDef d (ColumnDef n t cons) =
name n <+> maybe mempty typeName t
<+> sep (map cdef cons)
where
cdef (ColConstraintDef cnm con) =
@ -736,6 +720,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,8 +554,7 @@ data TableElement =
| TableConstraintDef (Maybe [Name]) TableConstraint
deriving (Eq,Show,Read,Data,Typeable)
data ColumnDef = ColumnDef Name TypeName
(Maybe DefaultClause)
data ColumnDef = ColumnDef Name (Maybe TypeName)
[ColConstraintDef]
-- (Maybe CollateClause)
deriving (Eq,Show,Read,Data,Typeable)
@ -577,6 +576,7 @@ data ColConstraint =
ReferentialAction
ReferentialAction
| ColCheckConstraint ScalarExpr
| ColDefaultClause DefaultClause
deriving (Eq,Show,Read,Data,Typeable)
data TableConstraint =