2012-12-24 01:27:14 +01:00
|
|
|
(declare (uses paths))
|
|
|
|
|
|
|
|
(require-extension sqlite3)
|
|
|
|
(require-library posix srfi-4)
|
|
|
|
|
|
|
|
(: string-no-null (string -> string))
|
|
|
|
(define (string-no-null str)
|
2012-12-27 03:08:55 +01:00
|
|
|
(if (and (> (string-length str) 0)
|
|
|
|
(char=? (string-ref str (- (string-length str) 1)) #\nul))
|
|
|
|
(substring str 0 (- (string-length str) 1))
|
|
|
|
str))
|
2012-12-24 01:27:14 +01:00
|
|
|
|
|
|
|
(: print-row (string fixnum string string -> void))
|
|
|
|
(define (print-row url seconds name description)
|
|
|
|
(format #t "~a~% ~a~% ~a~% ~a~%~%" (string-no-null name) (string-no-null description)
|
|
|
|
(string-no-null url) (seconds->string seconds)))
|
|
|
|
|
2012-12-27 03:08:55 +01:00
|
|
|
(define (add-bookmark url name description tags)
|
|
|
|
(execute db "INSERT INTO bookmark VALUES (?, STRFTIME('%s'), ?, ?)"
|
|
|
|
url name description)
|
|
|
|
(let ((bookmark-id (last-insert-rowid db)))
|
|
|
|
(for-each (lambda (tag)
|
|
|
|
(let ((tag-id (first-result db "SELECT rowid FROM tag WHERE name = ?" tag)))
|
|
|
|
(unless tag-id
|
|
|
|
(execute db "INSERT INTO tag VALUES (?)" tag)
|
|
|
|
(set! tag-id (last-insert-rowid db)))
|
|
|
|
(execute db "INSERT INTO bookmark_tag VALUES (?, ?)"
|
|
|
|
bookmark-id tag-id)))
|
2012-12-28 01:00:26 +01:00
|
|
|
tags))
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(define (url-string? str)
|
|
|
|
(and (> (string-length str) 7)
|
|
|
|
(or (string= (substring str 0 7) "http://")
|
|
|
|
(string= (substring str 0 8) "https://"))))
|
2012-12-27 03:08:55 +01:00
|
|
|
|
2012-12-24 01:27:14 +01:00
|
|
|
(define db (open-database (data-file "linkwave.db")))
|
2012-12-27 03:08:55 +01:00
|
|
|
|
|
|
|
(let ((cla (command-line-arguments)))
|
|
|
|
(if (null? cla)
|
|
|
|
(for-each-row print-row db "select * from bookmark")
|
|
|
|
(cond
|
2012-12-28 01:00:26 +01:00
|
|
|
((url-string? (car cla))
|
|
|
|
(with-transaction
|
|
|
|
db (lambda () (add-bookmark (car cla) (cadr cla) (caddr cla) (cdddr cla)))))
|
2012-12-27 03:08:55 +01:00
|
|
|
(else
|
|
|
|
(format #t "Unrecognized option: ~a~%" (car cla))))))
|
|
|
|
|
2012-12-24 01:27:14 +01:00
|
|
|
(finalize! db #t)
|