From 57f06ec8c977d9011b2692f20c7d5967c7d2a0cc Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Fri, 28 Dec 2012 01:46:15 +0100 Subject: [PATCH] Remove db as a global variable There's no need for it to be a global variable. --- linkwave.scm | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/linkwave.scm b/linkwave.scm index 2107f44..85e2d20 100644 --- a/linkwave.scm +++ b/linkwave.scm @@ -15,11 +15,11 @@ (format #t "~a~% ~a~% ~a~% ~a~%~%" (string-no-null name) (string-no-null description) (string-no-null url) (seconds->string seconds))) -(define (add-tag name) +(define (add-tag db name) (execute db "INSERT INTO tag VALUES (?)" name) (last-insert-rowid db)) -(define (add-bookmark url name description tags) +(define (add-bookmark db url name description tags) (execute db "INSERT INTO bookmark VALUES (?, STRFTIME('%s'), ?, ?)" url name description) (let ((bookmark-id (last-insert-rowid db))) @@ -29,7 +29,7 @@ (set! tag-id (first-result db "SELECT rowid FROM tag WHERE name = ?" tag)) (exn (exn sqlite3) (if (eq? (get-condition-property exn 'sqlite3 'status) 'done) - (set! tag-id (add-tag tag)) + (set! tag-id (add-tag db tag)) (signal exn)))) (execute db "INSERT INTO bookmark_tag VALUES (?, ?)" bookmark-id tag-id))) @@ -41,17 +41,16 @@ (string= (substring str 0 8) "https://")))) (define (main args) - (if (null? args) - (for-each-row print-row db "select * from bookmark") - (cond - ((url-string? (car args)) - (with-transaction - db (lambda () (add-bookmark (car args) (cadr args) (caddr args) (cdddr args)) #t))) - (else - (format #t "Unrecognized option: ~a~%" (car args)))))) + (let ((db (open-database (data-file "linkwave.db")))) + (if (null? args) + (for-each-row print-row db "select * from bookmark") + (cond + ((url-string? (car args)) + (with-transaction + db (lambda () (add-bookmark db (car args) (cadr args) (caddr args) (cdddr args)) #t))) + (else + (format #t "Unrecognized option: ~a~%" (car args))))) -(define db (open-database (data-file "linkwave.db"))) + (finalize! db #t))) (main (command-line-arguments)) - -(finalize! db #t)