(defvar my-databases nil "A list of database names used by my-update-database.") (setq my-databases '("square_training")) ;; Sends the current file to a chosen database (defun my-update-database () "Sends the current file to a chosen database." (interactive) (if (not my-sql-profile) (my-sql-set-profile)) (let ((db_name (let ((completion-ignore-case t)) (ido-completing-read "Database: " my-databases)))) (if (not (member db_name my-databases)) (push db_name my-databases)) (if buffer-file-name (progn (shell-command (concat "mysql " db_name " --host=" sql-server " --user=" sql-user " --password=" sql-password " < " buffer-file-name))) (message "No file associated with buffer.")))) (add-hook 'sql-mode-hook (lambda () (local-set-key (kbd "C-c C-b") 'my-update-database))) ;; The current mysql profile (defvar my-sql-profile nil) ;; Links mysql profile names to sql-mode variables (defvar my-sql-vars '(("localhost" . ("username" "password" "host")) ("otherhost" . ("username" "password" "host")))) (defun my-sql-mysql (change) (interactive "P") (my-sql-set-profile change) (sql-mysql)) ;; Runs sql-mysql. When called with a prefix argument (or on first run) it ;; prompts for the sql profile to select. (defun my-sql-set-profile (&optional change) "Sets up the sql-mode variables and then runs sql-mysql." (if (or (not my-sql-profile) change) (let ((completion-ignore-case t) (completion-function 'ido-completing-read)) (let ((choice (funcall completion-function "SQL Profile: " my-sql-vars))) (dolist (profile my-sql-vars) (let ((profile-name (car profile)) (profile-vars (cdr profile))) (if (string-match profile-name choice) (setq sql-user (nth 0 profile-vars) sql-password (nth 1 profile-vars) sql-server (nth 2 profile-vars) my-sql-profile profile-name)))))))) (provide 'my-mysql)