diff options
| author | 2012-12-24 01:27:14 +0100 | |
|---|---|---|
| committer | 2012-12-24 01:27:14 +0100 | |
| commit | 77c7dbaa9ae845b0795f4da33eb0d285f6b9d1c4 (patch) | |
| tree | 0168de30ce7b4107ed13fc57974f95850ea88e73 | |
| download | markam-77c7dbaa9ae845b0795f4da33eb0d285f6b9d1c4.tar.gz markam-77c7dbaa9ae845b0795f4da33eb0d285f6b9d1c4.zip | |
Initial commit
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | convert.scm | 49 | ||||
| -rw-r--r-- | linkwave.scm | 17 | ||||
| -rw-r--r-- | paths.scm | 17 |
4 files changed, 94 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c0a9ee8 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.PHONY: all + +all: linkwave convert +linkwave: linkwave.scm paths.o + csc $^ -o $@ + +convert: convert.scm paths.o + csc $^ -o $@ + +paths.o: paths.scm + csc -c $^ diff --git a/convert.scm b/convert.scm new file mode 100644 index 0000000..3e905b5 --- /dev/null +++ b/convert.scm @@ -0,0 +1,49 @@ +;;; convert.scm -- Convert the old database into a new one +(declare (uses paths)) + +(require-extension sqlite3) +(require-library srfi-4) + +(define (blob->seconds blob) + (u32vector-ref (blob->u32vector blob) 0)) + +(define (convert-table db name converter #!optional select) + (let ((count (first-result db (string-append "SELECT COUNT(*) FROM " name))) + (progress 0)) + (for-each-row (lambda args + (apply converter args) + (set! progress (+ progress 1)) + (format #t "Converted ~s of ~s ~a rows\r~!" progress count name)) + db (or select (string-append "SELECT * FROM " name))) + (newline))) + +(define (convert) + (let ((old-db (open-database (data-file "linkwave.db"))) + (new-db (open-database (data-file "nlinkwave.db")))) + (execute new-db "CREATE TABLE bookmark (url VARCHAR(255) UNIQUE, date INTEGER, name VARCHAR(255), description TEXT)") + (execute new-db "CREATE TABLE tag (name VARCHAR(255) UNIQUE)") + (execute new-db "CREATE TABLE bookmark_tag (bookmark_id INTEGER REFERENCES bookmark(rowid), tag_id INTEGER REFERENCES tag(rowid), PRIMARY KEY (bookmark_id, tag_id))") + + (convert-table old-db "bookmark" + (lambda (url dateblob name description) + (execute new-db "INSERT INTO bookmark VALUES (?, ?, ?, ?)" + url (blob->seconds dateblob) name description))) + + (convert-table old-db "tag" + (lambda (name) + (execute new-db "INSERT INTO tag VALUES (?)" name))) + + (convert-table old-db "bookmark_tag" + (lambda (url tag) + (execute new-db "INSERT INTO bookmark_tag VALUES (?, ?)" + (first-result new-db "SELECT rowid FROM bookmark WHERE url = ?" url) + (first-result new-db "SELECT rowid FROM tag WHERE name = ?" tag))) + "SELECT url, name FROM bookmark_tag JOIN tag ON (tag_id = tag.rowid)") + + (rename-file (data-file "linkwave.db") (data-file "old-linkwave.db")) + (rename-file (data-file "nlinkwave.db") (data-file "linkwave.db")) + (format #t "Database converted.~%"))) + +(if (file-exists? "/home/slash/.local/share/linkwave/nlinkwave.db") + (format #t "Converted database already exists.~%") + (convert)) diff --git a/linkwave.scm b/linkwave.scm new file mode 100644 index 0000000..7e26a05 --- /dev/null +++ b/linkwave.scm @@ -0,0 +1,17 @@ +(declare (uses paths)) + +(require-extension sqlite3) +(require-library posix srfi-4) + +(: string-no-null (string -> string)) +(define (string-no-null str) + (substring str 0 (- (string-length str) 1))) + +(: 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))) + +(define db (open-database (data-file "linkwave.db"))) +(for-each-row print-row db "select * from bookmark") +(finalize! db #t) diff --git a/paths.scm b/paths.scm new file mode 100644 index 0000000..50bd622 --- /dev/null +++ b/paths.scm @@ -0,0 +1,17 @@ +;;; paths.scm -- Functions which work with/on paths +(declare (unit paths)) + +(define (stored-file xdg-env file) + (let ((xdg (get-environment-variable xdg-env)) + (stored-file (string-append (get-environment-variable "HOME") "/.linkwave/" file))) + (unless (or (file-exists? stored-file) (not xdg)) + (set! stored-file (string-append xdg "/linkwave/" file))) + stored-file)) + +(: config-file (string -> string)) +(define (config-file file) + (stored-file "XDG_CONFIG_HOME" file)) + +(: data-file (string -> string)) +(define (data-file file) + (stored-file "XDG_DATA_HOME" file)) |
