Subject: bug#76025: [PATCH 1/1] * lisp/progmodes/sql.el: login without prompting

From: Phil Estival <[email-masked]>
Date: Sun, 02 Feb 2025 17:31:08 +0100

Tags: patch

This is a patch of `sql-product-interactive' in order to connect without
prompting for or asking to confirm connection parameters, using ones
already set and stored in `sql-database', `sql-server', -user, etc.

In GNU Emacs 30.0.50
Repository revision: f2bccae22bd47a2e7e0937b78ea06131711b935a
Repository branch: master

>From 5e2e6b85482f0b83867a40db9f47abd98db76da6 Mon Sep 17 00:00:00 2001
From: Phil Estival <[email-masked]>
Date: Wed, 22 Jan 2025 08:00:44 +0100
Subject: [PATCH 1/1] * lisp/progmodes/sql.el: login without prompting

`sql-product-interactive' has a new optional argument 'no-prompt' in
order to connect without opening the prompt by skipping its call to
`sql-get-login' when all connection parameters are set. It will still
prompt if any of these is missing, excepted for sqlite which doesn't
need any to open a transient in-memory database.
---
 lisp/progmodes/sql.el | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index a1c50a06990..79f6cf0e243 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -4524,5 +4524,5 @@ sql-connection-menu-filter

 ;;; Entry functions for different SQL interpreters.
 ;;;###autoload
-(defun sql-product-interactive (&optional product new-name)
-  "Run PRODUCT interpreter as an inferior process.
+(defun sql-product-interactive (&optional product new-name no-prompt)
+  "Run PRODUCT interpreter as an inferior process in a NEW-NAME'd buffer.

 If buffer `*SQL*' exists but no process is running, make a new process.
 If buffer exists and a process is running, just make sure buffer `*SQL*'
 is displayed.

+When NO-PROMPT is set, try to connect without prompting
+with the already supplied parameters.
 To specify the SQL product, prefix the call with
 \\[universal-argument].  To set the buffer name as well, prefix
 the call to \\[sql-product-interactive] with
@@ -4572,9 +4574,15 @@ sql-product-interactive
                   new-sqli-buffer rpt)

               ;; Get credentials.
-              (apply #'sql-get-login
-                     (sql-get-product-feature product :sqli-login))
+              (when (or (not no-prompt) ; default
+                        (and no-prompt
+                             (cl-some #'string-or-null-p
+                                      (list sql-server sql-user sql-password sql-database))
+                             ;; sqlite allows in-memory db, w/o login
+                             (not (and (string-or-null-p sql-database)
+                                       (eq product 'sqlite)))))
+                (apply #'sql-get-login
+                       (sql-get-product-feature product :sqli-login)))

               ;; Connect to database.
               (setq rpt (sql-make-progress-reporter nil "Login"))
--
2.39.5

--
Phil Estival