This repository was archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutl-sql.el
76 lines (59 loc) · 2.31 KB
/
utl-sql.el
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;; utl-sql.el --- Additional functionality for sql stuff
;; Author: Alex Kost <[email protected]>
;; Created: 22 Nov 2013
;;; Code:
(require 'sql)
(require 'cl-macs)
(require 'auth-source)
;;;###autoload
(defun utl-sql-password-from-auth-source (host &optional user)
"Return sql password from authinfo file by HOST and USER.
Return nil if credentials not found."
(let ((auth (car (auth-source-search :host host :user user))))
(when auth
(let* ((secret (plist-get auth :secret))
(password (if (functionp secret)
(funcall secret)
secret)))
(or password "")))))
(declare-function sql-mysql-completion-init "sql-completion")
(defun utl-sql-completion-setup ()
"Setup `sql-completion' for the current sql interaction buffer."
(and (require 'sql-completion nil t)
(eq major-mode 'sql-interactive-mode)
(eq sql-product 'mysql)
(sql-mysql-completion-init)))
;;; Log of sql commands
;; Idea from <http://www.emacswiki.org/emacs/SqlMode>. Add to .emacs:
;; (add-hook 'sql-interactive-mode-hook 'utl-sql-save-history)
(defcustom utl-sql-history-dir
(expand-file-name "sql" user-emacs-directory)
"Directory for history of sql commands."
:type 'string
:group 'sql)
;;;###autoload
(defun utl-sql-save-history ()
"Save a history of commands separately for each sql-product.
Use `utl-sql-history-dir'."
(if sql-product
(setq-local sql-input-ring-file-name
(expand-file-name (concat (symbol-name sql-product)
"-history.sql")
utl-sql-history-dir))
(error "SQL history will not be saved because sql-product is nil")))
;;; Mode line
(defvar utl-mode-info)
(defun utl-sql-highlight-product ()
"Add sql product name to `utl-mode-info' instead of `mode-name'.
This function is intended to be used as a substitution for
`sql-highlight-product'."
(when (derived-mode-p 'sql-mode)
(set-syntax-table (sql-product-syntax-table))
(sql-product-font-lock nil t))
(and (or (derived-mode-p 'sql-mode)
(derived-mode-p 'sql-interactive-mode))
(setq utl-mode-info
(or (sql-get-product-feature sql-product :name)
(symbol-name sql-product)))))
(provide 'utl-sql)
;;; utl-sql.el ends here