module Database.PostgreSQL.PQTypes.Model.Check (
Check(..)
, tblCheck
, sqlAddCheck
, sqlAddValidCheck
, sqlAddNotValidCheck
, sqlValidateCheck
, sqlDropCheck
) where
import Data.Monoid.Utils
import Database.PostgreSQL.PQTypes
import Prelude
data Check = Check {
chkName :: RawSQL ()
, chkCondition :: RawSQL ()
, chkValidated :: Bool
} deriving (Eq, Ord, Show)
tblCheck :: Check
tblCheck = Check
{ chkName = ""
, chkCondition = ""
, chkValidated = True
}
{-# DEPRECATED sqlAddCheck "Use sqlAddValidCheck instead" #-}
sqlAddCheck :: Check -> RawSQL ()
sqlAddCheck = sqlAddCheck_ True
sqlAddValidCheck :: Check -> RawSQL ()
sqlAddValidCheck = sqlAddCheck_ True
sqlAddNotValidCheck :: Check -> RawSQL ()
sqlAddNotValidCheck = sqlAddCheck_ False
sqlValidateCheck :: Check -> RawSQL ()
sqlValidateCheck Check{..} = "VALIDATE" <+> chkName
sqlAddCheck_ :: Bool -> Check -> RawSQL ()
sqlAddCheck_ valid Check{..} = smconcat [
"ADD CONSTRAINT"
, chkName
, "CHECK ("
, chkCondition
, ")"
, if valid then "" else " NOT VALID"
]
sqlDropCheck :: RawSQL () -> RawSQL ()
sqlDropCheck name = "DROP CONSTRAINT" <+> name