Add sql macro
This gives SQL queries a little more Lisp-y feel and allows for better formatting of the code written.
This commit is contained in:
parent
c7b0e8b951
commit
c442bbfe95
1 changed files with 46 additions and 26 deletions
|
@ -21,6 +21,10 @@
|
||||||
|
|
||||||
(export '(clark))
|
(export '(clark))
|
||||||
|
|
||||||
|
(defmacro sql (&body body)
|
||||||
|
(apply 'concatenate 'string
|
||||||
|
(mapcar (lambda (itm) (format nil "~A " itm)) body)))
|
||||||
|
|
||||||
(defvar *db* nil
|
(defvar *db* nil
|
||||||
"The database connection.")
|
"The database connection.")
|
||||||
|
|
||||||
|
@ -69,7 +73,8 @@
|
||||||
(when url-or-id
|
(when url-or-id
|
||||||
(if (integerp url-or-id)
|
(if (integerp url-or-id)
|
||||||
(execute-non-query
|
(execute-non-query
|
||||||
*db* "DELETE FROM bookmark_tag WHERE bookmark_id = ?" url-or-id)
|
*db* (sql delete from "bookmark_tag" where "bookmark_id" = ?)
|
||||||
|
url-or-id)
|
||||||
(clear-tags (get-bookmark-id url-or-id)))))
|
(clear-tags (get-bookmark-id url-or-id)))))
|
||||||
|
|
||||||
(defun ensure-db-exists (name)
|
(defun ensure-db-exists (name)
|
||||||
|
@ -77,19 +82,30 @@
|
||||||
(let ((db-exists (probe-file name)))
|
(let ((db-exists (probe-file name)))
|
||||||
(setf *db* (connect name))
|
(setf *db* (connect name))
|
||||||
(unless db-exists
|
(unless db-exists
|
||||||
(execute-non-query *db* "CREATE TABLE bookmark (url VARCHAR(255) UNIQUE, date INTEGER, name VARCHAR(255), description TEXT)")
|
(execute-non-query
|
||||||
(execute-non-query *db* "CREATE TABLE tag (name VARCHAR(255) UNIQUE)")
|
*db* (sql create table "bookmark" ("url" varchar (255) unique\,
|
||||||
(execute-non-query *db* "CREATE TABLE bookmark_tag (bookmark_id INTEGER REFERENCES bookmark(rowid), tag_id INTEGER REFERENCES tag(rowid), PRIMARY KEY (bookmark_id, tag_id))"))))
|
"date" integer\,
|
||||||
|
"name" varchar (255)\,
|
||||||
|
"description" text)))
|
||||||
|
(execute-non-query
|
||||||
|
*db* (sql create table "tag" ("name" varchar (255) unique)))
|
||||||
|
(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 get-bookmarks ()
|
(defun get-bookmarks ()
|
||||||
"Get a list of all bookmarks.
|
"Get a list of all bookmarks.
|
||||||
|
|
||||||
The result contains the url, name and the description of the bookmark."
|
The result contains the url, name and the description of the bookmark."
|
||||||
(execute-to-list *db* "select url, name, description from bookmark"))
|
(execute-to-list
|
||||||
|
*db* (sql select "url, name, description" from "bookmark")))
|
||||||
|
|
||||||
(defun get-bookmark-id (url)
|
(defun get-bookmark-id (url)
|
||||||
"Get the id of the bookmark for URL."
|
"Get the id of the bookmark for URL."
|
||||||
(execute-single *db* "SELECT rowid FROM bookmark WHERE url = ?" url))
|
(execute-single
|
||||||
|
*db* (sql select "rowid" from "bookmark" where "url" = ?) url))
|
||||||
|
|
||||||
(defun get-db-location ()
|
(defun get-db-location ()
|
||||||
"Get the location of the database."
|
"Get the location of the database."
|
||||||
|
@ -109,7 +125,8 @@ The result contains the url, name and the description of the bookmark."
|
||||||
|
|
||||||
(defun get-tag-id (name)
|
(defun get-tag-id (name)
|
||||||
"Get the rowid of tag NAME."
|
"Get the rowid of tag NAME."
|
||||||
(execute-single *db* "SELECT rowid FROM tag WHERE name = ?" name))
|
(execute-single
|
||||||
|
*db* (sql select "rowid" from "tag" where "name" = ?) name))
|
||||||
|
|
||||||
(defun help-message ()
|
(defun help-message ()
|
||||||
(format t (concatenate
|
(format t (concatenate
|
||||||
|
@ -133,17 +150,19 @@ The result contains the url, name and the description of the bookmark."
|
||||||
|
|
||||||
(defun insert-bookmark (url name description)
|
(defun insert-bookmark (url name description)
|
||||||
"Insert URL, NAME and DESCRIPTION into the bookmark table."
|
"Insert URL, NAME and DESCRIPTION into the bookmark table."
|
||||||
(execute-non-query *db* "INSERT INTO bookmark VALUES (?, ?, ?, ?)"
|
(execute-non-query
|
||||||
|
*db* (sql insert into "bookmark" values (?\, ?\, ?\, ?))
|
||||||
url (get-universal-time) name description))
|
url (get-universal-time) name description))
|
||||||
|
|
||||||
(defun insert-bookmark-tag (bookmark-id tag-id)
|
(defun insert-bookmark-tag (bookmark-id tag-id)
|
||||||
"Insert BOOKMARK-ID and TAG-ID into the bookmark_tag table."
|
"Insert BOOKMARK-ID and TAG-ID into the bookmark_tag table."
|
||||||
(execute-non-query *db* "INSERT INTO bookmark_tag VALUES (?, ?)"
|
(execute-non-query
|
||||||
|
*db* (sql insert into "bookmark_tag" values (?\, ?))
|
||||||
bookmark-id tag-id))
|
bookmark-id tag-id))
|
||||||
|
|
||||||
(defun insert-tag (name)
|
(defun insert-tag (name)
|
||||||
"Insert tag NAME into the database and return its rowid."
|
"Insert tag NAME into the database and return its rowid."
|
||||||
(execute-non-query *db* "INSERT INTO tag VALUES (?)" name)
|
(execute-non-query *db* (sql insert into "tag" values (?)) name)
|
||||||
(last-insert-rowid *db*))
|
(last-insert-rowid *db*))
|
||||||
|
|
||||||
(defun load-db ()
|
(defun load-db ()
|
||||||
|
@ -226,7 +245,8 @@ option will replace the previous value for that part."
|
||||||
qargs (nconc qargs (list (cadr desc-lst)))))
|
qargs (nconc qargs (list (cadr desc-lst)))))
|
||||||
(when qargs
|
(when qargs
|
||||||
(apply #'execute-non-query *db*
|
(apply #'execute-non-query *db*
|
||||||
(format nil "UPDATE bookmark SET ~A WHERE url = ?" query)
|
(format
|
||||||
|
nil (sql update "bookmark" set "~A" where "url" = ?) query)
|
||||||
(append qargs (list url))))))
|
(append qargs (list url))))))
|
||||||
|
|
||||||
(defcommand exists (url)
|
(defcommand exists (url)
|
||||||
|
@ -235,9 +255,10 @@ option will replace the previous value for that part."
|
||||||
|
|
||||||
Check if URL exists in the database. Prints `yes' when found and `no'
|
Check if URL exists in the database. Prints `yes' when found and `no'
|
||||||
otherwise."
|
otherwise."
|
||||||
(format t "~:[no~;yes~]~%"
|
(format
|
||||||
|
t "~:[no~;yes~]~%"
|
||||||
(execute-single
|
(execute-single
|
||||||
*db* "SELECT rowid FROM bookmark WHERE url = ?" url)))
|
*db* (sql select "rowid" from "bookmark" where "url" = ?) url)))
|
||||||
|
|
||||||
(defcommand help (&optional command)
|
(defcommand help (&optional command)
|
||||||
"Show help message."
|
"Show help message."
|
||||||
|
@ -260,7 +281,7 @@ otherwise."
|
||||||
Remove URL from the database."
|
Remove URL from the database."
|
||||||
(clear-tags url)
|
(clear-tags url)
|
||||||
(execute-non-query
|
(execute-non-query
|
||||||
*db* "DELETE FROM bookmark WHERE url = ?" url))
|
*db* (sql delete from "bookmark" where "url" = ?) url))
|
||||||
|
|
||||||
(defcommand search (str)
|
(defcommand search (str)
|
||||||
"Search through bookmarks."
|
"Search through bookmarks."
|
||||||
|
@ -270,14 +291,13 @@ Search the database for STR. Matches are made for substrings of a
|
||||||
bookmark's name or an exact match for a tag."
|
bookmark's name or an exact match for a tag."
|
||||||
(map nil #'print-bookmark
|
(map nil #'print-bookmark
|
||||||
(execute-to-list
|
(execute-to-list
|
||||||
*db* (concatenate 'string
|
*db* (sql select "url, name, description"
|
||||||
"SELECT url, name, description "
|
from "bookmark"
|
||||||
"FROM bookmark "
|
where "name" like ?
|
||||||
"WHERE name LIKE ? "
|
or ? in (select "name"
|
||||||
"OR ? IN (SELECT name "
|
from "tag"
|
||||||
"FROM tag "
|
join "bookmark_tag" on ("tag_id = tag.rowid")
|
||||||
"JOIN bookmark_tag ON (tag_id = tag.rowid) "
|
where "bookmark_id" = "bookmark.rowid"))
|
||||||
"WHERE bookmark_id = bookmark.rowid)")
|
|
||||||
(format nil "%~A%" str) str)))
|
(format nil "%~A%" str) str)))
|
||||||
|
|
||||||
(defcommand set-tags (url &rest tags)
|
(defcommand set-tags (url &rest tags)
|
||||||
|
|
Loading…
Reference in a new issue