1
Fork 0

add support for create/drop role, grant, revoke

This commit is contained in:
Jake Wheat 2015-08-15 21:01:48 +03:00
parent 91875b7e7e
commit 3c0788af6e
7 changed files with 405 additions and 27 deletions
Language/SQL/SimpleSQL

View file

@ -1459,7 +1459,8 @@ TODO: change style
> ,createTable
> ,createView
> ,createDomain
> ,createSequence]
> ,createSequence
> ,createRole]
> ,keyword_ "alter" *> choice [alterTable
> ,alterDomain
> ,alterSequence]
@ -1467,7 +1468,8 @@ TODO: change style
> ,dropTable
> ,dropView
> ,dropDomain
> ,dropSequence]
> ,dropSequence
> ,dropRole]
> ,delete
> ,truncateSt
> ,insert
@ -1477,6 +1479,8 @@ TODO: change style
> ,releaseSavepoint
> ,commit
> ,rollback
> ,grant
> ,revoke
> ,SelectStatement <$> queryExpr
> ]
@ -1793,6 +1797,82 @@ slightly hacky parser for signed integers
> rollback = keyword_ "rollback" >> optional (keyword_ "work") >>
> Rollback <$> optionMaybe (keywords_ ["to", "savepoint"] *> name)
------------------------------
= Access control
TODO: fix try at the 'on'
> grant :: Parser Statement
> grant = keyword_ "grant" >> (try priv <|> role)
> where
> priv = GrantPrivilege
> <$> commaSep privilegeAction
> <*> (keyword_ "on" *> privilegeObject)
> <*> (keyword_ "to" *> commaSep name)
> <*> option WithoutGrantOption
> (WithGrantOption <$ keywords_ ["with","grant","option"])
> role = GrantRole
> <$> commaSep name
> <*> (keyword_ "to" *> commaSep name)
> <*> option WithoutAdminOption
> (WithAdminOption <$ keywords_ ["with","admin","option"])
> createRole :: Parser Statement
> createRole = keyword_ "role" >>
> CreateRole <$> name
> dropRole :: Parser Statement
> dropRole = keyword_ "role" >>
> DropRole <$> name
TODO: fix try at the 'on'
> revoke :: Parser Statement
> revoke = keyword_ "revoke" >> (try priv <|> role)
> where
> priv = RevokePrivilege
> <$> option NoGrantOptionFor
> (GrantOptionFor <$ keywords_ ["grant","option","for"])
> <*> commaSep privilegeAction
> <*> (keyword_ "on" *> privilegeObject)
> <*> (keyword_ "from" *> commaSep name)
> <*> dropBehaviour
> role = RevokeRole
> <$> option NoAdminOptionFor
> (AdminOptionFor <$ keywords_ ["admin","option", "for"])
> <*> commaSep name
> <*> (keyword_ "from" *> commaSep name)
> <*> dropBehaviour
> privilegeAction :: Parser PrivilegeAction
> privilegeAction = choice
> [PrivAll <$ keywords_ ["all","privileges"]
> ,keyword_ "select" >>
> PrivSelect <$> option [] (parens $ commaSep name)
> ,PrivDelete <$ keyword_ "delete"
> ,PrivUsage <$ keyword_ "usage"
> ,PrivTrigger <$ keyword_ "trigger"
> ,PrivExecute <$ keyword_ "execute"
> ,keyword_ "insert" >>
> PrivInsert <$> option [] (parens $ commaSep name)
> ,keyword_ "update" >>
> PrivUpdate <$> option [] (parens $ commaSep name)
> ,keyword_ "references" >>
> PrivReferences <$> option [] (parens $ commaSep name)
> ]
> privilegeObject :: Parser PrivilegeObject
> privilegeObject = choice
> [keyword_ "domain" >> PrivDomain <$> names
> ,keyword_ "type" >> PrivType <$> names
> ,keyword_ "sequence" >> PrivSequence <$> names
> ,keywords_ ["specific","function"] >> PrivFunction <$> names
> ,optional (keyword_ "table") >> PrivTable <$> names
> ]
----------------------------
wrapper to parse a series of statements. They must be separated by

View file

@ -560,7 +560,7 @@ which have been changed to try to improve the layout of the output.
> text "drop" <+> text "view" <+> names n <+> dropBehav b
== access control
== transactions
> statement _ StartTransaction =
> texts ["start", "transaction"]
@ -578,7 +578,52 @@ which have been changed to try to improve the layout of the output.
> text "rollback"
> <+> maybe empty (\n -> texts ["to","savepoint"] <+> name n) mn
== transactions
== access control
> statement _ (GrantPrivilege pas po rs go) =
> text "grant" <+> commaSep (map privAct pas)
> <+> text "on" <+> privObj po
> <+> text "to" <+> commaSep (map name rs)
> <+> grantOpt go
> where
> grantOpt WithGrantOption = texts ["with","grant","option"]
> grantOpt WithoutGrantOption = empty
> statement _ (GrantRole rs trs ao) =
> text "grant" <+> commaSep (map name rs)
> <+> text "to" <+> commaSep (map name trs)
> <+> adminOpt ao
> where
> adminOpt WithAdminOption = texts ["with","admin","option"]
> adminOpt WithoutAdminOption = empty
> statement _ (CreateRole nm) =
> texts ["create","role"] <+> name nm
> statement _ (DropRole nm) =
> texts ["drop","role"] <+> name nm
> statement _ (RevokePrivilege go pas po rs db) =
> text "revoke"
> <+> grantOptFor go
> <+> commaSep (map privAct pas)
> <+> text "on" <+> privObj po
> <+> text "from" <+> commaSep (map name rs)
> <+> dropBehav db
> where
> grantOptFor GrantOptionFor = texts ["grant","option","for"]
> grantOptFor NoGrantOptionFor = empty
> statement _ (RevokeRole ao rs trs db) =
> text "revoke"
> <+> adminOptFor ao
> <+> commaSep (map name rs)
> <+> text "from" <+> commaSep (map name trs)
> <+> dropBehav db
> where
> adminOptFor AdminOptionFor = texts ["admin","option","for"]
> adminOptFor NoAdminOptionFor = empty
== sessions
@ -718,6 +763,29 @@ which have been changed to try to improve the layout of the output.
> tableConstraint d (TableCheckConstraint v) = text "check" <+> parens (valueExpr d v)
> privAct :: PrivilegeAction -> Doc
> privAct PrivAll = texts ["all","privileges"]
> privAct (PrivSelect cs) = text "select" <+> maybeColList cs
> privAct (PrivInsert cs) = text "insert" <+> maybeColList cs
> privAct (PrivUpdate cs) = text "update" <+> maybeColList cs
> privAct (PrivReferences cs) = text "references" <+> maybeColList cs
> privAct PrivDelete = text "delete"
> privAct PrivUsage = text "usage"
> privAct PrivTrigger = text "trigger"
> privAct PrivExecute = text "execute"
> maybeColList :: [Name] -> Doc
> maybeColList cs =
> if null cs
> then empty
> else parens (commaSep $ map name cs)
> privObj :: PrivilegeObject -> Doc
> privObj (PrivTable nm) = names nm
> privObj (PrivDomain nm) = text "domain" <+> names nm
> privObj (PrivType nm) = text "type" <+> names nm
> privObj (PrivSequence nm) = text "sequence" <+> names nm
> privObj (PrivFunction nm) = texts ["specific", "function"] <+> names nm
= utils

View file

@ -49,6 +49,12 @@
> ,AlterTableAction(..)
> ,CheckOption(..)
> ,AlterDomainAction(..)
> ,AdminOption(..)
> ,GrantOption(..)
> ,PrivilegeObject(..)
> ,PrivilegeAction(..)
> ,AdminOptionFor(..)
> ,GrantOptionFor(..)
> -- * Dialect
> ,Dialect(..)
> -- * Comment
@ -458,12 +464,13 @@ I'm not sure if this is valid syntax or not.
> | FreeLocator
> | HoldLocator -}
> -- access control
> {- | GrantPrivilege
> | GrantRole
> | CreateRole
> | DropRole
> | RevokePrivilege
> | RevokeRole -}
> | GrantPrivilege [PrivilegeAction] PrivilegeObject [Name] GrantOption
> | GrantRole [Name] [Name] AdminOption
> | CreateRole Name
> | DropRole Name
> | RevokePrivilege GrantOptionFor [PrivilegeAction] PrivilegeObject
> [Name] DropBehaviour
> | RevokeRole AdminOptionFor [Name] [Name] DropBehaviour
> -- transaction management
> | StartTransaction
> -- | SetTransaction
@ -642,6 +649,38 @@ I'm not sure if this is valid syntax or not.
> deriving (Eq,Show,Read,Data,Typeable)
> data AdminOption = WithAdminOption | WithoutAdminOption
> deriving (Eq,Show,Read,Data,Typeable)
> data GrantOption = WithGrantOption | WithoutGrantOption
> deriving (Eq,Show,Read,Data,Typeable)
> data AdminOptionFor = AdminOptionFor | NoAdminOptionFor
> deriving (Eq,Show,Read,Data,Typeable)
> data GrantOptionFor = GrantOptionFor | NoGrantOptionFor
> deriving (Eq,Show,Read,Data,Typeable)
> data PrivilegeObject =
> PrivTable [Name]
> | PrivDomain [Name]
> | PrivType [Name]
> | PrivSequence [Name]
> | PrivFunction [Name]
> deriving (Eq,Show,Read,Data,Typeable)
> data PrivilegeAction =
> PrivAll
> | PrivSelect [Name]
> | PrivDelete
> | PrivInsert [Name]
> | PrivUpdate [Name]
> | PrivReferences [Name]
> | PrivUsage
> | PrivTrigger
> | PrivExecute
> deriving (Eq,Show,Read,Data,Typeable)
--------------------------
> -- | Used to set the dialect used for parsing and pretty printing,