| Copyright | (c) Ole Krüger 2015-2016 |
|---|---|
| License | BSD3 |
| Maintainer | Ole Krüger <ole@vprsm.de> |
| Safe Haskell | None |
| Language | Haskell2010 |
Database.PostgreSQL.Store.Table
Description
- 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.
Constructors
| Reference | |
Fields
| |
Table types
Qualify a as a table type. mkTable can implement this class for you.
Minimal complete definition
Methods
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.
Constructors
| Unique [Name] | A combination of fields must be unique.
|
| Check String | The given statement must evaluate to true. Just like |
Instances
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
} deriving Show
mkTable ''Movie []
data Actor = Actor {
actorName :: String,
actorAge :: Int
} deriving Show
mkTable ''Actor [Unique ['actorName], Check [pgss| actorAge >= 18 |]]
data MovieCast = MovieCast {
movieCastMovie :: Reference Movie,
movieCastActor :: Reference Actor
} deriving Show
mkTable ''MovieCast [Unique ['movieCastMovie, 'movieCastActor]]
In this example, Reference takes care of adding the FOREIGN KEY constraint, so we don't have
to.