init-sql.el 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; init-sql.el --- LSP Configuration File -*- lexical-binding: t -*-
  2. ;;; Commentary:
  3. ;;; Code:
  4. (use-feature sql
  5. :commands add-sql-connection
  6. :config
  7. ;; partially inspired by https://dev.to/viglioni/emacs-as-sql-client-with-lsp-143l (but currently only works for postgres)
  8. (defun add-sql-connection (name product port server user password database extra)
  9. (let ((driver (if (eq product 'postgres)
  10. "postgresql"
  11. (symbol-name product))))
  12. (when (not (boundp 'sql-connection-alist))
  13. (setq sql-connection-alist nil))
  14. (add-to-list 'sql-connection-alist (list name
  15. (list 'sql-product `(quote ,product))
  16. (list 'sql-port port)
  17. (list 'sql-server server)
  18. (list 'sql-user user)
  19. (list 'sql-password password)
  20. (list 'sql-database (format "%s://%s:%s@%s:%s/%s?%s" product user password server port database extra))))
  21. (when (not (boundp 'lsp-sqls-connections))
  22. (setq lsp-sqls-connections nil))
  23. (add-to-list 'lsp-sqls-connections (list (cons 'driver driver) (cons 'dataSourceName (format "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable" server port user password database ))))))
  24. ;; https://www.emacswiki.org/emacs/SqlMode
  25. (defun my-sql-save-history-hook ()
  26. (let ((lval 'sql-input-ring-file-name)
  27. (rval 'sql-product))
  28. (if (symbol-value rval)
  29. (let ((filename
  30. (concat "~/.emacs.d/sql/"
  31. (symbol-name (symbol-value rval))
  32. "-history.sql")))
  33. (set (make-local-variable lval) filename))
  34. (error
  35. (format "SQL history will not be saved because %s is nil"
  36. (symbol-name rval))))))
  37. :hook (sql-interactive-mode . my-sql-save-history-hook))
  38. (use-package sqlup-mode
  39. :diminish
  40. :custom (sqlup-blacklist '("id" "ids"))
  41. :hook
  42. (sql-mode . sqlup-mode)
  43. (sql-interactive-mode . sqlup-mode)
  44. :bind ("C-c c C-u" . sqlup-capitalize-keywords-in-region))
  45. (use-package sql-indent
  46. :commands sqlind-minor-mode)
  47. (use-package sqlformat
  48. :bind (:map sql-mode-map ("C-c C-f" . sqlformat)))
  49. (provide 'init-sql)
  50. ;;; init-sql.el ends here