-
Notifications
You must be signed in to change notification settings - Fork 4
/
kvs-postgres.ss
35 lines (31 loc) · 1.18 KB
/
kvs-postgres.ss
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
;;;; Key Value Store Interface for Postgres
(import
:std/db/dbi
:std/db/postgresql
:std/db/postgresql-driver
:std/iter
:std/misc/path
:std/sugar
:clan/path-config
:clan/persist/kvs
:clan/persist/kvs-sql)
(defstruct (KvsPostgres KvsSql)
(begin-tx-stmt commit-tx-stmt abort-tx-stmt
read-stmt write-stmt delete-stmt)
constructor: :init!)
(defmethod {:init! KvsPostgres}
(lambda (self . args)
(def connection (apply sql-connect args))
(sql-eval connection (string-append
"CREATE TABLE IF NOT EXISTS kvs ( "
"key BLOB, "
"value BLOB NOT NULL, "
"PRIMARY KEY (key)) ;"))
(struct-instance-init!
self connection
(sql-prepare connection "BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE")
(sql-prepare connection "COMMIT TRANSACTION")
(sql-prepare connection "ROLLBACK TRANSACTION")
(sql-prepare connection "SELECT value FROM kvs WHERE key = ?")
(sql-prepare connection "INSERT INTO kvs (key, value) VALUES (?, ?) ON CONFLICT DO UPDATE SET value = excluded.value")
(sql-prepare connection "DELETE FROM kvs WHERE key = ?"))))