-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepared_statement.nim
More file actions
53 lines (42 loc) · 1.48 KB
/
prepared_statement.nim
File metadata and controls
53 lines (42 loc) · 1.48 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
## Prepared statement example.
##
## Demonstrates creating a server-side prepared statement, executing it
## multiple times with different parameters, and closing it.
##
## Usage:
## nim c -r examples/prepared_statement.nim
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()
# Create a temporary table
discard await conn.exec(
"""
CREATE TEMP TABLE products (
id serial PRIMARY KEY,
name text NOT NULL,
price int4 NOT NULL
)
"""
)
# Prepare a named statement for inserting rows
let insertStmt = await conn.prepare(
"insert_product", "INSERT INTO products (name, price) VALUES ($1, $2)"
)
# Execute the prepared statement multiple times with different parameters
discard await insertStmt.execute(@[toPgParam("Apple"), toPgParam(120'i32)])
discard await insertStmt.execute(@[toPgParam("Banana"), toPgParam(200'i32)])
discard await insertStmt.execute(@[toPgParam("Cherry"), toPgParam(350'i32)])
await insertStmt.close()
# Prepare a query statement with a parameter
let selectStmt = await conn.prepare(
"select_by_price", "SELECT name, price FROM products WHERE price >= $1"
)
let res = await selectStmt.execute(@[toPgParam(150'i32)])
echo "Products with price >= 150:"
for row in res.rows:
echo " ", row.getStr("name"), ": ", row.getInt("price")
await selectStmt.close()
waitFor main()