Skip to content

Commit 36ea400

Browse files
committed
Merge pull request #65 from joddie/feature/sql-cli
Add `drupal-drush-sql-cli` command
2 parents b59ad24 + 72c533c commit 36ea400

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

drupal-mode.el

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
(require 'cl)
3737
(require 'php-mode)
3838
(require 'format-spec)
39+
(require 'json)
40+
(require 'sql)
3941

4042
;; Silence byte compiler.
4143
(defvar css-indent-level)
@@ -239,7 +241,8 @@ get better filling in Doxygen comments."
239241
(?f . drupal-insert-function)
240242
(?m . drupal-module-name)
241243
(?e . drupal-drush-php-eval)
242-
(?t . drupal-wrap-string-in-t-function))
244+
(?t . drupal-wrap-string-in-t-function)
245+
(?s . drupal-drush-sql-cli))
243246
"Map of mnemonic keys and functions for keyboard shortcuts.
244247
See `drupal-mode-map'.")
245248

@@ -428,6 +431,10 @@ of the project)."
428431
[menu-bar drupal cache-clear]
429432
'(menu-item "Clear all caches" drupal-drush-cache-clear
430433
:enable (and drupal-rootdir drupal-drush-program)))
434+
(define-key drupal-mode-map
435+
[menu-bar drupal sql-cli]
436+
'(menu-item "Open SQL shell" drupal-drush-sql-cli
437+
:enable (and drupal-rootdir drupal-drush-program)))
431438

432439
(define-key drupal-mode-map
433440
[menu-bar drupal drupal-project drupal-project-bugs]
@@ -519,6 +526,48 @@ buffer."
519526
(search-forward-regexp "\\(\"\\|'\\)")
520527
(insert ")")))))
521528

529+
(defun drupal-drush-sql-cli ()
530+
"Run a SQL shell using \"drush sql-cli\" in a SQL-mode comint buffer."
531+
(interactive)
532+
(let* ((json-object-type 'plist)
533+
(config
534+
(json-read-from-string
535+
(with-temp-buffer
536+
(call-process drupal-drush-program nil t nil
537+
"sql-conf" "--format=json")
538+
(buffer-string)))))
539+
(when (not config)
540+
(error "No Drupal SQL configuration found."))
541+
(destructuring-bind (&key database driver &allow-other-keys) config
542+
(let ((sql-interactive-product
543+
(drupal--db-driver-to-sql-product driver))
544+
(start-buffer (current-buffer))
545+
(sqli-buffer
546+
(make-comint (format "SQL (%s)" database)
547+
drupal-drush-program nil "sql-cli")))
548+
(with-current-buffer sqli-buffer
549+
(sql-interactive-mode)
550+
(set (make-local-variable 'sql-buffer)
551+
(buffer-name (current-buffer)))
552+
553+
;; Set `sql-buffer' in the start buffer
554+
(with-current-buffer start-buffer
555+
(when (derived-mode-p 'sql-mode)
556+
(setq sql-buffer (buffer-name sqli-buffer))
557+
(run-hooks 'sql-set-sqli-hook)))
558+
559+
;; All done.
560+
(run-hooks 'sql-login-hook)
561+
(pop-to-buffer sqli-buffer))))))
562+
563+
(defun drupal--db-driver-to-sql-product (driver)
564+
"Translate a Drupal DB driver name into a sql-mode symbol."
565+
(let ((driver (intern driver)))
566+
(cond
567+
((eq driver 'pgsql) 'postgres)
568+
((assq driver sql-product-alist) driver)
569+
(t 'ansi))))
570+
522571

523572

524573
(defvar drupal-form-id-history nil

0 commit comments

Comments
 (0)