HowTo Query Multiple Databases With Emacs Db Mode
Ranked #18,380 in Computers & Electronics, #364,755 overall
Working With Multiple Database Using Emacs
Set The Database Parameters
(defvar db-server "")
(defvar db-database "")
(defvar db-user "")
(defvar db-password "")
(defun db-set-params (server database user password)
(setq db-server server
db-database database
db-user user
db-password password))
Database Static Data
Okay, so the first thing we need is something that maps short aliases to database details - server, database, user and password. I'll store the details in a vector.
Let's say (for the purposes of the example) that all of the accounts have the same password :)
(defconst ro-pwd "********")
(defconst rw-pwd "********")
(defun _db-one-info (region server user password)
(cons region (vector server "orders" user password)))
The Admin and Restricted Users
(defun _db-region-info (region p-server q-server)
(list (_db-one-info (concat region "-ro") p-server "restricted" ro-pwd)
(_db-one-info (concat region "-rw") p-server "admin" rw-pwd)))
Creating The Database Aliases
(defconst *db-info*
(append (_db-region-info "ldn" "LDN_ORDERS_DB" "LDN_ORDERS_QA")
(_db-region-info "fft" "FFT_ORDERS_DB" "FFT_ORDERS_QA")
(_db-region-info "mex" "MEX_ORDERS_DB" "MEX_ORDERS_QA")))
(defconst *db-valid-regions*
(mapcar (lambda (e) (car e)) *db-info*))
(defvar db-region "ldn-ro")
The Resultant Data
(insert (format "%s" *db-info*))
((ldn-ro . [LDN_ORDERS_DB orders restricted ********])
(ldn-rw . [LDN_ORDERS_DB orders admin ********])
(fft-ro . [FFT_ORDERS_DB orders restricted ********])
(fft-rw . [FFT_ORDERS_DB orders admin ********])
(mex-ro . [MEX_ORDERS_DB orders restricted ********])
(mex-rw . [MEX_ORDERS_DB orders admin ********]))
Retrieving The Database Details
(defsubst db-get-param (region ref)
(let ((tuple (assoc region *db-info*)))
(if tuple
(aref (cdr tuple) ref)
"UNKNOWN")))
(defsubst db-get-server (region) (db-get-param region 0))
(defsubst db-get-database (region) (db-get-param region 1))
(defsubst db-get-user (region) (db-get-param region 2))
(defsubst db-get-password (region) (db-get-param region 3))
Setting the Mode Line
(defvar db-mode-string "")
(defun db-set-mode-string ()
(setq db-mode-string (format "(db-region: %s)" db-region)))
mode-line-format
(%e - mode-line-mule-info mode-line-client
mode-line-modified mode-line-remote
mode-line-frame-identification
mode-line-buffer-identification
mode-line-position (vc-mode vc-mode)
mode-line-modes
(which-func-mode ( which-func-format --))
(global-mode-string (-- global-mode-string)) -%-)
As you can see, it is a list with a bunch of variables, some function calls and some special codes beginning with or containing a %. For example -%- produces an infinite number of dashes. Almost all modelines have this as a terminator.
If you add a variable to the mode-line-format, updating the variable automatically updates the modeline.
The Mode Line Update Function
(defun db-update-mode-line ()
(unless (memq 'db-mode-string mode-line-format)
(setq mode-line-format (delete "-%-" mode-line-format))
(setq mode-line-format
(append mode-line-format '(db-mode-string "-%-"))))
(db-set-mode-string)
(force-mode-line-update))
(add-hook 'db-mode-hook 'db-update-mode-line)
Selecting The Database By Its Alias
Finally, the main function - db-set-region
It kills any active database process and sets up the parameters appropriately for next time we connect.
(defun db-set-region (&optional region)
(interactive)
(unless region
(setq region
(ido-completing-read (format "New region (%s): " db-region)
*db-valid-regions*
nil t)))
(if (and (stringp region) (> (length region) 0))
(let ((proc (db-get-process)))
(when proc
(kill-process proc)
(kill-buffer (db-get-buffer)))
(setq db-region region)
(db-set-params (db-get-server db-region)
(db-get-database db-region)
(db-get-user db-region)
(db-get-password db-region))
(db-set-mode-string)
(message "Database region set to %s" region))
(error "Failed to set region")))
Emacs And Other Tech
Fetching RSS feed... please stand byby jareddavison2009
My Emacs Lenses:
Creating An Emacs Command
Emacs Hooks - An Introduction
All About Ediff
more »
- 5 featured lenses
- Winner of 3 trophies!
- Top lens » The Magic of Emacs Comint
Explore related pages
- The Magic of Emacs Comint The Magic of Emacs Comint
- VIM the Powerful Text Editor VIM the Powerful Text Editor
- Keeping Track of Your Websites, Blogs, Passwords & E-Mail Addresses Keeping Track of Your Websites, Blogs, Passwords & E-Mail Addresses
- About Database Support About Database Support
- Ediff - All About The Emacs Diff Comparison Tool Ediff - All About The Emacs Diff Comparison Tool
- Emacs Hooks - An Introduction Emacs Hooks - An Introduction