From 3820e6599232f84aec978fa99cc814028e2df65e Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sat, 29 Dec 2012 17:10:12 +0100 Subject: Rename convert to markam-convert --- src/.gitignore | 2 +- src/Makefile | 4 +- src/convert.scm | 112 ------------------------------------------------- src/markam-convert.scm | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 115 deletions(-) delete mode 100644 src/convert.scm create mode 100644 src/markam-convert.scm diff --git a/src/.gitignore b/src/.gitignore index 83dc013..634a7be 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,3 +1,3 @@ -convert +markam-convert markam *.o diff --git a/src/Makefile b/src/Makefile index 95baea2..d3882e3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,11 +2,11 @@ objects = paths.o common.o .PHONY: all -all: markam convert +all: markam markam-convert markam: markam.scm $(objects) csc $^ -o $@ -convert: convert.scm $(objects) +markam-convert: markam-convert.scm $(objects) csc $^ -o $@ $(objects): %.o: %.scm diff --git a/src/convert.scm b/src/convert.scm deleted file mode 100644 index 199a2a5..0000000 --- a/src/convert.scm +++ /dev/null @@ -1,112 +0,0 @@ -;;; markam -- Store/retrieve/manage bookmarks -;; Copyright (C) 2012 Tom Willemsen - -;; This program is free software: you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation, either version 3 of the License, or -;; (at your option) any later version. - -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . - -;;; Commentary: - -;; Convert any old database file to one usable with this version of -;; markam. If you're just starting to use markam there should be -;; no need to use this. - -;;; Code: - -(declare (uses paths - common)) - -(require-extension sqlite3) -(require-library srfi-4) - -(define (blob->seconds blob) - ;; Convert BLOB to an integer representing the seconds since - ;; 01-01-1970 (standard unix timstamp). This has to be done because - ;; in the previous version of markam I stored the timestamp as a - ;; blob and CHICKEN doesn't seem to like blobs much. - (if (u32vector? blob) - (u32vector-ref (blob->u32vector blob) 0) - 0)) - -(define (convert-table db name converter #!optional select) - ;; Convert NAME from DB using CONVERTER for each specific row. The - ;; optional SELECT should be an SQL query that will be used to fetch - ;; the rows to convert. - (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) - ;; Convert an old database into a new one and then replace the old - ;; with the new. - (let ((old-db (open-database (data-file "linkwave.db" "linkwave"))) - (new-db (open-database (data-file "markam.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)") - - (format #t "Database converted.~%"))) - -(define (display-help) - ;; Display markam's help message. - (format #t (string-append - "Usage: convert [options]...~%" - "~%" - "Possible options:~%" - "~%" - "--help, -h Display this help and exit~%" - "--version, -v Output version information and exit~%"))) - -(define (main args) - ;; Convert an old database file unless a conversion database already - ;; exists. - (if (null? args) - (if (file-exists? (data-file "markam.db")) - (format #t (string-append "Converted database already exists, or something may have gone " - "wrong during the last run~%")) - (convert)) - (do ((arg (car args) (and (not (null? args)) - (car args)))) - ((or (null? arg) (not arg))) - (cond - ((or (string= arg "-v") (string= arg "--version")) - (display-version) - (exit 0)) - ((or (string= arg "-h") (string= arg "--help")) - (display-help) - (exit 0)) - (else - (format #t "Unrecognized option: ~a~%" (car args)))) - (set! args (cdr args))))) - -(main (command-line-arguments)) diff --git a/src/markam-convert.scm b/src/markam-convert.scm new file mode 100644 index 0000000..b1605c5 --- /dev/null +++ b/src/markam-convert.scm @@ -0,0 +1,112 @@ +;;; markam -- Store/retrieve/manage bookmarks +;; Copyright (C) 2012 Tom Willemsen + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Convert any old database file to one usable with this version of +;; markam. If you're just starting to use markam there should be +;; no need to use this. + +;;; Code: + +(declare (uses paths + common)) + +(require-extension sqlite3) +(require-library srfi-4) + +(define (blob->seconds blob) + ;; Convert BLOB to an integer representing the seconds since + ;; 01-01-1970 (standard unix timstamp). This has to be done because + ;; in the previous version of markam I stored the timestamp as a + ;; blob and CHICKEN doesn't seem to like blobs much. + (if (u32vector? blob) + (u32vector-ref (blob->u32vector blob) 0) + 0)) + +(define (convert-table db name converter #!optional select) + ;; Convert NAME from DB using CONVERTER for each specific row. The + ;; optional SELECT should be an SQL query that will be used to fetch + ;; the rows to convert. + (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) + ;; Convert an old database into a new one and then replace the old + ;; with the new. + (let ((old-db (open-database (data-file "linkwave.db" "linkwave"))) + (new-db (open-database (data-file "markam.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)") + + (format #t "Database converted.~%"))) + +(define (display-help) + ;; Display markam's help message. + (format #t (string-append + "Usage: markam-convert [options]...~%" + "~%" + "Possible options:~%" + "~%" + "--help, -h Display this help and exit~%" + "--version, -v Output version information and exit~%"))) + +(define (main args) + ;; Convert an old database file unless a conversion database already + ;; exists. + (if (null? args) + (if (file-exists? (data-file "markam.db")) + (format #t (string-append "Converted database already exists, or something may have gone " + "wrong during the last run~%")) + (convert)) + (do ((arg (car args) (and (not (null? args)) + (car args)))) + ((or (null? arg) (not arg))) + (cond + ((or (string= arg "-v") (string= arg "--version")) + (display-version) + (exit 0)) + ((or (string= arg "-h") (string= arg "--help")) + (display-help) + (exit 0)) + (else + (format #t "Unrecognized option: ~a~%" (car args)))) + (set! args (cdr args))))) + +(main (command-line-arguments)) -- cgit v1.3-2-g0d8e