diff options
author | Thanos Apollo <[email protected]> | 2024-07-20 17:57:13 +0300 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2024-07-20 18:16:11 +0300 |
commit | 56d9ecb057327d43372c48dc5cffbd77ae0c79b1 (patch) | |
tree | 20382e03c1e428d2d6dec50925031a5c4626378a /gnosis.el | |
parent | 6815aa55d83458ef2de504834adff1001df4b601 (diff) |
db: Add gnosis-table-exists
* Check if a table exists before creating/deleting it.
Diffstat (limited to 'gnosis.el')
-rw-r--r-- | gnosis.el | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -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." |