Add derivedWidth function which preserve record type.

This commit is contained in:
Kei Hibino 2013-06-08 11:14:28 +09:00
parent e7bf643252
commit e8add5f809
2 changed files with 14 additions and 5 deletions

View File

@ -39,7 +39,7 @@ import Database.Record.KeyConstraint
import Database.Record.Persistable
(PersistableSqlType, PersistableType(..), sqlNullValue,
PersistableSqlValue, PersistableValue(..), fromSql, toSql,
PersistableRecordWidth, PersistableWidth(..),
PersistableRecordWidth, PersistableWidth(..), derivedWidth,
PersistableRecord, Persistable(..),
derivedPersistableValueRecord)
import Database.Record.FromSql

View File

@ -17,8 +17,8 @@ module Database.Record.Persistable (
PersistableSqlType, runPersistableNullValue, unsafePersistableSqlTypeFromNull,
-- * Specify record width
PersistableRecordWidth(runPersistableRecordWidth),
unsafeValueWidth, (<&>), maybeWidth,
PersistableRecordWidth, runPersistableRecordWidth,
unsafePersistableRecordWidth, unsafeValueWidth, (<&>), maybeWidth,
-- * Bidirectional conversion between single column type and SQL type
PersistableSqlValue, persistableSqlValue,
@ -34,7 +34,7 @@ module Database.Record.Persistable (
PersistableType(..), sqlNullValue,
PersistableValue (..), fromSql, toSql,
derivedPersistableValueRecord,
PersistableWidth (..), unsafePersistableRecordWidth,
PersistableWidth (..), derivedWidth,
Persistable (..)
) where
@ -75,7 +75,11 @@ persistableSqlValue = const PersistableSqlValue
-- | Proof object to specify width of Haskell type 'a'
-- when converting to SQL type list.
newtype PersistableRecordWidth a =
PersistableRecordWidth { runPersistableRecordWidth :: Int }
PersistableRecordWidth Int
-- | Get width 'Int' value of record type 'a'.
runPersistableRecordWidth :: PersistableRecordWidth a -> Int
runPersistableRecordWidth (PersistableRecordWidth w) = w
-- | Unsafely generate 'PersistableRecordWidth' proof object from specified width of Haskell type 'a'.
unsafePersistableRecordWidth :: Int -- ^ Specify width of Haskell type 'a'
@ -173,6 +177,11 @@ instance PersistableWidth a => PersistableWidth (Maybe a) where
instance PersistableWidth () where
persistableWidth = voidWidth
-- | Pass type parameter and inferred width value.
derivedWidth :: PersistableWidth a => (PersistableRecordWidth a, Int)
derivedWidth = (pw, runPersistableRecordWidth pw) where
pw = persistableWidth
-- | Interface of inference rule for 'PersistableSqlValue' proof object
class PersistableType q => PersistableValue q a where