{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Database.Relational.Constraint (
Key, indexes, unsafeDefineConstraintKey,
tableConstraint, projectionKey,
uniqueKey,
HasConstraintKey (..),
derivedUniqueKey,
Primary, Unique, NotNull
) where
import Database.Record (PersistableRecordWidth, PersistableWidth (persistableWidth))
import Database.Record.KeyConstraint
(KeyConstraint, unsafeSpecifyKeyConstraint,
Primary, Unique, NotNull)
import qualified Database.Record.KeyConstraint as C
import Database.Relational.Pi (Pi)
import qualified Database.Relational.Pi.Unsafe as UnsafePi
data Key c r ct = Key [Int] (PersistableRecordWidth ct)
indexes :: Key c r ct -> [Int]
indexes :: forall c r ct. Key c r ct -> [Int]
indexes (Key [Int]
is PersistableRecordWidth ct
_) = [Int]
is
width :: Key c r ct -> PersistableRecordWidth ct
width :: forall c r ct. Key c r ct -> PersistableRecordWidth ct
width (Key [Int]
_ PersistableRecordWidth ct
w) = PersistableRecordWidth ct
w
unsafeDefineConstraintKey :: PersistableWidth ct
=> [Int]
-> Key c r ct
unsafeDefineConstraintKey :: forall ct c r. PersistableWidth ct => [Int] -> Key c r ct
unsafeDefineConstraintKey [Int]
ixs = forall c r ct. [Int] -> PersistableRecordWidth ct -> Key c r ct
Key [Int]
ixs forall a. PersistableWidth a => PersistableRecordWidth a
persistableWidth
tableConstraint :: Key c r ct -> KeyConstraint c r
tableConstraint :: forall c r ct. Key c r ct -> KeyConstraint c r
tableConstraint = forall c r. [Int] -> KeyConstraint c r
unsafeSpecifyKeyConstraint forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c r ct. Key c r ct -> [Int]
indexes
projectionKey :: Key c r ct -> Pi r ct
projectionKey :: forall c r ct. Key c r ct -> Pi r ct
projectionKey Key c r ct
k = forall r1 r0. PersistableRecordWidth r1 -> [Int] -> Pi r0 r1
UnsafePi.defineDirectPi' PersistableRecordWidth ct
w [Int]
ixs where
ixs :: [Int]
ixs = forall c r ct. Key c r ct -> [Int]
indexes Key c r ct
k
w :: PersistableRecordWidth ct
w = forall c r ct. Key c r ct -> PersistableRecordWidth ct
width Key c r ct
k
unsafeReturnKey :: PersistableWidth ct
=> KeyConstraint c r -> Key c r ct
unsafeReturnKey :: forall ct c r.
PersistableWidth ct =>
KeyConstraint c r -> Key c r ct
unsafeReturnKey = forall ct c r. PersistableWidth ct => [Int] -> Key c r ct
unsafeDefineConstraintKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c r. KeyConstraint c r -> [Int]
C.indexes
mapConstraint :: PersistableWidth ct
=> (KeyConstraint c0 r -> KeyConstraint c1 r)
-> Key c0 r ct
-> Key c1 r ct
mapConstraint :: forall ct c0 r c1.
PersistableWidth ct =>
(KeyConstraint c0 r -> KeyConstraint c1 r)
-> Key c0 r ct -> Key c1 r ct
mapConstraint KeyConstraint c0 r -> KeyConstraint c1 r
f = forall ct c r.
PersistableWidth ct =>
KeyConstraint c r -> Key c r ct
unsafeReturnKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyConstraint c0 r -> KeyConstraint c1 r
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c r ct. Key c r ct -> KeyConstraint c r
tableConstraint
uniqueKey :: PersistableWidth ct
=> Key Primary r ct -> Key Unique r ct
uniqueKey :: forall ct r.
PersistableWidth ct =>
Key Primary r ct -> Key Unique r ct
uniqueKey = forall ct c0 r c1.
PersistableWidth ct =>
(KeyConstraint c0 r -> KeyConstraint c1 r)
-> Key c0 r ct -> Key c1 r ct
mapConstraint forall r. PrimaryConstraint r -> UniqueConstraint r
C.unique
class PersistableWidth ct => HasConstraintKey c r ct where
constraintKey :: Key c r ct
derivedUniqueKey :: HasConstraintKey Primary r ct => Key Unique r ct
derivedUniqueKey :: forall r ct. HasConstraintKey Primary r ct => Key Unique r ct
derivedUniqueKey = forall ct r.
PersistableWidth ct =>
Key Primary r ct -> Key Unique r ct
uniqueKey forall c r ct. HasConstraintKey c r ct => Key c r ct
constraintKey