1
Fork 0

add default and identity to create table

This commit is contained in:
Jake Wheat 2015-08-02 18:04:40 +03:00
parent 056cd1afda
commit e495e240c0
4 changed files with 260 additions and 16 deletions
Language/SQL/SimpleSQL

View file

@ -1468,6 +1468,51 @@ TODO: change style
> <*> parens (commaSep1 columnDef)
> where
> columnDef = ColumnDef <$> name <*> typeName
> <*> optionMaybe defaultClause
> defaultClause = choice [
> keyword_ "default" >>
> DefaultClause <$> valueExpr
> ,keyword_ "generated" >>
> IdentityColumnSpec
> <$> option GeneratedDefault
> (GeneratedAlways <$ keyword_ "always"
> <|> GeneratedByDefault <$ keywords_ ["by", "default"])
> <*> (keywords_ ["as", "identity"] *>
> option [] (parens sequenceGeneratorOptions))
> ]
> sequenceGeneratorOptions =
> -- todo: could try to combine exclusive options
> -- such as cycle and nocycle
> permute ((\a b c d e f g h -> catMaybes [a,b,c,d,e,f,g,h])
> <$?> (Nothing, Just <$> startWith)
> <|?> (Nothing, Just <$> incrementBy)
> <|?> (Nothing, Just <$> maxValue)
> <|?> (Nothing, Just <$> noMaxValue)
> <|?> (Nothing, Just <$> minValue)
> <|?> (Nothing, Just <$> noMinValue)
> <|?> (Nothing, Just <$> scycle)
> <|?> (Nothing, Just <$> noCycle)
> )
> startWith = keywords_ ["start", "with"] >>
> SGOStartWith <$> signedInteger
> incrementBy = keywords_ ["increment", "by"] >>
> SGOIncrementBy <$> signedInteger
> maxValue = keyword_ "maxvalue" >>
> SGOMaxValue <$> signedInteger
> noMaxValue = SGONoMaxValue <$ try (keywords_ ["no","maxvalue"])
> minValue = keyword_ "minvalue" >>
> SGOMinValue <$> signedInteger
> noMinValue = SGONoMinValue <$ try (keywords_ ["no","minvalue"])
> scycle = SGOCycle <$ keyword_ "cycle"
> noCycle = SGONoCycle <$ try (keywords_ ["no","cycle"])
slightly hacky parser for signed integers
> signedInteger :: Parser Integer
> signedInteger = do
> s <- option 1 (1 <$ symbol "+" <|> (-1) <$ symbol "-")
> d <- unsignedInteger
> return $ s * d
> dropSchema :: Parser Statement
> dropSchema = keyword_ "schema" >>

View file

@ -453,12 +453,34 @@ which have been changed to try to improve the layout of the output.
> statement _ (CreateSchema nm) =
> text "create" <+> text "schema" <+> names nm
> statement _ (CreateTable nm cds) =
> statement d (CreateTable nm cds) =
> text "create" <+> text "table" <+> names nm
> <+> parens (commaSep $ map cd cds)
> where
> cd (ColumnDef n t) = name n <+> typeName t
> cd (ColumnDef n t mdef) =
> name n <+> typeName t
> <+> case mdef of
> Nothing -> empty
> Just (DefaultClause def) ->
> text "default" <+> valueExpr d def
> Just (IdentityColumnSpec w o) ->
> text "generated"
> <+> (case w of
> GeneratedDefault -> empty
> GeneratedAlways -> text "always"
> GeneratedByDefault -> text "by" <+> text "default")
> <+> text "as" <+> text "identity"
> <+> (case o of
> [] -> empty
> os -> parens (sep $ map sgo os))
> sgo (SGOStartWith i) = texts ["start", "with", show i]
> sgo (SGOIncrementBy i) = texts ["increment", "by", show i]
> sgo (SGOMaxValue i) = texts ["maxvalue", show i]
> sgo SGONoMaxValue = texts ["no", "maxvalue"]
> sgo (SGOMinValue i) = texts ["minvalue", show i]
> sgo SGONoMinValue = texts ["no", "minvalue"]
> sgo SGOCycle = text "cycle"
> sgo SGONoCycle = text "no cycle"
> statement _ (DropSchema nm db) =
> text "drop" <+> text "schema" <+> names nm <+> dropBehav db
@ -521,3 +543,6 @@ which have been changed to try to improve the layout of the output.
> comment :: Comment -> Doc
> comment (BlockComment str) = text "/*" <+> text str <+> text "*/"
> texts :: [String] -> Doc
> texts ts = sep $ map text ts

View file

@ -36,7 +36,10 @@
> ,IdentityRestart(..)
> ,InsertSource(..)
> ,SetClause(..)
> ,TableElement(..)
> ,TableElement(..)
> ,DefaultClause(..)
> ,IdentityWhen(..)
> ,SequenceGeneratorOption(..)
> -- * Dialect
> ,Dialect(..)
> -- * Comment
@ -489,7 +492,7 @@ I'm not sure if this is valid syntax or not.
> data TableElement =
> ColumnDef Name TypeName
> -- (Maybe DefaultClause)
> (Maybe DefaultClause)
> -- (Maybe ColumnConstraintDef)
> -- (Maybe CollateClause)
> -- | TableConstraintDef
@ -498,10 +501,28 @@ I'm not sure if this is valid syntax or not.
> {-data TableConstraintDef
> deriving (Eq,Show,Read,Data,Typeable) -}
> {-data DefaultClause =
> data DefaultClause =
> DefaultClause ValueExpr
> | IdentityColumnSpec
> | GenerationClause-}
> | IdentityColumnSpec IdentityWhen [SequenceGeneratorOption]
> -- | GenerationClause
> deriving (Eq,Show,Read,Data,Typeable)
> data IdentityWhen =
> GeneratedDefault
> | GeneratedAlways
> | GeneratedByDefault
> deriving (Eq,Show,Read,Data,Typeable)
> data SequenceGeneratorOption =
> SGOStartWith Integer
> | SGOIncrementBy Integer
> | SGOMaxValue Integer
> | SGONoMaxValue
> | SGOMinValue Integer
> | SGONoMinValue
> | SGOCycle
> | SGONoCycle
> deriving (Eq,Show,Read,Data,Typeable)
> {-data ColumnConstraintDef =
> | NotNullConstraint