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.Query

Contents

Description

 

Synopsis

Tables

data TableDescription Source

Description of a table type

Constructors

TableDescription 

Fields

tableName :: String

Table name

tableIdentifier :: String

Identifier column name

class DescribableTable a where Source

Attach meta data to a table type

Minimal complete definition

Nothing

Methods

describeTable :: Proxy a -> TableDescription Source

Describe the table.

describeTableName :: Proxy a -> String Source

Describe table name.

describeTableIdentifier :: Proxy a -> String Source

Describe table identifier.

Querying

data Query Source

Query including statement and parameters. Use the pgsq quasi-quoter to conveniently create queries.

Constructors

Query 

Fields

queryStatement :: !ByteString

Statement

queryParams :: ![Value]

Parameters

pgsq :: QuasiQuoter Source

Generate a Query from a SQL statement.

Table and column names

All plain identifiers will be treated as Haskell names. They are going to be resolved to their fully-qualified and quoted version. Beware, the use of names which don't refer to a table type or field will likely result in unknown table or column errors. The associated table name of a type is retrieved using describeTableName. If you don't want a name to be resolved use a quoted identifier.

Example:

{-# LANGUAGE QuasiQuotes #-}
module MyModule where

...

data Table = Table { myField :: Int }
mkTable ''Table []

myQuery :: Query
myQuery = [pgsq| SELECT * FROM Table WHERE myField > 1337 |]

The SQL statement associated with myQuery will be:

SELECT * FROM "MyModule.Table" WHERE "MyModule.myField" > 1337

Variables

You can use reference variables with $myVariable. The variable's type has to be an instance of Column, otherwise it cannot be attached as query parameter.

Example:

magicNumber :: Int
magicNumber = 1337

myQuery :: Query
myQuery = [pgsq| SELECT * FROM Table WHERE myField > $magicNumber |]

Row identifiers

Each instance of (Table a) => Row a, (Table a) => Reference a and each row of the actual table inside the database has an identifier value. These identifiers are used to reference specific rows. The identifier column is exposed via the &MyTable pattern. Identifier field names are resolved using describeTableIdentifier.

Example:

[pgsq| SELECT *
       FROM TableA, TableB
       WHERE refToB = &TableB |]

Note refToB is a field of TableA. In different circumstances one would write such query as follows.

SELECT *
FROM TableA a, Table b
WHERE a.refToB = b.id