-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Type-safe ADT-database mapping library. -- -- This library provides just the general interface and helper functions. -- You must use a specific backend in order to make this useful. @package groundhog @version 0.2.0 -- | This module defines the functions and datatypes used throughout the -- framework. Most of them are for the internal use module Database.Groundhog.Core -- | Only instances of this class can be persisted in a database class (PersistField v, PurePersistField (AutoKey v)) => PersistEntity v where data family Field v :: ((* -> *) -> *) -> * -> * data family Key v :: * -> * type family AutoKey v type family DefaultKey v entityDef :: PersistEntity v => v -> EntityDef toEntityPersistValues :: (PersistEntity v, PersistBackend m) => v -> m ([PersistValue] -> [PersistValue]) fromEntityPersistValues :: (PersistEntity v, PersistBackend m) => [PersistValue] -> m (v, [PersistValue]) getUniques :: (PersistEntity v, DbDescriptor db) => Proxy db -> v -> (Int, [(String, [PersistValue] -> [PersistValue])]) entityFieldChain :: PersistEntity v => Field v c a -> FieldChain -- | 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 PersistZonedTime :: ZT -> PersistValue PersistNull :: PersistValue -- | Represents everything which can be put into a database. This data can -- be stored in multiple columns and tables. To get value of those -- columns we might need to access another table. That is why the result -- type is monadic. class PersistField a persistName :: PersistField a => a -> String toPersistValues :: (PersistField a, PersistBackend m) => a -> m ([PersistValue] -> [PersistValue]) fromPersistValues :: (PersistField a, PersistBackend m) => [PersistValue] -> m (a, [PersistValue]) dbType :: PersistField a => a -> DbType -- | Represents all datatypes that map into a single column. Getting value -- for that column might require monadic actions to access other tables. class PersistField a => SinglePersistField a toSinglePersistValue :: (SinglePersistField a, PersistBackend m) => a -> m PersistValue fromSinglePersistValue :: (SinglePersistField a, PersistBackend m) => PersistValue -> m a -- | Represents all datatypes that map into several columns. Getting values -- for those columns is pure. class PersistField a => PurePersistField a toPurePersistValues :: (PurePersistField a, DbDescriptor db) => Proxy db -> a -> ([PersistValue] -> [PersistValue]) fromPurePersistValues :: (PurePersistField a, DbDescriptor db) => Proxy db -> [PersistValue] -> (a, [PersistValue]) -- | Datatypes which can be converted directly to PersistValue. The -- no-value parameter DbDescriptor db => Proxy db allows -- conversion depend the database details while keeping it pure. class (SinglePersistField a, PurePersistField a) => PrimitivePersistField a toPrimitivePersistValue :: (PrimitivePersistField a, DbDescriptor db) => Proxy db -> a -> PersistValue fromPrimitivePersistValue :: (PrimitivePersistField a, DbDescriptor db) => Proxy db -> PersistValue -> a class PersistField v => Embedded v where data family Selector v :: * -> * selectorNum :: Embedded v => Selector v a -> Int -- | Any data that can be fetched from a database class Projection p r a | p -> r a projectionFieldChains :: Projection p r a => p -> [FieldChain] -> [FieldChain] projectionResult :: (Projection p r a, PersistBackend m) => p -> [PersistValue] -> m (a, [PersistValue]) data RestrictionHolder v (c :: (* -> *) -> *) -- | A holder for Unique constraints data Unique (u :: (* -> *) -> *) -- | A holder for DB type in backend-specific keys data KeyForBackend db v KeyForBackend :: (AutoKeyType db) -> KeyForBackend db v -- | Key marked with this type can have value for any backend data BackendSpecific -- | A phantom datatype to make instance head diffirent c -- (ConstructorMarker, v) data ConstructorMarker v a -- | A phantom datatype to make instance head diffirent u -- (UniqueMarker, v) data UniqueMarker v a data Proxy a data HFalse data HTrue -- | Avoid orphan instances. newtype ZT ZT :: ZonedTime -> ZT delim :: Char -- | Represents condition for a query. data Cond v (c :: (* -> *) -> *) And :: (Cond v c) -> (Cond v c) -> Cond v Or :: (Cond v c) -> (Cond v c) -> Cond v Not :: (Cond v c) -> Cond v Compare :: ExprRelation -> (Expr v c a) -> (Expr v c b) -> Cond v data ExprRelation Eq :: ExprRelation Ne :: ExprRelation Gt :: ExprRelation Lt :: ExprRelation Ge :: ExprRelation Le :: ExprRelation data Update v c Update :: f -> (Expr v c b) -> Update v c -- | Accesses fields of the embedded datatypes. For example, SomeField -- ==. ("abc", "def") ||. SomeField ~> Tuple2_0Selector ==. "def" (~>) :: (PersistEntity v, Constructor c, FieldLike f (RestrictionHolder v c) a, Embedded a) => f -> Selector a a' -> SubField v c a' -- | Convert field to an arithmetic value toArith :: (PersistEntity v, FieldLike f (RestrictionHolder v c) a') => f -> Arith v c a' -- | Generalises data that can occur in expressions (so far there are Field -- and SubField). class Projection f r a => FieldLike f r a | f -> r a fieldChain :: FieldLike f r a => f -> FieldChain newtype SubField v (c :: (* -> *) -> *) a SubField :: ((String, DbType), [(String, EmbeddedDef)]) -> SubField v a -- | It can be used in expressions like a regular field. Note that the -- constructor should be specified for the condition. For example, -- delete (AutoKeyField asTypeOf (undefined :: f v -- SomeConstructor) ==. k) or delete (AutoKeyField ==. k ||. -- SomeField ==. "DUPLICATE") data AutoKeyField v c AutoKeyField :: AutoKeyField v c type FieldChain = ((String, DbType), [(String, EmbeddedDef)]) -- | Types which when converted to PersistValue are never NULL. -- Consider the type Maybe (Maybe a). Now Nothing is stored as -- NULL, so we cannot distinguish between Just Nothing and Nothing which -- is a problem. The purpose of this class is to ban the inner Maybe's. -- Maybe this class can be removed when support for inner Maybe's -- appears. class NeverNull a -- | Constraint for use in arithmetic expressions. Num is not used -- to explicitly include only types supported by the library. TODO: -- consider replacement with Num class Numeric a -- | Arithmetic expressions which can include fields and literals data Arith v c a Plus :: (Arith v c a) -> (Arith v c a) -> Arith v c a Minus :: (Arith v c a) -> (Arith v c a) -> Arith v c a Mult :: (Arith v c a) -> (Arith v c a) -> Arith v c a Abs :: (Arith v c a) -> Arith v c a ArithField :: f -> Arith v c a Lit :: Int64 -> Arith v c a -- | Used to uniformly represent fields, constants and arithmetic -- expressions. A value should be converted to Expr for usage in -- expressions data Expr v c a ExprField :: f -> Expr v c f ExprArith :: Arith v c a -> Expr v c (Arith v c a) ExprPure :: a -> Expr v c a -- | Defines sort order of a result-set data Order v c Asc :: f -> Order v c Desc :: f -> Order v c class HasSelectOptions a v c | a -> v c where type family HasLimit a type family HasOffset a type family HasOrder a getSelectOptions :: HasSelectOptions a v c => a -> SelectOptions v c (HasLimit a) (HasOffset a) (HasOrder a) data SelectOptions v c hasLimit hasOffset hasOrder SelectOptions :: Cond v c -> Maybe Int -> Maybe Int -> [Order v c] -> SelectOptions v c hasLimit hasOffset hasOrder condOptions :: SelectOptions v c hasLimit hasOffset hasOrder -> Cond v c limitOptions :: SelectOptions v c hasLimit hasOffset hasOrder -> Maybe Int offsetOptions :: SelectOptions v c hasLimit hasOffset hasOrder -> Maybe Int orderOptions :: SelectOptions v c hasLimit hasOffset hasOrder -> [Order v c] limitTo :: (HasSelectOptions a v c, HasLimit a ~ HFalse) => a -> Int -> SelectOptions v c HTrue (HasOffset a) (HasOrder a) offsetBy :: (HasSelectOptions a v c, HasOffset a ~ HFalse) => a -> Int -> SelectOptions v c (HasLimit a) HTrue (HasOrder a) orderBy :: (HasSelectOptions a v c, HasOrder a ~ HFalse) => a -> [Order v c] -> SelectOptions v c (HasLimit a) (HasOffset a) HTrue -- | A DB data type. Naming attempts to reflect the underlying Haskell -- datatypes, eg DbString instead of DbVarchar. Different databases may -- have different translations for these types. data DbType DbString :: DbType DbInt32 :: DbType DbInt64 :: DbType DbReal :: DbType DbBool :: DbType DbDay :: DbType DbTime :: DbType DbDayTime :: DbType DbDayTimeZoned :: DbType -- | ByteString DbBlob :: DbType -- | Name for a database type More complex types DbOther :: String -> DbType DbMaybe :: DbType -> DbType -- | List table name and type of its argument DbList :: String -> DbType -> DbType DbEmbedded :: EmbeddedDef -> DbType -- | Nothing means autokey, Just contains a unique key definition and a -- name of unique constraint. DbEntity :: (Maybe (EmbeddedDef, String)) -> EntityDef -> DbType -- | Describes an ADT. data EntityDef EntityDef :: String -> [DbType] -> [ConstructorDef] -> EntityDef -- | Entity name. entityName (entityDef v) == persistName v entityName :: EntityDef -> String -- | Named types of the instantiated polymorphic type parameters typeParams :: EntityDef -> [DbType] -- | List of entity constructors definitions constructors :: EntityDef -> [ConstructorDef] -- | The first argument is a flag which defines if the field names should -- be concatenated with the outer field name (False) or used as is which -- provides full control over table column names (True). Value False -- should be the default value so that a datatype can be embedded without -- name conflict concern. The second argument list of field names and -- field types. data EmbeddedDef EmbeddedDef :: Bool -> [(String, DbType)] -> EmbeddedDef -- | Describes an entity constructor data ConstructorDef ConstructorDef :: Int -> String -> Maybe String -> [(String, DbType)] -> [UniqueDef] -> ConstructorDef -- | Number of the constructor in the ADT constrNum :: ConstructorDef -> Int -- | Constructor name constrName :: ConstructorDef -> String -- | Autokey name if any constrAutoKeyName :: ConstructorDef -> Maybe String -- | Parameter names with their named type constrParams :: ConstructorDef -> [(String, DbType)] -- | Uniqueness constraints on the constructor fiels constrUniques :: ConstructorDef -> [UniqueDef] -- | Phantom constructors are made instances of this class. This class -- should be used only by Template Haskell codegen class Constructor c phantomConstrName :: Constructor c => c (a :: * -> *) -> String phantomConstrNum :: Constructor c => c (a :: * -> *) -> Int class (Constructor (UniqueConstr uKey), PurePersistField uKey) => IsUniqueKey uKey where type family UniqueConstr uKey :: (* -> *) -> * extractUnique :: (IsUniqueKey uKey, uKey ~ Key v u) => v -> uKey uniqueNum :: IsUniqueKey uKey => uKey -> Int -- | Unique name and list of the field names that form a unique combination data UniqueDef UniqueDef :: String -> UniqueType -> [(String, DbType)] -> UniqueDef uniqueName :: UniqueDef -> String uniqueType :: UniqueDef -> UniqueType uniqueFields :: UniqueDef -> [(String, DbType)] -- | Defines how to treat the unique set of fields for a datatype data UniqueType UniqueConstraint :: UniqueType UniqueIndex :: UniqueType -- | Either error messages or migration queries with safety flag and -- execution order type SingleMigration = Either [String] [(Bool, Int, String)] -- | Datatype names and corresponding migrations type NamedMigrations = Map String SingleMigration type Migration m = StateT NamedMigrations m () class (Monad m, DbDescriptor (PhantomDb m)) => PersistBackend m where type family PhantomDb m insert :: (PersistBackend m, PersistEntity v) => v -> m (AutoKey v) insertBy :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v)) insertByAll :: (PersistBackend m, PersistEntity v) => v -> m (Either (AutoKey v) (AutoKey v)) replace :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> v -> m () select :: (PersistBackend m, PersistEntity v, Constructor c, HasSelectOptions opts v c) => opts -> m [v] selectAll :: (PersistBackend m, PersistEntity v) => m [(AutoKey v, v)] get :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m (Maybe v) getBy :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => Key v (Unique u) -> m (Maybe v) update :: (PersistBackend m, PersistEntity v, Constructor c) => [Update v c] -> Cond v c -> m () delete :: (PersistBackend m, PersistEntity v, Constructor c) => Cond v c -> m () deleteByKey :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m () count :: (PersistBackend m, PersistEntity v, Constructor c) => Cond v c -> m Int countAll :: (PersistBackend m, PersistEntity v) => v -> m Int project :: (PersistBackend m, PersistEntity v, Constructor c, Projection p (RestrictionHolder v c) a', HasSelectOptions opts v c) => p -> opts -> m [a'] migrate :: (PersistBackend m, PersistEntity v) => v -> Migration m executeRaw :: PersistBackend m => Bool -> String -> [PersistValue] -> m () queryRaw :: PersistBackend m => Bool -> String -> [PersistValue] -> (RowPopper m -> m a) -> m a insertList :: (PersistBackend m, PersistField a) => [a] -> m Int64 getList :: (PersistBackend m, PersistField a) => Int64 -> m [a] class PrimitivePersistField (AutoKeyType a) => DbDescriptor a where type family AutoKeyType a type RowPopper m = m (Maybe [PersistValue]) newtype Monad m => DbPersist conn m a DbPersist :: ReaderT conn m a -> DbPersist conn m a unDbPersist :: DbPersist conn m a -> ReaderT conn m a runDbPersist :: Monad m => DbPersist conn m a -> conn -> m a instance Show ExprRelation instance Monad m => Monad (DbPersist conn m) instance MonadIO m => MonadIO (DbPersist conn m) instance Functor m => Functor (DbPersist conn m) instance Applicative m => Applicative (DbPersist conn m) instance MonadTrans (DbPersist conn) instance Show UniqueType instance Eq UniqueType instance Eq EmbeddedDef instance Show EmbeddedDef instance Eq DbType instance Show DbType instance Show EntityDef instance Eq EntityDef instance Show ConstructorDef instance Eq ConstructorDef instance Show UniqueDef instance Eq UniqueDef instance Show ZT instance Read ZT instance Eq PersistValue instance Show PersistValue instance (PersistEntity v, Constructor c, Numeric a) => Num (Arith v c a) instance (PersistEntity v, Constructor c) => Show (Arith v c a) instance (PersistEntity v, Constructor c) => Eq (Arith v c a) instance Ord ZT instance Eq ZT instance MonadBaseControl IO m => MonadBaseControl IO (DbPersist conn m) instance MonadTransControl (DbPersist conn) instance MonadBase IO m => MonadBase IO (DbPersist conn m) instance HasSelectOptions (SelectOptions v c hasLimit hasOffset hasOrder) v c instance HasSelectOptions (Cond v c) v c -- | This helper module is intended for use by the backend creators module Database.Groundhog.Generic -- | Produce the migrations but not execute them. Fails when an unsafe -- migration occurs. createMigration :: PersistBackend m => Migration m -> m NamedMigrations -- | Execute the migrations and log them. executeMigration :: (PersistBackend m, MonadIO m) => (String -> IO ()) -> NamedMigrations -> m () -- | Execute migrations and log them. Executes the unsafe migrations -- without warnings executeMigrationUnsafe :: (PersistBackend m, MonadIO m) => (String -> IO ()) -> NamedMigrations -> m () -- | Run migrations and log them. Fails when an unsafe migration occurs. runMigration :: (PersistBackend m, MonadIO m) => (String -> IO ()) -> Migration m -> m () -- | Run migrations and log them. Executes the unsafe migrations without -- warnings runMigrationUnsafe :: (PersistBackend m, MonadIO m) => (String -> IO ()) -> Migration m -> m () -- | Pretty print the migrations printMigration :: MonadIO m => NamedMigrations -> m () -- | Joins the migrations. The result is either all error messages or all -- queries mergeMigrations :: [SingleMigration] -> SingleMigration -- | No-op silentMigrationLogger :: String -> IO () -- | Prints the queries to stdout defaultMigrationLogger :: String -> IO () failMessage :: PersistField a => a -> [PersistValue] -> String primToPersistValue :: (PersistBackend m, PrimitivePersistField a) => a -> m ([PersistValue] -> [PersistValue]) primFromPersistValue :: (PersistBackend m, PrimitivePersistField a) => [PersistValue] -> m (a, [PersistValue]) pureToPersistValue :: (PersistBackend m, PurePersistField a) => a -> m ([PersistValue] -> [PersistValue]) pureFromPersistValue :: (PersistBackend m, PurePersistField a) => [PersistValue] -> m (a, [PersistValue]) singleToPersistValue :: (PersistBackend m, SinglePersistField a) => a -> m ([PersistValue] -> [PersistValue]) singleFromPersistValue :: (PersistBackend m, SinglePersistField a) => [PersistValue] -> m (a, [PersistValue]) toSinglePersistValueUnique :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u)), PrimitivePersistField (Key v (Unique u))) => u (UniqueMarker v) -> v -> m PersistValue fromSinglePersistValueUnique :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u)), PrimitivePersistField (Key v (Unique u))) => u (UniqueMarker v) -> PersistValue -> m v toPersistValuesUnique :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m ([PersistValue] -> [PersistValue]) fromPersistValuesUnique :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> [PersistValue] -> m (v, [PersistValue]) toSinglePersistValueAutoKey :: (PersistBackend m, PersistEntity v, PrimitivePersistField (AutoKey v)) => v -> m PersistValue fromSinglePersistValueAutoKey :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => PersistValue -> m v bracket :: MonadBaseControl IO m => m a -> (a -> m b) -> (a -> m c) -> m c finally :: MonadBaseControl IO m => m a -> m b -> m a onException :: MonadBaseControl IO m => m a -> m b -> m a data PSEmbeddedFieldDef PSEmbeddedFieldDef :: String -> Maybe String -> Maybe String -> Maybe [PSEmbeddedFieldDef] -> PSEmbeddedFieldDef psEmbeddedFieldName :: PSEmbeddedFieldDef -> String psDbEmbeddedFieldName :: PSEmbeddedFieldDef -> Maybe String psDbEmbeddedTypeName :: PSEmbeddedFieldDef -> Maybe String psSubEmbedded :: PSEmbeddedFieldDef -> Maybe [PSEmbeddedFieldDef] applyEmbeddedDbTypeSettings :: [PSEmbeddedFieldDef] -> DbType -> DbType findOne :: (Eq c, Show c) => String -> (a -> c) -> (b -> c) -> a -> [b] -> b replaceOne :: (Eq c, Show c) => String -> (a -> c) -> (b -> c) -> (a -> b -> b) -> a -> [b] -> [b] -- | Returns only old elements, only new elements, and matched pairs (old, -- new). The new ones exist only in datatype, the old are present only in -- DB, match is typically by name (the properties of the matched elements -- may differ). matchElements :: Show a => (a -> b -> Bool) -> [a] -> [b] -> ([a], [b], [(a, b)]) haveSameElems :: Show a => (a -> b -> Bool) -> [a] -> [b] -> Bool mapAllRows :: Monad m => ([PersistValue] -> m a) -> RowPopper m -> m [a] phantomDb :: PersistBackend m => m (Proxy (PhantomDb m)) isSimple :: [ConstructorDef] -> Bool instance Show PSEmbeddedFieldDef module Database.Groundhog.Instances instance [overlap ok] (PersistEntity v, IsUniqueKey (Key v (Unique u)), Projection (u (UniqueMarker v)) r a') => FieldLike (u (UniqueMarker v)) r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (Field v c a) r a') => FieldLike (Field v c a) r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (SubField v c a) r a') => FieldLike (SubField v c a) r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (AutoKeyField v c) r a') => FieldLike (AutoKeyField v c) r a' instance [overlap ok] (Projection a1 r a1', Projection a2 r a2', Projection a3 r a3', Projection a4 r a4', Projection a5 r a5') => Projection (a1, a2, a3, a4, a5) r (a1', a2', a3', a4', a5') instance [overlap ok] (Projection a1 r a1', Projection a2 r a2', Projection a3 r a3', Projection a4 r a4') => Projection (a1, a2, a3, a4) r (a1', a2', a3', a4') instance [overlap ok] (Projection a1 r a1', Projection a2 r a2', Projection a3 r a3') => Projection (a1, a2, a3) r (a1', a2', a3') instance [overlap ok] (Projection a1 r a1', Projection a2 r a2') => Projection (a1, a2) r (a1', a2') instance [overlap ok] (PersistEntity v, IsUniqueKey (Key v (Unique u)), r ~ RestrictionHolder v (UniqueConstr (Key v (Unique u)))) => Projection (u (UniqueMarker v)) r (Key v (Unique u)) instance [overlap ok] (PersistEntity v, Constructor c) => Projection (c (ConstructorMarker v)) (RestrictionHolder v c) v instance [overlap ok] (PersistEntity v, Constructor c, PersistField (Key v BackendSpecific)) => Projection (AutoKeyField v c) (RestrictionHolder v c) (Key v BackendSpecific) instance [overlap ok] (PersistEntity v, Constructor c, PersistField a) => Projection (SubField v c a) (RestrictionHolder v c) a instance [overlap ok] (PersistEntity v, Constructor c, PersistField a) => Projection (Field v c a) (RestrictionHolder v c) a instance [overlap ok] (DbDescriptor db, PersistEntity v) => PersistField (KeyForBackend db v) instance [overlap ok] (PersistField a, PersistField b, PersistField c, PersistField d, PersistField e) => PersistField (a, b, c, d, e) instance [overlap ok] (PersistField a, PersistField b, PersistField c, PersistField d) => PersistField (a, b, c, d) instance [overlap ok] (PersistField a, PersistField b, PersistField c) => PersistField (a, b, c) instance [overlap ok] (PersistField a, PersistField b) => PersistField (a, b) instance [overlap ok] PersistField () instance [overlap ok] PersistField a => PersistField [a] instance [overlap ok] (PersistField a, NeverNull a) => PersistField (Maybe a) instance [overlap ok] PersistField ZonedTime instance [overlap ok] PersistField UTCTime instance [overlap ok] PersistField TimeOfDay instance [overlap ok] PersistField Day instance [overlap ok] PersistField Bool instance [overlap ok] PersistField Double instance [overlap ok] PersistField Word64 instance [overlap ok] PersistField Word32 instance [overlap ok] PersistField Word16 instance [overlap ok] PersistField Word8 instance [overlap ok] PersistField Int64 instance [overlap ok] PersistField Int32 instance [overlap ok] PersistField Int16 instance [overlap ok] PersistField Int8 instance [overlap ok] PersistField Int instance [overlap ok] PersistField Text instance [overlap ok] PersistField String instance [overlap ok] PersistField ByteString instance [overlap ok] NeverNull (KeyForBackend db v) instance [overlap ok] NeverNull (Key v u) instance [overlap ok] NeverNull UTCTime instance [overlap ok] NeverNull TimeOfDay instance [overlap ok] NeverNull Day instance [overlap ok] NeverNull Bool instance [overlap ok] NeverNull Double instance [overlap ok] NeverNull Int64 instance [overlap ok] NeverNull Int instance [overlap ok] NeverNull ByteString instance [overlap ok] NeverNull Text instance [overlap ok] NeverNull String instance [overlap ok] (DbDescriptor db, PersistEntity v) => PrimitivePersistField (KeyForBackend db v) instance [overlap ok] (PrimitivePersistField a, NeverNull a) => PrimitivePersistField (Maybe a) instance [overlap ok] PrimitivePersistField ZonedTime instance [overlap ok] PrimitivePersistField UTCTime instance [overlap ok] PrimitivePersistField TimeOfDay instance [overlap ok] PrimitivePersistField Day instance [overlap ok] PrimitivePersistField Bool instance [overlap ok] PrimitivePersistField Double instance [overlap ok] PrimitivePersistField Word64 instance [overlap ok] PrimitivePersistField Word32 instance [overlap ok] PrimitivePersistField Word16 instance [overlap ok] PrimitivePersistField Word8 instance [overlap ok] PrimitivePersistField Int64 instance [overlap ok] PrimitivePersistField Int32 instance [overlap ok] PrimitivePersistField Int16 instance [overlap ok] PrimitivePersistField Int8 instance [overlap ok] PrimitivePersistField Int instance [overlap ok] PrimitivePersistField ByteString instance [overlap ok] PrimitivePersistField Text instance [overlap ok] PrimitivePersistField String instance [overlap ok] Numeric Double instance [overlap ok] Numeric Word64 instance [overlap ok] Numeric Word32 instance [overlap ok] Numeric Word16 instance [overlap ok] Numeric Word8 instance [overlap ok] Numeric Int64 instance [overlap ok] Numeric Int32 instance [overlap ok] Numeric Int16 instance [overlap ok] Numeric Int8 instance [overlap ok] Numeric Int instance [overlap ok] (PurePersistField a, PurePersistField b, PurePersistField c, PurePersistField d, PurePersistField e) => PurePersistField (a, b, c, d, e) instance [overlap ok] (PurePersistField a, PurePersistField b, PurePersistField c, PurePersistField d) => PurePersistField (a, b, c, d) instance [overlap ok] (PurePersistField a, PurePersistField b, PurePersistField c) => PurePersistField (a, b, c) instance [overlap ok] (PurePersistField a, PurePersistField b) => PurePersistField (a, b) instance [overlap ok] PurePersistField () instance [overlap ok] (PrimitivePersistField a, NeverNull a) => PurePersistField (Maybe a) instance [overlap ok] PrimitivePersistField a => PurePersistField a instance [overlap ok] (SinglePersistField a, NeverNull a) => SinglePersistField (Maybe a) instance [overlap ok] PrimitivePersistField a => SinglePersistField a instance [overlap ok] (PersistField a', PersistField b', PersistField c', PersistField d', PersistField e') => Embedded (a', b', c', d', e') instance [overlap ok] (PersistField a', PersistField b', PersistField c', PersistField d') => Embedded (a', b', c', d') instance [overlap ok] (PersistField a', PersistField b', PersistField c') => Embedded (a', b', c') instance [overlap ok] (PersistField a', PersistField b') => Embedded (a', b') -- | This module provides mechanism for flexible and typesafe usage of -- plain data values and fields. The expressions can used in conditions -- and right part of Update statement. Example: -- --
--   StringField ==. "abc" &&. NumberField >. (0 :: Int) ||. MaybeField ==. (Nothing :: Maybe String) ||. MaybeField ==. Just "def"
--   
-- -- Note that polymorphic values like numbers or Nothing must have a type -- annotation module Database.Groundhog.Expression -- | Instances of this type can be converted to Expr. It is useful -- for uniform manipulation over fields and plain values class Expression a v c toExpression :: Expression a v c => a -> Expr v c a -- | Update field (=.) :: (Expression b v c, FieldLike f (RestrictionHolder v c) a', Unifiable f b) => f -> b -> Update v c -- | Boolean "and" operator. (&&.) :: Cond v c -> Cond v c -> Cond v c -- | Boolean "or" operator. (||.) :: Cond v c -> Cond v c -> Cond v c (==.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c (/=.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c (<.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c (<=.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c (>.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c (>=.) :: (Expression a v c, Expression b v c, Unifiable a b) => a -> b -> Cond v c instance [overlap ok] TypeEq x x HTrue instance [overlap ok] b ~ HFalse => TypeEq x y b instance [overlap ok] r ~ (HTrue, a) => NormalizeValue' a r instance [overlap ok] (TypeEq (DefaultKey a) (Key a u) isDef, r ~ (Not isDef, NormalizeKey isDef (Key a u))) => NormalizeValue' (Key a u) r instance [overlap ok] (TypeEq (DefaultKey a) (Key a u) isDef, r ~ (Not isDef, Maybe (NormalizeKey isDef (Key a u)))) => NormalizeValue' (Maybe (Key a u)) r instance [overlap ok] r ~ (isPlain, t) => NormalizeValue HTrue isPlain t r instance [overlap ok] NormalizeValue' t (isPlain, r) => NormalizeValue HFalse HTrue t (isPlain, r) instance [overlap ok] NormalizeValue' t (isPlain, r) => NormalizeValue HFalse HFalse t (HFalse, r) instance [overlap ok] r ~ (HTrue, a) => ExtractValue a r instance [overlap ok] r ~ (HFalse, Key v (Unique u)) => ExtractValue (u (UniqueMarker v)) r instance [overlap ok] r ~ (HFalse, Key v BackendSpecific) => ExtractValue (AutoKeyField v c) r instance [overlap ok] r ~ (HFalse, a) => ExtractValue (SubField v c a) r instance [overlap ok] r ~ (HFalse, a) => ExtractValue (Field v c a) r instance [overlap ok] r ~ (HFalse, a) => ExtractValue (Arith v c a) r instance [overlap ok] (ExtractValue t (isPlain, r), NormalizeValue counterpart isPlain r r') => Normalize counterpart t r' instance [overlap ok] (Normalize bk a (ak, r), Normalize ak b (bk, r)) => Unifiable a b instance [overlap ok] Unifiable a a instance [overlap ok] (PersistEntity v, Constructor c, FieldLike (u (UniqueMarker v)) (RestrictionHolder v c) a', c' ~ UniqueConstr (Key v' (Unique u)), v ~ v', IsUniqueKey (Key v' (Unique u)), c ~ c') => Expression (u (UniqueMarker v)) v' c' instance [overlap ok] (PersistEntity v, Constructor c, PersistField (Key v' BackendSpecific), FieldLike (AutoKeyField v c) (RestrictionHolder v c) a', v ~ v', c ~ c') => Expression (AutoKeyField v c) v' c' instance [overlap ok] (PersistEntity v, Constructor c, PersistField a, v ~ v', c ~ c') => Expression (SubField v c a) v' c' instance [overlap ok] (PersistEntity v, Constructor c, PersistField a, v ~ v', c ~ c') => Expression (Field v c a) v' c' instance [overlap ok] (PersistEntity v, Constructor c, PersistField a, v ~ v', c ~ c') => Expression (Arith v c a) v' c' instance [overlap ok] PurePersistField a => Expression a v c -- | This helper module is intended for use by the backend creators module Database.Groundhog.Generic.Migration data Column Column :: String -> Bool -> DbType -> Maybe String -> Column colName :: Column -> String colNull :: Column -> Bool colType :: Column -> DbType colDefault :: Column -> Maybe String data UniqueDef' UniqueDef' :: String -> UniqueType -> [String] -> UniqueDef' -- | Foreign table name and names of the corresponding columns type Reference = (String, [(String, String)]) data TableInfo TableInfo :: Maybe String -> [Column] -> [UniqueDef'] -> [(Maybe String, Reference)] -> TableInfo tablePrimaryKeyName :: TableInfo -> Maybe String tableColumns :: TableInfo -> [Column] tableUniques :: TableInfo -> [UniqueDef'] -- | constraint name and reference tableReferences :: TableInfo -> [(Maybe String, Reference)] data AlterColumn Type :: DbType -> AlterColumn IsNull :: AlterColumn NotNull :: AlterColumn Add :: Column -> AlterColumn Drop :: AlterColumn AddPrimaryKey :: AlterColumn Default :: String -> AlterColumn NoDefault :: AlterColumn UpdateValue :: String -> AlterColumn type AlterColumn' = (String, AlterColumn) data AlterTable AddUnique :: UniqueDef' -> AlterTable DropConstraint :: String -> AlterTable DropIndex :: String -> AlterTable AddReference :: Reference -> AlterTable DropReference :: String -> AlterTable AlterColumn :: AlterColumn' -> AlterTable data AlterDB AddTable :: String -> AlterDB -- | Table name, create statement, structure of table from DB, structure of -- table from datatype, alters AlterTable :: String -> String -> TableInfo -> TableInfo -> [AlterTable] -> AlterDB -- | Trigger name, table name DropTrigger :: String -> String -> AlterDB -- | Trigger name, table name, body AddTriggerOnDelete :: String -> String -> String -> AlterDB -- | Trigger name, table name, field name, body AddTriggerOnUpdate :: String -> String -> String -> String -> AlterDB CreateOrReplaceFunction :: String -> AlterDB DropFunction :: String -> AlterDB data MigrationPack m MigrationPack :: (DbType -> DbType -> Bool) -> ((Maybe String, Reference) -> (Maybe String, Reference) -> Bool) -> (UniqueDef' -> UniqueDef' -> Bool) -> (String -> m (Maybe (Either [String] TableInfo))) -> (String -> [(String, String)] -> m (Bool, [AlterDB])) -> (String -> String -> String -> m (Bool, [AlterDB])) -> (MigrationPack m -> Bool -> String -> ConstructorDef -> m (Bool, SingleMigration)) -> (String -> String) -> String -> String -> String -> Int -> ([UniqueDef'] -> [Reference] -> ([String], [AlterTable])) -> (Column -> String) -> (AlterDB -> SingleMigration) -> MigrationPack m compareTypes :: MigrationPack m -> DbType -> DbType -> Bool compareRefs :: MigrationPack m -> (Maybe String, Reference) -> (Maybe String, Reference) -> Bool compareUniqs :: MigrationPack m -> UniqueDef' -> UniqueDef' -> Bool checkTable :: MigrationPack m -> String -> m (Maybe (Either [String] TableInfo)) migTriggerOnDelete :: MigrationPack m -> String -> [(String, String)] -> m (Bool, [AlterDB]) migTriggerOnUpdate :: MigrationPack m -> String -> String -> String -> m (Bool, [AlterDB]) migConstr :: MigrationPack m -> MigrationPack m -> Bool -> String -> ConstructorDef -> m (Bool, SingleMigration) escape :: MigrationPack m -> String -> String primaryKeyType :: MigrationPack m -> String foreignKeyType :: MigrationPack m -> String mainTableId :: MigrationPack m -> String defaultPriority :: MigrationPack m -> Int -- | Sql pieces for the create table statement that add constraints and -- alterations for running after the table is created addUniquesReferences :: MigrationPack m -> [UniqueDef'] -> [Reference] -> ([String], [AlterTable]) showColumn :: MigrationPack m -> Column -> String showAlterDb :: MigrationPack m -> AlterDB -> SingleMigration mkColumns :: (DbType -> DbType) -> String -> DbType -> ([Column], [Reference]) -- | Create migration for a given entity and all entities it depends on. -- The stateful Map is used to avoid duplicate migrations when an entity -- type occurs several times in a datatype migrateRecursively :: (Monad m, PersistEntity v) => (EntityDef -> m SingleMigration) -> (DbType -> m SingleMigration) -> v -> StateT NamedMigrations m () migrateEntity :: Monad m => MigrationPack m -> EntityDef -> m SingleMigration migrateList :: Monad m => MigrationPack m -> DbType -> m SingleMigration getAlters :: MigrationPack m -> TableInfo -> TableInfo -> [AlterTable] defaultMigConstr :: Monad m => MigrationPack m -> Bool -> String -> ConstructorDef -> m (Bool, SingleMigration) instance Eq Column instance Show Column instance Show AlterColumn instance Show UniqueDef' instance Show AlterTable instance Show TableInfo instance Show AlterDB -- | This module defines the functions which are used only for backends -- creation. module Database.Groundhog.Generic.Sql -- | Renders conditions for SQL backend. Returns Nothing if the fields -- don't have any columns. renderCond :: (PersistEntity v, Constructor c, StringLike s, DbDescriptor db) => Proxy db -> (s -> s) -> (s -> s -> s) -> (s -> s -> s) -> Cond v c -> Maybe (RenderS s) defaultShowPrim :: PersistValue -> String renderArith :: (PersistEntity v, Constructor c, StringLike s, DbDescriptor db) => Proxy db -> (s -> s) -> Arith v c a -> RenderS s renderOrders :: (PersistEntity v, Constructor c, StringLike s) => (s -> s) -> [Order v c] -> s renderUpdates :: (PersistEntity v, Constructor c, StringLike s, DbDescriptor db) => Proxy db -> (s -> s) -> [Update v c] -> Maybe (RenderS s) renderFields :: StringLike s => (s -> s) -> [(String, DbType)] -> s renderChain :: StringLike s => (s -> s) -> FieldChain -> [s] -> [s] intercalateS :: StringLike s => s -> [s] -> s data RenderS s RenderS :: s -> ([PersistValue] -> [PersistValue]) -> RenderS s getQuery :: RenderS s -> s getValues :: RenderS s -> [PersistValue] -> [PersistValue] class (Monoid a, IsString a) => StringLike a fromChar :: StringLike a => Char -> a fromString :: IsString a => String -> a -- | An infix synonym for mappend. (<>) :: Monoid m => m -> m -> m parens :: StringLike s => Int -> Int -> RenderS s -> RenderS s instance StringLike String instance Monoid s => Monoid (RenderS s) -- | This helper module contains generic versions of PersistBackend -- functions module Database.Groundhog.Generic.PersistBackendHelpers get :: (PersistBackend m, StringLike s, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Key v BackendSpecific -> m (Maybe v) select :: (PersistBackend m, StringLike s, PersistEntity v, Constructor c, HasSelectOptions opts v c) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> s -> (Cond v c -> Maybe (RenderS s)) -> opts -> m [v] selectAll :: (PersistBackend m, StringLike s, PersistEntity v) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> m [(AutoKey v, v)] getBy :: (PersistBackend m, StringLike s, PersistEntity v, IsUniqueKey (Key v (Unique u))) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Key v (Unique u) -> m (Maybe v) project :: (PersistBackend m, StringLike s, PersistEntity v, Constructor c, Projection p (RestrictionHolder v c) a', HasSelectOptions opts v c) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> s -> (Cond v c -> Maybe (RenderS s)) -> p -> opts -> m [a'] count :: (PersistBackend m, StringLike s, PersistEntity v, Constructor c) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> (Cond v c -> Maybe (RenderS s)) -> Cond v c -> m Int replace :: (PersistBackend m, StringLike s, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> (s -> [PersistValue] -> m ()) -> (Bool -> String -> ConstructorDef -> s) -> Key v BackendSpecific -> v -> m () update :: (PersistBackend m, StringLike s, PersistEntity v, Constructor c) => (s -> s) -> (s -> [PersistValue] -> m ()) -> (Cond v c -> Maybe (RenderS s)) -> [Update v c] -> Cond v c -> m () delete :: (PersistBackend m, StringLike s, PersistEntity v, Constructor c) => (s -> s) -> (s -> [PersistValue] -> m ()) -> (Cond v c -> Maybe (RenderS s)) -> Cond v c -> m () insertByAll :: (PersistBackend m, StringLike s, PersistEntity v) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> v -> m (Either (AutoKey v) (AutoKey v)) deleteByKey :: (PersistBackend m, StringLike s, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (s -> s) -> (s -> [PersistValue] -> m ()) -> Key v BackendSpecific -> m () countAll :: (PersistBackend m, StringLike s, PersistEntity v) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> v -> m Int insertBy :: (PersistBackend m, StringLike s, PersistEntity v, IsUniqueKey (Key v (Unique u))) => (s -> s) -> (forall a. s -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v)) -- | This module exports the most commonly used functions and datatypes. -- -- The example below shows the most of the main features. See more -- examples in the examples directory. -- --
--   {-# LANGUAGE GADTs, TypeFamilies, TemplateHaskell, QuasiQuotes, FlexibleInstances #-}
--   import Control.Monad.IO.Class (liftIO)
--   import Database.Groundhog.TH
--   import Database.Groundhog.Sqlite
--   
--   data Customer a = Customer {customerName :: String, remark :: a} deriving Show
--   data Product = Product {productName :: String, quantity :: Int, customer :: Customer String} deriving Show
--   
--   mkPersist (defaultCodegenConfig {migrationFunction = Just "migrateAll"}) [groundhog|
--    - entity: Customer
--      constructors:
--        - name: Customer
--          uniques:
--            - name: NameConstraint
--              fields: [customerName]
--    - entity: Product
--    |]
--   
--   main = withSqliteConn ":memory:" $ runSqliteConn $ do
--     -- Customer is also migrated because Product references it.
--     -- It is possible to migrate schema for given type, e.g. migrate (undefined :: Customer String), or run migrateAll
--     runMigration defaultMigrationLogger migrateAll
--     let john = Customer "John Doe" "Phone: 01234567"
--     johnKey <- insert john
--     -- John is inserted only once because of the name constraint
--     insert $ Product "Apples" 5 john
--     insert $ Product "Melon" 2 john
--     -- Groundhog prevents SQL injections. Quotes and other special symbols are safe.
--     insert $ Product "Melon" 6 (Customer "Jack Smith" "Don't let him pay by check")
--     -- Bonus melon for all large melon orders. The values used in expressions should have known type, so literal 5 is annotated.
--     update [QuantityField =. toArith QuantityField + 1] (ProductNameField ==. "Melon" &&. QuantityField >. (5 :: Int))
--     productsForJohn <- select $ CustomerField ==. johnKey
--     liftIO $ putStrLn $ "Products for John: " ++ show productsForJohn
--     -- Check bonus
--     melon <- select $ (ProductNameField ==. "Melon") `orderBy` [Desc QuantityField]
--     liftIO $ putStrLn $ "Melon orders: " ++ show melon
--   
module Database.Groundhog