I like to use the following approach in REPLs to make the common "g to refresh the buffer" binding start the process when there isn't one running. This initial simpler code only works when you are in the SQLi buffer, and you have exited the REPL already, and wish to create a new connection.
(with-eval-after-load "sql"
(define-key sql-interactive-mode-map "g"
`(menu-item "" my-sqli-restart
:filter ,(lambda (cmd)
(unless (get-buffer-process (current-buffer))
cmd)))))
(defun my-sqli-restart (&optional arg)
"Restart `sql-product-interactive' using existing settings.
With a prefix argument, prompt for the connection settings."
(interactive "P")
(if arg
(call-interactively 'sql-product-interactive)
(cl-letf (((symbol-function 'sql-get-login) #'ignore))
(call-interactively 'sql-product-interactive))))
And here's a variant of my-sqli-restart
which (a) works from a sql-mode
buffer as well (obviously call M-x my-sqli-restart
in that instance, or bind it to a key sequence in sql-mode-map
), and (b) actively kills the inferior process in the SQLi buffer, if it was running.
(defun my-sqli-restart (&optional arg)
"Restart `sql-product-interactive' using existing settings.
With a prefix argument, prompt for the connection settings."
(interactive "P")
(save-window-excursion
(with-current-buffer (cond ((derived-mode-p 'sql-interactive-mode)
(current-buffer))
((and (derived-mode-p 'sql-mode)
(buffer-live-p (get-buffer sql-buffer)))
sql-buffer)
(t (prog1 (current-buffer)
(setq arg t))))
(cond ((sql-buffer-live-p (current-buffer))
(set-process-sentinel
(get-buffer-process (current-buffer))
`(lambda (process signal)
(when (memq (process-status process) '(exit signal))
(my-sqli-restart ',arg))))
(kill-process))
(arg
(call-interactively 'sql-product-interactive))
(t
(cl-letf (((symbol-function 'sql-get-login) #'ignore))
(call-interactively 'sql-product-interactive)))))))