-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit-sql.el
More file actions
47 lines (43 loc) · 1.73 KB
/
init-sql.el
File metadata and controls
47 lines (43 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(defvar my-sql-query-buffer)
(defun my-sql-query-buffer (arg)
"Open a `sql-mode' buffer which interacts with the current SQLi buffer.
Switches to an existing buffer if possible, otherwise creates a new buffer.
With C-u prefix arg, always creates a new buffer."
(interactive "P")
(let ((sqlibuf (current-buffer)))
(if (null (sql-buffer-live-p sqlibuf))
(error "Buffer %s is not a working SQLi buffer" sqlibuf)
(let ((product sql-product)
(querybuf
(or (and (not (consp arg)) ;; prefix arg
(boundp 'my-sql-query-buffer)
(buffer-live-p (get-buffer my-sql-query-buffer))
(get-buffer my-sql-query-buffer))
(generate-new-buffer
(format "*SQL ctl: %s*" (buffer-name sqlibuf))))))
(setq-local my-sql-query-buffer querybuf)
(pop-to-buffer querybuf '(display-buffer-reuse-window
. ((reusable-frames . visible))))
(unless (eq major-mode 'sql-mode)
(sql-mode)
(setq sql-product product)
(sql-highlight-product)
(setq sql-buffer sqlibuf)
(run-hooks 'sql-set-sqli-hook))))))
(defun org-babel-sql-expand-vars (body vars)
"Expand the variables held in VARS in BODY."
(mapc
(lambda (pair)
(setq body
(replace-regexp-in-string
(format "$%s" (car pair))
(let ((val (cdr pair)))
(if (listp val)
(let ((data-file (org-babel-temp-file "sql-data-")))
(with-temp-file data-file
(insert (orgtbl-to-csv val nil)))
data-file)
(if (stringp val) val (format "%S" val))))
body)))
vars)
body)