pg-store-0.0.1: Dead 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

Description

 

Synopsis

Documentation

data TableDescription Source

Description of a table type

Constructors

TableDescription 

Fields

tableName :: String

Table name

tableIdentifier :: String

Identifier column name

class DescribableTable a => Table a where Source

Methods

insert :: a -> Errand (Reference a) Source

Insert a row into the table and return a Reference to the inserted row.

find :: HasID i => i a -> Errand (Row a) Source

Find the row identified by the given reference.

update :: HasID i => i a -> a -> Errand () Source

Update an existing row.

delete :: HasID i => i a -> Errand () Source

Delete a row from the table.

createQuery :: Proxy a -> Query Source

Generate the query which creates this table inside the database. Use mkCreateQuery for convenience.

tableResultProcessor :: ResultProcessor [Row a] Source

Extract rows from a result set.

tableRefResultProcessor :: ResultProcessor [Reference a] Source

Extract only a Reference to each row.

data Row a Source

Resolved row

Constructors

Row 

Fields

rowID :: !Int64

Identifier

rowValue :: !a

Value

Instances

HasID Row Source 
Eq a => Eq (Row a) Source 
Ord a => Ord (Row a) Source 
Show a => Show (Row a) Source 
Table a => Result (Row a) Source 

newtype Reference a Source

Reference to a row

Constructors

Reference Int64 

class HasID a where Source

A value of that type contains an ID.

Methods

referenceID :: a b -> Int64 Source

Retrieve the underlying ID.

data TableConstraint Source

Options to mkTable.

Constructors

Unique [Name]

A combination of fields must be unique. Unique ['name1, 'name2, ...] works analogous to the following table constraint: UNIQUE (name1, name2, ...)

ForeignKey [Name] Name [Name]

A combination of fields references another combination of fields from a different table. ForeignKey ['name1, 'name2, ...] ''RefTable ['refname1, 'refname2, ...] works like this table constraint in SQL: FOREIGN KEY (name1, name2, ...) REFERENCES RefTable(refname1, refname2, ...)

mkTable :: Name -> [TableConstraint] -> Q [Dec] Source

Implement Table for a data 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 #-}
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 []

data MovieCast = MovieCast {
    movieCastMovie :: Reference Movie,
    movieCastActor :: Reference Actor
} deriving Show

mkTable ''MovieCast []

mkCreateQuery :: Name -> Q Exp Source

Generate a Query which will create the table described my the given type.

Example:

data Table = Table { myField :: Int }
mkTable ''Table []
...
query_ $(mkCreateQuery ''Table)