-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Type-safe datatype-database mapping library. -- -- This library maps your datatypes to a relational model, in a way -- similar to what ORM libraries do in OOP. The schema can be configured -- flexibly which makes integration with existing databases easy. -- Groundhog supports schema migrations, composite keys, compositional -- queries, and much more. See the folder with examples for introduction. @package groundhog @version 0.3.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 -- | Creating some datatypes may require calling a function, using a -- special constructor, or other syntax. The string (which can have -- placeholders) is included into query without escaping. The recursive -- constructions are not allowed, i.e., [PersistValue] cannot contain -- PersistCustom values. PersistCustom :: Utf8 -> [PersistValue] -> 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 PersistField a => Projection p db r a | p -> db r a projectionExprs :: Projection p db r a => p -> [UntypedExpr db r] -> [UntypedExpr db r] projectionResult :: (Projection p db 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 -- | Datatype for incremental building SQL queries newtype Utf8 Utf8 :: Builder -> Utf8 fromUtf8 :: Utf8 -> ByteString delim :: Char -- | Represents condition for a query. data Cond db r And :: (Cond db r) -> (Cond db r) -> Cond db r Or :: (Cond db r) -> (Cond db r) -> Cond db r Not :: (Cond db r) -> Cond db r Compare :: ExprRelation -> (UntypedExpr db r) -> (UntypedExpr db r) -> Cond db r CondRaw :: (QueryRaw db r) -> Cond db r data ExprRelation Eq :: ExprRelation Ne :: ExprRelation Gt :: ExprRelation Lt :: ExprRelation Ge :: ExprRelation Le :: ExprRelation data Update db r Update :: f -> (UntypedExpr db r) -> Update db r -- | Accesses fields of the embedded datatypes. For example, SomeField -- ==. ("abc", "def") ||. SomeField ~> Tuple2_0Selector ==. "def" (~>) :: (PersistEntity v, Constructor c, FieldLike f db (RestrictionHolder v c) a, Embedded a) => f -> Selector a a' -> SubField v c a' -- | This subset of Assignable is for plain database fields. class Assignable f db r a => FieldLike f db r a | f -> r a fieldChain :: FieldLike f db r a => f -> FieldChain -- | This subset of Projection instances is for things that behave like -- fields. Namely, they can occur in condition expressions (for example, -- Field and SubField) and on the left side of update statements. For -- example "lower(field)" is a valid Projection, but not Field like -- because it cannot be on the left side. Datatypes that index PostgreSQL -- arrays "arr[5]" or access composites "(comp).subfield" are valid -- instances of Assignable. class Projection f db r a => Assignable f db r a | f -> r a newtype SubField v (c :: (* -> *) -> *) a SubField :: FieldChain -> 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 -- | It is used to map field to column names. It can be either a column -- name for a regular field of non-embedded type or a list of this field -- and the outer fields in reverse order. Eg, fieldChain $ SomeField -- ~> Tuple2_0Selector may result in [("val0", DbString), ("some", -- DbEmbedded False [dbType "", dbType True])]. type FieldChain = ((String, DbType), [(String, EmbeddedDef)]) -- | Types which are never NULL when converted to PersistValue. -- 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 -- | Used to uniformly represent fields, constants and more complex things, -- e.g., arithmetic expressions. A value should be converted to -- UntypedExpr for usage in expressions data UntypedExpr db r ExprRaw :: Expr db r a -> UntypedExpr db r ExprField :: FieldChain -> UntypedExpr db r ExprPure :: a -> UntypedExpr db r -- | Expr with phantom type helps to keep type safety in complex -- expressions newtype Expr db r a Expr :: (QueryRaw db r) -> Expr db r a -- | Defines sort order of a result-set data Order db r Asc :: f -> Order db r Desc :: f -> Order db r class HasSelectOptions a db r | a -> db r where type family HasLimit a type family HasOffset a type family HasOrder a getSelectOptions :: HasSelectOptions a db r => a -> SelectOptions db r (HasLimit a) (HasOffset a) (HasOrder a) data SelectOptions db r hasLimit hasOffset hasOrder SelectOptions :: Cond db r -> Maybe Int -> Maybe Int -> [Order db r] -> SelectOptions db r hasLimit hasOffset hasOrder condOptions :: SelectOptions db r hasLimit hasOffset hasOrder -> Cond db r limitOptions :: SelectOptions db r hasLimit hasOffset hasOrder -> Maybe Int offsetOptions :: SelectOptions db r hasLimit hasOffset hasOrder -> Maybe Int orderOptions :: SelectOptions db r hasLimit hasOffset hasOrder -> [Order db r] limitTo :: (HasSelectOptions a db r, HasLimit a ~ HFalse) => a -> Int -> SelectOptions db r HTrue (HasOffset a) (HasOrder a) offsetBy :: (HasSelectOptions a db r, HasOffset a ~ HFalse) => a -> Int -> SelectOptions db r (HasLimit a) HTrue (HasOrder a) orderBy :: (HasSelectOptions a db r, HasOrder a ~ HFalse) => a -> [Order db r] -> SelectOptions db r (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 DbOther :: OtherTypeDef -> 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. Then there are actions on delete and on -- update. DbEntity :: (Maybe (EmbeddedDef, String)) -> (Maybe ReferenceActionType) -> (Maybe ReferenceActionType) -> EntityDef -> DbType -- | Describes an ADT. data EntityDef EntityDef :: String -> Maybe String -> [DbType] -> [ConstructorDef] -> EntityDef -- | Entity name. entityName (entityDef v) == persistName v entityName :: EntityDef -> String -- | Database schema for the entity table and tables of its constructors entitySchema :: EntityDef -> Maybe 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 -- | Stores name for a database type newtype OtherTypeDef OtherTypeDef :: ((DbType -> String) -> String) -> OtherTypeDef -- | 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 UniquePrimary :: UniqueType data ReferenceActionType NoAction :: ReferenceActionType Restrict :: ReferenceActionType Cascade :: ReferenceActionType SetNull :: ReferenceActionType SetDefault :: ReferenceActionType -- | 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) insert_ :: (PersistBackend m, PersistEntity v) => v -> m () 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 (PhantomDb m) (RestrictionHolder 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 (PhantomDb m) (RestrictionHolder v c)] -> Cond (PhantomDb m) (RestrictionHolder v c) -> m () delete :: (PersistBackend m, PersistEntity v, Constructor c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m () deleteByKey :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m () count :: (PersistBackend m, PersistEntity v, Constructor c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m Int countAll :: (PersistBackend m, PersistEntity v) => v -> m Int project :: (PersistBackend m, PersistEntity v, Constructor c, Projection p (PhantomDb m) (RestrictionHolder v c) a', HasSelectOptions opts (PhantomDb m) (RestrictionHolder 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 db) => DbDescriptor db where type family AutoKeyType db type family QueryRaw db :: * -> * backendName :: DbDescriptor db => Proxy db -> String 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 -- | Connection manager provides connection to the passed function handles -- transations. Manager can be a connection itself, a pool, Snaplet in -- Snap, foundation datatype in Yesod, etc. class ConnectionManager cm conn | cm -> conn withConn :: (ConnectionManager cm conn, MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m a withConnNoTransaction :: (ConnectionManager cm conn, MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m a -- | This connection manager always returns the same connection. This -- constraint is useful when performing operations which make sense only -- within one connection, for example, nested savepoints.. class ConnectionManager cm conn => SingleConnectionManager cm conn class Savepoint conn withConnSavepoint :: (Savepoint conn, MonadBaseControl IO m, MonadIO m) => String -> m a -> conn -> m a -- | Runs action within connection. It can handle a simple connection, a -- pool of them, etc. runDbConn :: (MonadBaseControl IO m, MonadIO m, ConnectionManager cm conn) => DbPersist conn m a -> cm -> 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 Monad m => MonadReader conn (DbPersist conn m) instance Show UniqueType instance Eq UniqueType instance Eq ReferenceActionType instance Show ReferenceActionType 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 Eq (Expr db r a) instance Show (Expr db r a) instance Ord ZT instance Eq ZT instance Show Utf8 instance Eq Utf8 instance Show OtherTypeDef instance Eq OtherTypeDef 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 db r hasLimit hasOffset hasOrder) db r instance HasSelectOptions (Cond db r) db r -- | 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 -- | This class helps to shorten the type signatures of user monadic code. class (MonadIO m, MonadBaseControl IO m, MonadReader cm m, ConnectionManager cm conn) => HasConn m cm conn -- | It helps to run database operations within your application monad. runDb :: HasConn m cm conn => DbPersist conn IO a -> m a -- | It helps to run withConnSavepoint within a monad. withSavepoint :: (HasConn m cm conn, SingleConnectionManager cm conn, Savepoint conn) => String -> m a -> m a 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 applyReferencesSettings :: Maybe ReferenceActionType -> Maybe ReferenceActionType -> 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 instance (MonadIO m, MonadBaseControl IO m, MonadReader cm m, ConnectionManager cm conn) => HasConn m cm conn module Database.Groundhog.Instances instance [overlap ok] (PersistEntity v, IsUniqueKey (Key v (Unique u)), Projection (u (UniqueMarker v)) db r a') => FieldLike (u (UniqueMarker v)) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (Field v c a) db r a') => FieldLike (Field v c a) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (SubField v c a) db r a') => FieldLike (SubField v c a) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (AutoKeyField v c) db r a') => FieldLike (AutoKeyField v c) db r a' instance [overlap ok] (PersistEntity v, IsUniqueKey (Key v (Unique u)), Projection (u (UniqueMarker v)) db r a') => Assignable (u (UniqueMarker v)) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (Field v c a) db r a') => Assignable (Field v c a) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (SubField v c a) db r a') => Assignable (SubField v c a) db r a' instance [overlap ok] (PersistEntity v, Constructor c, Projection (AutoKeyField v c) db r a') => Assignable (AutoKeyField v c) db r a' instance [overlap ok] (Projection a1 db r a1', Projection a2 db r a2', Projection a3 db r a3', Projection a4 db r a4', Projection a5 db r a5') => Projection (a1, a2, a3, a4, a5) db r (a1', a2', a3', a4', a5') instance [overlap ok] (Projection a1 db r a1', Projection a2 db r a2', Projection a3 db r a3', Projection a4 db r a4') => Projection (a1, a2, a3, a4) db r (a1', a2', a3', a4') instance [overlap ok] (Projection a1 db r a1', Projection a2 db r a2', Projection a3 db r a3') => Projection (a1, a2, a3) db r (a1', a2', a3') instance [overlap ok] (Projection a1 db r a1', Projection a2 db r a2') => Projection (a1, a2) db 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)) db r (Key v (Unique u)) instance [overlap ok] (PersistEntity v, Constructor c) => Projection (c (ConstructorMarker v)) db (RestrictionHolder v c) v instance [overlap ok] (PersistEntity v, Constructor c, PersistField (Key v BackendSpecific)) => Projection (AutoKeyField v c) db (RestrictionHolder v c) (Key v BackendSpecific) instance [overlap ok] PersistField a => Projection (Expr db r a) db r a instance [overlap ok] (PersistEntity v, Constructor c, PersistField a) => Projection (SubField v c a) db (RestrictionHolder v c) a instance [overlap ok] (PersistEntity v, Constructor c, PersistField a) => Projection (Field v c a) db (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] (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 db r a toExpr :: Expression db r a => a -> UntypedExpr db r class Unifiable a b class (Expression db r a, Unifiable a a') => ExpressionOf db r a a' -- | Update field (=.) :: (FieldLike f db r a', Expression db r b, Unifiable f b) => f -> b -> Update db r -- | Boolean "and" operator. (&&.) :: Cond db r -> Cond db r -> Cond db r -- | Boolean "or" operator. (||.) :: Cond db r -> Cond db r -> Cond db r (==.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r (/=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r (<.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r (<=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r (>.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r (>=.) :: (Expression db r a, Expression db r b, Unifiable a b) => a -> b -> Cond db r 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 (Expr db r' a) 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] (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, FieldLike (u (UniqueMarker v)) db r' a', r' ~ RestrictionHolder v (UniqueConstr (Key v (Unique u))), IsUniqueKey (Key v (Unique u))) => Expression db r' (u (UniqueMarker v)) instance [overlap ok] (PersistEntity v, Constructor c, PersistField (Key v BackendSpecific), FieldLike (AutoKeyField v c) db r' a') => Expression db r' (AutoKeyField v c) instance [overlap ok] (PersistEntity v, Constructor c, PersistField a, RestrictionHolder v c ~ r') => Expression db r' (SubField v c a) instance [overlap ok] (PersistEntity v, Constructor c, PersistField a, RestrictionHolder v c ~ r') => Expression db r' (Field v c a) instance [overlap ok] (PersistField a, db' ~ db, r' ~ r) => Expression db' r' (Expr db r a) instance [overlap ok] PurePersistField a => Expression db r a instance [overlap ok] (Expression db r a, Unifiable a a') => ExpressionOf db r a a' -- | 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 :: (SqlDb db, QueryRaw db ~ Snippet db) => (Utf8 -> Utf8) -> (Utf8 -> Utf8 -> Utf8) -> (Utf8 -> Utf8 -> Utf8) -> Cond db r -> Maybe (RenderS db r) defaultShowPrim :: PersistValue -> String renderOrders :: (Utf8 -> Utf8) -> [Order db r] -> Utf8 renderUpdates :: (SqlDb db, QueryRaw db ~ Snippet db) => (Utf8 -> Utf8) -> [Update db r] -> Maybe (RenderS db r) renderFields :: (Utf8 -> Utf8) -> [(String, DbType)] -> Utf8 renderChain :: (Utf8 -> Utf8) -> FieldChain -> [Utf8] -> [Utf8] renderExpr :: (DbDescriptor db, QueryRaw db ~ Snippet db) => (Utf8 -> Utf8) -> UntypedExpr db r -> RenderS db r renderExprPriority :: (DbDescriptor db, QueryRaw db ~ Snippet db) => (Utf8 -> Utf8) -> Int -> UntypedExpr db r -> RenderS db r renderExprExtended :: (DbDescriptor db, QueryRaw db ~ Snippet db) => (Utf8 -> Utf8) -> Int -> UntypedExpr db r -> [RenderS db r] renderPersistValue :: PersistValue -> RenderS db r intercalateS :: StringLike s => s -> [s] -> s commasJoin :: StringLike s => [s] -> s data RenderS db r RenderS :: Utf8 -> ([PersistValue] -> [PersistValue]) -> RenderS db r getQuery :: RenderS db r -> Utf8 getValues :: RenderS db r -> [PersistValue] -> [PersistValue] -- | Datatype for incremental building SQL queries newtype Utf8 Utf8 :: Builder -> Utf8 fromUtf8 :: Utf8 -> ByteString 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 function :: (SqlDb db, QueryRaw db ~ Snippet db) => String -> [UntypedExpr db r] -> Snippet db r operator :: (SqlDb db, QueryRaw db ~ Snippet db, Expression db r a, Expression db r b) => Int -> String -> a -> b -> Snippet db r parens :: Int -> Int -> RenderS db r -> RenderS db r -- | Escape function, priority of the outer operator. The result is a list -- for the embedded data which may expand to several RenderS. newtype Snippet db r Snippet :: ((Utf8 -> Utf8) -> Int -> [RenderS db r]) -> Snippet db r -- | This class distinguishes databases which support SQL-specific -- expressions. It contains ad hoc members for features whose syntax -- differs across the databases. class DbDescriptor db => SqlDb db append :: (SqlDb db, ExpressionOf db r a String, ExpressionOf db r b String) => a -> b -> Expr db r String liftExpr :: (SqlDb db, QueryRaw db ~ Snippet db, ExpressionOf db r a b) => a -> Expr db r b -- | Returns escaped table name optionally qualified with schema tableName :: StringLike s => (s -> s) -> EntityDef -> ConstructorDef -> s -- | Returns escaped main table name optionally qualified with schema mainTableName :: StringLike s => (s -> s) -> EntityDef -> s instance (SqlDb db, QueryRaw db ~ Snippet db, PersistField a, Num a) => Num (Expr db r a) instance StringLike String instance StringLike (RenderS db r) instance IsString (RenderS db r) instance Monoid (RenderS db r) instance StringLike Utf8 instance IsString Utf8 instance Monoid Utf8 -- | 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 -- | contains DbType that maps to one column (no DbEmbedded) colType :: Column -> DbType colDefault :: Column -> Maybe String data UniqueDef' UniqueDef' :: Maybe String -> UniqueType -> [String] -> UniqueDef' uniqueDefName :: UniqueDef' -> Maybe String uniqueDefType :: UniqueDef' -> UniqueType uniqueDefColumns :: UniqueDef' -> [String] data Reference Reference :: Maybe String -> String -> [(String, String)] -> Maybe ReferenceActionType -> Maybe ReferenceActionType -> Reference referencedTableSchema :: Reference -> Maybe String referencedTableName :: Reference -> String -- | child column, parent column referencedColumns :: Reference -> [(String, String)] referenceOnDelete :: Reference -> Maybe ReferenceActionType referenceOnUpdate :: Reference -> Maybe ReferenceActionType data TableInfo TableInfo :: [Column] -> [UniqueDef'] -> [(Maybe String, Reference)] -> TableInfo tableColumns :: TableInfo -> [Column] tableUniques :: TableInfo -> [UniqueDef'] -- | constraint name and reference tableReferences :: TableInfo -> [(Maybe String, Reference)] data AlterColumn Type :: DbType -> AlterColumn IsNull :: AlterColumn NotNull :: AlterColumn Default :: String -> AlterColumn NoDefault :: AlterColumn UpdateValue :: String -> AlterColumn data AlterTable AddUnique :: UniqueDef' -> AlterTable DropConstraint :: String -> AlterTable DropIndex :: String -> AlterTable AddReference :: Reference -> AlterTable DropReference :: String -> AlterTable DropColumn :: String -> AlterTable AddColumn :: Column -> AlterTable AlterColumn :: Column -> [AlterColumn] -> AlterTable data AlterDB AddTable :: String -> AlterDB -- | Table schema, table name, create statement, structure of table from -- DB, structure of table from datatype, alters AlterTable :: (Maybe String) -> String -> String -> TableInfo -> TableInfo -> [AlterTable] -> AlterDB -- | Trigger schema, trigger name, table schema, table name DropTrigger :: (Maybe String) -> String -> (Maybe String) -> String -> AlterDB -- | Trigger schema, trigger name, table schema, table name, body AddTriggerOnDelete :: (Maybe String) -> String -> (Maybe String) -> String -> String -> AlterDB -- | Trigger schema, trigger name, table schema, table name, field name, -- body AddTriggerOnUpdate :: (Maybe String) -> String -> (Maybe String) -> String -> (Maybe String) -> String -> AlterDB -- | Statement which creates the function CreateOrReplaceFunction :: String -> AlterDB -- | Function schema, function name DropFunction :: (Maybe String) -> String -> AlterDB data MigrationPack m MigrationPack :: (DbType -> DbType -> Bool) -> ((Maybe String, Reference) -> (Maybe String, Reference) -> Bool) -> (UniqueDef' -> UniqueDef' -> Bool) -> (Maybe String -> String -> [(String, String)] -> m (Bool, [AlterDB])) -> (Maybe String -> String -> [(String, String)] -> m [(Bool, [AlterDB])]) -> (MigrationPack m -> EntityDef -> ConstructorDef -> m (Bool, SingleMigration)) -> (String -> String) -> DbType -> 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 migTriggerOnDelete :: MigrationPack m -> Maybe String -> String -> [(String, String)] -> m (Bool, [AlterDB]) migTriggerOnUpdate :: MigrationPack m -> Maybe String -> String -> [(String, String)] -> m [(Bool, [AlterDB])] migConstr :: MigrationPack m -> MigrationPack m -> EntityDef -> ConstructorDef -> m (Bool, SingleMigration) escape :: MigrationPack m -> String -> String primaryKeyType :: MigrationPack m -> DbType primaryKeyTypeName :: MigrationPack m -> String foreignKeyTypeName :: 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 class SchemaAnalyzer m listTables :: SchemaAnalyzer m => Maybe String -> m [String] listTableTriggers :: SchemaAnalyzer m => Maybe String -> String -> m [String] analyzeTable :: SchemaAnalyzer m => Maybe String -> String -> m (Either [String] (Maybe TableInfo)) analyzeTrigger :: SchemaAnalyzer m => Maybe String -> String -> m (Maybe String) analyzeFunction :: SchemaAnalyzer m => Maybe String -> String -> m (Maybe String) mkColumns :: 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, SchemaAnalyzer m) => MigrationPack m -> EntityDef -> m SingleMigration migrateList :: (Monad m, SchemaAnalyzer m) => MigrationPack m -> DbType -> m SingleMigration getAlters :: MigrationPack m -> TableInfo -> TableInfo -> [AlterTable] defaultMigConstr :: (Monad m, SchemaAnalyzer m) => MigrationPack m -> EntityDef -> ConstructorDef -> m (Bool, SingleMigration) showReferenceAction :: ReferenceActionType -> String readReferenceAction :: String -> Maybe ReferenceActionType instance Eq Column instance Show Column instance Show Reference instance Show AlterColumn instance Show UniqueDef' instance Show AlterTable instance Show TableInfo instance Show AlterDB -- | This module has common SQL functions and operators which are supported -- in the most SQL databases module Database.Groundhog.Generic.Sql.Functions like :: (SqlDb db, QueryRaw db ~ Snippet db, ExpressionOf db r a String) => a -> String -> Cond db r in_ :: (SqlDb db, QueryRaw db ~ Snippet db, Expression db r a, Expression db r b, PrimitivePersistField b, Unifiable a b) => a -> [b] -> Cond db r notIn_ :: (SqlDb db, QueryRaw db ~ Snippet db, Expression db r a, Expression db r b, PrimitivePersistField b, Unifiable a b) => a -> [b] -> Cond db r lower :: (SqlDb db, QueryRaw db ~ Snippet db, ExpressionOf db r a String) => a -> Expr db r String upper :: (SqlDb db, QueryRaw db ~ Snippet db, ExpressionOf db r a String) => a -> Expr db r String -- | Convert field to an arithmetic value. It is kept for compatibility -- with older Groundhog versions and can be replaced with liftExpr. toArith :: (SqlDb db, QueryRaw db ~ Snippet db, ExpressionOf db r f a', FieldLike f db r a') => f -> Expr db r a' -- | This class distinguishes databases which support SQL-specific -- expressions. It contains ad hoc members for features whose syntax -- differs across the databases. class DbDescriptor db => SqlDb db append :: (SqlDb db, ExpressionOf db r a String, ExpressionOf db r b String) => a -> b -> Expr db r String -- | This helper module contains generic versions of PersistBackend -- functions module Database.Groundhog.Generic.PersistBackendHelpers get :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Key v BackendSpecific -> m (Maybe v) select :: (db ~ PhantomDb m, r ~ RestrictionHolder v c, PersistBackend m, PersistEntity v, Constructor c, HasSelectOptions opts db r) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Utf8 -> (Cond db r -> Maybe (RenderS db r)) -> opts -> m [v] selectAll :: (PersistBackend m, PersistEntity v) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> m [(AutoKey v, v)] getBy :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Key v (Unique u) -> m (Maybe v) project :: (SqlDb db, QueryRaw db ~ Snippet db, db ~ PhantomDb m, r ~ RestrictionHolder v c, PersistBackend m, PersistEntity v, Constructor c, Projection p db r a', HasSelectOptions opts db r) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> Utf8 -> (Cond db r -> Maybe (RenderS db r)) -> p -> opts -> m [a'] count :: (db ~ PhantomDb m, r ~ RestrictionHolder v c, PersistBackend m, PersistEntity v, Constructor c) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> (Cond db r -> Maybe (RenderS db r)) -> Cond db r -> m Int replace :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> (Utf8 -> [PersistValue] -> m ()) -> (Bool -> Utf8 -> ConstructorDef -> [PersistValue] -> RenderS db r) -> Key v BackendSpecific -> v -> m () update :: (SqlDb db, QueryRaw db ~ Snippet db, db ~ PhantomDb m, r ~ RestrictionHolder v c, PersistBackend m, PersistEntity v, Constructor c) => (Utf8 -> Utf8) -> (Utf8 -> [PersistValue] -> m ()) -> (Cond db r -> Maybe (RenderS db r)) -> [Update db r] -> Cond db r -> m () delete :: (db ~ PhantomDb m, r ~ RestrictionHolder v c, PersistBackend m, PersistEntity v, Constructor c) => (Utf8 -> Utf8) -> (Utf8 -> [PersistValue] -> m ()) -> (Cond db r -> Maybe (RenderS db r)) -> Cond db r -> m () insertByAll :: (PersistBackend m, PersistEntity v) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> v -> m (Either (AutoKey v) (AutoKey v)) deleteByKey :: (PersistBackend m, PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => (Utf8 -> Utf8) -> (Utf8 -> [PersistValue] -> m ()) -> Key v BackendSpecific -> m () countAll :: (PersistBackend m, PersistEntity v) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [DbType] -> [PersistValue] -> (RowPopper m -> m a) -> m a) -> v -> m Int insertBy :: (PersistBackend m, PersistEntity v, IsUniqueKey (Key v (Unique u))) => (Utf8 -> Utf8) -> (forall a. Utf8 -> [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:" $ runDbConn $ 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