aboutsummaryrefslogtreecommitdiff
path: root/dump
diff options
context:
space:
mode:
authorArun Isaac2021-08-27 05:49:11 -0500
committerArun Isaac2021-08-27 05:49:11 -0500
commitb6cb5c4d2c64781122223d4ee100a0d6f3dd03b9 (patch)
tree0b53ac191d07c4599d2b58c0d2a96561bcb23cf1 /dump
downloadgn-transform-databases-b6cb5c4d2c64781122223d4ee100a0d6f3dd03b9.tar.gz
Initial commit
Diffstat (limited to 'dump')
-rw-r--r--dump/sql.scm54
1 files changed, 54 insertions, 0 deletions
diff --git a/dump/sql.scm b/dump/sql.scm
new file mode 100644
index 0000000..576d6cd
--- /dev/null
+++ b/dump/sql.scm
@@ -0,0 +1,54 @@
+;;; Database helpers
+;;;
+;;; These functions should have been a part of guile-dbi. Never too
+;;; late to contribute upstream!
+
+(define-module (dump sql)
+ #:use-module (srfi srfi-26)
+ #:use-module (ice-9 match)
+ #:use-module (dbi dbi)
+ #:export (call-with-database
+ sql-exec
+ sql-fold
+ sql-map
+ sql-for-each
+ sql-find))
+
+(define (call-with-database backend connection-string proc)
+ (let ((db #f))
+ (dynamic-wind (lambda ()
+ (set! db (dbi-open backend connection-string)))
+ (cut proc db)
+ (cut dbi-close db))))
+
+(define (database-check-status db)
+ (match (dbi-get_status db)
+ ((code . str)
+ (unless (zero? code)
+ (error str)))))
+
+(define (sql-exec db statement)
+ (dbi-query db statement)
+ (database-check-status db))
+
+(define (sql-fold proc init db statement)
+ (sql-exec db statement)
+ (let loop ((result init))
+ (let ((row (dbi-get_row db)))
+ (if row
+ (loop (proc row result))
+ result))))
+
+(define (sql-map proc db statement)
+ (sql-fold (lambda (row result)
+ (cons (proc row) result))
+ (list) db statement))
+
+(define (sql-for-each proc db statement)
+ (sql-fold (lambda (row _)
+ (proc row))
+ #f db statement))
+
+(define (sql-find db statement)
+ (sql-exec db statement)
+ (dbi-get_row db))