-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipeline.nim
More file actions
56 lines (47 loc) · 1.55 KB
/
pipeline.nim
File metadata and controls
56 lines (47 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Pipeline example.
##
## Demonstrates batching multiple queries and commands into a single
## round trip using the pipeline API for improved performance.
##
## Usage:
## nim c -r examples/pipeline.nim
import std/strutils
import pkg/async_postgres
const Dsn = "postgresql://test:test@127.0.0.1:15432/test?sslmode=disable"
proc main() {.async.} =
let conn = await connect(Dsn)
defer:
await conn.close()
discard await conn.exec(
"""
CREATE TEMP TABLE tasks (
id serial PRIMARY KEY,
title text NOT NULL,
done bool NOT NULL DEFAULT false
)
"""
)
# Build a pipeline: multiple operations sent in a single round trip
let p = conn.newPipeline()
p.addExec("INSERT INTO tasks (title) VALUES ($1)", @[toPgParam("Write docs")])
p.addExec("INSERT INTO tasks (title) VALUES ($1)", @[toPgParam("Fix bug")])
p.addExec(
"INSERT INTO tasks (title, done) VALUES ($1, $2)",
@[toPgParam("Ship v1"), toPgParam(true)],
)
p.addQuery("SELECT id, title, done FROM tasks ORDER BY id")
p.addQuery("SELECT count(*) FROM tasks WHERE done = true")
# Execute all operations at once
let results = await p.execute()
for i, r in results:
case r.kind
of prkExec:
echo "Operation ", i, ": ", r.commandResult.commandTag
of prkQuery:
echo "Operation ", i, " (", r.queryResult.rowCount, " rows):"
for row in r.queryResult.rows:
var cols: seq[string]
for field in r.queryResult.fields:
cols.add(field.name & "=" & row.getStr(field.name))
echo " ", cols.join(", ")
waitFor main()