Skip to content

Commit 08d2aac

Browse files
committed
Document new arities
1 parent 402fb2b commit 08d2aac

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,35 @@ The connection pool is closed with `close-db!`. This closes all open connections
3333

3434
## Running SQL queries
3535

36-
Queries are executed with callback-based functions `query!` `insert!` `update!` `execute!` and [`core.async`](https://github.com/clojure/core.async) channel-based functions `<query!` `<insert!` `<update!` `<execute!`.
36+
Queries are executed with functions `query!` `insert!` `update!` `execute!`. All functions have two arities that use either callbacks or a[`core.async`](https://github.com/clojure/core.async) channels.
3737

38-
Channel-based functions return a channel where query result or exception is put.
38+
Channel-based functions return a channel where either query result or exception is put.
3939

4040
### execute! and query!
4141

42-
All other query functions delegate to `execute!`. This takes a db, a vector of sql string and parameters plus a callback with arity of two.
42+
All other query functions delegate to `execute!`. This takes a db and a seq of sql followed by optional parameters.
4343

4444
```clojure
45-
;; callback
46-
(execute! db ["select $1::text" "hello world"] (fn [rs err]
47-
(println rs err))
48-
; nil
49-
5045
;; async channel
51-
(<!! (<execute! db ["select name, price from products where id = $1" 1001]))
46+
(<!! (execute! db ["select name, price from products where id = $1" 1001]))
5247
; {:updated 0, :rows [{:id 1001, :name "hammer", :price 10}]}
5348

54-
(<!! (<execute! db ["select * from foobar"]))
49+
(<!! (execute! db ["select * from foobar"]))
5550
; #<SqlException com.github.pgasync.SqlException: ERROR: SQLSTATE=42P01, MESSAGE=relation "foobar" does not exist>
51+
52+
;; callback-based higher arity
53+
(execute! db ["select $1::text" "hello world"] (fn [rs err]
54+
(println rs err))
55+
; nil
5656
```
5757

5858
`query!` passes only `:rows` to callback.
5959

6060
```clojure
61-
(<!! (<query! db ["select name, price from products"]))
61+
(<!! (query! db ["select name, price from products"]))
6262
; [{:id 1000, :name "screwdriver", :price 15} {:id 1001, :name "hammer", :price 10}]
6363

64-
(<!! (<query! db ["select name, price from products where id = $1" 1001]))
64+
(<!! (query! db ["select name, price from products where id = $1" 1001]))
6565
; [{:id 1001, :name "hammer", :price 10}]
6666
```
6767

@@ -70,17 +70,17 @@ All other query functions delegate to `execute!`. This takes a db, a vector of s
7070
Insert is executed with an sql-spec that supports keys `:table` and `:returning`.
7171

7272
```clojure
73-
(<!! (<insert! db {:table "products"} {:name "screwdriver" :price 15}))
73+
(<!! (insert! db {:table "products"} {:name "screwdriver" :price 15}))
7474
; {:updated 1, :rows []}
7575

76-
(<!! (<insert! db {:table "products" :returning "id"} {:name "hammer" :price 5}))
76+
(<!! (insert! db {:table "products" :returning "id"} {:name "hammer" :price 5}))
7777
; {:updated 1, :rows [{:id 1001}]}
7878
```
7979

8080
Multiple rows can be inserted by passing a sequence to `insert!`.
8181

8282
```clojure
83-
(<!! (<insert! db {:table "products" :returning "id"}
83+
(<!! (insert! db {:table "products" :returning "id"}
8484
[{:name "hammer" :price 5}
8585
{:name "nail" :price 1}]))
8686
; {:updated 2, :rows [{:id 1001} {:id 1002}]}
@@ -91,7 +91,7 @@ Multiple rows can be inserted by passing a sequence to `insert!`.
9191
Update is executed with an sql-spec that supports keys `:table` `:returning` and `:where`.
9292

9393
```clojure
94-
(<!! (<update! db {:table "users" :where ["id = $1" 1001}} {:price 6}))
94+
(<!! (update! db {:table "users" :where ["id = $1" 1001}} {:price 6}))
9595
; {:updated 1, :rows []}
9696
```
9797

@@ -107,11 +107,11 @@ Channel-returning functions can be composed with `dosql` macro that returns `[re
107107

108108
```clojure
109109
(<!! (go
110-
(dosql [tx (<begin! db)
111-
rs (<insert! tx {:table products :returning "id"} {:name "saw"})
112-
_ (<insert! tx {:table promotions} {:product_id (get-in rs [:rows 0 :id])})
113-
rs (<query! tx ["select * from promotions"])
114-
_ (<commit! tx)]
110+
(dosql [tx (begin! db)
111+
rs (insert! tx {:table products :returning "id"} {:name "saw"})
112+
_ (insert! tx {:table promotions} {:product_id (get-in rs [:rows 0 :id])})
113+
rs (query! tx ["select * from promotions"])
114+
_ (commit! tx)]
115115
{:now-promoting rs})))
116116
; {:now-promoting [{:id 1, product_id 1002}]}
117117
```

0 commit comments

Comments
 (0)