From 5ad113d48f611eb3dded50406a62f72fd9bfda4f Mon Sep 17 00:00:00 2001 From: joddie Date: Tue, 25 Aug 2015 13:48:20 -0700 Subject: [PATCH 1/2] Add `drupal-drush-sql-cli` command --- drupal-mode.el | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/drupal-mode.el b/drupal-mode.el index f6c9476..70e22a9 100644 --- a/drupal-mode.el +++ b/drupal-mode.el @@ -36,6 +36,8 @@ (require 'cl) (require 'php-mode) (require 'format-spec) +(require 'json) +(require 'sql) ;; Silence byte compiler. (defvar css-indent-level) @@ -239,7 +241,8 @@ get better filling in Doxygen comments." (?f . drupal-insert-function) (?m . drupal-module-name) (?e . drupal-drush-php-eval) - (?t . drupal-wrap-string-in-t-function)) + (?t . drupal-wrap-string-in-t-function) + (?s . drupal-drush-sql-cli)) "Map of mnemonic keys and functions for keyboard shortcuts. See `drupal-mode-map'.") @@ -428,6 +431,10 @@ of the project)." [menu-bar drupal cache-clear] '(menu-item "Clear all caches" drupal-drush-cache-clear :enable (and drupal-rootdir drupal-drush-program))) +(define-key drupal-mode-map + [menu-bar drupal sql-cli] + '(menu-item "Open SQL shell" drupal-drush-sql-cli + :enable (and drupal-rootdir drupal-drush-program))) (define-key drupal-mode-map [menu-bar drupal drupal-project drupal-project-bugs] @@ -519,6 +526,50 @@ buffer." (search-forward-regexp "\\(\"\\|'\\)") (insert ")"))))) +(defun drupal-drush-sql-cli () + "Run a SQL shell using \"drush sql-cli\" in a SQL-mode comint buffer." + (interactive) + (require 'sql) + (require 'json) + (let* ((json-object-type 'plist) + (config + (json-read-from-string + (with-temp-buffer + (call-process drupal-drush-program nil t nil + "sql-conf" "--format=json") + (buffer-string))))) + (when (not config) + (error "No Drupal SQL configuration found.")) + (destructuring-bind (&key database driver &allow-other-keys) config + (let ((sql-interactive-product + (drupal--db-driver-to-sql-product driver)) + (start-buffer (current-buffer)) + (sqli-buffer + (make-comint (format "SQL (%s)" database) + drupal-drush-program nil "sql-cli"))) + (with-current-buffer sqli-buffer + (sql-interactive-mode) + (set (make-local-variable 'sql-buffer) + (buffer-name (current-buffer))) + + ;; Set `sql-buffer' in the start buffer + (with-current-buffer start-buffer + (when (derived-mode-p 'sql-mode) + (setq sql-buffer (buffer-name sqli-buffer)) + (run-hooks 'sql-set-sqli-hook))) + + ;; All done. + (run-hooks 'sql-login-hook) + (pop-to-buffer sqli-buffer)))))) + +(defun drupal--db-driver-to-sql-product (driver) + "Translate a Drupal DB driver name into a sql-mode symbol." + (let ((driver (intern driver))) + (cond + ((eq driver 'pgsql) 'postgres) + ((assq driver sql-product-alist) driver) + (t 'ansi)))) + (defvar drupal-form-id-history nil From 72c533c619f273d9539bfb925334088dab12bf1f Mon Sep 17 00:00:00 2001 From: joddie Date: Tue, 25 Aug 2015 14:08:56 -0700 Subject: [PATCH 2/2] Remove redundant `require` calls --- drupal-mode.el | 2 -- 1 file changed, 2 deletions(-) diff --git a/drupal-mode.el b/drupal-mode.el index 70e22a9..6d3c1ed 100644 --- a/drupal-mode.el +++ b/drupal-mode.el @@ -529,8 +529,6 @@ buffer." (defun drupal-drush-sql-cli () "Run a SQL shell using \"drush sql-cli\" in a SQL-mode comint buffer." (interactive) - (require 'sql) - (require 'json) (let* ((json-object-type 'plist) (config (json-read-from-string