Skip to content

Commit 540885d

Browse files
committed
Optionally prefix rows with a header of the column names
Closes #36.
1 parent e5f0928 commit 540885d

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

emacsql-sqlite-builtin.el

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ buffer. This is for debugging purposes."
6060
(cl-defmethod emacsql-send-message
6161
((connection emacsql-sqlite-builtin-connection) message)
6262
(condition-case err
63-
(mapcar (lambda (row)
64-
(mapcar (lambda (col)
65-
(cond ((null col) nil)
66-
((equal col "") "")
67-
((numberp col) col)
68-
(t (read col))))
69-
row))
70-
(sqlite-select (oref connection handle) message nil nil))
63+
(let ((include-header emacsql-include-header))
64+
(mapcar (lambda (row)
65+
(prog1 (mapcar (lambda (col)
66+
(cond (include-header col)
67+
((null col) nil)
68+
((equal col "") "")
69+
((numberp col) col)
70+
(t (read col))))
71+
row)
72+
(setq include-header nil)))
73+
(sqlite-select (oref connection handle) message nil
74+
(and emacsql-include-header 'full))))
7175
((sqlite-error sqlite-locked-error)
7276
(if (stringp (cdr err))
7377
(signal 'emacsql-error (list (cdr err)))

emacsql-sqlite-common.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ Also see http://www.sqlite.org/lang_keywords.html.")
8383
Elements have the form (ERRCODE SYMBOLIC-NAME EMACSQL-ERROR
8484
ERRSTR). Also see https://www.sqlite.org/rescode.html.")
8585

86+
(defconst emacsql-include-header nil
87+
"Whether to include names of columns as an additional row.
88+
Never enable this globally, only let-bind it around calls to `emacsql'.
89+
Currently only supported by `emacsql-sqlite-builtin-connection' and
90+
`emacsql-sqlite-module-connection'.")
91+
8692
;;; Utilities
8793

8894
(defun emacsql-sqlite-open (file &optional debug)

emacsql-sqlite-module.el

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ buffer. This is for debugging purposes."
6464
(cl-defmethod emacsql-send-message
6565
((connection emacsql-sqlite-module-connection) message)
6666
(condition-case err
67-
(let (rows)
67+
(let ((include-header emacsql-include-header)
68+
(rows ()))
6869
(sqlite3-exec (oref connection handle)
6970
message
70-
(lambda (_ row __)
71+
(lambda (_ row header)
72+
(when include-header
73+
(push header rows)
74+
(setq include-header nil))
7175
(push (mapcar (lambda (col)
7276
(cond ((null col) nil)
7377
((equal col "") "")

0 commit comments

Comments
 (0)