Skip to content

Commit

Permalink
Optionally prefix rows with a header of the column names
Browse files Browse the repository at this point in the history
Closes #36.
  • Loading branch information
tarsius committed Aug 28, 2024
1 parent e5f0928 commit 540885d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 12 additions & 8 deletions emacsql-sqlite-builtin.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ buffer. This is for debugging purposes."
(cl-defmethod emacsql-send-message
((connection emacsql-sqlite-builtin-connection) message)
(condition-case err
(mapcar (lambda (row)
(mapcar (lambda (col)
(cond ((null col) nil)
((equal col "") "")
((numberp col) col)
(t (read col))))
row))
(sqlite-select (oref connection handle) message nil nil))
(let ((include-header emacsql-include-header))
(mapcar (lambda (row)
(prog1 (mapcar (lambda (col)
(cond (include-header col)
((null col) nil)
((equal col "") "")
((numberp col) col)
(t (read col))))
row)
(setq include-header nil)))
(sqlite-select (oref connection handle) message nil
(and emacsql-include-header 'full))))
((sqlite-error sqlite-locked-error)
(if (stringp (cdr err))
(signal 'emacsql-error (list (cdr err)))
Expand Down
6 changes: 6 additions & 0 deletions emacsql-sqlite-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ Also see http://www.sqlite.org/lang_keywords.html.")
Elements have the form (ERRCODE SYMBOLIC-NAME EMACSQL-ERROR
ERRSTR). Also see https://www.sqlite.org/rescode.html.")

(defconst emacsql-include-header nil
"Whether to include names of columns as an additional row.
Never enable this globally, only let-bind it around calls to `emacsql'.
Currently only supported by `emacsql-sqlite-builtin-connection' and
`emacsql-sqlite-module-connection'.")

;;; Utilities

(defun emacsql-sqlite-open (file &optional debug)
Expand Down
8 changes: 6 additions & 2 deletions emacsql-sqlite-module.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ buffer. This is for debugging purposes."
(cl-defmethod emacsql-send-message
((connection emacsql-sqlite-module-connection) message)
(condition-case err
(let (rows)
(let ((include-header emacsql-include-header)
(rows ()))
(sqlite3-exec (oref connection handle)
message
(lambda (_ row __)
(lambda (_ row header)
(when include-header
(push header rows)
(setq include-header nil))
(push (mapcar (lambda (col)
(cond ((null col) nil)
((equal col "") "")
Expand Down

0 comments on commit 540885d

Please sign in to comment.