integrated new sql data structure with transaction insertion

This commit is contained in:
ndwarshuis 2018-12-27 15:27:29 -05:00
parent dff2ed108a
commit 2ac5059597
1 changed files with 30 additions and 29 deletions

View File

@ -2707,37 +2707,33 @@ any other symbols to their symbol name."
(entry (symbol-name entry)) (entry (symbol-name entry))
(t "NULL"))) (t "NULL")))
(defun nd/sql-construct-insertion (tbl data) (defun nd/sql-construct-insert (tbl-name tbl-data)
"Concatenate DATA into escaped comma-separated string for SQL insertion." "Concatenate DATA into escaped comma-separated string for SQL insertion."
(let* ((data-str (mapcar #'nd/sql-to-string data)) (let* ((data-str (mapcar #'nd/sql-to-string tbl-data))
(data-str (string-join data-str ","))) (data-str (string-join data-str ",")))
(concat "insert into " tbl " values(" data-str ");"))) (concat "insert into " (symbol-name tbl-name) " values(" data-str ");")))
(defun nd/sql-insert (db tbl data) (defun nd/sql-construct-insert-transaction (all-data &optional acc)
"Insert list DATA into TBL in sqlite database DB." "Construct transaction string to insert ALL-DATA into SQL.
(nd/sql-cmd db (nd/sql-construct-insertion tbl data))) Does not actually execute the string."
(if (not all-data)
(defun nd/sql-insert-multi (db tbl-data &optional acc)
"Insert TBL-DATA into sqlite database DB using transactions.
TBL-DATA is a plist with each key as the table and the value as a
list of lists holding data for that table."
(if (not tbl-data)
(concat acc "commit;") (concat acc "commit;")
(let* ((acc (or acc "begin transaction;")) (let* ((tbl-name (car all-data))
(tbl-name (car tbl-data)) (tbl-data (nth 1 all-data))
(row-data (cdr tbl-data)) (rem (cddr all-data))
(rem (cddr tbl-data)) (tbl-data-str (mapcar (lambda (d) (nd/sql-construct-insert tbl-name d)) tbl-data))
(concat-tbl (tbl-data-str (string-join tbl-data-str))
(lambda (tbl data &optional acc) (new-acc (or acc "begin transaction;"))
(if data (new-acc (concat new-acc tbl-data-str)))
(let* ((cur (car data)) (nd/sql-construct-insert-transaction rem new-acc))))
(rem (cdr data))
(acc-new (nd/sql-construct-insertion tbl cur)) (defun nd/sql-insert (db tbl-name tbl-data)
(acc-new (concat acc acc-new))) "Insert list TBL-DATA into TBL-NAME in sqlite database DB."
(funcall concat-tbl tbl rem acc-new)) (nd/sql-cmd db (nd/sql-construct-insertion tbl-name tbl-data)))
acc)))
(new-acc (funcall concat-tbl tbl row-data))) (defun nd/sql-insert-multi (db all-data)
(nd/sql-insert-multi (db rem new-acc))))) "Insert ALL-DATA into sqlite DB."
(nd/sql-cmd db (nd/sql-construct-insert-transaction all-data)))
#+END_SRC #+END_SRC
**** org parsing function **** org parsing function
Basic functions to parse org strings Basic functions to parse org strings
@ -3455,8 +3451,8 @@ ARCHIVE-FILE-PATH is the file path to the currently parsed archive file."
new-acc))) new-acc)))
(nd/org-element-header-to-sql rem archive-file-path new-acc)))) (nd/org-element-header-to-sql rem archive-file-path new-acc))))
(defun nd/org-archive-to-db () (defun nd/org-sql-extract ()
"Transfer archive files to sqlite database." "Return a plist of data to be inserted into sql database."
(let* ((rxv-path (expand-file-name "test.org_archive" org-directory)) (let* ((rxv-path (expand-file-name "test.org_archive" org-directory))
(tree (with-current-buffer (find-file-noselect rxv-path) (tree (with-current-buffer (find-file-noselect rxv-path)
(org-element-parse-buffer))) (org-element-parse-buffer)))
@ -3466,6 +3462,11 @@ ARCHIVE-FILE-PATH is the file path to the currently parsed archive file."
contents))) contents)))
(nd/org-element-header-to-sql headlines rxv-path))) (nd/org-element-header-to-sql headlines rxv-path)))
(defun nd/org-archive-to-db ()
"Transfer archive files to sqlite database."
(let ((sql-data (nd/org-sql-extract)))
(nd/sql-insert-multi nd/org-sqlite-db-path sql-data)))
;; these are obviously temporary ;; these are obviously temporary
(setq max-lisp-eval-depth 100000 (setq max-lisp-eval-depth 100000
max-specpdl-size 800000) max-specpdl-size 800000)