Copyright | 2016 Dylan Simon |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Database.PostgreSQL.Typed.Relation
Description
Automatically create data types based on tables and other relations.
Documentation
Arguments
:: String | Haskell type and constructor to create |
-> PGName | PostgreSQL table/relation name |
-> (String -> String) | How to generate field names from column names, e.g. |
-> DecsQ |
Create a new data type corresponding to the given PostgreSQL relation.
For example, if you have CREATE TABLE foo (abc integer NOT NULL, def text)
, then
dataPGRelation "Foo" "foo" ("foo_"++)
will be equivalent to:
data Foo = Foo{ foo_abc :: PGVal "integer", foo_def :: Maybe (PGVal "text") } instance PGType "foo" where PGVal "foo" = Foo instance PGParameter "foo" Foo where ... instance PGColumn "foo" Foo where ... instance PGColumn "foo" (Maybe Foo) where ... -- to handle NULL in not null columns instance PGRep Foo where PGRepType = "foo" instance PGRecordType "foo" instance PGRelation Foo where pgColumnNames _ = ["abc", "def"] uncurryFoo :: (PGVal "integer", Maybe (PGVal "text")) -> Foo
(Note that PGVal "integer" = Int32
and PGVal "text" = Text
by default.)
This provides instances for marshalling the corresponding composite/record types, e.g., using SELECT foo.*::foo FROM foo
.
If you want any derived instances, you'll need to create them yourself using StandaloneDeriving.
Requires language extensions: TemplateHaskell, FlexibleInstances, MultiParamTypeClasses, DataKinds, TypeFamilies, PatternGuards