summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThanos Apollo <[email protected]>2024-07-20 17:57:13 +0300
committerThanos Apollo <[email protected]>2024-07-20 18:16:11 +0300
commit56d9ecb057327d43372c48dc5cffbd77ae0c79b1 (patch)
tree20382e03c1e428d2d6dec50925031a5c4626378a
parent6815aa55d83458ef2de504834adff1001df4b601 (diff)
db: Add gnosis-table-exists
* Check if a table exists before creating/deleting it.
-rw-r--r--gnosis.el12
1 files changed, 10 insertions, 2 deletions
diff --git a/gnosis.el b/gnosis.el
index 6ff4bf0..1c3881d 100644
--- a/gnosis.el
+++ b/gnosis.el
@@ -287,13 +287,21 @@ Optional argument FLATTEN, when non-nil, flattens the result."
"Select VALUE from TABLE for note ID."
(gnosis-select value table `(= id ,id) t))
+(defun gnosis-table-exists-p (table)
+ "Check if TABLE exists."
+ (let ((tables (mapcar (lambda (str) (replace-regexp-in-string "_" "-" (symbol-name str)))
+ (cdr (gnosis-select 'name 'sqlite-master '(= type table) t)))))
+ (member (symbol-name table) tables)))
+
(cl-defun gnosis--create-table (table &optional values)
"Create TABLE for VALUES."
- (emacsql gnosis-db `[:create-table ,table ,values]))
+ (unless (gnosis-table-exists-p table)
+ (emacsql gnosis-db `[:create-table ,table ,values])))
(cl-defun gnosis--drop-table (table)
"Drop TABLE from `gnosis-db'."
- (emacsql gnosis-db `[:drop-table ,table]))
+ (when (gnosis-table-exists-p table)
+ (emacsql gnosis-db `[:drop-table ,table])))
(cl-defun gnosis--insert-into (table values)
"Insert VALUES to TABLE."