|
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +{-# LANGUAGE TemplateHaskell #-} |
| 3 | + |
| 4 | +module Database.SqlServer.Types.Certificate where |
| 5 | + |
| 6 | +import Database.SqlServer.Types.Identifiers |
| 7 | +import Database.SqlServer.Types.Entity |
| 8 | + |
| 9 | +import Text.PrettyPrint |
| 10 | +import Data.Time.Calendar |
| 11 | +import Test.QuickCheck |
| 12 | +import Control.Monad |
| 13 | + |
| 14 | +data CertificateDefinition = CertificateDefinition |
| 15 | + { |
| 16 | + certificateName :: RegularIdentifier |
| 17 | + , activeForBeginDialog :: Maybe (Maybe Bool) |
| 18 | + , expiryDate :: Maybe Day -- must be at least tomorrow |
| 19 | + , startDate :: Maybe Day -- must be at least 1st Jan 1970 |
| 20 | + , subject :: RegularIdentifier |
| 21 | + , encryptPassword :: RegularIdentifier |
| 22 | + } |
| 23 | + |
| 24 | +renderEncryptionByPassword :: RegularIdentifier -> Doc |
| 25 | +renderEncryptionByPassword s = text "ENCRYPTION BY PASSWORD = '" <> renderRegularIdentifier s <> text "'" |
| 26 | + |
| 27 | +renderSubject :: RegularIdentifier -> Doc |
| 28 | +renderSubject s = text "WITH SUBJECT = '" <> renderRegularIdentifier s <> text "'" |
| 29 | + |
| 30 | +renderExpiryDate :: Day -> Doc |
| 31 | +renderExpiryDate d = text "EXPIRY_DATE = '" <> text (filter (/= '-') (show d)) <> text "'" |
| 32 | + |
| 33 | +renderStartDate :: Day -> Doc |
| 34 | +renderStartDate d = text "START_DATE = '" <> text (filter (/= '-') (show d)) <> text "'" |
| 35 | + |
| 36 | +instance Arbitrary CertificateDefinition where |
| 37 | + arbitrary = do |
| 38 | + name <- arbitrary |
| 39 | + afbd <- arbitrary |
| 40 | + eDay <- liftM3 fromGregorian (elements [2016..3000]) (choose(1,12)) (choose(1,31)) |
| 41 | + x <- choose(- 1000,- 1) |
| 42 | + ex <- elements [Just eDay, Nothing] |
| 43 | + str <- elements [Just (addDays x eDay), Nothing] |
| 44 | + ep <- arbitrary |
| 45 | + sub <- arbitrary |
| 46 | + return $ CertificateDefinition { |
| 47 | + certificateName = name |
| 48 | + , activeForBeginDialog = afbd |
| 49 | + , startDate = str |
| 50 | + , expiryDate = ex |
| 51 | + , encryptPassword = ep |
| 52 | + , subject = sub |
| 53 | + } |
| 54 | + |
| 55 | +instance Entity CertificateDefinition where |
| 56 | + toDoc c = text "CREATE CERTIFICATE" <+> renderRegularIdentifier (certificateName c) $+$ |
| 57 | + renderEncryptionByPassword (encryptPassword c) $+$ |
| 58 | + hcat (punctuate comma $ filter (/= empty) |
| 59 | + [ renderSubject (subject c) |
| 60 | + , maybe empty renderExpiryDate (expiryDate c) |
| 61 | + , maybe empty renderStartDate (startDate c)]) |
| 62 | + |
0 commit comments