Copyright | (c) Ole Krüger 2015-2016 |
---|---|
License | BSD3 |
Maintainer | Ole Krüger <ole@vprsm.de> |
Safe Haskell | None |
Language | Haskell2010 |
- newtype Reference a = Reference {
- referenceID :: Int64
- class Table a where
- mkCreateQuery :: Name -> Q Exp
- data TableConstraint
- mkTable :: Name -> [TableConstraint] -> Q [Dec]
Auxiliary data types
Reference a row of type a
.
Table types
Qualify a
as a table type. mkTable
can implement this class for you.
insert :: a -> Errand (Reference a) Source #
Insert a row into the table and return a Reference
to the inserted row.
insertMany :: [a] -> Errand [Reference a] Source #
Insert multiple rows into the table at once.
find :: Reference a -> Errand a Source #
Find the row identified by the given reference.
update :: Reference a -> a -> Errand () Source #
Update an existing row.
delete :: Reference a -> Errand () Source #
Delete a row from the table.
createTableQuery :: Proxy a -> Query Source #
Generate the query which creates this table inside the database.
Use mkCreateQuery
for convenience.
mkCreateQuery :: Name -> Q Exp Source #
Generate a Query
expression which will create the table described by the given type.
Example:
data Table = Table { myField :: Int }mkTable
''Table [] ...query_
$(mkCreateQuery
''Table)
Table generation
data TableConstraint Source #
Options to mkTable
.
mkTable :: Name -> [TableConstraint] -> Q [Dec] Source #
Implement the type classes QueryTable
, Table
and Result
for the given type.
The given type must fulfill these requirements:
- Data type
- No type context
- No type variables
- One record constructor with 1 or more fields
- All field types must have an instance of
Column
Example:
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-} module Movies where ... data Movie = Movie { movieTitle ::String
, movieYear ::Int
} derivingShow
mkTable
''Movie [] data Actor = Actor { actorName ::String
, actorAge ::Int
} derivingShow
mkTable
''Actor [Unique
['actorName],Check
[pgss
| actorAge >= 18 |]] data MovieCast = MovieCast { movieCastMovie ::Reference
Movie, movieCastActor ::Reference
Actor } derivingShow
mkTable
''MovieCast [Unique
['movieCastMovie, 'movieCastActor]]
In this example, Reference
takes care of adding the FOREIGN KEY
constraint, so we don't have
to.