Function: sql-product-interactive

sql-product-interactive is an autoloaded, interactive and byte-compiled function defined in sql.el.gz.

Signature

(sql-product-interactive &optional PRODUCT NEW-NAME)

Documentation

Run PRODUCT interpreter as an inferior process.

If buffer *SQL* exists but no process is running, make a new process. If buffer exists and a process is running, just switch to buffer *SQL*.

To specify the SQL product, prefix the call with C-u (universal-argument). To set the buffer name as well, prefix the call to M-x sql-product-interactive (sql-product-interactive) with C-u (universal-argument) C-u (universal-argument).

(Type C-h m (describe-mode) in the SQL buffer for a list of commands.)

Probably introduced at or before Emacs version 24.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/sql.el.gz
;;; Entry functions for different SQL interpreters.
;;;###autoload
(defun sql-product-interactive (&optional product new-name)
  "Run PRODUCT interpreter as an inferior process.

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

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
\\[universal-argument] \\[universal-argument].

\(Type \\[describe-mode] in the SQL buffer for a list of commands.)"
  (interactive "P")

  ;; Handle universal arguments if specified
  (when (not (or executing-kbd-macro noninteractive))
    (when (>= (prefix-numeric-value product) 16)
      (when (not new-name)
        (setq new-name '(4)))
      (setq product '(4))))

  ;; Get the value of product that we need
  (setq product
        (cond
         ((= (prefix-numeric-value product) 4) ; C-u, prompt for product
          (sql-read-product "SQL product: " sql-product))
         ((assoc product sql-product-alist) ; Product specified
          product)
         (t sql-product)))              ; Default to sql-product

  ;; If we have a product and it has an interactive mode
  (if product
      (when (sql-get-product-feature product :sqli-comint-func)
        ;; If no new name specified or new name in buffer name,
        ;; try to pop to an active SQL interactive for the same product
        (let ((buf (sql-find-sqli-buffer product sql-connection)))
          (if (and buf (or (not new-name)
                           (and (stringp new-name)
                                (string-match-p (regexp-quote new-name) buf))))
              (sql-display-buffer buf)

            ;; We have a new name or sql-buffer doesn't exist or match
            ;; Start by remembering where we start
            (let ((start-buffer (current-buffer))
                  new-sqli-buffer rpt)

              ;; Get credentials.
              (apply #'sql-get-login
                     (sql-get-product-feature product :sqli-login))

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

              (let ((sql-user       (default-value 'sql-user))
                    (sql-password   (default-value 'sql-password))
                    (sql-server     (default-value 'sql-server))
                    (sql-database   (default-value 'sql-database))
                    (sql-port       (default-value 'sql-port))
                    (default-directory
                                    (or sql-default-directory
                                        default-directory)))

                ;; The password wallet returns a function which supplies the password.
                (when (functionp sql-password)
                  (setq sql-password (funcall sql-password)))

                ;; Call the COMINT service
                (funcall (sql-get-product-feature product :sqli-comint-func)
                         product
                         (sql-get-product-feature product :sqli-options)
                         ;; generate a buffer name
                         (cond
                          ((not new-name)
                           (sql-generate-unique-sqli-buffer-name product nil))
                          ((consp new-name)
                           (sql-generate-unique-sqli-buffer-name product
                            (read-string
                             "Buffer name (\"*SQL: XXX*\"; enter `XXX'): "
                             (sql-make-alternate-buffer-name product))))
                          ((stringp new-name)
                           (if (or (string-prefix-p " " new-name)
                                   (string-match-p "\\`[*].*[*]\\'" new-name))
                               new-name
                             (sql-generate-unique-sqli-buffer-name product new-name)))
                          (t
                           (sql-generate-unique-sqli-buffer-name product new-name)))))

              ;; Set SQLi mode.
              (let ((sql-interactive-product product))
                (sql-interactive-mode))

              ;; Set the new buffer name
              (setq new-sqli-buffer (current-buffer))
              (setq-local sql-buffer (buffer-name new-sqli-buffer))

              ;; Set `sql-buffer' in the start buffer
              (with-current-buffer start-buffer
                (when (derived-mode-p 'sql-mode)
                  (setq sql-buffer (buffer-name new-sqli-buffer))
                  (run-hooks 'sql-set-sqli-hook)))

              ;; Make sure the connection is complete
              ;; (Sometimes start up can be slow)
              ;;  and call the login hook
              (let ((proc (get-buffer-process new-sqli-buffer))
                    (secs sql-login-delay)
                    (step 0.3))
                (while (and proc
                            (memq (process-status proc) '(open run))
                            (or (accept-process-output proc step)
                                (<= 0.0 (setq secs (- secs step))))
                            (progn (goto-char (point-max))
                                   (not (re-search-backward sql-prompt-regexp 0 t))))
                  (sql-progress-reporter-update rpt)))

              (goto-char (point-max))
              (when (re-search-backward sql-prompt-regexp nil t)
                (run-hooks 'sql-login-hook))

              ;; All done.
              (sql-progress-reporter-done rpt)
              (goto-char (point-max))
              (let ((sql-display-sqli-buffer-function t))
                (sql-display-buffer new-sqli-buffer))
              (get-buffer new-sqli-buffer)))))
    (user-error "No default SQL product defined: set `sql-product'")))