Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- retrieveById :: forall a id. (Entity a, Convertible id SqlValue) => Conn -> id -> IO (Maybe a)
- retrieveAll :: forall a. Entity a => Conn -> IO [a]
- retrieveAllWhere :: forall a. Entity a => Conn -> String -> SqlValue -> IO [a]
- entitiesFromRows :: forall a. Entity a => Conn -> [[SqlValue]] -> IO [a]
- persist :: forall a. Entity a => Conn -> a -> IO ()
- insert :: forall a. Entity a => Conn -> a -> IO ()
- insertMany :: forall a. Entity a => Conn -> [a] -> IO ()
- update :: forall a. Entity a => Conn -> a -> IO ()
- updateMany :: forall a. Entity a => Conn -> [a] -> IO ()
- delete :: forall a. Entity a => Conn -> a -> IO ()
- setupTableFor :: forall a. Entity a => Conn -> IO ()
- idValue :: forall a. Entity a => Conn -> a -> IO SqlValue
- data Conn = forall conn.IConnection conn => Conn {
- db :: Database
- implicitCommit :: Bool
- connection :: conn
- connect :: forall conn. IConnection conn => Database -> conn -> Conn
- data Database
- class (Generic a, HasConstructor (Rep a), HasSelectors (Rep a)) => Entity a where
- class GToRow f
- class GFromRow f
- columnNameFor :: forall a. Entity a => String -> String
- maybeFieldTypeFor :: forall a. Entity a => String -> Maybe TypeRep
- toString :: forall a. (Generic a, GShow' (Rep a)) => a -> String
- type EntityId = (String, SqlValue)
- entityId :: forall a. Entity a => Conn -> a -> IO EntityId
- data TypeInfo a
- typeInfo :: forall a. (HasConstructor (Rep a), HasSelectors (Rep a), Generic a) => TypeInfo a
Documentation
retrieveById :: forall a id. (Entity a, Convertible id SqlValue) => Conn -> id -> IO (Maybe a) Source #
A function that retrieves an entity from a database. The function takes entity id as parameter. If an entity with the given id exists in the database, it is returned as a Just value. If no such entity exists, Nothing is returned. An error is thrown if there are more than one entity with the given id.
retrieveAll :: forall a. Entity a => Conn -> IO [a] Source #
This function retrieves all entities of type a
from a database.
The function takes an HDBC connection as parameter.
The type a
is determined by the context of the function call.
retrieveAllWhere :: forall a. Entity a => Conn -> String -> SqlValue -> IO [a] Source #
This function retrieves all entities of type a
where a given field has a given value.
The function takes an HDBC connection, the name of the field and the value as parameters.
The type a
is determined by the context of the function call.
The function returns a (possibly empty) list of all matching entities.
entitiesFromRows :: forall a. Entity a => Conn -> [[SqlValue]] -> IO [a] Source #
This function converts a list of database rows, represented as a `[[SqlValue]]` to a list of entities.
The function takes an HDBC connection and a list of database rows as parameters.
The type a
is determined by the context of the function call.
The function returns a (possibly empty) list of all matching entities.
The function is used internally by retrieveAll
and retrieveAllWhere
.
But it can also be used to convert the result of a custom SQL query to a list of entities.
persist :: forall a. Entity a => Conn -> a -> IO () Source #
A function that persists an entity to a database. The function takes an HDBC connection and an entity as parameters. The entity is either inserted or updated, depending on whether it already exists in the database. The required SQL statements are generated dynamically using Haskell generics and reflection
insert :: forall a. Entity a => Conn -> a -> IO () Source #
A function that explicitely inserts an entity into a database.
insertMany :: forall a. Entity a => Conn -> [a] -> IO () Source #
A function that inserts a list of entities into a database. The function takes an HDBC connection and a list of entities as parameters. The insert-statement is compiled only once and then executed for each entity.
update :: forall a. Entity a => Conn -> a -> IO () Source #
A function that explicitely updates an entity in a database.
updateMany :: forall a. Entity a => Conn -> [a] -> IO () Source #
A function that updates a list of entities in a database. The function takes an HDBC connection and a list of entities as parameters. The update-statement is compiled only once and then executed for each entity.
setupTableFor :: forall a. Entity a => Conn -> IO () Source #
set up a table for a given entity type. The table is dropped and recreated.
idValue :: forall a. Entity a => Conn -> a -> IO SqlValue Source #
A function that returns the primary key value of an entity as a SqlValue.
A wrapper around an HDBC IConnection.
forall conn.IConnection conn => Conn | |
|
Instances
IConnection Conn Source # | manually implement the IConnection type class for the Conn type. |
Defined in Database.GP.Conn disconnect :: Conn -> IO () runRaw :: Conn -> String -> IO () run :: Conn -> String -> [SqlValue] -> IO Integer prepare :: Conn -> String -> IO Statement hdbcDriverName :: Conn -> String hdbcClientVer :: Conn -> String proxiedClientName :: Conn -> String proxiedClientVer :: Conn -> String dbServerVer :: Conn -> String dbTransactionSupport :: Conn -> Bool getTables :: Conn -> IO [String] describeTable :: Conn -> String -> IO [(String, SqlColDesc)] |
connect :: forall conn. IConnection conn => Database -> conn -> Conn Source #
a smart constructor for the Conn type.
An enumeration of the supported database types.
Instances
Enum Database Source # | |
Show Database Source # | |
Eq Database Source # | |
class (Generic a, HasConstructor (Rep a), HasSelectors (Rep a)) => Entity a where Source #
Nothing
fromRow :: Conn -> [SqlValue] -> IO a Source #
Converts a database row to a value of type a
.
toRow :: Conn -> a -> IO [SqlValue] Source #
Converts a value of type a
to a database row.
Returns the name of the primary key field for a type a
.
fieldsToColumns :: [(String, String)] Source #
Returns a list of tuples that map field names to column names for a type a
.
default fieldsToColumns :: [(String, String)] Source #
Returns the name of the table for a type a
.
Instances
GToRow (U1 :: k -> Type) Source # | |
Defined in Database.GP.Entity | |
(GToRow a, GToRow b) => GToRow (a :*: b :: k -> Type) Source # | |
Defined in Database.GP.Entity | |
Convertible a SqlValue => GToRow (K1 i a :: k -> Type) Source # | |
Defined in Database.GP.Entity | |
GToRow a => GToRow (M1 i c a :: k -> Type) Source # | |
Defined in Database.GP.Entity |
gfromRow
Instances
GFromRow (U1 :: k -> Type) Source # | |
Defined in Database.GP.Entity | |
(KnownNat (NumFields f), GFromRow f, GFromRow g) => GFromRow (f :*: g :: Type -> Type) Source # | This instance is the most interesting one. It splits the list of
|
Defined in Database.GP.Entity | |
Convertible SqlValue a => GFromRow (K1 i a :: k -> Type) Source # | |
Defined in Database.GP.Entity | |
GFromRow a => GFromRow (M1 i c a :: k -> Type) Source # | |
Defined in Database.GP.Entity |
columnNameFor :: forall a. Entity a => String -> String Source #
A convenience function: returns the name of the column for a field of a type a
.
toString :: forall a. (Generic a, GShow' (Rep a)) => a -> String Source #
Returns a string representation of a value of type a
.
type EntityId = (String, SqlValue) Source #
The EntityId is a tuple of the constructor name and the primary key value of an Entity.
entityId :: forall a. Entity a => Conn -> a -> IO EntityId Source #
Computes the EntityId of an entity. The EntityId of an entity is a (typeRep, idValue) tuple.
A data type holding meta-data about a type.
The Phantom type parameter a
ensures type safety for reflective functions
that use this type to create type instances (See module RecordtypeReflection).
typeInfo :: forall a. (HasConstructor (Rep a), HasSelectors (Rep a), Generic a) => TypeInfo a Source #
this function is a smart constructor for TypeInfo objects.
It takes a value of type a
and returns a `TypeInfo a` object.
If the type has no named fields, an error is thrown.
If the type has more than one constructor, an error is thrown.
Orphan instances
Enum a => Convertible SqlValue a Source # | |
safeConvert :: SqlValue -> ConvertResult a | |
Enum a => Convertible a SqlValue Source # | |
safeConvert :: a -> ConvertResult SqlValue |