-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Type-safe, non-relational, multi-backend persistence. -- -- This library provides just the general interface and helper functions. -- You must use a specific backend in order to make this useful. @package persistent @version 0.4.0 -- | This defines the API for performing database actions. There are two -- levels to this API: dealing with fields, and dealing with entities. In -- SQL, a field corresponds to a column, and should be a single, -- non-composite value. An entity corresponds to a SQL table. In other -- words: An entity is a collection of fields. module Database.Persist.Base -- | A raw value which can be stored in any backend and can be marshalled -- to and from a PersistField. data PersistValue PersistString :: String -> PersistValue PersistByteString :: ByteString -> PersistValue PersistInt64 :: Int64 -> PersistValue PersistDouble :: Double -> PersistValue PersistBool :: Bool -> PersistValue PersistDay :: Day -> PersistValue PersistTimeOfDay :: TimeOfDay -> PersistValue PersistUTCTime :: UTCTime -> PersistValue PersistNull :: PersistValue -- | A SQL data type. Naming attempts to reflect the underlying Haskell -- datatypes, eg SqlString instead of SqlVarchar. Different SQL databases -- may have different translations for these types. data SqlType SqlString :: SqlType SqlInt32 :: SqlType -- | 8-byte integer; should be renamed SqlInt64 SqlInteger :: SqlType SqlReal :: SqlType SqlBool :: SqlType SqlDay :: SqlType SqlTime :: SqlType SqlDayTime :: SqlType SqlBlob :: SqlType -- | A value which can be marshalled to and from a PersistValue. class PersistField a toPersistValue :: PersistField a => a -> PersistValue fromPersistValue :: PersistField a => PersistValue -> Either String a sqlType :: PersistField a => a -> SqlType isNullable :: PersistField a => a -> Bool -- | A single database entity. For example, if writing a blog application, -- a blog entry would be an entry, containing fields such as title and -- content. class PersistEntity val where { data family Key val; data family Update val; data family Filter val; data family Order val; data family Unique val; } entityDef :: PersistEntity val => val -> EntityDef toPersistFields :: PersistEntity val => val -> [SomePersistField] fromPersistValues :: PersistEntity val => [PersistValue] -> Either String val halfDefined :: PersistEntity val => val toPersistKey :: PersistEntity val => Int64 -> Key val fromPersistKey :: PersistEntity val => Key val -> Int64 showPersistKey :: PersistEntity val => Key val -> String persistFilterToFieldName :: PersistEntity val => Filter val -> String persistFilterToFilter :: PersistEntity val => Filter val -> PersistFilter persistFilterToValue :: PersistEntity val => Filter val -> Either PersistValue [PersistValue] persistOrderToFieldName :: PersistEntity val => Order val -> String persistOrderToOrder :: PersistEntity val => Order val -> PersistOrder persistUpdateToFieldName :: PersistEntity val => Update val -> String persistUpdateToUpdate :: PersistEntity val => Update val -> PersistUpdate persistUpdateToValue :: PersistEntity val => Update val -> PersistValue persistUniqueToFieldNames :: PersistEntity val => Unique val -> [String] persistUniqueToValues :: PersistEntity val => Unique val -> [PersistValue] persistUniqueKeys :: PersistEntity val => val -> [Unique val] data EntityDef EntityDef :: String -> [String] -> [(String, String, [String])] -> [(String, [String])] -> [String] -> EntityDef entityName :: EntityDef -> String entityAttribs :: EntityDef -> [String] -- | name, type, attribs entityColumns :: EntityDef -> [(String, String, [String])] -- | name, columns entityUniques :: EntityDef -> [(String, [String])] entityDerives :: EntityDef -> [String] class Monad m => PersistBackend m insert :: (PersistBackend m, PersistEntity val) => val -> m (Key val) replace :: (PersistBackend m, PersistEntity val) => Key val -> val -> m () update :: (PersistBackend m, PersistEntity val) => Key val -> [Update val] -> m () updateWhere :: (PersistBackend m, PersistEntity val) => [Filter val] -> [Update val] -> m () delete :: (PersistBackend m, PersistEntity val) => Key val -> m () deleteBy :: (PersistBackend m, PersistEntity val) => Unique val -> m () deleteWhere :: (PersistBackend m, PersistEntity val) => [Filter val] -> m () get :: (PersistBackend m, PersistEntity val) => Key val -> m (Maybe val) getBy :: (PersistBackend m, PersistEntity val) => Unique val -> m (Maybe (Key val, val)) select :: (PersistBackend m, PersistEntity val) => [Filter val] -> [Order val] -> Int -> Int -> Enumerator (Key val, val) m a selectKeys :: (PersistBackend m, PersistEntity val) => [Filter val] -> Enumerator (Key val) m a count :: (PersistBackend m, PersistEntity val) => [Filter val] -> m Int data PersistFilter Eq :: PersistFilter Ne :: PersistFilter Gt :: PersistFilter Lt :: PersistFilter Ge :: PersistFilter Le :: PersistFilter In :: PersistFilter NotIn :: PersistFilter data PersistUpdate Update :: PersistUpdate Add :: PersistUpdate Subtract :: PersistUpdate Multiply :: PersistUpdate Divide :: PersistUpdate data PersistOrder Asc :: PersistOrder Desc :: PersistOrder data SomePersistField SomePersistField :: a -> SomePersistField -- | Call select but return the result as a list. selectList :: (PersistEntity val, PersistBackend m, Monad m) => [Filter val] -> [Order val] -> Int -> Int -> m [(Key val, val)] -- | Insert a value, checking for conflicts with any unique constraints. If -- a duplicate exists in the database, it is returned as Left. -- Otherwise, the new Key is returned as Right. insertBy :: (PersistEntity v, PersistBackend m) => v -> m (Either (Key v, v) (Key v)) -- | Check whether there are any conflicts for unique keys with this entity -- and existing entities in the database. -- -- Returns True if the entity would be unique, and could thus -- safely be inserted; returns False on a conflict. checkUnique :: (PersistEntity val, PersistBackend m) => val -> m Bool class PersistEntity a => DeleteCascade a deleteCascade :: (DeleteCascade a, PersistBackend m) => Key a -> m () deleteCascadeWhere :: (DeleteCascade a, PersistBackend m) => [Filter a] -> m () data PersistException PersistMarshalException :: String -> PersistException instance Typeable PersistException instance Typeable SqlType instance Typeable PersistValue instance Read PersistUpdate instance Show PersistUpdate instance Show PersistException instance Read PersistOrder instance Show PersistOrder instance Read PersistFilter instance Show PersistFilter instance Show EntityDef instance Show SqlType instance Read SqlType instance Eq SqlType instance Show PersistValue instance Read PersistValue instance Eq PersistValue instance Lift PersistUpdate instance Exception PersistException instance Lift PersistOrder instance Lift PersistFilter instance Lift EntityDef instance PersistField SomePersistField instance PersistField a => PersistField (Maybe a) instance PersistField UTCTime instance PersistField TimeOfDay instance PersistField Day instance PersistField Bool instance PersistField Double instance PersistField Word64 instance PersistField Word32 instance PersistField Word16 instance PersistField Word8 instance PersistField Int64 instance PersistField Int32 instance PersistField Int16 instance PersistField Int8 instance PersistField Int instance PersistField Html instance PersistField Text instance PersistField ByteString instance PersistField String -- | This module provides utilities for creating backends. Regular users do -- not need to use this module. module Database.Persist.TH -- | Create data types and appropriate PersistEntity instances for -- the given EntityDefs. Works well with the persist quasi-quoter. mkPersist :: [EntityDef] -> Q [Dec] share2 :: ([EntityDef] -> Q [Dec]) -> ([EntityDef] -> Q [Dec]) -> [EntityDef] -> Q [Dec] mkSave :: String -> [EntityDef] -> Q [Dec] mkDeleteCascade :: [EntityDef] -> Q [Dec] -- | Automatically creates a valid PersistField instance for any -- datatype that has valid Show and Read instances. Can be -- very convenient for Enum types. derivePersistField :: String -> Q [Dec] nullable :: [String] -> Bool module Database.Persist.Quasi -- | Converts a quasi-quoted syntax into a list of entity definitions, to -- be used as input to the template haskell generation code (mkPersist). persist :: QuasiQuoter persistFile :: FilePath -> Q Exp -- | Code that is only needed for writing GenericSql backends. module Database.Persist.GenericSql.Internal data Connection Connection :: (String -> IO Statement) -> (RawName -> [RawName] -> Either String (String, String)) -> IORef (Map String Statement) -> IO () -> (forall v. PersistEntity v => (String -> IO Statement) -> v -> IO (Either [String] [(Bool, String)])) -> ((String -> IO Statement) -> IO ()) -> ((String -> IO Statement) -> IO ()) -> ((String -> IO Statement) -> IO ()) -> (RawName -> String) -> String -> Connection prepare :: Connection -> String -> IO Statement insertSql :: Connection -> RawName -> [RawName] -> Either String (String, String) stmtMap :: Connection -> IORef (Map String Statement) close :: Connection -> IO () migrateSql :: Connection -> forall v. PersistEntity v => (String -> IO Statement) -> v -> IO (Either [String] [(Bool, String)]) begin :: Connection -> (String -> IO Statement) -> IO () commit :: Connection -> (String -> IO Statement) -> IO () rollback :: Connection -> (String -> IO Statement) -> IO () escapeName :: Connection -> RawName -> String noLimit :: Connection -> String data Statement Statement :: IO () -> IO () -> ([PersistValue] -> IO ()) -> (forall a m. MonadPeelIO m => [PersistValue] -> (RowPopper m -> m a) -> m a) -> Statement finalize :: Statement -> IO () reset :: Statement -> IO () execute :: Statement -> [PersistValue] -> IO () withStmt :: Statement -> forall a m. MonadPeelIO m => [PersistValue] -> (RowPopper m -> m a) -> m a withSqlConn :: MonadPeelIO m => IO Connection -> (Connection -> m a) -> m a withSqlPool :: MonadPeelIO m => IO Connection -> Int -> (Pool Connection -> m a) -> m a type RowPopper m = m (Maybe [PersistValue]) -- | Create the list of columns for the given entity. mkColumns :: PersistEntity val => val -> ([Column], [UniqueDef]) data Column Column :: RawName -> Bool -> SqlType -> Maybe String -> (Maybe (RawName, RawName)) -> Column cName :: Column -> RawName cNull :: Column -> Bool cType :: Column -> SqlType cDefault :: Column -> Maybe String cReference :: Column -> (Maybe (RawName, RawName)) type UniqueDef = (RawName, [RawName]) refName :: RawName -> RawName -> RawName tableColumns :: EntityDef -> [(RawName, String, [String])] rawFieldName :: (String, String, [String]) -> RawName rawTableName :: EntityDef -> RawName newtype RawName RawName :: String -> RawName unRawName :: RawName -> String instance Eq RawName instance Ord RawName module Database.Persist.GenericSql.Raw withStmt :: MonadPeelIO m => String -> [PersistValue] -> (RowPopper (SqlPersist m) -> SqlPersist m a) -> SqlPersist m a execute :: MonadIO m => String -> [PersistValue] -> SqlPersist m () newtype SqlPersist m a SqlPersist :: ReaderT Connection m a -> SqlPersist m a unSqlPersist :: SqlPersist m a -> ReaderT Connection m a getStmt' :: Connection -> String -> IO Statement getStmt :: MonadIO m => String -> SqlPersist m Statement instance Monad m => Monad (SqlPersist m) instance MonadIO m => MonadIO (SqlPersist m) instance MonadTrans SqlPersist instance Functor m => Functor (SqlPersist m) instance Applicative m => Applicative (SqlPersist m) instance MonadPeelIO m => MonadPeelIO (SqlPersist m) -- | This is a helper module for creating SQL backends. Regular users do -- not need to use this module. module Database.Persist.GenericSql newtype SqlPersist m a SqlPersist :: ReaderT Connection m a -> SqlPersist m a unSqlPersist :: SqlPersist m a -> ReaderT Connection m a data Connection type ConnectionPool = Pool Connection data Statement runSqlConn :: MonadPeelIO m => SqlPersist m a -> Connection -> m a runSqlPool :: MonadPeelIO m => SqlPersist m a -> Pool Connection -> m a type Migration m = WriterT [String] (WriterT CautiousMigration m) () parseMigration :: Monad m => Migration m -> m (Either [String] CautiousMigration) parseMigration' :: Monad m => Migration m -> m (CautiousMigration) printMigration :: MonadPeelIO m => Migration (SqlPersist m) -> SqlPersist m () getMigration :: MonadPeelIO m => Migration (SqlPersist m) -> SqlPersist m [Sql] runMigration :: MonadPeelIO m => Migration (SqlPersist m) -> SqlPersist m () -- | Same as runMigration, but returns a list of the SQL commands -- executed instead of printing them to stderr. runMigrationSilent :: MonadPeelIO m => Migration (SqlPersist m) -> SqlPersist m [String] runMigrationUnsafe :: MonadPeelIO m => Migration (SqlPersist m) -> SqlPersist m () migrate :: (MonadPeelIO m, PersistEntity val) => val -> Migration (SqlPersist m) -- | Creates a single function to perform all migrations for the entities -- defined here. One thing to be aware of is dependencies: if you have -- entities with foreign references, make sure to place those definitions -- after the entities they reference. mkMigrate :: String -> [EntityDef] -> Q [Dec] instance MonadPeelIO m => PersistBackend (SqlPersist m) module Database.Persist -- | A value which can be marshalled to and from a PersistValue. class PersistField a toPersistValue :: PersistField a => a -> PersistValue fromPersistValue :: PersistField a => PersistValue -> Either String a sqlType :: PersistField a => a -> SqlType isNullable :: PersistField a => a -> Bool -- | A single database entity. For example, if writing a blog application, -- a blog entry would be an entry, containing fields such as title and -- content. class PersistEntity val where { data family Key val; data family Update val; data family Filter val; data family Order val; data family Unique val; } entityDef :: PersistEntity val => val -> EntityDef toPersistFields :: PersistEntity val => val -> [SomePersistField] fromPersistValues :: PersistEntity val => [PersistValue] -> Either String val halfDefined :: PersistEntity val => val toPersistKey :: PersistEntity val => Int64 -> Key val fromPersistKey :: PersistEntity val => Key val -> Int64 showPersistKey :: PersistEntity val => Key val -> String persistFilterToFieldName :: PersistEntity val => Filter val -> String persistFilterToFilter :: PersistEntity val => Filter val -> PersistFilter persistFilterToValue :: PersistEntity val => Filter val -> Either PersistValue [PersistValue] persistOrderToFieldName :: PersistEntity val => Order val -> String persistOrderToOrder :: PersistEntity val => Order val -> PersistOrder persistUpdateToFieldName :: PersistEntity val => Update val -> String persistUpdateToUpdate :: PersistEntity val => Update val -> PersistUpdate persistUpdateToValue :: PersistEntity val => Update val -> PersistValue persistUniqueToFieldNames :: PersistEntity val => Unique val -> [String] persistUniqueToValues :: PersistEntity val => Unique val -> [PersistValue] persistUniqueKeys :: PersistEntity val => val -> [Unique val] class Monad m => PersistBackend m insert :: (PersistBackend m, PersistEntity val) => val -> m (Key val) replace :: (PersistBackend m, PersistEntity val) => Key val -> val -> m () update :: (PersistBackend m, PersistEntity val) => Key val -> [Update val] -> m () updateWhere :: (PersistBackend m, PersistEntity val) => [Filter val] -> [Update val] -> m () delete :: (PersistBackend m, PersistEntity val) => Key val -> m () deleteBy :: (PersistBackend m, PersistEntity val) => Unique val -> m () deleteWhere :: (PersistBackend m, PersistEntity val) => [Filter val] -> m () get :: (PersistBackend m, PersistEntity val) => Key val -> m (Maybe val) getBy :: (PersistBackend m, PersistEntity val) => Unique val -> m (Maybe (Key val, val)) select :: (PersistBackend m, PersistEntity val) => [Filter val] -> [Order val] -> Int -> Int -> Enumerator (Key val, val) m a selectKeys :: (PersistBackend m, PersistEntity val) => [Filter val] -> Enumerator (Key val) m a count :: (PersistBackend m, PersistEntity val) => [Filter val] -> m Int -- | Create data types and appropriate PersistEntity instances for -- the given EntityDefs. Works well with the persist quasi-quoter. mkPersist :: [EntityDef] -> Q [Dec] -- | Converts a quasi-quoted syntax into a list of entity definitions, to -- be used as input to the template haskell generation code (mkPersist). persist :: QuasiQuoter -- | Call select but return the result as a list. selectList :: (PersistEntity val, PersistBackend m, Monad m) => [Filter val] -> [Order val] -> Int -> Int -> m [(Key val, val)] -- | Insert a value, checking for conflicts with any unique constraints. If -- a duplicate exists in the database, it is returned as Left. -- Otherwise, the new Key is returned as Right. insertBy :: (PersistEntity v, PersistBackend m) => v -> m (Either (Key v, v) (Key v)) -- | Check whether there are any conflicts for unique keys with this entity -- and existing entities in the database. -- -- Returns True if the entity would be unique, and could thus -- safely be inserted; returns False on a conflict. checkUnique :: (PersistEntity val, PersistBackend m) => val -> m Bool