1 ;;; org-tag-eldoc-database.el --- org-tag-eldoc database storage -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
12 (require 'sqlite nil t
)
13 (require 'org-tag-eldoc-common
)
16 (defcustom org-tag-eldoc-database-file
17 (expand-file-name ".org-tag-eldoc-database.db" user-emacs-directory
)
18 "The location of org-tag-eldoc SQLite3 database."
21 :group
'org-tags-eldoc
)
23 (defcustom org-tag-eldoc-database-table-name
"default"
24 "The table name in org-tag-eldoc database."
27 :group
'org-tags-eldoc
)
29 (defmacro org-tag-eldoc-database--execute-with-db
(&rest body
)
30 "A macro for execute BODY with database."
31 `(if (sqlite-available-p)
32 (let ((db (sqlite-open org-tag-eldoc-database-file
)))
36 (error (format "[org-tag-eldoc] Error: database backend SQLite support of Emacs not available!"))))
38 ;;; TEST: expand the macro
40 ;; '(org-tag-eldoc-database--execute-with-db
41 ;; (sqlite-select db "SELECT 1+1;")
42 ;; (sqlite-execute db "SELECT 1+1;")))
45 (defun org-tag-eldoc-database--initlize ()
46 "Initialize the default SQLite3 database file `org-tag-eldoc-database-file'."
47 ;; Create the empty SQLite3 database file.
48 (unless (file-exists-p org-tag-eldoc-database-file
)
50 (format "sqlite3 %s \"VACUUM\";" org-tag-eldoc-database-file
))
51 (org-tag-eldoc-database--execute-with-db
54 (format "CREATE TABLE '%s' (tag VARCHAR, explanation TEXT);"
55 org-tag-eldoc-database-table-name
)))))
58 (org-tag-eldoc-database--initlize)
60 (defun org-tag-eldoc-database-query (tag)
61 "Query TAG for explanation in database."
62 (org-tag-eldoc-database--execute-with-db
63 (setq org-tag-eldoc--explanation
67 (format "SELECT explanation from '%s' WHERE tag = '%s';"
68 org-tag-eldoc-database-table-name tag
)))))
69 org-tag-eldoc--explanation
)
71 (defun org-tag-eldoc-database-save (tag explanation
)
72 "Save TAG and EXPLANATION into database."
73 (when (and (stringp explanation
)
74 (not (string-empty-p explanation
))
75 (not (string-equal explanation
"nil")))
76 (org-tag-eldoc-database--execute-with-db
79 (format "INSERT INTO '%s' (tag, explanation) VALUES ('%s', '%s');"
80 org-tag-eldoc-database-table-name
82 (message "[org-tag-eldoc] Saved tag explanation to database.")
83 ;; Still return the `explanation' after save to database.
86 (defun org-tag-eldoc-database-clean-empty-rows ()
87 "Clean and delete all empty explanation rows in database."
89 (org-tag-eldoc-database--execute-with-db
92 (format "DELETE FROM '%s' WHERE explanation is null or explanation = '';"
93 org-tag-eldoc-database-table-name
)))
94 (message "[org-tag-eldoc] Clean & deleted all empty records in database 'org-tag-eldoc'."))
96 (defun org-tag-eldoc-database-open-database-file ()
97 "Open the org-tag-eldoc SQLite3 database file `org-tag-eldoc-database-file'."
99 (sqlite-mode-open-file org-tag-eldoc-database-file
))
103 (provide 'org-tag-eldoc-database
)
105 ;;; org-tag-eldoc-database.el ends here