al-sql.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; al-sql.el --- Additional functionality for sql stuff
  2. ;; Copyright © 2013–2018 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'sql)
  17. (require 'al-buffer)
  18. ;;;###autoload
  19. (defun al/sql-switch-to-repl ()
  20. "Switch to SQLi buffer."
  21. (interactive)
  22. (unless (and sql-buffer
  23. (buffer-live-p (get-buffer sql-buffer)))
  24. (sql-set-sqli-buffer))
  25. (pop-to-buffer sql-buffer))
  26. ;;;###autoload
  27. (defun al/sql-switch-or-connect (conn)
  28. "Switch to SQLi buffer with connection CONN.
  29. Create it if it does not exist."
  30. (let ((buffer (sql-find-sqli-buffer nil conn)))
  31. (if buffer
  32. (al/display-buffer buffer)
  33. (sql-connect conn))))
  34. ;;; SQL passwords from .authinfo
  35. (require 'auth-source)
  36. ;;;###autoload
  37. (defun al/sql-password-from-auth-source (host &optional user)
  38. "Return sql password from authinfo file by HOST and USER.
  39. Return nil if credentials not found."
  40. (let ((auth (car (auth-source-search :host host :user user))))
  41. (when auth
  42. (let* ((secret (plist-get auth :secret))
  43. (password (if (functionp secret)
  44. (funcall secret)
  45. secret)))
  46. (or password "")))))
  47. ;;; Miscellaneous
  48. (defun al/sql-set-comment-start-skip ()
  49. "Set `comment-start-skip' variable for the current sql buffer."
  50. ;; This variable is not set in `sql-mode'. It is needed for
  51. ;; `comment-search-forward' (which is needed for `mwim').
  52. (setq-local comment-start-skip "--+ *"))
  53. (declare-function sql-mysql-completion-init "sql-completion")
  54. (defun al/sql-completion-setup ()
  55. "Setup `sql-completion' for the current sql interaction buffer."
  56. (and (require 'sql-completion nil t)
  57. (eq major-mode 'sql-interactive-mode)
  58. (eq sql-product 'mysql)
  59. (sql-mysql-completion-init)))
  60. ;;; Log of sql commands
  61. ;; Idea from <http://www.emacswiki.org/emacs/SqlMode>. Add to .emacs:
  62. ;; (add-hook 'sql-interactive-mode-hook 'al/sql-save-history)
  63. (defcustom al/sql-history-dir
  64. (expand-file-name "sql" user-emacs-directory)
  65. "Directory for history of sql commands."
  66. :type 'string
  67. :group 'sql)
  68. ;;;###autoload
  69. (defun al/sql-save-history ()
  70. "Save a history of commands separately for each sql-product.
  71. Use `al/sql-history-dir'."
  72. (if sql-product
  73. (setq-local sql-input-ring-file-name
  74. (expand-file-name (concat (symbol-name sql-product)
  75. "-history.sql")
  76. al/sql-history-dir))
  77. (error "SQL history will not be saved because sql-product is nil")))
  78. ;;; Mode line
  79. (require 'al-mode-line)
  80. (defun al/sql-highlight-product ()
  81. "Add sql product name to `al/mode-info' instead of `mode-name'.
  82. This function is intended to be used as a substitution for
  83. `sql-highlight-product'."
  84. (when (derived-mode-p 'sql-mode)
  85. (set-syntax-table (sql-product-syntax-table))
  86. (sql-product-font-lock nil t))
  87. (and (or (derived-mode-p 'sql-mode)
  88. (derived-mode-p 'sql-interactive-mode))
  89. (setq al/mode-info
  90. (or (sql-get-product-feature sql-product :name)
  91. (symbol-name sql-product)))))
  92. (provide 'al-sql)
  93. ;;; al-sql.el ends here