about summary refs log tree commit diff stats
path: root/.emacs.d/lisp
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2022-08-18 10:50:44 +0100
committerDavid Morgan <djm_uk@protonmail.com>2022-08-18 10:50:44 +0100
commitd0fd5825d14c610ac050975d1346076317632413 (patch)
tree2c5f1efe9d5a261d67ed517c544738d93c686b11 /.emacs.d/lisp
parentf84fa09ff226e4b4eceb62f70cfccf0dcbd09afe (diff)
downloaddotfiles-d0fd5825d14c610ac050975d1346076317632413.tar.gz
Add sql config
Diffstat (limited to '.emacs.d/lisp')
-rw-r--r--.emacs.d/lisp/init-sql.el56
1 files changed, 56 insertions, 0 deletions
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