7575
7676; ;; Code:
7777
78- (require 'org-macs )
7978(require 'ob )
8079(require 'sql )
8180
82-
8381(defvar org-babel-sql-session-start-time )
8482(defvar org-sql-session-preamble
8583 (list
@@ -325,11 +323,9 @@ This function is called by `org-babel-execute-src-block'."
325323 (with-current-buffer (get-buffer-create " *ob-sql-result*" )
326324 (goto-char (point-min ))
327325 ; ; clear the output or prompt and termination
328- (let ((clear-output ; ;(plist-get org-sql-session-clean-output in-engine)
329- (sql-get-product-feature in-engine :org-sql-session-clean-output )
330- ))
331- (while (re-search-forward clear-output nil t )
332- (replace-match " " ))
326+ (let ((clean-output(plist-get org-sql-session-clean-output in-engine)))
327+ (while (re-search-forward clean-output nil t )
328+ (replace-match " " ))
333329 (write-file out-file))))
334330
335331 ; ; else, command line
@@ -471,7 +467,7 @@ SET COLSEP '|'
471467 (goto-char (point-min ))
472468 ; ; clear the output of prompt and termination
473469 (while (re-search-forward
474- ( sql -get-product-feature in-engine : org-sql-session-clean-output )
470+ ( plist -get org-sql-session-clean-output in-engine )
475471 nil t )
476472 (replace-match " " )))
477473
@@ -542,61 +538,38 @@ argument mechanism."
542538PARAMS provides the sql connection parameters for a new or
543539existing SESSION. Clear the intermediate buffer from previous
544540output, and set the process filter. Return the comint process
545- buffer.
546-
547- The buffer naming was shortened from
548- *[session] engine://user@host/database*,
549- that clearly identifies the connexion from Emacs,
550- to *SQL [session]* in order to retrieve a session with its
551- name alone, the other parameters in the header args beeing
552- no longer needed while the session stays open."
541+ buffer."
553542 (sql-set-product in-engine)
554- (let* ( (sql-server (cdr (assoc :dbhost params)))
555- ; ; (sql-port (cdr (assoc :port params)))
556- (sql-database (cdr (assoc :database params)))
557- (sql-user (cdr (assoc :dbuser params)))
558- (sql-password (cdr (assoc :dbpassword params)))
559- (buffer-name (format " %s " (if (string= session " none" ) " "
560- (format " [%s ] " session))))
561- ; ; (buffer-name
562- ; ; (format "%s%s://%s%s/%s"
563- ; ; (if (string= session "none") "" (format "[%s] " session))
564- ; ; engine
565- ; ; (if sql-user (concat sql-user "@") "")
566- ; ; (if sql-server (concat sql-server ":") "")
567- ; ; sql-database))
568- (ob-sql-buffer (format " *SQL: %s * " buffer-name)))
569-
570- (if (org-babel-comint-buffer-livep ob-sql-buffer)
571- (progn ; set again the filter
572- (set-process-filter (get-buffer-process ob-sql-buffer)
573- #'org-sql-session-comint-output-filter )
574- ob-sql-buffer) ; and return the buffer
575- ; ; otherwise initiate a new connection
576- (save-window-excursion
577- (setq ob-sql-buffer ; start the client
578- (org-babel-sql-connect in-engine buffer-name)))
579- (let ((sql-term-proc (get-buffer-process ob-sql-buffer)))
580- (unless sql-term-proc
581- (user-error (format " SQL %s didn't start " in-engine)))
582-
583- ; ; clear the welcoming message out of the output from the
584- ; ; first command, in the case where we forgot quiet mode.
585- ; ; we can't evaluate how long the connection will take
586- ; ; so if quiet mode is off and the connexion takes time
587- ; ; then the welcoming message may show up
588-
589- (with-current-buffer (get-buffer ob-sql-buffer)
590- (let ((preamble (plist-get org-sql-session-preamble in-engine)))
591- (when preamble
592- (process-send-string ob-sql-buffer preamble)
593- (comint-send-input ))))
594- (sleep-for 0.1 ) ; or the result of the preamble will be in the process filter
595- ; ; set the redirection filter
596- (set-process-filter sql-term-proc
597- #'org-sql-session-comint-output-filter )
598- ; ; return that buffer
599- (get-buffer ob-sql-buffer)))))
543+ (let* ((sql-server (cdr (assoc :dbhost params)))
544+ ; ;(sql-port (cdr (assoc :port params)))
545+ (sql-database (cdr (assoc :database params)))
546+ (sql-user (cdr (assoc :dbuser params)))
547+ (sql-password (cdr (assoc :dbpassword params)))
548+ (buffer-name (format " %s " (if (string= session " none" ) " "
549+ (format " [%s ] " session))))
550+ (ob-sql-buffer (get-buffer (format " *SQL: %s * " buffer-name))))
551+
552+ (when (not (org-babel-comint-buffer-livep ob-sql-buffer))
553+ ; ; initiate a new connection
554+ (setq ob-sql-buffer ; start the client
555+ (org-babel-sql-connect in-engine buffer-name))
556+
557+ (let ((sql-term-proc (get-buffer-process ob-sql-buffer)))
558+ (unless sql-term-proc
559+ (user-error (format " SQL %s didn't start " in-engine)))
560+
561+ ; ; run preamble
562+ (with-current-buffer (get-buffer ob-sql-buffer)
563+ (let ((preamble (plist-get org-sql-session-preamble in-engine)))
564+ (when preamble
565+ (process-send-string ob-sql-buffer preamble)
566+ (comint-send-input ))))
567+ (sleep-for 0.1 ))) ; or the result of the preamble will be in the process filter
568+ ; ; set again redirection filter
569+ (set-process-filter (get-buffer-process ob-sql-buffer)
570+ #'org-sql-session-comint-output-filter )
571+ ; ; and return the buffer
572+ ob-sql-buffer))
600573
601574(defun org-babel-sql-connect (&optional engine sql-cnx )
602575 " Run ENGINE interpreter as an inferior process, with SQL-CNX as client buffer.
@@ -623,16 +596,12 @@ should also be prompted."
623596 rpt)
624597
625598 ; ; store the regexp used to clear output (prompt1|indicator|prompt2)
626- (plist-put org-sql-session-clean-output engine
627- (concat " \\ (" prompt-regexp " \\ )"
628- " \\ |\\ (" org-sql-session--batch-terminate " \n \\ )"
629- (when prompt-cont-regexp
630- (concat " \\ |\\ (" prompt-cont-regexp " \\ )" ))))
631- (sql-set-product-feature engine :org-sql-session-clean-output
632- (concat " \\ (" prompt-regexp " \\ )"
633- " \\ |\\ (" org-sql-session--batch-terminate " \n \\ )"
634- (when prompt-cont-regexp
635- (concat " \\ |\\ (" prompt-cont-regexp " \\ )" ))))
599+ (setq org-sql-session-clean-output
600+ (plist-put org-sql-session-clean-output engine
601+ (concat " \\ (" prompt-regexp " \\ )"
602+ " \\ |\\ (" org-sql-session--batch-terminate " \n \\ )"
603+ (when prompt-cont-regexp
604+ (concat " \\ |\\ (" prompt-cont-regexp " \\ )" )))))
636605
637606 ; ; Get credentials.
638607 ; ; either all fields are provided
@@ -644,14 +613,6 @@ should also be prompted."
644613 (sql-get-product-feature engine :sqli-login )))
645614 ; ; depending on client, password is forcefully prompted
646615
647- ; ; Connect to database.
648- ; ; (let ((sql-user (default-value 'sql-user))
649- ; ; (sql-password (default-value 'sql-password))
650- ; ; (sql-server (default-value 'sql-server))
651- ; ; (sql-database (default-value 'sql-database))
652- ; ; (sql-port (default-value 'sql-port))
653- ; ; (default-directory (or sql-default-directory default-directory)))
654-
655616 ; ; The password wallet returns a function
656617 ; ; which supplies the password. (untested)
657618 (when (functionp sql-password)
@@ -662,8 +623,7 @@ should also be prompted."
662623 (let ((previous-session
663624 (get-buffer (format " *SQL: %s * " sql-cnx))))
664625 (when previous-session
665- (with-current-buffer
666- previous-session (erase-buffer )))
626+ (with-current-buffer previous-session (erase-buffer )))
667627
668628 (setq sqli-buffer
669629 (let ((process-environment (copy-sequence process-environment))
@@ -675,7 +635,8 @@ should also be prompted."
675635 engine
676636 (sql-get-product-feature engine :sqli-options )
677637 (format " SQL: %s " sql-cnx))))
678- (setq sql-buffer (buffer-name sqli-buffer))
638+
639+ (setq sql-buffer (buffer-name sqli-buffer)) ; ; set the global var
679640
680641 (setq rpt (sql-make-progress-reporter nil " Login" ))
681642 (with-current-buffer sql-buffer
@@ -694,7 +655,7 @@ should also be prompted."
694655 ; ; no prompt, connexion failed (and process is terminated)
695656 (goto-char (point-max ))
696657 (unless (re-search-backward prompt-regexp 0 t )
697- (user-error " Connection failed" ))) ; is this a _user_ error?
658+ (user-error " Connection failed" )))
698659 ; ;(run-hooks 'sql-login-hook) ; don't
699660 )
700661 (sql-progress-reporter-done rpt)
@@ -712,8 +673,7 @@ Finnally add the termination command."
712673 (let ((commands (split-string str " \n " ))
713674 (terminal-command
714675 (concat " ^\s *"
715- ; ;(sql-get-product-feature sql-product :ob-sql-terminal-command))))
716- (plist-get org-sql-terminal-command-prefix in-engine))))
676+ (plist-get org-sql-terminal-command-prefix in-engine))))
717677 (mapconcat
718678 (lambda (s )
719679 (when (not
0 commit comments