summaryrefslogtreecommitdiffstats
path: root/src/convert.scm
blob: 3229823346414072a4cb51150b951f47916bca41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;;; linkwave -- Store/retrieve/manage bookmarks
;; Copyright (C) 2012  Tom Willemsen <tom at ryuslash dot org>

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Convert any old database file to one usable with this version of
;; linkwave.  If you're just starting to use linkwave there should be
;; no need to use this.

;;; Code:

(declare (uses paths))

(require-extension sqlite3)
(require-library srfi-4)

(define (blob->seconds blob)
  (if (u32vector? blob)
      (u32vector-ref (blob->u32vector blob) 0)
      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))