(internal): rename link columns (#1213)

from -> source
to -> dest

"from" is a reserved keyword in sqlite, so we avoid it.
This commit is contained in:
Jethro Kuan
2020-10-25 22:33:43 +08:00
committed by GitHub
parent b937bc9655
commit bd8b5587f5
4 changed files with 33 additions and 33 deletions

View File

@ -79,7 +79,7 @@ value like `most-positive-fixnum'."
:type 'int :type 'int
:group 'org-roam) :group 'org-roam)
(defconst org-roam-db--version 9) (defconst org-roam-db--version 10)
(defvar org-roam-db--connection (make-hash-table :test #'equal) (defvar org-roam-db--connection (make-hash-table :test #'equal)
"Database connection to Org-roam database.") "Database connection to Org-roam database.")
@ -144,8 +144,8 @@ SQL can be either the emacsql vector representation, or a string."
(level :not-null)]) (level :not-null)])
(links (links
[(from :not-null) [(source :not-null)
(to :not-null) (dest :not-null)
(type :not-null) (type :not-null)
(properties :not-null)]) (properties :not-null)])
@ -226,7 +226,7 @@ This is equivalent to removing the node from the graph."
(buffer-file-name (buffer-base-buffer)))))) (buffer-file-name (buffer-base-buffer))))))
(dolist (table (mapcar #'car org-roam-db--table-schemata)) (dolist (table (mapcar #'car org-roam-db--table-schemata))
(org-roam-db-query `[:delete :from ,table (org-roam-db-query `[:delete :from ,table
:where (= ,(if (eq table 'links) 'from 'file) $s1)] :where (= ,(if (eq table 'links) 'source 'file) $s1)]
file)))) file))))
;;;;; Inserting ;;;;; Inserting
@ -302,7 +302,7 @@ Return the number of rows inserted."
(let ((file (or org-roam-file-name (buffer-file-name)))) (let ((file (or org-roam-file-name (buffer-file-name))))
(when update-p (when update-p
(org-roam-db-query [:delete :from links (org-roam-db-query [:delete :from links
:where (= from $s1)] :where (= source $s1)]
file)) file))
(if-let ((links (org-roam--extract-links))) (if-let ((links (org-roam--extract-links)))
(progn (progn
@ -390,12 +390,12 @@ If the file does not have any connections, nil is returned."
links_of(file, link) AS links_of(file, link) AS
(WITH filelinks AS (SELECT * FROM links WHERE NOT \"type\" = '\"cite\"'), (WITH filelinks AS (SELECT * FROM links WHERE NOT \"type\" = '\"cite\"'),
citelinks AS (SELECT * FROM links citelinks AS (SELECT * FROM links
JOIN refs ON links.\"to\" = refs.\"ref\" JOIN refs ON links.\"dest\" = refs.\"ref\"
AND links.\"type\" = '\"cite\"') AND links.\"type\" = '\"cite\"')
SELECT \"from\", \"to\" FROM filelinks UNION SELECT \"source\", \"dest\" FROM filelinks UNION
SELECT \"to\", \"from\" FROM filelinks UNION SELECT \"dest\", \"source\" FROM filelinks UNION
SELECT \"file\", \"from\" FROM citelinks UNION SELECT \"file\", \"source\" FROM citelinks UNION
SELECT \"from\", \"file\" FROM citelinks), SELECT \"dest\", \"file\" FROM citelinks),
connected_component(file) AS connected_component(file) AS
(SELECT link FROM links_of WHERE file = $s1 (SELECT link FROM links_of WHERE file = $s1
UNION UNION
@ -412,12 +412,12 @@ connections, nil is returned."
links_of(file, link) AS links_of(file, link) AS
(WITH filelinks AS (SELECT * FROM links WHERE NOT \"type\" = '\"cite\"'), (WITH filelinks AS (SELECT * FROM links WHERE NOT \"type\" = '\"cite\"'),
citelinks AS (SELECT * FROM links citelinks AS (SELECT * FROM links
JOIN refs ON links.\"to\" = refs.\"ref\" JOIN refs ON links.\"dest\" = refs.\"ref\"
AND links.\"type\" = '\"cite\"') AND links.\"type\" = '\"cite\"')
SELECT \"from\", \"to\" FROM filelinks UNION SELECT \"source\", \"dest\" FROM filelinks UNION
SELECT \"to\", \"from\" FROM filelinks UNION SELECT \"dest\", \"source\" FROM filelinks UNION
SELECT \"file\", \"from\" FROM citelinks UNION SELECT \"file\", \"source\" FROM citelinks UNION
SELECT \"from\", \"file\" FROM citelinks), SELECT \"source\", \"file\" FROM citelinks),
-- Links are traversed in a breadth-first search. In order to calculate the -- Links are traversed in a breadth-first search. In order to calculate the
-- distance of nodes and to avoid following cyclic links, the visited nodes -- distance of nodes and to avoid following cyclic links, the visited nodes
-- are tracked in 'trace'. -- are tracked in 'trace'.

View File

@ -169,15 +169,15 @@ into a digraph."
(let* ((nodes (org-roam-db-query node-query)) (let* ((nodes (org-roam-db-query node-query))
(edges-query (edges-query
`[:with selected :as [:select [file] :from ,node-query] `[:with selected :as [:select [file] :from ,node-query]
:select :distinct [to from] :from links :select :distinct [dest source] :from links
:where (and (in to selected) (in from selected))]) :where (and (in dest selected) (in source selected))])
(edges-cites-query (edges-cites-query
`[:with selected :as [:select [file] :from ,node-query] `[:with selected :as [:select [file] :from ,node-query]
:select :distinct [file from] :select :distinct [file source]
:from links :inner :join refs :on (and (= links:to refs:ref) :from links :inner :join refs :on (and (= links:dest refs:ref)
(= links:type "cite") (= links:type "cite")
(= refs:type "cite")) (= refs:type "cite"))
:where (and (in file selected) (in from selected))]) :where (and (in file selected) (in source selected))])
(edges (org-roam-db-query edges-query)) (edges (org-roam-db-query edges-query))
(edges-cites (org-roam-db-query edges-cites-query))) (edges-cites (org-roam-db-query edges-cites-query)))
(insert "digraph \"org-roam\" {\n") (insert "digraph \"org-roam\" {\n")

View File

@ -565,7 +565,7 @@ Assume buffer is widened and point is on a headline."
"Extracts all link items within the current buffer. "Extracts all link items within the current buffer.
Link items are of the form: Link items are of the form:
[from to type properties] [source dest type properties]
This is the format that emacsql expects when inserting into the database. This is the format that emacsql expects when inserting into the database.
FILE-FROM is typically the buffer file path, but this may not exist, for example FILE-FROM is typically the buffer file path, but this may not exist, for example
@ -1040,11 +1040,11 @@ citation key, for Org-ref cite links."
(unless (listp targets) (unless (listp targets)
(setq targets (list targets))) (setq targets (list targets)))
(let ((conditions (--> targets (let ((conditions (--> targets
(mapcar (lambda (i) (list '= 'to i)) it) (mapcar (lambda (i) (list '= 'dest i)) it)
(org-roam--list-interleave it :or)))) (org-roam--list-interleave it :or))))
(org-roam-db-query `[:select [from to properties] :from links (org-roam-db-query `[:select [source dest properties] :from links
:where ,@conditions :where ,@conditions
:order-by (asc from)]))) :order-by (asc source)])))
(defun org-roam-id-get-file (id) (defun org-roam-id-get-file (id)
"Return the file if ID exists in the Org-roam database. "Return the file if ID exists in the Org-roam database.
@ -1374,9 +1374,9 @@ if applicable.
To be added to `org-roam-title-change-hook'." To be added to `org-roam-title-change-hook'."
(let* ((current-path (buffer-file-name)) (let* ((current-path (buffer-file-name))
(files-affected (org-roam-db-query [:select :distinct [from] (files-affected (org-roam-db-query [:select :distinct [source]
:from links :from links
:where (= to $s1)] :where (= dest $s1)]
current-path))) current-path)))
(dolist (file files-affected) (dolist (file files-affected)
(with-current-buffer (or (find-buffer-visiting (car file)) (with-current-buffer (or (find-buffer-visiting (car file))
@ -1421,9 +1421,9 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path."
(new-path (expand-file-name new-file)) (new-path (expand-file-name new-file))
(new-buffer (or (find-buffer-visiting new-path) (new-buffer (or (find-buffer-visiting new-path)
(find-file-noselect new-path))) (find-file-noselect new-path)))
(files-affected (org-roam-db-query [:select :distinct [from] (files-affected (org-roam-db-query [:select :distinct [source]
:from links :from links
:where (= to $s1)] :where (= dest $s1)]
old-path))) old-path)))
;; Remove database entries for old-file.org ;; Remove database entries for old-file.org
(org-roam-db--clear-file old-file) (org-roam-db--clear-file old-file)

View File

@ -300,21 +300,21 @@
;; Links ;; Links
(expect (caar (org-roam-db-query [:select (funcall count) :from links (expect (caar (org-roam-db-query [:select (funcall count) :from links
:where (= from $s1)] :where (= source $s1)]
(test-org-roam--abs-path "foo.org"))) :to-be 1) (test-org-roam--abs-path "foo.org"))) :to-be 1)
(expect (caar (org-roam-db-query [:select (funcall count) :from links (expect (caar (org-roam-db-query [:select (funcall count) :from links
:where (= from $s1)] :where (= source $s1)]
(test-org-roam--abs-path "nested/bar.org"))) :to-be 2) (test-org-roam--abs-path "nested/bar.org"))) :to-be 2)
;; Links -- File-to ;; Links -- File-to
(expect (caar (org-roam-db-query [:select (funcall count) :from links (expect (caar (org-roam-db-query [:select (funcall count) :from links
:where (= to $s1)] :where (= dest $s1)]
(test-org-roam--abs-path "nested/foo.org"))) :to-be 1) (test-org-roam--abs-path "nested/foo.org"))) :to-be 1)
(expect (caar (org-roam-db-query [:select (funcall count) :from links (expect (caar (org-roam-db-query [:select (funcall count) :from links
:where (= to $s1)] :where (= dest $s1)]
(test-org-roam--abs-path "nested/bar.org"))) :to-be 1) (test-org-roam--abs-path "nested/bar.org"))) :to-be 1)
(expect (caar (org-roam-db-query [:select (funcall count) :from links (expect (caar (org-roam-db-query [:select (funcall count) :from links
:where (= to $s1)] :where (= dest $s1)]
(test-org-roam--abs-path "unlinked.org"))) :to-be 0) (test-org-roam--abs-path "unlinked.org"))) :to-be 0)
;; TODO Test titles ;; TODO Test titles
(expect (org-roam-db-query [:select * :from titles]) (expect (org-roam-db-query [:select * :from titles])