aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/queries.lisp
blob: 4651ed66d30cc257f79ca92c868bd527a31b5ca5 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
;; Copyright (C) 2013  Tom Willemsen <tom at ryuslash dot org>

;; This file is part of CLark

;; CLark 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.

;; CLark 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 CLark. If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(in-package :org.ryuslash.clark)

(defun bookmark-exists-p (url)
  "Check if URL can be found in the database."
  (execute-single
   *db* (sql select "rowid" from "bookmark" where "url" = ?) url))

(defun bookmark-list ()
  "Get a list of all stored bookmarks."
  (execute-to-list
   *db* (sql select "url, name, description" from "bookmark")))

(defun bookmark-search (name-or-tag)
  "Get bookmarks by NAME-OR-TAG."
  (execute-to-list
   *db* (sql select "url, name, description"
             from "bookmark"
             where "name" like ?
             or ? in (select "name"
                      from "tag"
                      join "bookmark_tag" on ("tag_id = tag.rowid")
                      where "bookmark_id" = "bookmark.rowid"))
   (format nil "%~A%" name-or-tag) name-or-tag))

(defun create-table-bookmark ()
  "Create the bookmark table."
  (execute-non-query
   *db* (sql create table "bookmark"
             ("url" varchar (255) unique\,
              "date" integer\,
              "name" varchar (255)\,
              "description" text))))

(defun create-table-bookmark_tag ()
  "Create the bookmark_tag table."
  (execute-non-query
   *db* (sql create table "bookmark_tag"
             ("bookmark_id" integer references "bookmark(rowid)"\,
              "tag_id" integer references "tag(rowid)"\,
              primary key ("bookmark_id"\,  "tag_id")))))

(defun create-table-tag ()
  "Create the tag table."
  (execute-non-query
   *db* (sql create table "tag" ("name" varchar (255) unique))))

(defun delete-bookmark (url)
  "Delete URL from collection."
  (execute-non-query
   *db* (sql delete from "bookmark" where "url" = ?) url))

(defun delete-tags (id)
  "Clear the tags for bookmark with id ID."
  (execute-non-query *db* (sql delete from "bookmark_tag"
                               where "bookmark_id" = ?) id))

(defun get-bookmark-id (url)
  "Get the id of the bookmark for URL."
  (execute-single
   *db* (sql select "rowid" from "bookmark" where "url" = ?) url))

(defun get-tag-id (name)
  "Get the rowid of tag NAME."
  (execute-single
   *db* (sql select "rowid" from "tag" where "name" = ?) name))

(defun insert-bookmark (url name description)
  "Insert URL, NAME and DESCRIPTION into the bookmark table."
  (execute-non-query
   *db* (sql insert into "bookmark" values (?\, ?\, ?\, ?))
   url (get-universal-time) name description))

(defun insert-bookmark-tag (bookmark-id tag-id)
  "Insert BOOKMARK-ID and TAG-ID into the bookmark_tag table."
  (execute-non-query
   *db* (sql insert into "bookmark_tag" values (?\, ?))
   bookmark-id tag-id))

(defun insert-tag (name)
  "Insert tag NAME into the database and return its rowid."
  (execute-non-query *db* (sql insert into "tag" values (?)) name)
  (last-insert-rowid *db*))

(defun url-list (&optional tag)
  "Get a list of URLs, possibly with tag TAG."
  (if tag
      (url-list-for-tag tag)
      (url-list-no-tag)))

(defun url-list-for-tag (tag)
  "Get a list of all the stored URLs with tag TAG."
  (mapcar
   #'car
   (execute-to-list
    *db* (sql select "url"
              from "bookmark"
              where ? in (select "name"
                          from "tag"
                          join "bookmark_tag" on ("tag_id = tag.rowid")
                          where "bookmark_id" = "bookmark.rowid"))
    tag)))

(defun url-list-no-tag ()
  "Get a list of all URLs stored."
  (mapcar #'car
          (execute-to-list
           *db* (sql select "url"
                     from "bookmark"))))