Function: sql-save-connection

sql-save-connection is an interactive and byte-compiled function defined in sql.el.gz.

Signature

(sql-save-connection NAME)

Documentation

Captures the connection information of the current SQLi session.

The information is appended to sql-connection-alist and optionally is saved to the user's init file.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/sql.el.gz
(defun sql-save-connection (name)
  "Captures the connection information of the current SQLi session.

The information is appended to `sql-connection-alist' and
optionally is saved to the user's init file."

  (interactive "sNew connection name: ")

  (unless (derived-mode-p 'sql-interactive-mode)
    (user-error "Not in a SQL interactive mode!"))

  ;; Capture the buffer local settings
  (let* ((buf        (current-buffer))
         (connection (buffer-local-value 'sql-connection buf))
         (product    (buffer-local-value 'sql-product    buf))
         (user       (buffer-local-value 'sql-user       buf))
         (database   (buffer-local-value 'sql-database   buf))
         (server     (buffer-local-value 'sql-server     buf))
         (port       (buffer-local-value 'sql-port       buf)))

    (if connection
        (message "This session was started by a connection; it's already been saved.")

      (let ((login (sql-get-product-feature product :sqli-login))
            (alist sql-connection-alist)
            connect)

        ;; Remove the existing connection if the user says so
        (when (and (assoc name alist)
                   (yes-or-no-p (format "Replace connection definition <%s>? " name)))
          (setq alist (assq-delete-all name alist)))

        ;; Add the new connection if it doesn't exist
        (if (assoc name alist)
            (user-error "Connection <%s> already exists" name)
          (setq connect
                (cons name
                      (sql-for-each-login
                       `(product ,@login)
                       (lambda (token _plist)
                           (pcase token
                             ('product  `(sql-product  ',product))
                             ('user     `(sql-user     ,user))
                             ('database `(sql-database ,database))
                             ('server   `(sql-server   ,server))
                             ('port     `(sql-port     ,port)))))))

          (setq alist (append alist (list connect)))

          ;; confirm whether we want to save the connections
          (if (yes-or-no-p "Save the connections for future sessions? ")
              (customize-save-variable 'sql-connection-alist alist)
            (customize-set-variable 'sql-connection-alist alist)))))))