-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add postgres support.
- Loading branch information
Showing
10 changed files
with
112 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
manifest.ss | ||
version.ss | ||
build-deps | ||
run/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 = ?")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
;;;; Key Value Store for SQL in general (to be specialized by SQLite, PostgreSQL, etc.) | ||
|
||
(import | ||
:std/db/dbi | ||
:std/iter | ||
:std/misc/path | ||
:std/sugar | ||
:clan/path-config | ||
:clan/persist/kvs) | ||
|
||
(export #t) | ||
|
||
(defstruct (KvsSql Kvs) | ||
(begin-tx-stmt commit-tx-stmt abort-tx-stmt | ||
read-stmt write-stmt delete-stmt) | ||
constructor: :init!) | ||
|
||
(defmethod {:init! KvsSql} | ||
(lambda (self connection begin-tx-stmt commit-tx-stmt abort-tx-stmt read-stmt write-stmt delete-stmt) | ||
(struct-instance-init! self connection begin-tx-stmt commit-tx-stmt abort-tx-stmt read-stmt write-stmt delete-stmt))) | ||
|
||
(defmethod {begin-transaction KvsSql} (lambda (self) (sql-exec (KvsSql-begin-tx-stmt self)))) | ||
(defmethod {abort-transaction KvsSql} (lambda (self) (sql-exec (KvsSql-abort-tx-stmt self)))) | ||
(defmethod {commit-transaction KvsSql} (lambda (self) (sql-exec (KvsSql-commit-tx-stmt self)))) | ||
|
||
(defrule (with-statement (var stmt args ...) body ...) | ||
(let ((var stmt)) | ||
(try {bind var args ...} body ... | ||
(finally (sql-reset/clear stmt))))) | ||
|
||
(defmethod {read-key KvsSql} | ||
(lambda (K key) | ||
(with-statement (s (KvsSql-read-stmt K) key) | ||
(match {query-fetch s} | ||
((eq? #!void) (values {query-row s} #t)) | ||
((eq? iter-end) (values #f #f)))))) | ||
|
||
(defmethod {write-key KvsSql} | ||
(lambda (K k v) | ||
(with-statement (s (KvsSql-write-stmt K) k v) | ||
{exec s}))) | ||
|
||
(defmethod {delete-key KvsSql} | ||
(lambda (K k) | ||
(with-statement (s (KvsSql-delete-stmt K) k) | ||
{exec s}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters