pg-store-0.1.0: Simple storage interface to PostgreSQL

Copyright(c) Ole Krüger 2015-2016
LicenseBSD3
MaintainerOle Krüger <ole@vprsm.de>
Safe HaskellNone
LanguageHaskell2010

Database.PostgreSQL.Store.Table

Contents

Description

 

Synopsis

Auxiliary data types

Table types

class Table a where Source #

Qualify a as a table type. mkTable can implement this class for you.

Minimal complete definition

insert, find, update, delete, createTableQuery

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. Unique ['name1, 'name2, ...] works analogous to the table constraint UNIQUE (name1, name2, ...) in SQL.

Check String

The given statement must evaluate to true. Just like CHECK (statement) in SQL.

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.