From d0fd5825d14c610ac050975d1346076317632413 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Thu, 18 Aug 2022 10:50:44 +0100 Subject: Add sql config --- .emacs.d/lisp/init-sql.el | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .emacs.d/lisp/init-sql.el (limited to '.emacs.d/lisp') diff --git a/.emacs.d/lisp/init-sql.el b/.emacs.d/lisp/init-sql.el new file mode 100644 index 0000000..d7fa708 --- /dev/null +++ b/.emacs.d/lisp/init-sql.el @@ -0,0 +1,56 @@ +;;; init-sql.el --- LSP Configuration File -*- lexical-binding: t -*- +;;; Commentary: +;;; Code: + +(use-package sql + :ensure nil + :config + ;; partially inspired by https://dev.to/viglioni/emacs-as-sql-client-with-lsp-143l (but currently only works for postgres) + (defun add-sql-connection (name product port server user password database extra) + (let ((driver (if (eq product 'postgres) + "postgresql" + (symbol-name product)))) + (when (not (boundp 'sql-connection-alist)) + (setq sql-connection-alist nil)) + (add-to-list 'sql-connection-alist (list name + (list 'sql-product `(quote ,product)) + (list 'sql-port port) + (list 'sql-server server) + (list 'sql-user user) + (list 'sql-password password) + (list 'sql-database (format "%s://%s:%s@%s:%s/%s?%s" product user password server port database extra)))) + (when (not (boundp 'lsp-sqls-connections)) + (setq lsp-sqls-connections nil)) + (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 )))))) + + ;; https://www.emacswiki.org/emacs/SqlMode + (defun my-sql-save-history-hook () + (let ((lval 'sql-input-ring-file-name) + (rval 'sql-product)) + (if (symbol-value rval) + (let ((filename + (concat "~/.emacs.d/sql/" + (symbol-name (symbol-value rval)) + "-history.sql"))) + (set (make-local-variable lval) filename)) + (error + (format "SQL history will not be saved because %s is nil" + (symbol-name rval)))))) + + (add-hook 'sql-interactive-mode-hook 'my-sql-save-history-hook)) + + +(use-package sqlup-mode + :hook + (sql-mode . sqlup-mode) + (sql-interactive-mode . sqlup-mode) + :bind ("C-c c u" . sqlup-capitalize-keywords-in-region)) + +(use-package sql-indent + :commands sqlind-minor-mode) + +(use-package sqlformat + :bind (:map sql-mode-map ("C-c C-f" . sqlformat))) + +(provide 'init-sql) +;;; init-sql.el ends here -- cgit 1.4.1-2-gfad0