emacsql-sqlite3 -> emacsql-sqlite

Revert from emacsql-sqlite3 to emacsql-sqlite for foreign key support.
This makes it slightly harder to get Org-roam up and running for
Windows, but not impossible, but foreign keys makes database operations
faster and keeps the code clean.

Also, fix schemata for foreign keys on various tables, and add indices
where joins are common (org-roam-db--table-indices).
This commit is contained in:
Jethro Kuan
2021-05-02 16:30:43 +08:00
parent 0a7d365e22
commit f8dd345bd1
12 changed files with 58 additions and 59 deletions

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (s "1.12.0") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify ;; This program is free software; you can redistribute it and/or modify

View File

@ -8,7 +8,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.
@ -33,7 +33,7 @@
;;;; Library Requires ;;;; Library Requires
(eval-when-compile (require 'subr-x)) (eval-when-compile (require 'subr-x))
(require 'emacsql) (require 'emacsql)
(require 'emacsql-sqlite3) (require 'emacsql-sqlite)
(require 'seq) (require 'seq)
(eval-and-compile (eval-and-compile
@ -79,7 +79,7 @@ value like `most-positive-fixnum'."
:type 'int :type 'int
:group 'org-roam) :group 'org-roam)
(defconst org-roam-db--version 13) (defconst org-roam-db--version 14)
(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.")
@ -99,7 +99,7 @@ Performs a database upgrade when required."
(emacsql-live-p (org-roam-db--get-connection))) (emacsql-live-p (org-roam-db--get-connection)))
(let ((init-db (not (file-exists-p org-roam-db-location)))) (let ((init-db (not (file-exists-p org-roam-db-location))))
(make-directory (file-name-directory org-roam-db-location) t) (make-directory (file-name-directory org-roam-db-location) t)
(let ((conn (emacsql-sqlite3 org-roam-db-location))) (let ((conn (emacsql-sqlite org-roam-db-location)))
(set-process-query-on-exit-flag (emacsql-process conn) nil) (set-process-query-on-exit-flag (emacsql-process conn) nil)
(puthash (expand-file-name org-roam-directory) (puthash (expand-file-name org-roam-directory)
conn conn
@ -136,46 +136,47 @@ SQL can be either the emacsql vector representation, or a string."
(hash :not-null)]) (hash :not-null)])
(nodes (nodes
[(id :primary-key :not-null) ([(id :not-null :primary-key)
(file :not-null) (file :not-null)
(level :not-null) (level :not-null)
(pos :not-null) (pos :not-null)
todo todo
priority priority
(scheduled text) (scheduled text)
(deadline text) (deadline text)
title title
properties properties
olp] olp]
(:foreign-key [file] :references files [file] :on-delete :cascade)) (:foreign-key [file] :references files [file] :on-delete :cascade)))
(aliases (aliases
[(file :not-null) ([(node-id :not-null :primary-key)
(node-id :not-null) alias]
alias] (:foreign-key [node-id] :references nodes [id] :on-delete :cascade)))
(:foreign-key [node-id] :references nodes [id] :on-delete :cascade))
(refs (refs
([(file :not-null) ([(node-id :not-null)
(node-id :not-null)
(ref :not-null) (ref :not-null)
(type :not-null)] (type :not-null)]
(:foreign-key [node-id] :references nodes [id] :on-delete :cascade))) (:foreign-key [node-id] :references nodes [id] :on-delete :cascade)))
(tags (tags
[(file :not-null) ([(node-id :not-null)
(node-id :not-null)
tag] tag]
(:foreign-key [node-id] :references nodes [id] :on-delete :cascade)) (:foreign-key [node-id] :references nodes [id] :on-delete :cascade)))
(links (links
[(file :not-null) ([(pos :not-null)
(pos :not-null) (source :not-null)
(source :not-null) (dest :not-null)
(dest :not-null) (type :not-null)
(type :not-null) (properties :not-null)]
(properties :not-null)] (:foreign-key [source] :references nodes [id] :on-delete :cascade)))))
(:foreign-key [file] :references files [file] :on-delete :cascade))))
(defconst org-roam-db--table-indices
'((alias-node-id aliases [node-id])
(refs-node-id refs [node-id])
(tags-node-id tags [node-id])))
(defun org-roam-db--init (db) (defun org-roam-db--init (db)
"Initialize database DB with the correct schema and user version." "Initialize database DB with the correct schema and user version."
@ -183,6 +184,8 @@ SQL can be either the emacsql vector representation, or a string."
(emacsql db "PRAGMA foreign_keys = ON") (emacsql db "PRAGMA foreign_keys = ON")
(pcase-dolist (`(,table ,schema) org-roam-db--table-schemata) (pcase-dolist (`(,table ,schema) org-roam-db--table-schemata)
(emacsql db [:create-table $i1 $S2] table schema)) (emacsql db [:create-table $i1 $S2] table schema))
(pcase-dolist (`(,index-name ,table ,columns) org-roam-db--table-indices)
(emacsql db [:create-index $i1 :on $i2 $S3] index-name table columns))
(emacsql db (format "PRAGMA user_version = %s" org-roam-db--version)))) (emacsql db (format "PRAGMA user_version = %s" org-roam-db--version))))
(defun org-roam-db--upgrade-maybe (db version) (defun org-roam-db--upgrade-maybe (db version)
@ -224,10 +227,9 @@ the current `org-roam-directory'."
This is equivalent to removing the node from the graph. This is equivalent to removing the node from the graph.
If FILE is nil, clear the current buffer." If FILE is nil, clear the current buffer."
(setq file (or file (buffer-file-name (buffer-base-buffer)))) (setq file (or file (buffer-file-name (buffer-base-buffer))))
(dolist (table (mapcar #'car org-roam-db--table-schemata)) (org-roam-db-query [:delete :from files
(org-roam-db-query `[:delete :from ,table :where (= file $s1)]
:where (= file $s1)] file))
file)))
;;;;; Updating tables ;;;;; Updating tables
(defun org-roam-db-insert-file () (defun org-roam-db-insert-file ()
@ -297,14 +299,14 @@ If UPDATE-P is non-nil, first remove the file in the database."
[:insert :into tags [:insert :into tags
:values $v1] :values $v1]
(mapcar (lambda (tag) (mapcar (lambda (tag)
(vector file id (substring-no-properties tag))) (vector id (substring-no-properties tag)))
tags))) tags)))
(when aliases (when aliases
(org-roam-db-query (org-roam-db-query
[:insert :into aliases [:insert :into aliases
:values $v1] :values $v1]
(mapcar (lambda (alias) (mapcar (lambda (alias)
(vector file id alias)) (vector id alias))
(split-string-and-unquote aliases)))) (split-string-and-unquote aliases))))
(when refs (when refs
(setq refs (split-string-and-unquote refs)) (setq refs (split-string-and-unquote refs))
@ -312,7 +314,7 @@ If UPDATE-P is non-nil, first remove the file in the database."
(dolist (ref refs) (dolist (ref refs)
(if (string-match org-link-plain-re ref) (if (string-match org-link-plain-re ref)
(progn (progn
(push (vector file id (match-string 2 ref) (push (vector id (match-string 2 ref)
(match-string 1 ref)) rows)) (match-string 1 ref)) rows))
(lwarn '(org-roam) :warning (lwarn '(org-roam) :warning
"%s:%s\tInvalid ref %s, skipping..." "%s:%s\tInvalid ref %s, skipping..."
@ -356,18 +358,16 @@ If UPDATE-P is non-nil, first remove the file in the database."
(defun org-roam-db-insert-tags () (defun org-roam-db-insert-tags ()
"Insert tags for node at point into Org-roam cache." "Insert tags for node at point into Org-roam cache."
(when-let ((file (buffer-file-name (buffer-base-buffer))) (when-let ((node-id (org-id-get))
(node-id (org-id-get))
(tags (org-get-tags))) (tags (org-get-tags)))
(org-roam-db-query [:insert :into tags (org-roam-db-query [:insert :into tags
:values $v1] :values $v1]
(mapcar (lambda (tag) (mapcar (lambda (tag)
(vector file node-id tag)) tags)))) (vector node-id tag)) tags))))
(defun org-roam-db-insert-refs () (defun org-roam-db-insert-refs ()
"Insert refs for node at point into Org-roam cache." "Insert refs for node at point into Org-roam cache."
(when-let* ((file (buffer-file-name (buffer-base-buffer))) (when-let* ((node-id (org-id-get))
(node-id (org-id-get))
(refs (org-entry-get (point) "ROAM_REFS")) (refs (org-entry-get (point) "ROAM_REFS"))
(refs (split-string-and-unquote refs))) (refs (split-string-and-unquote refs)))
(let (rows) (let (rows)
@ -375,7 +375,7 @@ If UPDATE-P is non-nil, first remove the file in the database."
(save-match-data (save-match-data
(if (string-match org-link-plain-re ref) (if (string-match org-link-plain-re ref)
(progn (progn
(push (vector file node-id (match-string 2 ref) (match-string 1 ref)) rows)) (push (vector node-id (match-string 2 ref) (match-string 1 ref)) rows))
(lwarn '(org-roam) :warning (lwarn '(org-roam) :warning
"%s:%s\tInvalid ref %s, skipping..." (buffer-file-name) (point) ref)))) "%s:%s\tInvalid ref %s, skipping..." (buffer-file-name) (point) ref))))
(org-roam-db-query [:insert :into refs (org-roam-db-query [:insert :into refs
@ -386,8 +386,7 @@ If UPDATE-P is non-nil, first remove the file in the database."
"Insert link data for LINK at current point into the Org-roam cache." "Insert link data for LINK at current point into the Org-roam cache."
(save-excursion (save-excursion
(goto-char (org-element-property :begin link)) (goto-char (org-element-property :begin link))
(let ((file (buffer-file-name (buffer-base-buffer))) (let ((type (org-element-property :type link))
(type (org-element-property :type link))
(dest (org-element-property :path link)) (dest (org-element-property :path link))
(properties (list :outline (org-get-outline-path))) (properties (list :outline (org-get-outline-path)))
(source (org-roam-id-at-point))) (source (org-roam-id-at-point)))
@ -395,7 +394,7 @@ If UPDATE-P is non-nil, first remove the file in the database."
(org-roam-db-query (org-roam-db-query
[:insert :into links [:insert :into links
:values $v1] :values $v1]
(vector file (point) source dest type properties)))))) (vector (point) source dest type properties))))))
;;;;; Fetching ;;;;; Fetching
(defun org-roam-db--get-current-files () (defun org-roam-db--get-current-files ()

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/jethrokuan/org-roam ;; URL: https://github.com/jethrokuan/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -5,7 +5,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -5,7 +5,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.

View File

@ -6,7 +6,7 @@
;; URL: https://github.com/org-roam/org-roam ;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience ;; Keywords: org-mode, roam, convenience
;; Version: 2.0.0 ;; Version: 2.0.0
;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite3 "1.0.2") (magit-section "2.90.1")) ;; Package-Requires: ((emacs "26.1") (dash "2.13") (f "0.17.2") (org "9.4") (emacsql "3.0.0") (emacsql-sqlite "1.0.0") (magit-section "2.90.1"))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.