Allow default clauses inside constraints
This commit is contained in:
parent
39c8e05224
commit
74578f9cdf
6 changed files with 127 additions and 88 deletions
Language/SQL/SimpleSQL
|
@ -98,6 +98,8 @@ data Dialect = Dialect
|
|||
,diNonCommaSeparatedConstraints :: Bool
|
||||
-- | allow marking tables as "without rowid"
|
||||
,diWithoutRowidTables :: Bool
|
||||
-- | allow mixing in DEFAULT clauses with other constraints
|
||||
,diDefaultClausesAsConstraints :: Bool
|
||||
}
|
||||
deriving (Eq,Show,Read,Data,Typeable)
|
||||
|
||||
|
@ -123,6 +125,7 @@ ansi2011 = Dialect {diKeywords = ansi2011ReservedKeywords
|
|||
,diAutoincrement = False
|
||||
,diNonCommaSeparatedConstraints = False
|
||||
,diWithoutRowidTables = False
|
||||
,diDefaultClausesAsConstraints = False
|
||||
}
|
||||
|
||||
-- | mysql dialect
|
||||
|
|
|
@ -1738,22 +1738,7 @@ createIndex =
|
|||
|
||||
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))
|
||||
]
|
||||
|
||||
tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
|
||||
tableConstraintDef =
|
||||
|
@ -1802,7 +1787,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 +1813,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
|
||||
|
||||
|
|
|
@ -699,23 +699,8 @@ dropBehav Restrict = pretty "restrict"
|
|||
|
||||
|
||||
columnDef :: Dialect -> ColumnDef -> Doc a
|
||||
columnDef d (ColumnDef n t mdef cons) =
|
||||
columnDef d (ColumnDef n t 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))
|
||||
<+> sep (map cdef cons)
|
||||
where
|
||||
cdef (ColConstraintDef cnm con) =
|
||||
|
@ -735,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) =
|
||||
|
|
|
@ -554,7 +554,6 @@ data TableElement =
|
|||
deriving (Eq,Show,Read,Data,Typeable)
|
||||
|
||||
data ColumnDef = ColumnDef Name 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 =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue