Subject: bug#76025: [PATCH] ob-sql: session + sql.el w/o prompt

From: Phil Estival <[email-masked]>
Date: Tue, 13 May 2025 16:03:49 +0200

* [2025-02-15 16:25 +0000] Ihor Radchenko <[email-masked]>:
> Ihor Radchenko <[email-masked]> writes:
>
> May you please update your latest patch for ob-sql.el, converting it
> into (1) patch to sql.el; (2) patch for ob-sql.el that assumes changes
> to sql.el?

I'm submitting again the patch to sql.el with two additionnal modifications.
This replaces the previous one.

--
Phil Estival

>From d83ee2c93d249673281c401bb78eb0569cd17e07 Mon Sep 17 00:00:00 2001
From: Phil Estival <[email-masked]>
Date: Tue, 13 May 2025 15:39:51 +0200
Subject: [PATCH 1/2] * lisp/progmodes/sql.el: login without prompting

New optional parameter 'no-prompt' to `sql-product-interactive'.
When set with all connection parameters provided, skip the call to
`sql-get-login' to connect without opening the prompt, but still prompt
if any required parameter is missing.  Also skip the prompt for SQLite
when no database name is provided in order to connect to the in-memory
transient database.
---
 lisp/progmodes/sql.el | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index 58fbf3c51e7..af5b1a399cb 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -4524,13 +4524,15 @@ 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 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,8 +4574,16 @@ 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  ; or a parameter missing
+                             (cl-some #'(lambda (s) (or (null s) (string= (format "%s" s) "")))
+                                      (list sql-server sql-user sql-password sql-database))
+                             ;; sqlite allows in-memory db, w/o login
+                             (not (and (or (null sql-database)
+                                           (string= "" 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

>From 18c84feeb1d3f368a7e0e90692efe7a6a5b7c64f Mon Sep 17 00:00:00 2001
From: Phil Estival <[email-masked]>
Date: Tue, 13 May 2025 15:57:24 +0200
Subject: [PATCH 2/2] * lisp/progmodes/sql.el: sql-comint-sqlite allows nil
 database name

Connecting to SQLite with no database name provided (be it nil or empty
string), means to connect to the in-memory transient database.  Since no
parameter can be given, they are made optional.
---
 lisp/progmodes/sql.el | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index af5b1a399cb..296f0336081 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -5075,13 +5075,15 @@ sql-sqlite
   (interactive "P")
   (sql-product-interactive 'sqlite buffer))
 
-(defun sql-comint-sqlite (product options &optional buf-name)
-  "Create comint buffer and connect to SQLite."
+(defun sql-comint-sqlite (product &optional options buf-name)
+  "Create a comint buffer and connect to SQLite (PRODUCT).
+See `sql-comint' for the OPTIONS and BUF-NAME parameters."
   ;; Put all parameters to the program (if defined) in a list and call
   ;; make-comint.
   (let ((params
          (append options
-                 (if (not (string= "" sql-database))
+                 (if (and sql-database
+                          (not (string= "" sql-database)))
                      `(,(expand-file-name sql-database))))))
     (sql-comint product params buf-name)))
 
-- 
2.39.5