@@ -27,24 +27,26 @@ The plan is to support all the other SQL commands, but that's coming later. In t
2727Connecting and performing a query
2828
2929``` haskell
30- task : Task (List { name: Str , price: Dec }) _
31- task =
32- client <- Pg.Client. withConnect {
33- host: " localhost" ,
34- port: 5432 ,
35- user: " postgres" ,
36- database: " postgres" ,
37- auth: Password " password"
38- }
39-
40- Pg.Cmd. new " select name, price from products"
41- |> Pg.Cmd. expectN (
42- Pg.Result. succeed {
43- name: <- Pg.Result. str " name" |> Pg.Result. apply,
44- price: <- Pg.Result. dec " price" |> Pg.Result. apply
45- }
46- )
47- |> Pg.Client. command client
30+ products! : {} => Result (List { name : Str , price : Dec }) _
31+ products! = | _|
32+ client = Pg.Client. connect! (
33+ {
34+ host: " localhost" ,
35+ port: 5432 ,
36+ user: " postgres" ,
37+ auth: None ,
38+ database: " postgres" ,
39+ },
40+ )?
41+
42+ Pg.Cmd. new(" select name, price from products" )
43+ |> Pg.Cmd. expect_n(
44+ { Pg.Result. record_builder <-
45+ name: Pg.Result. str(" name" ),
46+ price: Pg.Result. dec(" price" ),
47+ },
48+ )
49+ |> Pg.Client. command! (client)
4850```
4951
5052<details >
@@ -53,15 +55,15 @@ Parameterized queries
5355</summary >
5456
5557``` elm
56- Pg . Cmd . new " select name, price from products where id = $1"
57- |> Pg . Cmd . bind [ Pg . Cmd . u32 productId ]
58- |> Pg . Cmd . expect1 (
59- Pg . Result . succeed {
60- name: <- Pg . Result . str " name" |> Pg . Result . apply ,
61- price: <- Pg . Result . dec " price" |> Pg . Result . apply
62- }
63- )
64- |> Pg . Client . command client
58+ Pg . Cmd . new( " select name, price from products where id = $1" )
59+ |> Pg . Cmd . bind( [ Pg . Cmd . u32( product_id ) ] )
60+ |> Pg . Cmd . expect1(
61+ { Pg . Result . record_builder <-
62+ name: Pg . Result . str( " name" ) ,
63+ price: Pg . Result . dec( " price" ) ,
64+ },
65+ )
66+ |> Pg . Client . command! ( client) ?
6567```
6668
6769</details >
@@ -72,16 +74,14 @@ Prepared statements
7274</summary >
7375
7476``` elm
75- selectUser <-
77+ select_user =
7678 " select email from users where id = $1"
77- |> Pg . Client . prepare { client, name: " selectUser" }
78- |> await
79-
80- selectUser
81- |> Pg . Cmd . bind [ Pg . Cmd . u32 userId ]
82- |> Pg . Cmd . expect1 ( Pg . Result . str " email" )
83- |> Pg . Client . command client
79+ |> Pg . Client . prepare! ( { client, name: " select_user" } ) ?
8480
81+ select_user
82+ |> Pg . Cmd . bind( [ Pg . Cmd . u32( user_id) ] )
83+ |> Pg . Cmd . expect1( Pg . Result . str( " email" ))
84+ |> Pg . Client . command! ( client) ?
8585```
8686
8787</details >
@@ -92,33 +92,36 @@ Batch commands in a single roundtrip (applicative)
9292</summary >
9393
9494``` elm
95- Pg . Batch . succeed \ email -> \ products -> { email, products }
96- |> Pg . Batch . with
95+ Pg . Batch . succeed( | email| | products| { email, products } )
96+ |> Pg . Batch . with(
9797 (
98- selectUser
99- |> Pg . Cmd . bind [ Pg . Cmd . u32 userId ]
100- |> Pg . Cmd . expect1 ( Pg . Result . str " email" )
101- )
102- |> Pg . Batch . with
98+ select_user
99+ |> Pg . Cmd . bind( [ Pg . Cmd . u32( user_id) ] )
100+ |> Pg . Cmd . expect1( Pg . Result . str( " email" ))
101+ ) ,
102+ )
103+ |> Pg . Batch . with(
103104 (
104- Pg . Cmd . new
105+ Pg . Cmd . new(
105106 """
106107 select name, price from products
107108 inner join orders on orders.product_id = products.id
108109 where orders.id = $1
109- """
110- |> Pg . Cmd . bind [ Pg . Cmd . u32 orderId ]
111- |> Pg . Cmd . expectN (
112- Pg . Result . succeed {
113- name: <- Pg . Result . str " name" |> Pg . Result . apply,
114- price: <- Pg . Result . dec " price" |> Pg . Result . apply
115- }
116- )
117- )
118- |> Pg . Client . batch client
110+ """ ,
111+ )
112+ |> Pg . Cmd . bind( [ Pg . Cmd . u32( order_id) ] )
113+ |> Pg . Cmd . expect_n(
114+ { Pg . Result . record_builder <-
115+ name: Pg . Result . str( " name" ) ,
116+ price: Pg . Result . dec( " price" ) ,
117+ },
118+ )
119+ ) ,
120+ )
121+ |> Pg . Client . batch! ( client) ?
119122```
120123
121- Note: ` selectUser ` referes to prepared statement in the previous example
124+ Note: ` select_user ` referes to prepared statement in the previous example
122125
123126</details >
124127
@@ -128,14 +131,14 @@ Batch commands in a single roundtrip (list)
128131</summary >
129132
130133``` elm
131- updateCmd = \ product ->
132- Pg . Cmd . new " update products set desc = $1 where id = $2"
133- |> Pg . Cmd . bind [ Pg . Cmd . str product. desc, Pg . Cmd . u32 product. id ]
134+ update_cmd = | product|
135+ Pg . Cmd . new( " update products set desc = $1 where id = $2" )
136+ |> Pg . Cmd . bind( [ Pg . Cmd . str( product. desc) , Pg . Cmd . u32( product. id) ] )
134137
135- productsToUpdate
136- |> List . map updateCmd
138+ products_to_update
139+ |> List . map( update_cmd )
137140|> Pg . Batch . sequence
138- |> Pg . Client . batch client
141+ |> Pg . Client . batch! ( client) ?
139142```
140143
141144Note: ` roc-pg ` automatically reuses statements in a batch by only parsing (and describing) once per unique SQL string. This also works with applicative batches.
0 commit comments