@@ -6,6 +6,66 @@ Maintainer: eitan@morphism.tech
66Stability: experimental
77
88Binary encoding and decoding between Haskell and PostgreSQL types.
9+
10+Instances are governed by the `Generic` and `HasDatatype` typeclasses.
11+
12+>>> import Control.Monad
13+>>> import Control.Monad.Base
14+
15+>>> import Squeal.PostgreSQL
16+
17+>>> data Schwarma = Beef | Lamb | Chicken deriving (Show, GHC.Generic)
18+>>> instance Generic Schwarma
19+>>> instance HasDatatypeInfo Schwarma
20+
21+>>> let q = values_ (label @"Beef" `As` #fromOnly :* Nil) :: Query '[] '[] '["fromOnly" ::: 'NotNull (EnumWith Schwarma)]
22+
23+>>> :{
24+void . withConnection "host=localhost port=5432 dbname=exampledb" $ do
25+ result <- runQuery q
26+ Just (Only schwarma) <- firstRow result
27+ liftBase $ print (schwarma :: Schwarma)
28+:}
29+Beef
30+
31+>>> :{
32+type family Schema :: SchemaType where
33+ Schema =
34+ '[ "schwarma" ::: 'Typedef (EnumWith Schwarma)
35+ , "tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull (EnumWith Schwarma)])
36+ ]
37+:}
38+
39+>>> :{
40+let
41+ setup =
42+ createTypeEnumWith @Schwarma #schwarma >>>
43+ createTable #tab (#schwarma `As` #col :* Nil) Nil
44+ :: Definition '[] Schema
45+ teardown =
46+ dropTable #tab >>>
47+ dropType #schwarma
48+ :: Definition Schema '[]
49+ manip =
50+ insertRow_ #tab (Set (param @1) `As` #col :* Nil)
51+ :: Manipulation Schema '[ 'NotNull (EnumWith Schwarma)] '[]
52+ qry =
53+ select (#col `As` #fromOnly :* Nil) (from (table #tab))
54+ :: Query Schema '[] '["fromOnly" ::: 'NotNull (EnumWith Schwarma)]
55+ session = do
56+ manipulateParams manip (Only Chicken)
57+ result <- runQuery qry
58+ Just (Only schwarma) <- firstRow result
59+ liftBase $ print (schwarma :: Schwarma)
60+:}
61+
62+>>> :{
63+void . withConnection "host=localhost port=5432 dbname=exampledb" $
64+ define setup
65+ & pqThen session
66+ & pqThen (define teardown)
67+:}
68+Chicken
969-}
1070
1171{-# LANGUAGE