From 908bbc3cfba656de895f4803d6b040f917594ff9 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Fri, 28 Dec 2012 01:00:26 +0100 Subject: [PATCH] Use a transaction when adding new bookmarks During testing it was discovered that the `first-result' procedure throws an exception, which left the database in a somewhat inconsistent state, the bookmark had been added, but (at least one of) the tags weren't added. This should make it all succeed or all fail. --- linkwave.scm | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/linkwave.scm b/linkwave.scm index f36c5e2..235615b 100644 --- a/linkwave.scm +++ b/linkwave.scm @@ -26,7 +26,13 @@ (set! tag-id (last-insert-rowid db))) (execute db "INSERT INTO bookmark_tag VALUES (?, ?)" bookmark-id tag-id))) - tags))) + tags)) + #t) + +(define (url-string? str) + (and (> (string-length str) 7) + (or (string= (substring str 0 7) "http://") + (string= (substring str 0 8) "https://")))) (define db (open-database (data-file "linkwave.db"))) @@ -34,8 +40,9 @@ (if (null? cla) (for-each-row print-row db "select * from bookmark") (cond - ((and (>= (string-length (car cla)) 7) (string= (substring (car cla) 0 7) "http://")) - (add-bookmark (car cla) (cadr cla) (caddr cla) (cdddr cla))) + ((url-string? (car cla)) + (with-transaction + db (lambda () (add-bookmark (car cla) (cadr cla) (caddr cla) (cdddr cla))))) (else (format #t "Unrecognized option: ~a~%" (car cla))))))