Skip to content

Commit

Permalink
Merge pull request #50 from Airsequel/autoincrement
Browse files Browse the repository at this point in the history
Support autoincrement columns
  • Loading branch information
JakeWheat authored Jan 8, 2024
2 parents 6957a39 + d53afd9 commit d8b32c0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Language/SQL/SimpleSQL/Dialect.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Data types to represent different dialect options
> ,diSqlServerSymbols :: Bool
> -- | allow sql server style for CONVERT function in format CONVERT(data_type(length), expression, style)
> ,diConvertFunction :: Bool
> -- | allow creating autoincrement columns
> ,diAutoincrement :: Bool
> }
> deriving (Eq,Show,Read,Data,Typeable)

Expand All @@ -112,6 +114,7 @@ Data types to represent different dialect options
> ,diPostgresSymbols = False
> ,diSqlServerSymbols = False
> ,diConvertFunction = False
> ,diAutoincrement = False
> }

> -- | mysql dialect
Expand Down
10 changes: 8 additions & 2 deletions Language/SQL/SimpleSQL/Parse.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,13 +1587,19 @@ TODO: change style
> colConstraintDef :: Parser ColConstraintDef
> colConstraintDef =
> ColConstraintDef
> <$> (optionMaybe (keyword_ "constraint" *> names))
> <$> optionMaybe (keyword_ "constraint" *> names)
> <*> (nullable <|> notNull <|> unique <|> primaryKey <|> check <|> references)
> where
> nullable = ColNullableConstraint <$ keyword "null"
> notNull = ColNotNullConstraint <$ keywords_ ["not", "null"]
> unique = ColUniqueConstraint <$ keyword_ "unique"
> primaryKey = ColPrimaryKeyConstraint <$ keywords_ ["primary", "key"]
> primaryKey = do
> keywords_ ["primary", "key"]
> d <- getState
> autoincrement <- if diAutoincrement d
> then optionMaybe (keyword_ "autoincrement")
> else pure Nothing
> pure $ ColPrimaryKeyConstraint $ isJust autoincrement
> check = keyword_ "check" >> ColCheckConstraint <$> parens scalarExpr
> references = keyword_ "references" >>
> (\t c m (ou,od) -> ColReferencesConstraint t c m ou od)
Expand Down
3 changes: 2 additions & 1 deletion Language/SQL/SimpleSQL/Pretty.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@ Try to do this when this code is ported to a modern pretty printing lib.
> <+> pcon con
> pcon ColNotNullConstraint = texts ["not","null"]
> pcon ColUniqueConstraint = text "unique"
> pcon ColPrimaryKeyConstraint = texts ["primary","key"]
> pcon (ColPrimaryKeyConstraint autoincrement) =
> texts $ ["primary","key"] ++ ["autoincrement"|autoincrement]
> pcon (ColCheckConstraint v) = text "check" <+> parens (scalarExpr d v)
> pcon (ColReferencesConstraint tb c m u del) =
> text "references"
Expand Down
7 changes: 5 additions & 2 deletions Language/SQL/SimpleSQL/Syntax.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
> ,IdentityWhen(..)
> ,SequenceGeneratorOption(..)
> ,ColConstraintDef(..)
> ,AutoincrementClause
> ,ColConstraint(..)
> ,TableConstraint(..)
> ,ReferenceMatch(..)
Expand Down Expand Up @@ -575,11 +576,13 @@ I'm not sure if this is valid syntax or not.
> -- (Maybe [ConstraintCharacteristics])
> deriving (Eq,Show,Read,Data,Typeable)

> type AutoincrementClause = Bool
>
> data ColConstraint =
> ColNullableConstraint
> | ColNotNullConstraint
> | ColUniqueConstraint
> | ColPrimaryKeyConstraint
> | ColPrimaryKeyConstraint AutoincrementClause
> | ColReferencesConstraint [Name] (Maybe Name)
> ReferenceMatch
> ReferentialAction
Expand Down Expand Up @@ -730,6 +733,6 @@ I'm not sure if this is valid syntax or not.

> -- | Comment. Useful when generating SQL code programmatically. The
> -- parser doesn't produce these.
> data Comment = BlockComment String
> newtype Comment = BlockComment String
> deriving (Eq,Show,Read,Data,Typeable)

8 changes: 7 additions & 1 deletion tools/Language/SQL/SimpleSQL/SQL2011Schema.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,13 @@ todo: constraint characteristics
> "create table t (a int primary key);"
> $ CreateTable [Name Nothing "t"]
> [TableColumnDef $ ColumnDef (Name Nothing "a") (TypeName [Name Nothing "int"]) Nothing
> [ColConstraintDef Nothing ColPrimaryKeyConstraint]])
> [ColConstraintDef Nothing (ColPrimaryKeyConstraint False)]])

> ,(TestStatement ansi2011 { diAutoincrement = True }
> "create table t (a int primary key autoincrement);"
> $ CreateTable [Name Nothing "t"]
> [TableColumnDef $ ColumnDef (Name Nothing "a") (TypeName [Name Nothing "int"]) Nothing
> [ColConstraintDef Nothing (ColPrimaryKeyConstraint True)]])

references t(a,b)
[ Full |partial| simepl]
Expand Down

0 comments on commit d8b32c0

Please sign in to comment.