Safe Haskell | None |
---|
This module defines the functions and datatypes used throughout the framework. Most of them are for the internal use
- class (PersistField v, PurePersistField (AutoKey v)) => PersistEntity v where
- data Field v :: ((* -> *) -> *) -> * -> *
- data Key v :: * -> *
- type AutoKey v
- type DefaultKey v
- type IsSumType v
- entityDef :: v -> EntityDef
- toEntityPersistValues :: PersistBackend m => v -> m ([PersistValue] -> [PersistValue])
- fromEntityPersistValues :: PersistBackend m => [PersistValue] -> m (v, [PersistValue])
- getUniques :: DbDescriptor db => proxy db -> v -> (Int, [(String, [PersistValue] -> [PersistValue])])
- entityFieldChain :: Field v c a -> FieldChain
- data PersistValue
- class PersistField a where
- persistName :: a -> String
- toPersistValues :: PersistBackend m => a -> m ([PersistValue] -> [PersistValue])
- fromPersistValues :: PersistBackend m => [PersistValue] -> m (a, [PersistValue])
- dbType :: a -> DbType
- class PersistField a => SinglePersistField a where
- toSinglePersistValue :: PersistBackend m => a -> m PersistValue
- fromSinglePersistValue :: PersistBackend m => PersistValue -> m a
- class PersistField a => PurePersistField a where
- toPurePersistValues :: DbDescriptor db => proxy db -> a -> [PersistValue] -> [PersistValue]
- fromPurePersistValues :: DbDescriptor db => proxy db -> [PersistValue] -> (a, [PersistValue])
- class PersistField a => PrimitivePersistField a where
- toPrimitivePersistValue :: DbDescriptor db => proxy db -> a -> PersistValue
- fromPrimitivePersistValue :: DbDescriptor db => proxy db -> PersistValue -> a
- class PersistField v => Embedded v where
- data Selector v :: * -> *
- selectorNum :: Selector v a -> Int
- class PersistField a => Projection p a | p -> a where
- type ProjectionDb p db :: Constraint
- type ProjectionRestriction p r :: Constraint
- projectionExprs :: (ProjectionDb p db, ProjectionRestriction p r) => p -> [UntypedExpr db r] -> [UntypedExpr db r]
- projectionResult :: PersistBackend m => p -> [PersistValue] -> m (a, [PersistValue])
- class (Projection p a, ProjectionDb p db, ProjectionRestriction p r) => Projection' p db r a
- data RestrictionHolder v c
- data Unique u
- data KeyForBackend db v = (DbDescriptor db, PersistEntity v) => KeyForBackend (AutoKeyType db)
- data BackendSpecific
- data ConstructorMarker v a
- data UniqueMarker v a
- data Proxy a
- data HFalse
- data HTrue
- newtype ZT = ZT ZonedTime
- newtype Utf8 = Utf8 Builder
- fromUtf8 :: Utf8 -> ByteString
- delim :: Char
- data Cond db r
- = And (Cond db r) (Cond db r)
- | Or (Cond db r) (Cond db r)
- | Not (Cond db r)
- | Compare ExprRelation (UntypedExpr db r) (UntypedExpr db r)
- | CondRaw (QueryRaw db r)
- | CondEmpty
- data ExprRelation
- data Update db r = forall f a . (Assignable f a, Projection' f db r a) => Update f (UntypedExpr db r)
- (~>) :: (EntityConstr v c, FieldLike f a, ProjectionRestriction f (RestrictionHolder v c), Embedded a) => f -> Selector a a' -> SubField v c a'
- class Assignable f a => FieldLike f a | f -> a where
- fieldChain :: f -> FieldChain
- class Projection f a => Assignable f a | f -> a
- newtype SubField v c a = SubField FieldChain
- data AutoKeyField v c where
- AutoKeyField :: AutoKeyField v c
- type FieldChain = ((String, DbType), [(String, EmbeddedDef)])
- class NeverNull a
- data UntypedExpr db r where
- ExprRaw :: QueryRaw db r -> UntypedExpr db r
- ExprField :: FieldChain -> UntypedExpr db r
- ExprPure :: forall db r a. PurePersistField a => a -> UntypedExpr db r
- ExprCond :: Cond db r -> UntypedExpr db r
- newtype Expr db r a = Expr (UntypedExpr db r)
- data Order db r
- = forall a f . Projection' f db r a => Asc f
- | forall a f . Projection' f db r a => Desc f
- class HasSelectOptions a db r | a -> db r where
- type HasLimit a
- type HasOffset a
- type HasOrder a
- type HasDistinct a
- getSelectOptions :: a -> SelectOptions db r (HasLimit a) (HasOffset a) (HasOrder a) (HasDistinct a)
- data SelectOptions db r hasLimit hasOffset hasOrder hasDistinct = SelectOptions {
- condOptions :: Cond db r
- limitOptions :: Maybe Int
- offsetOptions :: Maybe Int
- orderOptions :: [Order db r]
- distinctOptions :: Bool
- dbSpecificOptions :: [(String, QueryRaw db r)]
- limitTo :: (HasSelectOptions a db r, HasLimit a ~ HFalse) => a -> Int -> SelectOptions db r HTrue (HasOffset a) (HasOrder a) (HasDistinct a)
- offsetBy :: (HasSelectOptions a db r, HasOffset a ~ HFalse) => a -> Int -> SelectOptions db r (HasLimit a) HTrue (HasOrder a) (HasDistinct a)
- orderBy :: (HasSelectOptions a db r, HasOrder a ~ HFalse) => a -> [Order db r] -> SelectOptions db r (HasLimit a) (HasOffset a) HTrue (HasDistinct a)
- distinct :: (HasSelectOptions a db r, HasDistinct a ~ HFalse) => a -> SelectOptions db r (HasLimit a) (HasOffset a) (HasOrder a) HTrue
- data DbTypePrimitive
- data DbType
- data EntityDef = EntityDef {
- entityName :: String
- entitySchema :: Maybe String
- typeParams :: [DbType]
- constructors :: [ConstructorDef]
- data EmbeddedDef = EmbeddedDef Bool [(String, DbType)]
- newtype OtherTypeDef = OtherTypeDef ((DbTypePrimitive -> String) -> String)
- data ConstructorDef = ConstructorDef {
- constrNum :: Int
- constrName :: String
- constrAutoKeyName :: Maybe String
- constrParams :: [(String, DbType)]
- constrUniques :: [UniqueDef]
- class Constructor c where
- phantomConstrNum :: c (a :: * -> *) -> Int
- class PersistEntity v => EntityConstr v c where
- entityConstrNum :: proxy v -> c (a :: * -> *) -> Int
- class PurePersistField uKey => IsUniqueKey uKey where
- extractUnique :: uKey ~ Key v u => v -> uKey
- uniqueNum :: uKey -> Int
- data UniqueDef = UniqueDef {
- uniqueName :: String
- uniqueType :: UniqueType
- uniqueFields :: [(String, DbType)]
- data UniqueType
- data ReferenceActionType
- = NoAction
- | Restrict
- | Cascade
- | SetNull
- | SetDefault
- type ParentTableReference = (Either (EntityDef, Maybe String) (Maybe String, String, [String]), Maybe ReferenceActionType, Maybe ReferenceActionType)
- type SingleMigration = Either [String] [(Bool, Int, String)]
- type NamedMigrations = Map String SingleMigration
- type Migration m = StateT NamedMigrations m ()
- class (Monad m, DbDescriptor (PhantomDb m)) => PersistBackend m where
- type PhantomDb m
- insert :: PersistEntity v => v -> m (AutoKey v)
- insert_ :: PersistEntity v => v -> m ()
- insertBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v))
- insertByAll :: PersistEntity v => v -> m (Either (AutoKey v) (AutoKey v))
- replace :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> v -> m ()
- replaceBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m ()
- select :: (PersistEntity v, EntityConstr v c, HasSelectOptions opts (PhantomDb m) (RestrictionHolder v c)) => opts -> m [v]
- selectAll :: PersistEntity v => m [(AutoKey v, v)]
- get :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m (Maybe v)
- getBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => Key v (Unique u) -> m (Maybe v)
- update :: (PersistEntity v, EntityConstr v c) => [Update (PhantomDb m) (RestrictionHolder v c)] -> Cond (PhantomDb m) (RestrictionHolder v c) -> m ()
- delete :: (PersistEntity v, EntityConstr v c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m ()
- deleteBy :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m ()
- deleteAll :: PersistEntity v => v -> m ()
- count :: (PersistEntity v, EntityConstr v c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m Int
- countAll :: PersistEntity v => v -> m Int
- project :: (PersistEntity v, EntityConstr v c, Projection' p (PhantomDb m) (RestrictionHolder v c) a, HasSelectOptions opts (PhantomDb m) (RestrictionHolder v c)) => p -> opts -> m [a]
- migrate :: PersistEntity v => v -> Migration m
- executeRaw :: Bool -> String -> [PersistValue] -> m ()
- queryRaw :: Bool -> String -> [PersistValue] -> (RowPopper m -> m a) -> m a
- insertList :: PersistField a => [a] -> m Int64
- getList :: PersistField a => Int64 -> m [a]
- class PrimitivePersistField (AutoKeyType db) => DbDescriptor db where
- type AutoKeyType db
- type QueryRaw db :: * -> *
- backendName :: proxy db -> String
- type RowPopper m = m (Maybe [PersistValue])
- newtype DbPersist conn m a = DbPersist {
- unDbPersist :: ReaderT conn m a
- runDbPersist :: Monad m => DbPersist conn m a -> conn -> m a
- class ConnectionManager cm conn | cm -> conn where
- withConn :: (MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m a
- withConnNoTransaction :: (MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m a
- class ConnectionManager cm conn => SingleConnectionManager cm conn
- class Savepoint conn where
- withConnSavepoint :: (MonadBaseControl IO m, MonadIO m) => String -> m a -> conn -> m a
Main types
class (PersistField v, PurePersistField (AutoKey v)) => PersistEntity v whereSource
Only instances of this class can be persisted in a database
data Field v :: ((* -> *) -> *) -> * -> *Source
This type is used for typesafe manipulation of separate fields of datatype v.
Each constructor in Field
corresponds to its field in a datatype v.
It is parametrised by constructor phantom type and field value type.
A unique identifier of a value stored in a database. This may be a primary key, a constraint or unique indices. The second parameter is the key description.
This type is the default autoincremented key for the entity. If entity does not have such key, AutoKey v = ().
type DefaultKey v Source
This type is the default key for the entity.
It is HFalse for entity with one constructor and HTrue for sum types.
entityDef :: v -> EntityDefSource
Returns a complete description of the type
toEntityPersistValues :: PersistBackend m => v -> m ([PersistValue] -> [PersistValue])Source
Marshalls value to a list of PersistValue
ready for insert to a database
fromEntityPersistValues :: PersistBackend m => [PersistValue] -> m (v, [PersistValue])Source
Constructs the value from the list of PersistValue
getUniques :: DbDescriptor db => proxy db -> v -> (Int, [(String, [PersistValue] -> [PersistValue])])Source
Returns constructor number and a list of uniques names and corresponding field values
entityFieldChain :: Field v c a -> FieldChainSource
Is internally used by FieldLike Field instance We could avoid this function if class FieldLike allowed FieldLike Fields Data or FieldLike (Fields Data). However that would require additional extensions in user-space code
data PersistValue Source
A raw value which can be stored in any backend and can be marshalled to
and from a PersistField
.
PersistString String | |
PersistByteString ByteString | |
PersistInt64 Int64 | |
PersistDouble Double | |
PersistBool Bool | |
PersistDay Day | |
PersistTimeOfDay TimeOfDay | |
PersistUTCTime UTCTime | |
PersistZonedTime ZT | |
PersistNull | |
PersistCustom Utf8 [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. |
class PersistField a whereSource
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.
persistName :: a -> StringSource
Return name of the type. If it is polymorphic, the names of parameter types are separated with delim
symbol
toPersistValues :: PersistBackend m => a -> m ([PersistValue] -> [PersistValue])Source
Convert a value into something which can be stored in a database column. Note that for complex datatypes it may insert them to return identifier
fromPersistValues :: PersistBackend m => [PersistValue] -> m (a, [PersistValue])Source
Constructs a value from a PersistValue
. For complex datatypes it may query the database
Description of value type
PersistField Bool | |
PersistField Double | |
PersistField Int | |
PersistField Int8 | |
PersistField Int16 | |
PersistField Int32 | |
PersistField Int64 | |
PersistField Word8 | |
PersistField Word16 | |
PersistField Word32 | |
PersistField Word64 | |
PersistField String | |
PersistField () | |
PersistField ByteString | |
PersistField Text | |
PersistField ZonedTime | |
PersistField TimeOfDay | |
PersistField UTCTime | |
PersistField Day | |
PersistField a => PersistField [a] | |
(PersistField a, NeverNull a) => PersistField (Maybe a) | |
(PersistField a, PersistField b) => PersistField (a, b) | |
(DbDescriptor db, PersistEntity v) => PersistField (KeyForBackend db v) | |
(PersistField a, PersistField b, PersistField c) => PersistField (a, b, c) | |
(PersistField a, PersistField b, PersistField c, PersistField d) => PersistField (a, b, c, d) | |
(PersistField a, PersistField b, PersistField c, PersistField d, PersistField e) => PersistField (a, b, c, d, e) |
class PersistField a => SinglePersistField a whereSource
Represents all datatypes that map into a single column. Getting value for that column might require monadic actions to access other tables.
toSinglePersistValue :: PersistBackend m => a -> m PersistValueSource
fromSinglePersistValue :: PersistBackend m => PersistValue -> m aSource
class PersistField a => PurePersistField a whereSource
Represents all datatypes that map into several columns. Getting values for those columns is pure.
toPurePersistValues :: DbDescriptor db => proxy db -> a -> [PersistValue] -> [PersistValue]Source
fromPurePersistValues :: DbDescriptor db => proxy db -> [PersistValue] -> (a, [PersistValue])Source
PurePersistField () | |
PrimitivePersistField a => PurePersistField a | |
(PurePersistField a, PurePersistField b) => PurePersistField (a, b) | |
(PurePersistField a, PurePersistField b, PurePersistField c) => PurePersistField (a, b, c) | |
(PurePersistField a, PurePersistField b, PurePersistField c, PurePersistField d) => PurePersistField (a, b, c, d) | |
(PurePersistField a, PurePersistField b, PurePersistField c, PurePersistField d, PurePersistField e) => PurePersistField (a, b, c, d, e) |
class PersistField a => PrimitivePersistField a whereSource
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. A type which has an instance of PrimitivePersistField
should be an instance of superclasses SinglePersistField
and PurePersistField
as well.
toPrimitivePersistValue :: DbDescriptor db => proxy db -> a -> PersistValueSource
fromPrimitivePersistValue :: DbDescriptor db => proxy db -> PersistValue -> aSource
class PersistField v => Embedded v whereSource
selectorNum :: Selector v a -> IntSource
(PersistField a', PersistField b') => Embedded (a', b') | |
(PersistField a', PersistField b', PersistField c') => Embedded (a', b', c') | |
(PersistField a', PersistField b', PersistField c', PersistField d') => Embedded (a', b', c', d') | |
(PersistField a', PersistField b', PersistField c', PersistField d', PersistField e') => Embedded (a', b', c', d', e') |
class PersistField a => Projection p a | p -> a whereSource
Any data that can be fetched from a database
type ProjectionDb p db :: ConstraintSource
type ProjectionRestriction p r :: ConstraintSource
projectionExprs :: (ProjectionDb p db, ProjectionRestriction p r) => p -> [UntypedExpr db r] -> [UntypedExpr db r]Source
It returns multiple expressions that can be transformed into values which can be selected. Difflist is used for concatenation efficiency.
projectionResult :: PersistBackend m => p -> [PersistValue] -> m (a, [PersistValue])Source
It is like fromPersistValues
. However, we cannot use it for projections in all cases. For the PersistEntity
instances fromPersistValues
expects entity id instead of the entity values.
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => Projection (u (UniqueMarker v)) k | |
EntityConstr v c => Projection (c (ConstructorMarker v)) v | |
(EntityConstr v c, ~ * a (AutoKey v)) => Projection (AutoKeyField v c) a | |
~ * a Bool => Projection (Cond db r) a | |
(Projection a1 a1', Projection a2 a2') => Projection (a1, a2) (a1', a2') | |
PersistField a => Projection (Expr db r a) a | |
(EntityConstr v c, PersistField a) => Projection (SubField v c a) a | |
(EntityConstr v c, PersistField a) => Projection (Field v c a) a | |
(Projection a1 a1', Projection a2 a2', Projection a3 a3') => Projection (a1, a2, a3) (a1', a2', a3') | |
(Projection a1 a1', Projection a2 a2', Projection a3 a3', Projection a4 a4') => Projection (a1, a2, a3, a4) (a1', a2', a3', a4') | |
(Projection a1 a1', Projection a2 a2', Projection a3 a3', Projection a4 a4', Projection a5 a5') => Projection (a1, a2, a3, a4, a5) (a1', a2', a3', a4', a5') |
class (Projection p a, ProjectionDb p db, ProjectionRestriction p r) => Projection' p db r a Source
(Projection p a, ProjectionDb p db, ProjectionRestriction p r) => Projection' p db r a |
data RestrictionHolder v c Source
data KeyForBackend db v Source
It allows to store autogenerated keys of one database in another
(DbDescriptor db, PersistEntity v) => KeyForBackend (AutoKeyType db) |
(DbDescriptor db, PersistEntity v) => PrimitivePersistField (KeyForBackend db v) | |
(DbDescriptor db, PersistEntity v) => PersistField (KeyForBackend db v) | |
NeverNull (KeyForBackend db v) |
data BackendSpecific Source
Key marked with this type can have value for any backend
data ConstructorMarker v a Source
A phantom datatype to make instance head different c (ConstructorMarker v)
EntityConstr v c => Projection (c (ConstructorMarker v)) v |
data UniqueMarker v a Source
A phantom datatype to make instance head different u (UniqueMarker v)
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u)), ~ * (RestrictionHolder v c) r') => Expression db r' (u (UniqueMarker v)) | |
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => FieldLike (u (UniqueMarker v)) k | |
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => Assignable (u (UniqueMarker v)) k | |
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => Projection (u (UniqueMarker v)) k |
Datatype for incremental building SQL queries
fromUtf8 :: Utf8 -> ByteStringSource
Constructing expressions
Represents condition for a query.
And (Cond db r) (Cond db r) | |
Or (Cond db r) (Cond db r) | |
Not (Cond db r) | |
Compare ExprRelation (UntypedExpr db r) (UntypedExpr db r) | |
CondRaw (QueryRaw db r) | |
CondEmpty |
(~ * db' db, ~ * r' r) => Expression db' r' (Cond db r) | |
~ * a Bool => Projection (Cond db r) a | |
HasSelectOptions (Cond db r) db r |
forall f a . (Assignable f a, Projection' f db r a) => Update f (UntypedExpr db r) |
(~>) :: (EntityConstr v c, FieldLike f a, ProjectionRestriction f (RestrictionHolder v c), Embedded a) => f -> Selector a a' -> SubField v c a'Source
Accesses fields of the embedded datatypes. For example, SomeField ==. ("abc", "def") ||. SomeField ~> Tuple2_0Selector ==. "def"
class Assignable f a => FieldLike f a | f -> a whereSource
This subset of Assignable is for plain database fields.
fieldChain :: f -> FieldChainSource
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => FieldLike (u (UniqueMarker v)) k | |
(EntityConstr v c, ~ * a (AutoKey v)) => FieldLike (AutoKeyField v c) a | |
(EntityConstr v c, PersistField a) => FieldLike (SubField v c a) a | |
(EntityConstr v c, PersistField a) => FieldLike (Field v c a) a |
class Projection f a => Assignable f a | f -> aSource
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.
(PersistEntity v, IsUniqueKey k, ~ * k (Key v (Unique u))) => Assignable (u (UniqueMarker v)) k | |
(EntityConstr v c, ~ * a (AutoKey v)) => Assignable (AutoKeyField v c) a | |
(EntityConstr v c, PersistField a) => Assignable (SubField v c a) a | |
(EntityConstr v c, PersistField a) => Assignable (Field v c a) a |
(EntityConstr v c, PersistField a, ~ * (RestrictionHolder v c) r') => Expression db r' (SubField v c a) | |
(EntityConstr v c, PersistField a) => FieldLike (SubField v c a) a | |
(EntityConstr v c, PersistField a) => Assignable (SubField v c a) a | |
(EntityConstr v c, PersistField a) => Projection (SubField v c a) a |
data AutoKeyField v c whereSource
It can be used in expressions like a regular field.
For example, delete (AutoKeyField ==. k)
or delete (AutoKeyField ==. k ||. SomeField ==. "DUPLICATE")
AutoKeyField :: AutoKeyField v c |
(EntityConstr v c, ~ * (RestrictionHolder v c) r') => Expression db r' (AutoKeyField v c) | |
(EntityConstr v c, ~ * a (AutoKey v)) => FieldLike (AutoKeyField v c) a | |
(EntityConstr v c, ~ * a (AutoKey v)) => Assignable (AutoKeyField v c) a | |
(EntityConstr v c, ~ * a (AutoKey v)) => Projection (AutoKeyField v c) a |
type FieldChain = ((String, DbType), [(String, EmbeddedDef)])Source
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])].
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.
data UntypedExpr db r whereSource
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
ExprRaw :: QueryRaw db r -> UntypedExpr db r | |
ExprField :: FieldChain -> UntypedExpr db r | |
ExprPure :: forall db r a. PurePersistField a => a -> UntypedExpr db r | |
ExprCond :: Cond db r -> UntypedExpr db r |
Expr with phantom type helps to keep type safety in complex expressions
Expr (UntypedExpr db r) |
(PersistField a, ~ * db' db, ~ * r' r) => Expression db' r' (Expr db r a) | |
(SqlDb db, PersistField a, Enum a) => Enum (Expr db r a) | |
Eq (Expr db r a) | |
(FloatingSqlDb db, PersistField a, Floating a) => Floating (Expr db r a) | |
(SqlDb db, PersistField a, Fractional a) => Fractional (Expr db r a) | |
(SqlDb db, PurePersistField a, Integral a) => Integral (Expr db r a) | |
(SqlDb db, PersistField a, Num a) => Num (Expr db r a) | |
(SqlDb db, PersistField a, Ord a) => Ord (Expr db r a) | |
(SqlDb db, PersistField a, Real a) => Real (Expr db r a) | |
Show (Expr db r a) | |
PersistField a => Projection (Expr db r a) a |
Defines sort order of a result-set
forall a f . Projection' f db r a => Asc f | |
forall a f . Projection' f db r a => Desc f |
class HasSelectOptions a db r | a -> db r whereSource
This class helps to check that limit, offset, or order clauses are added to condition only once.
getSelectOptions :: a -> SelectOptions db r (HasLimit a) (HasOffset a) (HasOrder a) (HasDistinct a)Source
HasSelectOptions (Cond db r) db r | |
HasSelectOptions (SelectOptions db r hasLimit hasOffset hasOrder hasDistinct) db r |
data SelectOptions db r hasLimit hasOffset hasOrder hasDistinct Source
SelectOptions | |
|
HasSelectOptions (SelectOptions db r hasLimit hasOffset hasOrder hasDistinct) db r |
limitTo :: (HasSelectOptions a db r, HasLimit a ~ HFalse) => a -> Int -> SelectOptions db r HTrue (HasOffset a) (HasOrder a) (HasDistinct a)Source
offsetBy :: (HasSelectOptions a db r, HasOffset a ~ HFalse) => a -> Int -> SelectOptions db r (HasLimit a) HTrue (HasOrder a) (HasDistinct a)Source
orderBy :: (HasSelectOptions a db r, HasOrder a ~ HFalse) => a -> [Order db r] -> SelectOptions db r (HasLimit a) (HasOffset a) HTrue (HasDistinct a)Source
distinct :: (HasSelectOptions a db r, HasDistinct a ~ HFalse) => a -> SelectOptions db r (HasLimit a) (HasOffset a) (HasOrder a) HTrueSource
Select DISTINCT rows. select $ distinct CondEmpty
Type description
data DbTypePrimitive Source
A DB data type. Naming attempts to reflect the underlying Haskell datatypes, eg DbString instead of DbVarchar. Different databases may have different representations for these types.
DbTypePrimitive DbTypePrimitive Bool (Maybe String) (Maybe ParentTableReference) | type, nullable, default value, reference |
DbEmbedded EmbeddedDef (Maybe ParentTableReference) | |
DbList String DbType | List table name and type of its argument |
Describes an ADT.
EntityDef | |
|
data EmbeddedDef Source
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.
EmbeddedDef Bool [(String, DbType)] |
newtype OtherTypeDef Source
Stores name for a database type
OtherTypeDef ((DbTypePrimitive -> String) -> String) |
data ConstructorDef Source
Describes an entity constructor
ConstructorDef | |
|
class Constructor c whereSource
Phantom constructors are made instances of this class. This class should be used only by Template Haskell codegen
phantomConstrNum :: c (a :: * -> *) -> IntSource
Returns constructor index which can be used to get ConstructorDef from EntityDef
class PersistEntity v => EntityConstr v c whereSource
This class helps type inference in cases when query does not contain any fields which
define the constructor, but the entity has only one.
For example, in select $ AutoKeyField ==. k
the condition would need type annotation with constructor name only if we select a sum type.
entityConstrNum :: proxy v -> c (a :: * -> *) -> IntSource
(PersistEntity v, EntityConstr' (IsSumType v) c) => EntityConstr v c |
class PurePersistField uKey => IsUniqueKey uKey whereSource
extractUnique :: uKey ~ Key v u => v -> uKeySource
Creates value of unique key using the data extracted from the passed value
uniqueNum :: uKey -> IntSource
Ordinal number of the unique constraint in the list returned by constrUniques
Unique name and list of the field names that form a unique combination
UniqueDef | |
|
data UniqueType Source
Defines how to treat the unique set of fields for a datatype
UniqueConstraint | |
UniqueIndex | |
UniquePrimary Bool | is autoincremented |
type ParentTableReference = (Either (EntityDef, Maybe String) (Maybe String, String, [String]), Maybe ReferenceActionType, Maybe ReferenceActionType)Source
The reference contains either EntityDef of the parent table and name of the unique constraint. Or for tables not mapped by Groundhog schema name, table name, and list of columns Reference to the autogenerated key of a mapped entity = (Left (entityDef, Nothing), onDelete, onUpdate) Reference to a unique key of a mapped entity = (Left (entityDef, Just uniqueKeyName), onDelete, onUpdate) Reference to a table that is not mapped = (Right (schema, tableName, columns), onDelete, onUpdate)
Migration
type SingleMigration = Either [String] [(Bool, Int, String)]Source
Either error messages or migration queries with safety flag and execution order
type NamedMigrations = Map String SingleMigrationSource
Datatype names and corresponding migrations
type Migration m = StateT NamedMigrations m ()Source
Database
class (Monad m, DbDescriptor (PhantomDb m)) => PersistBackend m whereSource
A token which defines the DB type. For example, different monads working with Sqlite, return may Sqlite type.
insert :: PersistEntity v => v -> m (AutoKey v)Source
Insert a new record to a database and return its autogenerated key or ()
insert_ :: PersistEntity v => v -> m ()Source
Insert a new record to a database. For some backends it may be faster than insert
.
insertBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m (Either (AutoKey v) (AutoKey v))Source
Try to insert a record and return Right newkey. If there is a constraint violation for the given constraint, Left oldkey is returned , where oldkey is an identifier of the record with the matching values.
insertByAll :: PersistEntity v => v -> m (Either (AutoKey v) (AutoKey v))Source
Try to insert a record and return Right newkey. If there is a constraint violation for any constraint, Left oldkey is returned , where oldkey is an identifier of the record with the matching values. Note that if several constraints are violated, a key of an arbitrary matching record is returned.
replace :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> v -> m ()Source
Replace a record with the given autogenerated key. Result is undefined if the record does not exist.
replaceBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => u (UniqueMarker v) -> v -> m ()Source
Replace a record. The unique key marker defines what unique key of the entity is used.
select :: (PersistEntity v, EntityConstr v c, HasSelectOptions opts (PhantomDb m) (RestrictionHolder v c)) => opts -> m [v]Source
Return a list of the records satisfying the condition. Example: select $ (FirstField ==. "abc" &&. SecondField >. "def") `orderBy` [Asc ThirdField] `limitTo` 100
selectAll :: PersistEntity v => m [(AutoKey v, v)]Source
Return a list of all records. Order is undefined. It can be useful for datatypes with multiple constructors.
get :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m (Maybe v)Source
Fetch an entity from a database
getBy :: (PersistEntity v, IsUniqueKey (Key v (Unique u))) => Key v (Unique u) -> m (Maybe v)Source
Fetch an entity from a database by its unique key
update :: (PersistEntity v, EntityConstr v c) => [Update (PhantomDb m) (RestrictionHolder v c)] -> Cond (PhantomDb m) (RestrictionHolder v c) -> m ()Source
Update the records satisfying the condition. Example: update [FirstField =. "abc"] $ FirstField ==. "def"
delete :: (PersistEntity v, EntityConstr v c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m ()Source
Remove the records satisfying the condition
deleteBy :: (PersistEntity v, PrimitivePersistField (Key v BackendSpecific)) => Key v BackendSpecific -> m ()Source
Remove the record with given key. No-op if the record does not exist
deleteAll :: PersistEntity v => v -> m ()Source
Remove all records. The entity parameter is used only for type inference.
count :: (PersistEntity v, EntityConstr v c) => Cond (PhantomDb m) (RestrictionHolder v c) -> m IntSource
Count total number of records satisfying the condition
countAll :: PersistEntity v => v -> m IntSource
Count total number of records with all constructors. The entity parameter is used only for type inference
project :: (PersistEntity v, EntityConstr v c, Projection' p (PhantomDb m) (RestrictionHolder v c) a, HasSelectOptions opts (PhantomDb m) (RestrictionHolder v c)) => p -> opts -> m [a]Source
Fetch projection of some fields. Example: project (SecondField, ThirdField) $ (FirstField ==. "abc" &&. SecondField >. "def") `orderBy` [Asc ThirdField] `offsetBy` 100
migrate :: PersistEntity v => v -> Migration mSource
Check database schema and create migrations for the entity and the entities it contains
:: Bool | keep in cache |
-> String | query |
-> [PersistValue] | positional parameters |
-> m () |
Execute raw query
:: Bool | keep in cache |
-> String | query |
-> [PersistValue] | positional parameters |
-> (RowPopper m -> m a) | results processing function |
-> m a |
Execute raw query with results
insertList :: PersistField a => [a] -> m Int64Source
getList :: PersistField a => Int64 -> m [a]Source
class PrimitivePersistField (AutoKeyType db) => DbDescriptor db whereSource
type AutoKeyType db Source
Type of the database default autoincremented key. For example, Sqlite has Int64
type QueryRaw db :: * -> *Source
Value of this type can be used as a part of a query. For example, it can be RenderS for relational databases, or BSON for MongoDB.
backendName :: proxy db -> StringSource
Name of backend
type RowPopper m = m (Maybe [PersistValue])Source
newtype DbPersist conn m a Source
DbPersist | |
|
MonadBase IO m => MonadBase IO (DbPersist conn m) | |
MonadBaseControl IO m => MonadBaseControl IO (DbPersist conn m) | |
Monad m => MonadReader conn (DbPersist conn m) | |
MonadTrans (DbPersist conn) | |
MonadTransControl (DbPersist conn) | |
Monad m => Monad (DbPersist conn m) | |
Functor m => Functor (DbPersist conn m) | |
Applicative m => Applicative (DbPersist conn m) | |
MonadLogger m => MonadLogger (DbPersist conn m) | |
MonadIO m => MonadIO (DbPersist conn m) |
runDbPersist :: Monad m => DbPersist conn m a -> conn -> m aSource
Connections and transactions
class ConnectionManager cm conn | cm -> conn whereSource
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.
withConn :: (MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m aSource
Extracts the connection from manager and opens the transaction.
withConnNoTransaction :: (MonadBaseControl IO m, MonadIO m) => (conn -> m a) -> cm -> m aSource
Extracts the connection.
class ConnectionManager cm conn => SingleConnectionManager cm conn Source
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 Savepoint conn whereSource
withConnSavepoint :: (MonadBaseControl IO m, MonadIO m) => String -> m a -> conn -> m aSource
Wraps the passed action into a named savepoint