Copyright | (c) Ole Krüger 2015-2016 |
---|---|
License | BSD3 |
Maintainer | Ole Krüger <ole@vprsm.de> |
Safe Haskell | None |
Language | Haskell2010 |
- data TableDescription = TableDescription {}
- class DescribableTable a where
- describeTable :: Proxy a -> TableDescription
- describeTableName :: Proxy a -> String
- describeTableIdentifier :: Proxy a -> String
- data Query = Query {
- queryStatement :: !ByteString
- queryParams :: ![Value]
- pgsq :: QuasiQuoter
Tables
data TableDescription Source
Description of a table type
TableDescription | |
|
class DescribableTable a where Source
Attach meta data to a table type
Nothing
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
Query including statement and parameters.
Use the pgsq
quasi-quoter to conveniently create queries.
Query | |
|
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(
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 Table
a) => Reference
a&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