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.
This commit is contained in:
Tom Willemsen 2012-12-28 01:00:26 +01:00
parent 0c24993384
commit 908bbc3cfb

View file

@ -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))))))